main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*!
  2. \file main.c
  3. \brief master 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. #define I2C_10BIT_ADDRESS 0
  34. #define I2C0_OWN_ADDRESS7 0x72
  35. #define I2C0_SLAVE_ADDRESS7 0x82
  36. #define I2C0_SLAVE_ADDRESS10 0x0322
  37. uint8_t i2c_receiver[16];
  38. void rcu_config(void);
  39. void gpio_config(void);
  40. void i2c_config(void);
  41. /*!
  42. \brief main function
  43. \param[in] none
  44. \param[out] none
  45. \retval none
  46. */
  47. int main(void)
  48. {
  49. uint8_t i;
  50. #if I2C_10BIT_ADDRESS
  51. uint8_t slave10_first_byte, slave10_second_byte;
  52. #endif
  53. /* configure RCU */
  54. rcu_config();
  55. /* configure GPIO */
  56. gpio_config();
  57. /* configure I2C */
  58. i2c_config();
  59. /* wait until I2C bus is idle */
  60. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  61. /* send a start condition to I2C bus */
  62. i2c_start_on_bus(I2C0);
  63. /* wait until SBSEND bit is set */
  64. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  65. #if I2C_10BIT_ADDRESS
  66. slave10_first_byte = (0xF0) | (uint8_t)((I2C0_SLAVE_ADDRESS10 & 0x0300) >> 7);
  67. /* send slave address first byte to I2C bus */
  68. i2c_master_addressing(I2C0, slave10_first_byte, I2C_TRANSMITTER);
  69. /* wait until ADD10SEND bit is set */
  70. while(!i2c_flag_get(I2C0, I2C_FLAG_ADD10SEND));
  71. /* the second byte contains the remaining 8 bits of the 10-bit address */
  72. slave10_second_byte = (uint8_t)(I2C0_SLAVE_ADDRESS10 & 0x00FF);
  73. /* send slave address 2nd byte to I2C bus */
  74. i2c_master_addressing(I2C0, slave10_second_byte, I2C_TRANSMITTER);
  75. /* wait until ADDSEND bit is set */
  76. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  77. /* clear ADDSEND bit */
  78. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  79. /* send a start condition to I2C bus */
  80. i2c_start_on_bus(I2C0);
  81. /* wait until SBSEND bit is set */
  82. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  83. /* send slave address first byte to I2C bus */
  84. i2c_master_addressing(I2C0, slave10_first_byte, I2C_RECEIVER);
  85. #else
  86. /* send slave address to I2C bus */
  87. i2c_master_addressing(I2C0, I2C0_SLAVE_ADDRESS7, I2C_RECEIVER);
  88. #endif
  89. /* wait until ADDSEND bit is set */
  90. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  91. /* clear ADDSEND bit */
  92. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  93. for(i = 0; i < 16; i++) {
  94. if(13 == i) {
  95. /* wait until the second last data byte is received into the shift register */
  96. while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
  97. /* disable acknowledge */
  98. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  99. }
  100. /* wait until the RBNE bit is set */
  101. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  102. /* read a data from I2C_DATA */
  103. i2c_receiver[i] = i2c_data_receive(I2C0);
  104. }
  105. /* send a stop condition to I2C bus */
  106. i2c_stop_on_bus(I2C0);
  107. /* wait until stop condition generate */
  108. while(I2C_CTL0(I2C0) & 0x0200);
  109. /* enable acknowledge */
  110. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  111. while(1) {
  112. }
  113. }
  114. /*!
  115. \brief enable the peripheral clock
  116. \param[in] none
  117. \param[out] none
  118. \retval none
  119. */
  120. void rcu_config(void)
  121. {
  122. /* enable GPIOB clock */
  123. rcu_periph_clock_enable(RCU_GPIOB);
  124. /* enable I2C0 clock */
  125. rcu_periph_clock_enable(RCU_I2C0);
  126. }
  127. /*!
  128. \brief cofigure the GPIO ports
  129. \param[in] none
  130. \param[out] none
  131. \retval none
  132. */
  133. void gpio_config(void)
  134. {
  135. /* connect PB6 to I2C0_SCL */
  136. /* connect PB7 to I2C0_SDA */
  137. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
  138. }
  139. /*!
  140. \brief cofigure the I2C0 interfaces
  141. \param[in] none
  142. \param[out] none
  143. \retval none
  144. */
  145. void i2c_config(void)
  146. {
  147. /* configure I2C clock */
  148. i2c_clock_config(I2C0, 400000, I2C_DTCY_2);
  149. /* configure I2C address */
  150. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
  151. /* enable I2C0 */
  152. i2c_enable(I2C0);
  153. /* enable acknowledge */
  154. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  155. }