From fd26797490c9c78cce4d4a1894f9413f3c858f2d Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 25 Apr 2011 18:00:34 -0400 Subject: Adding nonblocking USART transmit, usart_tx(). Other USART transmission functions are still blocking, but are now implemented in terms of usart_tx(). --- libmaple/usart.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'libmaple/usart.c') diff --git a/libmaple/usart.c b/libmaple/usart.c index 22542fd..fbd4d70 100644 --- a/libmaple/usart.c +++ b/libmaple/usart.c @@ -26,6 +26,8 @@ /** * @file usart.c + * @author Marti Bolivar * @brief USART control routines */ @@ -158,7 +160,7 @@ void usart_disable(usart_dev *dev) { * @brief Call a function on each USART. * @param fn Function to call. */ -void usart_foreach(void (*fn)(usart_dev *dev)) { +void usart_foreach(void (*fn)(usart_dev*)) { fn(USART1); fn(USART2); fn(USART3); @@ -169,20 +171,27 @@ void usart_foreach(void (*fn)(usart_dev *dev)) { } /** - * @brief Print a null-terminated string to the specified serial port. - * @param dev Serial port to send the string on - * @param str String to send + * @brief Nonblocking USART transmit + * @param dev Serial port to transmit over + * @param buf Buffer to transmit + * @param len Maximum number of bytes to transmit + * @return Number of bytes transmitted */ -void usart_putstr(usart_dev *dev, const char* str) { - char ch; - - while ((ch = *(str++)) != '\0') { - usart_putc(dev, ch); +uint32 usart_tx(usart_dev *dev, const uint8 *buf, uint32 len) { + usart_reg_map *regs = dev->regs; + uint32 txed = 0; + while ((regs->SR & USART_SR_TXE) && (txed < len)) { + regs->DR = buf[txed++]; } + return txed; } /** - * @brief Print an unsigned integer to the specified serial port. + * @brief Transmit an unsigned integer to the specified serial port in + * decimal format. + * + * This function blocks until the integer's digits have been + * completely transmitted. * * @param dev Serial port to send on * @param val Number to print -- cgit v1.2.3