drv_i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. *
  10. */
  11. #include <rtdevice.h>
  12. #include <nrfx_twi_twim.h>
  13. #include <nrfx_twim.h>
  14. #include <drv_i2c.h>
  15. #if defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1)
  16. typedef struct
  17. {
  18. nrf_twim_frequency_t freq;
  19. uint32_t scl_pin;
  20. uint32_t sda_pin;
  21. nrfx_twim_t twi_instance;
  22. }drv_i2c_cfg_t;
  23. #ifdef BSP_USING_I2C0
  24. static drv_i2c_cfg_t drv_i2c_0 =
  25. {
  26. .freq = NRF_TWIM_FREQ_400K,
  27. .scl_pin = BSP_I2C0_SCL_PIN,
  28. .sda_pin = BSP_I2C0_SDA_PIN,
  29. .twi_instance = NRFX_TWIM_INSTANCE(0)
  30. };
  31. static struct rt_i2c_bus_device i2c0_bus;
  32. #endif
  33. #ifdef BSP_USING_I2C1
  34. static drv_i2c_cfg_t drv_i2c_1 =
  35. {
  36. .freq = NRF_TWIM_FREQ_400K,
  37. .scl_pin = BSP_I2C1_SCL_PIN,
  38. .sda_pin = BSP_I2C1_SDA_PIN,
  39. .twi_instance = NRFX_TWIM_INSTANCE(1)
  40. };
  41. static struct rt_i2c_bus_device i2c1_bus;
  42. #endif
  43. static int twi_master_init(struct rt_i2c_bus_device *bus)
  44. {
  45. nrfx_err_t rtn;
  46. nrfx_twim_config_t config = NRFX_TWIM_DEFAULT_CONFIG(0,0);
  47. drv_i2c_cfg_t *p_cfg = bus->priv;
  48. nrfx_twim_t const * p_instance = &p_cfg->twi_instance;
  49. config.frequency = p_cfg->freq;
  50. config.scl = p_cfg->scl_pin;
  51. config.sda = p_cfg->sda_pin;
  52. nrfx_twi_twim_bus_recover(config.scl,config.sda);
  53. rtn = nrfx_twim_init(p_instance,&config,NULL,NULL);
  54. nrfx_twim_enable(p_instance);
  55. return 0;
  56. }
  57. static rt_size_t _master_xfer(struct rt_i2c_bus_device *bus,
  58. struct rt_i2c_msg msgs[],
  59. rt_uint32_t num)
  60. {
  61. nrfx_twim_t const * p_instance = &((drv_i2c_cfg_t *)bus->priv)->twi_instance;
  62. nrfx_err_t ret = NRFX_ERROR_INTERNAL;
  63. uint32_t no_stop_flag = 0;
  64. nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(msgs->addr,msgs->buf, msgs->len);
  65. if((msgs->flags & 0x01) == RT_I2C_WR)
  66. {
  67. xfer.type = NRFX_TWIM_XFER_TX;
  68. if((msgs->flags & 0x40) == RT_I2C_NO_READ_ACK)
  69. {
  70. no_stop_flag = NRFX_TWIM_FLAG_TX_NO_STOP;
  71. }
  72. }
  73. else if((msgs->flags & 0x01) == RT_I2C_RD)
  74. {
  75. xfer.type = NRFX_TWIM_XFER_RX;
  76. }
  77. ret = nrfx_twim_xfer(p_instance,&xfer,no_stop_flag);
  78. return (ret == NRFX_SUCCESS) ? msgs->len : 0;
  79. }
  80. static const struct rt_i2c_bus_device_ops _i2c_ops =
  81. {
  82. _master_xfer,
  83. NULL,
  84. NULL,
  85. };
  86. int rt_hw_i2c_init(void)
  87. {
  88. #ifdef BSP_USING_I2C0
  89. i2c0_bus.ops= &_i2c_ops;
  90. i2c0_bus.timeout = 0;
  91. i2c0_bus.priv = (void *)&drv_i2c_0;
  92. twi_master_init(&i2c0_bus);
  93. rt_i2c_bus_device_register(&i2c0_bus, "i2c0");
  94. #endif
  95. #ifdef BSP_USING_I2C1
  96. i2c1_bus.ops= &_i2c_ops;
  97. i2c1_bus.timeout = 0;
  98. i2c1_bus.priv = (void *)&drv_i2c_1;
  99. twi_master_init(&i2c1_bus);
  100. rt_i2c_bus_device_register(&i2c1_bus, "i2c1");
  101. #endif
  102. return 0;
  103. }
  104. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  105. #endif /* defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1) */