drv_i2c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * File : drv_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-06-05 tanek first implementation.
  13. */
  14. #include "drv_i2c.h"
  15. #include <board.h>
  16. #include <finsh.h>
  17. #include <rtdevice.h>
  18. #include <rthw.h>
  19. #define DEBUG
  20. #ifdef DEBUG
  21. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  22. #else
  23. #define DEBUG_PRINTF(...)
  24. #endif
  25. static void stm32f4_i2c_gpio_init()
  26. {
  27. GPIO_InitTypeDef GPIO_Initure;
  28. __HAL_RCC_GPIOA_CLK_ENABLE();
  29. __HAL_RCC_GPIOC_CLK_ENABLE();
  30. GPIO_Initure.Pin = GPIO_PIN_8;
  31. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  32. GPIO_Initure.Pull = GPIO_PULLUP;
  33. GPIO_Initure.Speed = GPIO_SPEED_FAST;
  34. HAL_GPIO_Init(GPIOA, &GPIO_Initure);
  35. GPIO_Initure.Pin = GPIO_PIN_9;
  36. GPIO_Initure.Mode = GPIO_MODE_OUTPUT_OD;
  37. GPIO_Initure.Pull = GPIO_PULLUP;
  38. GPIO_Initure.Speed = GPIO_SPEED_FAST;
  39. HAL_GPIO_Init(GPIOC, &GPIO_Initure);
  40. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
  41. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
  42. }
  43. static void stm32f4_set_sda(void *data, rt_int32_t state)
  44. {
  45. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, (GPIO_PinState)state);
  46. }
  47. static void stm32f4_set_scl(void *data, rt_int32_t state)
  48. {
  49. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, (GPIO_PinState)state);
  50. }
  51. static rt_int32_t stm32f4_get_sda(void *data)
  52. {
  53. return (rt_int32_t)HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_9);
  54. }
  55. static rt_int32_t stm32f4_get_scl(void *data)
  56. {
  57. return (rt_int32_t)HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8);
  58. }
  59. static void stm32f4_udelay(rt_uint32_t us)
  60. {
  61. rt_int32_t i;
  62. for (; us > 0; us--)
  63. {
  64. i = 50;
  65. while (i > 0)
  66. {
  67. i--;
  68. }
  69. }
  70. }
  71. static const struct rt_i2c_bit_ops bit_ops = {
  72. RT_NULL,
  73. stm32f4_set_sda,
  74. stm32f4_set_scl,
  75. stm32f4_get_sda,
  76. stm32f4_get_scl,
  77. stm32f4_udelay,
  78. 5,
  79. 100
  80. };
  81. int stm32f4_i2c_init(void)
  82. {
  83. struct rt_i2c_bus_device *bus;
  84. bus = rt_malloc(sizeof(struct rt_i2c_bus_device));
  85. if (bus == RT_NULL)
  86. {
  87. rt_kprintf("rt_malloc failed\n");
  88. return -RT_ENOMEM;
  89. }
  90. rt_memset((void *)bus, 0, sizeof(struct rt_i2c_bus_device));
  91. bus->priv = (void *)&bit_ops;
  92. stm32f4_i2c_gpio_init();
  93. rt_i2c_bit_add_bus(bus, "i2c3");
  94. return RT_EOK;
  95. }
  96. INIT_DEVICE_EXPORT(stm32f4_i2c_init);