main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*!
  2. \file main.c
  3. \brief SPI fullduplex communication use DMA
  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. #define SPI_CRC_ENABLE 1
  35. #define ARRAYSIZE 10
  36. #define SET_SPI0_NSS_HIGH gpio_bit_set(GPIOA,GPIO_PIN_3);
  37. #define SET_SPI0_NSS_LOW gpio_bit_reset(GPIOA,GPIO_PIN_3);
  38. uint8_t spi0_send_array[ARRAYSIZE] = {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA};
  39. uint8_t spi2_send_array[ARRAYSIZE] = {0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA};
  40. uint8_t spi0_receive_array[ARRAYSIZE];
  41. uint8_t spi2_receive_array[ARRAYSIZE];
  42. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint8_t length);
  43. void rcu_config(void);
  44. void gpio_config(void);
  45. void dma_config(void);
  46. void spi_config(void);
  47. /*!
  48. \brief main function
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. int main(void)
  54. {
  55. /* init led2 and led3 */
  56. gd_eval_led_init(LED2);
  57. gd_eval_led_init(LED3);
  58. /* peripheral clock enable */
  59. rcu_config();
  60. /* GPIO config */
  61. gpio_config();
  62. /* DMA config */
  63. dma_config();
  64. /* SPI config */
  65. spi_config();
  66. SET_SPI0_NSS_HIGH
  67. /* SPI enable */
  68. spi_enable(SPI2);
  69. spi_enable(SPI0);
  70. /* DMA channel enable */
  71. dma_channel_enable(DMA0, DMA_CH1);
  72. dma_channel_enable(DMA0, DMA_CH2);
  73. dma_channel_enable(DMA1, DMA_CH0);
  74. dma_channel_enable(DMA1, DMA_CH1);
  75. SET_SPI0_NSS_LOW
  76. /* SPI DMA enable */
  77. spi_dma_enable(SPI2, SPI_DMA_TRANSMIT);
  78. spi_dma_enable(SPI2, SPI_DMA_RECEIVE);
  79. spi_dma_enable(SPI0, SPI_DMA_TRANSMIT);
  80. spi_dma_enable(SPI0, SPI_DMA_RECEIVE);
  81. /* wait dma transmit complete */
  82. while(!dma_flag_get(DMA0,DMA_CH2, DMA_FLAG_FTF)) {
  83. }
  84. while(!dma_flag_get(DMA1,DMA_CH1, DMA_FLAG_FTF)) {
  85. }
  86. while(!dma_flag_get(DMA1,DMA_CH0, DMA_FLAG_FTF)) {
  87. }
  88. while(!dma_flag_get(DMA0,DMA_CH1, DMA_FLAG_FTF)) {
  89. }
  90. SET_SPI0_NSS_HIGH
  91. #if SPI_CRC_ENABLE
  92. /* check the CRC error status */
  93. if(SET != spi_i2s_flag_get(SPI0, SPI_FLAG_CRCERR)) {
  94. gd_eval_led_on(LED2);
  95. } else {
  96. gd_eval_led_off(LED2);
  97. }
  98. if(SET != spi_i2s_flag_get(SPI2, SPI_FLAG_CRCERR)) {
  99. gd_eval_led_on(LED3);
  100. } else {
  101. gd_eval_led_off(LED3);
  102. }
  103. #else
  104. /* compare receive data with send data */
  105. if(memory_compare(spi2_receive_array, spi0_send_array, ARRAYSIZE))
  106. gd_eval_led_on(LED2);
  107. else
  108. gd_eval_led_off(LED2);
  109. if(memory_compare(spi0_receive_array, spi2_send_array, ARRAYSIZE))
  110. gd_eval_led_on(LED3);
  111. else
  112. gd_eval_led_off(LED3);
  113. #endif /* enable CRC function */
  114. while(1);
  115. }
  116. /*!
  117. \brief configure different peripheral clocks
  118. \param[in] none
  119. \param[out] none
  120. \retval none
  121. */
  122. void rcu_config(void)
  123. {
  124. rcu_periph_clock_enable(RCU_GPIOA);
  125. rcu_periph_clock_enable(RCU_GPIOC);
  126. rcu_periph_clock_enable(RCU_DMA0);
  127. rcu_periph_clock_enable(RCU_DMA1);
  128. rcu_periph_clock_enable(RCU_SPI0);
  129. rcu_periph_clock_enable(RCU_SPI2);
  130. rcu_periph_clock_enable(RCU_AF);
  131. }
  132. /*!
  133. \brief configure the GPIO peripheral
  134. \param[in] none
  135. \param[out] none
  136. \retval none
  137. */
  138. void gpio_config(void)
  139. {
  140. /* SPI0 GPIO config: SCK/PA5, MISO/PA6, MOSI/PA7 */
  141. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_7);
  142. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
  143. /* PA3 as NSS */
  144. gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  145. gpio_pin_remap_config(GPIO_SPI2_REMAP, ENABLE);
  146. /* SPI2 GPIO config: NSS/PA4, SCK/PC10, MISO/PC11, MOSI/PC12 */
  147. gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10 | GPIO_PIN_12);
  148. gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
  149. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
  150. }
  151. /*!
  152. \brief configure the DMA peripheral
  153. \param[in] none
  154. \param[out] none
  155. \retval none
  156. */
  157. void dma_config(void)
  158. {
  159. dma_parameter_struct dma_init_struct;
  160. /* SPI0 transmit dma config:DMA0-DMA_CH2 */
  161. dma_deinit(DMA0, DMA_CH2);
  162. dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI0);
  163. dma_init_struct.memory_addr = (uint32_t)spi0_send_array;
  164. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  165. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  166. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  167. dma_init_struct.priority = DMA_PRIORITY_LOW;
  168. dma_init_struct.number = ARRAYSIZE;
  169. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  170. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  171. dma_init(DMA0, DMA_CH2, &dma_init_struct);
  172. /* configure DMA mode */
  173. dma_circulation_disable(DMA0, DMA_CH2);
  174. dma_memory_to_memory_disable(DMA0, DMA_CH2);
  175. /* SPI0 receive dma config:DMA0-DMA_CH1 */
  176. dma_deinit(DMA0, DMA_CH1);
  177. dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI0);
  178. dma_init_struct.memory_addr = (uint32_t)spi0_receive_array;
  179. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  180. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  181. dma_init(DMA0, DMA_CH1, &dma_init_struct);
  182. /* configure DMA mode */
  183. dma_circulation_disable(DMA0, DMA_CH1);
  184. dma_memory_to_memory_disable(DMA0, DMA_CH1);
  185. /* SPI2 transmit dma config:DMA1,DMA_CH1 */
  186. dma_deinit(DMA1, DMA_CH1);
  187. dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI2);
  188. dma_init_struct.memory_addr = (uint32_t)spi2_send_array;
  189. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  190. dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
  191. dma_init(DMA1, DMA_CH1, &dma_init_struct);
  192. /* configure DMA mode */
  193. dma_circulation_disable(DMA1, DMA_CH1);
  194. dma_memory_to_memory_disable(DMA1, DMA_CH1);
  195. /* SPI2 receive dma config:DMA1,DMA_CH0 */
  196. dma_deinit(DMA1, DMA_CH0);
  197. dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI2);
  198. dma_init_struct.memory_addr = (uint32_t)spi2_receive_array;
  199. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  200. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  201. dma_init(DMA1, DMA_CH0, &dma_init_struct);
  202. /* configure DMA mode */
  203. dma_circulation_disable(DMA1, DMA_CH0);
  204. dma_memory_to_memory_disable(DMA1, DMA_CH0);
  205. }
  206. /*!
  207. \brief configure the SPI peripheral
  208. \param[in] none
  209. \param[out] none
  210. \retval none
  211. */
  212. void spi_config(void)
  213. {
  214. spi_parameter_struct spi_init_struct;
  215. /* SPI0 parameter config */
  216. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  217. spi_init_struct.device_mode = SPI_MASTER;
  218. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  219. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;
  220. spi_init_struct.nss = SPI_NSS_SOFT;
  221. spi_init_struct.prescale = SPI_PSC_256;
  222. spi_init_struct.endian = SPI_ENDIAN_MSB;
  223. spi_init(SPI0, &spi_init_struct);
  224. /* SPI2 parameter config */
  225. spi_init_struct.device_mode = SPI_SLAVE;
  226. spi_init_struct.nss = SPI_NSS_HARD;
  227. spi_init(SPI2, &spi_init_struct);
  228. }
  229. /*!
  230. \brief memory compare function
  231. \param[in] src: source data pointer
  232. \param[in] dst: destination data pointer
  233. \param[in] length: the compare data length
  234. \param[out] none
  235. \retval ErrStatus : ERROR or SUCCESS
  236. */
  237. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint8_t length)
  238. {
  239. while (length--){
  240. if (*src++ != *dst++)
  241. return ERROR;
  242. }
  243. return SUCCESS;
  244. }