main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. \file main.c
  3. \brief master receiver one byte
  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_SLAVE_ADDRESS7 0x72
  11. #define I2C1_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. /* wait until I2C bus is idle */
  33. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  34. /* send a start condition to I2C bus */
  35. i2c_start_on_bus(I2C0);
  36. /* wait until SBSEND bit is set */
  37. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  38. /* send slave address to I2C bus */
  39. i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_RECEIVER);
  40. /* wait until ADDSEND bit is set */
  41. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  42. /* N=1,reset ACKEN bit before clearing ADDRSEND bit */
  43. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  44. /* clear ADDSEND bit */
  45. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  46. /* N=1,send stop condition after clearing ADDRSEND bit */
  47. i2c_stop_on_bus(I2C0);
  48. /* wait until the RBNE bit is set */
  49. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  50. /* read a data from I2C_DATA */
  51. i2c_receiver[i++] = i2c_data_receive(I2C0);
  52. while(I2C_CTL0(I2C0)&0x0200);
  53. /* enable Acknowledge */
  54. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  55. while(1){
  56. }
  57. }
  58. /*!
  59. \brief enable the peripheral clock
  60. \param[in] none
  61. \param[out] none
  62. \retval none
  63. */
  64. void rcu_config(void)
  65. {
  66. /* enable GPIOB clock */
  67. rcu_periph_clock_enable(RCU_GPIOB);
  68. /* enable I2C0 clock */
  69. rcu_periph_clock_enable(RCU_I2C0);
  70. }
  71. /*!
  72. \brief cofigure the GPIO ports
  73. \param[in] none
  74. \param[out] none
  75. \retval none
  76. */
  77. void gpio_config(void)
  78. {
  79. /* connect PB6 to I2C0_SCL */
  80. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  81. /* connect PB7 to I2C0_SDA */
  82. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  83. /* configure GPIO pins of I2C0 */
  84. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  85. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  86. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  87. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  88. }
  89. /*!
  90. \brief cofigure the I2C interface
  91. \param[in] none
  92. \param[out] none
  93. \retval none
  94. */
  95. void i2c_config(void)
  96. {
  97. /* I2C clock configure */
  98. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  99. /* I2C address configure */
  100. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  101. /* enable I2C0 */
  102. i2c_enable(I2C0);
  103. /* enable acknowledge */
  104. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  105. }