zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.8k stars 6.59k forks source link

STM32WL LoRaWAN pinout incomplete / RFO pin not selectable #48511

Closed danielSchaeble closed 1 year ago

danielSchaeble commented 2 years ago

Is your enhancement proposal related to a problem? Please describe.

The STM32WL chip series has on chip LoRa and provides two input and two output pins for this. The low power output pin (RFO_LP) and the high power output pin (RFO_HP) are currently not choosable. By default the RFO_HP pin is used.

The limitation was found because of the following problem: A custom board with STM32WLE5CC, which is optimized to only use the RFO_LP pin was configured with two different firmwares. The first firmware is a bare metal version configured with STM32CubeMX and the second one is the lorawan class a sample from zephyr. The bare metal firmware reached and rssi of around -70 and the zephyr sample only of -110. After connecting the RFO_LP and the RFO_HP pin the zephyr sample reached an rssi of around -60.

Describe the solution you'd like An optimal solution would be an option in the devicetree to select the preferred RFO pin.

Additional context From the Datasheet of STM32WLE5CC image

erwango commented 2 years ago

Rather looks like a pure LORA driver point.

aurel32 commented 2 years ago

An optimal solution would be an option in the device tree to select the preferred RFO pin.

The SX127X driver has such an option, but not the SX126X driver, as both the SX1261 and SX1262 chips only have a single RF output, respectively LP and HP. The HP and LP output seems specific to the ST implementation, and the way to switch is not documented in the STM32WL reference manual, nor in the SX126X manual.

That said it is possible to find the way to switch between the two implementation by looking at STM32Cube. It appears that it uses a modified version of the loramac-node library, and uses the bit that selects between the SX1261 and the SX1262. So you might be able to get the LP output working by changing SX1262 into SX1261 in these lines: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/lora/sx126x_common.h#L25-L27

Rather looks like a pure LORA driver point.

If that works it should be possible to add a property in the st,stm32wl-subghz-radio binding and use it to select the correct chip version.

erwango commented 2 years ago

The HP and LP output seems specific to the ST implementation, and the way to switch is not documented in the STM32WL reference manual,

IIUC, it's actually documented in §5.8.4:

Set_PaConfig() command: byte 3 bits 7:1 Reserved, must be kept at reset value. bit 0 PaSel: PA selection. 0: HP PA selected 1: LP PA selected (default)

aurel32 commented 2 years ago

IIUC, it's actually documented in §5.8.4:

Indeed I search too quickly through the reference manual. Apologies.

erwango commented 2 years ago

IIUC, it's actually documented in §5.8.4:

Indeed I search too quickly through the reference manual. Apologies.

Took me some time to find it :-D

danielSchaeble commented 2 years ago

Thanks, so I am adding this up from you. According to the STM32WLE5CC RM (https://www.st.com/resource/en/reference_manual/rm0461-stm32wlex-advanced-armbased-32bit-mcus-with-subghz-radio-solution-stmicroelectronics.pdf) in §4.8.4 the Set_PaConfig(PaDutyCycle, HpMax, PaSel, 0x01) sets to LP or HP RFO.

In modules/lib/loramac-node/src/radio/sx126x/sx126x.c the function void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime) calls the set_PaConfig() function according to the device. This is for SX1261 a LP setting and for SX1262 a HP setting.

That said it is possible to find the way to switch between the two implementation by looking at STM32Cube. It appears that it uses a modified version of the loramac-node library, and uses the bit that selects between the SX1261 and the SX1262. So you might be able to get the LP output working by changing SX1262 into SX1261 in these lines: https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/lora/sx126x_common.h#L25-L27

According to these lines for STM32WL the SX1262 is selected and so HP output is selected.

So I just changed line 27 from #define SX126X_DEVICE_ID SX1262 to #define SX126X_DEVICE_ID SX1261 and it seems to work. My test with the rssi (of course the RFO pins in this case NOT connected) improved from -110 to -70.

danielSchaeble commented 1 year ago

An idea for a solution in the zephyrproject discord:

https://discord.com/channels/720317445772017664/883448180950327366/1072593303729623100

knthm commented 1 year ago

Note that until this issue is fixed boards which exclusively use the RFO_LP pin can't send LoRa transmissions, and the power amplifier may even be damaged if used in this configuration for longer durations.

I've spent two days debugging until I traced it to the RFO_ pins configuration and this issue, as my Olimex STM32WL Devkit wasn't sending detectable transmissions.