|
22 | 22 |
|
23 | 23 | #include "cpu.h"
|
24 | 24 | #include "mutex.h"
|
| 25 | +#include "byteorder.h" |
25 | 26 |
|
26 | 27 | #include "periph_conf.h"
|
27 | 28 | #include "periph/i2c.h"
|
@@ -183,16 +184,23 @@ int i2c_read_bytes(i2c_t dev, uint16_t address, void *data, size_t length, uint8
|
183 | 184 | int i2c_read_regs(i2c_t dev, uint16_t address, uint16_t reg,
|
184 | 185 | void *data, size_t length, uint8_t flags)
|
185 | 186 | {
|
| 187 | + uint16_t reg_end = reg; |
| 188 | + |
186 | 189 | if (flags & (I2C_NOSTART | I2C_NOSTOP)) {
|
187 | 190 | return -EOPNOTSUPP;
|
188 | 191 | }
|
189 | 192 |
|
| 193 | + /* Handle endianess of register if 16 bit */ |
| 194 | + if (flags & I2C_REG16) { |
| 195 | + reg_end = htons(reg); /* Make sure register is in big-endian on I2C bus */ |
| 196 | + } |
| 197 | + |
190 | 198 | /* prepare transfer */
|
191 | 199 | I2C_TransferSeq_TypeDef transfer;
|
192 | 200 |
|
193 | 201 | transfer.addr = (address << 1);
|
194 | 202 | transfer.flags = I2C_FLAG_WRITE_READ | ((flags & I2C_ADDR10) ? I2C_FLAG_10BIT_ADDR : 0);
|
195 |
| - transfer.buf[0].data = (uint8_t *) ® |
| 203 | + transfer.buf[0].data = (uint8_t *) ®_end; |
196 | 204 | transfer.buf[0].len = (flags & I2C_REG16) ? 2 : 1;
|
197 | 205 | transfer.buf[1].data = (uint8_t *) data;
|
198 | 206 | transfer.buf[1].len = length;
|
@@ -222,16 +230,23 @@ int i2c_write_bytes(i2c_t dev, uint16_t address, const void *data, size_t length
|
222 | 230 | int i2c_write_regs(i2c_t dev, uint16_t address, uint16_t reg,
|
223 | 231 | const void *data, size_t length, uint8_t flags)
|
224 | 232 | {
|
| 233 | + uint16_t reg_end = reg; |
| 234 | + |
225 | 235 | if (flags & (I2C_NOSTART | I2C_NOSTOP)) {
|
226 | 236 | return -EOPNOTSUPP;
|
227 | 237 | }
|
228 | 238 |
|
| 239 | + /* Handle endianess of register if 16 bit */ |
| 240 | + if (flags & I2C_REG16) { |
| 241 | + reg_end = htons(reg); /* Make sure register is in big-endian on I2C bus */ |
| 242 | + } |
| 243 | + |
229 | 244 | /* prepare transfer */
|
230 | 245 | I2C_TransferSeq_TypeDef transfer;
|
231 | 246 |
|
232 | 247 | transfer.addr = (address << 1);
|
233 | 248 | transfer.flags = I2C_FLAG_WRITE_WRITE | ((flags & I2C_ADDR10) ? I2C_FLAG_10BIT_ADDR : 0);
|
234 |
| - transfer.buf[0].data = (uint8_t *) ® |
| 249 | + transfer.buf[0].data = (uint8_t *) ®_end; |
235 | 250 | transfer.buf[0].len = (flags & I2C_REG16) ? 2 : 1;
|
236 | 251 | transfer.buf[1].data = (uint8_t *) data;
|
237 | 252 | transfer.buf[1].len = length;
|
|
0 commit comments