nvm.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Non-Volatile Memory driver
  5. *
  6. * Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef NVM_H_INCLUDED
  47. #define NVM_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_nvm_group SAM Non-Volatile Memory (NVM) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides
  52. * an interface for the configuration and management of non-volatile memories
  53. * within the device, for partitioning, erasing, reading, and writing of data.
  54. *
  55. * The following peripheral is used by this module:
  56. * - NVM (Non-Volatile Memory)
  57. *
  58. * The following devices can use this module:
  59. * - Atmel | SMART SAM D20/D21
  60. * - Atmel | SMART SAM R21
  61. * - Atmel | SMART SAM D09/D10/D11
  62. * - Atmel | SMART SAM L21/L22
  63. * - Atmel | SMART SAM DA1
  64. * - Atmel | SMART SAM C20/C21
  65. * - Atmel | SMART SAM HA1
  66. * - Atmel | SMART SAM R30
  67. *
  68. * The outline of this documentation is as follows:
  69. * - \ref asfdoc_sam0_nvm_prerequisites
  70. * - \ref asfdoc_sam0_nvm_module_overview
  71. * - \ref asfdoc_sam0_nvm_special_considerations
  72. * - \ref asfdoc_sam0_nvm_extra_info
  73. * - \ref asfdoc_sam0_nvm_examples
  74. * - \ref asfdoc_sam0_nvm_api_overview
  75. *
  76. *
  77. * \section asfdoc_sam0_nvm_prerequisites Prerequisites
  78. *
  79. * There are no prerequisites for this module.
  80. *
  81. *
  82. * \section asfdoc_sam0_nvm_module_overview Module Overview
  83. *
  84. * The Non-Volatile Memory (NVM) module provides an interface to the device's
  85. * Non-Volatile Memory controller, so that memory pages can be written, read,
  86. * erased, and reconfigured in a standardized manner.
  87. *
  88. * \subsection asfdoc_sam0_nvm_features Driver Feature Macro Definition
  89. * <table>
  90. * <tr>
  91. * <th>Driver feature macro</th>
  92. * <th>Supported devices</th>
  93. * </tr>
  94. * <tr>
  95. * <td>FEATURE_NVM_RWWEE</td>
  96. * <td>SAM L21/L22, SAM D21-64K, SAM DA1, SAM C20/C21, SAM R30</td>
  97. * </tr>
  98. * <tr>
  99. * <td>FEATURE_BOD12</td>
  100. * <td>SAM L21, SAMR30</td>
  101. * </tr>
  102. * </table>
  103. * \note The specific features are only available in the driver when the
  104. * selected device supports those features.
  105. *
  106. * \subsection asfdoc_sam0_nvm_module_overview_regions Memory Regions
  107. * The NVM memory space of the SAM devices is divided into two sections:
  108. * a Main Array section, and an Auxiliary space section. The Main Array space
  109. * can be configured to have an (emulated) EEPROM and/or boot loader section.
  110. * The memory layout with the EEPROM and bootloader partitions is shown in
  111. * \ref asfdoc_sam0_nvm_module_mem_layout "the figure below".
  112. *
  113. * \anchor asfdoc_sam0_nvm_module_mem_layout
  114. * \dot
  115. * digraph memory_layout {
  116. * size="5,5"
  117. * node [shape=plaintext, fontname=arial]
  118. * memory [label=<
  119. * <table border="0" cellborder="1" cellspacing="0" >
  120. * <tr>
  121. * <td align="right" border="0"> End of NVM Memory </td>
  122. * <td rowspan="3" align="center"> Reserved EEPROM Section </td>
  123. * </tr>
  124. * <tr>
  125. * <td align="right" border="0"> </td>
  126. * </tr>
  127. * <tr>
  128. * <td align="right" border="0"> Start of EEPROM Memory </td>
  129. * </tr>
  130. * <tr>
  131. * <td align="right" border="0"> End of Application Memory </td>
  132. * <td rowspan="3" align="center"> Application Section </td>
  133. * </tr>
  134. * <tr>
  135. * <td height="300" align="right" border="0"> </td>
  136. * </tr>
  137. * <tr>
  138. * <td align="right" border="0"> Start of Application Memory </td>
  139. * </tr>
  140. * <tr>
  141. * <td align="right" border="0"> End of Bootloader Memory </td>
  142. * <td rowspan="3" align="center"> BOOT Section </td>
  143. * </tr>
  144. * <tr>
  145. * <td align="right" border="0"> </td>
  146. * </tr>
  147. * <tr>
  148. * <td align="right" border="0"> Start of NVM Memory</td>
  149. * </tr>
  150. * </table>
  151. * >]
  152. * }
  153. * \enddot
  154. *
  155. * The Main Array is divided into rows and pages, where each row contains four
  156. * pages. The size of each page may vary from 8-1024 bytes dependent of the
  157. * device. Device specific parameters such as the page size and total number of
  158. * pages in the NVM memory space are available via the \ref nvm_get_parameters()
  159. * function.
  160. *
  161. * An NVM page number and address can be computed via the following equations:
  162. *
  163. * \f[ PageNum = (RowNum \times 4) + PagePosInRow \f]
  164. * \f[ PageAddr = PageNum \times PageSize \f]
  165. *
  166. * \ref asfdoc_sam0_nvm_module_row_layout "The figure below" shows an example
  167. * of the memory page and address values associated with logical row 7 of the
  168. * NVM memory space.
  169. *
  170. * \anchor asfdoc_sam0_nvm_module_row_layout
  171. * \dot
  172. * digraph row_layout {
  173. * size="4,4"
  174. * node [shape=plaintext, fontname=arial]
  175. * row [label=<
  176. * <table border="0" cellborder="1" cellspacing="0">
  177. * <tr>
  178. * <td align="right" border ="0"> Row 0x07 </td>
  179. * <td > Page 0x1F </td>
  180. * <td > Page 0x1E </td>
  181. * <td > Page 0x1D </td>
  182. * <td > Page 0x1C </td>
  183. * </tr>
  184. * <tr>
  185. * <td align="right" border ="0"> Address </td>
  186. * <td border="0"> 0x7C0 </td>
  187. * <td border="0"> 0x780 </td>
  188. * <td border="0"> 0x740 </td>
  189. * <td border="0"> 0x700 </td>
  190. * </tr>
  191. * </table>
  192. * >]
  193. * }
  194. * \enddot
  195. *
  196. * \subsection asfdoc_sam0_nvm_module_overview_locking_regions Region Lock Bits
  197. * As mentioned in \ref asfdoc_sam0_nvm_module_overview_regions, the main
  198. * block of the NVM memory is divided into a number of individually addressable
  199. * pages. These pages are grouped into 16 equal sized regions, where each region
  200. * can be locked separately issuing an \ref NVM_COMMAND_LOCK_REGION command or
  201. * by writing the LOCK bits in the User Row. Rows reserved for the EEPROM
  202. * section are not affected by the lock bits or commands.
  203. *
  204. * \note By using the \ref NVM_COMMAND_LOCK_REGION or
  205. * \ref NVM_COMMAND_UNLOCK_REGION commands the settings will remain in
  206. * effect until the next device reset. By changing the default lock
  207. * setting for the regions, the auxiliary space must to be written,
  208. * however the adjusted configuration will not take effect until the next
  209. * device reset.
  210. *
  211. * \note If the \ref asfdoc_sam0_nvm_special_consideration_security_bit is
  212. * set, the auxiliary space cannot be written to. Clearing of the security
  213. * bit can only be performed by a full chip erase.
  214. *
  215. * \subsection asfdoc_sam0_nvm_module_overview_sub_rw Read/Write
  216. * Reading from the NVM memory can be performed using direct addressing into the
  217. * NVM memory space, or by calling the \ref nvm_read_buffer() function.
  218. *
  219. * Writing to the NVM memory must be performed by the \ref nvm_write_buffer()
  220. * function - additionally, a manual page program command must be issued if
  221. * the NVM controller is configured in manual page writing mode.
  222. *
  223. * Before a page can be updated, the associated NVM memory row must be erased
  224. * first via the \ref nvm_erase_row() function. Writing to a non-erased page
  225. * will result in corrupt data being stored in the NVM memory space.
  226. *
  227. * \section asfdoc_sam0_nvm_special_considerations Special Considerations
  228. *
  229. * \subsection asfdoc_sam0_nvm_special_consideration_pageerase Page Erasure
  230. * The granularity of an erase is per row, while the granularity of a write is
  231. * per page. Thus, if the user application is modifying only one page of a row,
  232. * the remaining pages in the row must be buffered and the row erased, as an
  233. * erase is mandatory before writing to a page.
  234. *
  235. * \subsection asfdoc_sam0_nvm_special_consideration_clocks Clocks
  236. * The user must ensure that the driver is configured with a proper number of
  237. * wait states when the CPU is running at high frequencies.
  238. *
  239. * \subsection asfdoc_sam0_nvm_special_consideration_security_bit Security Bit
  240. * The User Row in the Auxiliary Space cannot be read or written when
  241. * the Security Bit is set. The Security Bit can be set by using passing
  242. * \ref NVM_COMMAND_SET_SECURITY_BIT to the \ref nvm_execute_command() function,
  243. * or it will be set if one tries to access a locked region. See
  244. * \ref asfdoc_sam0_nvm_module_overview_locking_regions.
  245. *
  246. * The Security Bit can only be cleared by performing a chip erase.
  247. *
  248. *
  249. * \section asfdoc_sam0_nvm_extra_info Extra Information
  250. *
  251. * For extra information, see \ref asfdoc_sam0_nvm_extra. This includes:
  252. * - \ref asfdoc_sam0_nvm_extra_acronyms
  253. * - \ref asfdoc_sam0_nvm_extra_dependencies
  254. * - \ref asfdoc_sam0_nvm_extra_errata
  255. * - \ref asfdoc_sam0_nvm_extra_history
  256. *
  257. *
  258. * \section asfdoc_sam0_nvm_examples Examples
  259. *
  260. * For a list of examples related to this driver, see
  261. * \ref asfdoc_sam0_nvm_exqsg.
  262. *
  263. *
  264. * \section asfdoc_sam0_nvm_api_overview API Overview
  265. * @{
  266. */
  267. #include <compiler.h>
  268. #include <status_codes.h>
  269. #include <stdbool.h>
  270. #ifdef __cplusplus
  271. extern "C" {
  272. #endif
  273. /* Define SAMD21-64K devices */
  274. #if defined(SAMD21E15L) || defined(SAMD21E16L) || defined(__SAMD21E15L__) || defined(__SAMD21E16L__) \
  275. || defined(SAMD21E15B) || defined(SAMD21E16B) || defined(__SAMD21E15B__) || defined(__SAMD21E16B__) \
  276. || defined(SAMD21E15BU) || defined(SAMD21E16BU) || defined(__SAMD21E15BU__) || defined(__SAMD21E16BU__) \
  277. || defined(SAMD21G15L) || defined(SAMD21G16L) || defined(__SAMD21G15L__) || defined(__SAMD21G16L__) \
  278. || defined(SAMD21G15B) || defined(SAMD21G16B) || defined(__SAMD21G15B__) || defined(__SAMD21G16B__) \
  279. || defined(SAMD21J15B) || defined(SAMD21J16B) || defined(__SAMD21J15B__) || defined(__SAMD21J16B__)
  280. # define SAMD21_64K
  281. #endif
  282. /**
  283. * \name Driver Feature Definition
  284. *
  285. * Define NVM features set according to the different device families.
  286. * @{
  287. */
  288. #if (SAML21) || (SAML22) || (SAMDA1) || (SAMC20) || (SAMC21) || (SAMR30) || defined(SAMD21_64K) || (SAMHA1) \
  289. || defined(__DOXYGEN__)
  290. /** Read while write EEPROM emulation feature. */
  291. # define FEATURE_NVM_RWWEE
  292. #endif
  293. #if (SAML21) || (SAMR30) || defined(__DOXYGEN__)
  294. /** Brown-out detector internal to the voltage regulator for VDDCORE. */
  295. #define FEATURE_BOD12
  296. #endif
  297. /*@}*/
  298. #if !defined(__DOXYGEN__)
  299. /**
  300. * \brief Mask for the error flags in the status register.
  301. */
  302. # define NVM_ERRORS_MASK (NVMCTRL_STATUS_PROGE | \
  303. NVMCTRL_STATUS_LOCKE | \
  304. NVMCTRL_STATUS_NVME)
  305. #endif
  306. /**
  307. * \brief NVM error flags.
  308. *
  309. * Possible NVM controller error codes, which can be returned by the NVM
  310. * controller after a command is issued.
  311. */
  312. enum nvm_error {
  313. /** No errors */
  314. NVM_ERROR_NONE = 0,
  315. /** Lock error, a locked region was attempted accessed */
  316. NVM_ERROR_LOCK = NVMCTRL_STATUS_NVME | NVMCTRL_STATUS_LOCKE,
  317. /** Program error, invalid command was executed */
  318. NVM_ERROR_PROG = NVMCTRL_STATUS_NVME | NVMCTRL_STATUS_PROGE,
  319. };
  320. /**
  321. * \brief NVM controller commands.
  322. */
  323. enum nvm_command {
  324. /** Erases the addressed memory row */
  325. NVM_COMMAND_ERASE_ROW = NVMCTRL_CTRLA_CMD_ER,
  326. /** Write the contents of the page buffer to the addressed memory page */
  327. NVM_COMMAND_WRITE_PAGE = NVMCTRL_CTRLA_CMD_WP,
  328. /** Erases the addressed auxiliary memory row.
  329. *
  330. * \note This command can only be given when the security bit is not set.
  331. */
  332. NVM_COMMAND_ERASE_AUX_ROW = NVMCTRL_CTRLA_CMD_EAR,
  333. /** Write the contents of the page buffer to the addressed auxiliary memory
  334. * row.
  335. *
  336. * \note This command can only be given when the security bit is not set.
  337. */
  338. NVM_COMMAND_WRITE_AUX_ROW = NVMCTRL_CTRLA_CMD_WAP,
  339. /** Locks the addressed memory region, preventing further modifications
  340. * until the region is unlocked or the device is erased
  341. */
  342. NVM_COMMAND_LOCK_REGION = NVMCTRL_CTRLA_CMD_LR,
  343. /** Unlocks the addressed memory region, allowing the region contents to be
  344. * modified
  345. */
  346. NVM_COMMAND_UNLOCK_REGION = NVMCTRL_CTRLA_CMD_UR,
  347. /** Clears the page buffer of the NVM controller, resetting the contents to
  348. * all zero values
  349. */
  350. NVM_COMMAND_PAGE_BUFFER_CLEAR = NVMCTRL_CTRLA_CMD_PBC,
  351. /** Sets the device security bit, disallowing the changing of lock bits and
  352. * auxiliary row data until a chip erase has been performed
  353. */
  354. NVM_COMMAND_SET_SECURITY_BIT = NVMCTRL_CTRLA_CMD_SSB,
  355. /** Enter power reduction mode in the NVM controller to reduce the power
  356. * consumption of the system
  357. */
  358. NVM_COMMAND_ENTER_LOW_POWER_MODE = NVMCTRL_CTRLA_CMD_SPRM,
  359. /** Exit power reduction mode in the NVM controller to allow other NVM
  360. * commands to be issued
  361. */
  362. NVM_COMMAND_EXIT_LOW_POWER_MODE = NVMCTRL_CTRLA_CMD_CPRM,
  363. #ifdef FEATURE_NVM_RWWEE
  364. /** Read while write (RWW) EEPROM area erase row */
  365. NVM_COMMAND_RWWEE_ERASE_ROW = NVMCTRL_CTRLA_CMD_RWWEEER,
  366. /** RWW EEPROM write page */
  367. NVM_COMMAND_RWWEE_WRITE_PAGE = NVMCTRL_CTRLA_CMD_RWWEEWP,
  368. #endif
  369. };
  370. /**
  371. * \brief NVM controller power reduction mode configurations.
  372. *
  373. * Power reduction modes of the NVM controller, to conserve power while the
  374. * device is in sleep.
  375. */
  376. enum nvm_sleep_power_mode {
  377. /** NVM controller exits low-power mode on first access after sleep */
  378. NVM_SLEEP_POWER_MODE_WAKEONACCESS = NVMCTRL_CTRLB_SLEEPPRM_WAKEONACCESS_Val,
  379. /** NVM controller exits low-power mode when the device exits sleep mode */
  380. NVM_SLEEP_POWER_MODE_WAKEUPINSTANT = NVMCTRL_CTRLB_SLEEPPRM_WAKEUPINSTANT_Val,
  381. /** Power reduction mode in the NVM controller disabled */
  382. NVM_SLEEP_POWER_MODE_ALWAYS_AWAKE = NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val,
  383. };
  384. /**
  385. * \brief NVM controller cache readmode configuration.
  386. *
  387. * Control how the NVM cache prefetch data from flash.
  388. *
  389. */
  390. enum nvm_cache_readmode {
  391. /** The NVM Controller (cache system) does not insert wait states on
  392. * a cache miss. Gives the best system performance.
  393. */
  394. NVM_CACHE_READMODE_NO_MISS_PENALTY,
  395. /** Reduces power consumption of the cache system, but inserts a
  396. * wait state each time there is a cache miss
  397. */
  398. NVM_CACHE_READMODE_LOW_POWER,
  399. /** The cache system ensures that a cache hit or miss takes the same
  400. * amount of time, determined by the number of programmed flash
  401. * wait states
  402. */
  403. NVM_CACHE_READMODE_DETERMINISTIC,
  404. };
  405. /**
  406. * \brief NVM controller configuration structure.
  407. *
  408. * Configuration structure for the NVM controller within the device.
  409. */
  410. struct nvm_config {
  411. /** Power reduction mode during device sleep */
  412. enum nvm_sleep_power_mode sleep_power_mode;
  413. /** Manual write mode; if enabled, pages loaded into the NVM buffer will
  414. * not be written until a separate write command is issued. If disabled,
  415. * writing to the last byte in the NVM page buffer will trigger an automatic
  416. * write.
  417. *
  418. * \note If a partial page is to be written, a manual write command must be
  419. * executed in either mode.
  420. */
  421. bool manual_page_write;
  422. /** Number of wait states to insert when reading from flash, to prevent
  423. * invalid data from being read at high clock frequencies
  424. */
  425. uint8_t wait_states;
  426. /**
  427. * Setting this to true will disable the pre-fetch cache in front of the
  428. * NVM controller
  429. */
  430. bool disable_cache;
  431. #if (SAMC20) || (SAMC21)
  432. /**
  433. * Setting this to true will disable the pre-fetch RWW cache in front of the
  434. * NVM controller.
  435. * If RWW cache is enabled, NVM cache will also be enabled.
  436. */
  437. bool disable_rww_cache;
  438. #endif
  439. /**
  440. * Select the mode for how the cache will pre-fetch data from the flash
  441. */
  442. enum nvm_cache_readmode cache_readmode;
  443. };
  444. /**
  445. * \brief NVM memory parameter structure.
  446. *
  447. * Structure containing the memory layout parameters of the NVM module.
  448. */
  449. struct nvm_parameters {
  450. /** Number of bytes per page */
  451. uint8_t page_size;
  452. /** Number of pages in the main array */
  453. uint16_t nvm_number_of_pages;
  454. /** Size of the emulated EEPROM memory section configured in the NVM
  455. * auxiliary memory space */
  456. uint32_t eeprom_number_of_pages;
  457. /** Size of the Bootloader memory section configured in the NVM auxiliary
  458. * memory space */
  459. uint32_t bootloader_number_of_pages;
  460. #ifdef FEATURE_NVM_RWWEE
  461. /** Number of pages in read while write EEPROM (RWWEE) emulation area */
  462. uint16_t rww_eeprom_number_of_pages;
  463. #endif
  464. };
  465. /**
  466. * \brief Bootloader size.
  467. *
  468. * Available bootloader protection sizes in kilobytes.
  469. *
  470. */
  471. enum nvm_bootloader_size {
  472. /** Boot Loader Size is 32768 bytes */
  473. NVM_BOOTLOADER_SIZE_128,
  474. /** Boot Loader Size is 16384 bytes */
  475. NVM_BOOTLOADER_SIZE_64,
  476. /** Boot Loader Size is 8192 bytes */
  477. NVM_BOOTLOADER_SIZE_32,
  478. /** Boot Loader Size is 4096 bytes */
  479. NVM_BOOTLOADER_SIZE_16,
  480. /** Boot Loader Size is 2048 bytes */
  481. NVM_BOOTLOADER_SIZE_8,
  482. /** Boot Loader Size is 1024 bytes */
  483. NVM_BOOTLOADER_SIZE_4,
  484. /** Boot Loader Size is 512 bytes */
  485. NVM_BOOTLOADER_SIZE_2,
  486. /** Boot Loader Size is 0 bytes */
  487. NVM_BOOTLOADER_SIZE_0,
  488. };
  489. /**
  490. * \brief EEPROM emulator size.
  491. *
  492. * Available space in flash dedicated for EEPROM emulator in bytes.
  493. *
  494. */
  495. enum nvm_eeprom_emulator_size {
  496. /** EEPROM Size for EEPROM emulation is 16384 bytes */
  497. NVM_EEPROM_EMULATOR_SIZE_16384,
  498. /** EEPROM Size for EEPROM emulation is 8192 bytes */
  499. NVM_EEPROM_EMULATOR_SIZE_8192,
  500. /** EEPROM Size for EEPROM emulation is 4096 bytes */
  501. NVM_EEPROM_EMULATOR_SIZE_4096,
  502. /** EEPROM Size for EEPROM emulation is 2048 bytes */
  503. NVM_EEPROM_EMULATOR_SIZE_2048,
  504. /** EEPROM Size for EEPROM emulation is 1024 bytes */
  505. NVM_EEPROM_EMULATOR_SIZE_1024,
  506. /** EEPROM Size for EEPROM emulation is 512 bytes */
  507. NVM_EEPROM_EMULATOR_SIZE_512,
  508. /** EEPROM Size for EEPROM emulation is 256 bytes */
  509. NVM_EEPROM_EMULATOR_SIZE_256,
  510. /** EEPROM Size for EEPROM emulation is 0 bytes */
  511. NVM_EEPROM_EMULATOR_SIZE_0,
  512. };
  513. /**
  514. * \brief BOD33 Action.
  515. *
  516. * What action should be triggered when BOD33 is detected.
  517. *
  518. */
  519. enum nvm_bod33_action {
  520. /** No action */
  521. NVM_BOD33_ACTION_NONE,
  522. /** The BOD33 generates a reset */
  523. NVM_BOD33_ACTION_RESET,
  524. /** The BOD33 generates an interrupt */
  525. NVM_BOD33_ACTION_INTERRUPT,
  526. };
  527. #ifdef FEATURE_BOD12
  528. /**
  529. * \brief BOD12 Action.
  530. *
  531. * What action should be triggered when BOD12 is detected.
  532. *
  533. */
  534. enum nvm_bod12_action {
  535. /** No action */
  536. NVM_BOD12_ACTION_NONE,
  537. /** The BOD12 generates a reset */
  538. NVM_BOD12_ACTION_RESET,
  539. /** The BOD12 generates an interrupt */
  540. NVM_BOD12_ACTION_INTERRUPT,
  541. };
  542. #endif
  543. /**
  544. * \brief WDT Window time-out period.
  545. *
  546. * Window mode time-out period in clock cycles.
  547. *
  548. */
  549. enum nvm_wdt_window_timeout {
  550. /** 8 clock cycles */
  551. NVM_WDT_WINDOW_TIMEOUT_PERIOD_8,
  552. /** 16 clock cycles */
  553. NVM_WDT_WINDOW_TIMEOUT_PERIOD_16,
  554. /** 32 clock cycles */
  555. NVM_WDT_WINDOW_TIMEOUT_PERIOD_32,
  556. /** 64 clock cycles */
  557. NVM_WDT_WINDOW_TIMEOUT_PERIOD_64,
  558. /** 128 clock cycles */
  559. NVM_WDT_WINDOW_TIMEOUT_PERIOD_128,
  560. /** 256 clock cycles */
  561. NVM_WDT_WINDOW_TIMEOUT_PERIOD_256,
  562. /** 512 clock cycles */
  563. NVM_WDT_WINDOW_TIMEOUT_PERIOD_512,
  564. /** 1024 clock cycles */
  565. NVM_WDT_WINDOW_TIMEOUT_PERIOD_1024,
  566. /** 2048 clock cycles */
  567. NVM_WDT_WINDOW_TIMEOUT_PERIOD_2048,
  568. /** 4096 clock cycles */
  569. NVM_WDT_WINDOW_TIMEOUT_PERIOD_4096,
  570. /** 8192 clock cycles */
  571. NVM_WDT_WINDOW_TIMEOUT_PERIOD_8192,
  572. /** 16384 clock cycles */
  573. NVM_WDT_WINDOW_TIMEOUT_PERIOD_16384,
  574. };
  575. /**
  576. * \brief WDT Early warning offset.
  577. *
  578. * This setting determine how many GCLK_WDT cycles before a watchdog time-out period
  579. * an early warning interrupt should be triggered.
  580. *
  581. */
  582. enum nvm_wdt_early_warning_offset {
  583. /** 8 clock cycles */
  584. NVM_WDT_EARLY_WARNING_OFFSET_8,
  585. /** 16 clock cycles */
  586. NVM_WDT_EARLY_WARNING_OFFSET_16,
  587. /** 32 clock cycles */
  588. NVM_WDT_EARLY_WARNING_OFFSET_32,
  589. /** 64 clock cycles */
  590. NVM_WDT_EARLY_WARNING_OFFSET_64,
  591. /** 128 clock cycles */
  592. NVM_WDT_EARLY_WARNING_OFFSET_128,
  593. /** 256 clock cycles */
  594. NVM_WDT_EARLY_WARNING_OFFSET_256,
  595. /** 512 clock cycles */
  596. NVM_WDT_EARLY_WARNING_OFFSET_512,
  597. /** 1024 clock cycles */
  598. NVM_WDT_EARLY_WARNING_OFFSET_1024,
  599. /** 2048 clock cycles */
  600. NVM_WDT_EARLY_WARNING_OFFSET_2048,
  601. /** 4096 clock cycles */
  602. NVM_WDT_EARLY_WARNING_OFFSET_4096,
  603. /** 8192 clock cycles */
  604. NVM_WDT_EARLY_WARNING_OFFSET_8192,
  605. /** 16384 clock cycles */
  606. NVM_WDT_EARLY_WARNING_OFFSET_16384,
  607. };
  608. /**
  609. * \brief NVM user row fuse setting structure.
  610. *
  611. * This structure contain the layout of the first 64 bits of the user row
  612. * which contain the fuse settings.
  613. */
  614. struct nvm_fusebits {
  615. /** Bootloader size */
  616. enum nvm_bootloader_size bootloader_size;
  617. /** EEPROM emulation area size */
  618. enum nvm_eeprom_emulator_size eeprom_size;
  619. #if (SAMC20) || (SAMC21)
  620. /** BODVDD Threshold level at power on */
  621. uint8_t bodvdd_level;
  622. /** BODVDD Enable at power on */
  623. bool bodvdd_enable;
  624. /** BODVDD Action at power on */
  625. enum nvm_bod33_action bodvdd_action;
  626. /* BODVDD Hysteresis at power on */
  627. bool bodvdd_hysteresis;
  628. #else
  629. /** BOD33 Threshold level at power on */
  630. uint8_t bod33_level;
  631. /** BOD33 Enable at power on */
  632. bool bod33_enable;
  633. /** BOD33 Action at power on */
  634. enum nvm_bod33_action bod33_action;
  635. /* BOD33 Hysteresis at power on */
  636. bool bod33_hysteresis;
  637. #endif
  638. /** WDT Enable at power on */
  639. bool wdt_enable;
  640. /** WDT Always-on at power on */
  641. bool wdt_always_on;
  642. /** WDT Period at power on */
  643. uint8_t wdt_timeout_period;
  644. /** WDT Window mode time-out at power on */
  645. enum nvm_wdt_window_timeout wdt_window_timeout;
  646. /** WDT Early warning interrupt time offset at power on */
  647. enum nvm_wdt_early_warning_offset wdt_early_warning_offset;
  648. /** WDT Window mode enabled at power on */
  649. bool wdt_window_mode_enable_at_poweron;
  650. /** NVM Lock bits */
  651. uint16_t lockbits;
  652. #ifdef FEATURE_BOD12
  653. /** BOD12 Threshold level at power on */
  654. uint8_t bod12_level;
  655. /** BOD12 Enable at power on */
  656. bool bod12_enable;
  657. /** BOD12 Action at power on */
  658. enum nvm_bod12_action bod12_action;
  659. /* BOD12 Hysteresis at power on */
  660. bool bod12_hysteresis;
  661. #endif
  662. };
  663. /**
  664. * \name Configuration and Initialization
  665. * @{
  666. */
  667. /**
  668. * \brief Initializes an NVM controller configuration structure to defaults.
  669. *
  670. * Initializes a given NVM controller configuration structure to a set of
  671. * known default values. This function should be called on all new
  672. * instances of these configuration structures before being modified by the
  673. * user application.
  674. *
  675. * The default configuration is as follows:
  676. * \li Power reduction mode enabled after sleep mode until first NVM access
  677. * \li Automatic page write mode disabled
  678. * \li Number of FLASH wait states left unchanged
  679. *
  680. * \param[out] config Configuration structure to initialize to default values
  681. *
  682. */
  683. static inline void nvm_get_config_defaults(
  684. struct nvm_config *const config)
  685. {
  686. /* Sanity check the parameters */
  687. Assert(config);
  688. /* Write the default configuration for the NVM configuration */
  689. config->sleep_power_mode = NVM_SLEEP_POWER_MODE_WAKEONACCESS;
  690. config->manual_page_write = true;
  691. config->wait_states = NVMCTRL->CTRLB.bit.RWS;
  692. config->disable_cache = false;
  693. #if (SAMC20) || (SAMC21)
  694. config->disable_rww_cache = false;
  695. #endif
  696. config->cache_readmode = NVM_CACHE_READMODE_NO_MISS_PENALTY;
  697. }
  698. enum status_code nvm_set_config(
  699. const struct nvm_config *const config);
  700. /**
  701. * \brief Checks if the NVM controller is ready to accept a new command.
  702. *
  703. * Checks the NVM controller to determine if it is currently busy execution an
  704. * operation, or ready for a new command.
  705. *
  706. * \return Busy state of the NVM controller.
  707. *
  708. * \retval true If the hardware module is ready for a new command
  709. * \retval false If the hardware module is busy executing a command
  710. *
  711. */
  712. static inline bool nvm_is_ready(void)
  713. {
  714. /* Get a pointer to the module hardware instance */
  715. Nvmctrl *const nvm_module = NVMCTRL;
  716. return nvm_module->INTFLAG.reg & NVMCTRL_INTFLAG_READY;
  717. }
  718. /** @} */
  719. /**
  720. * \name NVM Access Management
  721. * @{
  722. */
  723. void nvm_get_parameters(
  724. struct nvm_parameters *const parameters);
  725. enum status_code nvm_write_buffer(
  726. const uint32_t destination_address,
  727. const uint8_t *buffer,
  728. uint16_t length);
  729. enum status_code nvm_read_buffer(
  730. const uint32_t source_address,
  731. uint8_t *const buffer,
  732. uint16_t length);
  733. enum status_code nvm_update_buffer(
  734. const uint32_t destination_address,
  735. uint8_t *const buffer,
  736. uint16_t offset,
  737. uint16_t length);
  738. enum status_code nvm_erase_row(
  739. const uint32_t row_address);
  740. enum status_code nvm_execute_command(
  741. const enum nvm_command command,
  742. const uint32_t address,
  743. const uint32_t parameter);
  744. enum status_code nvm_get_fuses(struct nvm_fusebits *fusebits);
  745. enum status_code nvm_set_fuses(struct nvm_fusebits *fb);
  746. bool nvm_is_page_locked(uint16_t page_number);
  747. /**
  748. * \brief Retrieves the error code of the last issued NVM operation.
  749. *
  750. * Retrieves the error code from the last executed NVM operation. Once
  751. * retrieved, any error state flags in the controller are cleared.
  752. *
  753. * \note The \ref nvm_is_ready() function is an exception. Thus, errors
  754. * retrieved after running this function should be valid for the function
  755. * executed before \ref nvm_is_ready().
  756. *
  757. * \return Error caused by the last NVM operation.
  758. *
  759. * \retval NVM_ERROR_NONE No error occurred in the last NVM operation
  760. *
  761. * \retval NVM_ERROR_LOCK The last NVM operation attempted to access a locked
  762. * region
  763. * \retval NVM_ERROR_PROG An invalid NVM command was issued
  764. */
  765. static inline enum nvm_error nvm_get_error(void)
  766. {
  767. enum nvm_error ret_val;
  768. /* Get a pointer to the module hardware instance */
  769. Nvmctrl *const nvm_module = NVMCTRL;
  770. /* Mask out non-error bits */
  771. ret_val = (enum nvm_error)(nvm_module->STATUS.reg & NVM_ERRORS_MASK);
  772. /* Clear error flags */
  773. nvm_module->STATUS.reg = NVM_ERRORS_MASK;
  774. /* Return error code from the NVM controller */
  775. return ret_val;
  776. }
  777. /** @} */
  778. #ifdef __cplusplus
  779. }
  780. #endif
  781. /** @} */
  782. /**
  783. * \page asfdoc_sam0_nvm_extra Extra Information for NVM Driver
  784. *
  785. * \section asfdoc_sam0_nvm_extra_acronyms Acronyms
  786. * The table below presents the acronyms used in this module:
  787. *
  788. * <table>
  789. * <tr>
  790. * <th>Acronym</th>
  791. * <th>Description</th>
  792. * </tr>
  793. * <tr>
  794. * <td>NVM</td>
  795. * <td>Non-Volatile Memory</td>
  796. * </tr>
  797. * <tr>
  798. * <td>EEPROM</td>
  799. * <td>Electrically Erasable Programmable Read-Only Memory</td>
  800. * </tr>
  801. * </table>
  802. *
  803. *
  804. * \section asfdoc_sam0_nvm_extra_dependencies Dependencies
  805. * This driver has the following dependencies:
  806. *
  807. * - None
  808. *
  809. *
  810. * \section asfdoc_sam0_nvm_extra_errata Errata
  811. * There are no errata related to this driver.
  812. *
  813. *
  814. * \section asfdoc_sam0_nvm_extra_history Module History
  815. * An overview of the module history is presented in the table below, with
  816. * details on the enhancements and fixes made to the module since its first
  817. * release. The current version of this corresponds to the newest version in
  818. * the table.
  819. *
  820. * <table>
  821. * <tr>
  822. * <th>Changelog</th>
  823. * </tr>
  824. * <tr>
  825. * <td>Removed BOD12 reference, removed nvm_set_fuses() API</td>
  826. * </tr>
  827. * <tr>
  828. * <td>Added functions to read/write fuse settings</td>
  829. * </tr>
  830. * <tr>
  831. * <td>Added support for NVM cache configuration</td>
  832. * </tr>
  833. * <tr>
  834. * <td>Updated initialization function to also enable the digital interface
  835. * clock to the module if it is disabled</td>
  836. * </tr>
  837. * <tr>
  838. * <td>Initial Release</td>
  839. * </tr>
  840. * </table>
  841. */
  842. /**
  843. * \page asfdoc_sam0_nvm_exqsg Examples for NVM Driver
  844. *
  845. * This is a list of the available Quick Start guides (QSGs) and example
  846. * applications for \ref asfdoc_sam0_nvm_group. QSGs are simple examples with
  847. * step-by-step instructions to configure and use this driver in a selection of
  848. * use cases. Note that a QSG can be compiled as a standalone application or be
  849. * added to the user application.
  850. *
  851. * - \subpage asfdoc_sam0_nvm_basic_use_case
  852. *
  853. * \page asfdoc_sam0_nvm_document_revision_history Document Revision History
  854. *
  855. * <table>
  856. * <tr>
  857. * <th>Doc. Rev.</th>
  858. * <th>Date</th>
  859. * <th>Comments</th>
  860. * </tr>
  861. * <tr>
  862. * <td>42114E</td>
  863. * <td>12/2015</td>
  864. * <td>Added support for SAM L21/L22, SAM C21, SAM D09, SAMR30 and SAM DA1</td>
  865. * </tr>
  866. * <tr>
  867. * <td>42114D</td>
  868. * <td>12/2014</td>
  869. * <td>Added support for SAM R21 and SAM D10/D11</td>
  870. * </tr>
  871. * <tr>
  872. * <td>42114C</td>
  873. * <td>01/2014</td>
  874. * <td>Added support for SAM D21</td>
  875. * </tr>
  876. * <tr>
  877. * <td>42114B</td>
  878. * <td>06/2013</td>
  879. * <td>Corrected documentation typos</td>
  880. * </tr>
  881. * <tr>
  882. * <td>42114A</td>
  883. * <td>06/2013</td>
  884. * <td>Initial document release</td>
  885. * </tr>
  886. * </table>
  887. */
  888. #endif /* NVM_H_INCLUDED */