main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*!
  2. \file main.c
  3. \brief slave transmitter
  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 0x82
  12. #define I2C0_SLAVE_ADDRESS7 0x72
  13. #define I2C0_OWN_ADDRESS10 0x0322
  14. uint8_t i2c_transmitter[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. int i;
  27. /* RCU config */
  28. rcu_config();
  29. /* GPIO config */
  30. gpio_config();
  31. /* I2C config */
  32. i2c_config();
  33. for(i=0; i<16; i++){
  34. i2c_transmitter[i] = i+0x80;
  35. }
  36. #if I2C_10BIT_ADDRESS
  37. /* wait until ADDSEND bit is set */
  38. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  39. /* clear ADDSEND bit */
  40. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  41. /* wait until ADDSEND bit is set */
  42. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  43. /* clear ADDSEND bit */
  44. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  45. #else
  46. /* wait until ADDSEND bit is set */
  47. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  48. /* clear ADDSEND bit */
  49. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  50. #endif
  51. /* wait until the transmission data register is empty */
  52. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  53. for(i=0;i<16;i++){
  54. /* send a data byte */
  55. i2c_data_transmit(I2C0, i2c_transmitter[i]);
  56. /* wait until the transmission data register is empty */
  57. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  58. }
  59. /* the master doesn't acknowledge for the last byte */
  60. while(!i2c_flag_get(I2C0, I2C_FLAG_AERR));
  61. /* clear the bit of AERR */
  62. i2c_flag_clear(I2C0, I2C_FLAG_AERR);
  63. while(1){
  64. }
  65. }
  66. /*!
  67. \brief enable the peripheral clock
  68. \param[in] none
  69. \param[out] none
  70. \retval none
  71. */
  72. void rcu_config(void)
  73. {
  74. /* enable GPIOB clock */
  75. rcu_periph_clock_enable(RCU_GPIOB);
  76. /* enable I2C0 clock */
  77. rcu_periph_clock_enable(RCU_I2C0);
  78. }
  79. /*!
  80. \brief cofigure the GPIO ports
  81. \param[in] none
  82. \param[out] none
  83. \retval none
  84. */
  85. void gpio_config(void)
  86. {
  87. /* connect PB6 to I2C0_SCL */
  88. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  89. /* connect PB7 to I2C0_SDA */
  90. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  91. /* configure GPIO pins of I2C0 */
  92. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  93. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  94. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  95. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  96. }
  97. /*!
  98. \brief cofigure the I2C interface
  99. \param[in] none
  100. \param[out] none
  101. \retval none
  102. */
  103. void i2c_config(void)
  104. {
  105. /* I2C clock configure */
  106. i2c_clock_config(I2C0, 400000, I2C_DTCY_2);
  107. /* I2C address configure */
  108. #if I2C_10BIT_ADDRESS
  109. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_10BITS, I2C0_OWN_ADDRESS10);
  110. #else
  111. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7);
  112. #endif
  113. /* enable I2C0 */
  114. i2c_enable(I2C0);
  115. /* enable acknowledge */
  116. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  117. }