main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*!
  2. \file main.c
  3. \brief transfer data from FLASH to RAM
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include <string.h>
  11. #include "gd32f3x0_eval.h"
  12. #define TRANSFER_NUM 0x400 /* Configuration value in bytes */
  13. #define FMC_PAGE_SIZE ((uint16_t)0x800)
  14. #define BANK0_WRITE_START_ADDR ((uint32_t)0x08004000)
  15. void rcu_config(void);
  16. void nvic_config(void);
  17. void led_config(void);
  18. __IO uint32_t g_dmacomplete_flag = 0;
  19. uint8_t g_destbuf[TRANSFER_NUM];
  20. const uint32_t transdata = 0x3210ABCD;
  21. fmc_state_enum fmcstatus = FMC_READY;
  22. /*!
  23. \brief main function
  24. \param[in] none
  25. \param[out] none
  26. \retval none
  27. */
  28. int main(void)
  29. {
  30. uint32_t i, count;
  31. uint32_t *ptrd;
  32. uint32_t address = 0x00;
  33. ErrStatus access_flag = SUCCESS;
  34. dma_parameter_struct dma_init_struct;
  35. uint32_t wperror = 0;
  36. /* system clocks configuration */
  37. rcu_config();
  38. /* NVIC configuration */
  39. nvic_config();
  40. /* LED configuration */
  41. led_config() ;
  42. /* unlock the flash bank1 program erase controller */
  43. fmc_unlock();
  44. /* define the number of page to be erased */
  45. count = TRANSFER_NUM / FMC_PAGE_SIZE;
  46. /* clear all pending flags */
  47. fmc_flag_clear(FMC_FLAG_PGERR | FMC_FLAG_WPERR | FMC_FLAG_END);
  48. /* erase the flash pages */
  49. for(i = 0; i <= count; i++){
  50. fmcstatus = fmc_page_erase(BANK0_WRITE_START_ADDR + (FMC_PAGE_SIZE * i));
  51. wperror += (fmcstatus == FMC_WPERR);
  52. fmc_flag_clear(FMC_FLAG_PGERR | FMC_FLAG_WPERR | FMC_FLAG_END);
  53. }
  54. if(wperror != 0){
  55. while(1);
  56. }
  57. /* unlock the flash bank1 program erase controller */
  58. fmc_lock();
  59. ptrd = (uint32_t*)BANK0_WRITE_START_ADDR;
  60. count = TRANSFER_NUM / sizeof(*ptrd);
  61. for(i = 0; i < count; i++){
  62. if(0xFFFFFFFF != *ptrd){
  63. access_flag = ERROR;
  64. break;
  65. }
  66. ptrd++;
  67. }
  68. if(ERROR == access_flag){
  69. while(1);
  70. }
  71. /* unlock the flash bank1 program erase controller */
  72. fmc_unlock();
  73. /* clear all pending flags */
  74. fmc_flag_clear(FMC_FLAG_PGERR | FMC_FLAG_WPERR | FMC_FLAG_END);
  75. /* program flash bank1 */
  76. address = BANK0_WRITE_START_ADDR;
  77. wperror = 0;
  78. count = BANK0_WRITE_START_ADDR + TRANSFER_NUM;
  79. while(address < count){
  80. fmcstatus = fmc_word_program(address, transdata);
  81. address = address + 4;
  82. wperror += (FMC_WPERR == fmcstatus);
  83. fmc_flag_clear(FMC_FLAG_PGERR | FMC_FLAG_WPERR | FMC_FLAG_END);
  84. }
  85. if(wperror != 0){
  86. while(1);
  87. }
  88. fmc_lock();
  89. memset(g_destbuf ,0 ,TRANSFER_NUM);
  90. /* DMA channel0 initialize */
  91. dma_deinit(DMA_CH0);
  92. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  93. dma_init_struct.memory_addr = (uint32_t)g_destbuf;
  94. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  95. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  96. dma_init_struct.number = TRANSFER_NUM;
  97. dma_init_struct.periph_addr = (uint32_t)BANK0_WRITE_START_ADDR;
  98. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_ENABLE;
  99. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  100. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  101. dma_init(DMA_CH0, dma_init_struct);
  102. /* DMA channel0 mode configuration */
  103. dma_circulation_disable(DMA_CH0);
  104. dma_memory_to_memory_enable(DMA_CH0);
  105. /* DMA channel0 interrupt configuration */
  106. dma_interrupt_enable(DMA_CH0, DMA_INT_FTF);
  107. /* enable DMA transfer */
  108. dma_channel_enable(DMA_CH0);
  109. /* wait DMA interrupt */
  110. while(0 == g_dmacomplete_flag);
  111. /* compare destdata with transdata */
  112. ptrd = (uint32_t *)g_destbuf;
  113. count = TRANSFER_NUM / sizeof(*ptrd);
  114. for(i = 0; i < count; i++){
  115. if(transdata != *ptrd){
  116. access_flag = ERROR;
  117. break;
  118. }
  119. ptrd++;
  120. }
  121. /* transfer sucess */
  122. if(access_flag != ERROR){
  123. gd_eval_led_on(LED1);
  124. gd_eval_led_on(LED2);
  125. gd_eval_led_on(LED3);
  126. gd_eval_led_on(LED4);
  127. }else{
  128. gd_eval_led_on(LED1);
  129. gd_eval_led_on(LED3);
  130. }
  131. while(1);
  132. }
  133. /*!
  134. \brief configure LED
  135. \param[in] none
  136. \param[out] none
  137. \retval none
  138. */
  139. void led_config(void)
  140. {
  141. gd_eval_led_init(LED1);
  142. gd_eval_led_init(LED2);
  143. gd_eval_led_init(LED3);
  144. gd_eval_led_init(LED4);
  145. /* LED off */
  146. gd_eval_led_off(LED1);
  147. gd_eval_led_off(LED3);
  148. gd_eval_led_off(LED2);
  149. gd_eval_led_off(LED4);
  150. }
  151. /*!
  152. \brief configure the different system clocks
  153. \param[in] none
  154. \param[out] none
  155. \retval none
  156. */
  157. void rcu_config(void)
  158. {
  159. /* enable DMA clock */
  160. rcu_periph_clock_enable(RCU_DMA);
  161. }
  162. /*!
  163. \brief configure the nested vectored interrupt controller
  164. \param[in] none
  165. \param[out] none
  166. \retval none
  167. */
  168. void nvic_config(void)
  169. {
  170. nvic_irq_enable(DMA_Channel0_IRQn, 0, 0);
  171. }