drv_i2c.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * File : drv_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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-11-14 勤为本 first version
  23. */
  24. #include <rtthread.h>
  25. #include <drivers/i2c.h>
  26. #include <drivers/i2c-bit-ops.h>
  27. #include "drv_i2c.h"
  28. #include "../libraries/ls1c_gpio.h"
  29. #include "../libraries/ls1c_delay.h"
  30. #define LS1C_I2C_SCL (57) // gpio57
  31. #define LS1C_I2C_SDA (56) // gpio56
  32. #define LS1C_SET_GPIO_MODE
  33. static void ls1c_i2c_gpio_init(void)
  34. {
  35. gpio_init(LS1C_I2C_SCL, gpio_mode_output);
  36. gpio_set(LS1C_I2C_SCL, gpio_level_high);
  37. gpio_init(LS1C_I2C_SDA, gpio_mode_output);
  38. gpio_set(LS1C_I2C_SDA, gpio_level_high);
  39. return ;
  40. }
  41. static void ls1c_udelay(rt_uint32_t us)
  42. {
  43. delay_us((int)us);
  44. }
  45. static void ls1c_set_sda(void *data, rt_int32_t state)
  46. {
  47. #ifdef LS1C_SET_GPIO_MODE
  48. gpio_init(LS1C_I2C_SDA, gpio_mode_output);
  49. ls1c_udelay(5);
  50. #endif
  51. gpio_set(LS1C_I2C_SDA, state);
  52. return ;
  53. }
  54. static void ls1c_set_scl(void *data, rt_int32_t state)
  55. {
  56. #ifdef LS1C_SET_GPIO_MODE
  57. gpio_init(LS1C_I2C_SCL, gpio_mode_output);
  58. ls1c_udelay(5);
  59. #endif
  60. gpio_set(LS1C_I2C_SCL, state);
  61. return ;
  62. }
  63. static rt_int32_t ls1c_get_sda(void *data)
  64. {
  65. #ifdef LS1C_SET_GPIO_MODE
  66. gpio_init(LS1C_I2C_SDA, gpio_mode_input);
  67. ls1c_udelay(5);
  68. #endif
  69. return gpio_get(LS1C_I2C_SDA);
  70. }
  71. static rt_int32_t ls1c_get_scl(void *data)
  72. {
  73. #ifdef LS1C_SET_GPIO_MODE
  74. gpio_init(LS1C_I2C_SCL, gpio_mode_input);
  75. ls1c_udelay(5);
  76. #endif
  77. return gpio_get(LS1C_I2C_SCL);
  78. }
  79. static const struct rt_i2c_bit_ops bit_ops = {
  80. .data = RT_NULL,
  81. .set_sda = ls1c_set_sda,
  82. .set_scl = ls1c_set_scl,
  83. .get_sda = ls1c_get_sda,
  84. .get_scl = ls1c_get_scl,
  85. .udelay = ls1c_udelay,
  86. .delay_us = 20, // 此值为周期(us)
  87. .timeout = 10, // 单位为tick
  88. };
  89. int ls1c_i2c_init(void)
  90. {
  91. static struct rt_i2c_bus_device bus = {0};
  92. bus.priv = (void *)&bit_ops;
  93. ls1c_i2c_gpio_init();
  94. rt_i2c_bit_add_bus(&bus, "i2c2");
  95. return RT_EOK;
  96. }