打開跟蹤調試模塊,錯誤定位如下:
error> app: ERROR 4 [NRF_ERROR_NO_MEM] at ..\..\..\ble_app.c:1293
我們轉到ble_app.c 的1293行看一下:
其實看一下ble_ancs_c_init()的返回值,就知道是在函數內部已經出錯了,進入到函數中繼續跟蹤調試。
ret_code_t ble_ancs_c_init(ble_ancs_c_t * p_ancs, ble_ancs_c_init_t const * p_ancs_init)
{
uint32_t err_code;
//Verify that the parameters needed for to initialize this instance of ANCS are not NULL.
VERIFY_PARAM_NOT_NULL(p_ancs);
VERIFY_PARAM_NOT_NULL(p_ancs_init);
VERIFY_PARAM_NOT_NULL(p_ancs_init->evt_handler);
//Initialize state for the attribute parsing state machine.
p_ancs->parse_info.parse_state = COMMAND_ID;
p_ancs->parse_info.p_data_dest = NULL;
p_ancs->parse_info.current_attr_index = 0;
p_ancs->parse_info.current_app_id_index = 0;
p_ancs->evt_handler = p_ancs_init->evt_handler;
p_ancs->error_handler = p_ancs_init->error_handler;
p_ancs->conn_handle = BLE_CONN_HANDLE_INVALID;
p_ancs->service.data_source_cccd.uuid.uuid = BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG;
p_ancs->service.notif_source_cccd.uuid.uuid = BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG;
// Make sure instance of service is clear. GATT handles inside the service and characteristics are set to @ref BLE_GATT_HANDLE_INVALID.
memset(&p_ancs->service, 0, sizeof(ble_ancs_c_service_t));
tx_buffer_init();
// Assign UUID types.
err_code = sd_ble_uuid_vs_add(&ble_ancs_base_uuid128, &p_ancs->service.service.uuid.type);
VERIFY_SUCCESS(err_code);
err_code = sd_ble_uuid_vs_add(&ble_ancs_cp_base_uuid128, &p_ancs->service.control_point_char.uuid.type);
VERIFY_SUCCESS(err_code);
err_code = sd_ble_uuid_vs_add(&ble_ancs_ns_base_uuid128, &p_ancs->service.notif_source_char.uuid.type);
VERIFY_SUCCESS(err_code);
err_code = sd_ble_uuid_vs_add(&ble_ancs_ds_base_uuid128, &p_ancs->service.data_source_char.uuid.type);
VERIFY_SUCCESS(err_code);
// Assign UUID to the service.
p_ancs->service.service.uuid.uuid = ANCS_UUID_SERVICE;
p_ancs->service.service.uuid.type = p_ancs->service.service.uuid.type;
return ble_db_discovery_evt_register(&p_ancs->service.service.uuid);
}
就是在這個函數中出現錯誤的,我們看一下nRF52832的錯誤的類型:
/** @defgroup NRF_ERRORS_BASE Error Codes Base number definitions
* @{ */
#define NRF_ERROR_BASE_NUM (0x0) ///< Global error base
#define NRF_ERROR_SDM_BASE_NUM (0x1000) ///< SDM error base
#define NRF_ERROR_SOC_BASE_NUM (0x2000) ///< SoC error base
#define NRF_ERROR_STK_BASE_NUM (0x3000) ///< STK error base
/** @} */
#define NRF_SUCCESS (NRF_ERROR_BASE_NUM + 0) ///< Successful command
#define NRF_ERROR_SVC_HANDLER_MISSING (NRF_ERROR_BASE_NUM + 1) ///< SVC handler is missing
#define NRF_ERROR_SOFTDEVICE_NOT_ENABLED (NRF_ERROR_BASE_NUM + 2) ///< SoftDevice has not been enabled
#define NRF_ERROR_INTERNAL (NRF_ERROR_BASE_NUM + 3) ///< Internal Error
#define NRF_ERROR_NO_MEM (NRF_ERROR_BASE_NUM + 4) ///< No Memory for operation
#define NRF_ERROR_NOT_FOUND (NRF_ERROR_BASE_NUM + 5) ///< Not found
#define NRF_ERROR_NOT_SUPPORTED (NRF_ERROR_BASE_NUM + 6) ///< Not supported
#define NRF_ERROR_INVALID_PARAM (NRF_ERROR_BASE_NUM + 7) ///< Invalid Parameter
#define NRF_ERROR_INVALID_STATE (NRF_ERROR_BASE_NUM + 8) ///< Invalid state, operation disallowed in this state
#define NRF_ERROR_INVALID_LENGTH (NRF_ERROR_BASE_NUM + 9) ///< Invalid Length
#define NRF_ERROR_INVALID_FLAGS (NRF_ERROR_BASE_NUM + 10) ///< Invalid Flags
#define NRF_ERROR_INVALID_DATA (NRF_ERROR_BASE_NUM + 11) ///< Invalid Data
#define NRF_ERROR_DATA_SIZE (NRF_ERROR_BASE_NUM + 12) ///< Invalid Data size
#define NRF_ERROR_TIMEOUT (NRF_ERROR_BASE_NUM + 13) ///< Operation timed out
#define NRF_ERROR_NULL (NRF_ERROR_BASE_NUM + 14) ///< Null Pointer
#define NRF_ERROR_FORBIDDEN (NRF_ERROR_BASE_NUM + 15) ///< Forbidden Operation
#define NRF_ERROR_INVALID_ADDR (NRF_ERROR_BASE_NUM + 16) ///< Bad Memory Address
#define NRF_ERROR_BUSY (NRF_ERROR_BASE_NUM + 17) ///< Busy
#define NRF_ERROR_CONN_COUNT (NRF_ERROR_BASE_NUM + 18) ///< Maximum connection count exceeded.
#define NRF_ERROR_RESOURCES (NRF_ERROR_BASE_NUM + 19) ///< Not enough resources for operation
#define NRF_ERROR_INVALID_LICENSE_KEY (NRF_ERROR_BASE_NUM + 64) ///< Bad license key
根據返回的錯誤碼,可以確定內存不足。
可以根據三個方向調試內存:
1.堆棧內存
在startup_config.h中
* Define size of stack. Size must be multiple of 4. */
#define __STARTUP_CONFIG_STACK_SIZE 0x1000
/* Define size of heap. Size must be multiple of 4. */
#define __STARTUP_CONFIG_HEAP_SIZE 0x1000
2.Keil中內存設置
3.藍牙屬性列表大小
在sdk_config.h中,根據使用的藍牙服務,以及服務特徵的數量來調整對應的大小,確保藍牙屬性列表不會越界。
// <o> NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE - Attribute Table size in bytes. The size must be a multiple of 4.
#ifndef NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 2048
#endif
從上面三個方向入手,雖然能解決一些常見的內存不足的錯誤,但是還是沒有搞定NRF_ERROR_NO_MEM這個錯誤。
最後定位錯誤出現在sd_ble_uuid_vs_add()
這個函數;當時沒有什麼思路,就等啊等…,第二天看書的時候突然靈光一閃,這裏添加的是自定義服務,莫非nRF中的自定義服務需要單獨處理一下……順着這個思路,真的找到了問題的根源。
// <o> NRF_SDH_BLE_VS_UUID_COUNT - The number of vendor-specific UUIDs.
#ifndef NRF_SDH_BLE_VS_UUID_COUNT
#define NRF_SDH_BLE_VS_UUID_COUNT 1
#endif
我在ble_ancs_c_init()
中添加了4個自定義服務,除了蘋果的ANCS,還在使用了一個自定義服務,總的用來5個,修改#define NRF_SDH_BLE_VS_UUID_COUNT 5
之後完美運行ing