结构体
static struct
tool_config_t{
#define MAILIN_GADDRESS_LONG 9
uint8_t chMailingAddress[MAILIN_GADDRESS_LONG];//octet string
#define CUSTOMER_NUMBER_LONG 11
uint8_t chCustomerNumber[CUSTOMER_NUMBER_LONG];//octet string
#define UNLOCKING_CODE_LONG 23
uint8_t chUnlockingCode[UNLOCKING_CODE_LONG];//octet string
#define BLOCKING_CODE_LONG 4
uint8_t chBlockingCode[BLOCKING_CODE_LONG];//octet string param1.2
// 1+1 +1
#define SAFE_MODE_LONG 4
uint8_t chSafeMode[SAFE_MODE_LONG];//enum 0/1
struct blueTooth
{
#define PORT_DESCRIPTION_LONG 15 /
uint8_t chPortDescription[PORT_DESCRIPTION_LONG];//vidible_string ASCII
struct comdcb{ // 4
uint8_t chDataType;//
uint8_t chType; //
uint8_t chBaudRate;//chWorkingMode=1 :
uint8_t chParityBits;//low
uint8_t chDataBits;//4s
uint8_t chStopBits;//
uint8_t chFlowControl;//
}s_tComdcb;
}s_tBlueTooth;
uint8_t chFCS[2];
}s_tToolConfig = {0};
写入flash
在回调函数中添加事件句柄(nRF51822协议中的ble_stack_init(void)在main中重写)
static void ble_stack_init(void)
{
uint32_t err_code;
nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
// Initialize SoftDevice.
SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
ble_enable_params_t ble_enable_params;
err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT,
&ble_enable_params);
APP_ERROR_CHECK(err_code);
//Check the ram settings against the used number of links
CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
// Enable BLE stack.
err_code = softdevice_enable(&ble_enable_params);
APP_ERROR_CHECK(err_code);
// Subscribe for BLE events.
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
APP_ERROR_CHECK(err_code);
err_code=softdevice_sys_evt_handler_set(sys_evt_dispatch);
APP_ERROR_CHECK(err_code);
}
static void sys_evt_dispatch(uint32_t sys_evt)
{
pstorage_sys_event_handler(sys_evt);
}
flash的注册申请及写读函数
torage_handle_t g_block_id;
pstorage_handle_t block_id;//
uint8_t my_flag = 0;//
uint8_t my_buff[8]={0}; //
//
static void my_cb(pstorage_handle_t * handle,uint8_t op_code,uint32_t result,
uint8_t * p_data, uint32_t data_len)
{
switch(op_code)
{
case PSTORAGE_UPDATE_OP_CODE:
if (result == NRF_SUCCESS)
{
my_flag = 1; //
}
else
{
// Update operation failed.
}
break;
}
}
static void flash_tool_write(void *addr, uint8_t lenght)
{
uint8_t *adr =0;
pstorage_handle_t dest_block_id;
uint8_t len = lenght>80?80:lenght;
uint8_t buff[len];
printf("\r\nread write: ");
for(uint8_t i = 0; i< lenght/sizeof(uint8_t); i++){
adr=(uint8_t*)((char*)addr+i*sizeof(uint8_t));
buff[i]=*adr;
printf("%X",buff[i]);
}
printf("\r\n");
memcpy(my_buff, buff, lenght/sizeof(uint8_t));
pstorage_block_identifier_get(&g_block_id, 0, &dest_block_id); //
pstorage_update(&dest_block_id, my_buff,lenght/sizeof(uint8_t), 0);
}
static void flash_read(pstorage_handle_t g_block_id, uint8_t lenght)
{
uint8_t buff[lenght/sizeof(uint8_t)];
if ( my_flag == 1 ){
printf("\r\nread flash: ");
my_flag = 0;
pstorage_handle_t dest_block_id;
pstorage_block_identifier_get(&g_block_id,0,&dest_block_id);
pstorage_load(buff, &dest_block_id,lenght/sizeof(uint8_t), 0);
for(int i = 0; i< lenght/sizeof(uint8_t); i++){
printf("%X ",buff[i]);
}
printf("\r\n");
}
}
int main(void)
{
......
pstorage_module_param_t module_param1;
module_param1.block_count = 1; //
module_param1.block_size = 160; //
module_param1.cb = my_cb;
pstorage_init();
pstorage_register(&module_param1, &g_block_id);//
flash_tool_write(&s_tToolConfig,sizeof(s_tToolConfig));
my_flag=1;
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
// Enter main loop.
for (;;)
{
power_manage();
flash_read(g_block_id,sizeof(s_tToolConfig));
}
}