mirror of
https://github.com/OpenRTX/OpenRTX
synced 2026-08-08 12:29:06 -04:00
mcu: stm32: apply clang-format to i2c_stm32 driver
Signed-off-by: Silvano Seva <silseva@fastwebnet.it>
This commit is contained in:
parent
39cfcb9686
commit
2588968e6f
3 changed files with 50 additions and 62 deletions
|
|
@ -11,11 +11,10 @@
|
|||
|
||||
int i2c_init(const struct i2cDevice *dev, const uint8_t speed)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *) dev->periph;
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)dev->periph;
|
||||
uint32_t apbMask = 0;
|
||||
|
||||
switch((uint32_t) i2c)
|
||||
{
|
||||
switch ((uint32_t)i2c) {
|
||||
case I2C1_BASE:
|
||||
apbMask = RCC_APB1ENR_I2C1EN;
|
||||
break;
|
||||
|
|
@ -36,21 +35,20 @@ int i2c_init(const struct i2cDevice *dev, const uint8_t speed)
|
|||
RCC->APB1ENR |= apbMask;
|
||||
__DSB();
|
||||
|
||||
RCC->APB1RSTR |= apbMask;
|
||||
RCC->APB1RSTR |= apbMask;
|
||||
RCC->APB1RSTR &= ~apbMask;
|
||||
|
||||
switch(speed)
|
||||
{
|
||||
switch (speed) {
|
||||
case I2C_SPEED_LOW:
|
||||
i2c->CR2 = 42; /* No interrupts, APB1 clock is 42MHz */
|
||||
i2c->CCR = 1050; /* Freq = 20kHz */
|
||||
i2c->TRISE = 63; /* Max possible rise time */
|
||||
i2c->CR2 = 42; /* No interrupts, APB1 clock is 42MHz */
|
||||
i2c->CCR = 1050; /* Freq = 20kHz */
|
||||
i2c->TRISE = 63; /* Max possible rise time */
|
||||
break;
|
||||
|
||||
case I2C_SPEED_100kHz:
|
||||
i2c->CR2 = 42; /* No interrupts, APB1 clock is 42MHz */
|
||||
i2c->CCR = 210; /* Freq = 100kHz */
|
||||
i2c->TRISE = 43; /* Conforms to max rise time of 1000 ns */
|
||||
i2c->CR2 = 42; /* No interrupts, APB1 clock is 42MHz */
|
||||
i2c->CCR = 210; /* Freq = 100kHz */
|
||||
i2c->TRISE = 43; /* Conforms to max rise time of 1000 ns */
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
@ -60,15 +58,15 @@ int i2c_init(const struct i2cDevice *dev, const uint8_t speed)
|
|||
|
||||
i2c->CR1 = I2C_CR1_PE; /* Enable peripheral */
|
||||
|
||||
if(dev->mutex != NULL)
|
||||
pthread_mutex_init((pthread_mutex_t *) dev->mutex, NULL);
|
||||
if (dev->mutex != NULL)
|
||||
pthread_mutex_init((pthread_mutex_t *)dev->mutex, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void i2c_terminate(const struct i2cDevice *dev)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *) dev->periph;
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)dev->periph;
|
||||
|
||||
i2c_acquire(dev);
|
||||
|
||||
|
|
@ -76,56 +74,53 @@ void i2c_terminate(const struct i2cDevice *dev)
|
|||
RCC->APB1ENR &= ~RCC_APB1ENR_I2C1EN;
|
||||
__DSB();
|
||||
|
||||
if(dev->mutex != NULL)
|
||||
pthread_mutex_destroy((pthread_mutex_t *) dev->mutex);
|
||||
if (dev->mutex != NULL)
|
||||
pthread_mutex_destroy((pthread_mutex_t *)dev->mutex);
|
||||
}
|
||||
|
||||
|
||||
static int i2c_readImpl(const struct i2cDevice *dev, const uint8_t addr,
|
||||
void *data, const size_t length, const bool stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *) dev->periph;
|
||||
uint8_t *bytes = (uint8_t *) data;
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)dev->periph;
|
||||
uint8_t *bytes = (uint8_t *)data;
|
||||
|
||||
if(length == 0 || bytes == NULL)
|
||||
if (length == 0 || bytes == NULL)
|
||||
return -EINVAL;
|
||||
|
||||
// Send start
|
||||
i2c->CR1 |= I2C_CR1_START;
|
||||
while((i2c->SR1 & I2C_SR1_SB_Msk) == 0) ;
|
||||
while ((i2c->SR1 & I2C_SR1_SB_Msk) == 0)
|
||||
;
|
||||
|
||||
// Send address (read)
|
||||
i2c->DR = (addr << 1) | 1;
|
||||
|
||||
if(length == 1)
|
||||
if (length == 1)
|
||||
i2c->CR1 &= ~I2C_CR1_ACK; // Nack
|
||||
else
|
||||
i2c->CR1 |= I2C_CR1_ACK; // Ack
|
||||
|
||||
// Wait for ADDR bit to be set then read SR1 and SR2
|
||||
while((i2c->SR1 & I2C_SR1_ADDR_Msk) == 0)
|
||||
{
|
||||
while ((i2c->SR1 & I2C_SR1_ADDR_Msk) == 0) {
|
||||
// Check if the address was NACKed
|
||||
if((i2c->SR1 & I2C_SR1_AF_Msk) != 0)
|
||||
{
|
||||
if ((i2c->SR1 & I2C_SR1_AF_Msk) != 0) {
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
return -ENODEV;
|
||||
}
|
||||
}
|
||||
|
||||
// Read SR2 by checking that we are receiver
|
||||
if((i2c->SR2 & I2C_SR2_TRA_Msk) != 0)
|
||||
{
|
||||
if ((i2c->SR2 & I2C_SR2_TRA_Msk) != 0) {
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
return -EPROTO;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < length; i++)
|
||||
{
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
// Wait for data to be available
|
||||
while((i2c->SR1 & I2C_SR1_RXNE_Msk) == 0) ;
|
||||
while ((i2c->SR1 & I2C_SR1_RXNE_Msk) == 0)
|
||||
;
|
||||
|
||||
if((i + 2) >= length)
|
||||
if ((i + 2) >= length)
|
||||
i2c->CR1 &= ~I2C_CR1_ACK; // Nack
|
||||
else
|
||||
i2c->CR1 |= I2C_CR1_ACK; // Ack
|
||||
|
|
@ -133,7 +128,7 @@ static int i2c_readImpl(const struct i2cDevice *dev, const uint8_t addr,
|
|||
bytes[i] = i2c->DR;
|
||||
}
|
||||
|
||||
if(stop)
|
||||
if (stop)
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
|
||||
return 0;
|
||||
|
|
@ -142,53 +137,50 @@ static int i2c_readImpl(const struct i2cDevice *dev, const uint8_t addr,
|
|||
static int i2c_writeImpl(const struct i2cDevice *dev, const uint8_t addr,
|
||||
const void *data, const size_t length, const bool stop)
|
||||
{
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *) dev->periph;
|
||||
const uint8_t *bytes = (const uint8_t *) data;
|
||||
I2C_TypeDef *i2c = (I2C_TypeDef *)dev->periph;
|
||||
const uint8_t *bytes = (const uint8_t *)data;
|
||||
|
||||
if(length == 0 || bytes == NULL)
|
||||
if (length == 0 || bytes == NULL)
|
||||
return -EINVAL;
|
||||
|
||||
// Send start
|
||||
i2c->CR1 |= I2C_CR1_START;
|
||||
while((i2c->SR1 & I2C_SR1_SB_Msk) == 0) ;
|
||||
while ((i2c->SR1 & I2C_SR1_SB_Msk) == 0)
|
||||
;
|
||||
|
||||
// Send address (write)
|
||||
i2c->DR = addr << 1;
|
||||
|
||||
// Wait for ADDR bit to be set then read SR1 and SR2
|
||||
while((i2c->SR1 & I2C_SR1_ADDR_Msk) == 0)
|
||||
{
|
||||
while ((i2c->SR1 & I2C_SR1_ADDR_Msk) == 0) {
|
||||
// Check if the address was NACKed
|
||||
if((i2c->SR1 & I2C_SR1_AF_Msk) != 0)
|
||||
{
|
||||
if ((i2c->SR1 & I2C_SR1_AF_Msk) != 0) {
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
return -ENODEV;
|
||||
}
|
||||
}
|
||||
|
||||
// Read SR2 by checking that we are transmitter
|
||||
if((i2c->SR2 & I2C_SR2_TRA_Msk) == 0)
|
||||
{
|
||||
if ((i2c->SR2 & I2C_SR2_TRA_Msk) == 0) {
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
return -EPROTO; // We are not transmitter
|
||||
}
|
||||
|
||||
// Send data
|
||||
for(size_t i = 0; i < length; i++)
|
||||
{
|
||||
i2c->DR = bytes[i]; // Send data
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
i2c->DR = bytes[i]; // Send data
|
||||
// Wait for data to be sent
|
||||
while((i2c->SR1 & I2C_SR1_TXE_Msk) == 0) ;
|
||||
while ((i2c->SR1 & I2C_SR1_TXE_Msk) == 0)
|
||||
;
|
||||
}
|
||||
|
||||
if(stop)
|
||||
if (stop)
|
||||
i2c->CR1 |= I2C_CR1_STOP;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct i2cApi i2cStm32_driver =
|
||||
{
|
||||
.read = i2c_readImpl,
|
||||
.write = i2c_writeImpl
|
||||
const struct i2cApi i2cStm32_driver = {
|
||||
.read = i2c_readImpl,
|
||||
.write = i2c_writeImpl,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -22,13 +22,11 @@ extern const struct i2cApi i2cStm32_driver;
|
|||
* @param mutx: pointer to mutex, or NULL.
|
||||
*/
|
||||
#define I2C_STM32_DEVICE_DEFINE(name, peripheral, mutx) \
|
||||
const struct i2cDevice name = \
|
||||
{ \
|
||||
.driver = &i2cStm32_driver, \
|
||||
.periph = peripheral, \
|
||||
.mutex = mutx \
|
||||
};
|
||||
|
||||
const struct i2cDevice name = { \
|
||||
.driver = &i2cStm32_driver, \
|
||||
.periph = peripheral, \
|
||||
.mutex = mutx, \
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialise an STM32 I2C peripheral with a given bus speed.
|
||||
|
|
|
|||
|
|
@ -286,8 +286,6 @@ platform/mcu/STM32F4xx/drivers/USART3.h
|
|||
platform/mcu/STM32F4xx/drivers/delays.cpp
|
||||
platform/mcu/STM32F4xx/drivers/flash.c
|
||||
platform/mcu/STM32F4xx/drivers/flash.h
|
||||
platform/mcu/STM32F4xx/drivers/i2c_stm32.c
|
||||
platform/mcu/STM32F4xx/drivers/i2c_stm32.h
|
||||
platform/mcu/STM32F4xx/drivers/rcc.c
|
||||
platform/mcu/STM32F4xx/drivers/rcc.h
|
||||
platform/mcu/STM32F4xx/drivers/rng.c
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue