From bf0b987fe31b072c8d87420c89127a83a4ba1ea7 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Mon, 27 Dec 2010 15:29:22 +0100 Subject: [PATCH] Splitted out the usart recv and send functions to have blocking and non blocking versions of those. --- examples/obldc/usart/usart.c | 6 +++--- examples/stm32-h103/usart/usart.c | 6 +++--- include/libopenstm32/usart.h | 4 ++++ lib/usart.c | 32 +++++++++++++++++++++++++------ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/examples/obldc/usart/usart.c b/examples/obldc/usart/usart.c index ac0becb0..87121815 100644 --- a/examples/obldc/usart/usart.c +++ b/examples/obldc/usart/usart.c @@ -78,11 +78,11 @@ int main(void) /* Blink the LED (PC12) on the board with every transmitted byte. */ while (1) { gpio_toggle(GPIOA, GPIO6); /* LED on/off */ - usart_send(USART1, c + '0'); /* Send one byte on USART3. */ + usart_send_blocking(USART1, c + '0'); /* Send one byte on USART3. */ c = (c == 9) ? 0 : c + 1; /* Increment c. */ if ((j++ % 80) == 0) { /* Newline after line full. */ - usart_send(USART1, '\r'); - usart_send(USART1, '\n'); + usart_send_blocking(USART1, '\r'); + usart_send_blocking(USART1, '\n'); } for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ } diff --git a/examples/stm32-h103/usart/usart.c b/examples/stm32-h103/usart/usart.c index d8ffe3d3..44703483 100644 --- a/examples/stm32-h103/usart/usart.c +++ b/examples/stm32-h103/usart/usart.c @@ -69,11 +69,11 @@ int main(void) /* Blink the LED (PC12) on the board with every transmitted byte. */ while (1) { gpio_toggle(GPIOC, GPIO12); /* LED on/off */ - usart_send(USART3, c + '0'); /* Send one byte on USART3. */ + usart_send_blocking(USART3, c + '0'); /* Send one byte on USART3. */ c = (c == 9) ? 0 : c + 1; /* Increment c. */ if ((j++ % 80) == 0) { /* Newline after line full. */ - usart_send(USART3, '\r'); - usart_send(USART3, '\n'); + usart_send_blocking(USART3, '\r'); + usart_send_blocking(USART3, '\n'); } for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ } diff --git a/include/libopenstm32/usart.h b/include/libopenstm32/usart.h index 0cf86d18..7271ba75 100644 --- a/include/libopenstm32/usart.h +++ b/include/libopenstm32/usart.h @@ -292,5 +292,9 @@ void usart_enable(u32 usart); void usart_disable(u32 usart); void usart_send(u32 usart, u16 data); u16 usart_recv(u32 usart); +void usart_wait_send_ready(u32 usart); +void usart_wait_recv_ready(u32 usart); +void usart_send_blocking(u32 usart, u16 data); +u16 usart_recv_blocking(u32 usart); #endif diff --git a/lib/usart.c b/lib/usart.c index 57b3ed55..09feb6ef 100644 --- a/lib/usart.c +++ b/lib/usart.c @@ -85,16 +85,36 @@ void usart_send(u32 usart, u16 data) { /* Send data. */ USART_DR(usart) = (data & 0x1ff); - - /* Wait until the data has been transferred into the shift register. */ - while ((USART_SR(usart) & USART_SR_TXE) == 0); } u16 usart_recv(u32 usart) { - /* Wait until the data is ready to be received. */ - while ((USART_SR(usart) & USART_SR_RXNE) == 0); - /* Receive data. */ return USART_DR(usart) & 0x1ff; } + +void usart_wait_send_ready(u32 usart) +{ + /* Wait until the data has been transferred into the shift register. */ + while ((USART_SR(usart) & USART_SR_TXE) == 0); +} + +void usart_wait_recv_ready(u32 usart) +{ + /* Wait until the data is ready to be received. */ + while ((USART_SR(usart) & USART_SR_RXNE) == 0); +} + +void usart_send_blocking(u32 usart, u16 data) +{ + usart_send(usart, data); + + usart_wait_send_ready(usart); +} + +u16 usart_recv_blocking(u32 usart) +{ + usart_wait_recv_ready(usart); + + return usart_recv(usart); +}