iflash.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. #include "iflash.h"
  2. enum {RESET = 0, SET = !RESET};
  3. #define __IO volatile
  4. /* Base address of the Flash sectors */
  5. #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 Kbytes */
  6. #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 Kbytes */
  7. #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 Kbytes */
  8. #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 Kbytes */
  9. #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 Kbytes */
  10. #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 Kbytes */
  11. #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 Kbytes */
  12. #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 Kbytes */
  13. #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base address of Sector 8, 128 Kbytes */
  14. #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 Kbytes */
  15. #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 Kbytes */
  16. #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 Kbytes */
  17. #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base address of Sector 12, 16 Kbytes */
  18. #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base address of Sector 13, 16 Kbytes */
  19. #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base address of Sector 14, 16 Kbytes */
  20. #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base address of Sector 15, 16 Kbytes */
  21. #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base address of Sector 16, 64 Kbytes */
  22. #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base address of Sector 17, 128 Kbytes */
  23. #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base address of Sector 18, 128 Kbytes */
  24. #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base address of Sector 19, 128 Kbytes */
  25. #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base address of Sector 20, 128 Kbytes */
  26. #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base address of Sector 21, 128 Kbytes */
  27. #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base address of Sector 22, 128 Kbytes */
  28. #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base address of Sector 23, 128 Kbytes */
  29. #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
  30. /** @defgroup FLASH_Flags
  31. * @{
  32. */
  33. #define FLASH_FLAG_EOP ((uint32_t)0x00000001) /*!< FLASH End of Operation flag */
  34. #define FLASH_FLAG_OPERR ((uint32_t)0x00000002) /*!< FLASH operation Error flag */
  35. #define FLASH_FLAG_WRPERR ((uint32_t)0x00000010) /*!< FLASH Write protected error flag */
  36. #define FLASH_FLAG_PGAERR ((uint32_t)0x00000020) /*!< FLASH Programming Alignment error flag */
  37. #define FLASH_FLAG_PGPERR ((uint32_t)0x00000040) /*!< FLASH Programming Parallelism error flag */
  38. #define FLASH_FLAG_PGSERR ((uint32_t)0x00000080) /*!< FLASH Programming Sequence error flag */
  39. #define FLASH_FLAG_RDERR ((uint32_t)0x00000100) /*!< Read Protection error flag (PCROP) */
  40. #define FLASH_FLAG_BSY ((uint32_t)0x00010000) /*!< FLASH Busy flag */
  41. /** @defgroup FLASH_Sectors
  42. * @{
  43. */
  44. #define FLASH_Sector_0 ((uint16_t)0x0000) /*!< Sector Number 0 */
  45. #define FLASH_Sector_1 ((uint16_t)0x0008) /*!< Sector Number 1 */
  46. #define FLASH_Sector_2 ((uint16_t)0x0010) /*!< Sector Number 2 */
  47. #define FLASH_Sector_3 ((uint16_t)0x0018) /*!< Sector Number 3 */
  48. #define FLASH_Sector_4 ((uint16_t)0x0020) /*!< Sector Number 4 */
  49. #define FLASH_Sector_5 ((uint16_t)0x0028) /*!< Sector Number 5 */
  50. #define FLASH_Sector_6 ((uint16_t)0x0030) /*!< Sector Number 6 */
  51. #define FLASH_Sector_7 ((uint16_t)0x0038) /*!< Sector Number 7 */
  52. #define FLASH_Sector_8 ((uint16_t)0x0040) /*!< Sector Number 8 */
  53. #define FLASH_Sector_9 ((uint16_t)0x0048) /*!< Sector Number 9 */
  54. #define FLASH_Sector_10 ((uint16_t)0x0050) /*!< Sector Number 10 */
  55. #define FLASH_Sector_11 ((uint16_t)0x0058) /*!< Sector Number 11 */
  56. #define FLASH_Sector_12 ((uint16_t)0x0080) /*!< Sector Number 12 */
  57. #define FLASH_Sector_13 ((uint16_t)0x0088) /*!< Sector Number 13 */
  58. #define FLASH_Sector_14 ((uint16_t)0x0090) /*!< Sector Number 14 */
  59. #define FLASH_Sector_15 ((uint16_t)0x0098) /*!< Sector Number 15 */
  60. #define FLASH_Sector_16 ((uint16_t)0x00A0) /*!< Sector Number 16 */
  61. #define FLASH_Sector_17 ((uint16_t)0x00A8) /*!< Sector Number 17 */
  62. #define FLASH_Sector_18 ((uint16_t)0x00B0) /*!< Sector Number 18 */
  63. #define FLASH_Sector_19 ((uint16_t)0x00B8) /*!< Sector Number 19 */
  64. #define FLASH_Sector_20 ((uint16_t)0x00C0) /*!< Sector Number 20 */
  65. #define FLASH_Sector_21 ((uint16_t)0x00C8) /*!< Sector Number 21 */
  66. #define FLASH_Sector_22 ((uint16_t)0x00D0) /*!< Sector Number 22 */
  67. #define FLASH_Sector_23 ((uint16_t)0x00D8) /*!< Sector Number 23 */
  68. /**
  69. * @brief FLASH Status
  70. */
  71. typedef enum
  72. {
  73. FLASH_BUSY = 1,
  74. FLASH_ERROR_RD,
  75. FLASH_ERROR_PGS,
  76. FLASH_ERROR_PGP,
  77. FLASH_ERROR_PGA,
  78. FLASH_ERROR_WRP,
  79. FLASH_ERROR_PROGRAM,
  80. FLASH_ERROR_OPERATION,
  81. FLASH_COMPLETE
  82. }FLASH_Status;
  83. /** @defgroup FLASH_Voltage_Range
  84. * @{
  85. */
  86. #define VoltageRange_1 ((uint8_t)0x00) /*!< Device operating range: 1.8V to 2.1V */
  87. #define VoltageRange_2 ((uint8_t)0x01) /*!<Device operating range: 2.1V to 2.7V */
  88. #define VoltageRange_3 ((uint8_t)0x02) /*!<Device operating range: 2.7V to 3.6V */
  89. #define VoltageRange_4 ((uint8_t)0x03) /*!<Device operating range: 2.7V to 3.6V + External Vpp */
  90. /** @defgroup FLASH_Program_Parallelism
  91. * @{
  92. */
  93. #define FLASH_PSIZE_BYTE ((uint32_t)0x00000000)
  94. #define FLASH_PSIZE_HALF_WORD ((uint32_t)0x00000100)
  95. #define FLASH_PSIZE_WORD ((uint32_t)0x00000200)
  96. #define FLASH_PSIZE_DOUBLE_WORD ((uint32_t)0x00000300)
  97. #define CR_PSIZE_MASK ((uint32_t)0xFFFFFCFF)
  98. #define PERIPH_BASE ((uint32_t)0x40000000)
  99. #define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)
  100. #define FLASH_R_BASE (AHB1PERIPH_BASE + 0x3C00)
  101. /** @defgroup FLASH_Keys
  102. * @{
  103. */
  104. #define FLASH_KEY1 ((uint32_t)0x45670123)
  105. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  106. typedef struct
  107. {
  108. __IO uint32_t ACR; /*!< FLASH access control register, Address offset: 0x00 */
  109. __IO uint32_t KEYR; /*!< FLASH key register, Address offset: 0x04 */
  110. __IO uint32_t OPTKEYR; /*!< FLASH option key register, Address offset: 0x08 */
  111. __IO uint32_t SR; /*!< FLASH status register, Address offset: 0x0C */
  112. __IO uint32_t CR; /*!< FLASH control register, Address offset: 0x10 */
  113. __IO uint32_t OPTCR; /*!< FLASH option control register , Address offset: 0x14 */
  114. __IO uint32_t OPTCR1; /*!< FLASH option control register 1, Address offset: 0x18 */
  115. } FLASH_TypeDef;
  116. #define FLASH ((FLASH_TypeDef *) FLASH_R_BASE)
  117. /******************* Bits definition for FLASH_CR register ******************/
  118. #define FLASH_CR_PG ((uint32_t)0x00000001)
  119. #define FLASH_CR_SER ((uint32_t)0x00000002)
  120. #define FLASH_CR_STRT ((uint32_t)0x00010000)
  121. #define FLASH_CR_LOCK ((uint32_t)0x80000000)
  122. static uint32_t GetSector(uint32_t Address);
  123. void FLASH_Unlock(void);
  124. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange);
  125. FLASH_Status FLASH_WaitForLastOperation(void);
  126. FLASH_Status FLASH_GetStatus(void);
  127. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  128. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
  129. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data);
  130. void FLASH_Lock(void);
  131. /*准备写入的测试数据*/
  132. /* Exported types ------------------------------------------------------------*/
  133. /* Exported constants --------------------------------------------------------*/
  134. /* 要擦除内部FLASH的起始地址(含) */
  135. #define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_7
  136. /* 要擦除内部FLASH的结束地址(不含) */
  137. #define FLASH_USER_END_ADDR ADDR_FLASH_SECTOR_8
  138. /**
  139. * @brief InternalFlash_Test,对内部FLASH进行读写测试
  140. * @param None
  141. * @retval None
  142. */
  143. int InternalFlash_WriteBuf(const uint8_t* data, uint32_t length)
  144. {
  145. /*要擦除的起始扇区(包含)及结束扇区(不包含),如8-12,表示擦除8、9、10、11扇区*/
  146. uint32_t uwStartSector = 0;
  147. uint32_t uwEndSector = 0;
  148. uint32_t uwAddress = 0;
  149. uint32_t uwSectorCounter = 0;
  150. // __IO uint32_t uwData32 = 0;
  151. __IO uint32_t uwMemoryProgramStatus = 0;
  152. __IO uint32_t rr=0;
  153. uint8_t* pbuf = data;
  154. //pbuf = (uint8_t*)malloc(length*sizeof(uint8_t));
  155. static uint32_t i = 0;
  156. /* FLASH 解锁 ********************************/
  157. /* 使能访问FLASH控制寄存器 */
  158. FLASH_Unlock();
  159. /* 擦除用户区域 (用户区域指程序本身没有使用的空间,可以自定义)**/
  160. /* 清除各种FLASH的标志位 */
  161. // FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  162. // FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
  163. *((volatile uint32_t *)0x40023c0C) = (((uint32_t)0x00000001) | ((uint32_t)0x00000002) | ((uint32_t)0x00000010) | \
  164. ((uint32_t)0x00000020) | ((uint32_t)0x00000040) | ((uint32_t)0x00000080));
  165. uwStartSector = GetSector(FLASH_USER_START_ADDR);
  166. uwEndSector = GetSector(FLASH_USER_END_ADDR);
  167. /* 开始擦除操作 */
  168. uwSectorCounter = uwStartSector;
  169. while (uwSectorCounter <= uwEndSector)
  170. {
  171. // VoltageRange_3 以“字”的大小进行操作
  172. if (FLASH_EraseSector(uwSectorCounter, VoltageRange_3) != FLASH_COMPLETE)
  173. {
  174. /*擦除出错,返回,实际应用中可加入处理 */
  175. return -1;
  176. }
  177. /* 计数器指向下一个扇区 */
  178. if (uwSectorCounter == FLASH_Sector_11)
  179. {
  180. uwSectorCounter += 40;
  181. }
  182. else
  183. {
  184. uwSectorCounter += 8;
  185. }
  186. }
  187. /* 以“字节”的大小为单位写入数据 ********************************/
  188. uwAddress = FLASH_USER_START_ADDR;
  189. for (i=0;i < length;i++)
  190. {
  191. rr = pbuf[i];
  192. if (FLASH_ProgramByte(uwAddress, rr) == FLASH_COMPLETE)
  193. {
  194. uwAddress = uwAddress + 1;
  195. }
  196. else
  197. {
  198. /*写入出错,返回,实际应用中可加入处理 */
  199. break;
  200. }
  201. }
  202. /* 给FLASH上锁,防止内容被篡改*/
  203. FLASH_Lock();
  204. }
  205. /**
  206. * @brief 根据输入的地址给出它所在的sector
  207. * 例如:
  208. uwStartSector = GetSector(FLASH_USER_START_ADDR);
  209. uwEndSector = GetSector(FLASH_USER_END_ADDR);
  210. * @param Address:地址
  211. * @retval 地址所在的sector
  212. */
  213. static uint32_t GetSector(uint32_t Address)
  214. {
  215. uint32_t sector = 0;
  216. if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
  217. {
  218. sector = FLASH_Sector_0;
  219. }
  220. else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
  221. {
  222. sector = FLASH_Sector_1;
  223. }
  224. else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
  225. {
  226. sector = FLASH_Sector_2;
  227. }
  228. else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
  229. {
  230. sector = FLASH_Sector_3;
  231. }
  232. else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
  233. {
  234. sector = FLASH_Sector_4;
  235. }
  236. else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
  237. {
  238. sector = FLASH_Sector_5;
  239. }
  240. else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
  241. {
  242. sector = FLASH_Sector_6;
  243. }
  244. else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
  245. {
  246. sector = FLASH_Sector_7;
  247. }
  248. else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  249. {
  250. sector = FLASH_Sector_8;
  251. }
  252. else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  253. {
  254. sector = FLASH_Sector_9;
  255. }
  256. else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  257. {
  258. sector = FLASH_Sector_10;
  259. }
  260. else if((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11))
  261. {
  262. sector = FLASH_Sector_11;
  263. }
  264. else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
  265. {
  266. sector = FLASH_Sector_12;
  267. }
  268. else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
  269. {
  270. sector = FLASH_Sector_13;
  271. }
  272. else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
  273. {
  274. sector = FLASH_Sector_14;
  275. }
  276. else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
  277. {
  278. sector = FLASH_Sector_15;
  279. }
  280. else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
  281. {
  282. sector = FLASH_Sector_16;
  283. }
  284. else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
  285. {
  286. sector = FLASH_Sector_17;
  287. }
  288. else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
  289. {
  290. sector = FLASH_Sector_18;
  291. }
  292. else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
  293. {
  294. sector = FLASH_Sector_19;
  295. }
  296. else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
  297. {
  298. sector = FLASH_Sector_20;
  299. }
  300. else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
  301. {
  302. sector = FLASH_Sector_21;
  303. }
  304. else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
  305. {
  306. sector = FLASH_Sector_22;
  307. }
  308. else/*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_23))*/
  309. {
  310. sector = FLASH_Sector_23;
  311. }
  312. return sector;
  313. }
  314. void FLASH_Unlock(void)
  315. {
  316. if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  317. {
  318. /* Authorize the FLASH Registers access */
  319. FLASH->KEYR = FLASH_KEY1;
  320. FLASH->KEYR = FLASH_KEY2;
  321. }
  322. }
  323. FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
  324. {
  325. uint32_t tmp_psize = 0x0;
  326. FLASH_Status status = FLASH_COMPLETE;
  327. // /* Check the parameters */
  328. // assert_param(IS_FLASH_SECTOR(FLASH_Sector));
  329. // assert_param(IS_VOLTAGERANGE(VoltageRange));
  330. if(VoltageRange == VoltageRange_1)
  331. {
  332. tmp_psize = FLASH_PSIZE_BYTE;
  333. }
  334. else if(VoltageRange == VoltageRange_2)
  335. {
  336. tmp_psize = FLASH_PSIZE_HALF_WORD;
  337. }
  338. else if(VoltageRange == VoltageRange_3)
  339. {
  340. tmp_psize = FLASH_PSIZE_WORD;
  341. }
  342. else
  343. {
  344. tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
  345. }
  346. /* Wait for last operation to be completed */
  347. status = FLASH_WaitForLastOperation();
  348. if(status == FLASH_COMPLETE)
  349. {
  350. /* if the previous operation is completed, proceed to erase the sector */
  351. FLASH->CR &= CR_PSIZE_MASK;
  352. FLASH->CR |= tmp_psize;
  353. FLASH->CR &= SECTOR_MASK;
  354. FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
  355. FLASH->CR |= FLASH_CR_STRT;
  356. /* Wait for last operation to be completed */
  357. status = FLASH_WaitForLastOperation();
  358. /* if the erase operation is completed, disable the SER Bit */
  359. FLASH->CR &= (~FLASH_CR_SER);
  360. FLASH->CR &= SECTOR_MASK;
  361. }
  362. /* Return the Erase Status */
  363. return status;
  364. }
  365. FLASH_Status FLASH_WaitForLastOperation(void)
  366. {
  367. __IO FLASH_Status status = FLASH_COMPLETE;
  368. /* Check for the FLASH Status */
  369. status = FLASH_GetStatus();
  370. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  371. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  372. flag will be set */
  373. while(status == FLASH_BUSY)
  374. {
  375. status = FLASH_GetStatus();
  376. }
  377. /* Return the operation status */
  378. return status;
  379. }
  380. FLASH_Status FLASH_GetStatus(void)
  381. {
  382. FLASH_Status flashstatus = FLASH_COMPLETE;
  383. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  384. {
  385. flashstatus = FLASH_BUSY;
  386. }
  387. else
  388. {
  389. if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)
  390. {
  391. flashstatus = FLASH_ERROR_WRP;
  392. }
  393. else
  394. {
  395. if((FLASH->SR & FLASH_FLAG_RDERR) != (uint32_t)0x00)
  396. {
  397. flashstatus = FLASH_ERROR_RD;
  398. }
  399. else
  400. {
  401. if((FLASH->SR & (uint32_t)0xE0) != (uint32_t)0x00)
  402. {
  403. flashstatus = FLASH_ERROR_PROGRAM;
  404. }
  405. else
  406. {
  407. if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)
  408. {
  409. flashstatus = FLASH_ERROR_OPERATION;
  410. }
  411. else
  412. {
  413. flashstatus = FLASH_COMPLETE;
  414. }
  415. }
  416. }
  417. }
  418. }
  419. /* Return the FLASH Status */
  420. return flashstatus;
  421. }
  422. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  423. {
  424. FLASH_Status status = FLASH_COMPLETE;
  425. // /* Check the parameters */
  426. // assert_param(IS_FLASH_ADDRESS(Address));
  427. /* Wait for last operation to be completed */
  428. status = FLASH_WaitForLastOperation();
  429. if(status == FLASH_COMPLETE)
  430. {
  431. /* if the previous operation is completed, proceed to program the new data */
  432. FLASH->CR &= CR_PSIZE_MASK;
  433. FLASH->CR |= FLASH_PSIZE_WORD;
  434. FLASH->CR |= FLASH_CR_PG;
  435. *(__IO uint32_t*)Address = Data;
  436. /* Wait for last operation to be completed */
  437. status = FLASH_WaitForLastOperation();
  438. /* if the program operation is completed, disable the PG Bit */
  439. FLASH->CR &= (~FLASH_CR_PG);
  440. }
  441. /* Return the Program Status */
  442. return status;
  443. }
  444. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  445. {
  446. FLASH_Status status = FLASH_COMPLETE;
  447. // /* Check the parameters */
  448. // assert_param(IS_FLASH_ADDRESS(Address));
  449. /* Wait for last operation to be completed */
  450. status = FLASH_WaitForLastOperation();
  451. if(status == FLASH_COMPLETE)
  452. {
  453. /* if the previous operation is completed, proceed to program the new data */
  454. FLASH->CR &= CR_PSIZE_MASK;
  455. FLASH->CR |= FLASH_PSIZE_HALF_WORD;
  456. FLASH->CR |= FLASH_CR_PG;
  457. *(__IO uint16_t*)Address = Data;
  458. /* Wait for last operation to be completed */
  459. status = FLASH_WaitForLastOperation();
  460. /* if the program operation is completed, disable the PG Bit */
  461. FLASH->CR &= (~FLASH_CR_PG);
  462. }
  463. /* Return the Program Status */
  464. return status;
  465. }
  466. FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
  467. {
  468. FLASH_Status status = FLASH_COMPLETE;
  469. // /* Check the parameters */
  470. // assert_param(IS_FLASH_ADDRESS(Address));
  471. /* Wait for last operation to be completed */
  472. status = FLASH_WaitForLastOperation();
  473. if(status == FLASH_COMPLETE)
  474. {
  475. /* if the previous operation is completed, proceed to program the new data */
  476. FLASH->CR &= CR_PSIZE_MASK;
  477. FLASH->CR |= FLASH_PSIZE_BYTE;
  478. FLASH->CR |= FLASH_CR_PG;
  479. *(__IO uint8_t*)Address = Data;
  480. /* Wait for last operation to be completed */
  481. status = FLASH_WaitForLastOperation();
  482. /* if the program operation is completed, disable the PG Bit */
  483. FLASH->CR &= (~FLASH_CR_PG);
  484. }
  485. /* Return the Program Status */
  486. return status;
  487. }
  488. void FLASH_Lock(void)
  489. {
  490. /* Set the LOCK Bit to lock the FLASH Registers access */
  491. FLASH->CR |= FLASH_CR_LOCK;
  492. }