main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*!
  2. \file main.c
  3. \brief master receiver and slave transmitter in fast mode plus
  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 <stdio.h>
  34. #include "gd32f307c_eval.h"
  35. #define I2C0_SLAVE_ADDRESS7 0x82
  36. #define I2C1_SLAVE_ADDRESS7 0x72
  37. uint8_t i2c_transmitter[16];
  38. uint8_t i2c_receiver[16];
  39. __IO ErrStatus state = ERROR;
  40. void rcu_config(void);
  41. void gpio_config(void);
  42. void i2c_config(void);
  43. ErrStatus memory_compare(uint8_t *src, uint8_t *dst, uint16_t length);
  44. /*!
  45. \brief main function
  46. \param[in] none
  47. \param[out] none
  48. \retval none
  49. */
  50. int main(void)
  51. {
  52. uint8_t i;
  53. /* initialize led */
  54. gd_eval_led_init(LED2);
  55. gd_eval_led_init(LED3);
  56. /* configure RCU */
  57. rcu_config();
  58. /* configure GPIO */
  59. gpio_config();
  60. /* configure I2C */
  61. i2c_config();
  62. for(i = 0; i < 16; i++) {
  63. i2c_transmitter[i] = i + 0x80;
  64. }
  65. /* wait until I2C bus is idle */
  66. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  67. /* send a start condition to I2C bus */
  68. i2c_start_on_bus(I2C0);
  69. /* wait until SBSEND bit is set */
  70. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  71. /* send slave address to I2C bus */
  72. i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_RECEIVER);
  73. /* wait until ADDSEND bit is set */
  74. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  75. /* clear ADDSEND bit */
  76. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  77. /* wait until ADDSEND bit is set */
  78. while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
  79. /* clear ADDSEND bit */
  80. i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
  81. for(i = 0; i < 15; i++) {
  82. /* send a data byte */
  83. i2c_data_transmit(I2C1, i2c_transmitter[i]);
  84. /* wait until the transmission data register is empty */
  85. while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
  86. /* wait until the RBNE bit is set */
  87. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  88. /* read a data from I2C_DATA */
  89. i2c_receiver[i] = i2c_data_receive(I2C0);
  90. }
  91. /* send a NACK for the last data byte */
  92. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  93. /* send a data byte */
  94. i2c_data_transmit(I2C1, i2c_transmitter[i]);
  95. /* wait until the transmission data register is empty */
  96. while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
  97. /* the master doesn't acknowledge for the last byte */
  98. while(!i2c_flag_get(I2C1, I2C_FLAG_AERR));
  99. /* send a stop condition to I2C bus */
  100. i2c_stop_on_bus(I2C0);
  101. while(I2C_CTL0(I2C0) & 0x0200);
  102. i2c_receiver[i] = i2c_data_receive(I2C0);
  103. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  104. /* clear the bit of AE */
  105. i2c_flag_clear(I2C1, I2C_FLAG_AERR);
  106. /* compare the transmit buffer and the receive buffer */
  107. state = memory_compare(i2c_transmitter, i2c_receiver, 16);
  108. /* if success, LED2 and LED3 are on */
  109. if(SUCCESS == state) {
  110. gd_eval_led_on(LED2);
  111. gd_eval_led_on(LED3);
  112. } else {
  113. /* if failed, LED2 and LED3 are off */
  114. gd_eval_led_off(LED2);
  115. gd_eval_led_off(LED3);
  116. }
  117. while(1) {
  118. }
  119. }
  120. /*!
  121. \brief memory compare function
  122. \param[in] src : source data
  123. \param[in] dst : destination data
  124. \param[in] length : the compare data length
  125. \param[out] none
  126. \retval ErrStatus : ERROR or SUCCESS
  127. */
  128. ErrStatus memory_compare(uint8_t *src, uint8_t *dst, uint16_t length)
  129. {
  130. while(length--) {
  131. if(*src++ != *dst++) {
  132. return ERROR;
  133. }
  134. }
  135. return SUCCESS;
  136. }
  137. /*!
  138. \brief enable the peripheral clock
  139. \param[in] none
  140. \param[out] none
  141. \retval none
  142. */
  143. void rcu_config(void)
  144. {
  145. /* enable GPIOB clock */
  146. rcu_periph_clock_enable(RCU_GPIOB);
  147. /* enable I2C1 clock */
  148. rcu_periph_clock_enable(RCU_I2C1);
  149. /* enable I2C0 clock */
  150. rcu_periph_clock_enable(RCU_I2C0);
  151. }
  152. /*!
  153. \brief cofigure the GPIO ports
  154. \param[in] none
  155. \param[out] none
  156. \retval none
  157. */
  158. void gpio_config(void)
  159. {
  160. /* connect PB6 to I2C0_SCL */
  161. /* connect PB7 to I2C0_SDA */
  162. /* connect PB10 to I2C1_SCL */
  163. /* connect PB11 to I2C1_SDA */
  164. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_11);
  165. }
  166. /*!
  167. \brief cofigure the I2C0 and I2C1 interfaces
  168. \param[in] none
  169. \param[out] none
  170. \retval none
  171. */
  172. void i2c_config(void)
  173. {
  174. /* I2C clock configure */
  175. i2c_clock_config(I2C0, 1000000, I2C_DTCY_16_9);
  176. /* I2C address configure */
  177. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  178. /* enable I2C0 */
  179. i2c_enable(I2C0);
  180. /* enable acknowledge */
  181. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  182. i2c_clock_config(I2C1, 1000000, I2C_DTCY_16_9);
  183. /* I2C address configure */
  184. i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
  185. /* enable I2C1 */
  186. i2c_enable(I2C1);
  187. /* enable acknowledge */
  188. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  189. }