main.c 6.1 KB

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