[NUCLEO_F401RE] Update xxx_free() functions + typo

pull/329/head
bcostm 2014-05-26 16:14:33 +02:00
parent b968150d2f
commit 57db78211c
4 changed files with 102 additions and 50 deletions

View File

@ -93,7 +93,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
// I2C configuration
i2c_frequency(obj, 100000); // 100 kHz per default
// I2C master by default
obj->slave = 0;
}
@ -111,11 +111,11 @@ void i2c_frequency(i2c_t *obj, int hz) {
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 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
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 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
return 0;
}
}
i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address);
// Wait address is acknowledged
timeout = FLAG_TIMEOUT;
@ -235,7 +235,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
}
}
__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 @@ int i2c_byte_write(i2c_t *obj, int data) {
// 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_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
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 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
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 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
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 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
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 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
if (Timeout == 0) {
return 0;
}
}
}
/* Clear STOP flag */
__HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
@ -418,8 +417,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
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 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
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 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
if (Timeout == 0) {
return 0;
}
}
}
/* Clear AF flag */
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);

View File

@ -66,6 +66,8 @@ struct serial_s {
uint32_t databits;
uint32_t stopbits;
uint32_t parity;
PinName pin_tx;
PinName pin_rx;
};
struct spi_s {
@ -76,6 +78,10 @@ struct spi_s {
uint32_t mode;
uint32_t nss;
uint32_t br_presc;
PinName pin_miso;
PinName pin_mosi;
PinName pin_sclk;
PinName pin_ssel;
};
struct i2c_s {

View File

@ -93,12 +93,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
// Enable USART clock
if (obj->uart == UART_1) {
__USART1_CLK_ENABLE();
obj->index = 0;
}
if (obj->uart == UART_2) {
__USART2_CLK_ENABLE();
obj->index = 1;
}
if (obj->uart == UART_6) {
__USART6_CLK_ENABLE();
obj->index = 2;
}
// Configure the UART pins
@ -113,12 +116,10 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->stopbits = UART_STOPBITS_1;
obj->parity = UART_PARITY_NONE;
init_uart(obj);
obj->pin_tx = tx;
obj->pin_rx = rx;
// The index is used by irq
if (obj->uart == UART_1) obj->index = 0;
if (obj->uart == UART_2) obj->index = 1;
if (obj->uart == UART_6) obj->index = 2;
init_uart(obj);
// For stdio management
if (obj->uart == STDIO_UART) {
@ -129,6 +130,27 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
}
void serial_free(serial_t *obj) {
// Reset UART and disable clock
if (obj->uart == UART_1) {
__USART1_FORCE_RESET();
__USART1_RELEASE_RESET();
__USART1_CLK_DISABLE();
}
if (obj->uart == UART_2) {
__USART2_FORCE_RESET();
__USART2_RELEASE_RESET();
__USART2_CLK_DISABLE();
}
if (obj->uart == UART_6) {
__USART6_FORCE_RESET();
__USART6_RELEASE_RESET();
__USART6_CLK_DISABLE();
}
// Configure GPIOs
pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
serial_irq_ids[obj->index] = 0;
}

View File

@ -138,6 +138,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->cpha = SPI_PHASE_1EDGE;
obj->br_presc = SPI_BAUDRATEPRESCALER_256;
obj->pin_miso = miso;
obj->pin_mosi = mosi;
obj->pin_sclk = sclk;
obj->pin_ssel = ssel;
if (ssel == NC) { // SW NSS Master mode
obj->mode = SPI_MODE_MASTER;
obj->nss = SPI_NSS_SOFT;
@ -151,8 +156,30 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
}
void spi_free(spi_t *obj) {
SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
HAL_SPI_DeInit(&SpiHandle);
// Reset SPI and disable clock
if (obj->spi == SPI_1) {
__SPI1_FORCE_RESET();
__SPI1_RELEASE_RESET();
__SPI1_CLK_DISABLE();
}
if (obj->spi == SPI_2) {
__SPI2_FORCE_RESET();
__SPI2_RELEASE_RESET();
__SPI2_CLK_DISABLE();
}
if (obj->spi == SPI_3) {
__SPI3_FORCE_RESET();
__SPI3_RELEASE_RESET();
__SPI3_CLK_DISABLE();
}
// Configure GPIOs
pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
}
void spi_format(spi_t *obj, int bits, int mode, int slave) {