23
23
#include "fsl_lpi2c.h"
24
24
#include "PeripheralPins.h"
25
25
26
- /* 7 bit IIC addr - R/W flag not included */
27
- static int i2c_address = 0 ;
28
26
/* Array of I2C peripheral base address. */
29
27
static LPI2C_Type * const i2c_addrs [] = LPI2C_BASE_PTRS ;
30
28
@@ -59,11 +57,24 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
59
57
60
58
int i2c_start (i2c_t * obj )
61
59
{
62
- if (LPI2C_MasterStart (i2c_addrs [obj -> instance ], 0 , kLPI2C_Write ) != kStatus_Success ) {
63
- return 1 ;
64
- }
60
+ LPI2C_Type * base = i2c_addrs [obj -> instance ];
61
+ int status = 0 ;
65
62
66
- return 0 ;
63
+ obj -> address_set = 0 ;
64
+
65
+ /* Clear all flags. */
66
+ LPI2C_MasterClearStatusFlags (base , kLPI2C_MasterEndOfPacketFlag |
67
+ kLPI2C_MasterStopDetectFlag |
68
+ kLPI2C_MasterNackDetectFlag |
69
+ kLPI2C_MasterArbitrationLostFlag |
70
+ kLPI2C_MasterFifoErrFlag |
71
+ kLPI2C_MasterPinLowTimeoutFlag |
72
+ kLPI2C_MasterDataMatchFlag );
73
+
74
+ /* Turn off auto-stop option. */
75
+ base -> MCFGR1 &= ~LPI2C_MCFGR1_AUTOSTOP_MASK ;
76
+
77
+ return status ;
67
78
}
68
79
69
80
int i2c_stop (i2c_t * obj )
@@ -72,6 +83,8 @@ int i2c_stop(i2c_t *obj)
72
83
return 1 ;
73
84
}
74
85
86
+ obj -> address_set = 0 ;
87
+
75
88
return 0 ;
76
89
}
77
90
@@ -88,7 +101,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
88
101
LPI2C_Type * base = i2c_addrs [obj -> instance ];
89
102
lpi2c_master_transfer_t master_xfer ;
90
103
91
- i2c_address = address >> 1 ;
92
104
memset (& master_xfer , 0 , sizeof (master_xfer ));
93
105
master_xfer .slaveAddress = address >> 1 ;
94
106
master_xfer .direction = kLPI2C_Read ;
@@ -164,11 +176,10 @@ int i2c_byte_read(i2c_t *obj, int last)
164
176
lpi2c_master_transfer_t master_xfer ;
165
177
166
178
memset (& master_xfer , 0 , sizeof (master_xfer ));
167
- master_xfer .slaveAddress = i2c_address ;
168
179
master_xfer .direction = kLPI2C_Read ;
169
180
master_xfer .data = & data ;
170
181
master_xfer .dataSize = 1 ;
171
- master_xfer .flags = kLPI2C_TransferNoStopFlag ;
182
+ master_xfer .flags = kLPI2C_TransferNoStopFlag | kLPI2C_TransferNoStartFlag ;
172
183
173
184
if (LPI2C_MasterTransferBlocking (base , & master_xfer ) != kStatus_Success ) {
174
185
return I2C_ERROR_NO_SLAVE ;
@@ -179,21 +190,37 @@ int i2c_byte_read(i2c_t *obj, int last)
179
190
int i2c_byte_write (i2c_t * obj , int data )
180
191
{
181
192
LPI2C_Type * base = i2c_addrs [obj -> instance ];
182
- lpi2c_master_transfer_t master_xfer ;
183
- status_t ret_value ;
184
-
185
- memset (& master_xfer , 0 , sizeof (master_xfer ));
186
- master_xfer .slaveAddress = i2c_address ;
187
- master_xfer .direction = kLPI2C_Write ;
188
- master_xfer .data = & data ;
189
- master_xfer .dataSize = 1 ;
190
- master_xfer .flags = kLPI2C_TransferNoStopFlag ;
193
+ uint32_t status ;
194
+ size_t txCount ;
195
+ size_t txFifoSize = FSL_FEATURE_LPI2C_FIFO_SIZEn (base );
196
+
197
+ /* Clear error flags. */
198
+ LPI2C_MasterClearStatusFlags (base , LPI2C_MasterGetStatusFlags (base ));
199
+
200
+ /* Wait till there is room in the TX FIFO */
201
+ do {
202
+ /* Get the number of words in the tx fifo and compute empty slots. */
203
+ LPI2C_MasterGetFifoCounts (base , NULL , & txCount );
204
+ txCount = txFifoSize - txCount ;
205
+ } while (!txCount );
206
+
207
+ if (!obj -> address_set ) {
208
+ obj -> address_set = 1 ;
209
+ /* Issue start command. */
210
+ base -> MTDR = LPI2C_MTDR_CMD (0x4U ) | LPI2C_MTDR_DATA (data );
211
+ } else {
212
+ /* Write byte into LPI2C master data register. */
213
+ base -> MTDR = data ;
214
+ }
191
215
192
- ret_value = LPI2C_MasterTransferBlocking (base , & master_xfer );
216
+ /* Wait till data is pushed out of the FIFO */
217
+ while (!(base -> MSR & kLPI2C_MasterTxReadyFlag )) {
218
+ }
193
219
194
- if (ret_value == kStatus_Success ) {
220
+ status = LPI2C_MasterCheckAndClearError (base , LPI2C_MasterGetStatusFlags (base ));
221
+ if (status == kStatus_Success ) {
195
222
return 1 ;
196
- } else if (ret_value == kStatus_LPI2C_Nak ) {
223
+ } else if (status == kStatus_LPI2C_Nak ) {
197
224
return 0 ;
198
225
} else {
199
226
return 2 ;
0 commit comments