main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*!
  2. \file main.c
  3. \brief main flash program, erase
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include "gd32f307c_eval.h"
  34. #include "main.h"
  35. #define FMC_PAGE_SIZE ((uint16_t)0x800U)
  36. #define FMC_WRITE_START_ADDR ((uint32_t)0x08004000U)
  37. #define FMC_WRITE_END_ADDR ((uint32_t)0x08004800U)
  38. uint32_t *ptrd;
  39. uint32_t address = 0x00000000U;
  40. uint32_t data0 = 0x01234567U;
  41. led_typedef_enum lednum = LED4;
  42. /* calculate the number of page to be programmed/erased */
  43. uint32_t PageNum = (FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) / FMC_PAGE_SIZE;
  44. /* calculate the number of page to be programmed/erased */
  45. uint32_t WordNum = ((FMC_WRITE_END_ADDR - FMC_WRITE_START_ADDR) >> 2);
  46. /*!
  47. \brief erase fmc pages from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. void fmc_erase_pages(void)
  53. {
  54. uint32_t EraseCounter;
  55. /* unlock the flash program/erase controller */
  56. fmc_unlock();
  57. /* clear all pending flags */
  58. fmc_flag_clear(FMC_FLAG_BANK0_END);
  59. fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
  60. fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
  61. /* erase the flash pages */
  62. for(EraseCounter = 0; EraseCounter < PageNum; EraseCounter++){
  63. fmc_page_erase(FMC_WRITE_START_ADDR + (FMC_PAGE_SIZE * EraseCounter));
  64. fmc_flag_clear(FMC_FLAG_BANK0_END);
  65. fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
  66. fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
  67. }
  68. /* lock the main FMC after the erase operation */
  69. fmc_lock();
  70. }
  71. /*!
  72. \brief program fmc word by word from FMC_WRITE_START_ADDR to FMC_WRITE_END_ADDR
  73. \param[in] none
  74. \param[out] none
  75. \retval none
  76. */
  77. void fmc_program(void)
  78. {
  79. /* unlock the flash program/erase controller */
  80. fmc_unlock();
  81. address = FMC_WRITE_START_ADDR;
  82. /* program flash */
  83. while(address < FMC_WRITE_END_ADDR){
  84. fmc_word_program(address, data0);
  85. address += 4;
  86. fmc_flag_clear(FMC_FLAG_BANK0_END);
  87. fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
  88. fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
  89. }
  90. /* lock the main FMC after the program operation */
  91. fmc_lock();
  92. }
  93. /*!
  94. \brief check fmc erase result
  95. \param[in] none
  96. \param[out] none
  97. \retval none
  98. */
  99. void fmc_erase_pages_check(void)
  100. {
  101. uint32_t i;
  102. ptrd = (uint32_t *)FMC_WRITE_START_ADDR;
  103. /* check flash whether has been erased */
  104. for(i = 0; i < WordNum; i++){
  105. if(0xFFFFFFFF != (*ptrd)){
  106. lednum = LED2;
  107. gd_eval_led_on(lednum);
  108. break;
  109. }else{
  110. ptrd++;
  111. }
  112. }
  113. }
  114. /*!
  115. \brief check fmc program result
  116. \param[in] none
  117. \param[out] none
  118. \retval none
  119. */
  120. void fmc_program_check(void)
  121. {
  122. uint32_t i;
  123. ptrd = (uint32_t *)FMC_WRITE_START_ADDR;
  124. /* check flash whether has been programmed */
  125. for(i = 0; i < WordNum; i++){
  126. if((*ptrd) != data0){
  127. lednum = LED3;
  128. gd_eval_led_on(lednum);
  129. break;
  130. }else{
  131. ptrd++;
  132. }
  133. }
  134. }
  135. /*!
  136. \brief main function
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. int main(void)
  142. {
  143. /* initialize led on the board */
  144. gd_eval_led_init(LED2);
  145. gd_eval_led_init(LED3);
  146. gd_eval_led_init(LED4);
  147. /* step1: erase pages and check if it is successful. If not, light the LED2. */
  148. fmc_erase_pages();
  149. fmc_erase_pages_check();
  150. /* step2: program and check if it is successful. If not, light the LED3. */
  151. fmc_program();
  152. fmc_program_check();
  153. /* if all the operations are successful, light the LED4. */
  154. if(LED4 == lednum){
  155. gd_eval_led_on(LED4);
  156. }
  157. while(1);
  158. }