testing minimal program
[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 = 1;//(bdiv_fp6 >> 6) & 0xffff;        /* 16 bits integer part */
26         REG_FBRD = 40;//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         while(REG_FR & FR_TXFF);
40         REG_DR = c & 0xff;
41 }
42
43 int ser_getchar(void)
44 {
45         while(REG_FR & FR_RXFE);
46         return REG_DR & 0xff;
47 }
48
49 void ser_printstr(const char *s)
50 {
51         while(*s) {
52                 if(*s == '\n') {
53                         ser_putchar('\r');
54                 }
55                 ser_putchar(*s++);
56         }
57 }