main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*!
  2. \file main.c
  3. \brief slave 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 I2C0_OWN_ADDRESS7 0x82
  11. uint8_t i2c_receiver[16];
  12. void rcu_config(void);
  13. void gpio_config(void);
  14. void i2c_config(void);
  15. /*!
  16. \brief main function
  17. \param[in] none
  18. \param[out] none
  19. \retval none
  20. */
  21. int main(void)
  22. {
  23. int i;
  24. /* RCU config */
  25. rcu_config();
  26. /* GPIO config */
  27. gpio_config();
  28. /* I2C config */
  29. i2c_config();
  30. i=0;
  31. /* wait until ADDSEND bit is set */
  32. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  33. /* clear ADDSEND bit */
  34. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  35. for(i=0; i<16; i++){
  36. /* wait until the RBNE bit is set */
  37. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  38. /* read a data byte from I2C_DATA */
  39. i2c_receiver[i] = i2c_data_receive(I2C0);
  40. }
  41. /* wait until the STPDET bit is set */
  42. while(!i2c_flag_get(I2C0, I2C_FLAG_STPDET));
  43. /* clear the STPDET bit */
  44. i2c_enable(I2C0);
  45. while(1){
  46. }
  47. }
  48. /*!
  49. \brief enable the peripheral clock
  50. \param[in] none
  51. \param[out] none
  52. \retval none
  53. */
  54. void rcu_config(void)
  55. {
  56. /* enable GPIOB clock */
  57. rcu_periph_clock_enable(RCU_GPIOB);
  58. /* enable I2C0 clock */
  59. rcu_periph_clock_enable(RCU_I2C0);
  60. }
  61. /*!
  62. \brief cofigure the GPIO ports
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void gpio_config(void)
  68. {
  69. /* connect PB6 to I2C0_SCL */
  70. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  71. /* connect PB7 to I2C0_SDA */
  72. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  73. /* configure GPIO pins of I2C0 */
  74. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  75. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  76. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  77. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  78. }
  79. /*!
  80. \brief cofigure the I2C interface
  81. \param[in] none
  82. \param[out] none
  83. \retval none
  84. */
  85. void i2c_config(void)
  86. {
  87. /* I2C clock configure */
  88. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  89. /* I2C address configure */
  90. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
  91. /* enable I2C0 */
  92. i2c_enable(I2C0);
  93. /* enable acknowledge */
  94. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  95. }