main.c 5.1 KB

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