13 Commits
2.2.2 ... 2.3.2

49 changed files with 1095 additions and 586 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ sdkconfig
sdkconfig.old
dev_keys.txt
ttn-esp32
.vscode/

View File

@ -28,7 +28,7 @@
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],

14
CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
set(COMPONENT_SRCDIRS
"src"
"src/aes"
"src/hal"
"src/lmic"
)
set(COMPONENT_ADD_INCLUDEDIRS
"include"
)
set(COMPONENT_REQUIRES
nvs_flash
)
register_component()

23
Kconfig
View File

@ -14,16 +14,19 @@ config TTN_LORA_FREQ_EU_868
bool "Europe (868 MHz)"
config TTN_LORA_FREQ_US_915
bool "North America (915 Mhz)"
bool "North America (915 MHz)"
config TTN_LORA_FREQ_AU_921
bool "Australia (921 Mhz)"
bool "Australia (921 MHz)"
config TTN_LORA_FREQ_AS_923
bool "Asia (923 Mhz)"
bool "Asia (923 MHz)"
config TTN_LORA_FREQ_AS_923_JP
bool "Asia, region Japan (923 MHz)"
config TTN_LORA_FREQ_IN_866
bool "India (866 Mhz)"
bool "India (866 MHz)"
endchoice
@ -79,22 +82,22 @@ config TTN_BG_TASK_PRIO
choice TTN_PROVISION_UART
prompt "UART for provisioning"
prompt "AT commands"
default TTN_PROVISION_UART_DEFAULT
help
Select whether to use UART for listening for provisioning commands.
Select whether to listen on UART for AT commands.
- Default is to use UART0 on pins GPIO1(TX) and GPIO3(RX).
- If "Custom" is selected, UART0 or UART1 can be chosen,
and any pins can be selected.
- If "None" is selected, the feature is not available.
- If "None" is selected, AT commands are not available.
config TTN_PROVISION_UART_DEFAULT
bool "Default: UART0, TX=GPIO1, RX=GPIO3, 115,200 baud"
bool "Enabled - default settings: UART0, TX=GPIO1, RX=GPIO3, 115,200 baud"
config TTN_PROVISION_UART_CUSTOM
bool "Custom"
bool "Enabled - custom UART settings"
config TTN_PROVISION_UART_NONE
bool "None"
bool "Disabled"
endchoice
choice TTN_PROVISION_UART_NUM

View File

@ -20,6 +20,9 @@
#define CFG_au921 1
#elif defined(CONFIG_TTN_LORA_FREQ_AS_923)
#define CFG_as923 1
#elif defined(CONFIG_TTN_LORA_FREQ_AS_923_JP)
#define CFG_as923 1
#define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP
#elif defined(CONFIG_TTN_LORA_FREQ_IN_866)
#define CFG_in866 1
#else
@ -55,6 +58,14 @@
#error TTN timer must be configured using 'make menuconfig'
#endif
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#define TTN_HAS_AT_COMMANDS 1
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
#define TTN_CONFIG_UART 1
#endif
#endif
// 16 μs per tick
// LMIC requires ticks to be 15.5μs - 100 μs long
#define US_PER_OSTICK 16

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(hello_world)

View File

@ -1,3 +1,5 @@
PROJECT_NAME := hello_world
EXTRA_COMPONENT_DIRS := $(abspath ../..)
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,4 @@
set(COMPONENT_SRCS "main.cpp")
set(COMPONENT_ADD_INCLUDEDIRS "")
register_component()

View File

@ -89,7 +89,7 @@ extern "C" void app_main(void)
if (ttn.join())
{
printf("Joined.\n");
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
}
else
{

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mac_address)

View File

@ -1,3 +1,5 @@
PROJECT_NAME := mac_address
EXTRA_COMPONENT_DIRS := $(abspath ../..)
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,4 @@
set(COMPONENT_SRCS "main.cpp")
set(COMPONENT_ADD_INCLUDEDIRS "")
register_component()

View File

@ -87,7 +87,7 @@ extern "C" void app_main(void)
if (ttn.join())
{
printf("Joined.\n");
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
}
else
{

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(provisioning)

View File

@ -1,3 +1,5 @@
PROJECT_NAME := provisioning
EXTRA_COMPONENT_DIRS := $(abspath ../..)
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,4 @@
set(COMPONENT_SRCS "main.cpp")
set(COMPONENT_ADD_INCLUDEDIRS "")
register_component()

View File

@ -82,7 +82,7 @@ extern "C" void app_main(void)
if (ttn.join())
{
printf("Joined.\n");
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
}
else
{

View File

@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(send_recv)

View File

@ -1,3 +1,5 @@
PROJECT_NAME := send_recv
EXTRA_COMPONENT_DIRS := $(abspath ../..)
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,4 @@
set(COMPONENT_SRCS "main.cpp")
set(COMPONENT_ADD_INCLUDEDIRS "")
register_component()

View File

@ -99,7 +99,7 @@ extern "C" void app_main(void)
if (ttn.join())
{
printf("Joined.\n");
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, NULL);
xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
}
else
{

View File

@ -130,7 +130,7 @@ public:
bool provisionWithMAC(const char *appEui, const char *appKey);
/**
* @brief Start task that listens on configured UART for provisioning commands.
* @brief Start task that listens on configured UART for AT commands.
*
* Run 'make menuconfig' to configure it.
*/

27
library.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "ttn-esp32",
"description": "The Things Network device library for ESP32 (ESP-IDF) and SX127x based devices",
"keywords": "esp32, ttn, espidf, lorawan, sx1276, sx1272, rfm92, rfm95, lmic",
"authors": {
"name": "Manuel Bl",
"email": "manuel.bleichenbacher@gmail.com",
"url": "https://github.com/manuelbl/ttn-esp32",
"maintainer": true
},
"repository": {
"type": "git",
"url": "https://github.com/manuelbl/ttn-esp32.git",
"branch": "dev"
},
"version": "2.3.2",
"license": "MIT License",
"export": {
"include": [
"include",
"src",
"esp_idf_lmic_config.h"
]
},
"frameworks": "espidf",
"platforms": "espressif32"
}

View File

@ -16,48 +16,28 @@
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "provisioning.h"
#include "TTNProvisioning.h"
#include "lmic/lmic.h"
#include "hal/hal_esp32.h"
#define UART_NUM CONFIG_TTN_PROVISION_UART_NUM
#define MAX_LINE_LENGTH 128
#if defined(TTN_HAS_AT_COMMANDS)
const uart_port_t UART_NUM = (uart_port_t) CONFIG_TTN_PROVISION_UART_NUM;
const int MAX_LINE_LENGTH = 128;
#endif
static const char *TAG = "ttn_prov";
static const char *NVS_FLASH_PARTITION = "ttn";
static const char *NVS_FLASH_KEY_DEV_EUI = "devEui";
static const char *NVS_FLASH_KEY_APP_EUI = "appEui";
static const char *NVS_FLASH_KEY_APP_KEY = "appKey";
static const char* const TAG = "ttn_prov";
static const char* const NVS_FLASH_PARTITION = "ttn";
static const char* const NVS_FLASH_KEY_DEV_EUI = "devEui";
static const char* const NVS_FLASH_KEY_APP_EUI = "appEui";
static const char* const NVS_FLASH_KEY_APP_KEY = "appKey";
static bool provisioning_decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key);
static void provisioning_task(void* pvParameter);
static void provisioning_add_line_data(int numBytes);
static void provisioning_detect_line_end(int start_at);
static void provisioning_process_line();
static bool read_nvs_value(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent);
static bool write_nvs_value(nvs_handle handle, const char* key, const uint8_t* data, size_t len);
static bool hex_str_to_bin(const char *hex, uint8_t *buf, int len);
static int hex_tuple_to_byte(const char *hex);
static int hex_digit_to_val(char ch);
static void bin_to_hex_str(const uint8_t* buf, int len, char* hex);
static char val_to_hex_digit(int val);
static void swap_bytes(uint8_t* buf, int len);
static bool is_all_zeroes(const uint8_t* buf, int len);
static QueueHandle_t uart_queue = NULL;
static char* line_buf;
static int line_length;
static uint8_t last_line_end_char = 0;
static uint8_t global_dev_eui[8];
static uint8_t global_app_eui[8];
static uint8_t global_app_key[16];
static bool have_keys = false;
static bool quit_task = false;
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
static void provisioning_config_uart();
#if defined(TTN_HAS_AT_COMMANDS)
void ttn_provisioning_task_caller(void* pvParameter);
#endif
@ -86,22 +66,40 @@ void os_getDevKey (u1_t* buf)
memcpy(buf, global_app_key, 16);
}
// --- Constructor
TTNProvisioning::TTNProvisioning()
: have_keys(false)
#if defined(TTN_HAS_AT_COMMANDS)
, uart_queue(nullptr), line_buf(nullptr), line_length(0), last_line_end_char(0), quit_task(false)
#endif
{
}
// --- Provisioning task
void provisioning_start_task()
#if defined(TTN_HAS_AT_COMMANDS)
void TTNProvisioning::startTask()
{
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
provisioning_config_uart();
#if defined(TTN_CONFIG_UART)
configUART();
#endif
esp_err_t err = uart_driver_install(UART_NUM, 2048, 2048, 20, &uart_queue, 0);
ESP_ERROR_CHECK(err);
xTaskCreate(provisioning_task, "provisioning", 2048, NULL, 1, NULL);
xTaskCreate(ttn_provisioning_task_caller, "provisioning", 2048, this, 1, nullptr);
}
void provisioning_task(void* pvParameter)
void ttn_provisioning_task_caller(void* pvParameter)
{
TTNProvisioning* provisioning = (TTNProvisioning*)pvParameter;
provisioning->provisioningTask();
}
void TTNProvisioning::provisioningTask()
{
line_buf = (char*)malloc(MAX_LINE_LENGTH + 1);
line_length = 0;
@ -118,7 +116,7 @@ void provisioning_task(void* pvParameter)
switch (event.type)
{
case UART_DATA:
provisioning_add_line_data(event.size);
addLineData(event.size);
break;
case UART_FIFO_OVF:
@ -134,10 +132,10 @@ void provisioning_task(void* pvParameter)
free(line_buf);
uart_driver_delete(UART_NUM);
vTaskDelete(NULL);
vTaskDelete(nullptr);
}
void provisioning_add_line_data(int numBytes)
void TTNProvisioning::addLineData(int numBytes)
{
int n;
top:
@ -149,7 +147,7 @@ top:
int start_at = line_length;
line_length += n;
provisioning_detect_line_end(start_at);
detectLineEnd(start_at);
if (n < numBytes)
{
@ -158,7 +156,7 @@ top:
}
}
void provisioning_detect_line_end(int start_at)
void TTNProvisioning::detectLineEnd(int start_at)
{
top:
for (int p = start_at; p < line_length; p++)
@ -175,7 +173,7 @@ top:
last_line_end_char = ch;
if (p > 0)
provisioning_process_line();
processLine();
memcpy(line_buf, line_buf + p + 1, line_length - p - 1);
line_length -= p + 1;
@ -191,7 +189,7 @@ top:
line_length = 0; // Line too long; flush it
}
void provisioning_process_line()
void TTNProvisioning::processLine()
{
bool is_ok = true;
bool reset_needed = false;
@ -209,14 +207,14 @@ void provisioning_process_line()
char hexbuf[16];
memcpy(binbuf, global_dev_eui, 8);
swap_bytes(binbuf, 8);
bin_to_hex_str(binbuf, 8, hexbuf);
swapBytes(binbuf, 8);
binToHexStr(binbuf, 8, hexbuf);
uart_write_bytes(UART_NUM, hexbuf, 16);
uart_write_bytes(UART_NUM, "-", 1);
memcpy(binbuf, global_app_eui, 8);
swap_bytes(binbuf, 8);
bin_to_hex_str(binbuf, 8, hexbuf);
swapBytes(binbuf, 8);
binToHexStr(binbuf, 8, hexbuf);
uart_write_bytes(UART_NUM, hexbuf, 16);
uart_write_bytes(UART_NUM, "-00000000000000000000000000000000\r\n", 35);
@ -228,7 +226,7 @@ void provisioning_process_line()
{
line_buf[24] = 0;
line_buf[41] = 0;
is_ok = provisioning_decode_keys(line_buf + 8, line_buf + 25, line_buf + 42);
is_ok = decodeKeys(line_buf + 8, line_buf + 25, line_buf + 42);
reset_needed = is_ok;
}
}
@ -238,7 +236,7 @@ void provisioning_process_line()
if (is_ok)
{
line_buf[25] = 0;
is_ok = provisioning_from_mac(line_buf + 9, line_buf + 26);
is_ok = fromMAC(line_buf + 9, line_buf + 26);
reset_needed = is_ok;
}
}
@ -250,7 +248,7 @@ void provisioning_process_line()
esp_err_t err = esp_efuse_mac_get_default(mac);
ESP_ERROR_CHECK(err);
bin_to_hex_str(mac, 6, hexbuf);
binToHexStr(mac, 6, hexbuf);
for (int i = 0; i < 12; i += 2) {
if (i > 0)
uart_write_bytes(UART_NUM, ":", 1);
@ -266,7 +264,7 @@ void provisioning_process_line()
esp_err_t err = esp_efuse_mac_get_default(mac);
ESP_ERROR_CHECK(err);
bin_to_hex_str(mac, 6, hexbuf);
binToHexStr(mac, 6, hexbuf);
for (int i = 0; i < 12; i += 2) {
uart_write_bytes(UART_NUM, hexbuf + i, 2);
if (i == 4)
@ -285,18 +283,21 @@ void provisioning_process_line()
if (reset_needed)
{
hal_enterCriticalSection();
ttn_hal.enterCriticalSection();
LMIC_reset();
hal_leaveCriticalSection();
ttn_hal.leaveCriticalSection();
onEvent(EV_RESET);
}
uart_write_bytes(UART_NUM, is_ok ? "OK\r\n" : "ERROR\r\n", is_ok ? 4 : 7);
}
#if defined(CONFIG_TTN_PROVISION_UART_CONFIG_YES)
#endif
void provisioning_config_uart()
#if defined(TTN_CONFIG_UART)
void TTNProvisioning::configUART()
{
esp_err_t err;
@ -305,7 +306,9 @@ void provisioning_config_uart()
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 0,
.use_ref_tick = false
};
err = uart_param_config(UART_NUM, &uart_config);
ESP_ERROR_CHECK(err);
@ -319,17 +322,17 @@ void provisioning_config_uart()
// --- Key handling
bool provisioning_have_keys()
bool TTNProvisioning::haveKeys()
{
return have_keys;
}
bool provisioning_decode_keys(const char *dev_eui, const char *app_eui, const char *app_key)
bool TTNProvisioning::decodeKeys(const char *dev_eui, const char *app_eui, const char *app_key)
{
return provisioning_decode(true, dev_eui, app_eui, app_key);
return decode(true, dev_eui, app_eui, app_key);
}
bool provisioning_from_mac(const char *app_eui, const char *app_key)
bool TTNProvisioning::fromMAC(const char *app_eui, const char *app_key)
{
uint8_t mac[6];
esp_err_t err = esp_efuse_mac_get_default(mac);
@ -344,33 +347,33 @@ bool provisioning_from_mac(const char *app_eui, const char *app_key)
global_dev_eui[1] = mac[4];
global_dev_eui[0] = mac[5];
return provisioning_decode(false, NULL, app_eui, app_key);
return decode(false, nullptr, app_eui, app_key);
}
bool provisioning_decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key)
bool TTNProvisioning::decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key)
{
uint8_t buf_dev_eui[8];
uint8_t buf_app_eui[8];
uint8_t buf_app_key[16];
if (incl_dev_eui && (strlen(dev_eui) != 16 || !hex_str_to_bin(dev_eui, buf_dev_eui, 8)))
if (incl_dev_eui && (strlen(dev_eui) != 16 || !hexStrToBin(dev_eui, buf_dev_eui, 8)))
{
ESP_LOGW(TAG, "Invalid device EUI: %s", dev_eui);
return false;
}
if (incl_dev_eui)
swap_bytes(buf_dev_eui, 8);
swapBytes(buf_dev_eui, 8);
if (strlen(app_eui) != 16 || !hex_str_to_bin(app_eui, buf_app_eui, 8))
if (strlen(app_eui) != 16 || !hexStrToBin(app_eui, buf_app_eui, 8))
{
ESP_LOGW(TAG, "Invalid application EUI: %s", app_eui);
return false;
}
swap_bytes(buf_app_eui, 8);
swapBytes(buf_app_eui, 8);
if (strlen(app_key) != 32 || !hex_str_to_bin(app_key, buf_app_key, 16))
if (strlen(app_key) != 32 || !hexStrToBin(app_key, buf_app_key, 16))
{
ESP_LOGW(TAG, "Invalid application key: %s", app_key);
return false;
@ -381,11 +384,11 @@ bool provisioning_decode(bool incl_dev_eui, const char *dev_eui, const char *app
memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui));
memcpy(global_app_key, buf_app_key, sizeof(global_app_key));
have_keys = !is_all_zeroes(global_dev_eui, sizeof(global_dev_eui))
&& !is_all_zeroes(global_app_eui, sizeof(global_app_eui))
&& !is_all_zeroes(global_app_key, sizeof(global_app_key));
have_keys = !isAllZeros(global_dev_eui, sizeof(global_dev_eui))
&& !isAllZeros(global_app_eui, sizeof(global_app_eui))
&& !isAllZeros(global_app_key, sizeof(global_app_key));
if (!provisioning_save_keys())
if (!saveKeys())
return false;
return true;
@ -394,7 +397,7 @@ bool provisioning_decode(bool incl_dev_eui, const char *dev_eui, const char *app
// --- Non-volatile storage
bool provisioning_save_keys()
bool TTNProvisioning::saveKeys()
{
bool result = false;
@ -409,13 +412,13 @@ bool provisioning_save_keys()
if (res != ESP_OK)
goto done;
if (!write_nvs_value(handle, NVS_FLASH_KEY_DEV_EUI, global_dev_eui, sizeof(global_dev_eui)))
if (!writeNvsValue(handle, NVS_FLASH_KEY_DEV_EUI, global_dev_eui, sizeof(global_dev_eui)))
goto done;
if (!write_nvs_value(handle, NVS_FLASH_KEY_APP_EUI, global_app_eui, sizeof(global_app_eui)))
if (!writeNvsValue(handle, NVS_FLASH_KEY_APP_EUI, global_app_eui, sizeof(global_app_eui)))
goto done;
if (!write_nvs_value(handle, NVS_FLASH_KEY_APP_KEY, global_app_key, sizeof(global_app_key)))
if (!writeNvsValue(handle, NVS_FLASH_KEY_APP_KEY, global_app_key, sizeof(global_app_key)))
goto done;
res = nvs_commit(handle);
@ -429,7 +432,7 @@ done:
return result;
}
bool provisioning_restore_keys(bool silent)
bool TTNProvisioning::restoreKeys(bool silent)
{
uint8_t buf_dev_eui[8];
uint8_t buf_app_eui[8];
@ -448,22 +451,22 @@ bool provisioning_restore_keys(bool silent)
if (res != ESP_OK)
goto done;
if (!read_nvs_value(handle, NVS_FLASH_KEY_DEV_EUI, buf_dev_eui, sizeof(global_dev_eui), silent))
if (!readNvsValue(handle, NVS_FLASH_KEY_DEV_EUI, buf_dev_eui, sizeof(global_dev_eui), silent))
goto done;
if (!read_nvs_value(handle, NVS_FLASH_KEY_APP_EUI, buf_app_eui, sizeof(global_app_eui), silent))
if (!readNvsValue(handle, NVS_FLASH_KEY_APP_EUI, buf_app_eui, sizeof(global_app_eui), silent))
goto done;
if (!read_nvs_value(handle, NVS_FLASH_KEY_APP_KEY, buf_app_key, sizeof(global_app_key), silent))
if (!readNvsValue(handle, NVS_FLASH_KEY_APP_KEY, buf_app_key, sizeof(global_app_key), silent))
goto done;
memcpy(global_dev_eui, buf_dev_eui, sizeof(global_dev_eui));
memcpy(global_app_eui, buf_app_eui, sizeof(global_app_eui));
memcpy(global_app_key, buf_app_key, sizeof(global_app_key));
have_keys = !is_all_zeroes(global_dev_eui, sizeof(global_dev_eui))
&& !is_all_zeroes(global_app_eui, sizeof(global_app_eui))
&& !is_all_zeroes(global_app_key, sizeof(global_app_key));
have_keys = !isAllZeros(global_dev_eui, sizeof(global_dev_eui))
&& !isAllZeros(global_app_eui, sizeof(global_app_eui))
&& !isAllZeros(global_app_key, sizeof(global_app_key));
if (have_keys)
{
@ -479,7 +482,7 @@ done:
return true;
}
bool read_nvs_value(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent)
bool TTNProvisioning::readNvsValue(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent)
{
size_t size = expected_length;
esp_err_t res = nvs_get_blob(handle, key, data, &size);
@ -504,10 +507,10 @@ bool read_nvs_value(nvs_handle handle, const char* key, uint8_t* data, size_t ex
return false;
}
bool write_nvs_value(nvs_handle handle, const char* key, const uint8_t* data, size_t len)
bool TTNProvisioning::writeNvsValue(nvs_handle handle, const char* key, const uint8_t* data, size_t len)
{
uint8_t buf[16];
if (read_nvs_value(handle, key, buf, len, true) && memcmp(buf, data, len) == 0)
if (readNvsValue(handle, key, buf, len, true) && memcmp(buf, data, len) == 0)
return true; // unchanged
esp_err_t res = nvs_set_blob(handle, key, data, len);
@ -519,12 +522,12 @@ bool write_nvs_value(nvs_handle handle, const char* key, const uint8_t* data, si
// --- Helper functions ---
bool hex_str_to_bin(const char *hex, uint8_t *buf, int len)
bool TTNProvisioning::hexStrToBin(const char *hex, uint8_t *buf, int len)
{
const char* ptr = hex;
for (int i = 0; i < len; i++)
{
int val = hex_tuple_to_byte(ptr);
int val = hexTupleToByte(ptr);
if (val < 0)
return false;
buf[i] = val;
@ -533,18 +536,18 @@ bool hex_str_to_bin(const char *hex, uint8_t *buf, int len)
return true;
}
int hex_tuple_to_byte(const char *hex)
int TTNProvisioning::hexTupleToByte(const char *hex)
{
int nibble1 = hex_digit_to_val(hex[0]);
int nibble1 = hexDigitToVal(hex[0]);
if (nibble1 < 0)
return -1;
int nibble2 = hex_digit_to_val(hex[1]);
int nibble2 = hexDigitToVal(hex[1]);
if (nibble2 < 0)
return -1;
return (nibble1 << 4) | nibble2;
}
int hex_digit_to_val(char ch)
int TTNProvisioning::hexDigitToVal(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
@ -555,24 +558,24 @@ int hex_digit_to_val(char ch)
return -1;
}
void bin_to_hex_str(const uint8_t* buf, int len, char* hex)
void TTNProvisioning::binToHexStr(const uint8_t* buf, int len, char* hex)
{
for (int i = 0; i < len; i++)
{
uint8_t b = buf[i];
*hex = val_to_hex_digit((b & 0xf0) >> 4);
*hex = valToHexDigit((b & 0xf0) >> 4);
hex++;
*hex = val_to_hex_digit(b & 0x0f);
*hex = valToHexDigit(b & 0x0f);
hex++;
}
}
char val_to_hex_digit(int val)
char TTNProvisioning::valToHexDigit(int val)
{
return "0123456789ABCDEF"[val];
}
void swap_bytes(uint8_t* buf, int len)
void TTNProvisioning::swapBytes(uint8_t* buf, int len)
{
uint8_t* p1 = buf;
uint8_t* p2 = buf + len - 1;
@ -586,7 +589,7 @@ void swap_bytes(uint8_t* buf, int len)
}
}
bool is_all_zeroes(const uint8_t* buf, int len)
bool TTNProvisioning::isAllZeros(const uint8_t* buf, int len)
{
for (int i = 0; i < len; i++)
if (buf[i] != 0)

73
src/TTNProvisioning.h Normal file
View File

@ -0,0 +1,73 @@
/*******************************************************************************
*
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
*
* Copyright (c) 2018 Manuel Bleichenbacher
*
* Licensed under MIT License
* https://opensource.org/licenses/MIT
*
* Task listening on a UART port for provisioning commands.
*******************************************************************************/
#ifndef _ttnprovisioning_h_
#define _ttnprovisioning_h_
#include "lmic/oslmic.h"
#include "nvs_flash.h"
class TTNProvisioning
{
public:
TTNProvisioning();
bool haveKeys();
bool decodeKeys(const char *dev_eui, const char *app_eui, const char *app_key);
bool fromMAC(const char *app_eui, const char *app_key);
bool saveKeys();
bool restoreKeys(bool silent);
#if defined(TTN_HAS_AT_COMMANDS)
void startTask();
#endif
private:
bool decode(bool incl_dev_eui, const char *dev_eui, const char *app_eui, const char *app_key);
bool readNvsValue(nvs_handle handle, const char* key, uint8_t* data, size_t expected_length, bool silent);
bool writeNvsValue(nvs_handle handle, const char* key, const uint8_t* data, size_t len);
#if defined(TTN_HAS_AT_COMMANDS)
void provisioningTask();
void addLineData(int numBytes);
void detectLineEnd(int start_at);
void processLine();
#endif
#if defined(TTN_CONFIG_UART)
void configUART();
#endif
static bool hexStrToBin(const char *hex, uint8_t *buf, int len);
static int hexTupleToByte(const char *hex);
static int hexDigitToVal(char ch);
static void binToHexStr(const uint8_t* buf, int len, char* hex);
static char valToHexDigit(int val);
static void swapBytes(uint8_t* buf, int len);
static bool isAllZeros(const uint8_t* buf, int len);
private:
bool have_keys = false;
#if defined(TTN_HAS_AT_COMMANDS)
QueueHandle_t uart_queue;
char* line_buf;
int line_length;
uint8_t last_line_end_char;
bool quit_task;
friend void ttn_provisioning_task_caller(void* pvParameter);
#endif
};
#endif

View File

@ -16,7 +16,7 @@
#include "TheThingsNetwork.h"
#include "hal/hal_esp32.h"
#include "lmic/lmic.h"
#include "provisioning.h"
#include "TTNProvisioning.h"
enum ClientAction
@ -31,10 +31,11 @@ static const char *TAG = "ttn";
static TheThingsNetwork* ttnInstance;
static QueueHandle_t resultQueue;
static ClientAction clientAction = eActionUnrelated;
static TTNProvisioning provisioning;
TheThingsNetwork::TheThingsNetwork()
: messageCallback(NULL)
: messageCallback(nullptr)
{
#if defined(TTN_IS_DISABLED)
ESP_LOGE(TAG, "TTN is disabled. Configure a frequency plan using 'make menuconfig'");
@ -42,9 +43,9 @@ TheThingsNetwork::TheThingsNetwork()
esp_restart();
#endif
ASSERT(ttnInstance == NULL);
ASSERT(ttnInstance == nullptr);
ttnInstance = this;
hal_initCriticalSection();
ttn_hal.initCriticalSection();
}
TheThingsNetwork::~TheThingsNetwork()
@ -65,59 +66,68 @@ void TheThingsNetwork::configurePins(spi_host_device_t spi_host, uint8_t nss, ui
reset();
resultQueue = xQueueCreate(12, sizeof(int));
ASSERT(resultQueue != NULL);
hal_startBgTask();
ASSERT(resultQueue != nullptr);
ttn_hal.startBackgroundTask();
}
void TheThingsNetwork::reset()
{
hal_enterCriticalSection();
ttn_hal.enterCriticalSection();
LMIC_reset();
hal_leaveCriticalSection();
ttn_hal.leaveCriticalSection();
}
bool TheThingsNetwork::provision(const char *devEui, const char *appEui, const char *appKey)
{
if (!provisioning_decode_keys(devEui, appEui, appKey))
if (!provisioning.decodeKeys(devEui, appEui, appKey))
return false;
return provisioning_save_keys();
return provisioning.saveKeys();
}
bool TheThingsNetwork::provisionWithMAC(const char *appEui, const char *appKey)
{
if (!provisioning_from_mac(appEui, appKey))
if (!provisioning.fromMAC(appEui, appKey))
return false;
return provisioning_save_keys();
return provisioning.saveKeys();
}
void TheThingsNetwork::startProvisioningTask()
{
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
provisioning_start_task();
#if defined(TTN_HAS_AT_COMMANDS)
provisioning.startTask();
#else
ESP_LOGE(TAG, "AT commands are disabled. Change the configuration using 'make menuconfig'");
ASSERT(0);
esp_restart();
#endif
}
void TheThingsNetwork::waitForProvisioning()
{
#if !defined(CONFIG_TTN_PROVISION_UART_NONE)
#if defined(TTN_HAS_AT_COMMANDS)
if (isProvisioned())
{
ESP_LOGI(TAG, "Device is already provisioned");
return;
}
while (!provisioning_have_keys())
while (!provisioning.haveKeys())
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Device successfully provisioned");
#else
ESP_LOGE(TAG, "AT commands are disabled. Change the configuration using 'make menuconfig'");
ASSERT(0);
esp_restart();
#endif
}
bool TheThingsNetwork::join(const char *devEui, const char *appEui, const char *appKey)
{
if (!provisioning_decode_keys(devEui, appEui, appKey))
if (!provisioning.decodeKeys(devEui, appEui, appKey))
return false;
return joinCore();
@ -125,9 +135,9 @@ bool TheThingsNetwork::join(const char *devEui, const char *appEui, const char *
bool TheThingsNetwork::join()
{
if (!provisioning_have_keys())
if (!provisioning.haveKeys())
{
if (!provisioning_restore_keys(false))
if (!provisioning.restoreKeys(false))
return false;
}
@ -136,17 +146,17 @@ bool TheThingsNetwork::join()
bool TheThingsNetwork::joinCore()
{
if (!provisioning_have_keys())
if (!provisioning.haveKeys())
{
ESP_LOGW(TAG, "Device EUI, App EUI and/or App key have not been provided");
return false;
}
hal_enterCriticalSection();
ttn_hal.enterCriticalSection();
clientAction = eActionJoining;
LMIC_startJoining();
hal_wakeUp();
hal_leaveCriticalSection();
ttn_hal.wakeUp();
ttn_hal.leaveCriticalSection();
int result = 0;
xQueueReceive(resultQueue, &result, portMAX_DELAY);
@ -155,17 +165,17 @@ bool TheThingsNetwork::joinCore()
TTNResponseCode TheThingsNetwork::transmitMessage(const uint8_t *payload, size_t length, port_t port, bool confirm)
{
hal_enterCriticalSection();
ttn_hal.enterCriticalSection();
if (LMIC.opmode & OP_TXRXPEND)
{
hal_leaveCriticalSection();
ttn_hal.leaveCriticalSection();
return kTTNErrorTransmissionFailed;
}
clientAction = eActionSending;
LMIC_setTxData2(port, (xref2u1_t)payload, length, confirm);
hal_wakeUp();
hal_leaveCriticalSection();
ttn_hal.wakeUp();
ttn_hal.leaveCriticalSection();
int result = 0;
xQueueReceive(resultQueue, &result, portMAX_DELAY);
@ -173,12 +183,12 @@ TTNResponseCode TheThingsNetwork::transmitMessage(const uint8_t *payload, size_t
if (result == EV_TXCOMPLETE)
{
bool hasRecevied = (LMIC.txrxFlags & (TXRX_DNW1 | TXRX_DNW2)) != 0;
if (hasRecevied && messageCallback != NULL)
if (hasRecevied && messageCallback != nullptr)
{
port_t port = 0;
if ((LMIC.txrxFlags & TXRX_PORT))
port = LMIC.frame[LMIC.dataBeg - 1];
const uint8_t* msg = NULL;
const uint8_t* msg = nullptr;
if (LMIC.dataLen > 0)
msg = LMIC.frame + LMIC.dataBeg;
messageCallback(msg, LMIC.dataLen, port);
@ -198,12 +208,12 @@ void TheThingsNetwork::onMessage(TTNMessageCallback callback)
bool TheThingsNetwork::isProvisioned()
{
if (provisioning_have_keys())
if (provisioning.haveKeys())
return true;
provisioning_restore_keys(true);
provisioning.restoreKeys(true);
return provisioning_have_keys();
return provisioning.haveKeys();
}
@ -211,7 +221,7 @@ bool TheThingsNetwork::isProvisioned()
#if CONFIG_LOG_DEFAULT_LEVEL >= 3
static const char *eventNames[] = {
NULL,
nullptr,
"EV_SCAN_TIMEOUT", "EV_BEACON_FOUND",
"EV_BEACON_MISSED", "EV_BEACON_TRACKED", "EV_JOINING",
"EV_JOINED", "EV_RFU1", "EV_JOIN_FAILED", "EV_REJOIN_FAILED",

View File

@ -262,9 +262,6 @@ u4_t os_aes (u1_t mode, xref2u1_t buf, u2_t len) {
u4_t a0, a1, a2, a3;
u4_t t0, t1, t2, t3;
u4_t *ki, *ke;
// ttn-esp32 change: prevent error 'x' may be used uninitialized in this function
a0 = a1 = a2 = a3 = 0;
t0 = t1 = 0;
// load input block
if( (mode & AES_CTR) || ((mode & AES_MIC) && (mode & AES_MICNOAUX)==0) ) { // load CTR block or first MIC block

0
src/aes/other.c Normal file → Executable file
View File

View File

@ -22,44 +22,44 @@
#define LMIC_UNUSED_PIN 0xff
static const char *TAG = "ttn_hal";
static const char * const TAG = "ttn_hal";
lmic_pinmap lmic_pins;
HAL_ESP32 ttn_hal;
typedef enum {
DIO0 = 0,
DIO1,
DIO2,
TIMER,
WAKEUP
} event_t;
typedef struct {
struct HALQueueItem {
ostime_t time;
event_t ev;
} queue_item_t;
HAL_Event ev;
HALQueueItem() : time(0), ev(DIO0) { }
HALQueueItem(HAL_Event e, ostime_t t = 0)
: time(t), ev(e) { }
};
// -----------------------------------------------------------------------------
// Constructor
HAL_ESP32::HAL_ESP32()
: nextTimerEvent(0x200000000)
{
}
// -----------------------------------------------------------------------------
// I/O
static QueueHandle_t dio_queue;
void IRAM_ATTR dio_irq_handler(void *arg)
void IRAM_ATTR HAL_ESP32::dioIrqHandler(void *arg)
{
uint64_t now;
timer_get_counter_value(TTN_TIMER_GROUP, TTN_TIMER, &now);
event_t ev = (long)arg;
BaseType_t higher_prio_task_woken = pdFALSE;
queue_item_t item = {
.time = (ostime_t)now,
.ev = ev
};
xQueueSendFromISR(dio_queue, &item, &higher_prio_task_woken);
if (higher_prio_task_woken)
BaseType_t higherPrioTaskWoken = pdFALSE;
HALQueueItem item { (HAL_Event)(long)arg, (ostime_t)now };
xQueueSendFromISR(ttn_hal.dioQueue, &item, &higherPrioTaskWoken);
if (higherPrioTaskWoken)
portYIELD_FROM_ISR();
}
static void hal_io_init()
void HAL_ESP32::ioInit()
{
// NSS and DIO0 and DIO1 are required
ASSERT(lmic_pins.nss != LMIC_UNUSED_PIN);
@ -67,35 +67,35 @@ static void hal_io_init()
ASSERT(lmic_pins.dio1 != LMIC_UNUSED_PIN);
gpio_pad_select_gpio(lmic_pins.nss);
gpio_set_level(lmic_pins.nss, 0);
gpio_set_direction(lmic_pins.nss, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)lmic_pins.nss, 0);
gpio_set_direction((gpio_num_t)lmic_pins.nss, GPIO_MODE_OUTPUT);
if (lmic_pins.rxtx != LMIC_UNUSED_PIN)
{
gpio_pad_select_gpio(lmic_pins.rxtx);
gpio_set_level(lmic_pins.rxtx, 0);
gpio_set_direction(lmic_pins.rxtx, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)lmic_pins.rxtx, 0);
gpio_set_direction((gpio_num_t)lmic_pins.rxtx, GPIO_MODE_OUTPUT);
}
if (lmic_pins.rst != LMIC_UNUSED_PIN)
{
gpio_pad_select_gpio(lmic_pins.rst);
gpio_set_level(lmic_pins.rst, 0);
gpio_set_direction(lmic_pins.rst, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio((gpio_num_t)lmic_pins.rst);
gpio_set_level((gpio_num_t)lmic_pins.rst, 0);
gpio_set_direction((gpio_num_t)lmic_pins.rst, GPIO_MODE_OUTPUT);
}
dio_queue = xQueueCreate(12, sizeof(queue_item_t));
ASSERT(dio_queue != NULL);
dioQueue = xQueueCreate(12, sizeof(HALQueueItem));
ASSERT(dioQueue != NULL);
gpio_pad_select_gpio(lmic_pins.dio0);
gpio_set_direction(lmic_pins.dio0, GPIO_MODE_INPUT);
gpio_set_intr_type(lmic_pins.dio0, GPIO_INTR_POSEDGE);
gpio_isr_handler_add(lmic_pins.dio0, dio_irq_handler, (void *)0);
gpio_set_direction((gpio_num_t)lmic_pins.dio0, GPIO_MODE_INPUT);
gpio_set_intr_type((gpio_num_t)lmic_pins.dio0, GPIO_INTR_POSEDGE);
gpio_isr_handler_add((gpio_num_t)lmic_pins.dio0, dioIrqHandler, (void *)0);
gpio_pad_select_gpio(lmic_pins.dio1);
gpio_set_direction(lmic_pins.dio1, GPIO_MODE_INPUT);
gpio_set_intr_type(lmic_pins.dio1, GPIO_INTR_POSEDGE);
gpio_isr_handler_add(lmic_pins.dio1, dio_irq_handler, (void *)1);
gpio_pad_select_gpio((gpio_num_t)lmic_pins.dio1);
gpio_set_direction((gpio_num_t)lmic_pins.dio1, GPIO_MODE_INPUT);
gpio_set_intr_type((gpio_num_t)lmic_pins.dio1, GPIO_INTR_POSEDGE);
gpio_isr_handler_add((gpio_num_t)lmic_pins.dio1, dioIrqHandler, (void *)1);
ESP_LOGI(TAG, "IO initialized");
}
@ -105,7 +105,7 @@ void hal_pin_rxtx(u1_t val)
if (lmic_pins.rxtx == LMIC_UNUSED_PIN)
return;
gpio_set_level(lmic_pins.rxtx, val);
gpio_set_level((gpio_num_t)lmic_pins.rxtx, val);
}
void hal_pin_rst(u1_t val)
@ -115,110 +115,85 @@ void hal_pin_rst(u1_t val)
if (val == 0 || val == 1)
{ // drive pin
gpio_set_level(lmic_pins.rst, val);
gpio_set_direction(lmic_pins.rst, GPIO_MODE_OUTPUT);
gpio_set_level((gpio_num_t)lmic_pins.rst, val);
gpio_set_direction((gpio_num_t)lmic_pins.rst, GPIO_MODE_OUTPUT);
}
else
{ // keep pin floating
gpio_set_level(lmic_pins.rst, val);
gpio_set_direction(lmic_pins.rst, GPIO_MODE_INPUT);
gpio_set_level((gpio_num_t)lmic_pins.rst, val);
gpio_set_direction((gpio_num_t)lmic_pins.rst, GPIO_MODE_INPUT);
}
}
s1_t hal_getRssiCal (void) {
s1_t hal_getRssiCal (void)
{
return lmic_pins.rssi_cal;
}
ostime_t hal_setModuleActive (bit_t val)
{
return 0;
}
bit_t hal_queryUsingTcxo(void)
{
return false;
}
// -----------------------------------------------------------------------------
// SPI
#define SPI_QUEUE_SIZE 4
#define SPI_NUM_TRX_SLOTS (SPI_QUEUE_SIZE + 1)
static spi_device_handle_t spi_handle;
static spi_transaction_t spi_trx_queue[SPI_NUM_TRX_SLOTS];
static int spi_trx_queue_head = 0;
static int spi_num_outstanding_trx = 0;
static spi_transaction_t* get_next_spi_trx_desc()
{
spi_transaction_t* trx = spi_trx_queue + spi_trx_queue_head;
memset(trx, 0, sizeof(spi_transaction_t));
return trx;
}
static void collect_spi_result()
{
int head = spi_trx_queue_head;
int tail = head - spi_num_outstanding_trx;
if (tail < 0)
tail += SPI_NUM_TRX_SLOTS;
spi_transaction_t* trx;
esp_err_t err = spi_device_get_trans_result(spi_handle, &trx, 100 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(err);
ASSERT(trx == spi_trx_queue + tail);
spi_num_outstanding_trx--;
}
static void submit_spi_trx()
{
if (spi_num_outstanding_trx >= SPI_QUEUE_SIZE)
collect_spi_result();
int head = spi_trx_queue_head;
esp_err_t err = spi_device_queue_trans(spi_handle, spi_trx_queue + head, 100 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(err);
spi_num_outstanding_trx++;
head++;
if (head >= SPI_NUM_TRX_SLOTS)
head = 0;
spi_trx_queue_head = head;
}
static void hal_spi_init()
void HAL_ESP32::spiInit()
{
// init device
spi_device_interface_config_t spi_device_intf_config = {
.mode = 1,
.clock_speed_hz = CONFIG_TTN_SPI_FREQ,
.command_bits = 0,
.address_bits = 8,
.spics_io_num = lmic_pins.nss,
.queue_size = SPI_QUEUE_SIZE,
.cs_ena_posttrans = 2
};
spi_device_interface_config_t spiConfig;
memset(&spiConfig, 0, sizeof(spiConfig));
spiConfig.mode = 1;
spiConfig.clock_speed_hz = CONFIG_TTN_SPI_FREQ;
spiConfig.command_bits = 0;
spiConfig.address_bits = 8;
spiConfig.spics_io_num = lmic_pins.nss;
spiConfig.queue_size = 1;
spiConfig.cs_ena_posttrans = 2;
esp_err_t ret = spi_bus_add_device(lmic_pins.spi_host, &spi_device_intf_config, &spi_handle);
esp_err_t ret = spi_bus_add_device(lmic_pins.spi_host, &spiConfig, &spiHandle);
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "SPI initialized");
}
void hal_spi_write(u1_t cmd, const u1_t *buf, int len)
void hal_spi_write(u1_t cmd, const u1_t *buf, size_t len)
{
spi_transaction_t* trx = get_next_spi_trx_desc();
trx->addr = cmd;
trx->length = 8 * len;
trx->tx_buffer = buf;
submit_spi_trx();
ttn_hal.spiWrite(cmd, buf, len);
}
void hal_spi_read(u1_t cmd, u1_t *buf, int len)
void HAL_ESP32::spiWrite(uint8_t cmd, const uint8_t *buf, size_t len)
{
memset(&spiTransaction, 0, sizeof(spiTransaction));
spiTransaction.addr = cmd;
spiTransaction.length = 8 * len;
spiTransaction.tx_buffer = buf;
esp_err_t err = spi_device_transmit(spiHandle, &spiTransaction);
ESP_ERROR_CHECK(err);
}
void hal_spi_read(u1_t cmd, u1_t *buf, size_t len)
{
ttn_hal.spiRead(cmd, buf, len);
}
void HAL_ESP32::spiRead(uint8_t cmd, uint8_t *buf, size_t len)
{
memset(buf, 0, len);
spi_transaction_t* trx = get_next_spi_trx_desc();
trx->addr = cmd;
trx->length = 8 * len;
trx->rxlength = 8 * len;
trx->tx_buffer = buf;
trx->rx_buffer = buf;
submit_spi_trx();
while (spi_num_outstanding_trx > 0)
collect_spi_result();
memset(&spiTransaction, 0, sizeof(spiTransaction));
spiTransaction.addr = cmd;
spiTransaction.length = 8 * len;
spiTransaction.rxlength = 8 * len;
spiTransaction.tx_buffer = buf;
spiTransaction.rx_buffer = buf;
esp_err_t err = spi_device_transmit(spiHandle, &spiTransaction);
ESP_ERROR_CHECK(err);
}
// -----------------------------------------------------------------------------
@ -243,13 +218,9 @@ void hal_spi_read(u1_t cmd, u1_t *buf, int len)
* by 0x100000000.
*/
#define OVERRUN_TRESHOLD 0x10000 // approx 10 seconds
static const ostime_t OVERRUN_TRESHOLD = 0x10000; // approx 10 seconds
static uint64_t next_timer_event = 0x200000000;
static void IRAM_ATTR hal_timer_irq_handler(void *arg);
static void hal_time_init()
void HAL_ESP32::timerInit()
{
timer_config_t config = {
.alarm_en = false,
@ -261,13 +232,13 @@ static void hal_time_init()
};
timer_init(TTN_TIMER_GROUP, TTN_TIMER, &config);
timer_set_counter_value(TTN_TIMER_GROUP, TTN_TIMER, 0x0);
timer_isr_register(TTN_TIMER_GROUP, TTN_TIMER, hal_timer_irq_handler, NULL, ESP_INTR_FLAG_IRAM, NULL);
timer_isr_register(TTN_TIMER_GROUP, TTN_TIMER, timerIrqHandler, NULL, ESP_INTR_FLAG_IRAM, NULL);
timer_start(TTN_TIMER_GROUP, TTN_TIMER);
ESP_LOGI(TAG, "Timer initialized");
}
static void hal_prepare_next_alarm(u4_t time)
void HAL_ESP32::prepareNextAlarm(u4_t time)
{
uint64_t now;
timer_get_counter_value(TTN_TIMER_GROUP, TTN_TIMER, &now);
@ -282,74 +253,65 @@ static void hal_prepare_next_alarm(u4_t time)
timer_start(TTN_TIMER_GROUP, TTN_TIMER);
}
next_timer_event = time;
nextTimerEvent = time;
if (now32 > time && now32 - time > OVERRUN_TRESHOLD)
next_timer_event += 0x100000000;
nextTimerEvent += 0x100000000;
}
static void hal_arm_timer()
void HAL_ESP32::armTimer()
{
timer_set_alarm(TTN_TIMER_GROUP, TTN_TIMER, TIMER_ALARM_DIS);
timer_set_alarm_value(TTN_TIMER_GROUP, TTN_TIMER, next_timer_event);
timer_set_alarm_value(TTN_TIMER_GROUP, TTN_TIMER, nextTimerEvent);
timer_set_alarm(TTN_TIMER_GROUP, TTN_TIMER, TIMER_ALARM_EN);
}
static void hal_disarm_timer()
void HAL_ESP32::disarmTimer()
{
timer_set_alarm(TTN_TIMER_GROUP, TTN_TIMER, TIMER_ALARM_DIS);
next_timer_event = 0x200000000; // wait indefinitely (almost)
nextTimerEvent = 0x200000000; // wait indefinitely (almost)
}
static void IRAM_ATTR hal_timer_irq_handler(void *arg)
void IRAM_ATTR HAL_ESP32::timerIrqHandler(void *arg)
{
TTN_CLEAR_TIMER_ALARM;
BaseType_t higher_prio_task_woken = pdFALSE;
queue_item_t item = {
.ev = TIMER
};
xQueueSendFromISR(dio_queue, &item, &higher_prio_task_woken);
if (higher_prio_task_woken)
BaseType_t higherPrioTaskWoken = pdFALSE;
HALQueueItem item { TIMER };
xQueueSendFromISR(ttn_hal.dioQueue, &item, &higherPrioTaskWoken);
if (higherPrioTaskWoken)
portYIELD_FROM_ISR();
}
typedef enum {
CHECK_IO,
WAIT_FOR_ANY_EVENT,
WAIT_FOR_TIMER
} wait_open_e;
static bool hal_wait(wait_open_e wait_option)
bool HAL_ESP32::wait(WaitKind waitKind)
{
TickType_t ticks_to_wait = wait_option == CHECK_IO ? 0 : portMAX_DELAY;
TickType_t ticksToWait = waitKind == CHECK_IO ? 0 : portMAX_DELAY;
while (true)
{
queue_item_t item;
if (xQueueReceive(dio_queue, &item, ticks_to_wait) == pdFALSE)
HALQueueItem item;
if (xQueueReceive(dioQueue, &item, ticksToWait) == pdFALSE)
return false;
if (item.ev == WAKEUP)
{
if (wait_option != WAIT_FOR_TIMER)
if (waitKind != WAIT_FOR_TIMER)
{
hal_disarm_timer();
disarmTimer();
return true;
}
}
else if (item.ev == TIMER)
{
hal_disarm_timer();
if (wait_option != CHECK_IO)
disarmTimer();
if (waitKind != CHECK_IO)
return true;
}
else // IO interrupt
{
if (wait_option != WAIT_FOR_TIMER)
hal_disarm_timer();
hal_enterCriticalSection();
if (waitKind != WAIT_FOR_TIMER)
disarmTimer();
enterCriticalSection();
radio_irq_handler_v2(item.ev, item.time);
hal_leaveCriticalSection();
if (wait_option != WAIT_FOR_TIMER)
leaveCriticalSection();
if (waitKind != WAIT_FOR_TIMER)
return true;
}
}
@ -364,21 +326,29 @@ u4_t hal_ticks()
void hal_waitUntil(u4_t time)
{
hal_prepare_next_alarm(time);
hal_arm_timer();
hal_wait(WAIT_FOR_TIMER);
ttn_hal.waitUntil(time);
}
void hal_wakeUp()
void HAL_ESP32::waitUntil(uint32_t time)
{
queue_item_t item = {
.ev = WAKEUP
};
xQueueSend(dio_queue, &item, 0);
prepareNextAlarm(time);
armTimer();
wait(WAIT_FOR_TIMER);
}
void HAL_ESP32::wakeUp()
{
HALQueueItem item { WAKEUP };
xQueueSend(dioQueue, &item, 0);
}
// check and rewind for target time
u1_t hal_checkTimer(u4_t time)
{
return ttn_hal.checkTimer(time);
}
uint8_t HAL_ESP32::checkTimer(uint32_t time)
{
uint64_t now;
timer_get_counter_value(TTN_TIMER_GROUP, TTN_TIMER, &now);
@ -395,19 +365,25 @@ u1_t hal_checkTimer(u4_t time)
return 1; // timer has expired recently
}
hal_prepare_next_alarm(time);
prepareNextAlarm(time);
return 0;
}
void hal_sleep()
{
if (hal_wait(CHECK_IO))
ttn_hal.sleep();
}
void HAL_ESP32::sleep()
{
if (wait(CHECK_IO))
return;
hal_arm_timer();
hal_wait(WAIT_FOR_ANY_EVENT);
armTimer();
wait(WAIT_FOR_ANY_EVENT);
}
// -----------------------------------------------------------------------------
// IRQ
@ -423,44 +399,48 @@ void hal_enableIRQs()
// and don't access any shared data structures
}
// -----------------------------------------------------------------------------
// Synchronization between application code and background task
static SemaphoreHandle_t mutex;
void hal_initCriticalSection()
void HAL_ESP32::initCriticalSection()
{
mutex = xSemaphoreCreateRecursiveMutex();
}
void hal_enterCriticalSection()
void HAL_ESP32::enterCriticalSection()
{
xSemaphoreTakeRecursive(mutex, portMAX_DELAY);
}
void hal_leaveCriticalSection()
void HAL_ESP32::leaveCriticalSection()
{
xSemaphoreGiveRecursive(mutex);
}
// -----------------------------------------------------------------------------
static void hal_bgTask(void* pvParameter) {
void HAL_ESP32::backgroundTask(void* pvParameter) {
os_runloop();
}
void hal_init_ex(const void *pContext)
{
// configure radio I/O and interrupt handler
hal_io_init();
// configure radio SPI
hal_spi_init();
// configure timer and interrupt handler
hal_time_init();
ttn_hal.init();
}
void hal_startBgTask() {
xTaskCreate(hal_bgTask, "ttn_lora_task", 1024 * 4, NULL, CONFIG_TTN_BG_TASK_PRIO, NULL);
void HAL_ESP32::init()
{
// configure radio I/O and interrupt handler
ioInit();
// configure radio SPI
spiInit();
// configure timer and interrupt handler
timerInit();
}
void HAL_ESP32::startBackgroundTask() {
xTaskCreate(backgroundTask, "ttn_lora_task", 1024 * 4, NULL, CONFIG_TTN_BG_TASK_PRIO, NULL);
}
void hal_failed(const char *file, u2_t line)

View File

@ -14,33 +14,81 @@
#define _hal_esp32_h_
#include <stdint.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include "driver/spi_master.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct lmic_pinmap {
spi_host_device_t spi_host;
uint8_t nss;
uint8_t rxtx;
uint8_t rst;
uint8_t dio0;
uint8_t dio1;
int8_t rssi_cal; // cal in dB -- added to RSSI measured prior to decision. Must include noise guardband!
} lmic_pinmap;
typedef struct lmic_pinmap {
spi_host_device_t spi_host;
uint8_t nss;
uint8_t rxtx;
uint8_t rst;
uint8_t dio0;
uint8_t dio1;
int8_t rssi_cal; // cal in dB -- added to RSSI measured prior to decision. Must include noise guardband!
} lmic_pinmap;
extern lmic_pinmap lmic_pins;
extern lmic_pinmap lmic_pins;
void hal_startBgTask();
void hal_wakeUp();
void hal_initCriticalSection();
void hal_enterCriticalSection();
void hal_leaveCriticalSection();
#ifdef __cplusplus
}
#endif
enum HAL_Event {
DIO0 = 0,
DIO1,
DIO2,
TIMER,
WAKEUP
};
enum WaitKind {
CHECK_IO,
WAIT_FOR_ANY_EVENT,
WAIT_FOR_TIMER
};
class HAL_ESP32
{
public:
HAL_ESP32();
void init();
void startBackgroundTask();
void wakeUp();
void initCriticalSection();
void enterCriticalSection();
void leaveCriticalSection();
void spiWrite(uint8_t cmd, const uint8_t *buf, size_t len);
void spiRead(uint8_t cmd, uint8_t *buf, size_t len);
uint8_t checkTimer(uint32_t time);
void sleep();
void waitUntil(uint32_t time);
private:
static void backgroundTask(void* pvParameter);
static void dioIrqHandler(void* arg);
void ioInit();
void spiInit();
void timerInit();
void prepareNextAlarm(uint32_t time);
void armTimer();
void disarmTimer();
static void IRAM_ATTR timerIrqHandler(void *arg);
bool wait(WaitKind waitKind);
QueueHandle_t dioQueue;
spi_device_handle_t spiHandle;
spi_transaction_t spiTransaction;
uint64_t nextTimerEvent;
SemaphoreHandle_t mutex;
};
extern HAL_ESP32 ttn_hal;
#endif // _hal_esp32_h_

View File

@ -171,4 +171,11 @@
# endif // defined(LMIC_DISABLE_DR_LEGACY)
#endif // LMIC_DR_LEGACY
// LMIC_ENABLE_DeviceTimeReq
// enable support for MCMD_DeviceTimeReq and MCMD_DeviceTimeAns
// this is always defined, and non-zero to enable it.
#if !defined(LMIC_ENABLE_DeviceTimeReq)
# define LMIC_ENABLE_DeviceTimeReq 0
#endif
#endif // _lmic_config_h_

View File

@ -26,8 +26,12 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _hal_hpp_
#define _hal_hpp_
#ifndef _lmic_hal_h_
#define _lmic_hal_h_
#ifndef _oslmic_types_h_
# include "oslmic_types.h"
#endif
#ifdef __cplusplus
extern "C"{
@ -35,21 +39,20 @@ extern "C"{
/*
* initialize hardware (IO, SPI, TIMER, IRQ).
* This API is deprecated as it uses the const global lmic_pins,
* which the platform can't control or change.
*/
void hal_init (void);
/*
* initialize hardware, passing in platform-specific context
* Initialize hardware, passing in platform-specific context
* The pointer is to a HalPinmap_t.
*/
void hal_init_ex (const void *pContext);
/*
* drive radio NSS pin (0=low, 1=high).
*/
void hal_pin_nss (u1_t val);
/*
* drive radio RX/TX pins (0=rx, 1=tx).
* drive radio RX/TX pins (0=rx, 1=tx). Actual polarity
* is determined by the value of HalPinmap_t::rxtx_rx_active.
*/
void hal_pin_rxtx (u1_t val);
@ -58,29 +61,19 @@ void hal_pin_rxtx (u1_t val);
*/
void hal_pin_rst (u1_t val);
// BEGIN ttn-esp32 change
// use higher level SPI functions
/*
* perform SPI write transaction with radio
* Perform SPI write transaction with radio chip
* - write the command byte 'cmd'
* - write 'len' bytes in 'buf'
* - write 'len' bytes out of 'buf'
*/
void hal_spi_write(u1_t cmd, const u1_t* buf, int len);
void hal_spi_write(u1_t cmd, const u1_t* buf, size_t len);
/*
* perform SPI read transaction with radio
* Perform SPI read transaction with radio chip
* - write the command byte 'cmd'
* - read 'len' bytes into 'buf'
*/
void hal_spi_read(u1_t cmd, u1_t* buf, int len);
/*
* perform 8-bit SPI transaction with radio.
* - write given byte 'outval'
* - read byte and return value
*/
//u1_t hal_spi (u1_t outval);
// END ttn-esp32 change
void hal_spi_read(u1_t cmd, u1_t* buf, size_t len);
/*
* disable all CPU interrupts.
@ -128,8 +121,18 @@ void hal_failed (const char *file, u2_t line);
*/
s1_t hal_getRssiCal (void);
/*
* control the radio state
* - if val == 0, turn tcxo off and otherwise prepare for sleep
* - if val == 1, turn tcxo on and otherwise prep for activity
* - return the number of ticks that we need to wait
*/
ostime_t hal_setModuleActive (bit_t val);
bit_t hal_queryUsingTcxo(void);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // _hal_hpp_
#endif // _lmic_hal_h_

104
src/lmic/lmic.c Normal file → Executable file
View File

@ -713,6 +713,38 @@ scan_mac_cmds(
oidx += 2;
continue;
} /* end case */
case MCMD_DeviceTimeAns: {
#if LMIC_ENABLE_DeviceTimeReq
// don't process a spurious downlink.
if ( LMIC.txDeviceTimeReqState == lmic_RequestTimeState_rx ) {
// remember that it's time to notify the client.
LMIC.txDeviceTimeReqState = lmic_RequestTimeState_success;
// the network time is linked to the time of the last TX.
LMIC.localDeviceTime = LMIC.txend;
// save the network time.
// The first 4 bytes contain the seconds since the GPS epoch
// (i.e January the 6th 1980 at 00:00:00 UTC).
// Note: as per the LoRaWAN specs, the octet order for all
// multi-octet fields is little endian
// Note: the casts are necessary, because opts is an array of
// single byte values, and they might overflow when shifted
LMIC.netDeviceTime = ( (lmic_gpstime_t) opts[oidx + 1] ) |
(((lmic_gpstime_t) opts[oidx + 2]) << 8) |
(((lmic_gpstime_t) opts[oidx + 3]) << 16) |
(((lmic_gpstime_t) opts[oidx + 4]) << 24);
// The 5th byte contains the fractional seconds in 2^-8 second steps
LMIC.netDeviceTimeFrac = opts[oidx + 5];
#if LMIC_DEBUG_LEVEL > 0
LMIC_DEBUG_PRINTF("%"LMIC_PRId_ostime_t": MAC command DeviceTimeAns received: seconds_since_gps_epoch=%"PRIu32", fractional_seconds=%d\n", os_getTime(), LMIC.netDeviceTime, LMIC.netDeviceTimeFrac);
#endif
}
#endif // LMIC_ENABLE_DeviceTimeReq
oidx += 6;
continue;
} /* end case */
} /* end switch */
/* unrecognized mac commands fall out of switch to here */
EV(specCond, ERR, (e_.reason = EV::specCond_t::BAD_MAC_CMD,
@ -1139,7 +1171,8 @@ static bit_t processJoinAccept (void) {
LMIC.datarate = AS923_DR_SF10;
#endif
}
LMIC.opmode &= ~(OP_JOINING|OP_TRACK|OP_REJOIN|OP_TXRXPEND|OP_PINGINI) | OP_NEXTCHNL;
LMIC.opmode &= ~(OP_JOINING|OP_TRACK|OP_REJOIN|OP_TXRXPEND|OP_PINGINI);
LMIC.opmode |= OP_NEXTCHNL;
LMIC.txCnt = 0;
stateJustJoined();
LMIC.dn2Dr = LMIC.frame[OFF_JA_DLSET] & 0x0F;
@ -1328,6 +1361,13 @@ static void buildDataFrame (void) {
LMIC.txParamSetupAns = 0;
}
#endif
#if LMIC_ENABLE_DeviceTimeReq
if ( LMIC.txDeviceTimeReqState == lmic_RequestTimeState_tx ) {
LMIC.frame[end+0] = MCMD_DeviceTimeReq;
end += 1;
LMIC.txDeviceTimeReqState = lmic_RequestTimeState_rx;
}
#endif // LMIC_ENABLE_DeviceTimeReq
ASSERT(end <= OFF_DAT_OPTS+16);
u1_t flen = end + (txdata ? 5+dlen : 4);
@ -1587,6 +1627,24 @@ static bit_t processDnData (void) {
LMIC.dataBeg = LMIC.dataLen = 0;
txcomplete:
LMIC.opmode &= ~(OP_TXDATA|OP_TXRXPEND);
#if LMIC_ENABLE_DeviceTimeReq
lmic_request_time_state_t const requestTimeState = LMIC.txDeviceTimeReqState;
if ( requestTimeState != lmic_RequestTimeState_idle ) {
lmic_request_network_time_cb_t * const pNetworkTimeCb = LMIC.pNetworkTimeCb;
int flagSuccess = (LMIC.txDeviceTimeReqState == lmic_RequestTimeState_success);
LMIC.txDeviceTimeReqState = lmic_RequestTimeState_idle;
if (pNetworkTimeCb != NULL) {
// reset the callback, so that the user's routine
// can post another request if desired.
LMIC.pNetworkTimeCb = NULL;
// call the user's notification routine.
(*pNetworkTimeCb)(LMIC.pNetworkTimeUserData, flagSuccess);
}
}
#endif // LMIC_ENABLE_DeviceTimeReq
if( (LMIC.txrxFlags & (TXRX_DNW1|TXRX_DNW2|TXRX_PING)) != 0 && (LMIC.opmode & OP_LINKDEAD) != 0 ) {
LMIC.opmode &= ~OP_LINKDEAD;
reportEvent(EV_LINK_ALIVE);
@ -1728,12 +1786,11 @@ static void engineUpdate (void) {
#endif // !DISABLE_JOIN
ostime_t now = os_getTime();
ostime_t rxtime = 0;
ostime_t txbeg = 0;
// ttn-esp32: suppress unused variable
LMIC_UNREFERENCED_VARIABLE(rxtime);
#if !defined(DISABLE_BEACONS)
ostime_t rxtime = 0;
if( (LMIC.opmode & OP_TRACK) != 0 ) {
// We are tracking a beacon
ASSERT( now + RX_RAMPUP - LMIC.bcnRxtime <= 0 );
@ -1929,6 +1986,11 @@ void LMIC_reset (void) {
DO_DEVDB(LMIC.ping.dr, pingDr);
DO_DEVDB(LMIC.ping.intvExp, pingIntvExp);
#endif // !DISABLE_PING
#if LMIC_ENABLE_DeviceTimeReq
LMIC.txDeviceTimeReqState = lmic_RequestTimeState_idle;
LMIC.netDeviceTime = 0; // the "invalid" time.
LMIC.netDeviceTimeFrac = 0;
#endif // LMIC_ENABLE_DeviceTimeReq
}
@ -1970,7 +2032,6 @@ int LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed) {
return 0;
}
// Send a payload-less message to signal device is alive
void LMIC_sendAlive (void) {
LMIC.opmode |= OP_POLL;
@ -2068,3 +2129,36 @@ void LMIC_getSessionKeys (u4_t *netid, devaddr_t *devaddr, xref2u1_t nwkKey, xre
memcpy(artKey, LMIC.artKey, sizeof(LMIC.artKey));
memcpy(nwkKey, LMIC.nwkKey, sizeof(LMIC.nwkKey));
}
// \brief post an asynchronous request for the network time.
void LMIC_requestNetworkTime(lmic_request_network_time_cb_t *pCallbackfn, void *pUserData) {
#if LMIC_ENABLE_DeviceTimeReq
if (LMIC.txDeviceTimeReqState == lmic_RequestTimeState_idle) {
LMIC.txDeviceTimeReqState = lmic_RequestTimeState_tx;
LMIC.pNetworkTimeCb = pCallbackfn;
LMIC.pNetworkTimeUserData = pUserData;
return;
}
#endif // LMIC_ENABLE_DeviceTimeReq
// if no device time support, or if not in proper state,
// report a failure.
if (pCallbackfn != NULL)
(*pCallbackfn)(pUserData, /* false */ 0);
}
// \brief return local/remote time pair (if valid, and DeviceTimeReq enabled),
// return true for success, false for error. We adjust the sampled OS time
// back in time to the nearest second boundary.
int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference) {
#if LMIC_ENABLE_DeviceTimeReq
if (pReference != NULL && // valid parameter, and
LMIC.netDeviceTime != 0) { // ... we have a reasonable answer.
const ostime_t tAdjust = LMIC.netDeviceTimeFrac * ms2osticks(1000) / 256;
pReference->tLocal = LMIC.localDeviceTime - tAdjust;
pReference->tNetwork = LMIC.netDeviceTime;
return 1;
}
#endif // LMIC_ENABLE_DeviceTimeReq
return 0;
}

49
src/lmic/lmic.h Normal file → Executable file
View File

@ -1,7 +1,7 @@
/*
* Copyright (c) 2014-2016 IBM Corporation.
* Copyright (c) 2016 Matthijs Kooijman.
* Copyright (c) 2016-2018 MCCI Corporation.
* Copyright (c) 2016-2019 MCCI Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -105,7 +105,7 @@ extern "C"{
#define ARDUINO_LMIC_VERSION_CALC(major, minor, patch, local) \
(((major) << 24u) | ((minor) << 16u) | ((patch) << 8u) | (local))
#define ARDUINO_LMIC_VERSION ARDUINO_LMIC_VERSION_CALC(2, 2, 2, 0)
#define ARDUINO_LMIC_VERSION ARDUINO_LMIC_VERSION_CALC(2, 3, 2, 0) /* v2.3.2 */
#define ARDUINO_LMIC_VERSION_GET_MAJOR(v) \
(((v) >> 24u) & 0xFFu)
@ -243,6 +243,35 @@ enum {
MAX_CLOCK_ERROR = 65536,
};
// network time request callback function
// defined unconditionally, because APIs and types can't change based on config.
// This is called when a time-request succeeds or when we get a downlink
// without time request, "completing" the pending time request.
typedef void LMIC_ABI_STD lmic_request_network_time_cb_t(void *pUserData, int flagSuccess);
// how the network represents time.
typedef u4_t lmic_gpstime_t;
// rather than deal with 1/256 second tick, we adjust ostime back
// (as it's high res) to match tNetwork.
typedef struct lmic_time_reference_s lmic_time_reference_t;
struct lmic_time_reference_s {
// our best idea of when we sent the uplink (end of packet).
ostime_t tLocal;
// the network's best idea of when we sent the uplink.
lmic_gpstime_t tNetwork;
};
enum lmic_request_time_state_e {
lmic_RequestTimeState_idle = 0, // we're not doing anything
lmic_RequestTimeState_tx, // we want to tx a time request on next uplink
lmic_RequestTimeState_rx, // we have tx'ed, next downlink completes.
lmic_RequestTimeState_success // we sucessfully got time.
};
typedef u1_t lmic_request_time_state_t;
struct lmic_t {
// Radio settings TX/RX (also accessed by HAL)
ostime_t txend;
@ -306,6 +335,14 @@ struct lmic_t {
devaddr_t devaddr;
u4_t seqnoDn; // device level down stream seqno
u4_t seqnoUp;
#if LMIC_ENABLE_DeviceTimeReq
// put here for alignment, to reduce RAM use.
ostime_t localDeviceTime; // the LMIC.txend value for last DeviceTimeAns
lmic_gpstime_t netDeviceTime; // the netDeviceTime for lastDeviceTimeAns
// zero ==> not valid.
lmic_request_network_time_cb_t *pNetworkTimeCb; // call-back routine
void *pNetworkTimeUserData; // call-back data
#endif // LMIC_ENABLE_DeviceTimeReq
u1_t dnConf; // dn frame confirm pending: LORA::FCT_ACK or 0
s1_t adrAckReq; // counter until we reset data rate (0=off)
@ -329,6 +366,10 @@ struct lmic_t {
bit_t txParamSetupAns; // transmit setup answer pending.
u1_t txParam; // the saved TX param byte.
#endif
#if LMIC_ENABLE_DeviceTimeReq
lmic_request_time_state_t txDeviceTimeReqState; // current state, initially idle.
u1_t netDeviceTimeFrac; // updated on any DeviceTimeAns.
#endif
// rx1DrOffset is the offset from uplink to downlink datarate
u1_t rx1DrOffset; // captured from join. zero by default.
@ -368,6 +409,7 @@ struct lmic_t {
u1_t noRXIQinversion;
};
//! \var struct lmic_t LMIC
//! The state of LMIC MAC layer is encapsulated in this variable.
DECLARE_LMIC; //!< \internal
@ -417,6 +459,9 @@ u4_t LMIC_getSeqnoUp (void);
u4_t LMIC_setSeqnoUp (u4_t);
void LMIC_getSessionKeys (u4_t *netid, devaddr_t *devaddr, xref2u1_t nwkKey, xref2u1_t artKey);
void LMIC_requestNetworkTime(lmic_request_network_time_cb_t *pCallbackfn, void *pUserData);
int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference);
// Declare onEvent() function, to make sure any definition will have the
// C conventions, even when in a C++ file.
DECL_ON_LMIC_EVENT;

0
src/lmic/lmic_as923.c Normal file → Executable file
View File

0
src/lmic/lmic_au921.c Normal file → Executable file
View File

31
src/lmic/lmic_config_preconditions.h Normal file → Executable file
View File

@ -114,6 +114,12 @@ Revision history:
// following values. These are in order of the sections in the manual. Not all of the
// below are supported yet.
//
// CFG_as923jp is treated as a special case of CFG_as923, so it's not included in
// the below.
//
// TODO(tmm@mcci.com) consider moving this block to a central file as it's not
// user-editable.
//
# define CFG_LMIC_REGION_MASK \
((defined(CFG_eu868) << LMIC_REGION_eu868) | \
(defined(CFG_us915) << LMIC_REGION_us915) | \
@ -127,6 +133,8 @@ Revision history:
0)
// the selected region.
// TODO(tmm@mcci.com) consider moving this block to a central file as it's not
// user-editable.
#if defined(CFG_eu868)
# define CFG_region LMIC_REGION_eu868
#elif defined(CFG_us915)
@ -139,6 +147,10 @@ Revision history:
# define CFG_region LMIC_REGION_au921
#elif defined(CFG_cn490)
# define CFG_region LMIC_REGION_cn490
#elif defined(CFG_as923jp)
# define CFG_as923 1 /* CFG_as923jp implies CFG_as923 */
# define CFG_region LMIC_REGION_as923
# define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP
#elif defined(CFG_as923)
# define CFG_region LMIC_REGION_as923
#elif defined(CFG_kr921)
@ -149,7 +161,11 @@ Revision history:
# define CFG_region 0
#endif
// finally the mask of` US-like and EU-like regions
// a bitmask of EU-like regions -- these are regions which have up to 16
// channels indidually programmable via downloink.
//
// TODO(tmm@mcci.com) consider moving this block to a central file as it's not
// user-editable.
#define CFG_LMIC_EU_like_MASK ( \
(1 << LMIC_REGION_eu868) | \
/* (1 << LMIC_REGION_us915) | */ \
@ -162,6 +178,12 @@ Revision history:
(1 << LMIC_REGION_in866) | \
0)
// a bitmask of` US-like regions -- these are regions with 64 fixed 125 kHz channels
// overlaid by 8 500 kHz channels. The channel frequencies can't be changed, but
// subsets of channels can be selected via masks.
//
// TODO(tmm@mcci.com) consider moving this block to a central file as it's not
// user-editable.
#define CFG_LMIC_US_like_MASK ( \
/* (1 << LMIC_REGION_eu868) | */ \
(1 << LMIC_REGION_us915) | \
@ -174,9 +196,12 @@ Revision history:
/* (1 << LMIC_REGION_in866) | */ \
0)
//
// booleans that are true if the configured region is EU-like or US-like.
// TODO(tmm@mcci.com) consider moving this block to a central file as it's not
// user-editable.
//
#define CFG_LMIC_EU_like (!!(CFG_LMIC_REGION_MASK & CFG_LMIC_EU_like_MASK))
#define CFG_LMIC_US_like (!!(CFG_LMIC_REGION_MASK & CFG_LMIC_US_like_MASK))
#endif /* _LMIC_CONFIG_PRECONDITIONS_H_ */

217
src/lmic/lmic_env.h Executable file
View File

@ -0,0 +1,217 @@
/*
Module: lmic_env.h
Function:
Sets up macros etc. to make things a little easier for portabilty
Copyright notice and license info:
See LICENSE file accompanying this project.
Author:
Terry Moore, MCCI Corporation November 2018
Description:
This file is an adaptation of MCCI's standard IOCTL framework.
We duplicate a bit of functionality that we might get from other
libraries, so that the LMIC library can continue to stand alone.
*/
#ifndef _lmic_env_h_ /* prevent multiple includes */
#define _lmic_env_h_
/*
Macro: LMIC_C_ASSERT()
Function:
Declaration-like macro that will cause a compile error if arg is FALSE.
Definition:
LMIC_C_ASSERT(
BOOL fErrorIfFalse
);
Description:
This macro, if used where an external reference declarataion is
permitted, will either compile cleanly, or will cause a compilation
error. The results of using this macro where a declaration is not
permitted are unspecified.
This is different from #if !(fErrorIfFalse) / #error in that the
expression is evaluated by the compiler rather than by the pre-
processor. Therefore things like sizeof() can be used.
Returns:
No explicit result -- either compiles cleanly or causes a compile
error.
*/
#ifndef LMIC_C_ASSERT
# define LMIC_C_ASSERT(e) \
void LMIC_C_ASSERT__(int LMIC_C_ASSERT_x[(e) ? 1: -1])
#endif
/****************************************************************************\
|
| Define the begin/end declaration tags for C++ co-existance
|
\****************************************************************************/
#ifdef __cplusplus
# define LMIC_BEGIN_DECLS extern "C" {
# define LMIC_END_DECLS }
#else
# define LMIC_BEGIN_DECLS /* nothing */
# define LMIC_END_DECLS /* nothing */
#endif
//----------------------------------------------------------------------------
// Annotations to avoid various "unused" warnings. These must appear as a
// statement in the function body; the macro annotates the variable to quiet
// compiler warnings. The way this is done is compiler-specific, and so these
// definitions are fall-backs, which might be overridden.
//
// Although these are all similar, we don't want extra macro expansions,
// so we define each one explicitly rather than relying on a common macro.
//----------------------------------------------------------------------------
// signal that a parameter is intentionally unused.
#ifndef LMIC_UNREFERENCED_PARAMETER
# define LMIC_UNREFERENCED_PARAMETER(v) do { (void) (v); } while (0)
#endif
// an API parameter is a parameter that is required by an API definition, but
// happens to be unreferenced in this implementation. This is a stronger
// assertion than LMIC_UNREFERENCED_PARAMETER(): this parameter is here
// becuase of an API contract, but we have no use for it in this function.
#ifndef LMIC_API_PARAMETER
# define LMIC_API_PARAMETER(v) do { (void) (v); } while (0)
#endif
// an intentionally-unreferenced variable.
#ifndef LMIC_UNREFERENCED_VARIABLE
# define LMIC_UNREFERENCED_VARIABLE(v) do { (void) (v); } while (0)
#endif
// we have three (!) debug levels (LMIC_DEBUG_LEVEL > 0, LMIC_DEBUG_LEVEL > 1,
// and LMIC_X_DEBUG_LEVEL > 0. In each case we might have parameters or
// or varables that are only refereneced at the target debug level.
// Parameter referenced only if debugging at level > 0.
#ifndef LMIC_DEBUG1_PARAMETER
# if LMIC_DEBUG_LEVEL > 0
# define LMIC_DEBUG1_PARAMETER(v) do { ; } while (0)
# else
# define LMIC_DEBUG1_PARAMETER(v) do { (void) (v); } while (0)
# endif
#endif
// variable referenced only if debugging at level > 0
#ifndef LMIC_DEBUG1_VARIABLE
# if LMIC_DEBUG_LEVEL > 0
# define LMIC_DEBUG1_VARIABLE(v) do { ; } while (0)
# else
# define LMIC_DEBUG1_VARIABLE(v) do { (void) (v); } while (0)
# endif
#endif
// parameter referenced only if debugging at level > 1
#ifndef LMIC_DEBUG2_PARAMETER
# if LMIC_DEBUG_LEVEL > 1
# define LMIC_DEBUG2_PARAMETER(v) do { ; } while (0)
# else
# define LMIC_DEBUG2_PARAMETER(v) do { (void) (v); } while (0)
# endif
#endif
// variable referenced only if debugging at level > 1
#ifndef LMIC_DEBUG2_VARIABLE
# if LMIC_DEBUG_LEVEL > 1
# define LMIC_DEBUG2_VARIABLE(v) do { ; } while (0)
# else
# define LMIC_DEBUG2_VARIABLE(v) do { (void) (v); } while (0)
# endif
#endif
// parameter referenced only if LMIC_X_DEBUG_LEVEL > 0
#ifndef LMIC_X_DEBUG_PARAMETER
# if LMIC_X_DEBUG_LEVEL > 0
# define LMIC_X_DEBUG_PARAMETER(v) do { ; } while (0)
# else
# define LMIC_X_DEBUG_PARAMETER(v) do { (void) (v); } while (0)
# endif
#endif
// variable referenced only if LMIC_X_DEBUG_LEVEL > 0
#ifndef LMIC_X_DEBUG_VARIABLE
# if LMIC_X_DEBUG_LEVEL > 0
# define LMIC_X_DEBUG_VARIABLE(v) do { ; } while (0)
# else
# define LMIC_X_DEBUG_VARIABLE(v) do { (void) (v); } while (0)
# endif
#endif
// parameter referenced only if EV() macro is enabled (which it never is)
// TODO(tmm@mcci.com) take out the EV() framework as it reuqires C++, and
// this code is really C-99 to its bones.
#ifndef LMIC_EV_PARAMETER
# define LMIC_EV_PARAMETER(v) do { (void) (v); } while (0)
#endif
// variable referenced only if EV() macro is defined.
#ifndef LMIC_EV_VARIABLE
# define LMIC_EV_VARIABLE(v) do { (void) (v); } while (0)
#endif
/*
Macro: LMIC_ABI_STD
Index: Macro: LMIC_ABI_VARARGS
Function:
Annotation macros to force a particular binary calling sequence.
Definition:
#define LMIC_ABI_STD compiler-specific
#define LMIC_ABI_VARARGS compiler-specific
Description:
These macros are used when declaring a function type, and indicate
that a particular calling sequence is to be used. They are normally
used between the type portion of the function declaration and the
name of the function. For example:
typedef void LMIC_ABI_STD myCallBack_t(void);
It's important to use this in libraries on platforms with multiple
calling sequences, because different components can be compiled with
different defaults.
Returns:
Not applicable.
*/
/* ABI marker for normal (fixed parameter count) functions -- used for function types */
#ifndef LMIC_ABI_STD
# ifdef _MSC_VER
# define LMIC_ABI_STD __stdcall
# else
# define LMIC_ABI_STD /* nothing */
# endif
#endif
/* ABI marker for VARARG functions -- used for function types */
#ifndef LMIC_ABI_VARARGS
# ifdef _MSC_VER
# define LMIC_ABI_VARARGS __cdecl
# else
# define LMIC_ABI_VARARGS /* nothing */
# endif
#endif
#endif /* _lmic_env_h_ */

0
src/lmic/lmic_eu_like.c Normal file → Executable file
View File

0
src/lmic/lmic_in866.c Normal file → Executable file
View File

0
src/lmic/lmic_us915.c Normal file → Executable file
View File

0
src/lmic/lmic_us_like.c Normal file → Executable file
View File

View File

@ -9,7 +9,7 @@ Copyright & License:
See accompanying LICENSE file.
Author:
Terry Moore, MCCI September 2019
Terry Moore, MCCI September 2018
*/

2
src/lmic/lorabase.h Normal file → Executable file
View File

@ -449,6 +449,7 @@ enum {
MCMD_RXTimingSetupAns = 0x08, // : -
MCMD_TxParamSetupAns = 0x09, // : -
MCMD_DIChannelAns = 0x0A, // : u1: [7-2]:RFU 1:exists 0:OK
MCMD_DeviceTimeReq = 0x0D,
// Class B
MCMD_PING_IND = 0x10, // - pingability indic : u1: 7=RFU, 6-4:interval, 3-0:datarate
@ -468,6 +469,7 @@ enum {
MCMD_RXTimingSetupReq = 0x08, // : u1: [7-4]:RFU [3-0]: Delay 1-15s (0 => 1)
MCMD_TxParamSetupReq = 0x09, // : u1: [7-6]:RFU [5:4]: dl dwell/ul dwell [3:0] max EIRP
MCMD_DIChannelReq = 0x0A, // : u1: channel, u3: frequency
MCMD_DeviceTimeAns = 0x0D,
// Class B
MCMD_PING_SET = 0x11, // set ping freq : u3: freq

0
src/lmic/oslmic.c Normal file → Executable file
View File

132
src/lmic/oslmic.h Normal file → Executable file
View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2014-2016 IBM Corporation.
* Copyright (c) 2018 MCCI Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -29,29 +30,22 @@
#ifndef _oslmic_h_
#define _oslmic_h_
// Dependencies required for the LoRa MAC in C to run.
// Dependencies required for the LMIC to run.
// These settings can be adapted to the underlying system.
// You should not, however, change the lmic.[hc]
// You should not, however, change the lmic merely for porting purposes.[hc]
#include "config.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C"{
#ifndef _lmic_env_h_
# include "lmic_env.h"
#endif
//================================================================================
//================================================================================
// Target platform as C library
typedef uint8_t bit_t;
typedef uint8_t u1_t;
typedef int8_t s1_t;
typedef uint16_t u2_t;
typedef int16_t s2_t;
typedef uint32_t u4_t;
typedef int32_t s4_t;
typedef unsigned int uint;
typedef const char* str_t;
#ifndef _oslmic_types_h_
# include "oslmic_types.h"
#endif
LMIC_BEGIN_DECLS
#include <string.h>
#include "hal.h"
@ -73,7 +67,6 @@ typedef struct rxsched_t rxsched_t;
typedef struct bcninfo_t bcninfo_t;
typedef const u1_t* xref2cu1_t;
typedef u1_t* xref2u1_t;
typedef s4_t ostime_t;
// int32_t == s4_t is long on some platforms; and someday
// we will want 64-bit ostime_t. So, we will use a macro for the
@ -91,105 +84,6 @@ typedef s4_t ostime_t;
#define SIZEOFEXPR(x) sizeof(x)
//----------------------------------------------------------------------------
// Annotations to avoid various "unused" warnings. These must appear as a
// statement in the function body; the macro annotates the variable to quiet
// compiler warnings. The way this is done is compiler-specific, and so these
// definitions are fall-backs, which might be overridden.
//
// Although these are all similar, we don't want extra macro expansions,
// so we define each one explicitly rather than relying on a common macro.
//----------------------------------------------------------------------------
// signal that a parameter is intentionally unused.
#ifndef LMIC_UNREFERENCED_PARAMETER
# define LMIC_UNREFERENCED_PARAMETER(v) do { (void) (v); } while (0)
#endif
// an API parameter is a parameter that is required by an API definition, but
// happens to be unreferenced in this implementation. This is a stronger
// assertion than LMIC_UNREFERENCED_PARAMETER(): this parameter is here
// becuase of an API contract, but we have no use for it in this function.
#ifndef LMIC_API_PARAMETER
# define LMIC_API_PARAMETER(v) do { (void) (v); } while (0)
#endif
// an intentionally-unreferenced variable.
#ifndef LMIC_UNREFERENCED_VARIABLE
# define LMIC_UNREFERENCED_VARIABLE(v) do { (void) (v); } while (0)
#endif
// we have three (!) debug levels (LMIC_DEBUG_LEVEL > 0, LMIC_DEBUG_LEVEL > 1,
// and LMIC_X_DEBUG_LEVEL > 0. In each case we might have parameters or
// or varables that are only refereneced at the target debug level.
// Parameter referenced only if debugging at level > 0.
#ifndef LMIC_DEBUG1_PARAMETER
# if LMIC_DEBUG_LEVEL > 0
# define LMIC_DEBUG1_PARAMETER(v) do { ; } while (0)
# else
# define LMIC_DEBUG1_PARAMETER(v) do { (void) (v); } while (0)
# endif
#endif
// variable referenced only if debugging at level > 0
#ifndef LMIC_DEBUG1_VARIABLE
# if LMIC_DEBUG_LEVEL > 0
# define LMIC_DEBUG1_VARIABLE(v) do { ; } while (0)
# else
# define LMIC_DEBUG1_VARIABLE(v) do { (void) (v); } while (0)
# endif
#endif
// parameter referenced only if debugging at level > 1
#ifndef LMIC_DEBUG2_PARAMETER
# if LMIC_DEBUG_LEVEL > 1
# define LMIC_DEBUG2_PARAMETER(v) do { ; } while (0)
# else
# define LMIC_DEBUG2_PARAMETER(v) do { (void) (v); } while (0)
# endif
#endif
// variable referenced only if debugging at level > 1
#ifndef LMIC_DEBUG2_VARIABLE
# if LMIC_DEBUG_LEVEL > 1
# define LMIC_DEBUG2_VARIABLE(v) do { ; } while (0)
# else
# define LMIC_DEBUG2_VARIABLE(v) do { (void) (v); } while (0)
# endif
#endif
// parameter referenced only if LMIC_X_DEBUG_LEVEL > 0
#ifndef LMIC_X_DEBUG_PARAMETER
# if LMIC_X_DEBUG_LEVEL > 0
# define LMIC_X_DEBUG_PARAMETER(v) do { ; } while (0)
# else
# define LMIC_X_DEBUG_PARAMETER(v) do { (void) (v); } while (0)
# endif
#endif
// variable referenced only if LMIC_X_DEBUG_LEVEL > 0
#ifndef LMIC_X_DEBUG_VARIABLE
# if LMIC_X_DEBUG_LEVEL > 0
# define LMIC_X_DEBUG_VARIABLE(v) do { ; } while (0)
# else
# define LMIC_X_DEBUG_VARIABLE(v) do { (void) (v); } while (0)
# endif
#endif
// parameter referenced only if EV() macro is enabled (which it never is)
// TODO(tmm@mcci.com) take out the EV() framework as it reuqires C++, and
// this code is really C-99 to its bones.
#ifndef LMIC_EV_PARAMETER
# define LMIC_EV_PARAMETER(v) do { (void) (v); } while (0)
#endif
// variable referenced only if EV() macro is defined.
#ifndef LMIC_EV_VARIABLE
# define LMIC_EV_VARIABLE(v) do { (void) (v); } while (0)
#endif
#define ON_LMIC_EVENT(ev) onEvent(ev)
#define DECL_ON_LMIC_EVENT void onEvent(ev_t e)
@ -416,8 +310,6 @@ extern xref2u1_t AESaux;
u4_t os_aes (u1_t mode, xref2u1_t buf, u2_t len);
#endif
#ifdef __cplusplus
} // extern "C"
#endif
LMIC_END_DECLS
#endif // _oslmic_h_

47
src/lmic/oslmic_types.h Executable file
View File

@ -0,0 +1,47 @@
/*
Module: oslmic_types.h
Function:
Basic types from oslmic.h, shared by all layers.
Copyright & License:
See accompanying LICENSE file.
Author:
Terry Moore, MCCI November 2018
(based on oslmic.h from IBM).
*/
#ifndef _oslmic_types_h_
# define _oslmic_types_h_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
//================================================================================
//================================================================================
// Target platform as C library
typedef uint8_t bit_t;
typedef uint8_t u1_t;
typedef int8_t s1_t;
typedef uint16_t u2_t;
typedef int16_t s2_t;
typedef uint32_t u4_t;
typedef int32_t s4_t;
typedef unsigned int uint;
typedef const char* str_t;
// the HAL needs to give us ticks, so it ought to know the right type.
typedef s4_t ostime_t;
#ifdef __cplusplus
}
#endif
/* end of oslmic_types.h */
#endif /* _oslmic_types_h_ */

74
src/lmic/radio.c Normal file → Executable file
View File

@ -135,13 +135,22 @@
// #define RegAgcThresh2 0x45 // common
// #define RegAgcThresh3 0x46 // common
// #define RegPllHop 0x4B // common
#define RegPaDac 0x4D // common
// #define RegTcxo 0x58 // common
// #define RegPll 0x5C // common
// #define RegPllLowPn 0x5E // common
// #define RegFormerTemp 0x6C // common
// #define RegBitRateFrac 0x70 // common
#if defined(CFG_sx1276_radio)
#define RegTcxo 0x4B // common
#define RegPaDac 0x4D // common
#elif defined(CFG_sx1272_radio)
#define RegTcxo 0x58 // common
#define RegPaDac 0x5A // common
#endif
#define RegTcxo_TcxoInputOn (1u << 4)
// ----------------------------------------
// spread factors and mode for RegModemConfig2
#define SX1272_MC2_FSK 0x00
@ -290,58 +299,41 @@ static u1_t randbuf[16];
static void writeReg (u1_t addr, u1_t data ) {
// ttn-esp32 change: higher level SPI interface
hal_spi_write(addr | 0x80, &data, 1);
/*
hal_pin_nss(0);
hal_spi(addr | 0x80);
hal_spi(data);
hal_pin_nss(1);
*/
}
static u1_t readReg (u1_t addr) {
// ttn-esp32 change: higher level SPI interface
u1_t buf[1];
hal_spi_read(addr & 0x7f, buf, 1);
return buf[0];
/*
hal_pin_nss(0);
hal_spi(addr & 0x7F);
u1_t val = hal_spi(0x00);
hal_pin_nss(1);
return val;
*/
}
static void writeBuf (u1_t addr, xref2u1_t buf, u1_t len) {
// ttn-esp32 change: higher level SPI interface
hal_spi_write(addr | 0x80, buf, len);
/*
hal_pin_nss(0);
hal_spi(addr | 0x80);
for (u1_t i=0; i<len; i++) {
hal_spi(buf[i]);
}
hal_pin_nss(1);
*/
}
static void readBuf (u1_t addr, xref2u1_t buf, u1_t len) {
// ttn-esp32 change: higher level SPI interface
hal_spi_read(addr & 0x7f, buf, len);
/*
hal_pin_nss(0);
hal_spi(addr & 0x7F);
for (u1_t i=0; i<len; i++) {
buf[i] = hal_spi(0x00);
}
hal_pin_nss(1);
*/
}
static void requestModuleActive(bit_t state) {
ostime_t const ticks = hal_setModuleActive(state);
if (ticks)
hal_waitUntil(os_getTime() + ticks);;
}
static void writeOpmode(u1_t mode) {
u1_t const maskedMode = mode & OPMODE_MASK;
if (maskedMode != OPMODE_SLEEP)
requestModuleActive(1);
writeReg(RegOpMode, mode);
if (maskedMode == OPMODE_SLEEP)
requestModuleActive(0);
}
static void opmode (u1_t mode) {
writeReg(RegOpMode, (readReg(RegOpMode) & ~OPMODE_MASK) | mode);
writeOpmode((readReg(RegOpMode) & ~OPMODE_MASK) | mode);
}
static void opmodeLora() {
@ -349,7 +341,7 @@ static void opmodeLora() {
#ifdef CFG_sx1276_radio
u |= 0x8; // TBD: sx1276 high freq
#endif
writeReg(RegOpMode, u);
writeOpmode(u);
}
static void opmodeFSK() {
@ -357,7 +349,7 @@ static void opmodeFSK() {
#ifdef CFG_sx1276_radio
u |= 0x8; // TBD: sx1276 high freq
#endif
writeReg(RegOpMode, u);
writeOpmode(u);
}
// configure LoRa modem (cfg1, cfg2)
@ -483,7 +475,7 @@ static void configPower () {
static void txfsk () {
// select FSK modem (from sleep mode)
writeReg(RegOpMode, 0x10); // FSK, BT=0.5
writeOpmode(0x10); // FSK, BT=0.5
ASSERT(readReg(RegOpMode) == 0x10);
// enter standby mode (required for FIFO loading))
opmode(OPMODE_STANDBY);
@ -765,6 +757,8 @@ static void startrx (u1_t rxmode) {
int radio_init () {
hal_disableIRQs();
requestModuleActive(1);
// manually reset radio
#ifdef CFG_sx1276_radio
hal_pin_rst(0); // drive RST pin low
@ -788,6 +782,10 @@ int radio_init () {
#else
#error Missing CFG_sx1272_radio/CFG_sx1276_radio
#endif
// set the tcxo input, if needed
if (hal_queryUsingTcxo())
writeReg(RegTcxo, readReg(RegTcxo) | RegTcxo_TcxoInputOn);
// seed 15-byte randomness via noise rssi
rxlora(RXMODE_RSSI);
while( (readReg(RegOpMode) & OPMODE_MASK) != OPMODE_RX ); // continuous rx

View File

@ -1,36 +0,0 @@
/*******************************************************************************
*
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
*
* Copyright (c) 2018 Manuel Bleichenbacher
*
* Licensed under MIT License
* https://opensource.org/licenses/MIT
*
* Task listening on a UART port for provisioning commands.
*******************************************************************************/
#ifndef _provision_task_h_
#define _provision_task_h_
#include "lmic/oslmic.h"
#ifdef __cplusplus
extern "C" {
#endif
void provisioning_start_task();
bool provisioning_have_keys();
bool provisioning_decode_keys(const char *dev_eui, const char *app_eui, const char *app_key);
bool provisioning_from_mac(const char *app_eui, const char *app_key);
bool provisioning_save_keys();
bool provisioning_restore_keys(bool silent);
#ifdef __cplusplus
}
#endif
#endif