main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*!
  2. \file main.c
  3. \brief transfer data from FLASH to RAM
  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 <string.h>
  34. #include "gd32f307c_eval.h"
  35. #define TRANSFER_NUM 0x400 /* Configuration value in bytes */
  36. #define FMC_PAGE_SIZE ((uint16_t)0x800)
  37. #define BANK0_WRITE_START_ADDR ((uint32_t)0x08004000)
  38. void rcu_config(void);
  39. void nvic_config(void);
  40. void led_config(void);
  41. __IO uint32_t g_dmacomplete_flag = 0;
  42. uint8_t g_destbuf[TRANSFER_NUM];
  43. const uint32_t transdata = 0x3210ABCD;
  44. fmc_state_enum fmcstatus = FMC_READY;
  45. /*!
  46. \brief main function
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. int main(void)
  52. {
  53. uint32_t i, count;
  54. uint32_t *ptrd;
  55. uint32_t address = 0x00;
  56. ErrStatus access_flag = SUCCESS;
  57. dma_parameter_struct dma_init_struct;
  58. uint32_t wperror = 0;
  59. /* system clocks configuration */
  60. rcu_config();
  61. /* NVIC configuration */
  62. nvic_config();
  63. /* LED configuration */
  64. led_config() ;
  65. /* unlock the flash bank1 program erase controller */
  66. fmc_unlock();
  67. /* define the number of page to be erased */
  68. count = TRANSFER_NUM / FMC_PAGE_SIZE;
  69. /* clear all pending flags */
  70. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  71. /* erase the flash pages */
  72. for(i = 0; i <= count; i++){
  73. fmcstatus = fmc_page_erase(BANK0_WRITE_START_ADDR + (FMC_PAGE_SIZE * i));
  74. wperror += (fmcstatus == FMC_WPERR);
  75. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  76. }
  77. if(wperror != 0){
  78. while(1);
  79. }
  80. /* unlock the flash bank1 program erase controller */
  81. fmc_lock();
  82. ptrd = (uint32_t*)BANK0_WRITE_START_ADDR;
  83. count = TRANSFER_NUM / sizeof(*ptrd);
  84. for(i = 0; i < count; i++){
  85. if(0xFFFFFFFF != *ptrd){
  86. access_flag = ERROR;
  87. break;
  88. }
  89. ptrd++;
  90. }
  91. if(ERROR == access_flag){
  92. while(1);
  93. }
  94. /* unlock the flash bank1 program erase controller */
  95. fmc_unlock();
  96. /* clear all pending flags */
  97. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  98. /* program flash bank1 */
  99. address = BANK0_WRITE_START_ADDR;
  100. wperror = 0;
  101. count = BANK0_WRITE_START_ADDR + TRANSFER_NUM;
  102. while(address < count){
  103. fmcstatus = fmc_word_program(address, transdata);
  104. address = address + 4;
  105. wperror += (FMC_WPERR == fmcstatus);
  106. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  107. }
  108. if(wperror != 0){
  109. while(1);
  110. }
  111. fmc_lock();
  112. memset(g_destbuf ,0 ,TRANSFER_NUM);
  113. /* DMA channel0 initialize */
  114. dma_deinit(DMA0, DMA_CH0);
  115. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  116. dma_init_struct.memory_addr = (uint32_t)g_destbuf;
  117. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  118. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  119. dma_init_struct.number = TRANSFER_NUM;
  120. dma_init_struct.periph_addr = (uint32_t)BANK0_WRITE_START_ADDR;
  121. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_ENABLE;
  122. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  123. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  124. dma_init(DMA0, DMA_CH0, &dma_init_struct);
  125. /* DMA channel0 mode configuration */
  126. dma_circulation_disable(DMA0, DMA_CH0);
  127. dma_memory_to_memory_enable(DMA0, DMA_CH0);
  128. /* DMA channel0 interrupt configuration */
  129. dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_FTF);
  130. /* enable DMA transfer */
  131. dma_channel_enable(DMA0, DMA_CH0);
  132. /* wait DMA interrupt */
  133. while(0 == g_dmacomplete_flag);
  134. /* compare destdata with transdata */
  135. ptrd = (uint32_t *)g_destbuf;
  136. count = TRANSFER_NUM / sizeof(*ptrd);
  137. for(i = 0; i < count; i++){
  138. if(transdata != *ptrd){
  139. access_flag = ERROR;
  140. break;
  141. }
  142. ptrd++;
  143. }
  144. /* transfer sucess */
  145. if(access_flag != ERROR){
  146. gd_eval_led_on(LED2);
  147. gd_eval_led_on(LED3);
  148. gd_eval_led_on(LED4);
  149. gd_eval_led_on(LED5);
  150. }else{
  151. gd_eval_led_on(LED2);
  152. gd_eval_led_on(LED4);
  153. }
  154. while(1);
  155. }
  156. /*!
  157. \brief configure LED
  158. \param[in] none
  159. \param[out] none
  160. \retval none
  161. */
  162. void led_config(void)
  163. {
  164. gd_eval_led_init(LED2);
  165. gd_eval_led_init(LED3);
  166. gd_eval_led_init(LED4);
  167. gd_eval_led_init(LED5);
  168. /* LED off */
  169. gd_eval_led_off(LED2);
  170. gd_eval_led_off(LED3);
  171. gd_eval_led_off(LED4);
  172. gd_eval_led_init(LED5);
  173. }
  174. /*!
  175. \brief configure the different system clocks
  176. \param[in] none
  177. \param[out] none
  178. \retval none
  179. */
  180. void rcu_config(void)
  181. {
  182. /* enable DMA clock */
  183. rcu_periph_clock_enable(RCU_DMA0);
  184. }
  185. /*!
  186. \brief configure the nested vectored interrupt controller
  187. \param[in] none
  188. \param[out] none
  189. \retval none
  190. */
  191. void nvic_config(void)
  192. {
  193. nvic_irq_enable(DMA0_Channel0_IRQn,0,0);
  194. }