@@ -37,7 +37,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
37
37
uint32_t i2c_sda = pinmap_peripheral (sda , PinMap_I2C_SDA );
38
38
uint32_t i2c_scl = pinmap_peripheral (scl , PinMap_I2C_SCL );
39
39
obj -> instance = pinmap_merge (i2c_sda , i2c_scl );
40
- obj -> next_repeated_start = 0 ;
40
+
41
41
MBED_ASSERT ((int )obj -> instance != NC );
42
42
43
43
lpi2c_master_config_t master_config ;
@@ -50,6 +50,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
50
50
pinmap_pinout (sda , PinMap_I2C_SDA );
51
51
pinmap_pinout (scl , PinMap_I2C_SCL );
52
52
53
+ pin_mode (sda , PullUp_22K );
54
+ pin_mode (scl , PullUp_22K );
55
+
53
56
pin_mode_opendrain (sda , true);
54
57
pin_mode_opendrain (scl , true);
55
58
}
@@ -65,7 +68,6 @@ int i2c_start(i2c_t *obj)
65
68
66
69
int i2c_stop (i2c_t * obj )
67
70
{
68
- obj -> next_repeated_start = 0 ;
69
71
if (LPI2C_MasterStop (i2c_addrs [obj -> instance ]) != kStatus_Success ) {
70
72
return 1 ;
71
73
}
@@ -78,7 +80,7 @@ void i2c_frequency(i2c_t *obj, int hz)
78
80
uint32_t busClock ;
79
81
80
82
busClock = i2c_get_clock ();
81
- LPI2C_MasterSetBaudRate (i2c_addrs [obj -> instance ], hz , busClock );
83
+ LPI2C_MasterSetBaudRate (i2c_addrs [obj -> instance ], busClock , hz );
82
84
}
83
85
84
86
int i2c_read (i2c_t * obj , int address , char * data , int length , int stop )
@@ -92,13 +94,9 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
92
94
master_xfer .direction = kLPI2C_Read ;
93
95
master_xfer .data = (uint8_t * )data ;
94
96
master_xfer .dataSize = length ;
95
- if (obj -> next_repeated_start ) {
96
- master_xfer .flags |= kLPI2C_TransferRepeatedStartFlag ;
97
- }
98
97
if (!stop ) {
99
98
master_xfer .flags |= kLPI2C_TransferNoStopFlag ;
100
99
}
101
- obj -> next_repeated_start = master_xfer .flags & kLPI2C_TransferNoStopFlag ? 1 : 0 ;
102
100
103
101
/* The below function will issue a STOP signal at the end of the transfer.
104
102
* This is required by the hardware in order to receive the last byte
@@ -120,11 +118,20 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
120
118
return I2C_ERROR_NO_SLAVE ;
121
119
}
122
120
121
+ /* Wait till START has been flushed out of the FIFO */
122
+ while (!(base -> MSR & kLPI2C_MasterBusBusyFlag )) {
123
+ }
124
+
125
+ /* Send the STOP signal */
126
+ base -> MTDR = LPI2C_MTDR_CMD (0x2U );
127
+
128
+ /* Wait till STOP has been sent successfully */
129
+ while (!(base -> MSR & kLPI2C_MasterStopDetectFlag )) {
130
+ }
131
+
123
132
if (base -> MSR & kLPI2C_MasterNackDetectFlag ) {
124
- i2c_stop (obj );
125
133
return I2C_ERROR_NO_SLAVE ;
126
134
} else {
127
- i2c_stop (obj );
128
135
return length ;
129
136
}
130
137
}
@@ -134,13 +141,9 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
134
141
master_xfer .direction = kLPI2C_Write ;
135
142
master_xfer .data = (uint8_t * )data ;
136
143
master_xfer .dataSize = length ;
137
- if (obj -> next_repeated_start ) {
138
- master_xfer .flags |= kLPI2C_TransferRepeatedStartFlag ;
139
- }
140
144
if (!stop ) {
141
145
master_xfer .flags |= kLPI2C_TransferNoStopFlag ;
142
146
}
143
- obj -> next_repeated_start = master_xfer .flags & kLPI2C_TransferNoStopFlag ? 1 : 0 ;
144
147
145
148
if (LPI2C_MasterTransferBlocking (base , & master_xfer ) != kStatus_Success ) {
146
149
return I2C_ERROR_NO_SLAVE ;
0 commit comments