diff --git a/lib/stm32/common/usart_common_all.c b/lib/stm32/common/usart_common_all.c index 354909de..43fd1996 100644 --- a/lib/stm32/common/usart_common_all.c +++ b/lib/stm32/common/usart_common_all.c @@ -61,18 +61,6 @@ void usart_set_baudrate(uint32_t usart, uint32_t baud) } #endif -#if defined STM32F3 - if (usart == USART1) { - /* usart1 can be clocked from appart from pclk1 also from sysclk, - hsi and lse. Please improve! */ - clock = rcc_ppre2_frequency; - } - else { - /*There are from usart1 to usart5 interfaces in the stm32f3*/ - clock = rcc_ppre1_frequency; - } -#endif - /* * Yes it is as simple as that. The reference manual is * talking about fractional calculation but it seems to be only @@ -208,84 +196,6 @@ void usart_disable(uint32_t usart) USART_CR1(usart) &= ~USART_CR1_UE; } -/*---------------------------------------------------------------------------*/ -/** @brief USART Send a Data Word. - -@param[in] usart unsigned 32 bit. USART block register address base @ref -usart_reg_base -@param[in] data unsigned 16 bit. -*/ - -void usart_send(uint32_t usart, uint16_t data) -{ - /* Send data. */ -#if !defined(STM32F3) - USART_DR(usart) = (data & USART_DR_MASK); -#else - USART_TDR(usart) = (data & USART_TDR_MASK); -#endif -} - -/*---------------------------------------------------------------------------*/ -/** @brief USART Read a Received Data Word. - -If parity is enabled the MSB (bit 7 or 8 depending on the word length) is the -parity bit. - -@param[in] usart unsigned 32 bit. USART block register address base @ref -usart_reg_base -@returns unsigned 16 bit data word. -*/ - -uint16_t usart_recv(uint32_t usart) -{ - /* Receive data. */ -#if !defined(STM32F3) - return USART_DR(usart) & USART_DR_MASK; -#else - return USART_RDR(usart) & USART_RDR_MASK; -#endif -} - -/*---------------------------------------------------------------------------*/ -/** @brief USART Wait for Transmit Data Buffer Empty - -Blocks until the transmit data buffer becomes empty and is ready to accept the -next data word. - -@param[in] usart unsigned 32 bit. USART block register address base @ref -usart_reg_base -*/ - -void usart_wait_send_ready(uint32_t usart) -{ - /* Wait until the data has been transferred into the shift register. */ -#if !defined(STM32F3) - while ((USART_SR(usart) & USART_SR_TXE) == 0); -#else - while ((USART_ISR(usart) & USART_ISR_TXE) == 0); -#endif -} - -/*---------------------------------------------------------------------------*/ -/** @brief USART Wait for Received Data Available - -Blocks until the receive data buffer holds a valid received data word. - -@param[in] usart unsigned 32 bit. USART block register address base @ref -usart_reg_base -*/ - -void usart_wait_recv_ready(uint32_t usart) -{ - /* Wait until the data is ready to be received. */ -#if !defined(STM32F3) - while ((USART_SR(usart) & USART_SR_RXNE) == 0); -#else - while ((USART_ISR(usart) & USART_ISR_RXNE) == 0); -#endif -} - /*---------------------------------------------------------------------------*/ /** @brief USART Send Data Word with Blocking @@ -453,64 +363,5 @@ void usart_disable_error_interrupt(uint32_t usart) USART_CR3(usart) &= ~USART_CR3_EIE; } -/*---------------------------------------------------------------------------*/ -/** @brief USART Read a Status Flag. - -@param[in] usart unsigned 32 bit. USART block register address base @ref -usart_reg_base -@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags. -@returns boolean: flag set. -*/ - -bool usart_get_flag(uint32_t usart, uint32_t flag) -{ -#if !defined(STM32F3) - return ((USART_SR(usart) & flag) != 0); -#else - return ((USART_ISR(usart) & flag) != 0); -#endif -} - -/*---------------------------------------------------------------------------*/ -/** @brief USART Return Interrupt Source. - -Returns true if the specified interrupt flag (IDLE, RXNE, TC, TXE or OE) was -set and the interrupt was enabled. If the specified flag is not an interrupt -flag, the function returns false. - -@todo These are the most important interrupts likely to be used. Others -relating to LIN break, and error conditions in multibuffer communication, need -to be added for completeness. - -@param[in] usart unsigned 32 bit. USART block register address base @ref -usart_reg_base -@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags. -@returns boolean: flag and interrupt enable both set. -*/ - -bool usart_get_interrupt_source(uint32_t usart, uint32_t flag) -{ -#if !defined(STM32F3) - uint32_t flag_set = (USART_SR(usart) & flag); - /* IDLE, RXNE, TC, TXE interrupts */ - if ((flag >= USART_SR_IDLE) && (flag <= USART_SR_TXE)) { -#else - uint32_t flag_set = (USART_ISR(usart) & flag); - /* IDLE, RXNE, TC, TXE interrupts */ - if ((flag >= USART_ISR_IDLE) && (flag <= USART_ISR_TXE)) { -#endif - return ((flag_set & USART_CR1(usart)) != 0); - /* Overrun error */ -#if !defined(STM32F3) - } else if (flag == USART_SR_ORE) { -#else - } else if (flag == USART_ISR_ORE) { -#endif - return flag_set && (USART_CR3(usart) & USART_CR3_CTSIE); - } - - return false; -} - /**@}*/ diff --git a/lib/stm32/common/usart_common_f124.c b/lib/stm32/common/usart_common_f124.c new file mode 100644 index 00000000..ea5b4eeb --- /dev/null +++ b/lib/stm32/common/usart_common_f124.c @@ -0,0 +1,143 @@ +/** @addtogroup usart_file + +@author @htmlonly © @endhtmlonly 2009 Uwe Hermann + +This library supports the USART/UART in the STM32F series +of ARM Cortex Microcontrollers by ST Microelectronics. + +Devices can have up to 3 USARTs and 2 UARTs. + +*/ + +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/**@{*/ + +#include +#include + +/*---------------------------------------------------------------------------*/ +/** @brief USART Send a Data Word. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@param[in] data unsigned 16 bit. +*/ + +void usart_send(uint32_t usart, uint16_t data) +{ + /* Send data. */ + USART_DR(usart) = (data & USART_DR_MASK); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Read a Received Data Word. + +If parity is enabled the MSB (bit 7 or 8 depending on the word length) is the +parity bit. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@returns unsigned 16 bit data word. +*/ + +uint16_t usart_recv(uint32_t usart) +{ + /* Receive data. */ + return USART_DR(usart) & USART_DR_MASK; +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Wait for Transmit Data Buffer Empty + +Blocks until the transmit data buffer becomes empty and is ready to accept the +next data word. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +*/ + +void usart_wait_send_ready(uint32_t usart) +{ + /* Wait until the data has been transferred into the shift register. */ + while ((USART_SR(usart) & USART_SR_TXE) == 0); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Wait for Received Data Available + +Blocks until the receive data buffer holds a valid received data word. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +*/ + +void usart_wait_recv_ready(uint32_t usart) +{ + /* Wait until the data is ready to be received. */ + while ((USART_SR(usart) & USART_SR_RXNE) == 0); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Read a Status Flag. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags. +@returns boolean: flag set. +*/ + +bool usart_get_flag(uint32_t usart, uint32_t flag) +{ + return ((USART_SR(usart) & flag) != 0); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Return Interrupt Source. + +Returns true if the specified interrupt flag (IDLE, RXNE, TC, TXE or OE) was +set and the interrupt was enabled. If the specified flag is not an interrupt +flag, the function returns false. + +@todo These are the most important interrupts likely to be used. Others +relating to LIN break, and error conditions in multibuffer communication, need +to be added for completeness. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags. +@returns boolean: flag and interrupt enable both set. +*/ + +bool usart_get_interrupt_source(uint32_t usart, uint32_t flag) +{ + uint32_t flag_set = (USART_SR(usart) & flag); + /* IDLE, RXNE, TC, TXE interrupts */ + if ((flag >= USART_SR_IDLE) && (flag <= USART_SR_TXE)) { + return ((flag_set & USART_CR1(usart)) != 0); + /* Overrun error */ + } else if (flag == USART_SR_ORE) { + return flag_set && (USART_CR3(usart) & USART_CR3_CTSIE); + } + + return false; +} + +/**@}*/ diff --git a/lib/stm32/f1/Makefile b/lib/stm32/f1/Makefile index 03bd5bea..ce6b62df 100644 --- a/lib/stm32/f1/Makefile +++ b/lib/stm32/f1/Makefile @@ -39,7 +39,7 @@ OBJS = adc.o can.o desig.o exti.o ethernet.o flash.o gpio.o \ OBJS += crc_common_all.o dac_common_all.o dma_common_f13.o \ gpio_common_all.o i2c_common_all.o iwdg_common_all.o \ pwr_common_all.o spi_common_all.o spi_common_f124.o \ - timer_common_all.o usart_common_all.o + timer_common_all.o usart_common_all.o usart_common_f124.o OBJS += usb.o usb_control.o usb_standard.o usb_f103.o usb_f107.o \ usb_fx07_common.o diff --git a/lib/stm32/f2/Makefile b/lib/stm32/f2/Makefile index dfe9eb06..ee945601 100644 --- a/lib/stm32/f2/Makefile +++ b/lib/stm32/f2/Makefile @@ -39,8 +39,8 @@ OBJS += crc_common_all.o dac_common_all.o dma_common_f24.o \ gpio_common_all.o gpio_common_f234.o i2c_common_all.o \ iwdg_common_all.o rtc_common_bcd.o spi_common_all.o \ spi_common_f124.o timer_common_all.o timer_common_f24.o \ - usart_common_all.o flash_common_f234.o flash_common_f24.o \ - hash_common_f24.o crypto_common_f24.o + usart_common_all.o usart_common_f124.o flash_common_f234.o \ + flash_common_f24.o hash_common_f24.o crypto_common_f24.o VPATH += ../../usb:../:../../cm3:../common diff --git a/lib/stm32/f3/Makefile b/lib/stm32/f3/Makefile index 72117820..6775a4f1 100644 --- a/lib/stm32/f3/Makefile +++ b/lib/stm32/f3/Makefile @@ -34,7 +34,7 @@ CFLAGS = -Os -g \ # ARFLAGS = rcsv ARFLAGS = rcs -OBJS = rcc.o gpio.o adc.o exti2.o i2c.o spi.o +OBJS = rcc.o gpio.o adc.o exti2.o i2c.o spi.o usart.o OBJS += gpio_common_all.o gpio_common_f234.o \ dac_common_all.o usart_common_all.o crc_common_all.o\ diff --git a/lib/stm32/f3/usart.c b/lib/stm32/f3/usart.c index 3f151008..5145cc4c 100644 --- a/lib/stm32/f3/usart.c +++ b/lib/stm32/f3/usart.c @@ -31,3 +31,111 @@ LGPL License Terms @ref lgpl_license #include #include +/*---------------------------------------------------------------------------*/ +/** @brief USART Send a Data Word. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@param[in] data unsigned 16 bit. +*/ + +void usart_send(uint32_t usart, uint16_t data) +{ + /* Send data. */ + USART_TDR(usart) = (data & USART_TDR_MASK); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Read a Received Data Word. + +If parity is enabled the MSB (bit 7 or 8 depending on the word length) is the +parity bit. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@returns unsigned 16 bit data word. +*/ + +uint16_t usart_recv(uint32_t usart) +{ + /* Receive data. */ + return USART_RDR(usart) & USART_RDR_MASK; +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Wait for Transmit Data Buffer Empty + +Blocks until the transmit data buffer becomes empty and is ready to accept the +next data word. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +*/ + +void usart_wait_send_ready(uint32_t usart) +{ + /* Wait until the data has been transferred into the shift register. */ + while ((USART_ISR(usart) & USART_ISR_TXE) == 0); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Wait for Received Data Available + +Blocks until the receive data buffer holds a valid received data word. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +*/ + +void usart_wait_recv_ready(uint32_t usart) +{ + /* Wait until the data is ready to be received. */ + while ((USART_ISR(usart) & USART_ISR_RXNE) == 0); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Read a Status Flag. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags. +@returns boolean: flag set. +*/ + +bool usart_get_flag(uint32_t usart, uint32_t flag) +{ + return ((USART_ISR(usart) & flag) != 0); +} + +/*---------------------------------------------------------------------------*/ +/** @brief USART Return Interrupt Source. + +Returns true if the specified interrupt flag (IDLE, RXNE, TC, TXE or OE) was +set and the interrupt was enabled. If the specified flag is not an interrupt +flag, the function returns false. + +@todo These are the most important interrupts likely to be used. Others +relating to LIN break, and error conditions in multibuffer communication, need +to be added for completeness. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags. +@returns boolean: flag and interrupt enable both set. +*/ + +bool usart_get_interrupt_source(uint32_t usart, uint32_t flag) +{ + uint32_t flag_set = (USART_ISR(usart) & flag); + /* IDLE, RXNE, TC, TXE interrupts */ + if ((flag >= USART_ISR_IDLE) && (flag <= USART_ISR_TXE)) { + return ((flag_set & USART_CR1(usart)) != 0); + /* Overrun error */ + } else if (flag == USART_ISR_ORE) { + return flag_set && (USART_CR3(usart) & USART_CR3_CTSIE); + } + + return false; +} + +/**@}*/ \ No newline at end of file diff --git a/lib/stm32/f4/Makefile b/lib/stm32/f4/Makefile index a3cf22bd..547d7ac4 100644 --- a/lib/stm32/f4/Makefile +++ b/lib/stm32/f4/Makefile @@ -40,8 +40,9 @@ OBJS += crc_common_all.o dac_common_all.o dma_common_f24.o \ gpio_common_all.o gpio_common_f234.o i2c_common_all.o \ iwdg_common_all.o pwr_common_all.o rtc_common_bcd.o \ spi_common_all.o spi_common_f124.o timer_common_all.o \ - timer_common_f24.o usart_common_all.o flash_common_f234.o \ - flash_common_f24.o hash_common_f24.o crypto_common_f24.o + timer_common_f24.o usart_common_all.o usart_common_f124.o \ + flash_common_f234.o flash_common_f24.o hash_common_f24.o \ + crypto_common_f24.o OBJS += usb.o usb_standard.o usb_control.o usb_fx07_common.o \ usb_f107.o usb_f207.o