lpc_iap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**********************************************************************
  2. * $Id$ lpc_iap.c 2011-11-21
  3. *//**
  4. * @file lpc_iap.c
  5. * @brief Contains all functions support for IAP on LPC
  6. * @version 1.0
  7. * @date 21. November. 2011
  8. * @author NXP MCU SW Application Team
  9. *
  10. * Copyright(C) 2011, NXP Semiconductor
  11. * All rights reserved.
  12. *
  13. ***********************************************************************
  14. * Software that is described herein is for illustrative purposes only
  15. * which provides customers with programming information regarding the
  16. * products. This software is supplied "AS IS" without any warranties.
  17. * NXP Semiconductors assumes no responsibility or liability for the
  18. * use of the software, conveys no license or title under any patent,
  19. * copyright, or mask work right to the product. NXP Semiconductors
  20. * reserves the right to make changes in the software without
  21. * notification. NXP Semiconductors also make no representation or
  22. * warranty that such application will be suitable for the specified
  23. * use without further testing or modification.
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors'
  26. * relevant copyright in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. **********************************************************************/
  31. #ifdef __BUILD_WITH_EXAMPLE__
  32. #include "lpc_libcfg.h"
  33. #else
  34. #include "lpc_libcfg_default.h"
  35. #endif /* __BUILD_WITH_EXAMPLE__ */
  36. #ifdef _IAP
  37. #include "lpc_iap.h"
  38. #include "lpc_clkpwr.h"
  39. // IAP Command
  40. typedef void (*IAP)(uint32_t *cmd,uint32_t *result);
  41. IAP iap_entry = (IAP) IAP_LOCATION;
  42. #define IAP_Call iap_entry
  43. /** @addtogroup IAP_Public_Functions LCD Public Function
  44. * @ingroup IAP
  45. * @{
  46. */
  47. /*********************************************************************//**
  48. * @brief Get Sector Number
  49. *
  50. * @param[in] adr Sector Address
  51. *
  52. * @return Sector Number.
  53. *
  54. **********************************************************************/
  55. uint32_t GetSecNum (uint32_t adr)
  56. {
  57. uint32_t n;
  58. n = adr >> 12; // 4kB Sector
  59. if (n >= 0x10) {
  60. n = 0x0E + (n >> 3); // 32kB Sector
  61. }
  62. return (n); // Sector Number
  63. }
  64. /*********************************************************************//**
  65. * @brief Prepare sector(s) for write operation
  66. *
  67. * @param[in] start_sec The number of start sector
  68. * @param[in] end_sec The number of end sector
  69. *
  70. * @return CMD_SUCCESS/BUSY/INVALID_SECTOR.
  71. *
  72. **********************************************************************/
  73. IAP_STATUS_CODE PrepareSector(uint32_t start_sec, uint32_t end_sec)
  74. {
  75. IAP_COMMAND_Type command;
  76. command.cmd = IAP_PREPARE; // Prepare Sector for Write
  77. command.param[0] = start_sec; // Start Sector
  78. command.param[1] = end_sec; // End Sector
  79. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  80. return (IAP_STATUS_CODE)command.status;
  81. }
  82. /*********************************************************************//**
  83. * @brief Copy RAM to Flash
  84. *
  85. * @param[in] dest destination buffer (in Flash memory).
  86. * @param[in] source source buffer (in RAM).
  87. * @param[in] size the write size.
  88. *
  89. * @return CMD_SUCCESS.
  90. * SRC_ADDR_ERROR/DST_ADDR_ERROR
  91. * SRC_ADDR_NOT_MAPPED/DST_ADDR_NOT_MAPPED
  92. * COUNT_ERROR/SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION
  93. * BUSY
  94. *
  95. **********************************************************************/
  96. IAP_STATUS_CODE CopyRAM2Flash(uint8_t * dest, uint8_t* source, IAP_WRITE_SIZE size)
  97. {
  98. uint32_t sec;
  99. IAP_STATUS_CODE status;
  100. IAP_COMMAND_Type command;
  101. // Prepare sectors
  102. sec = GetSecNum((uint32_t)dest);
  103. status = PrepareSector(sec, sec);
  104. if(status != CMD_SUCCESS)
  105. return status;
  106. // write
  107. command.cmd = IAP_COPY_RAM2FLASH; // Copy RAM to Flash
  108. command.param[0] = (uint32_t)dest; // Destination Flash Address
  109. command.param[1] = (uint32_t)source; // Source RAM Address
  110. command.param[2] = size; // Number of bytes
  111. command.param[3] = CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU) / 1000; // CCLK in kHz
  112. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  113. return (IAP_STATUS_CODE)command.status; // Finished without Errors
  114. }
  115. /*********************************************************************//**
  116. * @brief Erase sector(s)
  117. *
  118. * @param[in] start_sec The number of start sector
  119. * @param[in] end_sec The number of end sector
  120. *
  121. * @return CMD_SUCCESS.
  122. * INVALID_SECTOR
  123. * SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION
  124. * BUSY
  125. *
  126. **********************************************************************/
  127. IAP_STATUS_CODE EraseSector(uint32_t start_sec, uint32_t end_sec)
  128. {
  129. IAP_COMMAND_Type command;
  130. IAP_STATUS_CODE status;
  131. // Prepare sectors
  132. status = PrepareSector(start_sec, end_sec);
  133. if(status != CMD_SUCCESS)
  134. return status;
  135. // Erase sectors
  136. command.cmd = IAP_ERASE; // Prepare Sector for Write
  137. command.param[0] = start_sec; // Start Sector
  138. command.param[1] = end_sec; // End Sector
  139. command.param[2] = CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU) / 1000; // CCLK in kHz
  140. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  141. return (IAP_STATUS_CODE)command.status;
  142. }
  143. /*********************************************************************//**
  144. * @brief Blank check sector(s)
  145. *
  146. * @param[in] start_sec The number of start sector
  147. * @param[in] end_sec The number of end sector
  148. * @param[out] first_nblank_loc The offset of the first non-blank word
  149. * @param[out] first_nblank_val The value of the first non-blank word
  150. *
  151. * @return CMD_SUCCESS.
  152. * INVALID_SECTOR
  153. * SECTOR_NOT_BLANK
  154. * BUSY
  155. *
  156. **********************************************************************/
  157. IAP_STATUS_CODE BlankCheckSector(uint32_t start_sec, uint32_t end_sec,
  158. uint32_t *first_nblank_loc,
  159. uint32_t *first_nblank_val)
  160. {
  161. IAP_COMMAND_Type command;
  162. command.cmd = IAP_BLANK_CHECK; // Prepare Sector for Write
  163. command.param[0] = start_sec; // Start Sector
  164. command.param[1] = end_sec; // End Sector
  165. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  166. if(command.status == SECTOR_NOT_BLANK)
  167. {
  168. // Update out value
  169. if(first_nblank_loc != NULL)
  170. *first_nblank_loc = command.result[0];
  171. if(first_nblank_val != NULL)
  172. *first_nblank_val = command.result[1];
  173. }
  174. return (IAP_STATUS_CODE)command.status;
  175. }
  176. /*********************************************************************//**
  177. * @brief Read part identification number
  178. *
  179. * @param[out] partID Part ID
  180. *
  181. * @return CMD_SUCCESS
  182. *
  183. **********************************************************************/
  184. IAP_STATUS_CODE ReadPartID(uint32_t *partID)
  185. {
  186. IAP_COMMAND_Type command;
  187. command.cmd = IAP_READ_PART_ID;
  188. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  189. if(command.status == CMD_SUCCESS)
  190. {
  191. if(partID != NULL)
  192. *partID = command.result[0];
  193. }
  194. return (IAP_STATUS_CODE)command.status;
  195. }
  196. /*********************************************************************//**
  197. * @brief Read boot code version. The version is interpreted as <major>.<minor>.
  198. *
  199. * @param[out] major The major
  200. * @param[out] minor The minor
  201. *
  202. * @return CMD_SUCCESS
  203. *
  204. **********************************************************************/
  205. IAP_STATUS_CODE ReadBootCodeVer(uint8_t *major, uint8_t* minor)
  206. {
  207. IAP_COMMAND_Type command;
  208. command.cmd = IAP_READ_BOOT_VER;
  209. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  210. if(command.status == CMD_SUCCESS)
  211. {
  212. if(major != NULL)
  213. *major = (command.result[0] >> 8) & 0xFF;
  214. if(minor != NULL)
  215. *minor = (command.result[0]) & 0xFF;
  216. }
  217. return (IAP_STATUS_CODE)command.status;
  218. }
  219. /*********************************************************************//**
  220. * @brief Read Device serial number.
  221. *
  222. * @param[out] uid Serial number.
  223. *
  224. * @return CMD_SUCCESS
  225. *
  226. **********************************************************************/
  227. IAP_STATUS_CODE ReadDeviceSerialNum(uint32_t *uid)
  228. {
  229. IAP_COMMAND_Type command;
  230. command.cmd = IAP_READ_SERIAL_NUMBER;
  231. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  232. if(command.status == CMD_SUCCESS)
  233. {
  234. if(uid != NULL)
  235. {
  236. uint32_t i = 0;
  237. for(i = 0; i < 4; i++)
  238. uid[i] = command.result[i];
  239. }
  240. }
  241. return (IAP_STATUS_CODE)command.status;
  242. }
  243. /*********************************************************************//**
  244. * @brief compare the memory contents at two locations.
  245. *
  246. * @param[in] addr1 The address of the 1st buffer (in RAM/Flash).
  247. * @param[in] addr2 The address of the 2nd buffer (in RAM/Flash).
  248. * @param[in] size Number of bytes to be compared; should be a multiple of 4.
  249. *
  250. * @return CMD_SUCCESS
  251. * COMPARE_ERROR
  252. * COUNT_ERROR (Byte count is not a multiple of 4)
  253. * ADDR_ERROR
  254. * ADDR_NOT_MAPPED
  255. *
  256. **********************************************************************/
  257. IAP_STATUS_CODE Compare(uint8_t *addr1, uint8_t *addr2, uint32_t size)
  258. {
  259. IAP_COMMAND_Type command;
  260. command.cmd = IAP_COMPARE;
  261. command.param[0] = (uint32_t)addr1;
  262. command.param[1] = (uint32_t)addr2;
  263. command.param[2] = size;
  264. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  265. return (IAP_STATUS_CODE)command.status;
  266. }
  267. /*********************************************************************//**
  268. * @brief Re-invoke ISP.
  269. *
  270. * @param[in] None.
  271. *
  272. * @return None.
  273. *
  274. **********************************************************************/
  275. void InvokeISP(void)
  276. {
  277. IAP_COMMAND_Type command;
  278. command.cmd = IAP_REINVOKE_ISP;
  279. IAP_Call (&command.cmd, &command.status); // Call IAP Command
  280. }
  281. /**
  282. * @}
  283. */
  284. #endif /*_IAP*/