|
6 | 6 | * Change Logs: |
7 | 7 | * Date Author Notes |
8 | 8 | * 2020-11-15 xckhmf First Verison |
| 9 | + * 2021-11-27 chenyingchun fix _master_xfer bug |
9 | 10 | * |
10 | 11 | */ |
11 | 12 |
|
@@ -56,36 +57,54 @@ static int twi_master_init(struct rt_i2c_bus_device *bus) |
56 | 57 | nrfx_twi_twim_bus_recover(config.scl,config.sda); |
57 | 58 |
|
58 | 59 | rtn = nrfx_twim_init(p_instance,&config,NULL,NULL); |
| 60 | + if (rtn != NRFX_SUCCESS) |
| 61 | + { |
| 62 | + return rtn; |
| 63 | + } |
59 | 64 | nrfx_twim_enable(p_instance); |
60 | 65 | return 0; |
61 | 66 | } |
62 | 67 |
|
63 | 68 | static rt_size_t _master_xfer(struct rt_i2c_bus_device *bus, |
64 | | - struct rt_i2c_msg msgs[], |
65 | | - rt_uint32_t num) |
| 69 | + struct rt_i2c_msg msgs[], |
| 70 | + rt_uint32_t num) |
66 | 71 | { |
67 | | - nrfx_twim_t const * p_instance = &((drv_i2c_cfg_t *)bus->priv)->twi_instance; |
| 72 | + struct rt_i2c_msg *msg; |
| 73 | + nrfx_twim_t const *p_instance = &((drv_i2c_cfg_t *)bus->priv)->twi_instance; |
68 | 74 | nrfx_err_t ret = NRFX_ERROR_INTERNAL; |
69 | 75 | uint32_t no_stop_flag = 0; |
| 76 | + rt_int32_t i = 0; |
70 | 77 |
|
71 | | - nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(msgs->addr,msgs->buf, msgs->len); |
72 | | - if((msgs->flags & 0x01) == RT_I2C_WR) |
| 78 | + for (i = 0; i < num; i++) |
73 | 79 | { |
74 | | - xfer.type = NRFX_TWIM_XFER_TX; |
75 | | - if((msgs->flags & 0x40) == RT_I2C_NO_READ_ACK) |
| 80 | + msg = &msgs[i]; |
| 81 | + nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(msg->addr, msg->buf, msg->len); |
| 82 | + |
| 83 | + if (msg->flags & RT_I2C_RD) |
76 | 84 | { |
77 | | - no_stop_flag = NRFX_TWIM_FLAG_TX_NO_STOP; |
| 85 | + xfer.type = NRFX_TWIM_XFER_RX; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + xfer.type = NRFX_TWIM_XFER_TX; |
| 90 | + if (msg->flags & RT_I2C_NO_READ_ACK) |
| 91 | + { |
| 92 | + no_stop_flag = NRFX_TWIM_FLAG_TX_NO_STOP; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + ret = nrfx_twim_xfer(p_instance, &xfer, no_stop_flag); |
| 97 | + if (ret != NRFX_SUCCESS) |
| 98 | + { |
| 99 | + goto out; |
78 | 100 | } |
79 | 101 | } |
80 | | - else if((msgs->flags & 0x01) == RT_I2C_RD) |
81 | | - { |
82 | | - xfer.type = NRFX_TWIM_XFER_RX; |
83 | | - } |
84 | | - ret = nrfx_twim_xfer(p_instance,&xfer,no_stop_flag); |
85 | | - return (ret == NRFX_SUCCESS) ? msgs->len : 0; |
86 | 102 |
|
| 103 | +out: |
| 104 | + return i; |
87 | 105 | } |
88 | 106 |
|
| 107 | + |
89 | 108 | static const struct rt_i2c_bus_device_ops _i2c_ops = |
90 | 109 | { |
91 | 110 | _master_xfer, |
|
0 commit comments