aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@leaflabs.com>2011-04-25 18:00:34 -0400
committerMarti Bolivar <mbolivar@leaflabs.com>2011-04-25 18:00:34 -0400
commitfd26797490c9c78cce4d4a1894f9413f3c858f2d (patch)
treefcdc3d216b7e857d3d3be29c2b270de6953af8c0
parentf82577c56f8525fc56aa6a0b71ec62435a4c13ba (diff)
downloadlibrambutan-fd26797490c9c78cce4d4a1894f9413f3c858f2d.tar.gz
librambutan-fd26797490c9c78cce4d4a1894f9413f3c858f2d.zip
Adding nonblocking USART transmit, usart_tx().
Other USART transmission functions are still blocking, but are now implemented in terms of usart_tx().
-rw-r--r--libmaple/usart.c29
-rw-r--r--libmaple/usart.h36
2 files changed, 42 insertions, 23 deletions
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 <mbolivar@leaflabs.com,
+ * Perry Hung <perry@leaflabs.com>
* @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
diff --git a/libmaple/usart.h b/libmaple/usart.h
index 02f23da..b2bb034 100644
--- a/libmaple/usart.h
+++ b/libmaple/usart.h
@@ -26,6 +26,8 @@
/**
* @file usart.h
+ * @author Marti Bolivar <mbolivar@leaflabs.com>,
+ * Perry Hung <perry@leaflabs.com>
* @brief USART definitions and prototypes
*/
@@ -251,20 +253,12 @@ extern usart_dev *UART4;
extern usart_dev *UART5;
#endif
-#ifdef STM32_MEDIUM_DENSITY
-#define NR_USARTS 3
-#elif defined(STM32_HIGH_DENSITY)
-#define NR_USARTS 5
-#else
-#warn "Only medium and high density devices are currently supported"
-#endif
-
void usart_init(usart_dev *dev);
void usart_set_baud_rate(usart_dev *dev, uint32 clock_speed, uint32 baud);
void usart_enable(usart_dev *dev);
void usart_disable(usart_dev *dev);
void usart_foreach(void (*fn)(usart_dev *dev));
-void usart_putstr(usart_dev *dev, const char*);
+uint32 usart_tx(usart_dev *dev, const uint8 *buf, uint32 len);
void usart_putudec(usart_dev *dev, uint32 val);
/**
@@ -276,16 +270,32 @@ static inline void usart_disable_all(void) {
/**
* @brief Transmit one character on a serial port.
+ *
+ * This function blocks until the character has been successfully
+ * transmitted.
+ *
* @param dev Serial port to send on.
* @param byte Byte to transmit.
*/
static inline void usart_putc(usart_dev* dev, uint8 byte) {
- usart_reg_map *regs = dev->regs;
-
- while ((regs->SR & USART_SR_TXE) == 0)
+ uint8 buf[] = {byte};
+ while (!usart_tx(dev, buf, 1))
;
+}
- regs->DR = byte;
+/**
+ * @brief Transmit a character string on a serial port.
+ *
+ * This function blocks until str is completely transmitted.
+ *
+ * @param dev Serial port to send on
+ * @param str String to send
+ */
+static inline void usart_putstr(usart_dev *dev, const char* str) {
+ uint32 i = 0;
+ while (str[i] != '\0') {
+ usart_putc(dev, str[i]);
+ }
}
/**