main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*!
  2. \file main.c
  3. \brief master receiver two bytes
  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 I2C0_OWN_ADDRESS7 0x72
  34. #define I2C0_SLAVE_ADDRESS7 0x82
  35. uint8_t i2c_receiver[16];
  36. void rcu_config(void);
  37. void gpio_config(void);
  38. void i2c_config(void);
  39. /*!
  40. \brief main function
  41. \param[in] none
  42. \param[out] none
  43. \retval none
  44. */
  45. int main(void)
  46. {
  47. uint8_t i;
  48. /* cofigure RCU */
  49. rcu_config();
  50. /* cofigure GPIO */
  51. gpio_config();
  52. /* cofigure I2C */
  53. i2c_config();
  54. i = 0;
  55. /* send a NACK for the next data byte which will be received into the shift register */
  56. i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
  57. /* wait until I2C bus is idle */
  58. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  59. /* send a start condition to I2C bus */
  60. i2c_start_on_bus(I2C0);
  61. /* wait until SBSEND bit is set */
  62. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  63. /* send slave address to I2C bus */
  64. i2c_master_addressing(I2C0, I2C0_SLAVE_ADDRESS7, I2C_RECEIVER);
  65. /* disable ACK before clearing ADDSEND bit */
  66. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  67. /* wait until ADDSEND bit is set */
  68. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  69. /* clear ADDSEND bit */
  70. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  71. /* Wait until the last data byte is received into the shift register */
  72. while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
  73. /* wait until the RBNE bit is set */
  74. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  75. /* read a data from I2C_DATA */
  76. i2c_receiver[i++] = i2c_data_receive(I2C0);
  77. /* wait until the RBNE bit is set */
  78. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  79. /* read a data from I2C_DATA */
  80. i2c_receiver[i++] = i2c_data_receive(I2C0);
  81. /* send a stop condition */
  82. i2c_stop_on_bus(I2C0);
  83. /* wait until stop condition generate */
  84. while(I2C_CTL0(I2C0) & 0x0200);
  85. i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
  86. /* enable acknowledge */
  87. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  88. while(1) {
  89. }
  90. }
  91. /*!
  92. \brief enable the peripheral clock
  93. \param[in] none
  94. \param[out] none
  95. \retval none
  96. */
  97. void rcu_config(void)
  98. {
  99. /* enable GPIOB clock */
  100. rcu_periph_clock_enable(RCU_GPIOB);
  101. /* enable I2C0 clock */
  102. rcu_periph_clock_enable(RCU_I2C0);
  103. }
  104. /*!
  105. \brief cofigure the GPIO ports
  106. \param[in] none
  107. \param[out] none
  108. \retval none
  109. */
  110. void gpio_config(void)
  111. {
  112. /* connect PB6 to I2C0_SCL */
  113. /* connect PB7 to I2C0_SDA */
  114. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
  115. }
  116. /*!
  117. \brief cofigure the I2C0 interfaces
  118. \param[in] none
  119. \param[out] none
  120. \retval none
  121. */
  122. void i2c_config(void)
  123. {
  124. /* cofigure I2C clock */
  125. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  126. /* cofigure I2C address */
  127. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
  128. /* enable I2C0 */
  129. i2c_enable(I2C0);
  130. /* enable acknowledge */
  131. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  132. }