aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..c306c6c
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,102 @@
1// Header definitions for memory mapped IO and common utilities.
2
3#include "shorthand.h"
4
5#define MEM_BASE 0x3F000000
6
7// GPIO registers.
8#define GPIO_GPFSEL0 ((vu32*)(MEM_BASE + 0x00200000))
9#define GPIO_GPFSEL1 ((vu32*)(MEM_BASE + 0x00200004))
10#define GPIO_GPFSEL2 ((vu32*)(MEM_BASE + 0x00200008))
11#define GPIO_GPFSEL3 ((vu32*)(MEM_BASE + 0x0020000C))
12#define GPIO_GPFSEL4 ((vu32*)(MEM_BASE + 0x00200010))
13#define GPIO_GPFSEL5 ((vu32*)(MEM_BASE + 0x00200014))
14#define GPIO_GPSET0 ((vu32*)(MEM_BASE + 0x0020001C))
15#define GPIO_GPSET1 ((vu32*)(MEM_BASE + 0x00200020))
16#define GPIO_GPCLR0 ((vu32*)(MEM_BASE + 0x00200028))
17#define GPIO_GPLEV0 ((vu32*)(MEM_BASE + 0x00200034))
18#define GPIO_GPLEV1 ((vu32*)(MEM_BASE + 0x00200038))
19#define GPIO_GPEDS0 ((vu32*)(MEM_BASE + 0x00200040))
20#define GPIO_GPEDS1 ((vu32*)(MEM_BASE + 0x00200044))
21#define GPIO_GPHEN0 ((vu32*)(MEM_BASE + 0x00200064))
22#define GPIO_GPHEN1 ((vu32*)(MEM_BASE + 0x00200068))
23#define GPIO_GPPUD ((vu32*)(MEM_BASE + 0x00200094))
24#define GPIO_GPPUDCLK0 ((vu32*)(MEM_BASE + 0x00200098))
25#define GPIO_GPPUDCLK1 ((vu32*)(MEM_BASE + 0x0020009C))
26
27// Auxiliary registers (Mini UART and SPI).
28#define AUX_ENABLE ((vu8*) (MEM_BASE + 0x00215004))
29#define AUX_MU_IO ((vu8*) (MEM_BASE + 0x00215040))
30#define AUX_MU_IER ((vu8*) (MEM_BASE + 0x00215044))
31#define AUX_MU_IIR ((vu8*) (MEM_BASE + 0x00215048))
32#define AUX_MU_LCR ((vu8*) (MEM_BASE + 0x0021504C))
33#define AUX_MU_MCR ((vu8*) (MEM_BASE + 0x00215050))
34#define AUX_MU_LSR ((vu8*) (MEM_BASE + 0x00215054))
35#define AUX_MU_MSR ((vu8*) (MEM_BASE + 0x00215058))
36#define AUX_MU_SCRATCH ((vu8*) (MEM_BASE + 0x0021505C))
37#define AUX_MU_CNTL ((vu8*) (MEM_BASE + 0x00215060))
38#define AUX_MU_STAT ((vu32*)(MEM_BASE + 0x00215064))
39#define AUX_MU_BAUD ((vu16*)(MEM_BASE + 0x00215068))
40
41/**
42 * Set baud rate and characteristics (115200 8N1) and map to GPIO
43 */
44void uart_init()
45{
46 register unsigned int r;
47
48 /* initialize UART */
49 *AUX_ENABLE |=1; // enable UART1, AUX mini uart
50 *AUX_MU_CNTL = 0;
51 *AUX_MU_LCR = 3; // 8 bits
52 *AUX_MU_MCR = 0;
53 *AUX_MU_IER = 0;
54 *AUX_MU_IIR = 0xc6; // disable interrupts
55 *AUX_MU_BAUD = 270; // 115200 baud
56 /* map UART1 to GPIO pins */
57 r=*GPIO_GPFSEL1;
58 r&=~((7<<12)|(7<<15)); // gpio14, gpio15
59 r|=(2<<12)|(2<<15); // alt5
60 *GPIO_GPFSEL1 = r;
61 *GPIO_GPPUD = 0; // enable pins 14 and 15
62 r=150; while(r--) { asm volatile("nop"); }
63 *GPIO_GPPUDCLK0 = (1<<14)|(1<<15);
64 r=150; while(r--) { asm volatile("nop"); }
65 *GPIO_GPPUDCLK0 = 0; // flush GPIO setup
66 *AUX_MU_CNTL = 3; // enable Tx, Rx
67}
68
69/**
70 * Send a character
71 */
72void uart_send(unsigned int c) {
73 /* wait until we can send */
74 do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x20));
75 /* write the character to the buffer */
76 *AUX_MU_IO=c;
77}
78
79/**
80 * Receive a character
81 */
82char uart_getc() {
83 char r;
84 /* wait until something is in the buffer */
85 do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x01));
86 /* read it and return */
87 r=(char)(*AUX_MU_IO);
88 /* convert carrige return to newline */
89 return r=='\r'?'\n':r;
90}
91
92/**
93 * Display a string
94 */
95void uart_puts(char *s) {
96 while(*s) {
97 /* convert newline to carrige return + newline */
98 if(*s=='\n')
99 uart_send('\r');
100 uart_send(*s++);
101 }
102}