main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*!
  2. \file main.c
  3. \brief master receiver and slave transmitter interrupt
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include "I2C_IE.h"
  34. #include "gd32f307c_eval.h"
  35. uint8_t i2c_buffer_transmitter[16];
  36. uint8_t i2c_buffer_receiver[16];
  37. volatile uint8_t *i2c_txbuffer;
  38. volatile uint8_t *i2c_rxbuffer;
  39. volatile uint16_t I2C_nBytes;
  40. volatile ErrStatus status;
  41. ErrStatus state = ERROR;
  42. void rcu_config(void);
  43. void gpio_config(void);
  44. void i2c_config(void);
  45. void i2c_nvic_config(void);
  46. ErrStatus memory_compare(uint8_t *src, uint8_t *dst, uint16_t length);
  47. /*!
  48. \brief main function
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. int main(void)
  54. {
  55. uint8_t i;
  56. /* initialize LED2, LED3, as the transfer instruction */
  57. gd_eval_led_init(LED2);
  58. gd_eval_led_init(LED3);
  59. rcu_config();
  60. gpio_config();
  61. i2c_config();
  62. i2c_nvic_config();
  63. for(i = 0; i < 16; i++) {
  64. i2c_buffer_transmitter[i] = i + 0x80;
  65. }
  66. /* initialize i2c_txbuffer, i2c_rxbuffer, I2C_nBytes and status */
  67. i2c_txbuffer = i2c_buffer_transmitter;
  68. i2c_rxbuffer = i2c_buffer_receiver;
  69. I2C_nBytes = 16;
  70. status = ERROR;
  71. /* enable the I2C0 interrupt */
  72. i2c_interrupt_enable(I2C0, I2C_INT_ERR);
  73. i2c_interrupt_enable(I2C0, I2C_INT_BUF);
  74. i2c_interrupt_enable(I2C0, I2C_INT_EV);
  75. /* enable the I2C1 interrupt */
  76. i2c_interrupt_enable(I2C1, I2C_INT_ERR);
  77. i2c_interrupt_enable(I2C1, I2C_INT_BUF);
  78. i2c_interrupt_enable(I2C1, I2C_INT_EV);
  79. if(2 == I2C_nBytes) {
  80. /* send ACK for the next byte */
  81. i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT);
  82. }
  83. /* the master waits until the I2C bus is idle */
  84. while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
  85. /* the master sends a start condition to I2C bus */
  86. i2c_start_on_bus(I2C0);
  87. while(I2C_nBytes > 0);
  88. while(SUCCESS != status);
  89. /* if the transfer is successfully completed, LED2 and LED3 is on */
  90. state = memory_compare(i2c_buffer_transmitter, i2c_buffer_receiver, 16);
  91. if(SUCCESS == state) {
  92. /* if success, LED2 and LED3 are on */
  93. gd_eval_led_on(LED2);
  94. gd_eval_led_on(LED3);
  95. } else {
  96. /* if failed, LED2 and LED3 are off */
  97. gd_eval_led_off(LED2);
  98. gd_eval_led_off(LED3);
  99. }
  100. while(1)
  101. {}
  102. }
  103. /*!
  104. \brief memory compare function
  105. \param[in] src : source data
  106. \param[in] dst : destination data
  107. \param[in] length : the compare data length
  108. \param[out] none
  109. \retval ErrStatus : ERROR or SUCCESS
  110. */
  111. ErrStatus memory_compare(uint8_t *src, uint8_t *dst, uint16_t length)
  112. {
  113. while(length--) {
  114. if(*src++ != *dst++) {
  115. return ERROR;
  116. }
  117. }
  118. return SUCCESS;
  119. }
  120. /*!
  121. \brief enable the peripheral clock
  122. \param[in] none
  123. \param[out] none
  124. \retval none
  125. */
  126. void rcu_config(void)
  127. {
  128. /* enable GPIOB clock */
  129. rcu_periph_clock_enable(RCU_GPIOB);
  130. /* enable I2C1 clock */
  131. rcu_periph_clock_enable(RCU_I2C1);
  132. /* enable I2C0 clock */
  133. rcu_periph_clock_enable(RCU_I2C0);
  134. }
  135. /*!
  136. \brief cofigure the GPIO ports
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. void gpio_config(void)
  142. {
  143. /* connect PB6 to I2C0_SCL */
  144. /* connect PB7 to I2C0_SDA */
  145. /* connect PB10 to I2C1_SCL */
  146. /* connect PB11 to I2C1_SDA */
  147. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_11);
  148. }
  149. /*!
  150. \brief cofigure the I2C0 and I2C1 interfaces
  151. \param[in] none
  152. \param[out] none
  153. \retval none
  154. */
  155. void i2c_config(void)
  156. {
  157. /* configure I2C clock */
  158. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  159. /* configure I2C address */
  160. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
  161. /* enable I2C0 */
  162. i2c_enable(I2C0);
  163. /* enable acknowledge */
  164. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  165. i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
  166. /* I2C address configure */
  167. i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C1_SLAVE_ADDRESS7);
  168. /* enable I2C1 */
  169. i2c_enable(I2C1);
  170. /* enable acknowledge */
  171. i2c_ack_config(I2C1, I2C_ACK_ENABLE);
  172. }
  173. /*!
  174. \brief cofigure the NVIC peripheral
  175. \param[in] none
  176. \param[out] none
  177. \retval none
  178. */
  179. void i2c_nvic_config(void)
  180. {
  181. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  182. nvic_irq_enable(I2C0_EV_IRQn, 0, 3);
  183. nvic_irq_enable(I2C1_EV_IRQn, 0, 4);
  184. nvic_irq_enable(I2C0_ER_IRQn, 0, 2);
  185. nvic_irq_enable(I2C1_ER_IRQn, 0, 1);
  186. }