main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*!
  2. \file main.c
  3. \brief master receiver and slave transmitter in fast mode plus
  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. /* initialize led */
  31. gd_eval_led_init(LED1);
  32. gd_eval_led_init(LED2);
  33. /* RCU config */
  34. rcu_config();
  35. /* GPIO config */
  36. gpio_config();
  37. /* I2C config */
  38. i2c_config();
  39. for(i=0; i<16; i++){
  40. i2c_transmitter[i] = i + 0x80;
  41. }
  42. /* wait until I2C bus is idle */
  43. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  44. /* send a start condition to I2C bus */
  45. i2c_start_on_bus(I2C0);
  46. /* wait until SBSEND bit is set */
  47. while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
  48. /* send slave address to I2C bus */
  49. i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_RECEIVER);
  50. /* wait until ADDSEND bit is set */
  51. while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
  52. /* clear ADDSEND bit */
  53. i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
  54. /* wait until ADDSEND bit is set */
  55. while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
  56. /* clear ADDSEND bit */
  57. i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
  58. for(i=0; i<15; i++){
  59. /* send a data byte */
  60. i2c_data_transmit(I2C1, i2c_transmitter[i]);
  61. /* wait until the transmission data register is empty */
  62. while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
  63. /* wait until the RBNE bit is set */
  64. while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
  65. /* read a data from I2C_DATA */
  66. i2c_receiver[i] = i2c_data_receive(I2C0);
  67. }
  68. /* send a NACK for the last data byte */
  69. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  70. /* send a data byte */
  71. i2c_data_transmit(I2C1, i2c_transmitter[i]);
  72. /* wait until the transmission data register is empty */
  73. while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
  74. /* the master doesn't acknowledge for the last byte */
  75. while(!i2c_flag_get(I2C1, I2C_FLAG_AERR));
  76. /* send a stop condition to I2C bus */
  77. i2c_stop_on_bus(I2C0);
  78. while(I2C_CTL0(I2C0)&0x0200);
  79. i2c_receiver[i] = i2c_data_receive(I2C0);
  80. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  81. /* clear the bit of AE */
  82. i2c_flag_clear(I2C1, I2C_STAT0_AERR);
  83. /* compare the transmit buffer and the receive buffer */
  84. state = memory_compare(i2c_transmitter, i2c_receiver, 16);
  85. /* if success, LED1 and LED2 are on */
  86. if(SUCCESS == state){
  87. gd_eval_led_on(LED1);
  88. gd_eval_led_on(LED2);
  89. }else{
  90. /* if failed, LED1 and LED2 are off */
  91. gd_eval_led_off(LED1);
  92. gd_eval_led_off(LED2);
  93. }
  94. while(1){
  95. }
  96. }
  97. /*!
  98. \brief memory compare function
  99. \param[in] src : source data
  100. \param[in] dst : destination data
  101. \param[in] length : the compare data length
  102. \param[out] none
  103. \retval ErrStatus : ERROR or SUCCESS
  104. */
  105. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  106. {
  107. while(length--){
  108. if(*src++ != *dst++){
  109. return ERROR;
  110. }
  111. }
  112. return SUCCESS;
  113. }
  114. /*!
  115. \brief enable the peripheral clock
  116. \param[in] none
  117. \param[out] none
  118. \retval none
  119. */
  120. void rcu_config(void)
  121. {
  122. /* enable GPIOB clock */
  123. rcu_periph_clock_enable(RCU_GPIOB);
  124. /* enable I2C1 clock */
  125. rcu_periph_clock_enable(RCU_I2C1);
  126. /* enable I2C0 clock */
  127. rcu_periph_clock_enable(RCU_I2C0);
  128. }
  129. /*!
  130. \brief cofigure the GPIO ports
  131. \param[in] none
  132. \param[out] none
  133. \retval none
  134. */
  135. void gpio_config(void)
  136. {
  137. /* connect PB6 to I2C0_SCL */
  138. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  139. /* connect PB7 to I2C0_SDA */
  140. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  141. /* connect PB10 to I2C1_SCL */
  142. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
  143. /* connect PB11 to I2C1_SDA */
  144. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
  145. /* configure GPIO pins of I2C0 */
  146. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  147. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  148. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  149. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  150. /* configure GPIO pins of I2C1 */
  151. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_10);
  152. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  153. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_11);
  154. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  155. }
  156. /*!
  157. \brief cofigure the I2C0 and I2C1 interfaces
  158. \param[in] none
  159. \param[out] none
  160. \retval none
  161. */
  162. void i2c_config(void)
  163. {
  164. /* I2C clock configure */
  165. i2c_clock_config(I2C0, 1000000, I2C_DTCY_2);
  166. /* I2C address configure */
  167. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  168. /* enable I2C0 */
  169. i2c_enable(I2C0);
  170. /* enable acknowledge */
  171. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  172. i2c_clock_config(I2C1, 1000000, I2C_DTCY_2);
  173. /* I2C address configure */
  174. i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
  175. /* enable I2C1 */
  176. i2c_enable(I2C1);
  177. /* enable acknowledge */
  178. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  179. }