Skip to content

Commit

Permalink
Feature/port sntp to zig (#8)
Browse files Browse the repository at this point in the history
* ntp code

* Removed sntp from wifi service and moved it to the user task state machine

* Updating Lwm2m system

* Modifying the connection to improve the dependency flow

* mqtt build

* Improved the reconnection logic

* Stabilizing re-connection logic in lwm2m

* moving picolibc around

* Update toolchain

* Update Wakaama

* Update FreeRTOS config

* Attempt to get better stability

* Working on stability

* Update the fw

* Fix this

* Merging

* Working for more stability

* attempting stabilization of lwm2m

* improvements
  • Loading branch information
FranciscoLlobet authored Feb 4, 2024
1 parent 4f1e6ca commit 7f6e9bc
Show file tree
Hide file tree
Showing 196 changed files with 1,673 additions and 1,567 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/zig-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ jobs:
with:
python-version: '3.x'
cache: 'pip'

# Not necessary for this workflow, but useful for debugging
- name: Install arm-none-eabi-gcc
run: |
sudo apt-get install -y gcc-arm-none-eabi clang-format
# Installs dependencies using pip
- name: Install deps
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@
[submodule "csrc/picolibc"]
path = csrc/picolibc
url = [email protected]:FranciscoLlobet/picolibc.git
[submodule "toolchain/picolibc"]
path = toolchain/picolibc
url = https://github.com/FranciscoLlobet/picolibc.git
15 changes: 13 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@
"miso_mbedtls_config.h": "c",
"freertos.h": "c",
"freertosconfig.h": "c",
"os_malloc.h": "c"
"os_malloc.h": "c",
"trace.h": "c",
"timers.h": "c",
"semphr.h": "c",
"task.h": "c",
"miso_ntp.h": "c",
"internals.h": "c",
"liblwm2m.h": "c",
"__locale": "c",
"string": "c",
"string_view": "c"
},
"cmake.configureOnOpen": false
"cmake.configureOnOpen": false,
"cortex-debug.variableUseNaturalFormat": true
}
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ zig build

### Wifi Connectivity

- TI Simplelink CC3100-SDK
- [TI Simplelink CC3100-SDK](https://www.ti.com/tool/CC3100SDK)

### Filesystem

- [FatFs 15](http://elm-chan.org/fsw/ff/00index_e.html). The standard FatFS driver for embedded devices.
- [FatFs 15](http://elm-chan.org/fsw/ff/00index_e.html).

### Utils

Expand All @@ -138,17 +138,17 @@ zig build

Configuration can be loaded via SD card by `config.txt`

| Field | Description | Type | |
|----------------|------------------------|-----------------------|-----------|
| wifi.ssid | Wifi WPA2 SSID | String | Mandadory |
| wifi.key | Wifi WPA2 SSID Key | String | Mandatory |
| Field | Description | Type | |
|----------------|------------------------|-----------------------|----------------------|
| wifi.ssid | Wifi WPA2 SSID | String | Mandadory |
| wifi.key | Wifi WPA2 SSID Key | String | Mandatory |
| lwm2m.endpoint | LWM2M endpoint | String | If LwM2M is compiled |
| lwm2m.uri | LWM2M Server Uri | URI String | If LwM2M is compiled |
| lwm2m.psk.id | LwM2M DTLS Psk ID | String | Optional |
| lwm2m.psk.key | LwM2M DTLS PSK Key | Base64 encoded String | Optional |
| ntp.url[] | sNTP server URI/URL | URI String Array | Mandatory |
| http.uri | Firmware image URI | URI String | Mandatory |
| http.key | Firmware public key | Base64 encoded string | Mandatory |
| lwm2m.psk.id | LwM2M DTLS Psk ID | String | Optional |
| lwm2m.psk.key | LwM2M DTLS PSK Key | Base64 encoded String | Optional |
| ntp.url[] | sNTP server URI/URL | URI String Array | Not implemented |
| http.uri | Firmware image URI | URI String | Mandatory |
| http.key | Firmware public key | Base64 encoded string | Mandatory |

```json
{
Expand Down Expand Up @@ -186,7 +186,7 @@ Configuration can be loaded via SD card by `config.txt`
}
},
"http": {
"url": "http://192.168.50.133:80/app.bin",
"url": "http://HTTP_HOST:80/app.bin",
"key": "APP_PUB_KEY_BASE64"
}
}
Expand Down
12 changes: 5 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ pub fn build(b: *std.Build) !void {

// Core C-source
"csrc/src/config.c",
"csrc/src/miso_ntp.c",
"csrc/src/sntp_packet.c",
"csrc/src/network.c",
"csrc/src/wifi_service.c",
};

const c_flags = [_][]const u8{ "-O2", "-DEFM32GG390F1024", "-DSL_CATALOG_POWER_MANAGER_PRESENT=1", "-fdata-sections", "-ffunction-sections", "-DMISO_APPLICATION" };
const c_flags = [_][]const u8{ "-O2", "-DEFM32GG390F1024", "-DSL_CATALOG_POWER_MANAGER_PRESENT=1", "-fdata-sections", "-ffunction-sections", "-DMISO_APPLICATION", "-fstack-protector-all" };

const c_flags_boot = [_][]const u8{ "-O2", "-DEFM32GG390F1024", "-DSL_CATALOG_POWER_MANAGER_PRESENT=1", "-fdata-sections", "-ffunction-sections", "-DMISO_BOOTLOADER" };

Expand Down Expand Up @@ -90,8 +88,8 @@ pub fn build(b: *std.Build) !void {
};

const bootloader = microzig.addFirmware(b, bootloader_target);
bootloader.addSystemIncludePath(.{ .path = "toolchain/picolibc/include" });
bootloader.addObjectFile(.{ .path = "toolchain/picolibc/libc.a" });
bootloader.addSystemIncludePath(.{ .path = "toolchain/clang-compiled/picolibc/include" });
bootloader.addObjectFile(.{ .path = "toolchain/clang-compiled/picolibc/libc.a" });
bootloader.addObjectFile(.{ .path = "csrc/system/gecko_sdk/emdrv/nvm3/lib/libnvm3_CM3_gcc.a" });

for (include_path_array) |path| {
Expand All @@ -115,8 +113,8 @@ pub fn build(b: *std.Build) !void {

const application = microzig.addFirmware(b, application_target);

application.addSystemIncludePath(.{ .path = "toolchain/picolibc/include" });
application.addObjectFile(.{ .path = "toolchain/picolibc/libc.a" });
application.addSystemIncludePath(.{ .path = "toolchain/clang-compiled/picolibc/include" });
application.addObjectFile(.{ .path = "toolchain/clang-compiled/picolibc/libc.a" });
application.addObjectFile(.{ .path = "csrc/system/gecko_sdk/emdrv/nvm3/lib/libnvm3_CM3_gcc.a" });

for (include_path_array) |path| {
Expand Down
2 changes: 1 addition & 1 deletion build_cc3100_sdk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const source_path = [_][]const u8{
"csrc/system/cc3100-sdk/simplelink/source/wlan.c",
};

const c_flags = [_][]const u8{ "-DEFM32GG390F1024", "-D__OSI__=1", "-D__SL__", "-DOS_USE_TRACE_ITM", "-fdata-sections", "-ffunction-sections" };
const c_flags = [_][]const u8{ "-O0", "-DEFM32GG390F1024", "-D__OSI__=1", "-D__SL__", "-fdata-sections", "-ffunction-sections", "-fstack-protector-all" };

pub fn aggregate(exe: *microzig.Firmware) void {
for (include_path) |path| {
Expand Down
2 changes: 1 addition & 1 deletion build_mbedtls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const source_path = [_][]const u8{
"csrc/src/mbedtls_adapter/treading.c",
};

const c_flags = [_][]const u8{ "-DMBEDTLS_CONFIG_FILE=\"miso_mbedtls_config.h\"", "-DEFM32GG390F1024", "-Os", "-fdata-sections", "-ffunction-sections" };
const c_flags = [_][]const u8{ "-DMBEDTLS_CONFIG_FILE=\"miso_mbedtls_config.h\"", "-DEFM32GG390F1024", "-O2", "-fdata-sections", "-ffunction-sections" };

pub fn aggregate(exe: *microzig.Firmware) void {
for (include_path) |path| {
Expand Down
2 changes: 1 addition & 1 deletion build_wakaama.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const source_path = [_][]const u8{
usr_src_path ++ "lightclient/miso_lightclient.c",
};

const c_flags = [_][]const u8{ "-DMBEDTLS_CONFIG_FILE=\"miso_mbedtls_config.h\"", "-DLWM2M_CLIENT_MODE=1", "-DLWM2M_COAP_DEFAULT_BLOCK_SIZE=512", "-DLWM2M_SUPPORT_JSON=1", "-DLWM2M_SUPPORT_SENML_JSON=1", "-DEFM32GG390F1024", "-O2", "-fdata-sections", "-ffunction-sections" };
const c_flags = [_][]const u8{ "-DMBEDTLS_CONFIG_FILE=\"miso_mbedtls_config.h\"", "-DLWM2M_CLIENT_MODE=1", "-DLWM2M_COAP_DEFAULT_BLOCK_SIZE=512", "-DLWM2M_SUPPORT_JSON=1", "-DLWM2M_SUPPORT_SENML_JSON=1", "-DEFM32GG390F1024", "-O2", "-fdata-sections", "-ffunction-sections", "-fstack-protector-all" };

pub fn aggregate(exe: *microzig.Firmware) void {
for (include_path) |path| {
Expand Down
2 changes: 1 addition & 1 deletion csrc/board/inc/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ uint32_t BOARD_SD_CARD_IsInserted(void);
void BOARD_Watchdog_Init(void);
void BOARD_Watchdog_Feed(void);
void BOARD_Watchdog_Enable(void);

void BOARD_Watchdog_Disable(void);

void BOARD_EM9301_Init(void);
void BOARD_EM9301_Enable(void);
Expand Down
97 changes: 54 additions & 43 deletions csrc/board/src/board_CC3100.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

#define BOARD_CC3100_FD ((int)INT32_C(1))

static SemaphoreHandle_t tx_semaphore = NULL;
static SemaphoreHandle_t rx_semaphore = NULL;
static SemaphoreHandle_t tx_queue = NULL;
static SemaphoreHandle_t rx_queue = NULL;

#define CC3100_SPI_RX_TIMEOUT_MS (1000)
#define CC3100_SPI_TX_TIMEOUT_MS (1000)

SPIDRV_HandleData_t cc3100_usart;

Expand Down Expand Up @@ -54,9 +57,9 @@ static void recieve_callback(struct SPIDRV_HandleData *handle, Ecode_t transferS

BaseType_t xHigherPriorityTaskWoken = pdFALSE;

if (NULL != rx_semaphore)
if (NULL != rx_queue)
{
(void)xQueueSendFromISR(rx_semaphore, &transfer_status_information, &xHigherPriorityTaskWoken);
(void)xQueueSendFromISR(rx_queue, &transfer_status_information, &xHigherPriorityTaskWoken);
}

portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
Expand All @@ -71,9 +74,9 @@ static void send_callback(struct SPIDRV_HandleData *handle, Ecode_t transferStat

BaseType_t xHigherPriorityTaskWoken = pdFALSE;

if (NULL != tx_semaphore)
if (NULL != tx_queue)
{
(void)xQueueSendFromISR(tx_semaphore, &transfer_status_information, &xHigherPriorityTaskWoken);
(void)xQueueSendFromISR(tx_queue, &transfer_status_information, &xHigherPriorityTaskWoken);
}

portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
Expand Down Expand Up @@ -127,14 +130,14 @@ void CC3100_DeviceEnablePreamble(void)

void CC3100_DeviceEnable(void)
{
if (tx_semaphore == NULL)
if (tx_queue == NULL)
{
tx_semaphore = xQueueCreate(1, sizeof(struct transfer_status_s));
tx_queue = xQueueCreate(1, sizeof(struct transfer_status_s));
}

if (rx_semaphore == NULL)
if (rx_queue == NULL)
{
rx_semaphore = xQueueCreate(1, sizeof(struct transfer_status_s));
rx_queue = xQueueCreate(1, sizeof(struct transfer_status_s));
}

GPIO_PinOutSet(WIFI_NHIB_PORT, WIFI_NHIB_PIN);
Expand All @@ -157,6 +160,14 @@ void CC3100_DeviceDisable(void)
false,
false);
BOARD_msDelay(1); // Very important (!)

//GPIO_PinOutClear(WIFI_NRESET_PORT, WIFI_NRESET_PIN);
//GPIO_PinOutClear(WIFI_NHIB_PORT, WIFI_NHIB_PIN);
//GPIO_PinOutClear(VDD_WIFI_EN_PORT, VDD_WIFI_EN_PIN);
//GPIO_PinOutSet(VDD_WIFI_EN_PORT, VDD_WIFI_EN_PIN);
//BOARD_msDelay(19);


// GPIO_PinModeSet(VDD_WIFI_EN_PORT, VDD_WIFI_EN_PIN, gpioModeDisabled, 0);
// GPIO_PinModeSet(WIFI_CSN_PORT, WIFI_CSN_PIN, gpioModeDisabled, 0);
GPIO_PinModeSet(WIFI_INT_PORT, WIFI_INT_PIN, gpioModeDisabled, 0);
Expand All @@ -166,21 +177,20 @@ void CC3100_DeviceDisable(void)
// GPIO_PinModeSet(WIFI_SPI0_MOSI_PORT, WIFI_SPI0_MOSI_PIN, gpioModeDisabled, 0);
// GPIO_PinModeSet(WIFI_SPI0_SCK_PORT, WIFI_SPI0_SCK_PIN, gpioModeDisabled, 0);

if (NULL != tx_semaphore)
if (NULL != tx_queue)
{
vQueueDelete(tx_semaphore);
tx_semaphore = NULL;
vQueueDelete(tx_queue);
tx_queue = NULL;
}
if (NULL != rx_semaphore)
if (NULL != rx_queue)
{
vQueueDelete(rx_semaphore);
rx_semaphore = NULL;
vQueueDelete(rx_queue);
rx_queue = NULL;
}
}

int CC3100_IfOpen(char *pIfName, unsigned long flags)
{
(void) pIfName;
(void) flags;
int ret = -1;
// Success: FD (positive integer)
Expand All @@ -189,6 +199,8 @@ int CC3100_IfOpen(char *pIfName, unsigned long flags)
{
if (ECODE_OK == SPIDRV_Init(&cc3100_usart, &cc3100_usart_init_data))
{
NVIC_SetPriority(USART0_RX_IRQn, 5);
NVIC_SetPriority(USART0_TX_IRQn, 6);
GPIO_PinOutSet(WIFI_NHIB_PORT, WIFI_NHIB_PIN); // Clear Hybernate
BOARD_msDelay(50);
cc3100_spi_deselect();
Expand Down Expand Up @@ -232,29 +244,26 @@ int CC3100_IfRead(Fd_t Fd, uint8_t *pBuff, int Len)

cc3100_spi_select();

if (rx_semaphore != NULL) // Change to check if scheduler is active
if (rx_queue != NULL) // Change to check if scheduler is active
{
(void)xQueueReset(rx_queue);

ecode = SPIDRV_MReceive(&cc3100_usart, pBuff, Len, recieve_callback);
if (ECODE_EMDRV_SPIDRV_OK == ecode)
{
if(pdTRUE == xQueueReceive(rx_semaphore, &transfer_status, 2*1000)) // Added rx timeout
if(pdTRUE == xQueueReceive(rx_queue, &transfer_status, CC3100_SPI_RX_TIMEOUT_MS)) // Added rx timeout
{
if (ECODE_EMDRV_SPIDRV_OK == transfer_status.transferStatus)
{
retVal = transfer_status.itemsTransferred;
}
ecode = transfer_status.transferStatus;
}
else
{
retVal = -1;
ecode = ECODE_EMDRV_SPIDRV_TIMEOUT;
}

if (ECODE_EMDRV_SPIDRV_OK == ecode)
{
retVal = transfer_status.itemsTransferred;
}
}
} else
{
ecode = SPIDRV_MReceiveB(&cc3100_usart, pBuff, Len);
if (ECODE_EMDRV_SPIDRV_OK == ecode)
{
retVal = Len;
}
}

Expand All @@ -277,26 +286,26 @@ int CC3100_IfWrite(Fd_t Fd, uint8_t *pBuff, int Len)

cc3100_spi_select();

if (rx_semaphore != NULL) // Change to check if scheduler is active
if (tx_queue != NULL) // Change to check if scheduler is active
{
xQueueReset(tx_queue);

ecode = SPIDRV_MTransmit(&cc3100_usart, pBuff, Len, send_callback);
if (ECODE_EMDRV_SPIDRV_OK == ecode)
if(ECODE_EMDRV_SPIDRV_OK == ecode)
{
(void) xQueueReceive(tx_semaphore, &transfer_status, portMAX_DELAY);
if (ECODE_EMDRV_SPIDRV_OK == transfer_status.transferStatus)
if(pdTRUE == xQueueReceive(tx_queue, &transfer_status, CC3100_SPI_TX_TIMEOUT_MS))
{
retVal = transfer_status.itemsTransferred;
} else
ecode = transfer_status.transferStatus;
}
else
{
retVal = -1;
ecode = ECODE_EMDRV_SPIDRV_TIMEOUT;
}
}
} else
{
ecode = SPIDRV_MTransmitB(&cc3100_usart, pBuff, Len);
if (ECODE_EMDRV_SPIDRV_OK == ecode)

if(ECODE_EMDRV_SPIDRV_OK == ecode)
{
retVal = Len;
retVal = transfer_status.itemsTransferred;
}
}

Expand Down Expand Up @@ -379,9 +388,11 @@ void CC3100_SockEvtHdlr(SlSockEvent_t *pSlSockEvent)
switch (pSlSockEvent->Event)
{
case SL_SOCKET_TX_FAILED_EVENT:
// status, sd
printf("tx failed\r\n");
break;
case SL_SOCKET_ASYNC_EVENT:
// sd, type
printf("socket async event\r\n");
break;
default:
Expand Down
5 changes: 5 additions & 0 deletions csrc/board/src/board_watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ void BOARD_Watchdog_Enable(void)
WDOGn_Enable(BOARD_WDOG, true);
}

void BOARD_Watchdog_Disable(void)
{
WDOGn_Enable(BOARD_WDOG, false);
}

void BOARD_Watchdog_Feed(void)
{
WDOGn_Feed(BOARD_WDOG);
Expand Down
Loading

0 comments on commit 7f6e9bc

Please sign in to comment.