Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
pru_pstorage.c
Go to the documentation of this file.
1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
2  *
3  * The information contained herein is property of Nordic Semiconductor ASA.
4  * Terms and conditions of usage are described in detail in NORDIC
5  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6  *
7  * Licensees are granted free, non-transferable use of the information. NO
8  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9  * the file.
10  *
11  */
12 
15 #ifdef PRU_PSTORAGE_ENABLE
16 
17 #include "pru_pstorage.h"
18 #include "pru.h"
19 #include "nrf_error.h"
20 #include "nrf_soc.h"
21 #include "string.h"
22 
23 #define PRU_PSTORAGE_EMPTY_FLASH_PATTERN 0xffffffff
24 #define PRU_PSTORAGE_FLASH_PAGE_SIZE ((uint16_t)NRF_FICR->CODEPAGESIZE)
25 #define PRU_PSTORAGE_FLASH_START_PAGE (NRF_FICR->CODESIZE - 4)
26 #define PRU_PSTORAGE_FLASH_START_ADDR \
27  (PRU_PSTORAGE_FLASH_PAGE_SIZE * PRU_PSTORAGE_FLASH_START_PAGE)
29 /******************************************************************************/
30 
32 /******************************************************************************/
33 
35 typedef enum {
36  PRU_PSTORAGE_STATE_IDLE,
37  PRU_PSTORAGE_STATE_ERASE,
38  PRU_PSTORAGE_STATE_WRITE
40 
41 static volatile bool m_operation_success;
42 static volatile pru_pstorage_state m_state;
46 /******************************************************************************/
49 /******************************************************************************/
50 
51 void pru_pstorage_sys_evt_handler(uint32_t event)
52 {
53  m_state = PRU_PSTORAGE_STATE_IDLE;
54  switch(event)
55  {
56  case NRF_EVT_FLASH_OPERATION_ERROR:
57  m_operation_success = false;
58  break;
59 
60  case NRF_EVT_FLASH_OPERATION_SUCCESS:
61  m_operation_success = true;
62  break;
63 
64  default:
65  break;
66  }
67 }
68 
70 {
71  uint32_t err_code = NRF_ERROR_BUSY;
72  m_operation_success = false;
73 
74  if(p_appdata == NULL)
75  {
76  APP_ERROR_HANDLER(NRF_ERROR_INVALID_PARAM);
77  return;
78  }
79 
80  // Wait for pending PSTORAGE action to complete
81  while(m_state != PRU_PSTORAGE_STATE_IDLE)
82  ;
83 
84  while(err_code == NRF_ERROR_BUSY)
85  {
86  m_state = PRU_PSTORAGE_STATE_WRITE;
87  err_code = sd_flash_write((uint32_t*) PRU_PSTORAGE_FLASH_START_ADDR,
88  (uint32_t *) p_appdata,
89  sizeof(pru_pstorage_appdata_t) / sizeof(uint32_t));
90  }
91  APP_ERROR_CHECK(err_code);
92 }
93 
95 {
96  if(p_appdata != NULL)
97  memcpy(p_appdata, (void*) PRU_PSTORAGE_FLASH_START_ADDR, sizeof(pru_pstorage_appdata_t));
98 
99  return p_appdata != NULL && (*(uint32_t *) p_appdata != PRU_PSTORAGE_EMPTY_FLASH_PATTERN);
100 }
101 
103  {
104  uint32_t err_code = NRF_ERROR_BUSY;
105 
106  m_state = PRU_PSTORAGE_STATE_ERASE;
107 
108  while(err_code == NRF_ERROR_BUSY)
109  {
110  err_code = sd_flash_page_erase(PRU_PSTORAGE_FLASH_START_PAGE);
111  }
112 
113  APP_ERROR_CHECK(err_code);
114 }
115 
116 void pru_pstorage_status(bool * p_complete, bool * p_success)
117 {
118  *p_complete = (m_state == PRU_PSTORAGE_STATE_IDLE);
119  *p_success = m_operation_success;
120 }
121 
124 #endif
pru_pstorage_state
Pstorage state.
Definition: pru_pstorage.c:35
static volatile pru_pstorage_state m_state
Definition: pru_pstorage.c:42
void pru_pstorage_erase_appdata(void)
Erase all of PRU appdata.
Definition: pru_pstorage.c:102
void pru_pstorage_write(pru_pstorage_appdata_t *p_appdata)
Writes application data to flash.
Definition: pru_pstorage.c:69
void pru_pstorage_status(bool *p_complete, bool *p_success)
Read status of pstorage operation.
Definition: pru_pstorage.c:116
void pru_pstorage_sys_evt_handler(uint32_t event)
Handles system callbacks.
Definition: pru_pstorage.c:51
#define PRU_PSTORAGE_FLASH_START_PAGE
Definition: pru_pstorage.c:25
PRU pstorage data container.
Definition: pru_pstorage.h:35
#define PRU_PSTORAGE_EMPTY_FLASH_PATTERN
Definition: pru_pstorage.c:23
static volatile bool m_operation_success
Definition: pru_pstorage.c:41
#define PRU_PSTORAGE_FLASH_START_ADDR
Definition: pru_pstorage.c:26
bool pru_pstorage_read(pru_pstorage_appdata_t *p_appdata)
Reads application data from flash.
Definition: pru_pstorage.c:94