main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*!
  2. \file main.c
  3. \brief master 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 I2C0_OWN_ADDRESS7 0x72
  11. #define I2C0_SLAVE_ADDRESS7 0x82
  12. uint8_t i2c_transmitter[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. for(i=0; i<16; i++){
  32. i2c_transmitter[i]=i+0x80;
  33. }
  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_TRANSMITTER);
  42. /* wait until ADDSEND bit is set */
  43. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  44. /* clear ADDSEND bit */
  45. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  46. /* wait until the transmit data buffer is empty */
  47. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  48. for(i=0; i<16; i++){
  49. /* data transmission */
  50. i2c_data_transmit(I2C0, i2c_transmitter[i]);
  51. /* wait until the TBE bit is set */
  52. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  53. }
  54. /* send a stop condition to I2C bus */
  55. i2c_stop_on_bus(I2C0);
  56. while(I2C_CTL0(I2C0)&0x0200);
  57. /* infinite loop */
  58. while(1){
  59. }
  60. }
  61. /*!
  62. \brief enable the peripheral clock
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void rcu_config(void)
  68. {
  69. /* enable GPIOB clock */
  70. rcu_periph_clock_enable(RCU_GPIOB);
  71. /* enable I2C0 clock */
  72. rcu_periph_clock_enable(RCU_I2C0);
  73. }
  74. /*!
  75. \brief cofigure the GPIO ports
  76. \param[in] none
  77. \param[out] none
  78. \retval none
  79. */
  80. void gpio_config(void)
  81. {
  82. /* connect PB6 to I2C0_SCL */
  83. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  84. /* connect PB7 to I2C0_SDA */
  85. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  86. /* configure GPIO pins of I2C0 */
  87. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  88. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  89. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  90. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  91. }
  92. /*!
  93. \brief cofigure the I2C interface
  94. \param[in] none
  95. \param[out] none
  96. \retval none
  97. */
  98. void i2c_config(void)
  99. {
  100. /* I2C clock configure */
  101. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  102. /* I2C address configure */
  103. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  104. /* enable I2C0 */
  105. i2c_enable(I2C0);
  106. /* enable acknowledge */
  107. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  108. }