lpc17xx_iap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. /*!
  36. * \file arch/cm3/dev/nxp/lpc17xx_iap.c
  37. * \brief LPC17xx in application programming flash controller support.
  38. *
  39. * \verbatim
  40. * $Id: $
  41. * \endverbatim
  42. */
  43. #include <sys/timer.h>
  44. #include <dev/nvmem.h>
  45. #include <stdint.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <arch/cm3/nxp/lpc17xx_iap.h>
  49. /*!
  50. * \addtogroup xgLpc17xxIap
  51. */
  52. /*@{*/
  53. /*! \brief Base address of the flash memory chip.
  54. */
  55. #ifndef FLASH_CHIP_BASE
  56. #define FLASH_CHIP_BASE 0x00000000
  57. #endif
  58. /*! \brief Address offset of the configuration sector.
  59. */
  60. #ifndef FLASH_CONF_SECTOR
  61. #define FLASH_CONF_SECTOR 0x00078000
  62. #endif
  63. #if ((FLASH_CONF_SECTOR & 0x000000FF) != 0)
  64. #error FLASH_CONF_SECTOR has to be 256 byte aligned
  65. #endif
  66. /*! \brief Size of the configuration area.
  67. *
  68. * During write operations a buffer with this size is allocated
  69. * from heap and may cause memory problems with large sectors.
  70. * Thus, this value may be less than the size of the configuration
  71. * sector, in which case the rest of the sector is unused.
  72. *
  73. * Currently only 1 sector can be used for system configurations.
  74. */
  75. #ifndef FLASH_CONF_SIZE
  76. #define FLASH_CONF_SIZE 256
  77. #elif ((FLASH_CONF_SIZE != 256) && (FLASH_CONF_SIZE != 512) && \
  78. (FLASH_CONF_SIZE != 1024) && (FLASH_CONF_SIZE != 4096))
  79. #error FLASH_CONF_SIZE has to be either 256 (default), 512, 1024 or 4096
  80. #endif
  81. typedef void (*IAP)(uint32_t *cmd, uint32_t *result);
  82. IAP iap_entry = (IAP) IAP_LOCATION;
  83. #define IAP_Call iap_entry
  84. typedef uint32_t flashdat_t;
  85. typedef unsigned long flashadr_t;
  86. typedef volatile flashdat_t *flashptr_t;
  87. /*!
  88. * \brief Get Sector Number
  89. *
  90. * \param addr Sector address
  91. *
  92. * \return sector number
  93. *
  94. */
  95. uint32_t Lpc17xxIapGetSectorNr (uint32_t addr)
  96. {
  97. uint32_t n;
  98. n = addr >> 12; /* 4kB Sector */
  99. if (n >= 0x10) {
  100. n = 0x0E + (n >> 3); /* 32kB Sector */
  101. }
  102. return n;
  103. }
  104. /*!
  105. * \brief Prepare sector(s) for write operation
  106. *
  107. * \param start_sec Number of start sector
  108. * \param end_sec Number of end sector
  109. *
  110. * \return CMD_SUCCESS/BUSY/INVALID_SECTOR
  111. *
  112. */
  113. static IAP_STATUS_CODE Lpc17xxIapSectorPrepare(uint32_t start_sec, uint32_t end_sec)
  114. {
  115. IAP_COMMAND_Type command;
  116. command.cmd = IAP_PREPARE; /* Prepare Sector for Write */
  117. command.param[0] = start_sec; /* Start Sector */
  118. command.param[1] = end_sec; /* End Sector */
  119. IAP_Call (&command.cmd, &command.status); /* Call IAP Command */
  120. return (IAP_STATUS_CODE)command.status;
  121. }
  122. /*!
  123. * \brief Read data from flash memory.
  124. *
  125. * \param addr Start location within the chip, starting at 0.
  126. * \param data Points to a buffer that receives the data.
  127. * \param len Number of bytes to read.
  128. *
  129. * \return Always CMD_SUCCESS.
  130. */
  131. IAP_STATUS_CODE Lpc17xxIapSectorRead(uint32_t addr, void *data, size_t len)
  132. {
  133. if (data == NULL) {
  134. return DST_ADDR_ERROR;
  135. }
  136. memcpy(data, (void *) (uptr_t) (FLASH_CHIP_BASE + addr), len);
  137. return CMD_SUCCESS;
  138. }
  139. /*!
  140. * \brief Write data to flash
  141. *
  142. * \param dest destination buffer (in Flash memory) (must be 256 byte aligned).
  143. * \param source source buffer (in RAM) (should be word aligned)
  144. * \param size the write size.
  145. *
  146. * \return CMD_SUCCESS. SRC_ADDR_ERROR/DST_ADDR_ERROR
  147. * SRC_ADDR_NOT_MAPPED/DST_ADDR_NOT_MAPPED
  148. * COUNT_ERROR/SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION
  149. * BUSY
  150. *
  151. */
  152. IAP_STATUS_CODE Lpc17xxIapSectorWrite(uint32_t dest, void* source, IAP_WRITE_SIZE size)
  153. {
  154. uint32_t sec;
  155. IAP_STATUS_CODE status;
  156. IAP_COMMAND_Type command;
  157. /* Prepare sectors */
  158. sec = Lpc17xxIapGetSectorNr(dest);
  159. status = Lpc17xxIapSectorPrepare(sec, sec);
  160. if(status != CMD_SUCCESS) {
  161. return status;
  162. }
  163. /* Write data to flash */
  164. command.cmd = IAP_COPY_RAM2FLASH; /* Copy RAM to Flash */
  165. command.param[0] = dest; /* Destination Flash Address */
  166. command.param[1] = (uint32_t)source; /* Source RAM Address */
  167. command.param[2] = size; /* Number of bytes */
  168. command.param[3] = NutArchClockGet(NUT_HWCLK_CPU) / 1000; /* CCLK in kHz */
  169. IAP_Call (&command.cmd, &command.status); /* Call IAP Command */
  170. return (IAP_STATUS_CODE)command.status;
  171. }
  172. /*!
  173. * \brief Erase sector(s)
  174. *
  175. * \param start_sec Number of start sector
  176. * \param end_sec Number of end sector
  177. *
  178. * \return CMD_SUCCESS, INVALID_SECTOR,
  179. * SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION, BUSY
  180. *
  181. */
  182. IAP_STATUS_CODE Lpc17xxIapSectorErase(uint32_t start_sec, uint32_t end_sec)
  183. {
  184. IAP_COMMAND_Type command;
  185. IAP_STATUS_CODE status;
  186. /* Prepare sectors */
  187. status = Lpc17xxIapSectorPrepare(start_sec, end_sec);
  188. if(status != CMD_SUCCESS) {
  189. return status;
  190. }
  191. /* Erase sectors */
  192. command.cmd = IAP_ERASE; /* Prepare Sector for Write */
  193. command.param[0] = start_sec; /* Start Sector */
  194. command.param[1] = end_sec; /* End Sector */
  195. command.param[2] = NutArchClockGet(NUT_HWCLK_CPU) / 1000; /* CCLK in kHz */
  196. IAP_Call (&command.cmd, &command.status); /* Call IAP Command */
  197. return (IAP_STATUS_CODE)command.status;
  198. }
  199. /*!
  200. * \brief Blank check sector(s)
  201. *
  202. * \param start_sec Number of start sector
  203. * \param end _sec Number of end sector
  204. *
  205. * \param first_nblank_off The offset of the first non-blank word
  206. * \param first_nblank_val The value of the first non-blank word
  207. * \return CMD_SUCCESS, INVALID_SECTOR, SECTOR_NOT_BLANK, BUSY
  208. *
  209. */
  210. IAP_STATUS_CODE Lpc17xxIapSectorBlankCheck(uint32_t start_sec, uint32_t end_sec,
  211. uint32_t *first_nblank_off,
  212. uint32_t *first_nblank_val)
  213. {
  214. IAP_COMMAND_Type command;
  215. command.cmd = IAP_BLANK_CHECK; /* Prepare Sector for Write */
  216. command.param[0] = start_sec; /* Start Sector */
  217. command.param[1] = end_sec; /* End Sector */
  218. IAP_Call (&command.cmd, &command.status); /* Call IAP Command */
  219. if(command.status == SECTOR_NOT_BLANK) {
  220. if(first_nblank_off != NULL) {
  221. *first_nblank_off = command.result[0];
  222. }
  223. if(first_nblank_val != NULL) {
  224. *first_nblank_val = command.result[1];
  225. }
  226. }
  227. return (IAP_STATUS_CODE)command.status;
  228. }
  229. /*!
  230. * \brief Read boot code version. The version is interpreted as <major>.<minor>.
  231. *
  232. * \param major Returns major version number
  233. * \param minor Returns minor version number
  234. *
  235. * \return CMD_SUCCESS
  236. *
  237. */
  238. IAP_STATUS_CODE Lpc17xxIapReadBootCodeVersion(uint8_t *major, uint8_t* minor)
  239. {
  240. IAP_COMMAND_Type command;
  241. command.cmd = IAP_READ_BOOT_VER;
  242. IAP_Call (&command.cmd, &command.status); /* Call IAP Command */
  243. if(command.status == CMD_SUCCESS) {
  244. if(major != NULL) {
  245. *major = (command.result[0] >> 8) & 0xFF;
  246. }
  247. if(minor != NULL) {
  248. *minor = (command.result[0]) & 0xFF;
  249. }
  250. }
  251. return (IAP_STATUS_CODE)command.status;
  252. }
  253. /*!
  254. * \brief Read Device serial number.
  255. *
  256. * \param uid Serial number
  257. *
  258. * \return CMD_SUCCESS
  259. *
  260. */
  261. IAP_STATUS_CODE Lpc17xxIapReadDeviceSerialNumber(uint32_t *uid)
  262. {
  263. IAP_COMMAND_Type command;
  264. command.cmd = IAP_READ_SERIAL_NUMBER;
  265. IAP_Call (&command.cmd, &command.status); /* Call IAP Command */
  266. if(command.status == CMD_SUCCESS) {
  267. if(uid != NULL) {
  268. uint32_t i = 0;
  269. for(i = 0; i < 4; i++) {
  270. uid[i] = command.result[i];
  271. }
  272. }
  273. }
  274. return (IAP_STATUS_CODE)command.status;
  275. }
  276. /*!
  277. * \brief Load configuration parameters from embedded flash memory.
  278. *
  279. * Applications should call NutNvMemLoad().
  280. *
  281. * \param pos Start location within configuration sector.
  282. * \param data Points to a buffer that receives the contents.
  283. * \param len Number of bytes to read.
  284. *
  285. * \return Always 0.
  286. */
  287. int Lpc17xxIapParamRead(unsigned int pos, void *data, size_t len)
  288. {
  289. Lpc17xxIapSectorRead(FLASH_CONF_SECTOR + pos, data, len);
  290. return 0;
  291. }
  292. /*!
  293. * \brief Store configuration parameters in embedded flash memory.
  294. *
  295. * Applications should call NutNvMemSave().
  296. *
  297. * \param pos Start location within configuration sector.
  298. * \param data Points to a buffer that contains the bytes to store.
  299. * \param len Number of bytes to store.
  300. *
  301. * \return 0 on success or -1 in case of an error.
  302. */
  303. int Lpc17xxIapParamWrite(unsigned int pos, const void *data, size_t len)
  304. {
  305. int rc = -1;
  306. uint8_t *buff;
  307. /* Load the complete configuration area. */
  308. if ((buff = malloc(FLASH_CONF_SIZE)) != NULL) {
  309. rc = Lpc17xxIapSectorRead(FLASH_CONF_SECTOR, buff, FLASH_CONF_SIZE);
  310. /* Compare old with new contents. */
  311. if (memcmp(buff + pos, data, len)) {
  312. /* New contents differs. Copy it into the sector buffer. */
  313. memcpy(buff + pos, data, len);
  314. /* Write back new data. Maintain region lock. */
  315. if (Lpc17xxIapSectorWrite(FLASH_CONF_SECTOR, buff, FLASH_CONF_SIZE) == CMD_SUCCESS) {
  316. rc = 0;
  317. }
  318. }
  319. free(buff);
  320. }
  321. return rc;
  322. }
  323. /*@}*/