ciu32f003_std_flash.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /************************************************************************************************/
  2. /**
  3. * @file ciu32f003_std_flash.c
  4. * @author MCU Ecosystem Development Team
  5. * @brief FLASH STD库驱动。
  6. * 实现FLASH擦除、编程API。
  7. *
  8. *
  9. **************************************************************************************************
  10. * @attention
  11. * Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
  12. *
  13. **************************************************************************************************
  14. */
  15. /************************************************************************************************/
  16. /**
  17. * @addtogroup CIU32F003_STD_Driver
  18. * @{
  19. */
  20. /**
  21. * @addtogroup FLASH
  22. * @{
  23. *
  24. */
  25. /************************************************************************************************/
  26. /*------------------------------------------includes--------------------------------------------*/
  27. #include "ciu32f003_std.h"
  28. #ifdef STD_FLASH_PERIPHERAL_USED
  29. /*-------------------------------------------functions------------------------------------------*/
  30. /************************************************************************************************/
  31. /**
  32. * @addtogroup FLASH_External_Functions
  33. * @{
  34. *
  35. */
  36. /************************************************************************************************/
  37. /**
  38. * @brief Flash擦除与Option byte区擦除
  39. * @param mode 擦除模式
  40. * @arg FLASH_MODE_PAGE_ERASE
  41. * @arg FLASH_MODE_MASS_ERASE
  42. * @param address 擦除访问地址
  43. * @note user flash区擦除时,需先调std_flash_unlock(),解锁flash
  44. * @note Option Byte区擦除时,需先调用std_flash_opt_unlock(),解锁选项字节
  45. * @retval std_status_t API执行结果
  46. */
  47. std_status_t std_flash_erase(uint32_t mode, uint32_t address)
  48. {
  49. std_status_t status = STD_OK;
  50. /* 设置擦除模式 */
  51. std_flash_set_operate_mode(mode);
  52. /* 执行擦除 */
  53. *(uint32_t *)address = 0xFFFFFFFF;
  54. /* 等待擦除完成,查询异常标志位 */
  55. while (std_flash_get_flag(FLASH_FLAG_BSY));
  56. if ((FLASH->SR & FLASH_FLAG_WRPERR) != 0x00000000U)
  57. {
  58. status = STD_ERR;
  59. }
  60. /* 清除所有标志 */
  61. std_flash_clear_flag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
  62. /* 退出擦除模式 */
  63. std_flash_set_operate_mode(FLASH_MODE_IDLE);
  64. return (status);
  65. }
  66. /**
  67. * @brief User Flash区与Option Byte区字编程
  68. * @param address 编程地址
  69. * @param prog_data 编程数据(4字节)
  70. * @note user flash区编程时,需先调std_flash_unlock(),解锁flash
  71. * @note Option Byte区字编程时,需先调用std_flash_opt_unlock(),解锁选项字节
  72. * @retval std_status_t API执行结果
  73. */
  74. std_status_t std_flash_word_program(uint32_t address, uint32_t prog_data)
  75. {
  76. std_status_t status = STD_OK;
  77. /* 进入编程模式 */
  78. std_flash_set_operate_mode(FLASH_MODE_PROGRAM);
  79. /* 向目标地址写入数据 */
  80. *(uint32_t *)address = prog_data;
  81. /* 等待编程完成,查询异常标志位 */
  82. while (std_flash_get_flag(FLASH_FLAG_BSY));
  83. if ((FLASH->SR & FLASH_FLAG_WRPERR) != 0x00000000U)
  84. {
  85. status = STD_ERR;
  86. }
  87. if (status == STD_OK)
  88. {
  89. /* 检查编程数据是否正确 */
  90. if(*((__IO uint32_t *)address) != prog_data)
  91. {
  92. status = STD_ERR;
  93. }
  94. }
  95. /* 清除所有标志 */
  96. std_flash_clear_flag(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR);
  97. /* 退出编程模式 */
  98. std_flash_set_operate_mode(FLASH_MODE_IDLE);
  99. return (status);
  100. }
  101. /**
  102. * @}
  103. */
  104. #endif /* STD_FLASH_PERIPHERAL_USED */
  105. /**
  106. * @}
  107. */
  108. /**
  109. * @}
  110. */