Skip to content

Commit

Permalink
fixup! Added I2C2 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
mdiepart authored and silseva committed Apr 28, 2024
1 parent 7c30a3d commit 92d6b2e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion platform/mcu/STM32F4xx/drivers/I2C2.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void smb2_init()

/* Enable ERR interrupt to handle timeouts and alerts (if needed) */
I2C2->CR2 = I2C_CR2_ITERREN | 42; /* ERR interrupt, APB1 clock is 42MHz */
NVIC_ClearPendingIRQ(I2C2_ER_IRQn);
NVIC_EnableIRQ(I2C2_ER_IRQn);
NVIC_SetPriority(I2C2_ER_IRQn, 10);

Expand Down Expand Up @@ -150,7 +151,10 @@ error_t i2c2_write_bytes(uint8_t addr, uint8_t *bytes, uint16_t length, bool sto
while(!(I2C2->SR1 & I2C_SR1_ADDR_Msk))
{
if(i2c2_smb_state.timeout)
{
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
else if(I2C2->SR1 & I2C_SR1_AF_Msk)
{
I2C2->CR1 |= I2C_CR1_STOP;
Expand All @@ -174,7 +178,10 @@ error_t i2c2_write_bytes(uint8_t addr, uint8_t *bytes, uint16_t length, bool sto
while(!(I2C2->SR1 & I2C_SR1_TXE_Msk))
{
if(i2c2_smb_state.timeout)
{
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
}
}

Expand Down Expand Up @@ -210,7 +217,10 @@ error_t i2c2_read_bytes(uint8_t addr, uint8_t *bytes, uint16_t length, bool stop
while(!(I2C2->SR1 & I2C_SR1_ADDR_Msk))
{
if(i2c2_smb_state.timeout)
{
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
else if(I2C2->SR1 & I2C_SR1_AF_Msk)
{
I2C2->CR1 |= I2C_CR1_STOP;
Expand All @@ -236,7 +246,10 @@ error_t i2c2_read_bytes(uint8_t addr, uint8_t *bytes, uint16_t length, bool stop
while(!(I2C2->SR1 & I2C_SR1_RXNE_Msk))
{
if(i2c2_smb_state.timeout)
{
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
}
if(i+2 >= length)
I2C2->CR1 &= ~I2C_CR1_ACK; // Nack
Expand Down Expand Up @@ -351,7 +364,10 @@ error_t smb2_block_read(uint8_t addr, uint8_t command, uint8_t *bytes, uint8_t *
while(!(I2C2->SR1 & I2C_SR1_ADDR_Msk))
{
if(i2c2_smb_state.timeout)
{
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
else if(I2C2->SR1 & I2C_SR1_AF_Msk)
{
I2C2->CR1 |= I2C_CR1_STOP;
Expand Down Expand Up @@ -385,6 +401,7 @@ error_t smb2_block_read(uint8_t addr, uint8_t command, uint8_t *bytes, uint8_t *
if(i2c2_smb_state.timeout)
{
free(bytes);
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
}
Expand Down Expand Up @@ -427,7 +444,10 @@ error_t smb2_block_write_block_read_process_call(uint8_t addr, uint8_t command,
while(!(I2C2->SR1 & I2C_SR1_ADDR_Msk))
{
if(i2c2_smb_state.timeout)
{
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
else if(I2C2->SR1 & I2C_SR1_AF_Msk)
{
I2C2->CR1 |= I2C_CR1_STOP;
Expand Down Expand Up @@ -462,6 +482,7 @@ error_t smb2_block_write_block_read_process_call(uint8_t addr, uint8_t command,
if(i2c2_smb_state.timeout)
{
free(bytesR);
i2c2_smb_state.timeout = false;
return ETIMEDOUT;
}
}
Expand All @@ -479,7 +500,10 @@ error_t smb2_block_write_block_read_process_call(uint8_t addr, uint8_t command,

}

void I2C2_ER_IRQHandler()
// Definition of C++ mangled name for linking to NVIC table
void __attribute__((alias("I2C2_ER_IRQHandler"))) _Z18I2C2_ER_IRQHandlerv();

void __attribute__((used)) I2C2_ER_IRQHandler()
{
if(I2C2->SR1 & I2C_SR1_TIMEOUT_Msk)
{
Expand Down

0 comments on commit 92d6b2e

Please sign in to comment.