diff --git a/src/main/drivers/bus_i2c.h b/src/main/drivers/bus_i2c.h index 39db2094e..c71a57093 100644 --- a/src/main/drivers/bus_i2c.h +++ b/src/main/drivers/bus_i2c.h @@ -17,6 +17,10 @@ #pragma once +#define I2C_SHORT_TIMEOUT ((uint32_t)0x1000) +#define I2C_LONG_TIMEOUT ((uint32_t)(10 * I2C_SHORT_TIMEOUT)) +#define I2C_DEFAULT_TIMEOUT I2C_SHORT_TIMEOUT + typedef enum I2CDevice { I2CDEV_1, I2CDEV_2, diff --git a/src/main/drivers/bus_i2c_stm32f30x.c b/src/main/drivers/bus_i2c_stm32f30x.c index 8c6c3d049..8b33e7e46 100644 --- a/src/main/drivers/bus_i2c_stm32f30x.c +++ b/src/main/drivers/bus_i2c_stm32f30x.c @@ -30,9 +30,6 @@ #ifndef SOFT_I2C -#define I2C_SHORT_TIMEOUT ((uint32_t)0x1000) -#define I2C_LONG_TIMEOUT ((uint32_t)(10 * I2C_SHORT_TIMEOUT)) - #define I2C1_SCL_GPIO GPIOB #define I2C1_SCL_GPIO_AF GPIO_AF_4 #define I2C1_SCL_PIN GPIO_Pin_6 @@ -123,6 +120,7 @@ void i2cInitPort(I2C_TypeDef *I2Cx) //I2C_InitStructure.I2C_Timing = 0x0070123D; // 800 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 50, Hold 5. I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4. + I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); @@ -197,7 +195,7 @@ bool i2cWrite(uint8_t addr_, uint8_t reg, uint8_t data) addr_ <<= 1; /* Test on BUSY Flag */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_BUSY) != RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -208,7 +206,7 @@ bool i2cWrite(uint8_t addr_, uint8_t reg, uint8_t data) I2C_TransferHandling(I2Cx, addr_, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -219,7 +217,7 @@ bool i2cWrite(uint8_t addr_, uint8_t reg, uint8_t data) I2C_SendData(I2Cx, (uint8_t) reg); /* Wait until TCR flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TCR) == RESET) { if ((i2cTimeout--) == 0) { @@ -231,7 +229,7 @@ bool i2cWrite(uint8_t addr_, uint8_t reg, uint8_t data) I2C_TransferHandling(I2Cx, addr_, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); /* Wait until TXIS flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -242,7 +240,7 @@ bool i2cWrite(uint8_t addr_, uint8_t reg, uint8_t data) I2C_SendData(I2Cx, data); /* Wait until STOPF flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_STOPF) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -260,7 +258,7 @@ bool i2cRead(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) addr_ <<= 1; /* Test on BUSY Flag */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_BUSY) != RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -271,7 +269,7 @@ bool i2cRead(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) I2C_TransferHandling(I2Cx, addr_, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -282,7 +280,7 @@ bool i2cRead(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) I2C_SendData(I2Cx, (uint8_t) reg); /* Wait until TC flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TC) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -295,7 +293,7 @@ bool i2cRead(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) /* Wait until all data are received */ while (len) { /* Wait until RXNE flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_RXNE) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx); @@ -312,7 +310,7 @@ bool i2cRead(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) } /* Wait until STOPF flag is set */ - i2cTimeout = I2C_LONG_TIMEOUT; + i2cTimeout = I2C_DEFAULT_TIMEOUT; while (I2C_GetFlagStatus(I2Cx, I2C_ISR_STOPF) == RESET) { if ((i2cTimeout--) == 0) { return i2cTimeoutUserCallback(I2Cx);