main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. \file main.c
  3. \brief master transmitter 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. #include <stdio.h>
  11. #include "gd32f3x0_eval.h"
  12. #define I2C0_SLAVE_ADDRESS7 0x82
  13. #define I2C1_SLAVE_ADDRESS7 0x72
  14. uint8_t i2c_transmitter[16];
  15. uint8_t i2c_receiver[16];
  16. ErrStatus state = ERROR;
  17. void rcu_config(void);
  18. void gpio_config(void);
  19. void i2c_config(void);
  20. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length);
  21. /*!
  22. \brief main function
  23. \param[in] none
  24. \param[out] none
  25. \retval none
  26. */
  27. int main(void)
  28. {
  29. int i;
  30. gd_eval_led_init(LED1);
  31. gd_eval_led_init(LED2);
  32. rcu_config();
  33. gpio_config();
  34. i2c_config();
  35. for(i=0;i<16;i++){
  36. i2c_transmitter[i]=i+0x80;
  37. }
  38. /* wait until I2C bus is idle */
  39. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  40. /* send a start condition to I2C bus */
  41. i2c_start_on_bus(I2C0);
  42. /* wait until SBSEND bit is set */
  43. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  44. /* send slave address to I2C bus*/
  45. i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_TRANSMITTER);
  46. /* wait until ADDSEND bit is set*/
  47. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  48. while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
  49. /* clear ADDSEND bit */
  50. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  51. i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
  52. for(i=0;i<16;i++){
  53. /* send a data byte */
  54. i2c_data_transmit(I2C0,i2c_transmitter[i]);
  55. /* wait until the transmission data register is empty*/
  56. while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
  57. /* wait until the RBNE bit is set */
  58. while(!i2c_flag_get(I2C1, I2C_FLAG_RBNE));
  59. /* read a data from I2C_DATA */
  60. i2c_receiver[i] = i2c_data_receive(I2C1);
  61. }
  62. /* send a stop condition to I2C bus*/
  63. i2c_stop_on_bus(I2C0);
  64. while(I2C_CTL0(I2C0)&0x0200);
  65. while(!i2c_flag_get(I2C1, I2C_FLAG_STPDET));
  66. /* clear the STPDET bit */
  67. i2c_enable(I2C0);
  68. state = memory_compare(i2c_transmitter, i2c_receiver,16);
  69. if(SUCCESS == state){
  70. gd_eval_led_on(LED1);
  71. gd_eval_led_on(LED2);
  72. }else{
  73. gd_eval_led_off(LED1);
  74. gd_eval_led_off(LED2);
  75. }
  76. while(1);
  77. }
  78. /*!
  79. \brief memory compare function
  80. \param[in] src : source data
  81. \param[in] dst : destination data
  82. \param[in] length : the compare data length
  83. \param[out] none
  84. \retval ErrStatus : ERROR or SUCCESS
  85. */
  86. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  87. {
  88. while(length--){
  89. if(*src++ != *dst++){
  90. return ERROR;
  91. }
  92. }
  93. return SUCCESS;
  94. }
  95. /*!
  96. \brief enable the peripheral clock
  97. \param[in] none
  98. \param[out] none
  99. \retval none
  100. */
  101. void rcu_config(void)
  102. {
  103. /* enable GPIOB clock */
  104. rcu_periph_clock_enable(RCU_GPIOB);
  105. /* enable I2C1 clock */
  106. rcu_periph_clock_enable(RCU_I2C1);
  107. /* enable I2C0 clock */
  108. rcu_periph_clock_enable(RCU_I2C0);
  109. }
  110. /*!
  111. \brief cofigure the GPIO ports.
  112. \param[in] none
  113. \param[out] none
  114. \retval none
  115. */
  116. void gpio_config(void)
  117. {
  118. /* connect PB6 to I2C0_SCL */
  119. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  120. /* connect PB7 to I2C0_SDA */
  121. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  122. /* connect PB10 to I2C1_SCL */
  123. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
  124. /* connect PB11 to I2C1_SDA */
  125. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
  126. /* configure GPIO pins of I2C0 */
  127. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  128. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  129. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  130. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  131. /* configure GPIO pins of I2C1 */
  132. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_10);
  133. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  134. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_11);
  135. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  136. }
  137. /*!
  138. \brief cofigure the I2C0 and I2C1 interfaces
  139. \param[in] none
  140. \param[out] none
  141. \retval none
  142. */
  143. void i2c_config(void)
  144. {
  145. /* configure I2C0 clock */
  146. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  147. /* configure I2C0 address */
  148. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  149. /* enable I2C0 */
  150. i2c_enable(I2C0);
  151. /* enable acknowledge */
  152. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  153. /* configure I2C1 clock */
  154. i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
  155. /* configure I2C1 address */
  156. i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
  157. /* enable I2C1 */
  158. i2c_enable(I2C1);
  159. /* enable acknowledge */
  160. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  161. }