drv_i2c.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-06-05 tanek first implementation.
  9. * 2018-03-08 ZYH Porting for stm32f4xx
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "drv_i2c.h"
  15. #include <board.h>
  16. /*user can change this*/
  17. #define I2C_BUS_NAME "i2c2"
  18. /*user should change this to adapt specific board*/
  19. #define I2C_SCL_PIN GPIO_PIN_6
  20. #define I2C_SCL_PORT GPIOB
  21. #define I2C_SCL_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
  22. #define I2C_SDA_PIN GPIO_PIN_7
  23. #define I2C_SDA_PORT GPIOB
  24. #define I2C_SDA_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
  25. static void drv_i2c_gpio_init()
  26. {
  27. GPIO_InitTypeDef GPIO_Initure;
  28. I2C_SCL_PORT_CLK_ENABLE();
  29. I2C_SDA_PORT_CLK_ENABLE();
  30. GPIO_Initure.Pin = I2C_SCL_PIN;
  31. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  32. GPIO_Initure.Pull = GPIO_PULLUP;
  33. GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  34. HAL_GPIO_Init(I2C_SCL_PORT, &GPIO_Initure);
  35. GPIO_Initure.Pin = I2C_SDA_PIN;
  36. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  37. GPIO_Initure.Pull = GPIO_PULLUP;
  38. GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  39. HAL_GPIO_Init(I2C_SDA_PORT, &GPIO_Initure);
  40. HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, GPIO_PIN_SET);
  41. HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, GPIO_PIN_SET);
  42. }
  43. static void drv_set_sda(void *data, rt_int32_t state)
  44. {
  45. HAL_GPIO_WritePin(I2C_SDA_PORT, I2C_SDA_PIN, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
  46. }
  47. static void drv_set_scl(void *data, rt_int32_t state)
  48. {
  49. HAL_GPIO_WritePin(I2C_SCL_PORT, I2C_SCL_PIN, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
  50. }
  51. static rt_int32_t drv_get_sda(void *data)
  52. {
  53. return HAL_GPIO_ReadPin(I2C_SDA_PORT, I2C_SDA_PIN) ? 1 : 0;
  54. }
  55. static rt_int32_t drv_get_scl(void *data)
  56. {
  57. return HAL_GPIO_ReadPin(I2C_SCL_PORT, I2C_SCL_PIN) ? 1 : 0;
  58. }
  59. static void drv_udelay(rt_uint32_t us)
  60. {
  61. __IO uint32_t Delay = us * (SystemCoreClock / 8U / 1000000U);
  62. do
  63. {
  64. __NOP();
  65. }
  66. while (Delay --);
  67. }
  68. static const struct rt_i2c_bit_ops drv_bit_ops =
  69. {
  70. RT_NULL,
  71. drv_set_sda,
  72. drv_set_scl,
  73. drv_get_sda,
  74. drv_get_scl,
  75. drv_udelay,
  76. 1,
  77. 100
  78. };
  79. int drv_i2c_init(void)
  80. {
  81. static struct rt_i2c_bus_device i2c2_bus;
  82. drv_i2c_gpio_init();
  83. rt_memset((void *)&i2c2_bus, 0, sizeof(struct rt_i2c_bus_device));
  84. i2c2_bus.priv = (void *)&drv_bit_ops;
  85. rt_i2c_bit_add_bus(&i2c2_bus, I2C_BUS_NAME);
  86. return RT_EOK;
  87. }
  88. INIT_DEVICE_EXPORT(drv_i2c_init);
  89. /* end of i2c driver */