drv_i2c.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * File : drv_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-06-05 tanek first implementation.
  23. * 2018-03-08 ZYH Porting for stm32f4xx
  24. */
  25. #include <rthw.h>
  26. #include <rtthread.h>
  27. #include <rtdevice.h>
  28. #include "drv_i2c.h"
  29. #include <board.h>
  30. /*user can change this*/
  31. #define I2C_BUS_NAME "i2c2"
  32. /*user should change this to adapt specific board*/
  33. #define I2C_SCL_PIN GPIO_PIN_6
  34. #define I2C_SCL_PORT GPIOB
  35. #define I2C_SCL_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
  36. #define I2C_SDA_PIN GPIO_PIN_7
  37. #define I2C_SDA_PORT GPIOB
  38. #define I2C_SDA_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
  39. static void drv_i2c_gpio_init()
  40. {
  41. GPIO_InitTypeDef GPIO_Initure;
  42. I2C_SCL_PORT_CLK_ENABLE();
  43. I2C_SDA_PORT_CLK_ENABLE();
  44. GPIO_Initure.Pin = I2C_SCL_PIN;
  45. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  46. GPIO_Initure.Pull = GPIO_PULLUP;
  47. GPIO_Initure.Speed = GPIO_SPEED_HIGH;
  48. HAL_GPIO_Init(I2C_SCL_PORT, &GPIO_Initure);
  49. GPIO_Initure.Pin = I2C_SDA_PIN;
  50. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  51. GPIO_Initure.Pull = GPIO_PULLUP;
  52. GPIO_Initure.Speed = GPIO_SPEED_HIGH;
  53. HAL_GPIO_Init(I2C_SDA_PORT, &GPIO_Initure);
  54. HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, GPIO_PIN_SET);
  55. HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, GPIO_PIN_SET);
  56. }
  57. static void drv_set_sda(void *data, rt_int32_t state)
  58. {
  59. HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
  60. }
  61. static void drv_set_scl(void *data, rt_int32_t state)
  62. {
  63. HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
  64. }
  65. static rt_int32_t drv_get_sda(void *data)
  66. {
  67. return HAL_GPIO_ReadPin(I2C_SDA_PORT, I2C_SDA_PIN) ? 1 : 0;
  68. }
  69. static rt_int32_t drv_get_scl(void *data)
  70. {
  71. return HAL_GPIO_ReadPin(I2C_SCL_PORT, I2C_SCL_PIN) ? 1 : 0;
  72. }
  73. static void drv_udelay(rt_uint32_t us)
  74. {
  75. __IO uint32_t Delay = us * (SystemCoreClock / 8U / 1000000U);
  76. do
  77. {
  78. __NOP();
  79. }
  80. while (Delay --);
  81. }
  82. static const struct rt_i2c_bit_ops drv_bit_ops =
  83. {
  84. RT_NULL,
  85. drv_set_sda,
  86. drv_set_scl,
  87. drv_get_sda,
  88. drv_get_scl,
  89. drv_udelay,
  90. 1,
  91. 100
  92. };
  93. int drv_i2c_init(void)
  94. {
  95. static struct rt_i2c_bus_device i2c2_bus;
  96. drv_i2c_gpio_init();
  97. rt_memset((void *)&i2c2_bus, 0, sizeof(struct rt_i2c_bus_device));
  98. i2c2_bus.priv = (void *)&drv_bit_ops;
  99. rt_i2c_bit_add_bus(&i2c2_bus, I2C_BUS_NAME);
  100. return RT_EOK;
  101. }
  102. INIT_DEVICE_EXPORT(drv_i2c_init);
  103. /* end of i2c driver */