1bc13fedee87a5ab0b39f6e5c5f0e3326779951c
[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         /* select alt0 function for GPIO 14 & 15 */
18         /*gpio_fsel(14, FSEL_ALT0);
19         gpio_fsel(15, FSEL_ALT0);*/
20
21         REG_ICR = 0x7ff;        /* clear pending interrupts */
22
23         /* calculate baud rate divisor */
24         bdiv_fp6 = (UART_CLK << 6) / (16 * baud);
25         REG_IBRD = (bdiv_fp6 >> 6) & 0xffff;    /* 16 bits integer part */
26         REG_FBRD = bdiv_fp6 & 0x3f;             /* 6 bits fractional precision */
27
28         /* line control: fifo enable, 8n1 */
29         REG_LCRH = LCRH_FIFOEN | LCRH_8BITS;
30         /* mask all interrupts */
31         REG_IMSC = I_CTS | I_RX | I_TX | I_RTIME | I_FRM | I_PAR | I_BRK | I_OVR;
32
33         /* enable UART RX&TX */
34         REG_CR = CR_UARTEN | CR_TXEN | CR_RXEN;
35 }
36
37 void ser_putchar(int c)
38 {
39         if(c == '\n') ser_putchar('\r');
40
41         while(REG_FR & FR_TXFF);
42         REG_DR = c & 0xff;
43 }
44
45 int ser_getchar(void)
46 {
47         while(REG_FR & FR_RXFE);
48         return REG_DR & 0xff;
49 }
50
51 void ser_printstr(const char *s)
52 {
53         while(*s) {
54                 ser_putchar(*s++);
55         }
56 }