main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*!
  2. \file main.c
  3. \brief transfer data from RAM 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 "gd32f307c_eval.h"
  34. #include <string.h>
  35. #define DATANUM 16
  36. __IO ErrStatus transferflag1 = ERROR;
  37. __IO ErrStatus transferflag2 = ERROR;
  38. __IO ErrStatus transferflag3 = ERROR;
  39. __IO ErrStatus transferflag4 = ERROR;
  40. uint8_t source_address[DATANUM]= {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  41. 0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};
  42. uint8_t destination_address1[DATANUM];
  43. uint8_t destination_address2[DATANUM];
  44. uint8_t destination_address3[DATANUM];
  45. uint8_t destination_address4[DATANUM];
  46. void destbuf_init(void);
  47. void led_config(void);
  48. ErrStatus uc_data_compare(uint8_t* src, uint8_t* dst, uint16_t length);
  49. /*!
  50. \brief main function
  51. \param[in] none
  52. \param[out] none
  53. \retval none
  54. */
  55. int main(void)
  56. {
  57. int i = 0;
  58. dma_parameter_struct dma_init_struct;
  59. /* enable DMA clock */
  60. rcu_periph_clock_enable(RCU_DMA0);
  61. /* initialize LED */
  62. led_config();
  63. /* all LED off */
  64. gd_eval_led_off(LED2);
  65. gd_eval_led_off(LED3);
  66. gd_eval_led_off(LED4);
  67. gd_eval_led_off(LED5);
  68. destbuf_init();
  69. /* initialize DMA channel1 */
  70. dma_deinit(DMA0, DMA_CH1);
  71. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  72. dma_init_struct.memory_addr = (uint32_t)destination_address1;
  73. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  74. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  75. dma_init_struct.number = DATANUM;
  76. dma_init_struct.periph_addr = (uint32_t)source_address;
  77. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_ENABLE;
  78. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  79. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  80. dma_init(DMA0, DMA_CH1, &dma_init_struct);
  81. /* configure DMA mode */
  82. dma_circulation_disable(DMA0, DMA_CH1);
  83. dma_memory_to_memory_enable(DMA0, DMA_CH1);
  84. /* initialize DMA channel2 */
  85. dma_deinit(DMA0, DMA_CH2);
  86. dma_init_struct.memory_addr = (uint32_t)destination_address2;
  87. dma_init(DMA0, DMA_CH2, &dma_init_struct);
  88. /* configure DMA mode */
  89. dma_circulation_disable(DMA0, DMA_CH2);
  90. dma_memory_to_memory_enable(DMA0, DMA_CH2);
  91. /* initialize DMA channel3 */
  92. dma_deinit(DMA0, DMA_CH3);
  93. dma_init_struct.memory_addr = (uint32_t)destination_address3;
  94. dma_init(DMA0, DMA_CH3, &dma_init_struct);
  95. /* configure DMA mode */
  96. dma_circulation_disable(DMA0, DMA_CH3);
  97. dma_memory_to_memory_enable(DMA0, DMA_CH3);
  98. /* initialize DMA channel4 */
  99. dma_deinit(DMA0, DMA_CH4);
  100. dma_init_struct.memory_addr = (uint32_t)destination_address4;
  101. dma_init(DMA0, DMA_CH4, &dma_init_struct);
  102. /* configure DMA mode */
  103. dma_circulation_disable(DMA0, DMA_CH4);
  104. dma_memory_to_memory_enable(DMA0, DMA_CH4);
  105. /* enable DMA channel1~channel4 */
  106. dma_channel_enable(DMA0, DMA_CH1);
  107. dma_channel_enable(DMA0, DMA_CH2);
  108. dma_channel_enable(DMA0, DMA_CH3);
  109. dma_channel_enable(DMA0, DMA_CH4);
  110. /* wait for DMA transfer complete */
  111. for(i = 0; i < 200; i++);
  112. /* compare the data of source_address with data of destination_address */
  113. transferflag1 = uc_data_compare(source_address, destination_address1, DATANUM);
  114. transferflag2 = uc_data_compare(source_address, destination_address2, DATANUM);
  115. transferflag3 = uc_data_compare(source_address, destination_address3, DATANUM);
  116. transferflag4 = uc_data_compare(source_address, destination_address4, DATANUM);
  117. /* if DMA channel1 transfer success,light LED2 */
  118. if(SUCCESS == transferflag1){
  119. gd_eval_led_on(LED2);
  120. }
  121. /* if DMA channel2 transfer success,light LED3 */
  122. if(SUCCESS == transferflag2){
  123. gd_eval_led_on(LED3);
  124. }
  125. /* if DMA channel3 transfer success,light LED4 */
  126. if(SUCCESS == transferflag3){
  127. gd_eval_led_on(LED4);
  128. }
  129. /* if DMA channel4 transfer success,light LED5 */
  130. if(SUCCESS == transferflag4){
  131. gd_eval_led_on(LED5);
  132. }
  133. while (1);
  134. }
  135. /*!
  136. \brief initialize destination buffer
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. void destbuf_init(void)
  142. {
  143. memset(destination_address1, 0, DATANUM);
  144. memset(destination_address2, 0, DATANUM);
  145. memset(destination_address3, 0, DATANUM);
  146. memset(destination_address4, 0, DATANUM);
  147. }
  148. /*!
  149. \brief LEDs configure
  150. \param[in] none
  151. \param[out] none
  152. \retval none
  153. */
  154. void led_config(void)
  155. {
  156. gd_eval_led_init (LED2);
  157. gd_eval_led_init (LED3);
  158. gd_eval_led_init (LED4);
  159. gd_eval_led_init (LED5);
  160. }
  161. /*!
  162. \brief data compare function
  163. \param[in] src : source data
  164. \param[in] dst : destination data
  165. \param[in] length : the compare data length
  166. \param[out] none
  167. \retval ErrStatus : ERROR or SUCCESS
  168. */
  169. ErrStatus uc_data_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  170. {
  171. while (length--){
  172. if (*src++ != *dst++){
  173. return ERROR;
  174. }
  175. }
  176. return SUCCESS;
  177. }