aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/usart.c
diff options
context:
space:
mode:
authoriperry <iperry@749a229e-a60e-11de-b98f-4500b42dc123>2009-12-17 02:37:07 +0000
committeriperry <iperry@749a229e-a60e-11de-b98f-4500b42dc123>2009-12-17 02:37:07 +0000
commit32e57dac2e61e79b029593eb4d34d727bcc10678 (patch)
tree98d7ff41993576bb150d13d5f63dc744f6812852 /src/lib/usart.c
downloadlibrambutan-32e57dac2e61e79b029593eb4d34d727bcc10678.tar.gz
librambutan-32e57dac2e61e79b029593eb4d34d727bcc10678.zip
Initial commit of library code, moved from leaftest repo
git-svn-id: https://leaflabs.googlecode.com/svn/trunk/library@69 749a229e-a60e-11de-b98f-4500b42dc123
Diffstat (limited to 'src/lib/usart.c')
-rw-r--r--src/lib/usart.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lib/usart.c b/src/lib/usart.c
new file mode 100644
index 0000000..c1a7f43
--- /dev/null
+++ b/src/lib/usart.c
@@ -0,0 +1,44 @@
+#include "stm32f10x_rcc.h"
+#include "usart.h"
+
+static inline void usart_putc(usart_port *port, uint8_t ch) {
+ port->DR = ch;
+
+ /* Wait till TXE = 1 */
+ while ((port->SR & USART_TXE) == 0)
+ ;
+}
+
+int32_t usart_init(uint8_t usart_num) {
+ ASSERT((usart_num < NR_USARTS) && (usart_num > 0));
+ usart_port *port;
+ uint32_t clk_speed;
+
+ switch (usart_num) {
+ case 1:
+ port = USART1_BASE;
+ clk_speed = USART1_CLK;
+ break;
+ case 2:
+ port = USART2_BASE;
+ clk_speed = USART2_CLK;
+ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
+ break;
+ case 3:
+ port = USART3_BASE;
+ break;
+ default:
+ /* should never get here */
+ ASSERT(0);
+ }
+ uint32_t baud = 9600;
+ uint32_t usartdiv = clk_speed / baud;
+
+ /* Set baud rate */
+ port->BRR = BIT_MASK_SHIFT(B9600_MANTISSA, 4) | B9600_FRACTION;
+
+ /* Enable the USART and set 8n1 (M bit clear) enable transmitter*/
+ port->CR1 = USART_UE | USART_TE;
+
+ return 0;
+}