1 /*
2  * Copyright (C) 2018 bzt (bztsrc@github)
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  */
25 
26 #include "gpio.h"
27 
28 /* Auxilary mini UART registers */
29 #define AUX_ENABLE      ((volatile unsigned int*)(MMIO_BASE+0x00215004))
30 #define AUX_MU_IO       ((volatile unsigned int*)(MMIO_BASE+0x00215040))
31 #define AUX_MU_IER      ((volatile unsigned int*)(MMIO_BASE+0x00215044))
32 #define AUX_MU_IIR      ((volatile unsigned int*)(MMIO_BASE+0x00215048))
33 #define AUX_MU_LCR      ((volatile unsigned int*)(MMIO_BASE+0x0021504C))
34 #define AUX_MU_MCR      ((volatile unsigned int*)(MMIO_BASE+0x00215050))
35 #define AUX_MU_LSR      ((volatile unsigned int*)(MMIO_BASE+0x00215054))
36 #define AUX_MU_MSR      ((volatile unsigned int*)(MMIO_BASE+0x00215058))
37 #define AUX_MU_SCRATCH  ((volatile unsigned int*)(MMIO_BASE+0x0021505C))
38 #define AUX_MU_CNTL     ((volatile unsigned int*)(MMIO_BASE+0x00215060))
39 #define AUX_MU_STAT     ((volatile unsigned int*)(MMIO_BASE+0x00215064))
40 #define AUX_MU_BAUD     ((volatile unsigned int*)(MMIO_BASE+0x00215068))
41 
42 /**
43  * Set baud rate and characteristics (115200 8N1) and map to GPIO
44  */
uart_init()45 void uart_init()
46 {
47     register unsigned int r;
48 
49     /* initialize UART */
50     *AUX_ENABLE |=1;       // enable UART1, AUX mini uart
51     *AUX_MU_CNTL = 0;
52     *AUX_MU_LCR = 3;       // 8 bits
53     *AUX_MU_MCR = 0;
54     *AUX_MU_IER = 0;
55     *AUX_MU_IIR = 0xc6;    // disable interrupts
56     *AUX_MU_BAUD = 270;    // 115200 baud
57     /* map UART1 to GPIO pins */
58     r=*GPFSEL1;
59     r&=~((7<<12)|(7<<15)); // gpio14, gpio15
60     r|=(2<<12)|(2<<15);    // alt5
61     *GPFSEL1 = r;
62     *GPPUD = 0;            // enable pins 14 and 15
63     r=150; while(r--) { asm volatile("nop"); }
64     *GPPUDCLK0 = (1<<14)|(1<<15);
65     r=150; while(r--) { asm volatile("nop"); }
66     *GPPUDCLK0 = 0;        // flush GPIO setup
67     *AUX_MU_CNTL = 3;      // enable Tx, Rx
68 }
69 
70 /**
71  * Send a character
72  */
uart_send(unsigned int c)73 void uart_send(unsigned int c) {
74     /* wait until we can send */
75     do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x20));
76     /* write the character to the buffer */
77     *AUX_MU_IO=c;
78 }
79 
80 /**
81  * Receive a character
82  */
uart_getc()83 char uart_getc() {
84     char r;
85     /* wait until something is in the buffer */
86     do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x01));
87     /* read it and return */
88     r=(char)(*AUX_MU_IO);
89     /* convert carrige return to newline */
90     return r=='\r'?'\n':r;
91 }
92 
93 /**
94  * Display a string
95  */
uart_puts(char * s)96 void uart_puts(char *s) {
97     while(*s) {
98         /* convert newline to carrige return + newline */
99         if(*s=='\n')
100             uart_send('\r');
101         uart_send(*s++);
102     }
103 }
104