diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c index 68c032d323..48e9e0bf3e 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c @@ -181,7 +181,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; int count; - + i2c_start(obj); // Send slave address for write @@ -245,7 +245,7 @@ int i2c_byte_write(i2c_t *obj, int data) { I2C_SendData(i2c, (uint8_t)data); // Wait until the byte is transmitted - timeout = FLAG_TIMEOUT; + timeout = FLAG_TIMEOUT; while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) && (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) { timeout--; @@ -299,10 +299,9 @@ int i2c_slave_receive(i2c_t *obj) { uint32_t event; I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); - event = I2C_GetLastEvent( i2c ); - if(event != 0) - { - switch(event){ + event = I2C_GetLastEvent(i2c); + if (event != 0) { + switch (event) { case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: retValue = WriteAddressed; break; @@ -317,24 +316,24 @@ int i2c_slave_receive(i2c_t *obj) { break; } - // clear ADDR - if((retValue == WriteAddressed) || (retValue == ReadAddressed)){ + // clear ADDR + if ((retValue == WriteAddressed) || (retValue == ReadAddressed)) { // read SR to clear ADDR flag i2c->SR1; i2c->SR2; } // clear stopf - if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) { + if (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) { // read SR1 and write CR1 to clear STOP flag i2c->SR1; - I2C_Cmd(i2c, ENABLE); + I2C_Cmd(i2c, ENABLE); } // clear AF - if(I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) { + if (I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) { I2C_ClearFlag(i2c, I2C_FLAG_AF); - } + } } - return(retValue); + return (retValue); } int i2c_slave_read(i2c_t *obj, char *data, int length) { diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/objects.h b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/objects.h index ad2dd17809..3b8a442be8 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/objects.h +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/objects.h @@ -65,6 +65,8 @@ struct serial_s { uint32_t databits; uint32_t stopbits; uint32_t parity; + PinName pin_tx; + PinName pin_rx; }; struct spi_s { @@ -75,6 +77,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 { diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c index e12688196b..9fa74d67d3 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pwmout_api.c @@ -98,8 +98,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) { } void pwmout_free(pwmout_t* obj) { - TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); - TIM_DeInit(tim); + // Configure GPIO + pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); } void pwmout_write(pwmout_t* obj, float value) { diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c index e810885042..ce515316da 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c @@ -113,6 +113,9 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) { obj->stopbits = USART_StopBits_1; obj->parity = USART_Parity_No; + obj->pin_tx = tx; + obj->pin_rx = rx; + init_usart(obj); // The index is used by irq @@ -128,6 +131,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) { + RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE); + } + if (obj->uart == UART_2) { + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE); + } + if (obj->uart == UART_3) { + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, DISABLE); + } + + // Configure GPIOs + pin_function(obj->pin_tx, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); + pin_function(obj->pin_rx, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); + serial_irq_ids[obj->index] = 0; } diff --git a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c index d7f40e9902..466f3c03b2 100644 --- a/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c +++ b/libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c @@ -119,6 +119,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel obj->cpha = SPI_CPHA_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) { // Master obj->mode = SPI_Mode_Master; obj->nss = SPI_NSS_Soft; @@ -132,8 +137,24 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel } void spi_free(spi_t *obj) { - SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); - SPI_I2S_DeInit(spi); + // Reset SPI and disable clock + if (obj->spi == SPI_1) { + RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE); + } + + if (obj->spi == SPI_2) { + RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE); + } + + // Configure GPIOs + pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); + pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); + pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); + pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); } void spi_format(spi_t *obj, int bits, int mode, int slave) {