Advertisement
leobin1989

Nrf51822 pstorage

Jul 11th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static void pstorage_handle(pstorage_handle_t *  p_handle,
  2.         uint8_t              op_code,
  3.         uint32_t             result,
  4.         uint8_t *            p_data,
  5.         uint32_t             data_len) {
  6.     //todo: this is for error handler. Need to nortify if something happen
  7.     uint32_t err_code;
  8.     uint8_t data[4] = {1,2,3,4};
  9.     uint8_t temp_result[4];
  10.     uint32_t count;
  11.     pstorage_handle_t temp_block;
  12.     switch(op_code)
  13.     {
  14.         case PSTORAGE_STORE_OP_CODE:
  15.         case PSTORAGE_UPDATE_OP_CODE:
  16.             if (result == NRF_SUCCESS)
  17.             {
  18.  
  19.                              nrf_delay_ms(1000);
  20.                 pstorage_block_identifier_get(&m_pstorage_handle, 0, &temp_block);
  21.                 err_code = pstorage_load(temp_result, &temp_block, 4, 0 );
  22.                 if (err_code != NRF_SUCCESS)
  23.                     APP_ERROR_CHECK(err_code);
  24.                 nrf_gpio_pin_set(LED_START + temp_result[0]);
  25. //Store operation successful.
  26.             }
  27.             else
  28.             {
  29. // Store operation failed.
  30.             }
  31. // Source memory can now be reused or freed.
  32.             break;
  33.         case PSTORAGE_CLEAR_OP_CODE:
  34.  
  35.             if (result == NRF_SUCCESS)
  36.             {
  37.                 pstorage_block_identifier_get(&m_pstorage_handle, 0, &temp_block);
  38.                            
  39.                 err_code = pstorage_store(&temp_block, data, 4, 0);
  40.                             //nrf_delay_ms(1000);
  41.                 if (err_code != NRF_SUCCESS)
  42.                     APP_ERROR_CHECK(err_code);
  43.             } else
  44.             {
  45.  
  46.             }
  47.             break;
  48.         case PSTORAGE_LOAD_OP_CODE:
  49.                     nrf_gpio_pin_set(LED_START + p_data[0]);
  50.                     break;
  51.         default:
  52.             break;
  53.     }
  54. }
  55.  
  56. //init pstorage
  57. static void data_init(void){
  58.     uint32_t err_code;
  59.     extern pstorage_handle_t m_pstorage_handle;
  60.     extern pstorage_module_param_t m_pstorage_param;
  61.     err_code = pstorage_init();
  62.     if (err_code == NRF_SUCCESS) {
  63.         //module initialization successful
  64.         m_pstorage_param.block_size = 20;
  65.         m_pstorage_param.block_count = 2;
  66.         m_pstorage_param.cb          = pstorage_handle;
  67.         err_code = pstorage_register(&m_pstorage_param, &m_pstorage_handle);
  68.         if (err_code != NRF_SUCCESS)
  69.             APP_ERROR_CHECK(err_code);
  70.         err_code = pstorage_clear(&m_pstorage_handle, 20*2);
  71.         if (err_code != NRF_SUCCESS)
  72.             APP_ERROR_CHECK(err_code);
  73.  
  74.     } else {
  75.         APP_ERROR_CHECK(err_code);
  76.     }
  77. }
  78.  
  79.  
  80. int main(void)
  81. {
  82.     advertising_status = 0;
  83.     // Initialize
  84.     leds_init();
  85.     timers_init();
  86.     ble_stack_init();
  87.     scheduler_init();
  88.     data_init();
  89.  
  90.  
  91.     // Enter main loop
  92.     for (;;)
  93.     {
  94.         app_sched_execute();
  95.         power_manage();
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement