common_nvm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * \file
  3. *
  4. * \brief Non volatile memories management
  5. *
  6. * Copyright (c) 2012-2015 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 COMMON_NVM_H_INCLUDED
  47. #define COMMON_NVM_H_INCLUDED
  48. #include "compiler.h"
  49. #include "conf_board.h"
  50. #include "parts.h"
  51. #include "status_codes.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX)
  56. #include "at45dbx.h"
  57. #endif
  58. /* ! \name Non volatile memory types */
  59. /* ! @{ */
  60. typedef enum {
  61. INT_FLASH /* !< Internal Flash */
  62. #if (XMEGA || UC3 || SAM4S)
  63. , INT_USERPAGE /* !< Userpage/User signature */
  64. #endif
  65. #if XMEGA
  66. , INT_EEPROM /* !< Internal EEPROM */
  67. #endif
  68. #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX)
  69. , AT45DBX /* !< External AT45DBX dataflash */
  70. #endif
  71. } mem_type_t;
  72. /* ! @} */
  73. #if SAM
  74. # ifndef IFLASH_PAGE_SIZE
  75. # define IFLASH_PAGE_SIZE IFLASH0_PAGE_SIZE
  76. # endif
  77. # ifndef IFLASH_ADDR
  78. # define IFLASH_ADDR IFLASH0_ADDR
  79. # endif
  80. #endif
  81. /**
  82. * \defgroup nvm_group NVM service
  83. *
  84. * See \ref common_nvm_quickstart.
  85. *
  86. * This is the common API for non volatile memories. Additional features are
  87. * available
  88. * in the documentation of the specific modules.
  89. *
  90. */
  91. /**
  92. * \brief Initialize the non volatile memory specified.
  93. *
  94. * \param mem Type of non volatile memory to initialize
  95. */
  96. status_code_t nvm_init(mem_type_t mem);
  97. /**
  98. * \brief Read single byte of data.
  99. *
  100. * \param mem Type of non volatile memory to read
  101. * \param address Address to read
  102. * \param data Pointer to where to store the read data
  103. */
  104. status_code_t nvm_read_char(mem_type_t mem, uint32_t address, uint8_t *data);
  105. /**
  106. * \brief Write single byte of data.
  107. *
  108. * \note For SAM4S internal flash, the page existed in the address must be erased first
  109. * before written, and the minimum write unit is a page,thus when writing a single
  110. * byte, a whole page that contains the data is writen.
  111. *
  112. * \param mem Type of non volatile memory to write
  113. * \param address Address to write
  114. * \param data Data to be written
  115. */
  116. status_code_t nvm_write_char(mem_type_t mem, uint32_t address, uint8_t data);
  117. /**
  118. * \brief Read \a len number of bytes from address \a address in non volatile
  119. * memory \a mem and store it in the buffer \a buffer
  120. *
  121. * \param mem Type of non volatile memory to read
  122. * \param address Address to read
  123. * \param buffer Pointer to destination buffer
  124. * \param len Number of bytes to read
  125. */
  126. status_code_t nvm_read(mem_type_t mem, uint32_t address, void *buffer,
  127. uint32_t len);
  128. /**
  129. * \brief Write \a len number of bytes at address \a address in non volatile
  130. * memory \a mem from the buffer \a buffer
  131. *
  132. * \note For SAM4S internal flash, the page existed in the address must be erased
  133. * first before written.
  134. *
  135. * \param mem Type of non volatile memory to write
  136. * \param address Address to write
  137. * \param buffer Pointer to source buffer
  138. * \param len Number of bytes to write
  139. */
  140. status_code_t nvm_write(mem_type_t mem, uint32_t address, void *buffer,
  141. uint32_t len);
  142. /**
  143. * \brief Erase a page in the non volatile memory.
  144. *
  145. * The function is only available for internal flash and/or userpage signature.
  146. *
  147. * \note For SAM4S internal flash erase, the minimum erase unit is 8 pages.
  148. *
  149. * \param mem Type of non volatile memory to erase
  150. * \param page_number Page number to erase
  151. */
  152. status_code_t nvm_page_erase(mem_type_t mem, uint32_t page_number);
  153. /**
  154. * \brief Get the size of whole non volatile memory specified.
  155. *
  156. * \param mem Type of non volatile memory
  157. * \param size Pointer to where to store the size
  158. */
  159. status_code_t nvm_get_size(mem_type_t mem, uint32_t *size);
  160. /**
  161. * \brief Get the size of a page in the non volatile memory specified.
  162. *
  163. * \param mem Type of non volatile memory
  164. * \param size Pointer to where to store the size
  165. */
  166. status_code_t nvm_get_page_size(mem_type_t mem, uint32_t *size);
  167. /**
  168. * \brief Get the page number from the byte address \a address.
  169. *
  170. * \param mem Type of non volatile memory
  171. * \param address Byte address of the non volatile memory
  172. * \param num Pointer to where to store the page number
  173. */
  174. status_code_t nvm_get_pagenumber(mem_type_t mem, uint32_t address,
  175. uint32_t *num);
  176. /**
  177. * \brief Enable security bit which blocks external read and write access
  178. * to the device.
  179. *
  180. */
  181. status_code_t nvm_set_security_bit(void);
  182. /**
  183. * \page common_nvm_quickstart Quick Start quide for common NVM driver
  184. *
  185. * This is the quick start quide for the \ref nvm_group "Common NVM driver",
  186. * with step-by-step instructions on how to configure and use the driver in a
  187. * selection of use cases.
  188. *
  189. * The use cases contain several code fragments. The code fragments in the
  190. * steps for setup can be copied into a custom initialization function, while
  191. * the steps for usage can be copied into, e.g., the main application function.
  192. *
  193. * \section nvm_basic_use_case Basic use case
  194. * In this basic use case, NVM driver is configured for Internal Flash
  195. *
  196. * \section nvm_basic_use_case_setup Setup steps
  197. *
  198. * \subsection nvm_basic_use_case_setup_code Example code
  199. * Add to you application C-file:
  200. * \code
  201. if(nvm_init(INT_FLASH) == STATUS_OK)
  202. do_something();
  203. \endcode
  204. *
  205. * \subsection nvm_basic_use_case_setup_flow Workflow
  206. * -# Ensure that board_init() has configured selected I/Os for TWI function
  207. * when using external AT45DBX dataflash
  208. * -# Ensure that \ref conf_nvm.h is present for the driver.
  209. * - \note This file is only for the driver and should not be included by the
  210. * user.
  211. * -# Call nvm_init \code nvm_init(INT_FLASH); \endcode
  212. * and optionally check its return code
  213. *
  214. * \section nvm_basic_use_case_usage Usage steps
  215. * \subsection nvm_basic_use_case_usage_code_writing Example code: Writing to
  216. * non volatile memory
  217. * Use in the application C-file:
  218. * \code
  219. uint8_t buffer[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE};
  220. if(nvm_write(INT_FLASH, test_address, (void *)buffer, sizeof(buffer)) ==
  221. STATUS_OK)
  222. do_something();
  223. \endcode
  224. *
  225. * \subsection nvm_basic_use_case_usage_flow Workflow
  226. * -# Prepare the data you want to send to the non volatile memory
  227. * \code uint8_t buffer[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE}; \endcode
  228. * -# Call nvm_write \code nvm_write(INT_FLASH, test_address, (void *)buffer,
  229. sizeof(buffer)) \endcode
  230. * and optionally check its return value for STATUS_OK.
  231. *
  232. * \subsection nvm_basic_use_case_usage_code_reading Example code: Reading from
  233. * non volatile memory
  234. * Use in application C-file:
  235. * \code
  236. uint8_t data_read[8];
  237. if(nvm_read(INT_FLASH, test_address, (void *)data_read, sizeof(data_read))
  238. == STATUS_OK) {
  239. //Check read content
  240. if(data_read[0] == 0xAA)
  241. do_something();
  242. }
  243. \endcode
  244. *
  245. * \subsection nvm_basic_use_case_usage_flow Workflow
  246. * -# Prepare a data buffer that will read data from non volatile memory
  247. * \code uint8_t data_read[8]; \endcode
  248. * -# Call nvm_read \code nvm_read(INT_FLASH, test_address, (void *)data_read,
  249. sizeof(data_read)); \endcode
  250. * and optionally check its return value for STATUS_OK.
  251. * The data read from the non volatile memory are in data_read.
  252. *
  253. * \subsection nvm_basic_use_case_usage_code_erasing Example code: Erasing a
  254. * page of non volatile memory
  255. * Use in the application C-file:
  256. * \code
  257. if(nvm_page_erase(INT_FLASH, test_page) == STATUS_OK)
  258. do_something();
  259. \endcode
  260. *
  261. * \subsection nvm_basic_use_case_usage_flow Workflow
  262. * -# Call nvm_page_erase \code nvm_page_erase(INT_FLASH, test_page) \endcode
  263. * and optionally check its return value for STATUS_OK.
  264. *
  265. * \subsection nvm_basic_use_case_usage_code_config Example code: Reading
  266. *configuration of non volatile memory
  267. * Use in application C-file:
  268. * \code
  269. uint8_t mem_size, page_size, page_num;
  270. nvm_get_size(INT_FLASH, &mem_size);
  271. nvm_get_page_size(INT_FLASH, &page_size);
  272. nvm_get_pagenumber(INT_FLASH, test_address, &page_num);
  273. \endcode
  274. *
  275. * \subsection nvm_basic_use_case_usage_flow Workflow
  276. * -# Prepare a buffer to store configuration of non volatile memory
  277. * \code uint8_t mem_size, page_size, page_num; \endcode
  278. * -# Call nvm_get_size \code nvm_get_size(INT_FLASH, &mem_size); \endcode
  279. * and optionally check its return value for STATUS_OK.
  280. * The memory size of the non volatile memory is in mem_size.
  281. * -# Call nvm_get_page_size \code nvm_get_page_size(INT_FLASH, &page_size);
  282. \endcode
  283. * and optionally check its return value for STATUS_OK.
  284. * The page size of the non volatile memory is in page_size.
  285. * -# Call nvm_get_pagenumber \code nvm_get_page_number(INT_FLASH, test_address,
  286. &page_num); \endcode
  287. * and optionally check its return value for STATUS_OK.
  288. * The page number of given address in the non volatile memory is in page_num.
  289. *
  290. * \subsection nvm_basic_use_case_usage_code_locking Example code: Enabling
  291. * security bit
  292. * Use in the application C-file:
  293. * \code
  294. if(nvm_set_security_bit() == STATUS_OK)
  295. do_something();
  296. \endcode
  297. *
  298. * \subsection nvm_basic_use_case_usage_flow Workflow
  299. * -# Call nvm_set_security_bit \code nvm_set_security_bit() \endcode
  300. * and optionally check its return value for STATUS_OK.
  301. */
  302. #ifdef __cplusplus
  303. }
  304. #endif
  305. #endif /* COMMON_NVM_H_INCLUDED */