Skip to content

Commit 52ee8ab

Browse files
committed
factory-setup: Check if BLE chip is booted
If the BLE chip boots for the first time it will request various things by itself, like the bond db. But if the chip already is booted (in case factory setup is run twice) we need to request something to get a message from it. The main MCU has to respond immediately when it sees STX, loading the firmware must be done before we reset the BLE MCU so that this MCU is ready.
1 parent 526de5c commit 52ee8ab

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/da14531/da14531_protocol.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ struct da14531_protocol {
8282

8383
static struct da14531_protocol _protocol;
8484

85+
static uint8_t* ble_fw = NULL;
86+
static size_t ble_fw_size = 0;
87+
static uint8_t ble_fw_checksum = 0;
88+
8589
#if 0
8690
static const char* _firmware_loader_state_str(enum firmware_loader_state state)
8791
{
@@ -104,14 +108,7 @@ static const char* _firmware_loader_state_str(enum firmware_loader_state state)
104108

105109
static void _firmware_loader_init(struct firmware_loader* self)
106110
{
107-
// If we are in factory setup we expect to load the ble firmware, so we start in IDLE.
108-
// In production bootloader and firmware we expect the da14531 to already be booted. We will still
109-
// load the firmware if it happens to not be loaded.
110-
#if FACTORYSETUP == 1
111-
self->state = FIRMWARE_LOADER_STATE_IDLE;
112-
#else
113111
self->state = FIRMWARE_LOADER_STATE_DONE;
114-
#endif
115112
}
116113

117114
#define SOH 0x01
@@ -125,10 +122,6 @@ static void _firmware_loader_poll(
125122
uint16_t* buf_in_len,
126123
struct ringbuffer* out_queue)
127124
{
128-
static uint8_t* ble_fw = NULL;
129-
static size_t ble_fw_size = 0;
130-
static uint8_t ble_fw_checksum = 0;
131-
132125
// if (*buf_in_len > 0) {
133126
// util_log(
134127
// "%s, got bytes %s",
@@ -394,7 +387,7 @@ struct da14531_protocol_frame* da14531_protocol_poll(
394387
const uint8_t** hww_data,
395388
struct ringbuffer* out_queue)
396389
{
397-
if (*hww_data) {
390+
if (hww_data && *hww_data) {
398391
uint8_t tmp[128];
399392
int len = da14531_protocol_format(
400393
&tmp[0], sizeof(tmp), DA14531_PROTOCOL_PACKET_TYPE_BLE_DATA, *hww_data, 64);
@@ -464,11 +457,19 @@ void da14531_protocol_init(void)
464457
// Only attempt swd reset in factory setup or debug builds. In production swd is turned off and
465458
// this is therefore useless.
466459
#if FACTORYSETUP == 1 || !defined(NDEBUG)
460+
// Load the firmware from external flash to RAM so that we are ready to flash.
461+
if (ble_fw == NULL) {
462+
if (!memory_spi_get_active_ble_firmware(&ble_fw, &ble_fw_size, &ble_fw_checksum)) {
463+
util_log("da14531: no valid firmware");
464+
}
465+
}
467466
// Reset the device if possible, if we cannot reset it over SWD, it must already be running
468467
if (!_swd_reset_da14531()) {
469468
// This may fail if the BLE chip has been started with a production firmware that has
470469
// disabled the debug interface
471470
util_log("da14531: Failed to reset over SWD");
471+
free(ble_fw);
472+
ble_fw = NULL;
472473
} else {
473474
// If we successfully reset the chip, we also would like to load it with firmware
474475
_protocol.loader.state = FIRMWARE_LOADER_STATE_IDLE;

src/factorysetup.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "common_main.h"
16+
#include "da14531/da14531.h"
1617
#include "da14531/da14531_binary.h"
1718
#include "da14531/da14531_protocol.h"
1819
#include "driver_init.h"
@@ -244,18 +245,18 @@ typedef enum {
244245
} error_code_t;
245246

246247
typedef enum {
247-
BLE_OK,
248-
BLE_ERR_FW_TOO_LARGE,
249-
BLE_ERR_FLASH_FW,
250-
BLE_ERR_GET_METADATA,
251-
BLE_ERR_SET_METADATA,
252-
BLE_ERR_READ_FW,
253-
BLE_ERR_FW_SIZE_MISMATCH,
254-
BLE_ERR_FW_CHECKSUM_MISMATCH,
255-
BLE_ERR_FW_MISMATCH,
256-
BLE_ERR_SPI_ERASE,
257-
BLE_ERR_FW_NOT_ALLOWED,
258-
BLE_ERR_NOT_BOOTED,
248+
BLE_OK = 0,
249+
BLE_ERR_FW_TOO_LARGE = 1,
250+
BLE_ERR_FLASH_FW = 2,
251+
BLE_ERR_GET_METADATA = 3,
252+
BLE_ERR_SET_METADATA = 4,
253+
BLE_ERR_READ_FW = 5,
254+
BLE_ERR_FW_SIZE_MISMATCH = 6,
255+
BLE_ERR_FW_CHECKSUM_MISMATCH = 7,
256+
BLE_ERR_FW_MISMATCH = 8,
257+
BLE_ERR_SPI_ERASE = 9,
258+
BLE_ERR_FW_NOT_ALLOWED = 10,
259+
BLE_ERR_NOT_BOOTED = 11,
259260
} ble_error_code_t;
260261

261262
static ble_error_code_t _ble_result;
@@ -563,6 +564,10 @@ static ble_error_code_t _setup_ble(void)
563564
uint8_t uart_write_buf[1024];
564565
struct ringbuffer uart_write_queue;
565566
ringbuffer_init(&uart_write_queue, uart_write_buf, sizeof(uart_write_buf));
567+
// If the BLE chip already was successfully booted, for example by running the factory-setup
568+
// once already and not power cycled, we need to ask for something to get a uart frame back.
569+
// Therefore we schedule a "get connection state".
570+
da14531_get_connection_state(&uart_write_queue);
566571
int32_t timeout = 1000000;
567572
while (timeout-- > 0) {
568573
uart_poll(uart_read_buf, sizeof(uart_read_buf), &uart_read_buf_len, &uart_write_queue);
@@ -573,6 +578,7 @@ static ble_error_code_t _setup_ble(void)
573578
return BLE_OK;
574579
}
575580
}
581+
screen_print_debug("Failed to check BLE chip status", 0);
576582
return BLE_ERR_NOT_BOOTED;
577583
}
578584

0 commit comments

Comments
 (0)