Skip to content

Commit

Permalink
Upintegrate changes from the OpenMRNIDF repository (#771)
Browse files Browse the repository at this point in the history
This PR pulls in changes that happened in the OpenMRNIDF repository. Most of these were authored by Mike Dunston.

- All instances of ifdef ESP32 replaced with ESP_PLATFORM
- Removed conditional code for supporting ESP-IDF v3.x and v4.x
  Only ESP-IDF v5 and up is now supported
- new feature for OPENMRN_HAVE_SOCKET_FSTAT
- fixed compile errors around spiram
- refactored definition of ADC pin into a separate class in a separate file
- fixes around printf PRIu32.
- do not print error messages on CAN overflow
- fix to not start the hub twice in the wifi manager
- Remove deprecated hardware can driver
- Adds I2C driver written by Mike Dunston for the ESP32.

===

* Upintegrate changes from OpenMRNIDF.

- All instances of ifdef ESP32 replaced with ESP_PLATFORM
- Removed conditional code for supporting ESP-IDF v3.x and v4.x
  Only ESP-IDF v5 and up is now supported
- new feature for OPENMRN_HAVE_SOCKET_FSTAT
- fixed compile errors around spiram
- refactored definition of ADC pin.
- fixes around printf PRIu32.
- do not print error messages on CAN overflow
- fix to not start the hub twice in the wifi manager

* Fix compile error.

* Remove deprecated hardware can driver.

* Adds I2C driver written by Mike Dunston for the ESP32.

* Adds refactored Adc Input class.

* Fix comments.
  • Loading branch information
balazsracz authored Feb 4, 2024
1 parent f0a0091 commit 8c7cc44
Show file tree
Hide file tree
Showing 45 changed files with 1,219 additions and 1,057 deletions.
4 changes: 2 additions & 2 deletions arduino/OpenMRNLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ OpenMRN::OpenMRN(openlcb::NodeID node_id)
init(node_id);
}

#ifdef ESP32
#ifdef ESP_PLATFORM
extern "C" {

#ifndef OPENMRN_EXCLUDE_REBOOT_IMPL
Expand All @@ -62,6 +62,6 @@ ssize_t os_get_free_heap()
#endif // OPENMRN_EXCLUDE_FREE_HEAP_IMPL

}
#endif // ESP32
#endif // ESP_PLATFORM

} // namespace openmrn_arduino
14 changes: 1 addition & 13 deletions arduino/OpenMRNLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@
#include "utils/logging.h"
#include "utils/Uninitialized.hxx"

#if defined(ESP32)
#if defined(ESP_PLATFORM)

#include <esp_idf_version.h>
#include <esp_task.h>
#include <esp_task_wdt.h>

Expand All @@ -70,8 +69,6 @@ constexpr UBaseType_t OPENMRN_TASK_PRIORITY = ESP_TASK_TCPIP_PRIO - 1;
#include "freertos_drivers/esp32/Esp32Gpio.hxx"
#include "freertos_drivers/esp32/Esp32SocInfo.hxx"

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4,3,0)

// If we are using ESP-IDF v4.3 (or later) enable the Esp32Ledc API.
#include "freertos_drivers/esp32/Esp32Ledc.hxx"

Expand All @@ -92,15 +89,6 @@ constexpr UBaseType_t OPENMRN_TASK_PRIORITY = ESP_TASK_TCPIP_PRIO - 1;

#endif // NOT ESP32-H2 and NOT ESP32-C2

#endif // IDF v4.3+

#if defined(CONFIG_IDF_TARGET_ESP32)
// Note: This code is deprecated in favor of the TWAI interface which exposes
// both select() and fnctl() interfaces. Support for this may be removed in the
// future.
#include "freertos_drivers/esp32/Esp32HardwareCanAdapter.hxx"
#endif // ESP32 only

#include "freertos_drivers/esp32/Esp32HardwareSerialAdapter.hxx"
#include "freertos_drivers/esp32/Esp32WiFiManager.hxx"

Expand Down
2 changes: 1 addition & 1 deletion arduino/libify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ copy_file . arduino/{library.json,library.properties,keywords.txt,README.md,LICE
copy_dir . arduino/examples

copy_file src arduino/OpenMRNLite.{h,cpp} arduino/CDIXMLGenerator.hxx \
include/{can_frame.h,nmranet_config.h,openmrn_features.h} \
include/{can_frame.h,nmranet_config.h,openmrn_features.h,i2c.h,i2c-dev.h} \
include/freertos/{bootloader_hal.h,can_ioctl.h,endian.h,freertos_includes.h,stropts.h} \
include/freertos_select/ifaddrs.h

Expand Down
2 changes: 1 addition & 1 deletion include/can_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

#elif defined (__nuttx__) || defined (__FreeRTOS__) || defined (__MACH__) || \
defined (__WIN32__) || defined (__EMSCRIPTEN__) || defined (ESP_NONOS) || \
defined (ARDUINO) || defined (ESP32)
defined (ARDUINO) || defined (ESP_PLATFORM)
#include <stdint.h>

struct can_frame
Expand Down
2 changes: 1 addition & 1 deletion include/freertos/can_ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <stdint.h>
#ifdef __FreeRTOS__
#include "freertos/stropts.h"
#elif defined(ESP32)
#elif defined(ESP_PLATFORM)
#include "stropts.h"
#endif

Expand Down
54 changes: 53 additions & 1 deletion include/freertos/freertos_includes.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
#ifdef ESP32
/** \copyright
* Copyright (c) 2019, Balazs Racz
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \file freertos_includes.h
* This file simplifies the include path for FreeRTOS header files between
* platforms.
*
* @author Balazs Racz
* @date 2 March 2019
*/

#ifdef ESP_PLATFORM

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include "sdkconfig.h"

#define NSEC_TO_TICK(ns) \
(((((long long)(ns)) / 1000 * configTICK_RATE_HZ) + 999999) / 1000000)

// IDF v5.0 has introduced a configuration option (disabled by default) which
// enables the usage of legacy FreeRTOS data types, if that configuration option
// is *NOT* selected *AND* IDF v5.0+ is in use we need to add compatibility
// defines in order to compile OpenMRN successfully.
#if !defined(CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY)

// used in os/os.c and os/os.h
#define portTickType TickType_t
#define xTaskHandle TaskHandle_t
#define xQueueHandle QueueHandle_t
#define xSemaphoreHandle SemaphoreHandle_t

// used in freertos_drivers/arduino/CpuLoad.hxx and os/os.c
#define pcTaskGetTaskName pcTaskGetName

#endif // IDF v5.0+ and !CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY

#else

#include <FreeRTOS.h>
Expand Down
4 changes: 2 additions & 2 deletions include/freertos/stropts.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
extern "C" {
#endif

#if defined(ESP32)
#if defined(ESP_PLATFORM)
#include <sys/ioctl.h>
#else
/** Request and ioctl transaction
Expand All @@ -47,7 +47,7 @@ extern "C" {
* @param ... key data (as a pointer or unsigned long type)
*/
int ioctl(int fd, unsigned long int key, ...);
#endif // ESP32
#endif // ESP_PLATFORM

/** ioctl key value for operation (not read or write) */
#define IOC_NONE 0U
Expand Down
2 changes: 1 addition & 1 deletion include/i2c-dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#if defined (__linux__)
#include <linux/i2c-dev.h>
#elif defined (__FreeRTOS__)
#elif defined (__FreeRTOS__) || defined(ESP_PLATFORM)
#include <stdint.h>
/** magic number for this driver's ioctl calls */
#define I2C_MAGIC ('i')
Expand Down
2 changes: 1 addition & 1 deletion include/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#if defined (__linux__)
#include <linux/i2c.h>
#elif defined (__FreeRTOS__)
#elif defined (__FreeRTOS__) || defined(ESP_PLATFORM)
#include <stdint.h>
/** Used in @ref i2c_rdwr_ioctl_data to describe a transaction segment. */
struct i2c_msg
Expand Down
33 changes: 20 additions & 13 deletions include/openmrn_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
#ifndef _INCLUDE_OPENMRN_FEATURES_
#define _INCLUDE_OPENMRN_FEATURES_

#ifdef ESP32
#ifdef ESP_PLATFORM
#include <esp_idf_version.h>
#else
#define ESP_IDF_VERSION 0
#define ESP_IDF_VERSION_VAL(a,b,c) 1
#endif // ESP32
#endif // ESP_PLATFORM

#ifdef __FreeRTOS__
/// Compiles the FreeRTOS event group based ::select() implementation.
Expand All @@ -54,43 +54,50 @@
#define OPENMRN_FEATURE_REENT 1
#endif

#if defined(__FreeRTOS__) || ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4,3,0)
#if defined(__FreeRTOS__) || defined(ESP_PLATFORM)
// Note: this is not using OPENMRN_FEATURE_DEVICE_SELECT due to other usages
// of that macro which may conflict with the ESP32 version of this feature.
/// Adds support for FD based CAN interfaces.
#define OPENMRN_FEATURE_FD_CAN_DEVICE 1
#endif

#if defined(__linux__) || defined(__MACH__) || defined(__WINNT__) || defined(ESP32) || defined(OPENMRN_FEATURE_DEVTAB)
#if defined(__linux__) || defined(__MACH__) || defined(__WINNT__) || \
defined(ESP_PLATFORM) || defined(OPENMRN_FEATURE_DEVTAB)
/// Enables the code using ::open ::close ::read ::write for non-volatile
/// storage, FileMemorySpace for the configuration space, and
/// SNIP_DYNAMIC_FILE_NAME for node names.
#define OPENMRN_HAVE_POSIX_FD 1
#endif

#if !defined(ESP_PLATFORM)
/// Enables the code using ::fstat to confirm if the file handle is a socket.
#define OPENMRN_HAVE_SOCKET_FSTAT 1
#endif

/// @todo this should probably be a whitelist: __linux__ || __MACH__.
#if !defined(__FreeRTOS__) && !defined(__WINNT__) && !defined(ESP32) && \
#if !defined(__FreeRTOS__) && !defined(__WINNT__) && !defined(ESP_PLATFORM) && \
!defined(ARDUINO) && !defined(ESP_NONOS)
/// Uses ::pselect in the Executor for sleep and pkill for waking up.
#define OPENMRN_HAVE_PSELECT 1
#endif

#if defined(__WINNT__) || defined(ESP32) || defined(ESP_NONOS)
#if defined(__WINNT__) || defined(ESP_PLATFORM) || defined(ESP_NONOS)
/// Uses ::select in the executor to sleep (unsure how wakeup is handled)
#define OPENMRN_HAVE_SELECT 1
#endif

#if defined(OPENMRN_HAVE_SELECT) || defined(OPENMRN_HAVE_PSELECT) || defined(OPENMRN_FEATURE_DEVICE_SELECT)
#if defined(OPENMRN_HAVE_SELECT) || defined(OPENMRN_HAVE_PSELECT) || \
defined(OPENMRN_FEATURE_DEVICE_SELECT)
#define OPENMRN_FEATURE_EXECUTOR_SELECT 1
#endif

#if (defined(ARDUINO) && !defined(ESP32)) || defined(ESP_NONOS) || \
#if (defined(ARDUINO) && !defined(ESP_PLATFORM)) || defined(ESP_NONOS) || \
defined(__EMSCRIPTEN__)
/// A loop() function is calling the executor in the single-threaded OS context.
#define OPENMRN_FEATURE_SINGLE_THREADED 1
#endif

#if defined(__FreeRTOS__) || defined(ESP32)
#if defined(__FreeRTOS__) || defined(ESP_PLATFORM)
/// Use os_mutex_... implementation based on FreeRTOS mutex and semaphores.
#define OPENMRN_FEATURE_MUTEX_FREERTOS 1

Expand All @@ -111,7 +118,7 @@
#define OPENMRN_FEATURE_SEM_TIMEDWAIT 1
#endif

#if defined(__FreeRTOS__) || defined(ESP32)
#if defined(__FreeRTOS__) || defined(ESP_PLATFORM)
/// Use FreeRTOS implementation for os_thread_create and keeping a list of live
/// threads.
#define OPENMRN_FEATURE_THREAD_FREERTOS 1
Expand All @@ -131,7 +138,7 @@
#endif

#if defined(__linux__) || defined(__MACH__) || defined(__FreeRTOS__) || \
defined(ESP32)
defined(ESP_PLATFORM)
/// Compiles support for BSD sockets API.
#define OPENMRN_FEATURE_BSD_SOCKETS 1

Expand All @@ -141,7 +148,7 @@
#define OPENMRN_HAVE_BSD_SOCKETS_RX_TIMEOUT 1
#endif

#if !defined(__FreeRTOS__) && !defined(ESP32)
#if !defined(__FreeRTOS__) && !defined(ESP_PLATFORM)
/// Compiles support for calling getsockname when binding a socket to a port
/// when listening for incoming connections.
#define OPENMRN_HAVE_BSD_SOCKETS_GETSOCKNAME 1
Expand All @@ -157,7 +164,7 @@
#define OPENMRN_FEATURE_BSD_SOCKETS_IGNORE_SIGPIPE 1
#endif

#if defined(__linux__) || defined(__MACH__) || defined(ESP32)
#if defined(__linux__) || defined(__MACH__) || defined(ESP_PLATFORM)
/// Compiles support for reporting EOF as an error for read/write.
#define OPENMRN_FEATURE_BSD_SOCKETS_REPORT_EOF_ERROR 1
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/executor/Executor.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public:
*/
void add_from_isr(Executable *msg, unsigned priority = UINT_MAX) override
{
#ifdef ESP32
#ifdef ESP_PLATFORM
// On the ESP32 we need to call insert instead of insert_locked to
// ensure that all code paths lock the queue for consistency since
// this code path is not guaranteed to be protected by a critical
Expand All @@ -333,7 +333,7 @@ public:
#else
queue_.insert_locked(
msg, priority >= NUM_PRIO ? NUM_PRIO - 1 : priority);
#endif // ESP32
#endif // ESP_PLATFORM
selectHelper_.wakeup_from_isr();
}
#endif // OPENMRN_FEATURE_RTOS_FROM_ISR
Expand Down
13 changes: 5 additions & 8 deletions src/executor/StateFlow.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ protected:
Pool *p = pool;
if (!p)
{
HASSERT(target_flow != nullptr);
p = target_flow->pool();
}
LOG(VERBOSE, "allocate from pool %p, main pool %p", p, mainBufferPool);
Expand Down Expand Up @@ -736,14 +737,12 @@ protected:
*/
Action listen_and_call(StateFlowSelectHelper *helper, int fd, Callback c)
{
// ESP-IDF does not implement fstat for the LwIP VFS layer
// https://github.com/espressif/esp-idf/issues/7198
#ifndef ESP32
#if OPENMRN_HAVE_SOCKET_FSTAT
// verify that the fd is a socket
struct stat stat;
fstat(fd, &stat);
HASSERT(S_ISSOCK(stat.st_mode));
#endif // ESP32
#endif // OPENMRN_HAVE_SOCKET_FSTAT

helper->reset(Selectable::READ, fd, Selectable::MAX_PRIO);
helper->set_wakeup(this);
Expand All @@ -759,14 +758,12 @@ protected:
*/
Action connect_and_call(StateFlowSelectHelper *helper, int fd, Callback c)
{
// ESP-IDF does not implement fstat for the LwIP VFS layer
// https://github.com/espressif/esp-idf/issues/7198
#ifndef ESP32
#if OPENMRN_HAVE_SOCKET_FSTAT
// verify that the fd is a socket
struct stat stat;
fstat(fd, &stat);
HASSERT(S_ISSOCK(stat.st_mode));
#endif // ESP32
#endif // OPENMRN_HAVE_SOCKET_FSTAT

helper->reset(Selectable::WRITE, fd, Selectable::MAX_PRIO);
helper->set_wakeup(this);
Expand Down
2 changes: 1 addition & 1 deletion src/freertos_drivers/common/libatomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#include <stdint.h>

#if defined(STM32F0xx) || (!defined(ARDUINO) && !defined(ESP32))
#if defined(STM32F0xx) || (!defined(ARDUINO) && !defined(ESP_PLATFORM))
// On Cortex-M0 the only way to do atomic operation is to disable interrupts.

/// Disables interrupts and saves the interrupt enable flag in a register.
Expand Down
Loading

0 comments on commit 8c7cc44

Please sign in to comment.