main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*!
  2. \file main.c
  3. \brief master send and slave receive data use interrupt 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 "gd32f30x_it.h"
  34. #include "gd32f307c_eval.h"
  35. #define SPI_CRC_ENABLE 1
  36. #define ARRAYSIZE 10
  37. #define SET_SPI0_NSS_HIGH gpio_bit_set(GPIOA,GPIO_PIN_3);
  38. #define SET_SPI0_NSS_LOW gpio_bit_reset(GPIOA,GPIO_PIN_3);
  39. __IO uint32_t send_n = 0, receive_n = 0;
  40. uint8_t spi0_send_array[ARRAYSIZE] = {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA};
  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 spi_config(void);
  46. /*!
  47. \brief main function
  48. \param[in] none
  49. \param[out] none
  50. \retval none
  51. */
  52. int main(void)
  53. {
  54. /* init led2 */
  55. gd_eval_led_init(LED2);
  56. /* NVIC config */
  57. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  58. nvic_irq_enable(SPI0_IRQn,1,1);
  59. nvic_irq_enable(SPI2_IRQn,0,1);
  60. /* peripheral clock enable */
  61. rcu_config();
  62. /* GPIO config */
  63. gpio_config();
  64. /* SPI config */
  65. spi_config();
  66. SET_SPI0_NSS_HIGH
  67. /* SPI int enable */
  68. spi_i2s_interrupt_enable(SPI0, SPI_I2S_INT_TBE);
  69. spi_i2s_interrupt_enable(SPI2, SPI_I2S_INT_RBNE);
  70. SET_SPI0_NSS_LOW
  71. /* SPI enable */
  72. spi_enable(SPI2);
  73. spi_enable(SPI0);
  74. /* wait transmit complete */
  75. while(receive_n < ARRAYSIZE);
  76. #if SPI_CRC_ENABLE
  77. /* check the CRC error status */
  78. if(SET != spi_i2s_flag_get(SPI2, SPI_FLAG_CRCERR)) {
  79. gd_eval_led_on(LED2);
  80. } else {
  81. gd_eval_led_off(LED2);
  82. }
  83. SET_SPI0_NSS_HIGH
  84. #else
  85. /* compare receive data with send data */
  86. if(memory_compare(spi2_receive_array, spi0_send_array, ARRAYSIZE))
  87. gd_eval_led_on(LED2);
  88. else
  89. gd_eval_led_off(LED2);
  90. #endif /* enable CRC function */
  91. while(1);
  92. }
  93. /*!
  94. \brief configure different peripheral clocks
  95. \param[in] none
  96. \param[out] none
  97. \retval none
  98. */
  99. void rcu_config(void)
  100. {
  101. rcu_periph_clock_enable(RCU_GPIOA);
  102. rcu_periph_clock_enable(RCU_GPIOC);
  103. rcu_periph_clock_enable(RCU_SPI0);
  104. rcu_periph_clock_enable(RCU_SPI2);
  105. rcu_periph_clock_enable(RCU_AF);
  106. }
  107. /*!
  108. \brief configure the GPIO peripheral
  109. \param[in] none
  110. \param[out] none
  111. \retval none
  112. */
  113. void gpio_config(void)
  114. {
  115. /* SPI0 GPIO config:SCK/PA5, MOSI/PA7 */
  116. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_7);
  117. /* config PA3 as SPI0_NSS */
  118. gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
  119. gpio_pin_remap_config(GPIO_SPI2_REMAP,ENABLE);
  120. /* SPI2 GPIO config: NSS/PA4, SCK/PC10, MISO/PC11 */
  121. gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10 |GPIO_PIN_11);
  122. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
  123. }
  124. /*!
  125. \brief configure the SPI peripheral
  126. \param[in] none
  127. \param[out] none
  128. \retval none
  129. */
  130. void spi_config(void)
  131. {
  132. spi_parameter_struct spi_init_struct;
  133. /* SPI0 parameter config */
  134. spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;
  135. spi_init_struct.device_mode = SPI_MASTER;
  136. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  137. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  138. spi_init_struct.nss = SPI_NSS_SOFT;
  139. spi_init_struct.prescale = SPI_PSC_256;
  140. spi_init_struct.endian = SPI_ENDIAN_MSB;
  141. spi_init(SPI0, &spi_init_struct);
  142. /* SPI2 parameter config */
  143. spi_init_struct.trans_mode = SPI_TRANSMODE_BDRECEIVE;
  144. spi_init_struct.device_mode = SPI_SLAVE;
  145. spi_init_struct.nss = SPI_NSS_HARD;
  146. spi_init(SPI2, &spi_init_struct);
  147. #if SPI_CRC_ENABLE
  148. /* configure SPI CRC function */
  149. spi_crc_polynomial_set(SPI0, 7);
  150. spi_crc_polynomial_set(SPI2, 7);
  151. spi_crc_on(SPI0);
  152. spi_crc_on(SPI2);
  153. #endif /* enable CRC function */
  154. }
  155. /*!
  156. \brief memory compare function
  157. \param[in] src: source data pointer
  158. \param[in] dst: destination data pointer
  159. \param[in] length: the compare data length
  160. \param[out] none
  161. \retval ErrStatus: ERROR or SUCCESS
  162. */
  163. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint8_t length)
  164. {
  165. while (length--){
  166. if (*src++ != *dst++)
  167. return ERROR;
  168. }
  169. return SUCCESS;
  170. }