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