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