i2c.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*!
  2. \file i2c.c
  3. \brief I2C configuration file
  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 "gd32f30x.h"
  30. #include "i2c.h"
  31. #include <stdio.h>
  32. #include "systick.h"
  33. /*!
  34. \brief configure the GPIO ports
  35. \param[in] none
  36. \param[out] none
  37. \retval none
  38. */
  39. void gpio_config(void)
  40. {
  41. /* enable GPIOB clock */
  42. rcu_periph_clock_enable(RCU_GPIO_I2C);
  43. /* connect PB6 to I2C_SCL */
  44. /* connect PB7 to I2C_SDA */
  45. gpio_init(I2C_SCL_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
  46. gpio_init(I2C_SDA_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
  47. }
  48. /*!
  49. \brief configure the I2C interfaces
  50. \param[in] none
  51. \param[out] none
  52. \retval none
  53. */
  54. void i2c_config(void)
  55. {
  56. /* enable I2C clock */
  57. rcu_periph_clock_enable(RCU_I2C);
  58. /* configure I2C clock */
  59. i2c_clock_config(I2CX, I2C_SPEED, I2C_DTCY_2);
  60. /* configure I2C address */
  61. i2c_mode_addr_config(I2CX, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2CX_SLAVE_ADDRESS7);
  62. /* enable I2CX */
  63. i2c_enable(I2CX);
  64. /* enable acknowledge */
  65. i2c_ack_config(I2CX, I2C_ACK_ENABLE);
  66. }
  67. /*!
  68. \brief reset i2c bus
  69. \param[in] none
  70. \param[out] none
  71. \retval none
  72. */
  73. void i2c_bus_reset()
  74. {
  75. i2c_deinit(I2CX);
  76. /* configure SDA/SCL for GPIO */
  77. GPIO_BC(I2C_SCL_PORT) |= I2C_SCL_PIN;
  78. GPIO_BC(I2C_SDA_PORT) |= I2C_SDA_PIN;
  79. gpio_init(I2C_SCL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
  80. gpio_init(I2C_SDA_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
  81. __NOP();
  82. __NOP();
  83. __NOP();
  84. __NOP();
  85. __NOP();
  86. GPIO_BOP(I2C_SCL_PORT) |= I2C_SCL_PIN;
  87. __NOP();
  88. __NOP();
  89. __NOP();
  90. __NOP();
  91. __NOP();
  92. GPIO_BOP(I2C_SDA_PORT) |= I2C_SDA_PIN;
  93. /* connect I2C_SCL_PIN to I2C_SCL */
  94. /* connect I2C_SDA_PIN to I2C_SDA */
  95. gpio_init(I2C_SCL_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN);
  96. gpio_init(I2C_SDA_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN);
  97. /* configure the I2CX interface */
  98. i2c_config();
  99. }