OSAL_Nv.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /**************************************************************************************************
  2. Filename: OSAL_Nv.c
  3. Revised: $Date: 2008-08-06 17:17:11 -0700 (Wed, 06 Aug 2008) $
  4. Revision: $Revision: 17715 $
  5. Description: This module contains the OSAL non-volatile memory functions.
  6. Copyright 2006-2009 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /**************************************************************************************************
  34. Notes:
  35. - A trick buried deep in initPage() requires that the MSB of the NV Item Id is to
  36. be reserved for use by this module.
  37. **************************************************************************************************/
  38. /*********************************************************************
  39. * INCLUDES
  40. */
  41. #include "hal_adc.h"
  42. #include "hal_flash.h"
  43. #include "hal_types.h"
  44. #include "ZComdef.h"
  45. #include "OSAL_Nv.h"
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. #define OSAL_NV_PAGE_SIZE HAL_FLASH_PAGE_SIZE
  50. #define OSAL_NV_PAGES_USED HAL_NV_PAGE_CNT
  51. #define OSAL_NV_PAGE_BEG HAL_NV_PAGE_BEG
  52. #define OSAL_NV_PAGE_END (OSAL_NV_PAGE_BEG + OSAL_NV_PAGES_USED - 1)
  53. #define OSAL_NV_ACTIVE 0x00
  54. #define OSAL_NV_ERASED 0xFF
  55. #define OSAL_NV_ERASED_ID 0xFFFF
  56. #define OSAL_NV_ZEROED_ID 0x0000
  57. // Reserve MSB of Id to signal a search for the "old" source copy (new write interrupted/failed.)
  58. #define OSAL_NV_SOURCE_ID 0x8000
  59. #define OSAL_NV_PAGE_FREE HAL_FLASH_PAGE_SIZE
  60. // In case pages 0-1 are ever used, define a null page value.
  61. #define OSAL_NV_PAGE_NULL 0
  62. // In case item Id 0 is ever used, define a null item value.
  63. #define OSAL_NV_ITEM_NULL 0
  64. #define OSAL_NV_WORD_SIZE HAL_FLASH_WORD_SIZE
  65. #define OSAL_NV_PAGE_HDR_OFFSET 0
  66. /*********************************************************************
  67. * MACROS
  68. */
  69. #define OSAL_NV_CHECK_BUS_VOLTAGE (HalAdcCheckVdd( HAL_ADC_VDD_LIMIT_4 ))
  70. #define OSAL_NV_DATA_SIZE( LEN ) \
  71. ((((LEN) + OSAL_NV_WORD_SIZE - 1) / OSAL_NV_WORD_SIZE) * OSAL_NV_WORD_SIZE)
  72. #define OSAL_NV_ITEM_SIZE( LEN ) \
  73. (((((LEN) + OSAL_NV_WORD_SIZE - 1) / OSAL_NV_WORD_SIZE) * OSAL_NV_WORD_SIZE) + OSAL_NV_HDR_SIZE)
  74. /*********************************************************************
  75. * TYPEDEFS
  76. */
  77. typedef struct
  78. {
  79. uint16 id;
  80. uint16 len; // Enforce Flash-WORD size on len.
  81. uint16 chk; // Byte-wise checksum of the 'len' data bytes of the item.
  82. uint16 stat; // Item status.
  83. } osalNvHdr_t;
  84. // Struct member offsets.
  85. #define OSAL_NV_HDR_ID 0
  86. #define OSAL_NV_HDR_LEN 2
  87. #define OSAL_NV_HDR_CHK 4
  88. #define OSAL_NV_HDR_STAT 6
  89. #define OSAL_NV_HDR_ITEM 2 // Length of any item of a header struct.
  90. #define OSAL_NV_HDR_SIZE 8
  91. #define OSAL_NV_HDR_HALF (OSAL_NV_HDR_SIZE / 2)
  92. typedef struct
  93. {
  94. uint16 active;
  95. uint16 inUse;
  96. uint16 xfer;
  97. uint16 spare;
  98. } osalNvPgHdr_t;
  99. // Struct member offsets.
  100. #define OSAL_NV_PG_ACTIVE 0
  101. #define OSAL_NV_PG_INUSE 2
  102. #define OSAL_NV_PG_XFER 4
  103. #define OSAL_NV_PG_SPARE 6
  104. #define OSAL_NV_PAGE_HDR_SIZE 8
  105. #define OSAL_NV_PAGE_HDR_HALF (OSAL_NV_PAGE_HDR_SIZE / 2)
  106. typedef enum
  107. {
  108. eNvXfer,
  109. eNvZero
  110. } eNvHdrEnum;
  111. typedef enum
  112. {
  113. ePgActive,
  114. ePgInUse,
  115. ePgXfer,
  116. ePgSpare
  117. } ePgHdrEnum;
  118. /*********************************************************************
  119. * EXTERNAL FUNCTIONS
  120. */
  121. extern bool HalAdcCheckVdd(uint8 limit);
  122. /*********************************************************************
  123. * GLOBAL VARIABLES
  124. */
  125. #ifndef OAD_KEEP_NV_PAGES
  126. // When NV pages are to remain intact during OAD download,
  127. // the image itself should not include NV pages.
  128. #pragma location="ZIGNV_ADDRESS_SPACE"
  129. __no_init uint8 _nvBuf[OSAL_NV_PAGES_USED * OSAL_NV_PAGE_SIZE];
  130. #pragma required=_nvBuf
  131. #pragma location="IEEE_ADDRESS_SPACE"
  132. __no_init uint8 _nvIEEE[Z_EXTADDR_LEN];
  133. #pragma required=_nvIEEE
  134. #endif // OAD_KEEP_NV_PAGES
  135. /*********************************************************************
  136. * LOCAL VARIABLES
  137. */
  138. // Offset into the page of the first available erased space.
  139. static uint16 pgOff[OSAL_NV_PAGES_USED];
  140. // Count of the bytes lost for the zeroed-out items.
  141. static uint16 pgLost[OSAL_NV_PAGES_USED];
  142. static uint8 pgRes; // Page reserved for item compacting transfer.
  143. // Saving ~100 code bytes to move a uint8* parameter/return value from findItem() to a global.
  144. static uint8 findPg;
  145. /* Immediately before the voltage critical operations of a page erase or
  146. * a word write, check bus voltage. If less than min, set global flag & abort.
  147. * Since this is to be done at the lowest level, many void functions would have to be changed to
  148. * return a value and code added to check that value before proceeding, resulting in a very
  149. * expensive code size hit for implementing this properly. Therefore, use this global as follows:
  150. * at the start of osal_nv_item_init/osal_nv_write, set to FALSE, and at the end, before returning,
  151. * check the value. Thus, the global is an accumulator of any error that occurred in any of the
  152. * attempts to modify Flash with a low bus voltage during the complicated sequence of events that
  153. * may occur on any item init or write. This is much more expedient and code saving than adding
  154. * return values and checking return values to early out. No matter which method is used, an NV
  155. * data record may end up mangled due to the low VCC conditions. The strategy is that the headers
  156. * and checksums will detect and allow recovery from such a condition.
  157. *
  158. * One unfortunate side-effect of using the global fail flag vice adding and checking return
  159. * values, is that when setting an item Id to zero has failed due to the low VCC check,
  160. * the page lost-bytes counter is still updated. Having an artificially high lost-byte
  161. * count makes it look like there are more bytes to recover from compacting a page than there may
  162. * actually be. The easy work-around implemented is it to invoke initNV() from osal_nv_item_init or
  163. * osal_nv_write anytime that the failF gets set - this will re-walk all of the pages and set the
  164. * page offset count and page lost bytes count to their actual values.
  165. */
  166. static uint8 failF;
  167. /*********************************************************************
  168. * LOCAL FUNCTIONS
  169. */
  170. static uint8 initNV( void );
  171. static void setPageUse( uint8 pg, uint8 inUse );
  172. static uint16 initPage( uint8 pg, uint16 id, uint8 findDups );
  173. static void erasePage( uint8 pg );
  174. static void compactPage( uint8 pg );
  175. static uint16 findItem( uint16 id );
  176. static uint8 initItem( uint8 flag, uint16 id, uint16 len, void *buf );
  177. static void setItem( uint8 pg, uint16 offset, eNvHdrEnum stat );
  178. static uint16 calcChkB( uint16 len, uint8 *buf );
  179. static uint16 calcChkF( byte pg, uint16 offset, uint16 len );
  180. static void writeWord( uint8 pg, uint16 offset, uint8 *buf );
  181. static void writeWordH( uint8 pg, uint16 offset, uint8 *buf );
  182. static void writeWordM( uint8 pg, uint16 offset, uint8 *buf, uint16 cnt );
  183. static void writeBuf( uint8 pg, uint16 offset, uint16 len, uint8 *buf );
  184. static void xferBuf( uint8 srcPg, uint16 srcOff, uint8 dstPg, uint16 dstOff, uint16 len );
  185. static uint8 writeItem( uint8 pg, uint16 id, uint16 len, void *buf, uint8 flag );
  186. /*********************************************************************
  187. * @fn initNV
  188. *
  189. * @brief Initialize the NV flash pages.
  190. *
  191. * @param none
  192. *
  193. * @return TRUE
  194. */
  195. static uint8 initNV( void )
  196. {
  197. osalNvPgHdr_t pgHdr;
  198. uint8 oldPg = OSAL_NV_PAGE_NULL;
  199. uint8 newPg = OSAL_NV_PAGE_NULL;
  200. uint8 findDups = FALSE;
  201. uint8 pg;
  202. pgRes = OSAL_NV_PAGE_NULL;
  203. for ( pg = OSAL_NV_PAGE_BEG; pg <= OSAL_NV_PAGE_END; pg++ )
  204. {
  205. HalFlashRead(pg, OSAL_NV_PAGE_HDR_OFFSET, (uint8 *)(&pgHdr), OSAL_NV_HDR_SIZE);
  206. if ( pgHdr.active == OSAL_NV_ERASED_ID )
  207. {
  208. if ( pgRes == OSAL_NV_PAGE_NULL )
  209. {
  210. pgRes = pg;
  211. }
  212. else
  213. {
  214. setPageUse( pg, TRUE );
  215. }
  216. }
  217. else // Page is active.
  218. {
  219. // If the page is not yet in use, it is the tgt of items from an xfer.
  220. if ( pgHdr.inUse == OSAL_NV_ERASED_ID )
  221. {
  222. newPg = pg;
  223. }
  224. // An Xfer from this page was in progress.
  225. else if ( pgHdr.xfer != OSAL_NV_ERASED_ID )
  226. {
  227. oldPg = pg;
  228. }
  229. }
  230. // Calculate page offset and lost bytes - any "old" item triggers an N^2 re-scan from start.
  231. if ( initPage( pg, OSAL_NV_ITEM_NULL, findDups ) != OSAL_NV_ITEM_NULL )
  232. {
  233. findDups = TRUE;
  234. pg = OSAL_NV_PAGE_BEG-1;
  235. continue;
  236. }
  237. } // for ( pg = OSAL_NV_PAGE_BEG; pg <= OSAL_NV_PAGE_END; pg++ )
  238. /* First the old page is erased, and then the new page is put into use.
  239. * So if a transfer was in progress, the new page will always not yet be
  240. * marked as in use, since that is the last step to ending a transfer.
  241. */
  242. if ( newPg != OSAL_NV_PAGE_NULL )
  243. {
  244. /* If there is already a fallow page reserved, keep it and put the newPg in use.
  245. * An unfinished compaction will finish to the new reserve page and the old page
  246. * will be erased and reserved.
  247. */
  248. if ( pgRes != OSAL_NV_PAGE_NULL )
  249. {
  250. setPageUse( newPg, TRUE );
  251. }
  252. /* If setting old page to 'xfer' failed or board reset before it was effected, there is no way
  253. * to know which page was the 'old page' - so just reset all NV pages to start clean.
  254. */
  255. else if ( oldPg != OSAL_NV_PAGE_NULL )
  256. {
  257. pgRes = newPg;
  258. }
  259. /* If a page compaction was interrupted and the page being compacted is not
  260. * yet erased, then there may be items remaining to xfer before erasing.
  261. */
  262. if ( oldPg != OSAL_NV_PAGE_NULL )
  263. {
  264. compactPage( oldPg );
  265. }
  266. }
  267. /* If no page met the criteria to be the reserve page:
  268. * - A compactPage() failed or board reset before doing so.
  269. * - Perhaps the user changed which Flash pages are dedicated to NV and downloaded the code
  270. * without erasing Flash?
  271. */
  272. if ( pgRes == OSAL_NV_PAGE_NULL )
  273. {
  274. for ( pg = OSAL_NV_PAGE_BEG; pg <= OSAL_NV_PAGE_END; pg++ )
  275. {
  276. erasePage( pg );
  277. }
  278. initNV();
  279. }
  280. return TRUE;
  281. }
  282. /*********************************************************************
  283. * @fn setPageUse
  284. *
  285. * @brief Set page header active/inUse state according to 'inUse'.
  286. *
  287. * @param pg - Valid NV page to verify and init.
  288. * @param inUse - Boolean TRUE if inUse, FALSE if only active.
  289. *
  290. * @return none
  291. */
  292. static void setPageUse( uint8 pg, uint8 inUse )
  293. {
  294. osalNvPgHdr_t pgHdr;
  295. pgHdr.active = OSAL_NV_ZEROED_ID;
  296. if ( inUse )
  297. {
  298. pgHdr.inUse = OSAL_NV_ZEROED_ID;
  299. }
  300. else
  301. {
  302. pgHdr.inUse = OSAL_NV_ERASED_ID;
  303. }
  304. writeWord( pg, OSAL_NV_PAGE_HDR_OFFSET, (uint8*)(&pgHdr) );
  305. }
  306. /*********************************************************************
  307. * @fn initPage
  308. *
  309. * @brief Walk the page items; calculate checksums, lost bytes & page offset.
  310. *
  311. * @param pg - Valid NV page to verify and init.
  312. * @param id - Valid NV item Id to use function as a "findItem".
  313. * If set to NULL then just perform the page initialization.
  314. *
  315. * @return If 'id' is non-NULL and good checksums are found, return the offset
  316. * of the data corresponding to item Id; else OSAL_NV_ITEM_NULL.
  317. */
  318. static uint16 initPage( uint8 pg, uint16 id, uint8 findDups )
  319. {
  320. uint16 offset = OSAL_NV_PAGE_HDR_SIZE;
  321. uint16 sz, lost = 0;
  322. osalNvHdr_t hdr;
  323. do
  324. {
  325. HalFlashRead(pg, offset, (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  326. if ( hdr.id == OSAL_NV_ERASED_ID )
  327. {
  328. break;
  329. }
  330. offset += OSAL_NV_HDR_SIZE;
  331. sz = OSAL_NV_DATA_SIZE( hdr.len );
  332. // A bad 'len' write has blown away the rest of the page.
  333. if ( (offset + sz) > OSAL_NV_PAGE_FREE )
  334. {
  335. lost += (OSAL_NV_PAGE_FREE - offset + OSAL_NV_HDR_SIZE);
  336. offset = OSAL_NV_PAGE_FREE;
  337. break;
  338. }
  339. if ( hdr.id != OSAL_NV_ZEROED_ID )
  340. {
  341. /* This trick allows function to do double duty for findItem() without
  342. * compromising its essential functionality at powerup initialization.
  343. */
  344. if ( id != OSAL_NV_ITEM_NULL )
  345. {
  346. /* This trick allows asking to find the old/transferred item in case
  347. * of a successful new item write that gets interrupted before the
  348. * old item can be zeroed out.
  349. */
  350. if ( (id & 0x7fff) == hdr.id )
  351. {
  352. if ( (((id & OSAL_NV_SOURCE_ID) == 0) && (hdr.stat == OSAL_NV_ERASED_ID)) ||
  353. (((id & OSAL_NV_SOURCE_ID) != 0) && (hdr.stat != OSAL_NV_ERASED_ID)) )
  354. {
  355. return offset;
  356. }
  357. }
  358. }
  359. // When invoked from the osal_nv_init(), verify checksums and find & zero any duplicates.
  360. else
  361. {
  362. if ( hdr.chk == calcChkF( pg, offset, hdr.len ) )
  363. {
  364. if ( findDups )
  365. {
  366. if ( hdr.stat == OSAL_NV_ERASED_ID )
  367. {
  368. /* The trick of setting the MSB of the item Id causes the logic
  369. * immediately above to return a valid page only if the header 'stat'
  370. * indicates that it was the older item being transferred.
  371. */
  372. uint16 off = findItem( (hdr.id | OSAL_NV_SOURCE_ID) );
  373. if ( off != OSAL_NV_ITEM_NULL )
  374. {
  375. setItem( findPg, off, eNvZero ); // Mark old duplicate as invalid.
  376. }
  377. }
  378. }
  379. // Any "old" item immediately exits and triggers the N^2 exhaustive initialization.
  380. else if ( hdr.stat != OSAL_NV_ERASED_ID )
  381. {
  382. return OSAL_NV_ERASED_ID;
  383. }
  384. }
  385. else
  386. {
  387. setItem( pg, offset, eNvZero ); // Mark bad checksum as invalid.
  388. lost += (OSAL_NV_HDR_SIZE + sz);
  389. }
  390. }
  391. }
  392. else
  393. {
  394. lost += (OSAL_NV_HDR_SIZE + sz);
  395. }
  396. offset += sz;
  397. } while ( TRUE );
  398. pgOff[pg - OSAL_NV_PAGE_BEG] = offset;
  399. pgLost[pg - OSAL_NV_PAGE_BEG] = lost;
  400. return OSAL_NV_ITEM_NULL;
  401. }
  402. /*********************************************************************
  403. * @fn erasePage
  404. *
  405. * @brief Erases a page in Flash.
  406. *
  407. * @param pg - Valid NV page to erase.
  408. *
  409. * @return none
  410. */
  411. static void erasePage( uint8 pg )
  412. {
  413. if ( !OSAL_NV_CHECK_BUS_VOLTAGE )
  414. {
  415. failF = TRUE;
  416. return;
  417. }
  418. HalFlashErase(pg);
  419. pgOff[pg - OSAL_NV_PAGE_BEG] = OSAL_NV_PAGE_HDR_SIZE;
  420. pgLost[pg - OSAL_NV_PAGE_BEG] = 0;
  421. }
  422. /*********************************************************************
  423. * @fn compactPage
  424. *
  425. * @brief Compacts the page specified.
  426. *
  427. * @param srcPg - Valid NV page to erase.
  428. *
  429. * @return none
  430. */
  431. static void compactPage( uint8 srcPg )
  432. {
  433. uint16 dstOff = pgOff[pgRes-OSAL_NV_PAGE_BEG];
  434. uint16 srcOff = OSAL_NV_ZEROED_ID;
  435. osalNvHdr_t hdr;
  436. // Mark page as being in process of compaction.
  437. writeWordH( srcPg, OSAL_NV_PG_XFER, (uint8*)(&srcOff) );
  438. srcOff = OSAL_NV_PAGE_HDR_SIZE;
  439. do
  440. {
  441. uint16 sz;
  442. HalFlashRead(srcPg, srcOff, (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  443. if ( hdr.id == OSAL_NV_ERASED_ID )
  444. {
  445. break;
  446. }
  447. srcOff += OSAL_NV_HDR_SIZE;
  448. if ( (srcOff + hdr.len) > OSAL_NV_PAGE_FREE )
  449. {
  450. break;
  451. }
  452. sz = OSAL_NV_DATA_SIZE( hdr.len );
  453. if ( hdr.id != OSAL_NV_ZEROED_ID )
  454. {
  455. if ( hdr.chk == calcChkF( srcPg, srcOff, hdr.len ) )
  456. {
  457. setItem( srcPg, srcOff, eNvXfer );
  458. writeBuf( pgRes, dstOff, OSAL_NV_HDR_SIZE, (byte *)(&hdr) );
  459. dstOff += OSAL_NV_HDR_SIZE;
  460. xferBuf( srcPg, srcOff, pgRes, dstOff, sz );
  461. dstOff += sz;
  462. }
  463. setItem( srcPg, srcOff, eNvZero ); // Mark old location as invalid.
  464. }
  465. srcOff += sz;
  466. } while ( TRUE );
  467. pgOff[pgRes-OSAL_NV_PAGE_BEG] = dstOff;
  468. /* In order to recover from a page compaction that is interrupted,
  469. * the logic in osal_nv_init() depends upon the following order:
  470. * 1. Compacted page is erased.
  471. * 2. State of the target of compaction is changed ePgActive to ePgInUse.
  472. */
  473. erasePage( srcPg );
  474. // Mark the reserve page as being in use.
  475. setPageUse( pgRes, TRUE );
  476. // Set the reserve page to be the newly erased page.
  477. pgRes = srcPg;
  478. }
  479. /*********************************************************************
  480. * @fn findItem
  481. *
  482. * @brief Find an item Id in NV and return the page and offset to its data.
  483. *
  484. * @param id - Valid NV item Id.
  485. *
  486. * @return Offset of data corresponding to item Id, if found;
  487. * otherwise OSAL_NV_ITEM_NULL.
  488. *
  489. * The page containing the item, if found;
  490. * otherwise no valid assignment made - left equal to item Id.
  491. *
  492. */
  493. static uint16 findItem( uint16 id )
  494. {
  495. uint16 off;
  496. uint8 pg;
  497. for ( pg = OSAL_NV_PAGE_BEG; pg <= OSAL_NV_PAGE_END; pg++ )
  498. {
  499. if ( (off = initPage( pg, id, FALSE )) != OSAL_NV_ITEM_NULL )
  500. {
  501. findPg = pg;
  502. return off;
  503. }
  504. }
  505. // Now attempt to find the item as the "old" item of a failed/interrupted NV write.
  506. if ( (id & OSAL_NV_SOURCE_ID) == 0 )
  507. {
  508. return findItem( id | OSAL_NV_SOURCE_ID );
  509. }
  510. else
  511. {
  512. findPg = OSAL_NV_PAGE_NULL;
  513. return OSAL_NV_ITEM_NULL;
  514. }
  515. }
  516. /*********************************************************************
  517. * @fn initItem
  518. *
  519. * @brief An NV item is created and initialized with the data passed to the function, if any.
  520. *
  521. * @param id - Valid NV item Id.
  522. * @param len - Item data length.
  523. * @param *buf - Pointer to item initalization data. Set to NULL if none.
  524. *
  525. * @return TRUE if item write and read back checksums ok; FALSE otherwise.
  526. */
  527. static uint8 initItem( uint8 flag, uint16 id, uint16 len, void *buf )
  528. {
  529. uint16 sz = OSAL_NV_ITEM_SIZE( len );
  530. uint8 rtrn = FALSE;
  531. uint8 cnt = OSAL_NV_PAGES_USED;
  532. uint8 pg = pgRes+1; // Set to 1 after the reserve page to even wear across all available pages.
  533. uint8 idx;
  534. do {
  535. if (pg >= OSAL_NV_PAGE_BEG+OSAL_NV_PAGES_USED)
  536. {
  537. pg = OSAL_NV_PAGE_BEG;
  538. }
  539. if ( pg != pgRes )
  540. {
  541. idx = pg - OSAL_NV_PAGE_BEG;
  542. if ( (pgOff[idx] - pgLost[idx] + sz) <= OSAL_NV_PAGE_FREE )
  543. {
  544. break;
  545. }
  546. }
  547. pg++;
  548. } while (--cnt);
  549. if (cnt)
  550. {
  551. // Item fits if an old page is compacted.
  552. if ( (pgOff[idx] + sz) > OSAL_NV_PAGE_FREE )
  553. {
  554. pg = pgRes;
  555. }
  556. // New item is the first one written to the reserved page, then the old page is compacted.
  557. rtrn = writeItem( pg, id, len, buf, flag );
  558. if ( pg == pgRes )
  559. {
  560. if ( flag )
  561. {
  562. compactPage( OSAL_NV_PAGE_BEG+idx );
  563. }
  564. else
  565. {
  566. *(uint8 *)buf = OSAL_NV_PAGE_BEG+idx;
  567. }
  568. }
  569. }
  570. return rtrn;
  571. }
  572. /*********************************************************************
  573. * @fn setItem
  574. *
  575. * @brief Set an item Id or status to mark its state.
  576. *
  577. * @param pg - Valid NV page.
  578. * @param offset - Valid offset into the page of the item data - the header
  579. * offset is calculated from this.
  580. * @param stat - Valid enum value for the item status.
  581. *
  582. * @return none
  583. */
  584. static void setItem( uint8 pg, uint16 offset, eNvHdrEnum stat )
  585. {
  586. osalNvHdr_t hdr;
  587. offset -= OSAL_NV_HDR_SIZE;
  588. HalFlashRead(pg, offset, (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  589. if ( stat == eNvXfer )
  590. {
  591. hdr.stat = OSAL_NV_ACTIVE;
  592. writeWord( pg, offset+OSAL_NV_HDR_CHK, (uint8*)(&(hdr.chk)) );
  593. }
  594. else // if ( stat == eNvZero )
  595. {
  596. uint16 sz = ((hdr.len + (OSAL_NV_WORD_SIZE-1)) / OSAL_NV_WORD_SIZE) * OSAL_NV_WORD_SIZE +
  597. OSAL_NV_HDR_SIZE;
  598. hdr.id = 0;
  599. writeWord( pg, offset, (uint8 *)(&hdr) );
  600. pgLost[pg-OSAL_NV_PAGE_BEG] += sz;
  601. }
  602. }
  603. /*********************************************************************
  604. * @fn calcChkB
  605. *
  606. * @brief Calculates the data checksum over the 'buf' parameter.
  607. *
  608. * @param pg - A valid NV Flash page.
  609. * @param offset - A valid offset into the page.
  610. * @param len - Byte count of the data to be checksummed.
  611. *
  612. * @return Calculated checksum of the data bytes.
  613. */
  614. static uint16 calcChkB( uint16 len, uint8 *buf )
  615. {
  616. uint8 fill = len % OSAL_NV_WORD_SIZE;
  617. uint16 chk;
  618. if ( !buf )
  619. {
  620. chk = len * OSAL_NV_ERASED;
  621. }
  622. else
  623. {
  624. chk = 0;
  625. while ( len-- )
  626. {
  627. chk += *buf++;
  628. }
  629. }
  630. // calcChkF() will calculate over OSAL_NV_WORD_SIZE alignment.
  631. if ( fill )
  632. {
  633. chk += (OSAL_NV_WORD_SIZE - fill) * OSAL_NV_ERASED;
  634. }
  635. return chk;
  636. }
  637. /*********************************************************************
  638. * @fn calcChkF
  639. *
  640. * @brief Calculates the data checksum by reading the data bytes from NV.
  641. *
  642. * @param pg - A valid NV Flash page.
  643. * @param offset - A valid offset into the page.
  644. * @param len - Byte count of the data to be checksummed.
  645. *
  646. * @return Calculated checksum of the data bytes.
  647. */
  648. static uint16 calcChkF( byte pg, uint16 offset, uint16 len )
  649. {
  650. uint16 chk = 0;
  651. len = (len + (OSAL_NV_WORD_SIZE-1)) / OSAL_NV_WORD_SIZE;
  652. while ( len-- )
  653. {
  654. uint8 cnt, tmp[OSAL_NV_WORD_SIZE];
  655. HalFlashRead(pg, offset, tmp, OSAL_NV_WORD_SIZE);
  656. offset += OSAL_NV_WORD_SIZE;
  657. for ( cnt = 0; cnt < OSAL_NV_WORD_SIZE; cnt++ )
  658. {
  659. chk += tmp[cnt];
  660. }
  661. }
  662. return chk;
  663. }
  664. /*********************************************************************
  665. * @fn writeWord
  666. *
  667. * @brief Writes a Flash-WORD to NV.
  668. *
  669. * @param pg - A valid NV Flash page.
  670. * @param offset - A valid offset into the page.
  671. * @param buf - Pointer to source buffer.
  672. *
  673. * @return none
  674. */
  675. static void writeWord( uint8 pg, uint16 offset, uint8 *buf )
  676. {
  677. offset = (offset >> 2) + ((uint16)pg << 9);
  678. if ( OSAL_NV_CHECK_BUS_VOLTAGE )
  679. {
  680. HalFlashWrite(offset, buf, 1);
  681. }
  682. else
  683. {
  684. failF = TRUE;
  685. }
  686. }
  687. /*********************************************************************
  688. * @fn writeWordM
  689. *
  690. * @brief Writes multiple Flash-WORDs to NV.
  691. *
  692. * @param pg - A valid NV Flash page.
  693. * @param offset - A valid offset into the page.
  694. * @param buf - Pointer to source buffer.
  695. * @param cnt - Number of 4-byte blocks to write.
  696. *
  697. * @return none
  698. */
  699. static void writeWordM( uint8 pg, uint16 offset, uint8 *buf, uint16 cnt )
  700. {
  701. offset = (offset >> 2) + ((uint16)pg << 9);
  702. if ( OSAL_NV_CHECK_BUS_VOLTAGE )
  703. {
  704. HalFlashWrite(offset, buf, cnt);
  705. }
  706. else
  707. {
  708. failF = TRUE;
  709. }
  710. }
  711. /*********************************************************************
  712. * @fn writeWordH
  713. *
  714. * @brief Writes the 1st half of a Flash-WORD to NV (filling 2nd half with 0xffff).
  715. *
  716. * @param pg - A valid NV Flash page.
  717. * @param offset - A valid offset into the page.
  718. * @param buf - Pointer to source buffer.
  719. *
  720. * @return none
  721. */
  722. static void writeWordH( uint8 pg, uint16 offset, uint8 *buf )
  723. {
  724. uint8 tmp[4];
  725. tmp[0] = buf[0];
  726. tmp[1] = buf[1];
  727. tmp[2] = OSAL_NV_ERASED;
  728. tmp[3] = OSAL_NV_ERASED;
  729. writeWord( pg, offset, tmp );
  730. }
  731. /*********************************************************************
  732. * @fn writeBuf
  733. *
  734. * @brief Writes a data buffer to NV.
  735. *
  736. * @param dstPg - A valid NV Flash page.
  737. * @param offset - A valid offset into the page.
  738. * @param len - Byte count of the data to write.
  739. * @param buf - The data to write.
  740. *
  741. * @return TRUE if data buf checksum matches read back checksum, else FALSE.
  742. */
  743. static void writeBuf( uint8 dstPg, uint16 dstOff, uint16 len, uint8 *buf )
  744. {
  745. uint8 rem = dstOff % OSAL_NV_WORD_SIZE;
  746. uint8 tmp[OSAL_NV_WORD_SIZE];
  747. if ( rem )
  748. {
  749. dstOff = (dstOff / OSAL_NV_WORD_SIZE) * OSAL_NV_WORD_SIZE;
  750. HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE);
  751. while ( (rem < OSAL_NV_WORD_SIZE) && len )
  752. {
  753. tmp[rem++] = *buf++;
  754. len--;
  755. }
  756. writeWord( dstPg, dstOff, tmp );
  757. dstOff += OSAL_NV_WORD_SIZE;
  758. }
  759. rem = len % OSAL_NV_WORD_SIZE;
  760. len /= OSAL_NV_WORD_SIZE;
  761. if ( len )
  762. {
  763. writeWordM( dstPg, dstOff, buf, len );
  764. dstOff += OSAL_NV_WORD_SIZE * len;
  765. buf += OSAL_NV_WORD_SIZE * len;
  766. }
  767. if ( rem )
  768. {
  769. uint8 idx = 0;
  770. HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE);
  771. while ( rem-- )
  772. {
  773. tmp[idx++] = *buf++;
  774. }
  775. writeWord( dstPg, dstOff, tmp );
  776. }
  777. }
  778. /*********************************************************************
  779. * @fn xferBuf
  780. *
  781. * @brief Xfers an NV buffer from one location to another, enforcing OSAL_NV_WORD_SIZE writes.
  782. *
  783. * @return none
  784. */
  785. static void xferBuf( uint8 srcPg, uint16 srcOff, uint8 dstPg, uint16 dstOff, uint16 len )
  786. {
  787. uint8 rem = dstOff % OSAL_NV_WORD_SIZE;
  788. uint8 tmp[OSAL_NV_WORD_SIZE];
  789. if ( rem )
  790. {
  791. dstOff -= rem;
  792. HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE);
  793. while ( (rem < OSAL_NV_WORD_SIZE) && len )
  794. {
  795. HalFlashRead(srcPg, srcOff, tmp+rem, 1);
  796. srcOff++;
  797. rem++;
  798. len--;
  799. }
  800. writeWord( dstPg, dstOff, tmp );
  801. dstOff += OSAL_NV_WORD_SIZE;
  802. }
  803. rem = len % OSAL_NV_WORD_SIZE;
  804. len /= OSAL_NV_WORD_SIZE;
  805. while ( len-- )
  806. {
  807. HalFlashRead(srcPg, srcOff, tmp, OSAL_NV_WORD_SIZE);
  808. srcOff += OSAL_NV_WORD_SIZE;
  809. writeWord( dstPg, dstOff, tmp );
  810. dstOff += OSAL_NV_WORD_SIZE;
  811. }
  812. if ( rem )
  813. {
  814. uint8 idx = 0;
  815. HalFlashRead(dstPg, dstOff, tmp, OSAL_NV_WORD_SIZE);
  816. while ( rem-- )
  817. {
  818. HalFlashRead(srcPg, srcOff, tmp+idx, 1);
  819. srcOff++;
  820. idx++;
  821. }
  822. writeWord( dstPg, dstOff, tmp );
  823. }
  824. }
  825. /*********************************************************************
  826. * @fn writeItem
  827. *
  828. * @brief Writes an item header/data combo to the specified NV page.
  829. *
  830. * @param pg - Valid NV Flash page.
  831. * @param id - Valid NV item Id.
  832. * @param len - Byte count of the data to write.
  833. * @param buf - The data to write. If NULL, no data/checksum write.
  834. * @param flag - TRUE if the checksum should be written, FALSE otherwise.
  835. *
  836. * @return TRUE if header/data to write matches header/data read back, else FALSE.
  837. */
  838. static uint8 writeItem( uint8 pg, uint16 id, uint16 len, void *buf, uint8 flag )
  839. {
  840. uint16 offset = pgOff[pg-OSAL_NV_PAGE_BEG];
  841. uint8 rtrn = FALSE;
  842. osalNvHdr_t hdr;
  843. if ( pg == pgRes )
  844. {
  845. /* Mark reserve page as being active, in process of receiving items.
  846. * Invoking function must effect a page compaction.
  847. */
  848. setPageUse( pg, FALSE );
  849. }
  850. hdr.id = id;
  851. hdr.len = len;
  852. writeWord( pg, offset, (uint8 *)&hdr );
  853. HalFlashRead(pg, offset, (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  854. if ( (hdr.id == id) && (hdr.len == len) )
  855. {
  856. if ( flag )
  857. {
  858. uint16 chk = calcChkB( len, buf );
  859. offset += OSAL_NV_HDR_SIZE;
  860. if ( buf != NULL )
  861. {
  862. writeBuf( pg, offset, len, buf );
  863. }
  864. if ( chk == calcChkF( pg, offset, len ) )
  865. {
  866. writeWordH(pg, (offset-OSAL_NV_WORD_SIZE), (uint8 *)&chk);
  867. HalFlashRead(pg, (offset-OSAL_NV_HDR_SIZE), (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  868. if ( chk == hdr.chk )
  869. {
  870. rtrn = pg;
  871. }
  872. }
  873. }
  874. else
  875. {
  876. rtrn = pg;
  877. }
  878. }
  879. len = OSAL_NV_ITEM_SIZE( hdr.len );
  880. pgOff[pg-OSAL_NV_PAGE_BEG] += len;
  881. return rtrn;
  882. }
  883. /*********************************************************************
  884. * @fn osal_nv_init
  885. *
  886. * @brief Initialize NV service.
  887. *
  888. * @param p - Not used.
  889. *
  890. * @return none
  891. */
  892. void osal_nv_init( void *p )
  893. {
  894. (void)p; // Suppress Lint warning.
  895. (void)initNV(); // Always returns TRUE after pages have been erased.
  896. }
  897. /*********************************************************************
  898. * @fn osal_nv_item_init
  899. *
  900. * @brief If the NV item does not already exist, it is created and
  901. * initialized with the data passed to the function, if any.
  902. * This function must be called before calling osal_nv_read() or
  903. * osal_nv_write().
  904. *
  905. * @param id - Valid NV item Id.
  906. * @param len - Item length.
  907. * @param *buf - Pointer to item initalization data. Set to NULL if none.
  908. *
  909. * @return NV_ITEM_UNINIT - Id did not exist and was created successfully.
  910. * ZSUCCESS - Id already existed, no action taken.
  911. * NV_OPER_FAILED - Failure to find or create Id.
  912. */
  913. uint8 osal_nv_item_init( uint16 id, uint16 len, void *buf )
  914. {
  915. /* Global fail flag for fail due to low bus voltage has less impact on code
  916. * size than passing back a return value all the way from the lowest level.
  917. */
  918. failF = FALSE;
  919. if ( (findItem( id ) != OSAL_NV_ITEM_NULL) )
  920. {
  921. return ZSUCCESS;
  922. }
  923. else if ( initItem( TRUE, id, len, buf ) )
  924. {
  925. if ( failF )
  926. {
  927. (void)initNV(); // See comment at the declaration of failF.
  928. return NV_OPER_FAILED;
  929. }
  930. else
  931. {
  932. return NV_ITEM_UNINIT;
  933. }
  934. }
  935. else
  936. {
  937. return NV_OPER_FAILED;
  938. }
  939. }
  940. /*********************************************************************
  941. * @fn osal_nv_item_len
  942. *
  943. * @brief Get the data length of the item stored in NV memory.
  944. *
  945. * @param id - Valid NV item Id.
  946. *
  947. * @return Item length, if found; zero otherwise.
  948. */
  949. uint16 osal_nv_item_len( uint16 id )
  950. {
  951. uint16 offset = findItem( id );
  952. if ( offset == OSAL_NV_ITEM_NULL )
  953. {
  954. return 0;
  955. }
  956. else
  957. {
  958. osalNvHdr_t hdr;
  959. HalFlashRead(findPg, (offset - OSAL_NV_HDR_SIZE), (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  960. return hdr.len;
  961. }
  962. }
  963. /*********************************************************************
  964. * @fn osal_nv_write
  965. *
  966. * @brief Write a data item to NV. Function can write an entire item to NV or
  967. * an element of an item by indexing into the item with an offset.
  968. *
  969. * @param id - Valid NV item Id.
  970. * @param ndx - Index offset into item
  971. * @param len - Length of data to write.
  972. * @param *buf - Data to write.
  973. *
  974. * @return ZSUCCESS if successful, NV_ITEM_UNINIT if item did not
  975. * exist in NV and offset is non-zero, NV_OPER_FAILED if failure.
  976. */
  977. uint8 osal_nv_write( uint16 id, uint16 ndx, uint16 len, void *buf )
  978. {
  979. uint8 rtrn = ZSUCCESS;
  980. /* Global fail flag for fail due to low bus voltage has less impact on code
  981. * size than passing back a return value all the way from the lowest level.
  982. */
  983. failF = FALSE;
  984. if ( len != 0 )
  985. {
  986. osalNvHdr_t hdr;
  987. uint16 origOff, srcOff;
  988. uint16 cnt;
  989. uint8 *ptr;
  990. origOff = srcOff = findItem( id );
  991. if ( srcOff == OSAL_NV_ITEM_NULL )
  992. {
  993. return NV_ITEM_UNINIT;
  994. }
  995. HalFlashRead(findPg, (srcOff - OSAL_NV_HDR_SIZE), (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  996. if ( hdr.len < (ndx + len) )
  997. {
  998. return NV_OPER_FAILED;
  999. }
  1000. srcOff += ndx;
  1001. ptr = buf;
  1002. cnt = len;
  1003. while ( cnt )
  1004. {
  1005. uint8 tmp;
  1006. HalFlashRead(findPg, srcOff++, &tmp, 1);
  1007. if ( tmp != *ptr++ )
  1008. {
  1009. break;
  1010. }
  1011. cnt--;
  1012. }
  1013. if ( cnt != 0 ) // If the buffer to write is different in one or more bytes.
  1014. {
  1015. uint8 comPg, dstPg = initItem( FALSE, id, hdr.len, &comPg );
  1016. if ( dstPg != OSAL_NV_PAGE_NULL )
  1017. {
  1018. uint16 tmp = OSAL_NV_DATA_SIZE( hdr.len );
  1019. uint16 dstOff = pgOff[dstPg-OSAL_NV_PAGE_BEG] - tmp;
  1020. uint8 srcPg = findPg;
  1021. srcOff = origOff;
  1022. setItem( srcPg, srcOff, eNvXfer );
  1023. xferBuf( srcPg, srcOff, dstPg, dstOff, ndx );
  1024. srcOff += ndx;
  1025. dstOff += ndx;
  1026. writeBuf( dstPg, dstOff, len, buf );
  1027. srcOff += len;
  1028. dstOff += len;
  1029. xferBuf( srcPg, srcOff, dstPg, dstOff, (hdr.len-ndx-len) );
  1030. // Calculate and write the new checksum.
  1031. dstOff = pgOff[dstPg-OSAL_NV_PAGE_BEG] - tmp;
  1032. tmp = calcChkF( dstPg, dstOff, hdr.len );
  1033. dstOff -= OSAL_NV_HDR_SIZE;
  1034. writeWordH( dstPg, (dstOff+OSAL_NV_HDR_CHK), (uint8 *)&tmp );
  1035. HalFlashRead(dstPg, dstOff, (uint8 *)(&hdr), OSAL_NV_HDR_SIZE);
  1036. if ( tmp == hdr.chk )
  1037. {
  1038. setItem( srcPg, origOff, eNvZero );
  1039. }
  1040. else
  1041. {
  1042. rtrn = NV_OPER_FAILED;
  1043. }
  1044. if ( dstPg == pgRes )
  1045. {
  1046. compactPage( comPg );
  1047. }
  1048. }
  1049. else
  1050. {
  1051. rtrn = NV_OPER_FAILED;
  1052. }
  1053. }
  1054. }
  1055. if ( failF )
  1056. {
  1057. (void)initNV(); // See comment at the declaration of failF.
  1058. rtrn = NV_OPER_FAILED;
  1059. }
  1060. return rtrn;
  1061. }
  1062. /*********************************************************************
  1063. * @fn osal_nv_read
  1064. *
  1065. * @brief Read data from NV. This function can be used to read an entire item from NV or
  1066. * an element of an item by indexing into the item with an offset.
  1067. * Read data is copied into *buf.
  1068. *
  1069. * @param id - Valid NV item Id.
  1070. * @param ndx - Index offset into item
  1071. * @param len - Length of data to read.
  1072. * @param *buf - Data is read into this buffer.
  1073. *
  1074. * @return ZSUCCESS if NV data was copied to the parameter 'buf'.
  1075. * Otherwise, NV_OPER_FAILED for failure.
  1076. */
  1077. uint8 osal_nv_read( uint16 id, uint16 ndx, uint16 len, void *buf )
  1078. {
  1079. uint16 offset;
  1080. offset = findItem( id );
  1081. if ( offset == OSAL_NV_ITEM_NULL )
  1082. {
  1083. return NV_OPER_FAILED;
  1084. }
  1085. HalFlashRead(findPg, offset+ndx, buf, len);
  1086. return ZSUCCESS;
  1087. }
  1088. /*********************************************************************
  1089. *********************************************************************/