basic implemtation general purpose clock and tinyprintf
[cortex-from-scratch] / drivers / uart.c
1 #include <stdbool.h>
2 #include <stddef.h>
3 #include <stdint.h>
4
5 #include <sys/mmap.h>
6 #include <sys/robsys.h>
7
8 #include <lib/regfunc.h>
9 #include <lib/string.h>
10
11 #include <drivers/uart.h>
12
13 #define UARTBUF 256
14 #define ECHO 1
15
16 static struct {
17          uint8_t buf[UARTBUF];
18          uint32_t rpos;
19          uint32_t wpos;
20 } linefeed;
21
22 void set_baudrate();
23
24 void * uart_handler() {
25
26         while (rchkbit(USART1_SR, 5)) {
27                 char echochar = *USART1_DR;
28                 linefeed.buf[linefeed.wpos++] = echochar;
29                  if (linefeed.wpos == UARTBUF)
30                          linefeed.wpos = 0;
31         }
32                 
33 }
34
35 void uart_init() {
36
37         linefeed.rpos = 0; 
38         linefeed.wpos = 0;
39
40         //memset(&linefeed, 0, (sizeof(struct linefeed) ));
41         //rsetbitsfrom(RCC_APB2ENR, 0, 0x4005);
42         rsetbit(RCC_APB2ENR, 0);
43         rsetbit(RCC_APB2ENR, 2);
44         rsetbit(RCC_APB2ENR, 14);
45         
46         /* (after enable GPIOA), on PA9&PA10 and set mode
47          *  to alternative output */
48         rwrite(GPIOA_CRH, 0x444444D4);
49         // set event control register, output on PA, Pin 9 TODO: check
50         rsetbitsfrom(AFIO_EVCR, 0, 0x89);
51
52         //disable temporarily to set values
53         rclrbit(USART1_CR1, 13);
54
55         set_baudrate();
56
57         //set stop bit, default is 1 stop bit 0x00
58         rwrite(USART1_CR2, 0x0000);
59         
60         /* parity = 8 bit, UART1 enabled,
61          * TX and RX enabled, interrupts enabled */
62         rwrite(USART1_CR1, 0x0000302C);
63
64         ivt_set_gate(53, uart_handler, 0);
65         
66         rsetbit(NVIC_ISER1, 5);
67 }
68
69 void uart_putc(unsigned char ch) {
70         
71         if (ch == '\n') {
72                 while(!rchkbit(USART1_SR, 6));
73                 regw_u8(USART1_DR, 0x0D, 0, OWRITE); // return line
74         }
75                 while(!rchkbit(USART1_SR, 6));
76                 regw_u8(USART1_DR, ch, 0, OWRITE);
77 }
78
79 char uart_getc(void) {
80          char c;
81
82          if (linefeed.rpos != linefeed.wpos) {
83                  c = linefeed.buf[linefeed.rpos++];
84                  if (linefeed.rpos == UARTBUF)
85                          linefeed.rpos = 0;
86                  return c;
87          }
88          return 0;
89  }
90
91 /* Calculate baud rate. Example how this is done
92  * to set this register on a stm32
93  * Desired baudrate: 115200,  CLK: 8 MHz
94  * Desired Baudrate = CLK / (16 * USARTDIV)
95  * USARTDIV = 4.34
96  * FRACTION: 16 x 0.34 = 0d5.44 0d5 -> 0x5
97  * MANTISSA: 0d4.34 0d4 -> 0x4 
98  * USART_BRR = 0x45*/
99         
100 void set_baudrate() {
101
102 //      rwrite(USART1_BRR, 0x000001A1); 48 MHZ
103 //      rwrite(USART1_BRR, 0x0000022B); 64 MHz
104 //      rwrite(USART1_BRR, 0x00000138); 36 MHz
105 //      rwrite(USART1_BRR, 0x00000271); 72 MHz
106 #ifdef ENABLE_HSE
107         rwrite(USART1_BRR, 0x00000138);
108 #else
109         rwrite(USART1_BRR, 0x00000045);
110 #endif
111 }