25
25
#include "peripheral_clock_defines.h"
26
26
#include "PeripheralPins.h"
27
27
28
+ /* 7 bit IIC addr - R/W flag not included */
28
29
static int i2c_address = 0 ;
29
30
/* Array of I2C peripheral base address. */
30
31
static I2C_Type * const i2c_addrs [] = I2C_BASE_PTRS ;
@@ -35,6 +36,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
35
36
uint32_t i2c_sda = pinmap_peripheral (sda , PinMap_I2C_SDA );
36
37
uint32_t i2c_scl = pinmap_peripheral (scl , PinMap_I2C_SCL );
37
38
obj -> instance = pinmap_merge (i2c_sda , i2c_scl );
39
+ obj -> next_repeated_start = 0 ;
38
40
MBED_ASSERT ((int )obj -> instance != NC );
39
41
40
42
i2c_master_config_t master_config ;
@@ -77,6 +79,7 @@ int i2c_start(i2c_t *obj) {
77
79
78
80
int i2c_stop (i2c_t * obj ) {
79
81
if (I2C_MasterStop (i2c_addrs [obj -> instance ]) != kStatus_Success ) {
82
+ obj -> next_repeated_start = 0 ;
80
83
return 1 ;
81
84
}
82
85
@@ -94,12 +97,19 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
94
97
I2C_Type * base = i2c_addrs [obj -> instance ];
95
98
i2c_master_transfer_t master_xfer ;
96
99
97
- i2c_address = address ;
100
+ i2c_address = address >> 1 ;
98
101
memset (& master_xfer , 0 , sizeof (master_xfer ));
99
- master_xfer .slaveAddress = address ;
102
+ master_xfer .slaveAddress = address >> 1 ;
100
103
master_xfer .direction = kI2C_Read ;
101
104
master_xfer .data = (uint8_t * )data ;
102
105
master_xfer .dataSize = length ;
106
+ if (obj -> next_repeated_start ) {
107
+ master_xfer .flags |= kI2C_TransferRepeatedStartFlag ;
108
+ }
109
+ if (!stop ) {
110
+ master_xfer .flags |= kI2C_TransferNoStopFlag ;
111
+ }
112
+ obj -> next_repeated_start = master_xfer .flags & kI2C_TransferNoStopFlag ? 1 : 0 ;
103
113
104
114
/* The below function will issue a STOP signal at the end of the transfer.
105
115
* This is required by the hardware in order to receive the last byte
@@ -116,12 +126,17 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
116
126
i2c_master_transfer_t master_xfer ;
117
127
118
128
memset (& master_xfer , 0 , sizeof (master_xfer ));
119
- master_xfer .slaveAddress = address ;
129
+ master_xfer .slaveAddress = address >> 1 ;
120
130
master_xfer .direction = kI2C_Write ;
121
131
master_xfer .data = (uint8_t * )data ;
122
132
master_xfer .dataSize = length ;
123
- if (!stop )
124
- master_xfer .flags = kI2C_TransferNoStopFlag ;
133
+ if (obj -> next_repeated_start ) {
134
+ master_xfer .flags |= kI2C_TransferRepeatedStartFlag ;
135
+ }
136
+ if (!stop ) {
137
+ master_xfer .flags |= kI2C_TransferNoStopFlag ;
138
+ }
139
+ obj -> next_repeated_start = master_xfer .flags & kI2C_TransferNoStopFlag ? 1 : 0 ;
125
140
126
141
if (I2C_MasterTransferBlocking (base , & master_xfer ) != kStatus_Success ) {
127
142
return I2C_ERROR_NO_SLAVE ;
0 commit comments