drv_i2c.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-15 Magicoe The first version for LPC55S6x
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "fsl_iocon.h"
  14. #include "fsl_gpio.h"
  15. #include "fsl_i2c.h"
  16. #ifdef RT_USING_I2C
  17. #define i2c_dbg rt_kprintf
  18. struct lpc_i2c_bus
  19. {
  20. struct rt_i2c_bus_device parent;
  21. I2C_Type *I2C;
  22. char *device_name;
  23. };
  24. static rt_size_t lpc_i2c_xfer(struct rt_i2c_bus_device *bus,
  25. struct rt_i2c_msg msgs[], rt_uint32_t num)
  26. {
  27. struct rt_i2c_msg *msg;
  28. i2c_master_transfer_t xfer = {0};
  29. rt_uint32_t i;
  30. rt_err_t ret = RT_ERROR;
  31. struct lpc_i2c_bus *lpc_i2c = (struct lpc_i2c_bus *)bus;
  32. for (i = 0; i < num; i++)
  33. {
  34. msg = &msgs[i];
  35. if (msg->flags & RT_I2C_RD)
  36. {
  37. xfer.slaveAddress = msg->addr;
  38. xfer.direction = kI2C_Read;
  39. xfer.subaddress = 0;
  40. xfer.subaddressSize = 0;
  41. xfer.data = msg->buf;
  42. xfer.dataSize = msg->len;
  43. if(i != 0)
  44. xfer.flags = kI2C_TransferRepeatedStartFlag;
  45. else
  46. xfer.flags = kI2C_TransferDefaultFlag;
  47. if (I2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success)
  48. {
  49. i2c_dbg("i2c bus write failed,i2c bus stop!\n");
  50. goto out;
  51. }
  52. }
  53. else
  54. {
  55. xfer.slaveAddress = msg->addr;
  56. xfer.direction = kI2C_Write;
  57. xfer.subaddress = 0;
  58. xfer.subaddressSize = 0;
  59. xfer.data = msg->buf;
  60. xfer.dataSize = msg->len;
  61. if(i == 0)
  62. xfer.flags = kI2C_TransferNoStopFlag;
  63. else
  64. xfer.flags = kI2C_TransferDefaultFlag;
  65. if (I2C_MasterTransferBlocking(lpc_i2c->I2C, &xfer) != kStatus_Success)
  66. {
  67. i2c_dbg("i2c bus write failed,i2c bus stop!\n");
  68. goto out;
  69. }
  70. }
  71. }
  72. ret = i;
  73. out:
  74. i2c_dbg("send stop condition\n");
  75. return ret;
  76. }
  77. static const struct rt_i2c_bus_device_ops i2c_ops =
  78. {
  79. lpc_i2c_xfer,
  80. RT_NULL,
  81. RT_NULL
  82. };
  83. int rt_hw_i2c_init(void)
  84. {
  85. i2c_master_config_t masterConfig;
  86. #ifdef BSP_USING_I2C1
  87. static struct lpc_i2c_bus lpc_i2c1;
  88. /* attach 12 MHz clock to FLEXCOMM2 (I2C master for touch controller) */
  89. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM1);
  90. I2C_MasterGetDefaultConfig(&masterConfig);
  91. /* Change the default baudrate configuration */
  92. masterConfig.baudRate_Bps = 100000U;
  93. /* Initialize the I2C master peripheral */
  94. I2C_MasterInit(I2C1, &masterConfig, 12000000);
  95. rt_memset((void *)&lpc_i2c1, 0, sizeof(struct lpc_i2c_bus));
  96. lpc_i2c1.parent.ops = &i2c_ops;
  97. lpc_i2c1.I2C = I2C1;
  98. lpc_i2c1.device_name = "i2c1";
  99. rt_i2c_bus_device_register(&lpc_i2c1.parent, "i2c1");
  100. #endif /* BSP_USING_I2C1 */
  101. #ifdef BSP_USING_I2C4
  102. static struct lpc_i2c_bus lpc_i2c4;
  103. /* attach 12 MHz clock to FLEXCOMM2 (I2C master for touch controller) */
  104. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
  105. I2C_MasterGetDefaultConfig(&masterConfig);
  106. /* Change the default baudrate configuration */
  107. masterConfig.baudRate_Bps = 100000U;
  108. /* Initialize the I2C master peripheral */
  109. I2C_MasterInit(I2C4, &masterConfig, 12000000);
  110. rt_memset((void *)&lpc_i2c4, 0, sizeof(struct lpc_i2c_bus));
  111. lpc_i2c4.parent.ops = &i2c_ops;
  112. lpc_i2c4.I2C = I2C4;
  113. lpc_i2c4.device_name = "i2c4";
  114. rt_i2c_bus_device_register(&lpc_i2c4.parent, "i2c4");
  115. #endif /* BSP_USING_I2C4 */
  116. return 0;
  117. }
  118. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  119. #endif /* RT_USING_I2C */