drv_i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-15 xckhmf First Verison
  9. * 2021-11-27 chenyingchun fix _master_xfer bug
  10. *
  11. */
  12. #include <rtdevice.h>
  13. #include <nrfx_twi_twim.h>
  14. #include <nrfx_twim.h>
  15. #include <drv_i2c.h>
  16. #if defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1)
  17. typedef struct
  18. {
  19. nrf_twim_frequency_t freq;
  20. uint32_t scl_pin;
  21. uint32_t sda_pin;
  22. nrfx_twim_t twi_instance;
  23. }drv_i2c_cfg_t;
  24. #ifdef BSP_USING_I2C0
  25. static drv_i2c_cfg_t drv_i2c_0 =
  26. {
  27. .freq = NRF_TWIM_FREQ_400K,
  28. .scl_pin = BSP_I2C0_SCL_PIN,
  29. .sda_pin = BSP_I2C0_SDA_PIN,
  30. .twi_instance = NRFX_TWIM_INSTANCE(0)
  31. };
  32. static struct rt_i2c_bus_device i2c0_bus;
  33. #endif
  34. #ifdef BSP_USING_I2C1
  35. static drv_i2c_cfg_t drv_i2c_1 =
  36. {
  37. .freq = NRF_TWIM_FREQ_400K,
  38. .scl_pin = BSP_I2C1_SCL_PIN,
  39. .sda_pin = BSP_I2C1_SDA_PIN,
  40. .twi_instance = NRFX_TWIM_INSTANCE(1)
  41. };
  42. static struct rt_i2c_bus_device i2c1_bus;
  43. #endif
  44. static int twi_master_init(struct rt_i2c_bus_device *bus)
  45. {
  46. nrfx_err_t rtn;
  47. nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG(0,0);
  48. drv_i2c_cfg_t *p_cfg = bus->priv;
  49. nrfx_twim_t const * p_instance = &p_cfg->twi_instance;
  50. config.frequency = p_cfg->freq;
  51. config.scl = p_cfg->scl_pin;
  52. config.sda = p_cfg->sda_pin;
  53. nrfx_twi_twim_bus_recover(config.scl,config.sda);
  54. rtn = nrfx_twim_init(p_instance,&config,NULL,NULL);
  55. if (rtn != NRFX_SUCCESS)
  56. {
  57. return rtn;
  58. }
  59. nrfx_twim_enable(p_instance);
  60. return 0;
  61. }
  62. static rt_size_t _master_xfer(struct rt_i2c_bus_device *bus,
  63. struct rt_i2c_msg msgs[],
  64. rt_uint32_t num)
  65. {
  66. struct rt_i2c_msg *msg;
  67. nrfx_twim_t const *p_instance = &((drv_i2c_cfg_t *)bus->priv)->twi_instance;
  68. nrfx_err_t ret = NRFX_ERROR_INTERNAL;
  69. uint32_t no_stop_flag = 0;
  70. rt_int32_t i = 0;
  71. for (i = 0; i < num; i++)
  72. {
  73. msg = &msgs[i];
  74. nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(msg->addr, msg->buf, msg->len);
  75. if (msg->flags & RT_I2C_RD)
  76. {
  77. xfer.type = NRFX_TWIM_XFER_RX;
  78. }
  79. else
  80. {
  81. xfer.type = NRFX_TWIM_XFER_TX;
  82. if (msg->flags & RT_I2C_NO_READ_ACK)
  83. {
  84. no_stop_flag = NRFX_TWIM_FLAG_TX_NO_STOP;
  85. }
  86. }
  87. ret = nrfx_twim_xfer(p_instance, &xfer, no_stop_flag);
  88. if (ret != NRFX_SUCCESS)
  89. {
  90. goto out;
  91. }
  92. }
  93. out:
  94. return i;
  95. }
  96. static const struct rt_i2c_bus_device_ops _i2c_ops =
  97. {
  98. _master_xfer,
  99. NULL,
  100. NULL,
  101. };
  102. int rt_hw_i2c_init(void)
  103. {
  104. #ifdef BSP_USING_I2C0
  105. i2c0_bus.ops= &_i2c_ops;
  106. i2c0_bus.timeout = 0;
  107. i2c0_bus.priv = (void *)&drv_i2c_0;
  108. twi_master_init(&i2c0_bus);
  109. rt_i2c_bus_device_register(&i2c0_bus, "i2c0");
  110. #endif
  111. #ifdef BSP_USING_I2C1
  112. i2c1_bus.ops= &_i2c_ops;
  113. i2c1_bus.timeout = 0;
  114. i2c1_bus.priv = (void *)&drv_i2c_1;
  115. twi_master_init(&i2c1_bus);
  116. rt_i2c_bus_device_register(&i2c1_bus, "i2c1");
  117. #endif
  118. return 0;
  119. }
  120. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  121. #endif /* defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1) */