basic uart functionality (not tested)
[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         // enable clock to UART1
10         *RCC_APB2ENR = 0x4005;
11         *GPIOA_CRH = 0x444444D4;
12         *AFIO_EVCR = 0x89;
13         
14         *USART1_CR1 = (0 << 13); // disable control register
15         *USART1_CR3 = 0xC0;
16
17         /* baud rate 9600 
18          * 115200 = 8MHz / (16 * USARTDIV)
19          * USARTDIV = 4.34
20          * FRACTION: 16 x 0.34 = 0d5.44 0d5 -> 0x5
21          * MANTISSA: 0d4.34 0d4 -> 0x4 
22          * USART_BRR = 0x45*/
23
24         *USART1_BRR = 0x00000045;
25
26         /* parity = 8 bit, UART1 enabled, TX and RX enabled, interrupts enabled */
27         *USART1_CR1 = (volatile uint32_t) 0x000030AC; 
28
29 /* 
30  * Configure UART0:
31         1. Disable the UART by clearing the UARTEN bit in the UARTCTL register.
32         2. Write the integer portion of the BRD to the UARTIBRD register.
33         3. Write the fractional portion of the BRD to the UARTFBRD register.
34         4. Write the desired serial parameters to the UARTLCRH register
35         5. Enable the UART by setting the UARTEN bit in the UARTCTL register.
36 */
37
38 /* TODO: bitrate: How fast is CPU running?*/
39
40 /* *UART0_CTRL = 0;
41 *UART0_IBRD = 27; 
42 *UART0_FBRD = 9;
43 *UART0_LCRH = 0x60; 
44 *UART0_CTRL = 0x301;
45
46 *UART1_CTRL = 0;
47 *UART1_IBRD = 27; 
48 *UART1_FBRD = 9;
49 *UART1_LCRH = 0x60; 
50 *UART1_CTRL = 0x301;
51 */
52
53 }
54
55 extern void uart_putc(unsigned char ch) {
56         
57 //      if (ch == '\n') {
58 //              while ((*UART1_DR & 0x8)); // busy bit
59 //              *USART1_DR = 0x0D; // return line
60 //      }
61 //      
62 //      while ((*UART0_FLAG & 0x8)); // busy bit
63         *USART1_DR = ch;
64 }
65
66 extern void uart_puts(unsigned char *str)
67 {
68
69     int i;
70
71     for (i = 0; i < strlen(str); i++)
72     {
73         uart_putc(str[i]);
74     }
75 }
76
77
78 char * uart_read() {
79
80         return NULL;
81 }
82
83