This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Connecting BLE and using fstorage. Hard fault error 7 when ble_advertising_start is called

Hi,

I'm currently trying to make a flash logger using the nRF51822_XXAB. My logger is also advertising temperature, battery and other informations in beacon form. I also want to make it connectable so that I can push a 32bit word of epoch type that will then be written in flash between what I want to log to keep track of real time. All worked pretty well until I tried to turn my beacon to be connectable. I succesfully added everything I needed I thought, and I can now connect to it, write my characteristic and it is then written in flash as intended. 

My problem is that I call advertising_init in my main loop after making various readings so that my data is updated. I also have a flag that looks if a connection has been made which is set to 1 in case BLE_GAP_EVT_CONNECTED in on_ble_evt function. It is then set to 0 in case BLE_GAP_EVT_DISCONNECTED. My adfvertising_init in my main loop is called only if(!connection_has_been_made). The thing is, sometimes when I connect and then disconnect I go in hard fault. I tried to look for where this is happenning and I don't see anywhere where my error_status is not 0. This error happens way less if I delay my main loop by doing something like :   for (;;) { thicking++; if(thicking>1000000) { thicking=0; ..whole main loop.. }}. It must have something to do with where I am in my loop when I connect but I can't find it.

I have also a second problem, I write in flash once every 10 seconds and the process takes about 200 ms for various reasons, if I connect during this time I go in hard fault once I disconnect. For this error, I found that it happens when I call  err_code = ble_advertising_start(BLE_ADV_MODE_FAST) in case BLE_GAP_EVT_DISCONNECTED in on_ble_evt function. The error status is 7, which means NRF_ERROR_INVALID_PARAM.

These are my .h and .c for everythng related to BLE:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __ADVERTISEMENT_H
#define __ADVERTISEMENT_H
#include <stdint.h>
#include <string.h>
#include "nordic_common.h"
#include "nrf.h"
#include "ble_hci.h"
#include "ble_advertising.h"
#include "ble_advdata.h"
#include "ble_conn_params.h"
#include "ble_conn_state.h"
#include "softdevice_handler.h"
#include "app_timer.h"
#include "bsp.h"
#include "bsp_btn_ble.h"
#include "updateflash.h"
#include "SEGGER_RTT.h"
#include "SEGGER_RTT_Conf.h"
#define IS_SRVC_CHANGED_CHARACT_PRESENT 0 /**< Include the service_changed characteristic. If not enabled, the server's database cannot be changed for the lifetime of the device. */
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "advertisement.h"
ble_os_t m_our_service;
//ble_nus_t m_nus; /**< Structure to identify the Nordic UART Service. */
uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
uint8_t connection_has_been_made=0;
char result[10];
extern uint32_t new_time_sync;
//ble_uuid_t m_adv_uuids[] = {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}}; /**< Universally unique service identifier. */
uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] = // Information advertised by the Beacon.
{
APP_DEVICE_TYPE, // Manufacturer specific information. Specifies the device type in this implementation.
APP_ADV_DATA_LENGTH, // Manufacturer specific information. Specifies the length of the manufacturer specific data in this implementation.
APP_BEACON_UUID, // 128 bit UUID value.
APP_MAJOR_VALUE, // Major arbitrary value that can be used to distinguish between Beacons.
APP_MINOR_VALUE, // Minor arbitrary value that can be used to distinguish between Beacons.
APP_MEASURED_RSSI // Manufacturer specific information. The Beacon's measured TX power in
// this implementation.
};
/**@brief Function for assert macro callback.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


This is my main:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void)
{
uint32_t thicking=0;
uint32_t err_code;
nrf_temp_init();
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
err_code = bsp_init(BSP_INIT_LED, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL);
APP_ERROR_CHECK(err_code) ;
ble_stack_init() ;
gap_params_init() ;
services_init() ;
uint8_t data_ble[size_ble_data] = {type_beacon,0,battery_level,temperature[0],temperature[1],humidity,acceleration_x,0,acceleration_y,0,acceleration_z,0};
advertising_init(data_ble,size_ble_data) ;
conn_params_init() ;
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code) ;
SEGGER_RTT_WriteString(0, " RESET RESET RESET RESET RESET RESET \r\n");
// Enter main loop.
for (;;)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


This is my fstorsge event handler:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
{
if (result != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
else
{
fs_callback_flag = 0;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


EDIT: Forgot to mention I'm using SDK 12.3.0 with S130