ddd5923da5b59d0b99ac3925237c699f01a71c1c
[rpikern] / src / serial.c
1 #include <stdint.h>
2 #include "serial.h"
3 #include "uart.h"
4 #include "gpio.h"
5
6 /* baud rate: BAUDDIV = (UART_CLK / (16 * baud)) */
7 #define UART_CLK        3000000
8
9 void init_serial(int baud)
10 {
11         uint32_t bdiv_fp6;
12
13         REG_CR = 0;             /* disable UART */
14
15         /* disable pullups for GPIO 14 & 15 */
16         gpio_pullups(0xc000, 0, PUD_DISABLE);
17
18         REG_ICR = 0;    /* clear pending interrupts */
19
20         /* calculate baud rate divisor */
21         bdiv_fp6 = (UART_CLK << 6) / (16 * baud);
22         REG_IBRD = (bdiv_fp6 >> 6) & 0xffff;    /* 16 bits integer part */
23         REG_FBRD = bdiv_fp6 & 0x3f;             /* 6 bits fractional precision */
24
25         /* line control: fifo enable, 8n1 */
26         REG_LCRH = LCRH_FIFOEN | LCRH_8BITS;
27         /* mask all interrupts */
28         REG_IMSC = I_CTS | I_RX | I_TX | I_RTIME | I_FRM | I_PAR | I_BRK | I_OVR;
29
30         /* enable UART RX&TX */
31         REG_CR = CR_UARTEN | CR_TXEN | CR_RXEN;
32 }
33
34 void ser_putchar(int c)
35 {
36         while(REG_FR & FR_TXFF);
37         REG_DR = c & 0xff;
38 }
39
40 int ser_getchar(void)
41 {
42         while(REG_FR & FR_RXFE);
43         return REG_DR & 0xff;
44 }
45
46 void ser_printstr(const char *s)
47 {
48         while(*s) {
49                 if(*s == '\n') {
50                         ser_putchar('\r');
51                 }
52                 ser_putchar(*s++);
53         }
54 }