main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*!
  2. \file main.c
  3. \brief USART synchronous
  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 <stdio.h>
  35. #define txbuffer_size1 (countof(txbuffer1) - 1)
  36. #define txbuffer_size2 (countof(txbuffer2) - 1)
  37. #define DYMMY_BYTE 0x00
  38. #define countof(a) (sizeof(a) / sizeof(*(a)))
  39. uint8_t txbuffer1[] = "USART synchronous example: USART0 -> SPI0 using TXE and RXNE Flags";
  40. uint8_t txbuffer2[] = "USART synchronous example: SPI0 -> USART0 using TXE and RXNE Flags";
  41. uint8_t rxbuffer1[txbuffer_size2];
  42. uint8_t rxbuffer2[txbuffer_size1];
  43. __IO uint8_t data_read1 = txbuffer_size2;
  44. __IO uint8_t data_read2 = txbuffer_size1;
  45. __IO uint8_t tx_counter1 = 0, rx_counter1 = 0;
  46. __IO uint8_t tx_counter2 = 0, rx_counter2 = 0;
  47. ErrStatus state1 = ERROR;
  48. ErrStatus state2 = ERROR;
  49. void usart_config(void);
  50. void spi_config(void);
  51. void led_init(void);
  52. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length);
  53. /*!
  54. \brief main function
  55. \param[in] none
  56. \param[out] none
  57. \retval none
  58. */
  59. int main(void)
  60. {
  61. /* initialize leds */
  62. led_init();
  63. /* turn off LED2~5 */
  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. /* configure USART */
  69. usart_config();
  70. /* configure SPI */
  71. spi_config();
  72. while(data_read2--){
  73. while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)){
  74. }
  75. /* write one byte in the USART0 data register */
  76. usart_data_transmit(USART0, txbuffer1[tx_counter1++]);
  77. /* wait until end of transmit */
  78. while(RESET == usart_flag_get(USART0, USART_FLAG_TC)){
  79. }
  80. /* wait the byte is entirely received by SPI0 */
  81. while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE)){
  82. }
  83. /* store the received byte in the rxbuffer2 */
  84. rxbuffer2[rx_counter2++] = spi_i2s_data_receive(SPI0);
  85. }
  86. /* clear the USART0 data register */
  87. usart_data_receive(USART0);
  88. while(data_read1--){
  89. /* wait until end of transmit */
  90. while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE)){
  91. }
  92. /* write one byte in the SPI0 transmit data register */
  93. spi_i2s_data_transmit(SPI0, txbuffer2[tx_counter2++]);
  94. /* send a dummy byte to generate clock to slave */
  95. usart_data_transmit(USART0, DYMMY_BYTE);
  96. /* wait until end of transmit */
  97. while(RESET == usart_flag_get(USART0, USART_FLAG_TC)){
  98. }
  99. /* wait the byte is entirely received by USART0 */
  100. while(RESET == usart_flag_get(USART0, USART_FLAG_RBNE)){
  101. }
  102. /* store the received byte in the rxbuffer1 */
  103. rxbuffer1[rx_counter1++] = usart_data_receive(USART0);
  104. }
  105. /* check the received data with the send ones */
  106. state1 = memory_compare(txbuffer1, rxbuffer2, txbuffer_size1);
  107. state2 = memory_compare(txbuffer2, rxbuffer1, txbuffer_size2);
  108. if(SUCCESS == state1){
  109. /* if the data transmitted from USART0 and received by SPI0 are the same */
  110. gd_eval_led_on(LED2);
  111. gd_eval_led_on(LED4);
  112. }else{
  113. /* if the data transmitted from USART0 and received by SPI0 are not the same */
  114. gd_eval_led_off(LED2);
  115. gd_eval_led_off(LED4);
  116. }
  117. if(SUCCESS == state2){
  118. /* if the data transmitted from SPI0 and received by USART0 are the same */
  119. gd_eval_led_on(LED3);
  120. gd_eval_led_on(LED5);
  121. }else{
  122. /* if the data transmitted from SPI0 and received by USART0 are not the same */
  123. gd_eval_led_off(LED3);
  124. gd_eval_led_off(LED5);
  125. }
  126. while(1){
  127. }
  128. }
  129. /*!
  130. \brief configure USART
  131. \param[in] none
  132. \param[out] none
  133. \retval none
  134. */
  135. void usart_config(void)
  136. {
  137. rcu_periph_clock_enable(RCU_GPIOA);
  138. rcu_periph_clock_enable(RCU_USART0);
  139. rcu_periph_clock_enable(RCU_AF);
  140. /* configure USART Tx as alternate function push-pull */
  141. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9|GPIO_PIN_8);
  142. gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  143. /* configure USART synchronous mode */
  144. usart_synchronous_clock_enable(USART0);
  145. usart_synchronous_clock_config(USART0, USART_CLEN_EN, USART_CPH_2CK, USART_CPL_HIGH);
  146. usart_baudrate_set(USART0, 115200);
  147. /* configure USART transmitter */
  148. usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  149. /* configure USART receiver */
  150. usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  151. /* enable USART */
  152. usart_enable(USART0);
  153. }
  154. /*!
  155. \brief configure SPI
  156. \param[in] none
  157. \param[out] none
  158. \retval none
  159. */
  160. void spi_config(void)
  161. {
  162. spi_parameter_struct spi_init_parameter;
  163. rcu_periph_clock_enable(RCU_GPIOA);
  164. rcu_periph_clock_enable(RCU_SPI0);
  165. rcu_periph_clock_enable(RCU_AF);
  166. spi_i2s_deinit(SPI0);
  167. gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_5 | GPIO_PIN_6);
  168. gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_10MHZ, GPIO_PIN_7);
  169. /* configure SPI0 */
  170. spi_init_parameter.device_mode = SPI_SLAVE;
  171. spi_init_parameter.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  172. spi_init_parameter.frame_size = SPI_FRAMESIZE_8BIT;
  173. spi_init_parameter.nss = SPI_NSS_SOFT;
  174. spi_init_parameter.endian = SPI_ENDIAN_LSB;
  175. spi_init_parameter.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  176. spi_init_parameter.prescale = SPI_PSC_32;
  177. spi_init(SPI0, &spi_init_parameter);
  178. /* SPI0 enable */
  179. spi_enable(SPI0);
  180. }
  181. /*!
  182. \brief initialize leds
  183. \param[in] none
  184. \param[out] none
  185. \retval none
  186. */
  187. void led_init(void)
  188. {
  189. gd_eval_led_init(LED2);
  190. gd_eval_led_init(LED3);
  191. gd_eval_led_init(LED4);
  192. gd_eval_led_init(LED5);
  193. }
  194. /*!
  195. \brief memory compare function
  196. \param[in] src: source data
  197. \param[in] dst: destination data
  198. \param[in] length: the compare data length
  199. \param[out] none
  200. \retval ErrStatus: ERROR or SUCCESS
  201. */
  202. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  203. {
  204. while(length--){
  205. if(*src++ != *dst++){
  206. return ERROR;
  207. }
  208. }
  209. return SUCCESS;
  210. }