main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*!
  2. \file main.c
  3. \brief master receiver two bytes
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #define I2C0_OWN_ADDRESS7 0x72
  11. #define I2C0_SLAVE_ADDRESS7 0x82
  12. uint8_t i2c_receiver[16];
  13. void rcu_config(void);
  14. void gpio_config(void);
  15. void i2c_config(void);
  16. /*!
  17. \brief main function
  18. \param[in] none
  19. \param[out] none
  20. \retval none
  21. */
  22. int main(void)
  23. {
  24. int i;
  25. /* RCU config */
  26. rcu_config();
  27. /* GPIO config */
  28. gpio_config();
  29. /* I2C config */
  30. i2c_config();
  31. i=0;
  32. /* send a NACK for the next data byte which will be received into the shift register */
  33. i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
  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. /* send slave address to I2C bus */
  41. i2c_master_addressing(I2C0, I2C0_SLAVE_ADDRESS7, I2C_RECEIVER);
  42. /* disable ACK before clearing ADDSEND bit */
  43. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  44. /* wait until ADDSEND bit is set */
  45. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  46. /* clear ADDSEND bit */
  47. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  48. /* Wait until the last data byte is received into the shift register */
  49. while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
  50. /* wait until the RBNE bit is set */
  51. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  52. /* read a data from I2C_DATA */
  53. i2c_receiver[i++] = i2c_data_receive(I2C0);
  54. /* wait until the RBNE bit is set */
  55. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  56. /* read a data from I2C_DATA */
  57. i2c_receiver[i++] = i2c_data_receive(I2C0);
  58. /* send a stop condition */
  59. i2c_stop_on_bus(I2C0);
  60. while(I2C_CTL0(I2C0)&0x0200);
  61. i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
  62. /* enable acknowledge */
  63. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  64. while(1){
  65. }
  66. }
  67. /*!
  68. \brief enable the peripheral clock
  69. \param[in] none
  70. \param[out] none
  71. \retval none
  72. */
  73. void rcu_config(void)
  74. {
  75. /* enable GPIOB clock */
  76. rcu_periph_clock_enable(RCU_GPIOB);
  77. /* enable I2C0 clock */
  78. rcu_periph_clock_enable(RCU_I2C0);
  79. }
  80. /*!
  81. \brief cofigure the GPIO ports
  82. \param[in] none
  83. \param[out] none
  84. \retval none
  85. */
  86. void gpio_config(void)
  87. {
  88. /* connect PB6 to I2C0_SCL */
  89. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  90. /* connect PB7 to I2C0_SDA */
  91. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  92. /* configure GPIO pins of I2C0 */
  93. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  94. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  95. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  96. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  97. }
  98. /*!
  99. \brief cofigure the I2C interface
  100. \param[in] none
  101. \param[out] none
  102. \retval none
  103. */
  104. void i2c_config(void)
  105. {
  106. /* I2C clock configure */
  107. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  108. /* I2C address configure */
  109. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
  110. /* enable I2C0 */
  111. i2c_enable(I2C0);
  112. /* enable acknowledge */
  113. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  114. }