main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!
  2. \file main.c
  3. \brief master transmitter slave receiver
  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. gd_eval_led_init(LED2);
  54. gd_eval_led_init(LED3);
  55. rcu_config();
  56. gpio_config();
  57. i2c_config();
  58. for(i = 0; i < 16; i++) {
  59. i2c_transmitter[i] = i + 0x80;
  60. }
  61. /* wait until I2C bus is idle */
  62. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  63. /* send a start condition to I2C bus */
  64. i2c_start_on_bus(I2C0);
  65. /* wait until SBSEND bit is set */
  66. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  67. /* send slave address to I2C bus*/
  68. i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_TRANSMITTER);
  69. /* wait until ADDSEND bit is set*/
  70. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  71. while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
  72. /* clear ADDSEND bit */
  73. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  74. i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
  75. for(i = 0; i < 16; i++) {
  76. /* send a data byte */
  77. i2c_data_transmit(I2C0, i2c_transmitter[i]);
  78. /* wait until the transmission data register is empty*/
  79. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  80. /* wait until the RBNE bit is set */
  81. while(!i2c_flag_get(I2C1, I2C_FLAG_RBNE));
  82. /* read a data from I2C_DATA */
  83. i2c_receiver[i] = i2c_data_receive(I2C1);
  84. }
  85. /* send a stop condition to I2C bus*/
  86. i2c_stop_on_bus(I2C0);
  87. /* wait until stop condition generate */
  88. while(I2C_CTL0(I2C0) & 0x0200);
  89. while(!i2c_flag_get(I2C1, I2C_FLAG_STPDET));
  90. /* clear the STPDET bit */
  91. i2c_enable(I2C1);
  92. state = memory_compare(i2c_transmitter, i2c_receiver, 16);
  93. if(SUCCESS == state) {
  94. gd_eval_led_on(LED2);
  95. gd_eval_led_on(LED3);
  96. } else {
  97. gd_eval_led_off(LED2);
  98. gd_eval_led_off(LED3);
  99. }
  100. while(1);
  101. }
  102. /*!
  103. \brief memory compare function
  104. \param[in] src : source data
  105. \param[in] dst : destination data
  106. \param[in] length : the compare data length
  107. \param[out] none
  108. \retval ErrStatus : ERROR or SUCCESS
  109. */
  110. ErrStatus memory_compare(uint8_t *src, uint8_t *dst, uint16_t length)
  111. {
  112. while(length--) {
  113. if(*src++ != *dst++) {
  114. return ERROR;
  115. }
  116. }
  117. return SUCCESS;
  118. }
  119. /*!
  120. \brief enable the peripheral clock
  121. \param[in] none
  122. \param[out] none
  123. \retval none
  124. */
  125. void rcu_config(void)
  126. {
  127. /* enable GPIOB clock */
  128. rcu_periph_clock_enable(RCU_GPIOB);
  129. /* enable I2C1 clock */
  130. rcu_periph_clock_enable(RCU_I2C1);
  131. /* enable I2C0 clock */
  132. rcu_periph_clock_enable(RCU_I2C0);
  133. }
  134. /*!
  135. \brief cofigure the GPIO ports.
  136. \param[in] none
  137. \param[out] none
  138. \retval none
  139. */
  140. void gpio_config(void)
  141. {
  142. /* connect PB6 to I2C0_SCL */
  143. /* connect PB7 to I2C0_SDA */
  144. /* connect PB10 to I2C1_SCL */
  145. /* connect PB11 to I2C1_SDA */
  146. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_11);
  147. }
  148. /*!
  149. \brief cofigure the I2C0 and I2C1 interfaces
  150. \param[in] none
  151. \param[out] none
  152. \retval none
  153. */
  154. void i2c_config(void)
  155. {
  156. /* configure I2C0 clock */
  157. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  158. /* configure I2C0 address */
  159. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  160. /* enable I2C0 */
  161. i2c_enable(I2C0);
  162. /* enable acknowledge */
  163. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  164. /* configure I2C1 clock */
  165. i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
  166. /* configure I2C1 address */
  167. i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
  168. /* enable I2C1 */
  169. i2c_enable(I2C1);
  170. /* enable acknowledge */
  171. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  172. }