flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /******************************************************************************
  2. ******************************************************************************
  3. *
  4. * @file flash.c
  5. *
  6. * @brief application entry point which performs application specific tasks.
  7. *
  8. *******************************************************************************
  9. *
  10. * provide a demo for how to initialize the NV32, output messages via SCI,
  11. * flash operations, etc.
  12. * NOTE:
  13. * printf call may occupy a lot of memory (around 1924 bytes), so please
  14. * consider your code size before using printf.
  15. ******************************************************************************
  16. *
  17. * provide FLASH driver
  18. *
  19. ******************************************************************************/
  20. #include "flash.h"
  21. /******************************************************************************
  22. * Global variables
  23. ******************************************************************************/
  24. /******************************************************************************
  25. * Constants and macros
  26. ******************************************************************************/
  27. /******************************************************************************
  28. * Local types
  29. ******************************************************************************/
  30. /******************************************************************************
  31. * Local function prototypes
  32. ******************************************************************************/
  33. /******************************************************************************
  34. * Local variables
  35. ******************************************************************************/
  36. /******************************************************************************
  37. * Local functions
  38. ******************************************************************************/
  39. /******************************************************************************
  40. * Global functions
  41. ******************************************************************************/
  42. /*****************************************************************************//*!
  43. +FUNCTION----------------------------------------------------------------
  44. * @function name: Flash_CopyInRAM
  45. *
  46. * @brief This section of the code is the one that copies the routine into RAM.
  47. * It is following the steps documented in Technical Note 228
  48. *
  49. * @param
  50. *
  51. * @return none
  52. *
  53. * @ Pass/ Fail criteria: none
  54. *****************************************************************************/
  55. #define FLASH_ENABLE_STALLING_FLASH_CONTROLLER
  56. /*****************************************************************************//*!
  57. +FUNCTION----------------------------------------------------------------
  58. * @function name: Flash_Init
  59. *
  60. * @brief initialize flash driver
  61. *
  62. * @param
  63. *
  64. * @return none
  65. *
  66. * @ Pass/ Fail criteria: none
  67. *****************************************************************************/
  68. uint16_t Flash_Init(void)
  69. {
  70. uint16_t err = FLASH_ERR_SUCCESS;
  71. uint32_t clkDIV = BUS_CLK_HZ/1000000L - 1;
  72. uint32_t Tpgs =(285 *(BUS_CLK_HZ/100))/1000000L; //update 2016.8.4 by ¹â½Å°å¤ÎGG
  73. uint32_t Tprog =(675*(BUS_CLK_HZ/100))/1000000L; //by ¹â½Å°å¤ÎGG
  74. // printf("Tpgs= %x \n" , Tpgs);
  75. // printf("Tprog= %x \n" , Tprog);
  76. EFMCR=(clkDIV<<24) + 0x00001103; //divide to 1M hz
  77. EFMETM0=(Tpgs<<16) + 0x00001194; //0x00281194; //
  78. EFMETM1=(Tprog<<16) + 0x000088B8; //
  79. // printf("EFMCR= %x \n" , EFMCR);
  80. // printf("EFMETM0= %x \n" , EFMETM0);
  81. // printf("EFMETM1= %x \n" , EFMETM1);
  82. return(err);
  83. }
  84. /*****************************************************************************//*!
  85. +FUNCTION----------------------------------------------------------------
  86. * @function name: FlashProgram
  87. *
  88. * @brief program flash routine, each program operation supports up to 2 longwords
  89. * programming
  90. *
  91. * @param
  92. *
  93. * @return none
  94. *
  95. * @ Pass/ Fail criteria: none
  96. *****************************************************************************/
  97. uint16_t Flash_Program(uint32_t wNVMTargetAddress, uint8_t *pData, uint16_t sizeBytes)
  98. {
  99. uint16_t err = FLASH_ERR_SUCCESS;
  100. uint16_t w2LongWordCount = sizeBytes>>3;
  101. uint8_t wLeftBytes = (sizeBytes & 0x07);
  102. uint16_t wLeftLongWords = wLeftBytes>>2;
  103. uint32_t wTargetAddress = wNVMTargetAddress;
  104. uint32_t dwData0,dwData1;
  105. uint32_t *pdwData = (uint32_t*)pData;
  106. int i;
  107. //printf("\n adr : 0x%x ,data = 0x%x\n",w2LongWordCount,wLeftLongWords );
  108. // Check address to see if it is aligned to 4 bytes
  109. // Global address [1:0] must be 00.
  110. if(wNVMTargetAddress & 0x03)
  111. {
  112. err = FLASH_ERR_INVALID_PARAM;
  113. return (err);
  114. }
  115. // Loop for the two longwords (8 bytes) programming
  116. for(i = 0; i < w2LongWordCount; i++)
  117. {
  118. dwData0 = *pdwData++;
  119. dwData1 = *pdwData++;
  120. err = Flash_Program2LongWords(wTargetAddress, dwData0, dwData1);
  121. if(err)
  122. {
  123. goto EndP;
  124. //break;
  125. }
  126. wTargetAddress += 8;
  127. }
  128. // Loop for the single longword (4 bytes) programming
  129. for(i = 0; i < wLeftLongWords; i++)
  130. {
  131. dwData0 = *pdwData++;
  132. //printf("\n adr : 0x%x ,data = 0x%x\n",i,dwData0 );
  133. err = Flash_Program1LongWord(wTargetAddress, dwData0);
  134. //printf("\n adr : 0x%x ,data = 0x%x\n",i,dwData0 );
  135. if(err)
  136. {
  137. goto EndP;
  138. //break;
  139. }
  140. wTargetAddress += 4;
  141. }
  142. wLeftBytes = (wLeftBytes-(wLeftLongWords<<2)); // calculate the # of bytes that are not programmed
  143. if(!wLeftBytes){
  144. return (err);
  145. }
  146. #if defined(BIG_ENDIAN)
  147. dwData0 = 0;
  148. pData = (uint8_t*)pdwData; // pointer to the left bytes
  149. for(i = wLeftBytes; i >0; i--)
  150. {
  151. dwData0 <<= 8;
  152. dwData0 |= *pData++; // MSB byte first
  153. }
  154. // Calculate how many bytes need to be filled with 0xFFs
  155. // in order to form a single longword for the left bytes of data
  156. wLeftBytes = 4 - wLeftBytes;
  157. //
  158. for(i = wLeftBytes; i >0; i--)
  159. {
  160. dwData0 <<= 8;
  161. dwData0 |= 0xFF; // MSB byte first
  162. }
  163. #else
  164. dwData0 = 0xFFFFFFFFL;
  165. pData = (uint8_t*)pdwData+wLeftBytes-1; // pointer to the left bytes
  166. for(i = wLeftBytes; i >0; i--)
  167. {
  168. dwData0 <<= 8;
  169. dwData0 |= *pData--; // MSB byte first
  170. }
  171. #endif
  172. // Now program the last longword
  173. err = Flash_Program1LongWord(wTargetAddress, dwData0);
  174. EndP:
  175. return (err);
  176. }
  177. uint16_t Flash_Program1LongWord(uint32_t wNVMTargetAddress, uint32_t dwData)
  178. {
  179. uint16_t err = FLASH_ERR_SUCCESS;
  180. // Check address to see if it is aligned to 4 bytes
  181. // Global address [1:0] must be 00.
  182. if(wNVMTargetAddress & 0x03)
  183. {
  184. err = FLASH_ERR_INVALID_PARAM;
  185. return (err);
  186. }
  187. // Clear error flags
  188. EFMCMD = FLASH_CMD_CLEAR;
  189. // Write index to specify the command code to be loaded
  190. M32(wNVMTargetAddress) = dwData;
  191. // Write command code and memory address bits[23:16]
  192. EFM_LaunchCMD(FLASH_CMD_PROGRAM);
  193. return (err);
  194. }
  195. uint16_t Flash_Program2LongWords(uint32_t wNVMTargetAddress, uint32_t dwData0, uint32_t dwData1)
  196. {
  197. uint16_t err = FLASH_ERR_SUCCESS;
  198. // Check address to see if it is aligned to 4 bytes
  199. // Global address [1:0] must be 00.
  200. if(wNVMTargetAddress & 0x03)
  201. {
  202. err = FLASH_ERR_INVALID_PARAM;
  203. return (err);
  204. }
  205. // Clear error flags
  206. EFMCMD = FLASH_CMD_CLEAR;
  207. // printf("\n write data adr : 0x%x ,data = 0x%x\n",dwData0,dwData1 );
  208. // Write index to specify the command code to be loaded
  209. M32(wNVMTargetAddress) = dwData0;
  210. // Write command code and memory address bits[23:16]
  211. EFM_LaunchCMD(FLASH_CMD_PROGRAM);
  212. wNVMTargetAddress = wNVMTargetAddress +4;
  213. // printf("\n write data adr : 0x%x ,data = 0x%x\n",wNVMTargetAddress,dwData1 );
  214. // Clear error flags
  215. EFMCMD = FLASH_CMD_CLEAR;
  216. // Write index to specify the command code to be loaded
  217. M32(wNVMTargetAddress) = dwData1;
  218. // Write command code and memory address bits[23:16]
  219. EFM_LaunchCMD(FLASH_CMD_PROGRAM);
  220. // printf("\n write data adr : 0x%x ,data = 0x%x\n",wNVMTargetAddress,dwData1 );
  221. return (err);
  222. }
  223. /*****************************************************************************//*!
  224. +FUNCTION----------------------------------------------------------------
  225. * @function name: Flash_EraseSector
  226. *
  227. * @brief erase flash sector, each flash sector is of 512 bytes long,
  228. * global address [1:0] = 00.
  229. *
  230. * @param
  231. *
  232. * @return none
  233. *
  234. * @ Pass/ Fail criteria: none
  235. *****************************************************************************/
  236. uint16_t Flash_EraseSector(uint32_t wNVMTargetAddress)
  237. {
  238. uint16_t err = FLASH_ERR_SUCCESS;
  239. // Check address to see if it is aligned to 4 bytes
  240. // Global address [1:0] must be 00.
  241. if(wNVMTargetAddress & 0x03)
  242. {
  243. err = FLASH_ERR_INVALID_PARAM;
  244. return (err);
  245. }
  246. // Clear error flags
  247. EFMCMD = FLASH_CMD_CLEAR;
  248. M32(wNVMTargetAddress) = 0xffffffff;
  249. EFM_LaunchCMD(FLASH_CMD_ERASE_SECTOR);
  250. return (err);
  251. }
  252. uint16_t Flash_VerifyBackdoorKey()
  253. {
  254. uint16_t err = FLASH_ERR_SUCCESS;
  255. // int i;
  256. // Clear error flags
  257. EFMCMD = FLASH_CMD_CLEAR;
  258. // Write index to specify the command code to be loaded
  259. Custombkd = FLASH_FACTORY_KEY;
  260. return (err);
  261. }
  262. /*****************************************************************************//*!
  263. +FUNCTION----------------------------------------------------------------
  264. * @function name: NVM_EraseAll
  265. *
  266. * @brief erase all block,both flash and EEPROM
  267. *
  268. * @param
  269. *
  270. * @return none
  271. *
  272. * @ Pass/ Fail criteria: none
  273. *****************************************************************************/
  274. uint16_t NVM_EraseAll(void)
  275. {
  276. uint16_t err = FLASH_ERR_SUCCESS;
  277. EFMCMD = FLASH_CMD_CLEAR;
  278. EFM_LaunchCMD(FLASH_CMD_ERASE_ALL);
  279. // Clear error flags
  280. return err;
  281. }
  282. /*****************************************************************************//*!
  283. +FUNCTION----------------------------------------------------------------
  284. * @function name: NVM_Unsecure
  285. *
  286. * @brief unsecure
  287. *
  288. * @param
  289. *
  290. * @return none
  291. *
  292. * @ Pass/ Fail criteria: none
  293. *****************************************************************************/
  294. uint16_t NVM_Unsecure(void)
  295. {
  296. uint16_t err = FLASH_ERR_SUCCESS;
  297. return err;
  298. }
  299. #ifdef IAR
  300. void __ramfunc EFM_LaunchCMD(uint32_t EFM_CMD)
  301. #else
  302. void EFM_LaunchCMD(uint32_t EFM_CMD)
  303. #endif
  304. {
  305. DisableInterrupts;
  306. if((EFMCMD&EFM_DONE_MASK)== EFM_STATUS_READY)
  307. {
  308. EFMCMD = EFM_CMD;
  309. }
  310. while(1)
  311. {
  312. if((EFMCMD&EFM_DONE_MASK) == EFM_STATUS_DONE) break;
  313. }
  314. EnableInterrupts;
  315. }