drv_i2c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "drv_i2c.h"
  7. #include "fsl_common.h"
  8. #include "fsl_iocon.h"
  9. #include "fsl_i2c.h"
  10. struct lpc_i2c
  11. {
  12. struct rt_i2c_bus_device bus;
  13. I2C_Type *base;
  14. char *device_name;
  15. };
  16. static uint32_t get_i2c_freq(I2C_Type *base)
  17. {
  18. uint32_t freq = 0;
  19. #if defined(BSP_USING_I2C4)
  20. if(base == I2C4)
  21. {
  22. freq = CLOCK_GetFreq(kCLOCK_Flexcomm4);
  23. }
  24. #endif
  25. return freq;
  26. }
  27. static rt_size_t master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  28. {
  29. rt_size_t ret = (0);
  30. rt_uint32_t index = 0;
  31. struct lpc_i2c *lpc_i2c = RT_NULL;
  32. struct rt_i2c_msg *msg = RT_NULL;
  33. i2c_direction_t direction;
  34. status_t result = kStatus_Success;
  35. RT_ASSERT(bus != RT_NULL);
  36. lpc_i2c = (struct lpc_i2c *)bus;
  37. for(index = 0; index < num; index++)
  38. {
  39. msg = &msgs[index];
  40. direction = ((msg->flags & RT_I2C_RD) ? kI2C_Read : kI2C_Write);
  41. if (!(msg->flags & RT_I2C_NO_START))
  42. {
  43. /* Start condition and slave address. */
  44. result = I2C_MasterStart(lpc_i2c->base, msg->addr, direction);
  45. }
  46. if (result == kStatus_Success)
  47. {
  48. if (direction == kI2C_Write)
  49. {
  50. /* Transmit data. */
  51. result = I2C_MasterWriteBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
  52. }
  53. else
  54. {
  55. /* Receive Data. */
  56. result = I2C_MasterReadBlocking(lpc_i2c->base, msg->buf, msg->len, kI2C_TransferDefaultFlag);
  57. }
  58. }
  59. }
  60. if (result == kStatus_Success)
  61. {
  62. ret = index;
  63. }
  64. I2C_MasterStop(lpc_i2c->base);
  65. return ret;
  66. }
  67. static rt_size_t slave_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num)
  68. {
  69. return 0;
  70. }
  71. static rt_err_t i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t cmd, rt_uint32_t arg)
  72. {
  73. return RT_EOK;
  74. }
  75. static const struct rt_i2c_bus_device_ops ops =
  76. {
  77. master_xfer,
  78. slave_xfer,
  79. i2c_bus_control,
  80. };
  81. #if defined(BSP_USING_I2C4)
  82. static struct lpc_i2c i2c4 = {0};
  83. #endif
  84. int rt_hw_i2c_init(void)
  85. {
  86. CLOCK_EnableClock(kCLOCK_Iocon);
  87. #if defined(BSP_USING_I2C4)
  88. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
  89. RESET_PeripheralReset(kFC4_RST_SHIFT_RSTn);
  90. i2c4.base = I2C4;
  91. i2c4.device_name = "i2c4";
  92. i2c4.bus.ops = &ops;
  93. IOCON_PinMuxSet(IOCON, 1, 1, IOCON_MODE_PULLUP | IOCON_FUNC5 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF);
  94. IOCON_PinMuxSet(IOCON, 1, 2, IOCON_MODE_PULLUP | IOCON_FUNC5 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF);
  95. i2c_master_config_t masterConfig;
  96. I2C_MasterGetDefaultConfig(&masterConfig);
  97. masterConfig.baudRate_Bps = 100*1000U;
  98. I2C_MasterInit(I2C4, &masterConfig, get_i2c_freq(I2C4));
  99. rt_i2c_bus_device_register(&i2c4.bus, i2c4.device_name);
  100. #endif
  101. return RT_EOK;
  102. }
  103. INIT_BOARD_EXPORT(rt_hw_i2c_init);