main.c 9.1 KB

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