diff options
Diffstat (limited to 'libmaple/usart.h')
-rw-r--r-- | libmaple/usart.h | 88 |
1 files changed, 77 insertions, 11 deletions
diff --git a/libmaple/usart.h b/libmaple/usart.h index 02fb6e0..beffa89 100644 --- a/libmaple/usart.h +++ b/libmaple/usart.h @@ -23,32 +23,98 @@ * ****************************************************************************/ /** - * @file usart.h - * - * @brief USART Definitions + * @brief USART definitions and prototypes */ #ifndef _USART_H_ #define _USART_H_ -#define NR_USARTS 0x3 +#include "ring_buffer.h" #ifdef __cplusplus extern "C"{ #endif -#define USART_MAX_BAUD 225000 +#define USART_TXE BIT(7) + +/* usart device numbers */ +enum { + USART1, + USART2, + USART3, +}; + +/* peripheral register struct */ +typedef struct usart_port { + volatile uint32 SR; // Status register + volatile uint32 DR; // Data register + volatile uint32 BRR; // Baud rate register + volatile uint32 CR1; // Control register 1 + volatile uint32 CR2; // Control register 2 + volatile uint32 CR3; // Control register 3 + volatile uint32 GTPR; // Guard time and prescaler register +} usart_port; + +/* usart descriptor */ +struct usart_dev { + usart_port *base; + ring_buffer rb; + uint8 rx_buf[64]; + const uint8 rcc_dev_num; + const uint8 nvic_dev_num; +}; + +extern struct usart_dev usart_dev_table[]; + + +/** + * @brief send one character on a usart + * @param usart_num usart to send on + * @param byte byte to send + */ +static inline void usart_putc(uint8 usart_num, uint8 byte) { + usart_port *port = usart_dev_table[usart_num].base; + + port->DR = byte; + + /* Wait for transmission to complete */ + while ((port->SR & USART_TXE) == 0) + ; +} + + +/** + * @brief read one character from a usart + * @param usart_num usart to read from + * @return byte read + */ +static inline uint8 usart_getc(uint8 usart_num) { + return rb_remove(&usart_dev_table[usart_num].rb); +} + + +/** + * @brief return the amount of data available in the rx buffer + * @param usart_num which usart to check + * @return number of bytes in the rx buffer + */ +static inline uint32 usart_data_available(uint8 usart_num) { + return rb_full_count(&usart_dev_table[usart_num].rb); +} + + +/** + * @brief removes the contents of the rx fifo + * @param usart_num which usart to reset + */ +static inline void usart_reset_rx(uint8 usart_num) { + rb_reset(&usart_dev_table[usart_num].rb); +} void usart_init(uint8 usart_num, uint32 baud); void usart_disable(uint8 usart_num); - void usart_putstr(uint8 usart_num, const char*); void usart_putudec(uint8 usart_num, uint32 val); -void usart_putc(uint8 usart_num, uint8 ch); - -uint32 usart_data_available(uint8 usart_num); -uint8 usart_getc(uint8 usart_num); -void usart_clear_buffer(uint8 usart_num); #ifdef __cplusplus } // extern "C" |