main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*!
  2. \file main.c
  3. \brief main flash program, erase and reprogram
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "main.h"
  11. #include "gd32f3x0_eval.h"
  12. #define FMC_PAGE_SIZE ((uint16_t)0x400U)
  13. #define FMC_WRITE_START_ADDR ((uint32_t)0x08004000U)
  14. #define FMC_WRITE_END_ADDR ((uint32_t)0x08004800U)
  15. uint32_t *ptrd;
  16. uint32_t address = 0x00U;
  17. uint32_t data0 = 0x01234567U;
  18. uint32_t data1 = 0xd583179bU;
  19. led_typedef_enum lednum = LED4;
  20. /* calculate the number of page to be programmed/erased */
  21. uint32_t PageNum = (FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) / FMC_PAGE_SIZE;
  22. /* calculate the number of page to be programmed/erased */
  23. uint32_t WordNum = ((FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) >> 2);
  24. /*!
  25. \brief erase fmc pages from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
  26. \param[in] none
  27. \param[out] none
  28. \retval none
  29. */
  30. void fmc_erase_pages(void)
  31. {
  32. uint32_t EraseCounter;
  33. /* unlock the flash program/erase controller */
  34. fmc_unlock();
  35. /* clear all pending flags */
  36. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  37. /* erase the flash pages */
  38. for(EraseCounter = 0U; EraseCounter < PageNum; EraseCounter++){
  39. fmc_page_erase(FMC_WRITE_START_ADDR + (FMC_PAGE_SIZE * EraseCounter));
  40. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  41. }
  42. /* lock the main FMC after the erase operation */
  43. fmc_lock();
  44. }
  45. /*!
  46. \brief program fmc word by word from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. void fmc_program(void)
  52. {
  53. /* unlock the flash program/erase controller */
  54. fmc_unlock();
  55. address = FMC_WRITE_START_ADDR;
  56. /* program flash */
  57. while(address < FMC_WRITE_END_ADDR){
  58. fmc_word_program(address, data0);
  59. address += 4U;
  60. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  61. }
  62. /* lock the main FMC after the program operation */
  63. fmc_lock();
  64. }
  65. /*!
  66. \brief reprogram fmc word by word from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
  67. \param[in] none
  68. \param[out] none
  69. \retval none
  70. */
  71. void fmc_reprogram(void)
  72. {
  73. /* unlock the flash program/erase controller */
  74. fmc_unlock();
  75. address = FMC_WRITE_START_ADDR;
  76. /* reprogram the previous program address */
  77. while(address < FMC_WRITE_END_ADDR){
  78. fmc_word_reprogram(address, data1);
  79. address += 4U;
  80. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  81. }
  82. /* lock the main FMC after the reprogram operation */
  83. fmc_lock();
  84. }
  85. /*!
  86. \brief check fmc erase result
  87. \param[in] none
  88. \param[out] none
  89. \retval none
  90. */
  91. void fmc_erase_pages_check(void)
  92. {
  93. uint32_t i;
  94. ptrd = (uint32_t *)FMC_WRITE_START_ADDR;
  95. /* check flash whether has been erased */
  96. for(i = 0U; i < WordNum; i++){
  97. if(0xFFFFFFFFU != (*ptrd)){
  98. lednum = LED1;
  99. gd_eval_led_on(lednum);
  100. break;
  101. }else{
  102. ptrd++;
  103. }
  104. }
  105. }
  106. /*!
  107. \brief check fmc program result
  108. \param[in] none
  109. \param[out] none
  110. \retval none
  111. */
  112. void fmc_program_check(void)
  113. {
  114. uint32_t i;
  115. ptrd = (uint32_t *)FMC_WRITE_START_ADDR;
  116. /* check flash whether has been programmed */
  117. for(i = 0U; i < WordNum; i++){
  118. if((*ptrd) != data0){
  119. lednum = LED2;
  120. gd_eval_led_on(lednum);
  121. break;
  122. }else{
  123. ptrd++;
  124. }
  125. }
  126. }
  127. /*!
  128. \brief check fmc reprogram result
  129. \param[in] none
  130. \param[out] none
  131. \retval none
  132. */
  133. void fmc_reprogram_check(void)
  134. {
  135. uint32_t i;
  136. ptrd = (uint32_t *)FMC_WRITE_START_ADDR;
  137. /* check reprogram result, only can reprogram corresponding bit value from 1 to 0 */
  138. for(i = 0U; i < WordNum; i++){
  139. if(0x01030503U != (*ptrd)){
  140. lednum = LED3;
  141. gd_eval_led_on(lednum);
  142. break;
  143. }else{
  144. ptrd++;
  145. }
  146. }
  147. }
  148. /*!
  149. \brief main function
  150. \param[in] none
  151. \param[out] none
  152. \retval none
  153. */
  154. int main(void)
  155. {
  156. /* initialize led on the board */
  157. gd_eval_led_init(LED1);
  158. gd_eval_led_init(LED2);
  159. gd_eval_led_init(LED3);
  160. gd_eval_led_init(LED4);
  161. /* step1: erase pages and check if it is successful. If not, light the LED1. */
  162. fmc_erase_pages();
  163. fmc_erase_pages_check();
  164. /* step2: program and check if it is successful. If not, light the LED2. */
  165. fmc_program();
  166. fmc_program_check();
  167. /* step3: reprogram and check if it is successful. If not, light the LED3. */
  168. fmc_reprogram();
  169. fmc_reprogram_check();
  170. /* if all the operations are successful, light the LED4. */
  171. if(LED4 == lednum){
  172. gd_eval_led_on(LED4);
  173. }
  174. while(1){
  175. }
  176. }