hw_i2c.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * File :hw_i2c.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, 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. * 2018-01-04 Sundm75 the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "ls1c_i2c.h"
  27. struct ls1c_i2c_bus
  28. {
  29. struct rt_i2c_bus_device parent;
  30. rt_uint32_t u32Module;
  31. };
  32. rt_size_t rt_i2c_master_xfer(struct rt_i2c_bus_device *bus,
  33. struct rt_i2c_msg *msgs,
  34. rt_uint32_t num)
  35. {
  36. struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;
  37. ls1c_i2c_info_t i2c_info;
  38. struct rt_i2c_msg *msg;
  39. int i;
  40. rt_int32_t ret = RT_EOK;
  41. i2c_info.clock = 50000; // 50kb/s
  42. i2c_info.I2Cx = i2c_bus->u32Module;
  43. i2c_init(&i2c_info);
  44. for (i = 0; i < num; i++)
  45. {
  46. msg = &msgs[i];
  47. if (msg->flags == RT_I2C_RD)
  48. {
  49. i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_READ);
  50. i2c_receive_ack(&i2c_info);
  51. i2c_receive_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len);
  52. i2c_send_stop(&i2c_info);
  53. }
  54. else if(msg->flags == RT_I2C_WR)
  55. {
  56. i2c_send_start_and_addr(&i2c_info, msg->addr, LS1C_I2C_DIRECTION_WRITE);
  57. i2c_receive_ack(&i2c_info);
  58. i2c_send_data(&i2c_info, (rt_uint8_t *)msg->buf, msg->len);
  59. i2c_send_stop(&i2c_info);
  60. }
  61. ret++;
  62. }
  63. return ret;
  64. }
  65. rt_err_t rt_i2c_bus_control(struct rt_i2c_bus_device *bus,
  66. rt_uint32_t cmd,
  67. rt_uint32_t arg)
  68. {
  69. struct ls1c_i2c_bus * i2c_bus = (struct ls1c_i2c_bus *)bus;
  70. RT_ASSERT(bus != RT_NULL);
  71. i2c_bus = (struct ls1c_i2c_bus *)bus->parent.user_data;
  72. RT_ASSERT(i2c_bus != RT_NULL);
  73. switch (cmd)
  74. {
  75. case RT_DEVICE_CTRL_CONFIG :
  76. break;
  77. }
  78. return RT_EOK;
  79. }
  80. static const struct rt_i2c_bus_device_ops ls1c_i2c_ops =
  81. {
  82. rt_i2c_master_xfer,
  83. RT_NULL,
  84. rt_i2c_bus_control
  85. };
  86. #ifdef RT_USING_I2C0
  87. static struct ls1c_i2c_bus ls1c_i2c_bus_0 =
  88. {
  89. {1},
  90. LS1C_I2C_0,
  91. };
  92. #endif
  93. #ifdef RT_USING_I2C1
  94. static struct ls1c_i2c_bus ls1c_i2c_bus_1 =
  95. {
  96. {1},
  97. LS1C_I2C_1,
  98. };
  99. #endif
  100. #ifdef RT_USING_I2C2
  101. static struct ls1c_i2c_bus ls1c_i2c_bus_2 =
  102. {
  103. {1},
  104. LS1C_I2C_2,
  105. };
  106. #endif
  107. int rt_i2c_init(void)
  108. {
  109. struct ls1c_i2c_bus* ls1c_i2c;
  110. #ifdef RT_USING_I2C0
  111. ls1c_i2c = &ls1c_i2c_bus_0;
  112. ls1c_i2c->parent.ops = &ls1c_i2c_ops;
  113. rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c0");
  114. rt_kprintf("i2c0_init!\n");
  115. #endif
  116. #ifdef RT_USING_I2C1
  117. ls1c_i2c = &ls1c_i2c_bus_1;
  118. ls1c_i2c->parent.ops = &ls1c_i2c_ops;
  119. rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c1");
  120. rt_kprintf("i2c1_init!\n");
  121. #endif
  122. #ifdef RT_USING_I2C2
  123. ls1c_i2c = &ls1c_i2c_bus_2;
  124. ls1c_i2c->parent.ops = &ls1c_i2c_ops;
  125. rt_i2c_bus_device_register(&ls1c_i2c->parent, "i2c2");
  126. rt_kprintf("i2c2_init!\n");
  127. #endif
  128. return RT_EOK;
  129. }