mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Add cambus sequential read/write functions.
* These two functions handle no-stop/repeated start.
This commit is contained in:
parent
bd32cb67e8
commit
d9a9768d76
@ -10,8 +10,9 @@
|
||||
*/
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "py/mphal.h"
|
||||
|
||||
#include STM32_HAL_H
|
||||
#include "systick.h"
|
||||
#include "omv_boardconfig.h"
|
||||
#include "cambus.h"
|
||||
#define I2C_TIMEOUT (1000)
|
||||
@ -215,3 +216,56 @@ int cambus_writew_bytes(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint16_t reg_a
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cambus_read_bytes_seq(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint8_t *buf, int len, bool nostop)
|
||||
{
|
||||
int ret = 0;
|
||||
HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
|
||||
HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
|
||||
|
||||
if (HAL_I2C_Master_Seq_Receive_IT(i2c, slv_addr, buf, len,
|
||||
(nostop == true) ? I2C_FIRST_FRAME : I2C_FIRST_AND_LAST_FRAME) != HAL_OK) {
|
||||
ret = -1;
|
||||
goto i2c_error;
|
||||
}
|
||||
|
||||
mp_uint_t tick_start = mp_hal_ticks_ms();
|
||||
while (HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) {
|
||||
if ((mp_hal_ticks_ms() - tick_start) >= I2C_TIMEOUT) {
|
||||
ret = -1;
|
||||
}
|
||||
__WFI();
|
||||
}
|
||||
|
||||
i2c_error:
|
||||
HAL_NVIC_DisableIRQ(I2C2_EV_IRQn);
|
||||
HAL_NVIC_DisableIRQ(I2C2_ER_IRQn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int cambus_write_bytes_seq(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint8_t *buf, int len, bool nostop)
|
||||
{
|
||||
int ret = 0;
|
||||
HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
|
||||
HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
|
||||
|
||||
if (HAL_I2C_Master_Seq_Transmit_IT(i2c, slv_addr, buf, len,
|
||||
(nostop == true) ? I2C_FIRST_FRAME : I2C_FIRST_AND_LAST_FRAME) != HAL_OK) {
|
||||
ret = -1;
|
||||
goto i2c_error;
|
||||
}
|
||||
|
||||
mp_uint_t tick_start = mp_hal_ticks_ms();
|
||||
while (HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) {
|
||||
if ((mp_hal_ticks_ms() - tick_start) >= I2C_TIMEOUT) {
|
||||
ret = -1;
|
||||
goto i2c_error;
|
||||
}
|
||||
__WFI();
|
||||
}
|
||||
|
||||
i2c_error:
|
||||
HAL_NVIC_DisableIRQ(I2C2_EV_IRQn);
|
||||
HAL_NVIC_DisableIRQ(I2C2_ER_IRQn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#ifndef __CAMBUS_H__
|
||||
#define __CAMBUS_H__
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include STM32_HAL_H
|
||||
#if defined(STM32F4)
|
||||
#define I2C_TIMING_STANDARD (100000U)
|
||||
@ -46,4 +47,6 @@ int cambus_read_bytes(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint8_t reg_addr
|
||||
int cambus_write_bytes(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint8_t reg_addr, uint8_t *buf, int len);
|
||||
int cambus_readw_bytes(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len);
|
||||
int cambus_writew_bytes(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint16_t reg_addr, uint8_t *buf, int len);
|
||||
int cambus_read_bytes_seq(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint8_t *buf, int len, bool nostop);
|
||||
int cambus_write_bytes_seq(I2C_HandleTypeDef *i2c, uint8_t slv_addr, uint8_t *buf, int len, bool nostop);
|
||||
#endif // __CAMBUS_H__
|
||||
|
||||
Loading…
Reference in New Issue
Block a user