From d9b275c321517d97272071752b948cdf462ca781 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Feb 2019 12:12:11 +0700 Subject: [PATCH] increase buffer depth to fix OTA issue with nrfConnect on iOS - also add optional debug helper - increase led conn cycle when ble connected for easier recognition --- .../bootloader_dfu/dfu_single_bank.c | 2 +- .../bootloader_dfu/dfu_transport_ble.c | 112 ++++++++++++++++++ src/boards.c | 4 +- src/boards.h | 46 +++++++ src/sdk_config.h | 4 +- .../Adafruit_nRF52_Bootloader.emProject | 2 + 6 files changed, 165 insertions(+), 5 deletions(-) diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c index 4e57a93..e6ccbf5 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c @@ -154,7 +154,7 @@ static void dfu_prepare_func_app_erase(uint32_t image_size) nrf_nvmc_page_erase(DFU_BANK_0_REGION_START + i * CODE_PAGE_SIZE); } - // simulate complete call + // invoke complete callback pstorage_callback_handler(&m_storage_handle_app, PSTORAGE_CLEAR_OP_CODE, NRF_SUCCESS, NULL, 0); } } diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c index 711cb78..6fb0787 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c @@ -736,6 +736,11 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) uint32_t err_code; ble_gatts_rw_authorize_reply_params_t auth_reply; +#ifdef CFG_DEBUG + extern const char* dbg_ble_event_str(uint16_t evt_id); + ADALOG("BLE", dbg_ble_event_str(p_ble_evt->header.evt_id)); +#endif + switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: @@ -896,6 +901,7 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: { uint16_t att_mtu = MIN(p_ble_evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu, BLEGATT_ATT_MTU_MAX); + ADALOG("GAP", "ATT MTU is changed to %d", att_mtu); APP_ERROR_CHECK( sd_ble_gatts_exchange_mtu_reply(m_conn_handle, att_mtu) ); } break; @@ -1088,3 +1094,109 @@ uint32_t dfu_transport_ble_close() return NRF_SUCCESS; } + +#ifdef CFG_DEBUG + +typedef struct +{ + uint32_t key; + void const * data; +}lookup_entry_t; + +typedef struct +{ + uint16_t count; + lookup_entry_t const* items; +} lookup_table_t; + +void const * lookup_find(lookup_table_t const* p_table, uint32_t key) +{ + for(uint16_t i=0; icount; i++) + { + if (p_table->items[i].key == key) return p_table->items[i].data; + } + + return NULL; +} + +/*------------------------------------------------------------------*/ +/* BLE Event String + *------------------------------------------------------------------*/ +static lookup_entry_t const _strevt_lookup[] = +{ + // BLE common: 0x01 + { .key = BLE_EVT_USER_MEM_REQUEST , .data= "BLE_EVT_USER_MEM_REQUEST" }, + { .key = BLE_EVT_USER_MEM_RELEASE , .data= "BLE_EVT_USER_MEM_RELEASE" }, + + // BLE Gap: 0x10 + { .key = BLE_GAP_EVT_CONNECTED , .data= "BLE_GAP_EVT_CONNECTED" }, + { .key = BLE_GAP_EVT_DISCONNECTED , .data= "BLE_GAP_EVT_DISCONNECTED" }, + { .key = BLE_GAP_EVT_CONN_PARAM_UPDATE , .data= "BLE_GAP_EVT_CONN_PARAM_UPDATE" }, + { .key = BLE_GAP_EVT_SEC_PARAMS_REQUEST , .data= "BLE_GAP_EVT_SEC_PARAMS_REQUEST" }, + { .key = BLE_GAP_EVT_SEC_INFO_REQUEST , .data= "BLE_GAP_EVT_SEC_INFO_REQUEST" }, + { .key = BLE_GAP_EVT_PASSKEY_DISPLAY , .data= "BLE_GAP_EVT_PASSKEY_DISPLAY" }, + { .key = BLE_GAP_EVT_KEY_PRESSED , .data= "BLE_GAP_EVT_KEY_PRESSED" }, + { .key = BLE_GAP_EVT_AUTH_KEY_REQUEST , .data= "BLE_GAP_EVT_AUTH_KEY_REQUEST" }, + { .key = BLE_GAP_EVT_LESC_DHKEY_REQUEST , .data= "BLE_GAP_EVT_LESC_DHKEY_REQUEST" }, + { .key = BLE_GAP_EVT_AUTH_STATUS , .data= "BLE_GAP_EVT_AUTH_STATUS" }, + { .key = BLE_GAP_EVT_CONN_SEC_UPDATE , .data= "BLE_GAP_EVT_CONN_SEC_UPDATE" }, + { .key = BLE_GAP_EVT_TIMEOUT , .data= "BLE_GAP_EVT_TIMEOUT" }, + { .key = BLE_GAP_EVT_RSSI_CHANGED , .data= "BLE_GAP_EVT_RSSI_CHANGED" }, + { .key = BLE_GAP_EVT_ADV_REPORT , .data= "BLE_GAP_EVT_ADV_REPORT" }, + { .key = BLE_GAP_EVT_SEC_REQUEST , .data= "BLE_GAP_EVT_SEC_REQUEST" }, + { .key = BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST , .data= "BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST" }, + { .key = BLE_GAP_EVT_SCAN_REQ_REPORT , .data= "BLE_GAP_EVT_SCAN_REQ_REPORT" }, + { .key = BLE_GAP_EVT_PHY_UPDATE_REQUEST , .data= "BLE_GAP_EVT_PHY_UPDATE_REQUEST" }, + { .key = BLE_GAP_EVT_PHY_UPDATE , .data= "BLE_GAP_EVT_PHY_UPDATE" }, + { .key = BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST , .data= "BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST" }, + { .key = BLE_GAP_EVT_DATA_LENGTH_UPDATE , .data= "BLE_GAP_EVT_DATA_LENGTH_UPDATE" }, + { .key = BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT , .data= "BLE_GAP_EVT_QOS_CHANNEL_SURVEY_REPORT" }, + { .key = BLE_GAP_EVT_ADV_SET_TERMINATED , .data= "BLE_GAP_EVT_ADV_SET_TERMINATED" }, + + // BLE Gattc: 0x30 + { .key = BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP , .data= "BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP" }, + { .key = BLE_GATTC_EVT_REL_DISC_RSP , .data= "BLE_GATTC_EVT_REL_DISC_RSP" }, + { .key = BLE_GATTC_EVT_CHAR_DISC_RSP , .data= "BLE_GATTC_EVT_CHAR_DISC_RSP" }, + { .key = BLE_GATTC_EVT_DESC_DISC_RSP , .data= "BLE_GATTC_EVT_DESC_DISC_RSP" }, + { .key = BLE_GATTC_EVT_ATTR_INFO_DISC_RSP , .data= "BLE_GATTC_EVT_ATTR_INFO_DISC_RSP" }, + { .key = BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP , .data= "BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP" }, + { .key = BLE_GATTC_EVT_READ_RSP , .data= "BLE_GATTC_EVT_READ_RSP" }, + { .key = BLE_GATTC_EVT_CHAR_VALS_READ_RSP , .data= "BLE_GATTC_EVT_CHAR_VALS_READ_RSP" }, + { .key = BLE_GATTC_EVT_WRITE_RSP , .data= "BLE_GATTC_EVT_WRITE_RSP" }, + { .key = BLE_GATTC_EVT_HVX , .data= "BLE_GATTC_EVT_HVX" }, + { .key = BLE_GATTC_EVT_EXCHANGE_MTU_RSP , .data= "BLE_GATTC_EVT_EXCHANGE_MTU_RSP" }, + { .key = BLE_GATTC_EVT_TIMEOUT , .data= "BLE_GATTC_EVT_TIMEOUT" }, + { .key = BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE , .data= "BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE" }, + + // BLE Gatts: 0x50 + { .key = BLE_GATTS_EVT_WRITE , .data= "BLE_GATTS_EVT_WRITE" }, + { .key = BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST , .data= "BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST" }, + { .key = BLE_GATTS_EVT_SYS_ATTR_MISSING , .data= "BLE_GATTS_EVT_SYS_ATTR_MISSING" }, + { .key = BLE_GATTS_EVT_HVC , .data= "BLE_GATTS_EVT_HVC" }, + { .key = BLE_GATTS_EVT_SC_CONFIRM , .data= "BLE_GATTS_EVT_SC_CONFIRM" }, + { .key = BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST , .data= "BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST" }, + { .key = BLE_GATTS_EVT_TIMEOUT , .data= "BLE_GATTS_EVT_TIMEOUT" }, + { .key = BLE_GATTS_EVT_HVN_TX_COMPLETE , .data= "BLE_GATTS_EVT_HVN_TX_COMPLETE" }, +}; + +lookup_table_t const _strevt_table = +{ + .count = arrcount(_strevt_lookup), + .items = _strevt_lookup +}; + +const char* dbg_ble_event_str(uint16_t evt_id) +{ + const char * str = (const char *) lookup_find(&_strevt_table, evt_id); + static char unknown_str[7] = {0}; + + if ( str == NULL ) + { + sprintf(unknown_str, "0x%04X", evt_id); + str = unknown_str; + } + + return str; +} + +#endif diff --git a/src/boards.c b/src/boards.c index bd1f2ae..5c6ba90 100644 --- a/src/boards.c +++ b/src/boards.c @@ -253,9 +253,9 @@ void led_state(uint32_t state) case STATE_BLE_CONNECTED: new_rgb_color = 0x0000ff; #ifdef LED_SECONDARY_PIN - secondary_cycle_length = 500; + secondary_cycle_length = 3000; #else - primary_cycle_length = 500; + primary_cycle_length = 3000; #endif break; diff --git a/src/boards.h b/src/boards.h index 6f434e8..37f5f2d 100644 --- a/src/boards.h +++ b/src/boards.h @@ -96,4 +96,50 @@ static inline bool button_pressed(uint32_t pin) bool is_ota(void); +//--------------------------------------------------------------------+ +// DEBUG +//--------------------------------------------------------------------+ +//#define CFG_DEBUG + +#ifdef CFG_DEBUG + +#include + +#define PRINT_LOCATION() printf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__) +#define PRINT_MESS(x) printf("%s: %d: %s \n" , __FUNCTION__, __LINE__, (char*)(x)) +#define PRTNT_HEAP() if (CFG_DEBUG == 3) printf("\n%s: %d: Heap free: %d\n", __FUNCTION__, __LINE__, util_heap_get_free_size()) +#define PRINT_STR(x) printf("%s: %d: " #x " = %s\n" , __FUNCTION__, __LINE__, (char*)(x) ) +#define PRINT_INT(x) printf("%s: %d: " #x " = %ld\n" , __FUNCTION__, __LINE__, (uint32_t) (x) ) +#define PRINT_HEX(x) printf("%s: %d: " #x " = 0x%X\n" , __FUNCTION__, __LINE__, (uint32_t) (x) ) + +#define PRINT_BUFFER(buf, n) \ + do {\ + uint8_t const* p8 = (uint8_t const*) (buf);\ + printf(#buf ": ");\ + for(uint32_t i=0; i<(n); i++) printf("%x ", p8[i]);\ + printf("\n");\ + }while(0) + +#define ADALOG(tag, ...) \ + do { \ + if ( tag ) printf("[%s] ", tag);\ + printf(__VA_ARGS__);\ + printf("\n");\ + }while(0) + +#else + +#define PRINT_LOCATION() +#define PRINT_MESS(x) +#define PRTNT_HEAP() +#define PRINT_STR(x) +#define PRINT_INT(x) +#define PRINT_HEX(x) +#define PRINT_BUFFER(buf, n) + +#define ADALOG(...) + +#endif + + #endif diff --git a/src/sdk_config.h b/src/sdk_config.h index 62cd86f..51d9e92 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -64,9 +64,9 @@ // HCI_MEM_POOL_ENABLED - hci_mem_pool - memory pool implementation used by HCI //========================================================== #define HCI_MEM_POOL_ENABLED 1 -#define HCI_TX_BUF_SIZE 600 +#define HCI_TX_BUF_SIZE 600 // not used #define HCI_RX_BUF_SIZE 600 -#define HCI_RX_BUF_QUEUE_SIZE 4 +#define HCI_RX_BUF_QUEUE_SIZE 8 // must be power of 2 //========================================================== // UART_ENABLED - nrf_drv_uart - UART/UARTE peripheral driver diff --git a/src/segger/Adafruit_nRF52_Bootloader.emProject b/src/segger/Adafruit_nRF52_Bootloader.emProject index 6caf020..57303ee 100644 --- a/src/segger/Adafruit_nRF52_Bootloader.emProject +++ b/src/segger/Adafruit_nRF52_Bootloader.emProject @@ -375,10 +375,12 @@