main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. \file main.c
  3. \brief use the I2C bus to write and read EEPROM
  4. \version 2022-05-30, V1.0.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2022, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include "gd32f30x.h"
  31. #include "gd32f307c_eval.h"
  32. #include "systick.h"
  33. #include "i2c.h"
  34. #include "at24cxx.h"
  35. uint8_t count = 0;
  36. void led_turn_on(uint8_t led_number);
  37. void led_config(void);
  38. void i2c_nvic_config(void);
  39. /*!
  40. \brief configure the LEDs
  41. \param[in] none
  42. \param[out] none
  43. \retval none
  44. */
  45. void led_config(void)
  46. {
  47. gd_eval_led_init(LED2);
  48. gd_eval_led_init(LED3);
  49. gd_eval_led_init(LED4);
  50. gd_eval_led_init(LED5);
  51. /* turn off LED2,LED3,LED4,LED5 */
  52. gd_eval_led_off(LED2);
  53. gd_eval_led_off(LED3);
  54. gd_eval_led_off(LED4);
  55. gd_eval_led_off(LED5);
  56. }
  57. /*!
  58. \brief main function
  59. \param[in] none
  60. \param[out] none
  61. \retval none
  62. */
  63. int main(void)
  64. {
  65. /* configure systick */
  66. systick_config();
  67. /* configure LEDs */
  68. led_config();
  69. /* configure USART */
  70. gd_eval_com_init(EVAL_COM0);
  71. printf("I2C-24C02 configured....\n\r");
  72. /* configure the NVIC */
  73. i2c_nvic_config();
  74. /* configure GPIO */
  75. gpio_config();
  76. /* configure I2C */
  77. i2c_config();
  78. /* initialize EEPROM */
  79. i2c_eeprom_init();
  80. printf("\r\nThe I2C is hardware interface ");
  81. printf("\r\nThe speed is %d", I2C_SPEED);
  82. if(I2C_OK == i2c_24c02_test()) {
  83. while(1) {
  84. /* turn off all LEDs */
  85. gd_eval_led_off(LED2);
  86. gd_eval_led_off(LED3);
  87. gd_eval_led_off(LED4);
  88. gd_eval_led_off(LED5);
  89. /* turn on a LED */
  90. led_turn_on(count % 4);
  91. count++;
  92. if(count >= 4) {
  93. count = 0;
  94. }
  95. delay_1ms(500);
  96. }
  97. }
  98. /* turn on all LEDs */
  99. gd_eval_led_on(LED2);
  100. gd_eval_led_on(LED3);
  101. gd_eval_led_on(LED4);
  102. gd_eval_led_on(LED5);
  103. while(1) {
  104. }
  105. }
  106. /*!
  107. \brief turn on a LED
  108. \param[in] led_number
  109. \param[out] none
  110. \retval none
  111. */
  112. void led_turn_on(uint8_t led_number)
  113. {
  114. switch(led_number) {
  115. case 0:
  116. gd_eval_led_on(LED2);
  117. break;
  118. case 1:
  119. gd_eval_led_on(LED3);
  120. break;
  121. case 2:
  122. gd_eval_led_on(LED4);
  123. break;
  124. case 3:
  125. gd_eval_led_on(LED5);
  126. break;
  127. default:
  128. gd_eval_led_on(LED2);
  129. gd_eval_led_on(LED3);
  130. gd_eval_led_on(LED4);
  131. gd_eval_led_on(LED5);
  132. break;
  133. }
  134. }
  135. /*!
  136. \brief configure the NVIC peripheral
  137. \param[in] none
  138. \param[out] none
  139. \retval none
  140. */
  141. void i2c_nvic_config(void)
  142. {
  143. nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
  144. nvic_irq_enable(I2C0_EV_IRQn, 0, 2);
  145. nvic_irq_enable(I2C0_ER_IRQn, 0, 1);
  146. }
  147. /* retarget the C library printf function to the usart */
  148. int fputc(int ch, FILE *f)
  149. {
  150. usart_data_transmit(EVAL_COM0, (uint8_t) ch);
  151. while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
  152. return ch;
  153. }