mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
Diff: targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/i2c_api.c
- Revision:
- 215:83cf97a28428
- Parent:
- 185:e752b4ee7de1
- Child:
- 227:7bd0639b8911
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/i2c_api.c Tue May 27 08:15:07 2014 +0100 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F401RE/i2c_api.c Tue May 27 08:30:06 2014 +0100 @@ -93,7 +93,7 @@ // I2C configuration i2c_frequency(obj, 100000); // 100 kHz per default - + // I2C master by default obj->slave = 0; } @@ -111,11 +111,11 @@ I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2cHandle.Init.OwnAddress1 = 0; I2cHandle.Init.OwnAddress2 = 0; - HAL_I2C_Init(&I2cHandle); - if(obj->slave) { + HAL_I2C_Init(&I2cHandle); + if (obj->slave) { /* Enable Address Acknowledge */ I2cHandle.Instance->CR1 |= I2C_CR1_ACK; - } + } } else { error("I2C error: frequency setting failed (max 400kHz)."); } @@ -172,10 +172,10 @@ return 0; } } - - i2c->DR = __HAL_I2C_7BIT_ADD_READ(address); - - + + i2c->DR = __HAL_I2C_7BIT_ADD_READ(address); + + // Wait address is acknowledged timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) { @@ -222,9 +222,9 @@ return 0; } } - + i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address); - + // Wait address is acknowledged timeout = FLAG_TIMEOUT; @@ -235,7 +235,7 @@ } } __HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle); - + for (count = 0; count < length; count++) { if (i2c_byte_write(obj, data[count]) != 1) { i2c_stop(obj); @@ -283,7 +283,7 @@ // Wait until the byte is transmitted timeout = FLAG_TIMEOUT; while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) && - (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) { + (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) { if ((timeout--) == 0) { return 0; } @@ -325,7 +325,7 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) { I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); - if(enable_slave) { + if (enable_slave) { obj->slave = 1; /* Enable Address Acknowledge */ I2cHandle.Instance->CR1 |= I2C_CR1_ACK; @@ -340,19 +340,19 @@ int i2c_slave_receive(i2c_t *obj) { int retValue = NoData; - - if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { - if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { - if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1) - retValue = ReadAddressed; + + if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) { + if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) { + if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1) + retValue = ReadAddressed; else - retValue = WriteAddressed; - + retValue = WriteAddressed; + __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR); - } + } } - - return(retValue); + + return (retValue); } int i2c_slave_read(i2c_t *obj, char *data, int length) { @@ -362,8 +362,7 @@ I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); - while(length > 0) - { + while (length > 0) { /* Wait until RXNE flag is set */ // Wait until the byte is received Timeout = FLAG_TIMEOUT; @@ -372,14 +371,14 @@ if (Timeout == 0) { return 0; } - } + } /* Read data from DR */ (*data++) = I2cHandle.Instance->DR; length--; size++; - if((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)){ + if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) { /* Read data from DR */ (*data++) = I2cHandle.Instance->DR; length--; @@ -394,8 +393,8 @@ if (Timeout == 0) { return 0; } - } - + } + /* Clear STOP flag */ __HAL_I2C_CLEAR_STOPFLAG(&I2cHandle); @@ -418,8 +417,7 @@ I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c); - while(length > 0) - { + while (length > 0) { /* Wait until TXE flag is set */ Timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) { @@ -427,23 +425,22 @@ if (Timeout == 0) { return 0; } - } + } - - /* Write data to DR */ - I2cHandle.Instance->DR = (*data++); - length--; - size++; - if((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) - { /* Write data to DR */ I2cHandle.Instance->DR = (*data++); length--; size++; - } + + if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) { + /* Write data to DR */ + I2cHandle.Instance->DR = (*data++); + length--; + size++; + } } - + /* Wait until AF flag is set */ Timeout = FLAG_TIMEOUT; while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) { @@ -451,9 +448,9 @@ if (Timeout == 0) { return 0; } - } + } - + /* Clear AF flag */ __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);