Skip to content

Commit c551723

Browse files
pi-anldpgeorge
authored andcommitted
stm32/powerctrl: Disable WB55 BLE before entering deepsleep.
Signed-off-by: Andrew Leech <[email protected]>
1 parent 2eca86e commit c551723

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

ports/stm32/mpbthciport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
116116

117117
int mp_bluetooth_hci_uart_deinit(void) {
118118
DEBUG_printf("mp_bluetooth_hci_uart_deinit (stm32 rfcore)\n");
119-
119+
rfcore_ble_reset();
120120
return 0;
121121
}
122122

ports/stm32/powerctrl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "powerctrl.h"
3030
#include "rtc.h"
3131
#include "genhdr/pllfreqtable.h"
32+
#include "extmod/modbluetooth.h"
3233

3334
#if defined(STM32H7)
3435
#define RCC_SR RSR
@@ -947,6 +948,10 @@ void powerctrl_enter_standby_mode(void) {
947948
}
948949
#endif
949950

951+
#if defined(STM32WB) && MICROPY_PY_BLUETOOTH
952+
mp_bluetooth_deinit();
953+
#endif
954+
950955
// We need to clear the PWR wake-up-flag before entering standby, since
951956
// the flag may have been set by a previous wake-up event. Furthermore,
952957
// we need to disable the wake-up sources while clearing this flag, so

ports/stm32/rfcore.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,18 +600,38 @@ static const struct {
600600
void rfcore_ble_init(void) {
601601
DEBUG_printf("rfcore_ble_init\n");
602602

603-
// Clear any outstanding messages from ipcc_init.
604-
tl_check_msg(&ipcc_mem_sys_queue, IPCC_CH_SYS, NULL);
605-
606603
// Configure and reset the BLE controller.
607-
tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params), 0);
608-
tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), NULL, 0);
604+
if (!rfcore_ble_reset()) {
605+
// ble init can fail if core2 has previously locked up. Reset HSI & rfcore to retry.
606+
LL_RCC_HSI_Disable();
607+
mp_hal_delay_ms(100);
608+
LL_RCC_HSI_Enable();
609+
610+
rfcore_init();
611+
rfcore_ble_reset();
612+
}
609613

610614
// Enable PES rather than SEM7 to moderate flash access between the cores.
611615
uint8_t buf = 0; // FLASH_ACTIVITY_CONTROL_PES
612616
tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_C2_SET_FLASH_ACTIVITY_CONTROL), &buf, 1, 0);
613617
}
614618

619+
bool rfcore_ble_reset(void) {
620+
DEBUG_printf("rfcore_ble_reset\n");
621+
622+
// Clear any outstanding messages from ipcc_init.
623+
tl_check_msg(&ipcc_mem_sys_queue, IPCC_CH_SYS, NULL);
624+
625+
// Configure and reset the BLE controller.
626+
int ret = tl_sys_hci_cmd_resp(HCI_OPCODE(OGF_VENDOR, OCF_BLE_INIT), (const uint8_t *)&ble_init_params, sizeof(ble_init_params), 500);
627+
628+
if (ret == -MP_ETIMEDOUT) {
629+
return false;
630+
}
631+
tl_ble_hci_cmd_resp(HCI_OPCODE(0x03, 0x0003), NULL, 0);
632+
return true;
633+
}
634+
615635
void rfcore_ble_hci_cmd(size_t len, const uint8_t *src) {
616636
DEBUG_printf("rfcore_ble_hci_cmd\n");
617637

ports/stm32/rfcore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef void (*rfcore_ble_msg_callback_t)(void *, const uint8_t *, size_t);
3333
void rfcore_init(void);
3434

3535
void rfcore_ble_init(void);
36+
bool rfcore_ble_reset(void);
3637
void rfcore_ble_hci_cmd(size_t len, const uint8_t *src);
3738
size_t rfcore_ble_check_msg(rfcore_ble_msg_callback_t cb, void *env);
3839
void rfcore_ble_set_txpower(uint8_t level);

0 commit comments

Comments
 (0)