main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*!
  2. \file main.c
  3. \brief master receiver
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #define I2C_10BIT_ADDRESS 0
  11. #define I2C0_OWN_ADDRESS7 0x72
  12. #define I2C0_SLAVE_ADDRESS7 0x82
  13. #define I2C0_SLAVE_ADDRESS10 0x0322
  14. uint8_t i2c_receiver[16];
  15. void rcu_config(void);
  16. void gpio_config(void);
  17. void i2c_config(void);
  18. /*!
  19. \brief main function
  20. \param[in] none
  21. \param[out] none
  22. \retval none
  23. */
  24. int main(void)
  25. {
  26. uint8_t i;
  27. uint8_t slave10_first_byte,slave10_second_byte;
  28. /* RCU config */
  29. rcu_config();
  30. /* GPIO config */
  31. gpio_config();
  32. /* I2C config */
  33. i2c_config();
  34. /* wait until I2C bus is idle */
  35. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  36. /* send a start condition to I2C bus */
  37. i2c_start_on_bus(I2C0);
  38. /* wait until SBSEND bit is set */
  39. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  40. #if I2C_10BIT_ADDRESS
  41. slave10_first_byte = (0xF0) | (uint8_t)((I2C0_SLAVE_ADDRESS10 & 0x0300)>>7);
  42. /* send slave address first byte to I2C bus */
  43. i2c_master_addressing(I2C0, slave10_first_byte, I2C_TRANSMITTER);
  44. /* wait until ADD10SEND bit is set */
  45. while(!i2c_flag_get(I2C0, I2C_FLAG_ADD10SEND));
  46. /* the second byte contains the remaining 8 bits of the 10-bit address */
  47. slave10_second_byte = (uint8_t)(I2C0_SLAVE_ADDRESS10 & 0x00FF);
  48. /* send slave address 2nd byte to I2C bus */
  49. i2c_master_addressing(I2C0, slave10_second_byte, I2C_TRANSMITTER);
  50. /* wait until ADDSEND bit is set */
  51. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  52. /* clear ADDSEND bit */
  53. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  54. /* send a start condition to I2C bus */
  55. i2c_start_on_bus(I2C0);
  56. /* wait until SBSEND bit is set */
  57. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  58. /* send slave address first byte to I2C bus */
  59. i2c_master_addressing(I2C0, slave10_first_byte, I2C_RECEIVER);
  60. #else
  61. /* send slave address to I2C bus */
  62. i2c_master_addressing(I2C0, I2C0_SLAVE_ADDRESS7, I2C_RECEIVER);
  63. #endif
  64. /* wait until ADDSEND bit is set */
  65. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  66. /* clear ADDSEND bit */
  67. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  68. for(i=0; i<16; i++){
  69. if(13 == i){
  70. /* wait until the second last data byte is received into the shift register */
  71. while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
  72. /* disable acknowledge */
  73. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  74. }
  75. /* wait until the RBNE bit is set */
  76. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  77. /* read a data from I2C_DATA */
  78. i2c_receiver[i] = i2c_data_receive(I2C0);
  79. }
  80. /* send a stop condition to I2C bus */
  81. i2c_stop_on_bus(I2C0);
  82. while(I2C_CTL0(I2C0)&0x0200);
  83. /* enable acknowledge */
  84. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  85. while(1){
  86. }
  87. }
  88. /*!
  89. \brief enable the peripheral clock
  90. \param[in] none
  91. \param[out] none
  92. \retval none
  93. */
  94. void rcu_config(void)
  95. {
  96. /* enable GPIOB clock */
  97. rcu_periph_clock_enable(RCU_GPIOB);
  98. /* enable I2C0 clock */
  99. rcu_periph_clock_enable(RCU_I2C0);
  100. }
  101. /*!
  102. \brief cofigure the GPIO ports
  103. \param[in] none
  104. \param[out] none
  105. \retval none
  106. */
  107. void gpio_config(void)
  108. {
  109. /* connect PB6 to I2C0_SCL */
  110. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  111. /* connect PB7 to I2C0_SDA */
  112. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  113. /* configure GPIO pins of I2C0 */
  114. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  115. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  116. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  117. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  118. }
  119. /*!
  120. \brief cofigure the I2C0 and I2C1 interfaces
  121. \param[in] none
  122. \param[out] none
  123. \retval none
  124. */
  125. void i2c_config(void)
  126. {
  127. /* I2C clock configure */
  128. i2c_clock_config(I2C0, 400000, I2C_DTCY_2);
  129. /* I2C address configure */
  130. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
  131. /* enable I2C0 */
  132. i2c_enable(I2C0);
  133. /* enable acknowledge */
  134. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  135. }