stm32f0: use usart-v2 instead of private usart
Use the usart-common base plus the usart-v2 code, instead of private implementations. Less code, more common apis across targets. Of note is the trick to make F0 look like it has an APB2 bus. It's the only stm32 that doesn't have a documented APB2 bus, but still has peripherals enabled via an "APB2" register, and they match how other targets have an APB2. Simply make APB2 an alias of APB1, as it's only used for clock speed detection.
This commit is contained in:
parent
9d709e52b4
commit
670a7cd83e
@ -389,6 +389,10 @@ Control</b>
|
||||
/* --- Variable definitions ------------------------------------------------ */
|
||||
extern uint32_t rcc_ahb_frequency;
|
||||
extern uint32_t rcc_apb1_frequency;
|
||||
/** F0 doens't _realllly_ have apb2, but it has a bunch of things
|
||||
* enabled via the "APB2" enable register. Fake it out.
|
||||
*/
|
||||
#define rcc_apb2_frequency rcc_apb1_frequency
|
||||
|
||||
enum rcc_osc {
|
||||
RCC_HSI14, RCC_HSI, RCC_HSE, RCC_PLL, RCC_LSI, RCC_LSE, RCC_HSI48
|
||||
|
@ -31,6 +31,7 @@
|
||||
#ifndef LIBOPENCM3_USART_H
|
||||
#define LIBOPENCM3_USART_H
|
||||
|
||||
#include <libopencm3/stm32/common/usart_common_all.h>
|
||||
#include <libopencm3/stm32/common/usart_common_v2.h>
|
||||
|
||||
/*****************************************************************************/
|
||||
@ -67,12 +68,6 @@
|
||||
#define USART_CR2_ABRMOD_STARTBIT (0 << USART_CR2_ABRMOD_SHIFT)
|
||||
#define USART_CR2_ABRMOD_FALLTOFALL (1 << USART_CR2_ABRMOD_SHIFT)
|
||||
|
||||
#define USART_CR2_STOP_SHIFT 12
|
||||
#define USART_CR2_STOP (3 << USART_CR2_STOP_SHIFT)
|
||||
#define USART_CR2_STOP_1_0BIT (0 << USART_CR2_STOP_SHIFT)
|
||||
#define USART_CR2_STOP_2_0BIT (2 << USART_CR2_STOP_SHIFT)
|
||||
#define USART_CR2_STOP_1_5BIT (3 << USART_CR2_STOP_SHIFT)
|
||||
|
||||
/* USART_CR3 Values ---------------------------------------------------------*/
|
||||
|
||||
#define USART_CR3_SCARCNT_SHIFT 17
|
||||
@ -90,59 +85,12 @@
|
||||
#define USART_GTPR_PSC (0xFF << USART_GTPR_PSC_SHIFT)
|
||||
#define USART_GTPR_PSC_VAL(x) ((x) << USART_GTPR_PSC_SHIFT)
|
||||
|
||||
/*****************************************************************************/
|
||||
/* API definitions */
|
||||
/*****************************************************************************/
|
||||
|
||||
#define USART_PARITY (USART_CR1_PCE | USART_CR1_PS)
|
||||
#define USART_PARITY_NONE (0)
|
||||
#define USART_PARITY_EVEN (USART_CR1_PCE)
|
||||
#define USART_PARITY_ODD (USART_CR1_PCE | USART_CR1_PS)
|
||||
|
||||
#define USART_MODE (USART_CR1_TE | USART_CR1_RE)
|
||||
#define USART_MODE_NONE (0)
|
||||
#define USART_MODE_RX (USART_CR1_RE)
|
||||
#define USART_MODE_TX (USART_CR1_TE)
|
||||
#define USART_MODE_TX_RX (USART_CR1_TE | USART_CR1_RE)
|
||||
|
||||
#define USART_FLOWCONTROL (USART_CR3_RTSE | USART_CR3_CTSE)
|
||||
#define USART_FLOWCONTROL_NONE (0)
|
||||
#define USART_FLOWCONTROL_RTS (USART_CR3_RTSE)
|
||||
#define USART_FLOWCONTROL_CTS (USART_CR3_CTSE)
|
||||
#define USART_FLOWCONTROL_RTS_CTS (USART_CR3_RTSE | USART_CR3_CTSE)
|
||||
|
||||
/*****************************************************************************/
|
||||
/* API Functions */
|
||||
/*****************************************************************************/
|
||||
|
||||
BEGIN_DECLS
|
||||
|
||||
void usart_set_baudrate(uint32_t usart, uint32_t baud);
|
||||
void usart_set_databits(uint32_t usart, uint32_t bits);
|
||||
void usart_set_stopbits(uint32_t usart, uint32_t stopbits);
|
||||
void usart_set_parity(uint32_t usart, uint32_t parity);
|
||||
void usart_set_mode(uint32_t usart, uint32_t mode);
|
||||
void usart_set_flow_control(uint32_t usart, uint32_t flowcontrol);
|
||||
void usart_enable(uint32_t usart);
|
||||
void usart_disable(uint32_t usart);
|
||||
void usart_send(uint32_t usart, uint16_t data);
|
||||
uint16_t usart_recv(uint32_t usart);
|
||||
void usart_wait_send_ready(uint32_t usart);
|
||||
void usart_wait_recv_ready(uint32_t usart);
|
||||
void usart_send_blocking(uint32_t usart, uint16_t data);
|
||||
uint16_t usart_recv_blocking(uint32_t usart);
|
||||
void usart_enable_rx_dma(uint32_t usart);
|
||||
void usart_disable_rx_dma(uint32_t usart);
|
||||
void usart_enable_tx_dma(uint32_t usart);
|
||||
void usart_disable_tx_dma(uint32_t usart);
|
||||
void usart_enable_rx_interrupt(uint32_t usart);
|
||||
void usart_disable_rx_interrupt(uint32_t usart);
|
||||
void usart_enable_tx_interrupt(uint32_t usart);
|
||||
void usart_disable_tx_interrupt(uint32_t usart);
|
||||
void usart_enable_error_interrupt(uint32_t usart);
|
||||
void usart_disable_error_interrupt(uint32_t usart);
|
||||
bool usart_get_flag(uint32_t usart, uint32_t flag);
|
||||
|
||||
END_DECLS
|
||||
|
||||
#endif
|
||||
|
@ -46,7 +46,7 @@ OBJS += gpio_common_all.o gpio_common_f0234.o crc_common_all.o \
|
||||
timer_common_all.o timer_common_f0234.o rcc_common_all.o
|
||||
OBJS += adc_common_v2.o
|
||||
OBJS += crs_common_all.o
|
||||
OBJS += usart_common_v2.o
|
||||
OBJS += usart_common_all.o usart_common_v2.o
|
||||
OBJS += i2c_common_v2.o
|
||||
|
||||
OBJS += usb.o usb_control.o usb_standard.o
|
||||
|
@ -31,214 +31,6 @@
|
||||
#include <libopencm3/stm32/usart.h>
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Set Baudrate.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param baud Baud rate specified in Hz.
|
||||
*/
|
||||
|
||||
void usart_set_baudrate(uint32_t usart, uint32_t baud)
|
||||
{
|
||||
uint32_t clock = rcc_apb1_frequency;
|
||||
|
||||
if (usart == USART1) {
|
||||
clock = rcc_apb1_frequency;
|
||||
/* TODO selective PCLK, SYSCLK, HSI or LSE */
|
||||
}
|
||||
|
||||
/* TODO check oversampling 16 */
|
||||
USART_BRR(usart) = ((2 * clock) + baud) / (2 * baud);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Set Word Length.
|
||||
*
|
||||
* The word length is set to 8 or 9 bits. Note that the last bit will be a
|
||||
* parity bit if parity is enabled, in which case the data length will be 7
|
||||
* or 8 bits respectively.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param bits Word length in bits 8 or 9.
|
||||
*/
|
||||
|
||||
void usart_set_databits(uint32_t usart, uint32_t bits)
|
||||
{
|
||||
if (bits == 8) {
|
||||
USART_CR1(usart) &= ~USART_CR1_M; /* 8 data bits */
|
||||
} else {
|
||||
USART_CR1(usart) |= USART_CR1_M; /* 9 data bits */
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Set Stop Bit(s).
|
||||
*
|
||||
* The stop bits are specified as 0.5, 1, 1.5 or 2.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param stopbits Stop bits @ref usart_cr2_stopbits.
|
||||
*/
|
||||
|
||||
void usart_set_stopbits(uint32_t usart, uint32_t stopbits)
|
||||
{
|
||||
USART_CR2(usart) = (USART_CR2(usart) & ~USART_CR2_STOP) | stopbits;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Set Parity.
|
||||
*
|
||||
* The parity bit can be selected as none, even or odd.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param parity Parity @ref usart_cr1_parity.
|
||||
*/
|
||||
|
||||
void usart_set_parity(uint32_t usart, uint32_t parity)
|
||||
{
|
||||
USART_CR1(usart) = (USART_CR1(usart) & ~USART_PARITY) | parity;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Set Rx/Tx Mode.
|
||||
*
|
||||
* The mode can be selected as Rx only, Tx only or Rx+Tx.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param mode Mode @ref usart_cr1_mode.
|
||||
*/
|
||||
|
||||
void usart_set_mode(uint32_t usart, uint32_t mode)
|
||||
{
|
||||
USART_CR1(usart) = (USART_CR1(usart) & ~USART_MODE) | mode;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Set Hardware Flow Control.
|
||||
*
|
||||
* The flow control bit can be selected as none, RTS, CTS or RTS+CTS.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param flowcontrol Flowcontrol @ref usart_cr3_flowcontrol.
|
||||
*/
|
||||
|
||||
void usart_set_flow_control(uint32_t usart, uint32_t flowctrl)
|
||||
{
|
||||
USART_CR3(usart) = (USART_CR3(usart) & ~USART_FLOWCONTROL) | flowctrl;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Enable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_enable(uint32_t usart)
|
||||
{
|
||||
USART_CR1(usart) |= USART_CR1_UE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Disable.
|
||||
*
|
||||
* At the end of the current frame, the USART is disabled to reduce power.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_disable(uint32_t usart)
|
||||
{
|
||||
USART_CR1(usart) &= ~USART_CR1_UE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Send a Data Word.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param data
|
||||
*/
|
||||
|
||||
void usart_send(uint32_t usart, uint16_t data)
|
||||
{
|
||||
USART_TDR(usart) = data;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @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 usart USART block register address base @ref usart_reg_base
|
||||
* @returns data word.
|
||||
*/
|
||||
|
||||
uint16_t usart_recv(uint32_t usart)
|
||||
{
|
||||
/* Receive data. */
|
||||
return USART_RDR(usart);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @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 usart 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 usart 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 Send Data Word with Blocking
|
||||
*
|
||||
* Blocks until the transmit data buffer becomes empty then writes the next
|
||||
* data word for transmission.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param data word to send
|
||||
*/
|
||||
|
||||
void usart_send_blocking(uint32_t usart, uint16_t data)
|
||||
{
|
||||
usart_wait_send_ready(usart);
|
||||
usart_send(usart, data);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Read a Received Data Word with Blocking.
|
||||
*
|
||||
* Wait until a data word has been received then return the word.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @returns data word.
|
||||
*/
|
||||
|
||||
uint16_t usart_recv_blocking(uint32_t usart)
|
||||
{
|
||||
usart_wait_recv_ready(usart);
|
||||
|
||||
return usart_recv(usart);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Receiver DMA Enable.
|
||||
@ -292,84 +84,6 @@ void usart_disable_tx_dma(uint32_t usart)
|
||||
USART_CR3(usart) &= ~USART_CR3_DMAT;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Receiver Interrupt Enable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
void usart_enable_rx_interrupt(uint32_t usart)
|
||||
{
|
||||
USART_CR1(usart) |= USART_CR1_RXNEIE;
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Receiver Interrupt Disable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_disable_rx_interrupt(uint32_t usart)
|
||||
{
|
||||
USART_CR1(usart) &= ~USART_CR1_RXNEIE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Transmitter Interrupt Enable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_enable_tx_interrupt(uint32_t usart)
|
||||
{
|
||||
USART_CR1(usart) |= USART_CR1_TXEIE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Transmitter Interrupt Disable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_disable_tx_interrupt(uint32_t usart)
|
||||
{
|
||||
USART_CR1(usart) &= ~USART_CR1_TXEIE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Error Interrupt Enable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_enable_error_interrupt(uint32_t usart)
|
||||
{
|
||||
USART_CR3(usart) |= USART_CR3_EIE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Error Interrupt Disable.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
*/
|
||||
|
||||
void usart_disable_error_interrupt(uint32_t usart)
|
||||
{
|
||||
USART_CR3(usart) &= ~USART_CR3_EIE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief USART Read a Status Flag.
|
||||
*
|
||||
* @param usart USART block register address base @ref usart_reg_base
|
||||
* @param flag 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);
|
||||
}
|
||||
|
||||
/**@}*/
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user