diff --git a/.gitmodules b/.gitmodules index b422b45..0a934ed 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "src/LoRaMac-node"] path = src/LoRaMac-node - url = https://github.com/Lora-net/LoRaMac-node.git + url = https://github.com/boston-engineering/LoRaMac-node.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 3da2380..f980f89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,8 +36,10 @@ set(COMPONENT_SRCS "src/LoRaMac-node/src/apps/LoRaMac/common/LmHandler/packages/LmhpClockSync.c" "src/LoRaMac-node/src/apps/LoRaMac/common/LmHandler/packages/LmhpCompliance.c" "src/LoRaMac-node/src/mac/region/Region.c" +"src/LoRaMac-node/src/mac/region/RegionBaseUS.c" "src/LoRaMac-node/src/mac/region/RegionCommon.c" "src/LoRaMac-node/src/mac/region/RegionEU868.c" +"src/LoRaMac-node/src/mac/region/RegionUS915.c" ) # idf.py build 2>&1 | tee LOG | grep "undefined reference" | sed -e 's/.*reference to//' | sort | uniq @@ -54,7 +56,14 @@ set(COMPONENT_ADD_INCLUDEDIRS "src/LoRaMac-node/src/peripherals/soft-se" ) -register_component() +idf_component_register(SRCS ${COMPONENT_SRCS} + INCLUDE_DIRS ${COMPONENT_ADD_INCLUDEDIRS} + REQUIRES ${COMPONENT_REQUIRES} +) -add_definitions(-DREGION_EU868=1) +include_directories( + $ENV{IDF_PATH}/components/driver/gpio/include + $ENV{IDF_PATH}/components/driver/spi/include + $ENV{IDF_PATH}/components/esp_timer/include +) add_definitions(-DSOFT_SE) diff --git a/Kconfig b/Kconfig new file mode 100644 index 0000000..c170da5 --- /dev/null +++ b/Kconfig @@ -0,0 +1,47 @@ +menu "SX126x Configuration" + + config SX126X_SPI_MISO_GPIO + int "SX126x SPI MISO GPIO" + help + Set the GPIO number used for SPI MISO. + + config SX126X_SPI_MOSI_GPIO + int "SX126x SPI MOSI GPIO" + help + Set the GPIO number used for SPI MOSI. + + config SX126X_SPI_SCLK_GPIO + int "SX126x SPI SCLK GPIO" + help + Set the GPIO number used for SPI SCLK. + + config SX126X_SPI_CS_GPIO + int "SX126x SPI CS GPIO" + help + Set the GPIO number used for SPI CS. + + config SX126X_RESET_GPIO + int "SX126x RESET GPIO" + help + Set the GPIO number used for RESET. + + config SX126X_IRQ_DIO1_GPIO + int "SX126x IRQ (DIO1) GPIO" + help + Set the GPIO number used for IRQ (DIO1). + + config SX126X_BUSY_GPIO + int "SX126x BUSY GPIO" + help + Set the GPIO number used for BUSY. + + config SX126X_SPI_HOST + int "SX126x SPI HOST" + default 1 + help + The SPI peripheral used to communicate with the SX1262. + 0 = SPI1_HOST + 1 = SPI2_HOST + 2 = SPI3_HOST + +endmenu \ No newline at end of file diff --git a/README.md b/README.md index 384e180..5187c1a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,15 @@ # LoRaMac-node-esp32 -ESP32 port of LoRaMac-node +#### ESP32 port of LoRaMac-node +Creates a Semtech SX126x compatible ESP32 component that pulls in all LoRaMac-node sources, allowing for a custom ESP32 board definition to be used. -This is work in progress - not usable yet +## Compatibility +This library is written for and is currently only compatible with Semtech SX126x LoRaWAN chips. + +## Configuration +- Running menuconfig with LoRaMac-node-esp32 added as a component will allow for configuration of SPI pins and the host to be used. +- Region(s) of operation must be selected by pulling in one of the pre-processor definitions used by LoRaMac-node, e.g. `REGION_US915`. + - Regions are listed in `src/LoRaMac-node/src/mac/region/Region.h` + +## Notes +- `SX126xWaitOnBusy()` indefinitely blocks with a busy wait, waiting for a low BUSY signal +- This is work in progress - not usable yet diff --git a/idf_component.yml b/idf_component.yml new file mode 100644 index 0000000..d9e375f --- /dev/null +++ b/idf_component.yml @@ -0,0 +1,4 @@ +description: Creates an ESP32 component to allow ESP32 board compatibility with the LoRaMac-node stack. +dependencies: + idf: + version: ">=5.0" diff --git a/src/LoRaMac-node b/src/LoRaMac-node index 2bf36bd..5aa59d0 160000 --- a/src/LoRaMac-node +++ b/src/LoRaMac-node @@ -1 +1 @@ -Subproject commit 2bf36bde72f68257eb96b5c00900619546bedca8 +Subproject commit 5aa59d026a883e00f3226d74101b2a73735ca99e diff --git a/src/board/generic_esp32/generic-esp32-board.c b/src/board/generic_esp32/generic-esp32-board.c index 478ba6a..9ac7424 100644 --- a/src/board/generic_esp32/generic-esp32-board.c +++ b/src/board/generic_esp32/generic-esp32-board.c @@ -1,30 +1,133 @@ - +/* In this implementation, milliseconds units are used throughout. Ticks are milliseconds. */ #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "esp_err.h" #include "esp_log.h" +#include "esp_timer.h" + +#include "driver/gpio.h" +#include "driver/spi_common.h" +#include "driver/spi_master.h" #include "freertos/task.h" // from LoRaMac #include "board.h" +#include "timer.h" // ANSI #include - +#include + +#define ESP_INTR_FLAG_DEFAULT 0 + +#define HZ_PER_KHZ 1000 +#define KHZ_PER_MHZ 1000 +#define HZ_PER_MHZ (HZ_PER_KHZ * KHZ_PER_MHZ) + +#define MS_PER_S 1000 +#define US_PER_MS 1000 +#define US_PER_S (MS_PER_S * US_PER_MS) + +#define SX126X_MAX_SPI_CLOCK_SPEED_MHZ 16 +#define SX126X_NUM_COMMAND_BITS 8 +#define SX126X_NUM_REGISTER_ADDRESS_BITS 16 +#define SX126X_NUM_COMMAND_ADDRESS_BITS 0 +#define SX126X_NUM_BUFFER_OFFSET_BITS 8 + +typedef struct spi_s { + spi_device_handle_t handle; + gpio_num_t miso; + gpio_num_t mosi; + gpio_num_t sclk; + gpio_num_t cs; + gpio_num_t reset; + gpio_num_t irq_dio1; +#if defined(SX1261MBXBAS) || defined(SX1262MBXCAS) || defined(SX1262MBXDAS) + gpio_num_t busy; +#endif +} spi_c; + +static spi_c lora_spi; static const char *TAG = "ESP32Board"; static portMUX_TYPE my_spinlock = portMUX_INITIALIZER_UNLOCKED; +static esp_timer_handle_t irq_timer_handle; +static uint32_t rtc_timer_context; +static uint32_t alarm_start_time; + +static void IrqTimerExpiryCallback(void* arg) { + TimerIrqHandler(); +} + void BoardInitMcu( void ) { } void BoardInitPeriph(void) { - + // Create timer + const esp_timer_create_args_t timer_args = { + .callback = &IrqTimerExpiryCallback, + .name = "SX126X IRQ Timer" + }; + ESP_ERROR_CHECK(esp_timer_create(&timer_args, &irq_timer_handle)); + + lora_spi.miso = CONFIG_SX126X_SPI_MISO_GPIO; + lora_spi.mosi = CONFIG_SX126X_SPI_MOSI_GPIO; + lora_spi.sclk = CONFIG_SX126X_SPI_SCLK_GPIO; + lora_spi.cs = CONFIG_SX126X_SPI_CS_GPIO; + lora_spi.reset = CONFIG_SX126X_RESET_GPIO; + lora_spi.irq_dio1 = CONFIG_SX126X_IRQ_DIO1_GPIO; +#if defined(SX1261MBXBAS) || defined(SX1262MBXCAS) || defined(SX1262MBXDAS) + lora_spi.busy = CONFIG_SX126X_BUSY_GPIO; + + gpio_config_t busy_conf = { + (1ULL<