Skip to content

Commit 8706a1a

Browse files
authored
Merge pull request #15136 from jeromecoutant/PR_WL_LORA
STM32WL LORA radio: add a critical section
2 parents ea0538e + f42b3e4 commit 8706a1a

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

connectivity/drivers/lora/TARGET_STM32WL/STM32WL_LoRaRadio.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ SPDX-License-Identifier: BSD-3-Clause
3939
#include "Timer.h"
4040
#include "STM32WL_LoRaRadio.h"
4141

42+
#ifndef DEBUG_STDIO
43+
#define DEBUG_STDIO 0
44+
#endif
45+
46+
#if DEBUG_STDIO
47+
#define DEBUG_PRINTF(...) do { printf(__VA_ARGS__); } while(0)
48+
#else
49+
#define DEBUG_PRINTF(...) {}
50+
#endif
51+
4252
uint8_t regulator_mode = MBED_CONF_STM32WL_LORA_DRIVER_REGULATOR_MODE;
4353

4454
uint8_t crystal_select = MBED_CONF_STM32WL_LORA_DRIVER_CRYSTAL_SELECT;
@@ -136,7 +146,6 @@ STM32WL_LoRaRadio::STM32WL_LoRaRadio()
136146

137147
STM32WL_LoRaRadio::~STM32WL_LoRaRadio()
138148
{
139-
140149
}
141150

142151
/**
@@ -270,7 +279,7 @@ void HAL_SUBGHZ_MspInit(SUBGHZ_HandleTypeDef *subghzHandle)
270279
static void RadioIrqProcess()
271280
{
272281
radio_irq_masks_t irq_status;
273-
282+
core_util_critical_section_enter();
274283

275284
irq_status = (radio_irq_masks_t)STM32WL_LoRaRadio::get_irq_status();
276285
/* clear IRQs lines after recovering their status */
@@ -292,8 +301,10 @@ static void RadioIrqProcess()
292301
if ((irq_status & IRQ_RX_TX_TIMEOUT) == IRQ_RX_TX_TIMEOUT) {
293302
STM32WL_LoRaRadio::HAL_SUBGHZ_RxTxTimeoutCallback();
294303
}
304+
core_util_critical_section_exit();
295305
}
296-
/* ----- */
306+
307+
297308
/* HAL_SUBGHz Callbacks definitions */
298309
void STM32WL_LoRaRadio::HAL_SUBGHZ_TxCpltCallback(void)
299310
{
@@ -352,7 +363,6 @@ void STM32WL_LoRaRadio::HAL_SUBGHZ_CADStatusCallback(void)
352363
}
353364
}
354365

355-
356366
void STM32WL_LoRaRadio::HAL_SUBGHZ_RxTxTimeoutCallback(void)
357367
{
358368
if ((_radio_events->tx_timeout) && (_operating_mode == MODE_TX)) {
@@ -373,14 +383,13 @@ void STM32WL_LoRaRadio::HAL_SUBGHZ_RxTxTimeoutCallback(void)
373383
}
374384
}
375385

376-
/* ----- */
377-
/* HAL_SUBGHz Callbacks definitions END */
378386

379387

380388
/* STM32WL specific BSP Nucleo board functions */
381389
void STM32WL_LoRaRadio::SUBGRF_SetSwitch(uint8_t paSelect, RFState_t rxtx)
382390
{
383391
RBI_Switch_TypeDef state = RBI_SWITCH_RX;
392+
DEBUG_PRINTF("STM32WL_LoRaRadio::SUBGRF_SetSwitch %u %u\n", paSelect, rxtx);
384393

385394
if (rxtx == RFSWITCH_TX) {
386395
if (paSelect == RFO_LP) {
@@ -400,6 +409,7 @@ void STM32WL_LoRaRadio::SUBGRF_SetSwitch(uint8_t paSelect, RFState_t rxtx)
400409

401410
uint8_t STM32WL_LoRaRadio::SUBGRF_SetRfTxPower(int8_t power)
402411
{
412+
DEBUG_PRINTF("STM32WL_LoRaRadio::SUBGRF_SetRfTxPower %u\n", power);
403413
uint8_t paSelect = RFO_LP;
404414

405415
int32_t TxConfig = board_rf_switch_config;
@@ -434,6 +444,7 @@ uint8_t STM32WL_LoRaRadio::SUBGRF_SetRfTxPower(int8_t power)
434444
void STM32WL_LoRaRadio::SUBGRF_SetTxParams(uint8_t paSelect, int8_t power, radio_ramp_time_t rampTime)
435445
{
436446
uint8_t buf[2];
447+
DEBUG_PRINTF("STM32WL_LoRaRadio::SUBGRF_SetTxParams %u %u\n", paSelect, power);
437448

438449
if (paSelect == RFO_LP) {
439450
if (power == 15) {
@@ -496,6 +507,7 @@ void STM32WL_LoRaRadio::Radio_SMPS_Set(uint8_t level)
496507
void STM32WL_LoRaRadio::calibrate_image(uint32_t freq)
497508
{
498509
uint8_t cal_freq[2];
510+
DEBUG_PRINTF("STM32WL_LoRaRadio::calibrate_image %u\n", freq);
499511

500512
if (freq > 900000000) {
501513
cal_freq[0] = 0xE1;
@@ -521,6 +533,7 @@ void STM32WL_LoRaRadio::calibrate_image(uint32_t freq)
521533

522534
void STM32WL_LoRaRadio::set_channel(uint32_t frequency)
523535
{
536+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_channel %u\n", frequency);
524537
#if MBED_CONF_STM32WL_LORA_DRIVER_SLEEP_MODE == 1
525538
// At this point, we are not sure what is the Modem type, set both
526539
_mod_params.params.lora.operational_frequency = frequency;
@@ -571,6 +584,7 @@ void STM32WL_LoRaRadio::standby(void)
571584
void STM32WL_LoRaRadio::SUBGRF_SetTcxoMode(radio_TCXO_ctrl_voltage_t voltage,
572585
uint32_t timeout)
573586
{
587+
DEBUG_PRINTF("STM32WL_LoRaRadio::SUBGRF_SetTcxoMode %u\n", voltage);
574588
uint8_t buf[4];
575589

576590
buf[0] = voltage & 0x07;
@@ -585,7 +599,7 @@ void STM32WL_LoRaRadio::init_radio(radio_events_t *events)
585599
{
586600
HAL_StatusTypeDef error_value;
587601
uint32_t vector = 0;
588-
602+
DEBUG_PRINTF("STM32WL_LoRaRadio::init_radio\n");
589603
_radio_events = events;
590604

591605
_tx_timeout = 0;
@@ -602,7 +616,6 @@ void STM32WL_LoRaRadio::init_radio(radio_events_t *events)
602616
SUBGRF_SetTxParams(RFO_LP, 0, RADIO_RAMP_200_US);
603617

604618
sleep();
605-
606619
}
607620

608621

@@ -634,6 +647,7 @@ void STM32WL_LoRaRadio::cold_start_wakeup()
634647

635648
void STM32WL_LoRaRadio::set_public_network(bool enable)
636649
{
650+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_public_network %u\n", enable);
637651
if (enable) {
638652
// Change LoRa modem SyncWord
639653
write_to_register(REG_LR_SYNCWORD, (LORA_MAC_PUBLIC_SYNCWORD >> 8) & 0xFF);
@@ -680,12 +694,14 @@ uint32_t STM32WL_LoRaRadio::time_on_air(radio_modems_t modem, uint8_t pkt_len)
680694
}
681695
break;
682696
}
697+
DEBUG_PRINTF("STM32WL_LoRaRadio::time_on_air %u %u => %u\n", modem, pkt_len, air_time);
683698

684699
return air_time;
685700
}
686701

687702
void STM32WL_LoRaRadio::radio_reset()
688703
{
704+
DEBUG_PRINTF("STM32WL_LoRaRadio::radio_reset\n");
689705

690706
// give some time for automatic image calibration
691707
rtos::ThisThread::sleep_for(6ms);
@@ -704,10 +720,12 @@ void STM32WL_LoRaRadio::wakeup()
704720
cold_start_wakeup();
705721
#endif
706722
}
723+
DEBUG_PRINTF("STM32WL_LoRaRadio::wakeup\n");
707724
}
708725

709726
void STM32WL_LoRaRadio::sleep(void)
710727
{
728+
DEBUG_PRINTF("STM32WL_LoRaRadio::sleep\n");
711729
#if MBED_CONF_STM32WL_LORA_DRIVER_SLEEP_MODE == 1
712730
// cold start, power consumption 160 nA
713731
sleep_state = 0x00;
@@ -739,7 +757,9 @@ uint32_t STM32WL_LoRaRadio::random(void)
739757
read_register(RANDOM_NUMBER_GENERATORBASEADDR, buf, 4);
740758
standby();
741759

742-
return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
760+
uint32_t random_value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] ;
761+
DEBUG_PRINTF("STM32WL_LoRaRadio::random %u\n", random_value);
762+
return random_value;
743763
}
744764

745765
void STM32WL_LoRaRadio::write_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size)
@@ -748,7 +768,6 @@ void STM32WL_LoRaRadio::write_opmode_command(uint8_t cmd, uint8_t *buffer, uint1
748768

749769
error_value = HAL_SUBGHZ_ExecSetCmd(&hsubghz, (SUBGHZ_RadioSetCmd_t)cmd, buffer, size);
750770
MBED_ASSERT(error_value == HAL_OK);
751-
752771
}
753772

754773
void STM32WL_LoRaRadio::read_opmode_command(uint8_t cmd, uint8_t *buffer, uint16_t size)
@@ -757,7 +776,6 @@ void STM32WL_LoRaRadio::read_opmode_command(uint8_t cmd, uint8_t *buffer, uint16
757776

758777
error_value = HAL_SUBGHZ_ExecGetCmd(&hsubghz, (SUBGHZ_RadioGetCmd_t)cmd, buffer, size);
759778
MBED_ASSERT(error_value == HAL_OK);
760-
761779
}
762780

763781
void STM32WL_LoRaRadio::write_to_register(uint16_t addr, uint8_t data)
@@ -813,6 +831,7 @@ void STM32WL_LoRaRadio::write_fifo(uint8_t *buffer, uint8_t size)
813831
void STM32WL_LoRaRadio::set_modem(uint8_t modem)
814832
{
815833
_active_modem = modem;
834+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_modem %u\n", modem);
816835

817836
// setting modem type must happen in standby mode
818837
if (_operating_mode != MODE_STDBY_RC) {
@@ -875,6 +894,7 @@ void STM32WL_LoRaRadio::set_tx_config(radio_modems_t modem,
875894
bool iq_inverted,
876895
uint32_t timeout)
877896
{
897+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_tx_config %u %u %u %u\n", modem, power, fdev, bandwidth);
878898

879899
uint8_t modem_type = (uint8_t) modem;
880900
switch (modem_type) {
@@ -966,6 +986,7 @@ void STM32WL_LoRaRadio::set_rx_config(radio_modems_t modem,
966986
uint8_t max_payload_len;
967987
(void) freq_hop_on;
968988
(void) hop_period;
989+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_rx_config %u %u %u %u\n", modem, bandwidth, datarate, coderate);
969990

970991
if (rx_continuous) {
971992
_reception_mode = RECEPTION_MODE_CONTINUOUS;
@@ -1089,6 +1110,7 @@ void STM32WL_LoRaRadio::configure_dio_irq(uint16_t irq_mask, uint16_t dio1_mask,
10891110

10901111
void STM32WL_LoRaRadio::send(uint8_t *buffer, uint8_t size)
10911112
{
1113+
DEBUG_PRINTF("STM32WL_LoRaRadio::send %u\n", size);
10921114
set_tx_power(_tx_power);
10931115
configure_dio_irq(IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
10941116
IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
@@ -1127,6 +1149,8 @@ void STM32WL_LoRaRadio::send(uint8_t *buffer, uint8_t size)
11271149

11281150
void STM32WL_LoRaRadio::receive(void)
11291151
{
1152+
DEBUG_PRINTF("STM32WL_LoRaRadio::receive\n");
1153+
11301154
if (get_modem() == MODEM_LORA) {
11311155
if (_reception_mode != RECEPTION_MODE_CONTINUOUS) {
11321156
// Data-sheet Table 13-11: StopOnPreambParam
@@ -1243,6 +1267,7 @@ void STM32WL_LoRaRadio::set_pa_config(uint8_t pa_DC, uint8_t hp_max,
12431267
uint8_t device_type, uint8_t pa_LUT)
12441268
{
12451269
uint8_t buf[4];
1270+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_pa_config %u %u %u %u\n", pa_DC, hp_max, device_type, pa_LUT);
12461271

12471272
buf[0] = pa_DC;
12481273
buf[1] = hp_max;
@@ -1253,6 +1278,7 @@ void STM32WL_LoRaRadio::set_pa_config(uint8_t pa_DC, uint8_t hp_max,
12531278

12541279
void STM32WL_LoRaRadio::set_crc_seed(uint16_t seed)
12551280
{
1281+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_crc_seed\n");
12561282
if (_active_modem == MODEM_FSK) {
12571283
uint8_t buf[2];
12581284
buf[0] = (uint8_t)((seed >> 8) & 0xFF);
@@ -1263,6 +1289,7 @@ void STM32WL_LoRaRadio::set_crc_seed(uint16_t seed)
12631289

12641290
void STM32WL_LoRaRadio::set_crc_polynomial(uint16_t polynomial)
12651291
{
1292+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_crc_polynomial\n");
12661293
if (_active_modem == MODEM_FSK) {
12671294
uint8_t buf[2];
12681295
buf[0] = (uint8_t)((polynomial >> 8) & 0xFF);
@@ -1273,6 +1300,7 @@ void STM32WL_LoRaRadio::set_crc_polynomial(uint16_t polynomial)
12731300

12741301
void STM32WL_LoRaRadio::set_whitening_seed(uint16_t seed)
12751302
{
1303+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_whitening_seed\n");
12761304
if (_active_modem == MODEM_FSK) {
12771305
uint8_t reg_value = read_register(REG_LR_WHITSEEDBASEADDR_MSB) & 0xFE;
12781306
reg_value = ((seed >> 8) & 0x01) | reg_value;
@@ -1286,6 +1314,7 @@ void STM32WL_LoRaRadio::set_packet_params(packet_params_t *packet_params)
12861314
uint8_t n;
12871315
uint8_t crc_val = 0;
12881316
uint8_t buf[9] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1317+
DEBUG_PRINTF("STM32WL_LoRaRadio::set_packet_params %u\n", packet_params->modem_type);
12891318

12901319
// Check if required configuration corresponds to the stored packet type
12911320
// If not, silently update radio packet type
@@ -1363,6 +1392,7 @@ void STM32WL_LoRaRadio::set_buffer_base_addr(uint8_t tx_base_addr, uint8_t rx_ba
13631392

13641393
uint8_t STM32WL_LoRaRadio::get_status(void)
13651394
{
1395+
DEBUG_PRINTF("STM32WL_LoRaRadio::get_status\n");
13661396
switch (_operating_mode) {
13671397
case MODE_TX:
13681398
return RF_TX_RUNNING;
@@ -1382,12 +1412,15 @@ int8_t STM32WL_LoRaRadio::get_rssi()
13821412

13831413
read_opmode_command((uint8_t) RADIO_GET_RSSIINST, buf, 1);
13841414
rssi = -buf[0] >> 1;
1415+
1416+
DEBUG_PRINTF("STM32WL_LoRaRadio::get_rssi %d\n", rssi);
13851417
return rssi;
13861418
}
13871419

13881420
void STM32WL_LoRaRadio::get_rx_buffer_status(uint8_t *payload_len,
13891421
uint8_t *start_ptr)
13901422
{
1423+
// DEBUG_PRINTF("STM32WL_LoRaRadio::get_rx_buffer_status\n");
13911424
uint8_t status[2];
13921425

13931426
read_opmode_command((uint8_t) RADIO_GET_RXBUFFERSTATUS, status, 2);
@@ -1406,6 +1439,7 @@ void STM32WL_LoRaRadio::get_rx_buffer_status(uint8_t *payload_len,
14061439

14071440
void STM32WL_LoRaRadio::get_packet_status(packet_status_t *pkt_status)
14081441
{
1442+
// DEBUG_PRINTF("STM32WL_LoRaRadio::get_packet_status\n");
14091443
uint8_t status[3];
14101444

14111445
read_opmode_command((uint8_t) RADIO_GET_PACKETSTATUS, status, 3);
@@ -1437,13 +1471,15 @@ void STM32WL_LoRaRadio::get_packet_status(packet_status_t *pkt_status)
14371471
radio_error_t STM32WL_LoRaRadio::get_device_errors(void)
14381472
{
14391473
radio_error_t error;
1474+
DEBUG_PRINTF("STM32WL_LoRaRadio::get_device_errors\n");
14401475

14411476
read_opmode_command((uint8_t) RADIO_GET_ERROR, (uint8_t *)&error, 2);
14421477
return error;
14431478
}
14441479

14451480
void STM32WL_LoRaRadio::clear_device_errors(void)
14461481
{
1482+
DEBUG_PRINTF("STM32WL_LoRaRadio::clear_device_errors\n");
14471483
uint8_t buf[2] = {0x00, 0x00};
14481484
write_opmode_command((uint8_t) RADIO_CLR_ERROR, buf, 2);
14491485
}

0 commit comments

Comments
 (0)