Contiki 3.x
nvm.h
Go to the documentation of this file.
1 /** @file hal/micro/cortexm3/nvm.h
2  * @brief Cortex-M3 Non-Volatile Memory data storage system.
3  * See @ref nvm for documentation.
4  *
5  * The functions in this file return an ::StStatus value.
6  * See error-def.h for definitions of all ::StStatus return values.
7  *
8  * See hal/micro/cortexm3/nvm.h for source code.
9  *
10  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
11  */
12 
13 /**
14  * @addtogroup stm32w-cpu
15  * @{ */
16 
17 /** @defgroup nvm
18  * @brief Cortex-M3 Non-Volatile Memory data storage system.
19  *
20  * This header defines the API for NVM data storage. This header also
21  * describes the algorithm behind the NVM data storage system with notes
22  * on algorithm behavior.
23  *
24  * See hal/micro/cortexm3/nvm.h for source code.
25  *
26  * @note The algorithm description uses "page" to indicate an area of memory
27  * that is a multiple of physical flash pages. There are two pages: LEFT
28  * and RIGHT. The term "flash page" is used to refer to a page of
29  * physical flash.
30  *
31  * NVM data storage works by alternating between two pages: LEFT and RIGHT.
32  * The basic algorithm is driven by a call to halCommonSaveToNvm(). It will:
33  * - erase the inactive page
34  * - write the new data to the inactive page
35  * - copy existing data from the active page to the inactive page
36  * - mark the inactive page as the new active page
37  * - mark the old active page as the new inactive page
38  * To accomplish alternating between two pages and knowing which page has the
39  * valid set of data, the algorithm uses 4 bytes of mgmt data that exists
40  * at the top of both LEFT and RIGHT (the term "mgmt" is shorthand referring to
41  * the management data). The management data is comprised of a Valid marker,
42  * an Active marker, a Dead marker, and a Spare byte. Viewing the
43  * management data as a single 32 bit quantity yields:
44  * - Valid is mgmt[0]
45  * - Active is mgmt[1]
46  * - Dead is mgmt[2]
47  * - Spare is mgmt[3]
48  * The algorithm is based on a simple, circular state machine. The following
49  * discussion details all of the possible mgmt bytes and the states they
50  * correspond to. The "Reads from" line indicates which page a call to
51  * halCommonReadFromNvm() will read from (an 'x' page will stuff the read
52  * data with 0xFF). The vertical "erase" and "write" words indicate the
53  * flash altering actions taken between those states. Invalid mgmt bytes
54  * is equivalent to erased mgmt bytes (state 0) and will trigger an
55  * erase of both LEFT and RIGHT. State 3 and state 7 are the only exit
56  * states. When the algorithm is run, regardless of starting state, it
57  * will advance to the next exit state. This means if the "Read from"
58  * is LEFT then the state machine will advance until state 7 and then exit.
59  * If "Read from" is RIGHT, then the state machine will advance until
60  * state 3 and then exit.
61  *
62  * @code
63  * Starting from erased or invalid mgmt, write to LEFT
64  * State # 0 0 1 2 3
65  * Reads from: x x e w L L L
66  * Valid xx|xx FF|FF r r 00|FF 00|FF 00|00
67  * Active xx|xx FF|FF a i 00|FF 00|FF 00|00
68  * Dead xx|xx FF|FF s t FF|FF FF|00 FF|00
69  * Spare xx|xx FF|FF e e FF|FF FF|FF FF|FF
70  *
71  *
72  * Starting from LEFT page, transition to RIGHT page:
73  * State # 3 4 5 6 7
74  * Reads from: L e L w R R R
75  * Valid 00|00 r 00|FF r 00|00 00|00 00|00
76  * Active 00|00 a 00|FF i 00|FF 00|FF 00|00
77  * Dead FF|00 s FF|FF t FF|FF 00|FF 00|FF
78  * Spare FF|FF e FF|FF e FF|FF FF|FF FF|FF
79  *
80  *
81  * Starting from RIGHT page, transition to LEFT page:
82  * State # 7 8 9 10 3
83  * Reads from: R e R w L L L
84  * Valid 00|00 r FF|00 r 00|00 00|00 00|00
85  * Active 00|00 a FF|00 i FF|00 FF|00 00|00
86  * Dead 00|FF s FF|FF t FF|FF FF|00 FF|00
87  * Spare FF|FF e FF|FF e FF|FF FF|FF FF|FF
88  * @endcode
89  *
90  * Based on the 10 possible states, there are 5 valid 32bit mgmt words:
91  * - 0xFFFFFFFF
92  * - 0xFFFFFF00
93  * - 0xFFFF0000
94  * - 0xFF000000
95  * - 0xFF00FFFF
96  * The algorithm determines the current state by using these 5 mgmt words
97  * with the 10 possible combinations of LEFT mgmt and RIGHT mgmt.
98  *
99  * Detailed State Description:
100  * - State 0:
101  * In this state the mgmt bytes do not conform to any of the other states
102  * and therefore the entire NVM system, both the LEFT and RIGHT, is
103  * invalid. Invalid could be as simple as both LEFT and RIGHT are erased
104  * or as complex as serious memory corruption or a bug caused bad data to
105  * be written to the NVM. By using a small set of very strict, precise,
106  * valid states (versus other management systems such as a simple counter),
107  * the algorithm/data gains some protection against not only corruption, but
108  * also executing the NVM algorithm on a chip that previously did not
109  * have the NVM system running on it.
110  * - State 1, 4, 8
111  * In these states, mgmt is saying that one page is valid and active, while
112  * the other page is erased. This tells the algorithm which page to read
113  * from and indicates that the other page has already been erased.
114  * - State 2
115  * This state is only necessary for transitioning from state 0. From state
116  * 0, the goal is to arrive at state 3. Ideally, the RIGHT mgmt would
117  * be written with 0xFF000000, but the flash library only permits 16 bit
118  * writes. If a reset were to occur in the middle of this section of the
119  * algorithm, we want to ensure that the mgmt is left in a known state,
120  * state 2, so that the algorithm could continue from where it got
121  * interrupted.
122  * - State 5, 9
123  * These states indicate that the other page has just become valid because
124  * the new data has just been written. Once at these states, reading
125  * from the NVM will now pull data from the other page.
126  * - State 6, 10
127  * These states indicate that the old page is now dead and not in use.
128  * While the algorithm already knows to read from the new page, the Dead
129  * mgmt byte is primarily used to indicate that the other page needs to
130  * be erased. Conceptually, the Dead byte can also be considered a type
131  * of "garbage collection" flag indicating the old page needs to be
132  * destroyed and has not yet been erased.
133  * - State 3, 7
134  * These states are the final exit points of the circular state machine.
135  * Once at these states, the current page is marked Valid and Active and
136  * the old page is marked as Dead. The algorithm knows which page to
137  * read from and which page needs to be erased on the next write to the NVM.
138  *
139  *
140  * Notes on algorithm behavior:
141  * - Refer to nvm-def.h for a list of offset/length that define the data
142  * stored in NVM storage space.
143  * - All writes to flash are 16bit granularity and therefore the internal
144  * flash writes cast the data to uint16_t. Length is also required to be
145  * a multiple of 16bits.
146  * - Flash page erase uses a granularity of a single flash page. The size
147  * of a flash page depends on the chip and is defined in memmap.h with
148  * the define MFB_PAGE_SIZE_B.
149  * - Erasing will only occur when halCommonSaveToNvm() is called.
150  * - Erasing will always occur when halCommonSaveToNvm() is called unless the
151  * page intended to be erased is already entirely 0xFFFF.
152  * - When reading and management is invalid, the read will return 0xFF for data.
153  * - Calling halCommonSaveToNvm() while in any state is always valid and the
154  * new data will be written to flash.
155  * - halCommonSaveToNvm() will always advance the state machine to 3 or 7.
156  * - When writing and management is invalid, both LEFT and RIGHT will be erased
157  * and the new data will be written to LEFT.
158  * - Writing causes the new data being passed into halCommonSaveToNvm() to be
159  * written to flash. The data already existing in the currently valid page
160  * will be copied over to the new page.
161  * - Reading or writing to an offset equal to or greater than NVM_DATA_SIZE_B is
162  * illegal and will cause an assert.
163  * - Offset and length must always be multiples of 16bits. If not, both a read
164  * and a write will trigger an assert.
165  * - Offset and length must be supplied in bytes.
166  * - All data in NVM storage must exist above the mgmt bytes, denoted by
167  * NVM_MGMT_SIZE_B.
168  * - The bottom 64 bytes of NVM storage are allocated to radio calibration
169  * values. These 64 bytes *must* exist for the radio to function.
170  * - There is no error checking beyond checking for 16bit alignment. This
171  * means it is possible to use data offset and size combinations that
172  * exceed NVM storage space or overlap with other data. Be careful!
173  *@{
174  */
175 
176 
177 #ifndef NVM_H_
178 #define NVM_H_
179 
180 //Pull in the MFB_ definitions.
182 //Pull in nvm-def.h so any code including nvm.h has access to the
183 //offsets and sizes defining the NVM data.
185 //Necessary to define StStatus and codes.
186 #include "error.h"
187 
188 
189 /**
190  * @brief Copy the NVM data from flash into the provided RAM location.
191  * It is illegal for the offset to be greater than NVM_DATA_SIZE_B.
192  *
193  * @param data A (RAM) pointer to where the data should be copied.
194  *
195  * @param offset The location from which the data should be copied. Must be
196  * 16bit aligned.
197  *
198  * @param length The length of the data in bytes. Must be 16bit aligned.
199  *
200  * @return An StStatus value indicating the success of the function.
201  * - ST_SUCCESS if the read completed cleanly.
202  * - ST_ERR_FATAL if the NVM storage management indicated an invalid
203  * state. The function will return entirely 0xFF in the data parameter.
204  */
205 StStatus halCommonReadFromNvm(void *data, uint32_t offset, uint16_t length);
206 
207 /**
208  * @brief Return the address of the token in NVM
209  *
210  * @param offset The location offset from which the address should be returned
211  *
212  *
213  * @return The address requested
214  */
215 uint16_t *halCommonGetAddressFromNvm(uint32_t offset);
216 
217 /**
218  * @brief Write the NVM data from the provided location RAM into flash.
219  * It is illegal for the offset to be greater than NVM_DATA_SIZE_B.
220  *
221  * @param data A (RAM) pointer from where the data should be taken.
222  *
223  * @param offset The location to which the data should be written. Must be
224  * 16bit aligned.
225  *
226  * @param length The length of the data in bytes. Must be 16bit aligned.
227  *
228  * @return An StStatus value indicating the success of the function.
229  * - ST_SUCCESS if the write completed cleanly.
230  * - Any other status value is an error code generated by the low level
231  * flash erase and write API. Refer to flash.h for details.
232  */
233 StStatus halCommonWriteToNvm(const void *data, uint32_t offset, uint16_t length);
234 
235 /**
236  * @brief Define the number of physical flash pages that comprise a NVM page.
237  * Since NVM_DATA_SIZE_B must be a multiple of MFB_PAGE_SIZE_B, increasing the
238  * size of NVM storage should be done by modifying this define.
239  *
240  * @note The total flash area consumed by NVM storage is double this value.
241  * This is due to the fact that there are two NVM pages, LEFT and RIGHT,
242  * which the algorithm alternates between.
243  */
244 #define NVM_FLASH_PAGE_COUNT (1)
245 
246 /**
247  * @brief Define the total size of a NVM page, in bytes. This must be a
248  * multiple of the memory map define MFB_PAGE_SIZE_B. Note that 4 bytes of
249  * the total size of an NVM page are dedicated to page management.
250  *
251  * @note <b>DO NOT EDIT THIS DEFINE. Instead, edit NVM_FLASH_PAGE_COUNT.</b>
252  */
253 #define NVM_DATA_SIZE_B (MFB_PAGE_SIZE_B*NVM_FLASH_PAGE_COUNT)
254 #if ((NVM_DATA_SIZE_B%MFB_PAGE_SIZE_B) != 0)
255  #error Illegal NVM data storage size. NVM_DATA_SIZE_B must be a multiple of MFB_PAGE_SIZE_B.
256 #endif
257 
258 /**
259  * @brief Define the absolute address of the LEFT page. LEFT page storage
260  * is defined by nvmStorageLeft[NVM_DATA_SIZE_B] and placed by the linker
261  * using the segment "NVM".
262  */
263 #define NVM_LEFT_PAGE ((uint32_t)nvmStorageLeft)
264 
265 /**
266  * @brief Define the absolute address of the RIGHT page. RIGHT page storage
267  * is defined by nvmStorageRight[NVM_DATA_SIZE_B] and placed by the linker
268  * using the segment "NVM".
269  */
270 #define NVM_RIGHT_PAGE ((uint32_t)nvmStorageRight)
271 
272 /**
273  * @brief Define the number of bytes that comprise the NVM management bytes.
274  * All data must begin at an offset above the management bytes.
275  *
276  * @note This value <b>must not change</b>.
277  */
278 #define NVM_MGMT_SIZE_B (4)
279 
280 /** @} END addtogroup */
281 /** @} */
282 
283 #endif // NVM_H_
284 
uint16_t * halCommonGetAddressFromNvm(uint32_t offset)
Return the address of the token in NVM.
Definition: nvm.c:158
uint8_t halCommonWriteToNvm(const void *data, uint32_t offset, uint16_t length)
Write the NVM data from the provided location RAM into flash.
Definition: nvm.c:280
uint8_t halCommonReadFromNvm(void *data, uint32_t offset, uint16_t length)
Copy the NVM data from flash into the provided RAM location.
Definition: nvm.c:98
Data definitions for the Cortex-M3 Non-Volatile Memory data storage system.
STM32W108 series memory map definitions used by the full hal.