main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*!
  2. \file main.c
  3. \brief master receiver and slave transmitter interrupt
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #include "gd32f3x0.h"
  10. #include "I2C_IE.h"
  11. #include "gd32f3x0_eval.h"
  12. uint8_t i2c_buffer_transmitter[16];
  13. uint8_t i2c_buffer_receiver[16];
  14. volatile uint8_t* i2c_txbuffer;
  15. volatile uint8_t* i2c_rxbuffer;
  16. volatile uint16_t I2C_nBytes;
  17. volatile ErrStatus status;
  18. ErrStatus state = ERROR;
  19. void rcu_config(void);
  20. void gpio_config(void);
  21. void i2c_config(void);
  22. void i2c_nvic_config(void);
  23. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length);
  24. /*!
  25. \brief main function
  26. \param[in] none
  27. \param[out] none
  28. \retval none
  29. */
  30. int main(void)
  31. {
  32. int i;
  33. /* initialize LED1, LED2, as the transfer instruction */
  34. gd_eval_led_init(LED1);
  35. gd_eval_led_init(LED2);
  36. rcu_config();
  37. gpio_config();
  38. i2c_config();
  39. i2c_nvic_config();
  40. for(i=0; i<16; i++){
  41. i2c_buffer_transmitter[i] = i + 0x80;
  42. }
  43. /* initialize i2c_txbuffer, i2c_rxbuffer, I2C_nBytes and status */
  44. i2c_txbuffer = i2c_buffer_transmitter;
  45. i2c_rxbuffer = i2c_buffer_receiver;
  46. I2C_nBytes = 16;
  47. status = ERROR;
  48. /* enable the I2C0 interrupt */
  49. i2c_interrupt_enable(I2C0, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV);
  50. /* enable the I2C1 interrupt */
  51. i2c_interrupt_enable(I2C1, I2C_INT_ERR | I2C_INT_BUF | I2C_INT_EV);
  52. if(2 == I2C_nBytes){
  53. /* send ACK for the next byte */
  54. i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
  55. }
  56. /* the master waits until the I2C bus is idle */
  57. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  58. /* the master sends a start condition to I2C bus */
  59. i2c_start_on_bus(I2C0);
  60. while(I2C_nBytes>0);
  61. while(SUCCESS != status);
  62. /* if the transfer is successfully completed, LED1 and LED2 is on */
  63. state = memory_compare(i2c_buffer_transmitter, i2c_buffer_receiver, 16);
  64. if(SUCCESS == state){
  65. /* if success, LED1 and LED2 are on */
  66. gd_eval_led_on(LED1);
  67. gd_eval_led_on(LED2);
  68. }else{
  69. /* if failed, LED1 and LED2 are off */
  70. gd_eval_led_off(LED1);
  71. gd_eval_led_off(LED2);
  72. }
  73. while(1)
  74. {}
  75. }
  76. /*!
  77. \brief memory compare function
  78. \param[in] src : source data
  79. \param[in] dst : destination data
  80. \param[in] length : the compare data length
  81. \param[out] none
  82. \retval ErrStatus : ERROR or SUCCESS
  83. */
  84. ErrStatus memory_compare(uint8_t* src, uint8_t* dst, uint16_t length)
  85. {
  86. while(length--){
  87. if(*src++ != *dst++){
  88. return ERROR;
  89. }
  90. }
  91. return SUCCESS;
  92. }
  93. /*!
  94. \brief enable the peripheral clock
  95. \param[in] none
  96. \param[out] none
  97. \retval none
  98. */
  99. void rcu_config(void)
  100. {
  101. /* enable GPIOB clock */
  102. rcu_periph_clock_enable(RCU_GPIOB);
  103. /* enable I2C1 clock */
  104. rcu_periph_clock_enable(RCU_I2C1);
  105. /* enable I2C0 clock */
  106. rcu_periph_clock_enable(RCU_I2C0);
  107. }
  108. /*!
  109. \brief cofigure the GPIO ports
  110. \param[in] none
  111. \param[out] none
  112. \retval none
  113. */
  114. void gpio_config(void)
  115. {
  116. /* connect PB6 to I2C0_SCL */
  117. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
  118. /* connect PB7 to I2C0_SDA */
  119. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
  120. /* connect PB10 to I2C1_SCL */
  121. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
  122. /* connect PB11 to I2C1_SDA */
  123. gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
  124. /* configure GPIO pins of I2C0 */
  125. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
  126. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  127. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
  128. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  129. /* configure GPIO pins of I2C1 */
  130. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_10);
  131. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
  132. gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_11);
  133. gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  134. }
  135. /*!
  136. \brief cofigure the I2C0 and I2C1 interfaces
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. void i2c_config(void)
  142. {
  143. /* I2C clock configure */
  144. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  145. /* I2C address configure */
  146. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  147. /* enable I2C0 */
  148. i2c_enable(I2C0);
  149. /* enable acknowledge */
  150. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  151. i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
  152. /* I2C address configure */
  153. i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
  154. /* enable I2C1 */
  155. i2c_enable(I2C1);
  156. /* enable acknowledge */
  157. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  158. }
  159. /*!
  160. \brief cofigure the NVIC peripheral
  161. \param[in] none
  162. \param[out] none
  163. \retval none
  164. */
  165. void i2c_nvic_config(void)
  166. {
  167. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  168. nvic_irq_enable(I2C0_EV_IRQn, 0, 3);
  169. nvic_irq_enable(I2C1_EV_IRQn, 0, 4);
  170. nvic_irq_enable(I2C0_ER_IRQn, 0, 2);
  171. nvic_irq_enable(I2C1_ER_IRQn, 0, 1);
  172. }