jlab-sensing / ENTS-node-firmware

Firmware for STM32 and ESP32 microcontrollers on the Soil Power Sensor board
MIT License
0 stars 3 forks source link

Implement LoRaWAN interface (Due: 2024-02-13) #3

Closed jmadden173 closed 8 months ago

jmadden173 commented 11 months ago

Expect to have a template LoRaWAN packet sent to The Things Network. The interface must be able to verify that the data was uploaded.

jmadden173 commented 9 months ago

Some notes:

Example projects in: https://github.com/jlab-sensing/STM32CubeWL/tree/main Youtube Series shows what to change in the examples: https://youtu.be/8KNfrWoa1fw?feature=shared Checked out CubeMX and there appears to be middleware for LoRa Wan

jmadden173 commented 9 months ago

Currently working on getting CubeMX generate LoRaWAN code to compile. First ran into an issue that I2C wasn't setup in CubeMX so the configuration got reset. Now getting the following error:

In file included from Src/app_lorawan.c:24:0:
Inc/sys_app.h:32:10: fatal error: stm32_adv_trace.h: No such file or directory

The stm32_adv_trace.h is located here: https://github.com/STMicroelectronics/STM32CubeWL/blob/cf151e1d31819c540b2832c4d1f22ba24fd511cb/Utilities/trace/adv_trace/stm32_adv_trace.h

But it appears that Utilities/trace/adv_trace isn't included in the build command.

arm-none-eabi-gcc -o .pio/build/stm32/src/app_lorawan.o -c -save-temps=obj
-mthumb -mcpu=cortex-m4 -Os -ffunction-sections -fdata-sections -Wall -nostdlib
-DPLATFORMIO=60107 -DSTM32WLxx -DSTM32WLE5xx -DDMA_CCR_SECM -DDMA_CCR_PRIV
-DUSE_HAL_DRIVER -DF_CPU=48000000L -IInc -ISrc
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Utilities/timer
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Utilities/sequencer
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Utilities/misc
-I/home/jtmadden/repos/jlab/soil-power-sensor-firmware/proto/c/include
-I/home/jtmadden/repos/jlab/soil-power-sensor-firmware/proto/c/src -ISrc -IInc
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Drivers/CMSIS/Include
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Drivers/CMSIS/Device/ST/STM32WLxx/Include
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Drivers/STM32WLxx_HAL_Driver/Inc
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Drivers/STM32WLxx_HAL_Driver/Src
-I/home/jtmadden/.platformio/packages/framework-stm32cubewl/Drivers/CMSIS/DSP/Include
Src/app_lorawan.c

Need to look into the STM32 build tools for platformio to see if there is a flag that includes the folder or if its something we have to add manually.

Also I'm running into the terminology IF layer, not entirely sure what that means, more investigation needs to be done....

jmadden173 commented 9 months ago

IF layer I think is interface layer

jmadden173 commented 9 months ago

I updated the build script in https://github.com/jlab-sensing/platform-ststm32/tree/develop. People may need to update the dependencies to fix #include errors.

jmadden173 commented 9 months ago

Getting more errors relating to #include. It appears that the Middlewares/Third_Party/LoRaWAN/ is not being included in the build. Seeing the following errors. I'm noticing that nothing is showing on the dependency graph for LoRa. Similar problem to above.

(.venv) jtmadden@spruce:~/repos/jlab/soil-power-sensor-firmware/stm32 (3-implement-lorawan-interface) > pio run                                                                                                                                          [1]
*************************************************************************************************************************************************************************************************************************************************************
Obsolete PIO Core v6.1.7 is used (previous was 6.1.13)
Please remove multiple PIO Cores from a system:
https://docs.platformio.org/en/latest/core/installation/troubleshooting.html
*************************************************************************************************************************************************************************************************************************************************************
Processing stm32 (platform: https://github.com/jlab-sensing/platform-ststm32; board: WioE5; framework: stm32cube)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/WioE5.html
PLATFORM: ST STM32 (17.0.0+sha.7136ebc) > WioE5
HARDWARE: STM32WL55JC 48MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES: 
 - framework-stm32cubewl @ 1.26.2+sha.77fa61b 
 - tool-ldscripts-ststm32 @ 0.2.0 
 - tool-openocd @ 1.0.0+sha.6930394 
 - toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
Warning! Cannot find the default startup file for `stm32wl55jc`. Ignore this warning if the startup code is part of your project.
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 16 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Soil Power Sensor Protocal Buffer @ 2.0.0
|-- Util-misc
|-- Util-adv_trace
|-- Util-tiny_lpm
|-- Util-timer
Building in release mode
Compiling .pio/build/stm32/src/lora_app.o
Compiling .pio/build/stm32/src/lora_info.o
Compiling .pio/build/stm32/src/radio_board_if.o
Src/lora_info.c:22:10: fatal error: LoRaMac.h: No such file or directory

*****************************************************************
* Looking for LoRaMac.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:LoRaMac.h"
* Web  > https://registry.platformio.org/search?q=header:LoRaMac.h
*
*****************************************************************

 #include "LoRaMac.h"
          ^~~~~~~~~~~
compilation terminated.
Compiling .pio/build/stm32/src/startup_stm32wl55xx_cm4.o
*** [.pio/build/stm32/src/lora_info.o] Error 1
Src/lora_app.c:26:10: fatal error: LmHandler.h: No such file or directory

*******************************************************************
* Looking for LmHandler.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:LmHandler.h"
* Web  > https://registry.platformio.org/search?q=header:LmHandler.h
*
*******************************************************************

 #include "LmHandler.h"
          ^~~~~~~~~~~~~
compilation terminated.
*** [.pio/build/stm32/src/lora_app.o] Error 1
...
jmadden173 commented 9 months ago

^ above issue has been fixed. Users will need to update their platform-ststm32

jmadden173 commented 9 months ago

It appears that none of the middlewares for CubeWL are built as part of platformio. I needed to add LoRaWAN and SubGHz_Phy to the build script.

Throughout I've been running into missing usart_if.h and adc_if.h files. I know that they are interface files but I just found out where some of them are generated. Inside of CubeMX under LoRaWAN middleware you can set the platform settings. I tried setting the VCOM and usart1_if.h/.c was generated.

image

jmadden173 commented 9 months ago

Had to copy the following to Inc/frag_decoder_if.h

/home/jtmadden/.platformio/packages/STM32CubeWL/Middlewares/Third_Party/LoRaWAN/Conf/frag_decoder_if_template.h

Edit: this is only if we want to do OTA updates (referred to as FUOTA) by STM. Currently has been disabled to get build to work.

jmadden173 commented 9 months ago

Okay I've fixed all the include and build errors. The build scripts had to be refactored to account for library dependency stuff. Now Utilities are built separately with Middleware-LoRaWAN built as a single library with some Utilities dependencies. The build also had to remove some source files from LoRaWAN/LmHandler that were related to FUOTA and would require further configuration.

jmadden173 commented 9 months ago

Was getting the following errors

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/WioE5.html
PLATFORM: ST STM32 (17.0.0+sha.ead2e9d) > WioE5
HARDWARE: STM32WL55JC 48MHz, 64KB RAM, 256KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES: 
 - framework-stm32cubewl @ 1.26.2+sha.77fa61b 
 - tool-ldscripts-ststm32 @ 0.2.0 
 - tool-openocd @ 1.0.0+sha.0ecdfeb 
 - toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
Warning! Cannot find the default startup file for `stm32wl55jc`. Ignore this warning if the startup code is part of your project.
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ deep+, Compatibility ~ soft
Found 18 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Soil Power Sensor Protocal Buffer @ 2.0.0
|-- Utilities-misc
|-- Middleware-LoRaWAN
|-- Utilities-timer
|-- Utilities-lpm-tiny_lpm
|-- Utilities-trace-adv_trace
Building in release mode
Linking .pio/build/stm32/firmware.elf
.pio/build/stm32/libe7e/libLoRaWAN.a(LoRaMac.o): In function `OpenContinuousRxCWindow':
LoRaMac.c:(.text.OpenContinuousRxCWindow+0x60): undefined reference to `Radio'
.pio/build/stm32/libe7e/libLoRaWAN.a(LoRaMac.o): In function `ScheduleTx':
LoRaMac.c:(.text.ScheduleTx+0x2e8): undefined reference to `Radio'
.pio/build/stm32/libe7e/libLoRaWAN.a(LoRaMac.o): In function `SwitchClass':
LoRaMac.c:(.text.SwitchClass+0xdc): undefined reference to `Radio'
.pio/build/stm32/libe7e/libLoRaWAN.a(LoRaMac.o): In function `LoRaMacMibSetRequestConfirm':
LoRaMac.c:(.text.LoRaMacMibSetRequestConfirm+0x2a0): undefined reference to `Radio'
.pio/build/stm32/libe7e/libLoRaWAN.a(LoRaMac.o): In function `LoRaMacMlmeRequest':
LoRaMac.c:(.text.LoRaMacMlmeRequest+0x1ec): undefined reference to `Radio'
.pio/build/stm32/libe7e/libLoRaWAN.a(RegionUS915.o):RegionUS915.c:(.text.GetTimeOnAir+0x38): more undefined references to `Radio' follow
collect2: error: ld returned 1 exit status
*** [.pio/build/stm32/firmware.elf] Error 1

I was trying to figure out where Radio was defined. Turns out the example projects used BSP (Board Support Package) to configure the radio and hardware components. Was worried about having to figure out how to integrate it myself but the Wio-E5 example project has a BSP defined! Plan on copying it over from the repo: https://github.com/Seeed-Studio/LoRaWan-E5-Node/

jmadden173 commented 9 months ago

Related to above. Radio is defined within the SubGHz_Phy middleware. I was looking into it and it wasn't building even though I verified that the build function was being called. Turns out there was an error in the manifest (missing closing >) that was the culprit.

jmadden173 commented 9 months ago

Fixed more errors related to dependencies. I was putting dependency key under the wrong parent.

jmadden173 commented 8 months ago

I moved all the BSP components into the source tree since I couldn't be bothered to fix the reference to source tree components. In the future it would be nice to have more separation between components.

I believe I've squashed all bugs related to the WioE5 BSP. It appears that STM at some point changed the interface that was generated by CubeMX, and the Seeedstudio example project used an older version. Therefore I was getting a few undefined reference errors. The process for fixing them was (1) compiling the project, (2) see what reference was giving errors, (3) checking for the difference in BSP libraries to those in https://github.com/jlab-sensing/STM32CubeWL, (4) copy and pasting missing code. Ideally if this issue is closed there should be an up to date version of BSP for the WioE5.

jmadden173 commented 8 months ago

I had to enable DMA on UART1, which may break other code including tests due to print statements but it successfully compiles! Now moving onto the errors in the code.

jmadden173 commented 8 months ago

See #29 for update on gateway. In the meantime we can piggyback off the full network to test LoRa functionality.

jmadden173 commented 8 months ago

Gateway has been registered and I am able to see messages on TTN and on the serial monitor on our board. Working on figuring out why joins are failing.

Serial:

###### = JOIN FAILED
330s091:TX on freq 912100000 Hz at DR 0
Could not send request
334s104:MAC txTimeOut

###### = JOIN FAILED
340s091:TX on freq 913700000 Hz at DR 0
Could not send request
344s104:MAC txTimeOut

TTN: image

jmadden173 commented 8 months ago

Updated the pin and clock configuration to match the Seeedstudio WioE5 LoRa application. The device now runs at 48 MHz. Am able to successfully transmit messages from the device to TTN. Next step is figuring out the RX as I am getting the following error:

###### ========== MCPS-Confirm =============
1100s042:TX on freq 903900000 Hz at DR 3
1100s044:SEND REQUEST
1100s091:MAC txDone
1105s071:RX_1 on freq 923300000 Hz at DR 13
1105s114:IRQ_RX_TX_TIMEOUT
1105s114:MAC rxTimeOut
1106s085:RX_2 on freq 923300000 Hz at DR 8
1106s153:IRQ_RX_TX_TIMEOUT
1106s153:MAC rxTimeOut

Edit: I'm an idiot and TTN doesn't have anything to downlink. I was testing and was simulating uplink messages....