init vector implementation
[cortex-from-scratch] / uart.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4 #include <stm32.h>
5 #include <mmap.h>
6
7 void uart_init() {
8
9 /* To use the UARTs, the peripheral clock must be enabled by setting the UART0, UART1, or UART2
10 bits in the RCGC1 register. (section 12.4: Initialization and Configuration */
11
12 *SYSCTRL_RCGC1 = *SYSCTRL_RCGC1 | 0x0003;
13
14 /* 
15  * Configure UART0:
16         1. Disable the UART by clearing the UARTEN bit in the UARTCTL register.
17         2. Write the integer portion of the BRD to the UARTIBRD register.
18         3. Write the fractional portion of the BRD to the UARTFBRD register.
19         4. Write the desired serial parameters to the UARTLCRH register
20         5. Enable the UART by setting the UARTEN bit in the UARTCTL register.
21 */
22
23 /* TODO: bitrate: How fast is CPU running?*/
24
25 *UART0_CTRL = 0;
26 *UART0_IBRD = 27; 
27 *UART0_FBRD = 9;
28 *UART0_LCRH = 0x60; 
29 *UART0_CTRL = 0x301;
30
31 *UART1_CTRL = 0;
32 *UART1_IBRD = 27; 
33 *UART1_FBRD = 9;
34 *UART1_LCRH = 0x60; 
35 *UART1_CTRL = 0x301;
36
37
38 }
39
40 extern void uart_putc(unsigned char ch) {
41         
42         if (ch == '\n') {
43                 while ((*UART0_FLAG & 0x8)); // busy bit
44                 *UART0_DATA = 0x0D; // return line
45         }
46         
47         while ((*UART0_FLAG & 0x8)); // busy bit
48         *UART0_DATA = ch;
49 }
50
51 extern void uart_puts(unsigned char *str)
52 {
53
54     int i;
55
56     for (i = 0; i < strlen(str); i++)
57     {
58         uart_putc(str[i]);
59     }
60 }
61
62
63 char * uart_read() {
64
65         return NULL;
66 }
67
68