mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge pull request #1078 from openmv/cambus_functions
Add new and updated read/write bytes functions.
This commit is contained in:
commit
78a00c0bbf
@ -27,11 +27,11 @@ void MLX90621_I2CInit(cambus_t *hbus)
|
|||||||
|
|
||||||
int MLX90621_I2CReadEEPROM(uint8_t slaveAddr, uint8_t startAddress, uint16_t nMemAddressRead, uint8_t *data)
|
int MLX90621_I2CReadEEPROM(uint8_t slaveAddr, uint8_t startAddress, uint16_t nMemAddressRead, uint8_t *data)
|
||||||
{
|
{
|
||||||
if (cambus_write_bytes_seq(bus, (slaveAddr << 1), &startAddress, 1, true) != 0) {
|
if (cambus_write_bytes(bus, (slaveAddr << 1), &startAddress, 1, CAMBUS_XFER_NO_STOP) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cambus_read_bytes_seq(bus, (slaveAddr << 1), data, nMemAddressRead, false) != 0) {
|
if (cambus_read_bytes(bus, (slaveAddr << 1), data, nMemAddressRead, CAMBUS_XFER_NO_FLAGS) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,11 +48,11 @@ int MLX90621_I2CRead(uint8_t slaveAddr,uint8_t command,
|
|||||||
nMemAddressRead
|
nMemAddressRead
|
||||||
};
|
};
|
||||||
|
|
||||||
if (cambus_write_bytes_seq(bus, (slaveAddr << 1), cmd, 4, true) != 0) {
|
if (cambus_write_bytes(bus, (slaveAddr << 1), cmd, 4, CAMBUS_XFER_NO_STOP) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cambus_read_bytes_seq(bus, (slaveAddr << 1), (uint8_t *) data, nMemAddressRead * 2, false) != 0) {
|
if (cambus_read_bytes(bus, (slaveAddr << 1), (uint8_t *) data, nMemAddressRead * 2, CAMBUS_XFER_NO_FLAGS) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ int MLX90621_I2CWrite(uint8_t slaveAddr, uint8_t command, uint8_t checkValue, ui
|
|||||||
(data >> 8)
|
(data >> 8)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (cambus_write_bytes_seq(bus, (slaveAddr << 1), cmd, 5, false) != 0) {
|
if (cambus_write_bytes(bus, (slaveAddr << 1), cmd, 5, CAMBUS_XFER_NO_FLAGS) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -35,24 +35,34 @@ int MLX90640_I2CGeneralReset()
|
|||||||
|
|
||||||
int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
|
int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
|
||||||
{
|
{
|
||||||
uint8_t* p = (uint8_t*) data;
|
startAddress = __REVSH(startAddress);
|
||||||
if (cambus_readw_bytes(bus, (slaveAddr<<1), startAddress, p, nMemAddressRead*2) != 0) {
|
|
||||||
|
if (cambus_write_bytes(bus, (slaveAddr<<1), (uint8_t *) &startAddress, 2, CAMBUS_XFER_NO_STOP) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int cnt=0; cnt < nMemAddressRead*2; cnt+=2) {
|
|
||||||
uint8_t tempBuffer = p[cnt+1];
|
if (cambus_read_bytes(bus, (slaveAddr<<1), (uint8_t *) data, nMemAddressRead*2, CAMBUS_XFER_NO_FLAGS) != 0) {
|
||||||
p[cnt+1] = p[cnt];
|
return -1;
|
||||||
p[cnt] = tempBuffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(int i=0; i<nMemAddressRead; i++) {
|
||||||
|
data[i] = __REVSH(data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
|
int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
|
||||||
{
|
{
|
||||||
data = (data >> 8) | (data << 8);
|
data = __REVSH(data);
|
||||||
if (cambus_writew_bytes(bus, (slaveAddr << 1), writeAddress, (uint8_t*) &data, 2) != 0) {
|
writeAddress = __REVSH(writeAddress);
|
||||||
|
|
||||||
|
if (cambus_write_bytes(bus, (slaveAddr << 1), (uint8_t*) &writeAddress, 2, CAMBUS_XFER_SUSPEND) != 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cambus_write_bytes(bus, (slaveAddr << 1), (uint8_t *) &data, 2, CAMBUS_XFER_NO_FLAGS) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -13,6 +13,18 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "cambus_struct.h"
|
#include "cambus_struct.h"
|
||||||
|
// For use with read/write_bytes
|
||||||
|
enum cambus_xfer_flags {
|
||||||
|
// Stop condition after the transfer.
|
||||||
|
// Normal transfer with start condition, address, data and stop condition.
|
||||||
|
CAMBUS_XFER_NO_FLAGS = (0<<0),
|
||||||
|
// No stop condition after the transfer.
|
||||||
|
// This flag allows the next transfer to change direction with repeated start.
|
||||||
|
CAMBUS_XFER_NO_STOP = (1<<0),
|
||||||
|
// No stop condition after the transfer.
|
||||||
|
// This flag allows chaining multiple writes or reads with the same direction.
|
||||||
|
CAMBUS_XFER_SUSPEND = (1<<1),
|
||||||
|
};
|
||||||
int cambus_init(cambus_t *bus, uint32_t bus_id, uint32_t speed);
|
int cambus_init(cambus_t *bus, uint32_t bus_id, uint32_t speed);
|
||||||
int cambus_deinit(cambus_t *bus);
|
int cambus_deinit(cambus_t *bus);
|
||||||
int cambus_scan(cambus_t *bus);
|
int cambus_scan(cambus_t *bus);
|
||||||
@ -26,10 +38,6 @@ int cambus_readw(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint16_t *r
|
|||||||
int cambus_writew(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint16_t reg_data);
|
int cambus_writew(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint16_t reg_data);
|
||||||
int cambus_readw2(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint16_t *reg_data);
|
int cambus_readw2(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint16_t *reg_data);
|
||||||
int cambus_writew2(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint16_t reg_data);
|
int cambus_writew2(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint16_t reg_data);
|
||||||
int cambus_read_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len);
|
int cambus_read_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, uint32_t flags);
|
||||||
int cambus_write_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len);
|
int cambus_write_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, uint32_t flags);
|
||||||
int cambus_readw_bytes(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len);
|
|
||||||
int cambus_writew_bytes(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len);
|
|
||||||
int cambus_read_bytes_seq(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, bool nostop);
|
|
||||||
int cambus_write_bytes_seq(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, bool nostop);
|
|
||||||
#endif // __CAMBUS_H__
|
#endif // __CAMBUS_H__
|
||||||
|
|||||||
@ -133,16 +133,17 @@ static void fir_MLX90640_get_frame(float *Ta, float *To)
|
|||||||
static void fir_AMG8833_get_frame(float *Ta, float *To)
|
static void fir_AMG8833_get_frame(float *Ta, float *To)
|
||||||
{
|
{
|
||||||
int16_t temp;
|
int16_t temp;
|
||||||
PY_ASSERT_TRUE_MSG(cambus_read_bytes(&fir_bus, AMG8833_ADDR, AMG8833_THERMISTOR_REGISTER,
|
int error = 0;
|
||||||
(uint8_t *) &temp, sizeof(int16_t)) >= 0,
|
error |= cambus_write_bytes(&fir_bus, AMG8833_ADDR, (uint8_t [1]){AMG8833_THERMISTOR_REGISTER}, 1, CAMBUS_XFER_NO_STOP);
|
||||||
"Failed to read the AMG8833 sensor data!");
|
error |= cambus_read_bytes(&fir_bus, AMG8833_ADDR, (uint8_t *) &temp, sizeof(temp), CAMBUS_XFER_NO_FLAGS);
|
||||||
|
PY_ASSERT_TRUE_MSG((error == 0), "Failed to read the AMG8833 sensor data!");
|
||||||
|
|
||||||
*Ta = AMG8833_12_TO_16(temp) * 0.0625f;
|
*Ta = AMG8833_12_TO_16(temp) * 0.0625f;
|
||||||
|
|
||||||
int16_t *data = fb_alloc(AMG8833_WIDTH * AMG8833_HEIGHT * sizeof(int16_t), FB_ALLOC_NO_HINT);
|
int16_t *data = fb_alloc(AMG8833_WIDTH * AMG8833_HEIGHT * sizeof(int16_t), FB_ALLOC_NO_HINT);
|
||||||
PY_ASSERT_TRUE_MSG(cambus_read_bytes(&fir_bus, AMG8833_ADDR, AMG8833_TEMPERATURE_REGISTER,
|
error |= cambus_write_bytes(&fir_bus, AMG8833_ADDR, (uint8_t [1]){AMG8833_TEMPERATURE_REGISTER}, 1, CAMBUS_XFER_NO_STOP);
|
||||||
(uint8_t *) data, AMG8833_WIDTH * AMG8833_HEIGHT * sizeof(int16_t)) >= 0,
|
error |= cambus_read_bytes(&fir_bus, AMG8833_ADDR, (uint8_t *) data, AMG8833_WIDTH * AMG8833_HEIGHT * 2, CAMBUS_XFER_NO_FLAGS);
|
||||||
"Failed to read the AMG8833 sensor data!");
|
PY_ASSERT_TRUE_MSG((error == 0), "Failed to read the AMG8833 sensor data!");
|
||||||
|
|
||||||
for (int i = 0, ii = AMG8833_WIDTH * AMG8833_HEIGHT; i < ii; i++) {
|
for (int i = 0, ii = AMG8833_WIDTH * AMG8833_HEIGHT; i < ii; i++) {
|
||||||
To[i] = AMG8833_12_TO_16(data[i]) * 0.25f;
|
To[i] = AMG8833_12_TO_16(data[i]) * 0.25f;
|
||||||
@ -348,9 +349,9 @@ mp_obj_t py_fir_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
|||||||
FIR_AMG8833_RETRY:
|
FIR_AMG8833_RETRY:
|
||||||
cambus_init(&fir_bus, FIR_I2C_ID, CAMBUS_SPEED_STANDARD);
|
cambus_init(&fir_bus, FIR_I2C_ID, CAMBUS_SPEED_STANDARD);
|
||||||
|
|
||||||
int error = cambus_write_bytes(&fir_bus, AMG8833_ADDR, AMG8833_RESET_REGISTER,
|
int error = 0;
|
||||||
(uint8_t [1]){AMG8833_INITIAL_RESET_VALUE}, 1);
|
error |= cambus_write_bytes(&fir_bus, AMG8833_ADDR, (uint8_t [1]){AMG8833_RESET_REGISTER}, 1, CAMBUS_XFER_SUSPEND);
|
||||||
|
error |= cambus_write_bytes(&fir_bus, AMG8833_ADDR, (uint8_t [1]){AMG8833_INITIAL_RESET_VALUE}, 1, CAMBUS_XFER_NO_FLAGS);
|
||||||
if (error != 0) {
|
if (error != 0) {
|
||||||
if (first_init) {
|
if (first_init) {
|
||||||
first_init = false;
|
first_init = false;
|
||||||
@ -460,9 +461,10 @@ mp_obj_t py_fir_read_ta()
|
|||||||
}
|
}
|
||||||
case FIR_AMG8833: {
|
case FIR_AMG8833: {
|
||||||
int16_t temp;
|
int16_t temp;
|
||||||
PY_ASSERT_TRUE_MSG(cambus_read_bytes(&fir_bus, AMG8833_ADDR, AMG8833_THERMISTOR_REGISTER,
|
int error = 0;
|
||||||
(uint8_t *) &temp, sizeof(int16_t)) >= 0,
|
error |= cambus_write_bytes(&fir_bus, AMG8833_ADDR, (uint8_t [1]){AMG8833_THERMISTOR_REGISTER}, 1, CAMBUS_XFER_NO_STOP);
|
||||||
"Failed to read the AMG8833 sensor data!");
|
error |= cambus_read_bytes(&fir_bus, AMG8833_ADDR, (uint8_t *) &temp, sizeof(temp), CAMBUS_XFER_NO_FLAGS);
|
||||||
|
PY_ASSERT_TRUE_MSG((error == 0), "Failed to read the AMG8833 sensor data!");
|
||||||
return mp_obj_new_float(AMG8833_12_TO_16(temp) * 0.0625f);
|
return mp_obj_new_float(AMG8833_12_TO_16(temp) * 0.0625f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,6 +56,9 @@ int cambus_init(cambus_t *bus, uint32_t bus_id, uint32_t speed)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This bus needs to be enabled for suspended transfers.
|
||||||
|
nrfx_twi_enable(&bus->twi);
|
||||||
|
|
||||||
bus->initialized = true;
|
bus->initialized = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -63,6 +66,7 @@ int cambus_init(cambus_t *bus, uint32_t bus_id, uint32_t speed)
|
|||||||
int cambus_deinit(cambus_t *bus)
|
int cambus_deinit(cambus_t *bus)
|
||||||
{
|
{
|
||||||
if (bus->initialized) {
|
if (bus->initialized) {
|
||||||
|
nrfx_twi_disable(&bus->twi);
|
||||||
nrfx_twi_uninit(&bus->twi);
|
nrfx_twi_uninit(&bus->twi);
|
||||||
bus->initialized = false;
|
bus->initialized = false;
|
||||||
}
|
}
|
||||||
@ -79,83 +83,37 @@ int cambus_gencall(cambus_t *bus, uint8_t cmd)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cambus_read_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len)
|
int cambus_read_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, uint32_t flags)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
slv_addr = slv_addr >> 1;
|
slv_addr = slv_addr >> 1;
|
||||||
|
uint32_t xfer_flags = 0;
|
||||||
nrfx_twi_enable(&bus->twi);
|
if (flags & CAMBUS_XFER_SUSPEND) {
|
||||||
nrfx_twi_xfer_desc_t desc1 = NRFX_TWI_XFER_DESC_TX(slv_addr, ®_addr, 1);
|
xfer_flags |= NRFX_TWI_FLAG_SUSPEND;
|
||||||
if (nrfx_twi_xfer(&bus->twi, &desc1, NRFX_TWI_FLAG_TX_NO_STOP) != NRFX_SUCCESS) {
|
|
||||||
ret = -1;
|
|
||||||
goto i2c_error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nrfx_twi_xfer_desc_t desc2 = NRFX_TWI_XFER_DESC_RX(slv_addr, buf, len);
|
|
||||||
if (nrfx_twi_xfer(&bus->twi, &desc2, 0) != NRFX_SUCCESS) {
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_error:
|
|
||||||
nrfx_twi_disable(&bus->twi);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_write_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
slv_addr = slv_addr >> 1;
|
|
||||||
|
|
||||||
nrfx_twi_enable(&bus->twi);
|
|
||||||
nrfx_twi_xfer_desc_t desc1 = NRFX_TWI_XFER_DESC_TX(slv_addr, ®_addr, 1);
|
|
||||||
if (nrfx_twi_xfer(&bus->twi, &desc1, NRFX_TWI_FLAG_SUSPEND) != NRFX_SUCCESS) {
|
|
||||||
ret = -1;
|
|
||||||
goto i2c_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
nrfx_twi_xfer_desc_t desc2 = NRFX_TWI_XFER_DESC_TX(slv_addr, buf, len);
|
|
||||||
if (nrfx_twi_xfer(&bus->twi, &desc2, 0) != NRFX_SUCCESS) {
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_error:
|
|
||||||
nrfx_twi_disable(&bus->twi);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_readw_bytes(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_writew_bytes(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_read_bytes_seq(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, bool nostop)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
slv_addr = slv_addr >> 1;
|
|
||||||
nrfx_twi_enable(&bus->twi);
|
|
||||||
nrfx_twi_xfer_desc_t desc = NRFX_TWI_XFER_DESC_RX(slv_addr, buf, len);
|
nrfx_twi_xfer_desc_t desc = NRFX_TWI_XFER_DESC_RX(slv_addr, buf, len);
|
||||||
if (nrfx_twi_xfer(&bus->twi, &desc, 0) != NRFX_SUCCESS) {
|
if (nrfx_twi_xfer(&bus->twi, &desc, xfer_flags) != NRFX_SUCCESS) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
nrfx_twi_disable(&bus->twi);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cambus_write_bytes_seq(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, bool nostop)
|
int cambus_write_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, uint32_t flags)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
slv_addr = slv_addr >> 1;
|
slv_addr = slv_addr >> 1;
|
||||||
nrfx_twi_enable(&bus->twi);
|
uint32_t xfer_flags = 0;
|
||||||
|
if (flags & CAMBUS_XFER_NO_STOP) {
|
||||||
|
xfer_flags |= NRFX_TWI_FLAG_TX_NO_STOP;
|
||||||
|
} else if (flags & CAMBUS_XFER_SUSPEND) {
|
||||||
|
xfer_flags |= NRFX_TWI_FLAG_SUSPEND;
|
||||||
|
}
|
||||||
|
|
||||||
nrfx_twi_xfer_desc_t desc = NRFX_TWI_XFER_DESC_TX(slv_addr, buf, len);
|
nrfx_twi_xfer_desc_t desc = NRFX_TWI_XFER_DESC_TX(slv_addr, buf, len);
|
||||||
if (nrfx_twi_xfer(&bus->twi, &desc, (nostop == true) ? NRFX_TWI_FLAG_TX_NO_STOP:0) != NRFX_SUCCESS) {
|
if (nrfx_twi_xfer(&bus->twi, &desc, xfer_flags) != NRFX_SUCCESS) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
nrfx_twi_disable(&bus->twi);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -206,6 +206,19 @@ static int cambus_set_irq_state(cambus_t *bus, bool enabled)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int cambus_wait_timeout(cambus_t *bus, uint32_t timeout)
|
||||||
|
{
|
||||||
|
mp_uint_t tick_start;
|
||||||
|
tick_start = mp_hal_ticks_ms();
|
||||||
|
while (HAL_I2C_GetState(bus->i2c) != HAL_I2C_STATE_READY) {
|
||||||
|
if ((mp_hal_ticks_ms() - tick_start) >= I2C_TIMEOUT) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
__WFI();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int cambus_deinit(cambus_t *bus)
|
int cambus_deinit(cambus_t *bus)
|
||||||
{
|
{
|
||||||
if (bus->i2c && bus->i2c->Instance) {
|
if (bus->i2c && bus->i2c->Instance) {
|
||||||
@ -322,87 +335,48 @@ int cambus_writew2(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint16_t
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cambus_read_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len)
|
int cambus_read_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, uint32_t flags)
|
||||||
{
|
|
||||||
if (HAL_I2C_Mem_Read(bus->i2c, slv_addr, reg_addr,
|
|
||||||
I2C_MEMADD_SIZE_8BIT, buf, len, I2C_TIMEOUT) != HAL_OK) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_write_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
if (HAL_I2C_Mem_Write(bus->i2c, slv_addr, reg_addr,
|
|
||||||
I2C_MEMADD_SIZE_8BIT, buf, len, I2C_TIMEOUT) != HAL_OK) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_readw_bytes(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
if (HAL_I2C_Mem_Read(bus->i2c, slv_addr, reg_addr,
|
|
||||||
I2C_MEMADD_SIZE_16BIT, buf, len, I2C_TIMEOUT) != HAL_OK) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_writew_bytes(cambus_t *bus, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
if (HAL_I2C_Mem_Write(bus->i2c, slv_addr, reg_addr,
|
|
||||||
I2C_MEMADD_SIZE_16BIT, buf, len, I2C_TIMEOUT) != HAL_OK) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cambus_read_bytes_seq(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, bool nostop)
|
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
uint32_t xfer_flags = 0;
|
||||||
|
if (flags & CAMBUS_XFER_NO_STOP) {
|
||||||
|
xfer_flags |= I2C_FIRST_FRAME;
|
||||||
|
} else if (flags & CAMBUS_XFER_SUSPEND) {
|
||||||
|
xfer_flags |= I2C_NEXT_FRAME;
|
||||||
|
} else {
|
||||||
|
xfer_flags |= I2C_LAST_FRAME;
|
||||||
|
}
|
||||||
|
|
||||||
cambus_set_irq_state(bus, true);
|
cambus_set_irq_state(bus, true);
|
||||||
|
|
||||||
if (HAL_I2C_Master_Seq_Receive_IT(bus->i2c, slv_addr, buf, len,
|
if (HAL_I2C_Master_Seq_Receive_IT(bus->i2c, slv_addr, buf, len, xfer_flags) != HAL_OK
|
||||||
(nostop == true) ? I2C_FIRST_FRAME : I2C_FIRST_AND_LAST_FRAME) != HAL_OK) {
|
|| cambus_wait_timeout(bus, I2C_TIMEOUT) != 0) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto i2c_error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_uint_t tick_start = mp_hal_ticks_ms();
|
|
||||||
while (HAL_I2C_GetState(bus->i2c) != HAL_I2C_STATE_READY) {
|
|
||||||
if ((mp_hal_ticks_ms() - tick_start) >= I2C_TIMEOUT) {
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
__WFI();
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_error:
|
|
||||||
cambus_set_irq_state(bus, false);
|
cambus_set_irq_state(bus, false);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cambus_write_bytes_seq(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, bool nostop)
|
int cambus_write_bytes(cambus_t *bus, uint8_t slv_addr, uint8_t *buf, int len, uint32_t flags)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
uint32_t xfer_flags = 0;
|
||||||
|
if (flags & CAMBUS_XFER_NO_STOP) {
|
||||||
|
xfer_flags |= I2C_FIRST_FRAME;
|
||||||
|
} else if (flags & CAMBUS_XFER_SUSPEND) {
|
||||||
|
xfer_flags |= I2C_NEXT_FRAME;
|
||||||
|
} else {
|
||||||
|
xfer_flags |= I2C_LAST_FRAME;
|
||||||
|
}
|
||||||
|
|
||||||
cambus_set_irq_state(bus, true);
|
cambus_set_irq_state(bus, true);
|
||||||
|
|
||||||
if (HAL_I2C_Master_Seq_Transmit_IT(bus->i2c, slv_addr, buf, len,
|
if (HAL_I2C_Master_Seq_Transmit_IT(bus->i2c, slv_addr, buf, len, xfer_flags) != HAL_OK
|
||||||
(nostop == true) ? I2C_FIRST_FRAME : I2C_FIRST_AND_LAST_FRAME) != HAL_OK) {
|
|| cambus_wait_timeout(bus, I2C_TIMEOUT) != 0) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto i2c_error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_uint_t tick_start = mp_hal_ticks_ms();
|
|
||||||
while (HAL_I2C_GetState(bus->i2c) != HAL_I2C_STATE_READY) {
|
|
||||||
if ((mp_hal_ticks_ms() - tick_start) >= I2C_TIMEOUT) {
|
|
||||||
ret = -1;
|
|
||||||
goto i2c_error;
|
|
||||||
}
|
|
||||||
__WFI();
|
|
||||||
}
|
|
||||||
|
|
||||||
i2c_error:
|
|
||||||
cambus_set_irq_state(bus, false);
|
cambus_set_irq_state(bus, false);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user