diff options
author | Marti Bolivar <mbolivar@leaflabs.com> | 2011-09-14 20:38:26 -0400 |
---|---|---|
committer | Marti Bolivar <mbolivar@leaflabs.com> | 2011-10-07 14:38:33 -0400 |
commit | e92073f86f6c6cb5002e235a4bc6f39b6609d177 (patch) | |
tree | 8819b3ae100391d6062b7fe9974eaeb82a5d91c3 | |
parent | 60eb25f9156f3d7b2af07e5b6c0b741c7e43e969 (diff) | |
download | librambutan-e92073f86f6c6cb5002e235a4bc6f39b6609d177.tar.gz librambutan-e92073f86f6c6cb5002e235a4bc6f39b6609d177.zip |
usart: Add usart_rx().
usart_rx() is a nonblocking USART receive. Add it for symmetry with
usart_tx().
-rw-r--r-- | libmaple/usart.c | 16 | ||||
-rw-r--r-- | libmaple/usart.h | 1 |
2 files changed, 17 insertions, 0 deletions
diff --git a/libmaple/usart.c b/libmaple/usart.c index 998da17..0bdc37a 100644 --- a/libmaple/usart.c +++ b/libmaple/usart.c @@ -192,6 +192,22 @@ uint32 usart_tx(usart_dev *dev, const uint8 *buf, uint32 len) { } /** + * @brief Nonblocking USART receive. + * @param dev Serial port to receive bytes from + * @param buf Buffer to store received bytes into + * @param len Maximum number of bytes to store + * @return Number of bytes received + */ +uint32 usart_rx(usart_dev *dev, uint8 *buf, uint32 len) { + uint32 rxed = 0; + while (usart_data_available(dev) && rxed < len) { + *buf++ = usart_getc(dev); + rxed++; + } + return rxed; +} + +/** * @brief Transmit an unsigned integer to the specified serial port in * decimal format. * diff --git a/libmaple/usart.h b/libmaple/usart.h index e747e65..ed00e16 100644 --- a/libmaple/usart.h +++ b/libmaple/usart.h @@ -259,6 +259,7 @@ void usart_enable(usart_dev *dev); void usart_disable(usart_dev *dev); void usart_foreach(void (*fn)(usart_dev *dev)); uint32 usart_tx(usart_dev *dev, const uint8 *buf, uint32 len); +uint32 usart_rx(usart_dev *dev, uint8 *buf, uint32 len); void usart_putudec(usart_dev *dev, uint32 val); /** |