main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*!
  2. \file main.c
  3. \brief I2S master send and slave receive 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 arraysize 10
  35. uint8_t spi1_send_array[arraysize] = {0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA};
  36. uint8_t spi2_receive_array[arraysize];
  37. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint8_t length);
  38. void rcu_config(void);
  39. void gpio_config(void);
  40. void dma_config(void);
  41. void spi_config(void);
  42. /*!
  43. \brief main function
  44. \param[in] none
  45. \param[out] none
  46. \retval none
  47. */
  48. int main(void)
  49. {
  50. /* init led2 */
  51. gd_eval_led_init(LED2);
  52. /* peripheral clock enable */
  53. rcu_config();
  54. /* GPIO config */
  55. gpio_config();
  56. /* DMA config */
  57. dma_config();
  58. /* SPI config */
  59. spi_config();
  60. /* SPI enable */
  61. i2s_enable(SPI2);
  62. i2s_enable(SPI1);
  63. /* DMA channel enable */
  64. dma_channel_enable(DMA0,DMA_CH4);
  65. dma_channel_enable(DMA1,DMA_CH0);
  66. /* SPI DMA enable */
  67. spi_dma_enable(SPI2, SPI_DMA_RECEIVE);
  68. spi_dma_enable(SPI1, SPI_DMA_TRANSMIT);
  69. /* wait DMA transmit complete */
  70. while(!dma_flag_get(DMA0, DMA_CH4, DMA_INTF_FTFIF));
  71. while(!dma_flag_get(DMA1, DMA_CH0, DMA_INTF_FTFIF));
  72. /* compare receive data with send data */
  73. if(memory_compare(spi2_receive_array, spi1_send_array, arraysize))
  74. gd_eval_led_on(LED2);
  75. else
  76. gd_eval_led_off(LED2);
  77. while(1);
  78. }
  79. /*!
  80. \brief configure different peripheral clocks
  81. \param[in] none
  82. \param[out] none
  83. \retval none
  84. */
  85. void rcu_config(void)
  86. {
  87. #ifdef GD32F30X_CL
  88. rcu_pll2_config(RCU_PLL2_MUL8);
  89. rcu_osci_on(RCU_PLL2_CK);
  90. while((RCU_CTL & RCU_CTL_PLL2STB) == 0)
  91. {
  92. }
  93. rcu_i2s1_clock_config(RCU_I2S1SRC_CKPLL2_MUL2);
  94. rcu_i2s2_clock_config(RCU_I2S2SRC_CKPLL2_MUL2);
  95. #endif
  96. rcu_periph_clock_enable(RCU_SPI1);
  97. rcu_periph_clock_enable(RCU_SPI2);
  98. rcu_periph_clock_enable(RCU_GPIOA);
  99. rcu_periph_clock_enable(RCU_GPIOB);
  100. rcu_periph_clock_enable(RCU_DMA0);
  101. rcu_periph_clock_enable(RCU_DMA1);
  102. }
  103. /*!
  104. \brief configure the GPIO peripheral
  105. \param[in] none
  106. \param[out] none
  107. \retval none
  108. */
  109. void gpio_config(void)
  110. {
  111. /* I2S1 GPIO config: I2S1_WS/PB12, I2S1_CK/PB13, I2S_SD/PB15 */
  112. gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12 | GPIO_PIN_13 |GPIO_PIN_15);
  113. /* I2S2 GPIO config: I2S2_WS/PA15, I2S2_CK/PB3, I2S2_SD/PB5 */
  114. gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5);
  115. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
  116. }
  117. /*!
  118. \brief configure the DMA peripheral
  119. \param[in] none
  120. \param[out] none
  121. \retval none
  122. */
  123. void dma_config(void)
  124. {
  125. dma_parameter_struct dma_init_struct;
  126. /* SPI1 transmit dma config: DMA0,DMA_CH4 */
  127. dma_deinit(DMA0, DMA_CH4);
  128. dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI1);
  129. dma_init_struct.memory_addr = (uint32_t)spi1_send_array;
  130. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  131. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  132. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  133. dma_init_struct.priority = DMA_PRIORITY_LOW;
  134. dma_init_struct.number = arraysize;
  135. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  136. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  137. dma_init(DMA0, DMA_CH4, &dma_init_struct);
  138. /* configure DMA mode */
  139. dma_circulation_disable(DMA0, DMA_CH4);
  140. dma_memory_to_memory_disable(DMA0, DMA_CH4);
  141. /* SPI2 receive dma config: DMA1-DMA_CH0 */
  142. dma_deinit(DMA1, DMA_CH0);
  143. dma_init_struct.periph_addr = (uint32_t)&SPI_DATA(SPI2);
  144. dma_init_struct.memory_addr = (uint32_t)spi2_receive_array;
  145. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  146. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  147. dma_init(DMA1, DMA_CH0, &dma_init_struct);
  148. /* configure DMA mode */
  149. dma_circulation_disable(DMA1, DMA_CH0);
  150. dma_memory_to_memory_disable(DMA1, DMA_CH0);
  151. }
  152. /*!
  153. \brief configure the SPI peripheral
  154. \param[in] none
  155. \param[out] none
  156. \retval none
  157. */
  158. void spi_config(void)
  159. {
  160. spi_i2s_deinit(SPI1);
  161. spi_i2s_deinit(SPI2);
  162. i2s_init(SPI1, I2S_MODE_MASTERTX, I2S_STD_PHILLIPS, I2S_CKPL_LOW);
  163. i2s_psc_config(SPI1, I2S_AUDIOSAMPLE_11K, I2S_FRAMEFORMAT_DT16B_CH16B, I2S_MCKOUT_DISABLE);
  164. i2s_init(SPI2, I2S_MODE_SLAVERX, I2S_STD_PHILLIPS, I2S_CKPL_LOW);
  165. i2s_psc_config(SPI2, I2S_AUDIOSAMPLE_11K, I2S_FRAMEFORMAT_DT16B_CH16B, I2S_MCKOUT_DISABLE);
  166. }
  167. /*!
  168. \brief memory compare function
  169. \param[in] src: source data pointer
  170. \param[in] dst: destination data pointer
  171. \param[in] length: the compare data length
  172. \param[out] none
  173. \retval ErrStatus : ERROR or SUCCESS
  174. */
  175. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint8_t length)
  176. {
  177. while (length--){
  178. if (*src++ != *dst++)
  179. return ERROR;
  180. }
  181. return SUCCESS;
  182. }