|
| 1 | + |
| 2 | +//------------------------------------------------------------------------- |
| 3 | +//------------------------------------------------------------------------- |
| 4 | + |
| 5 | +extern void PUT32 ( unsigned int, unsigned int ); |
| 6 | +extern unsigned int GET32 ( unsigned int ); |
| 7 | +extern void dummy ( unsigned int ); |
| 8 | + |
| 9 | +#define GPFSEL1 0x20200004 |
| 10 | +#define GPSET0 0x2020001C |
| 11 | +#define GPCLR0 0x20200028 |
| 12 | +#define GPPUD 0x20200094 |
| 13 | +#define GPPUDCLK0 0x20200098 |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +#define UART0_BASE 0x20201000 |
| 18 | +#define UART0_DR (UART0_BASE+0x00) |
| 19 | +#define UART0_RSRECR (UART0_BASE+0x04) |
| 20 | +#define UART0_FR (UART0_BASE+0x18) |
| 21 | +#define UART0_ILPR (UART0_BASE+0x20) |
| 22 | +#define UART0_IBRD (UART0_BASE+0x24) |
| 23 | +#define UART0_FBRD (UART0_BASE+0x28) |
| 24 | +#define UART0_LCRH (UART0_BASE+0x2C) |
| 25 | +#define UART0_CR (UART0_BASE+0x30) |
| 26 | +#define UART0_IFLS (UART0_BASE+0x34) |
| 27 | +#define UART0_IMSC (UART0_BASE+0x38) |
| 28 | +#define UART0_RIS (UART0_BASE+0x3C) |
| 29 | +#define UART0_MIS (UART0_BASE+0x40) |
| 30 | +#define UART0_ICR (UART0_BASE+0x44) |
| 31 | +#define UART0_DMACR (UART0_BASE+0x48) |
| 32 | +#define UART0_ITCR (UART0_BASE+0x80) |
| 33 | +#define UART0_ITIP (UART0_BASE+0x84) |
| 34 | +#define UART0_ITOP (UART0_BASE+0x88) |
| 35 | +#define UART0_TDR (UART0_BASE+0x8C) |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +//GPIO14 TXD0 and TXD1 |
| 45 | +//GPIO15 RXD0 and RXD1 |
| 46 | +//alt function 5 for uart1 |
| 47 | +//alt function 0 for uart0 |
| 48 | + |
| 49 | +//(3000000 / (16 * 115200) = 1.627 |
| 50 | +//(0.627*64)+0.5 = 40 |
| 51 | +//int 1 frac 40 |
| 52 | + |
| 53 | +//------------------------------------------------------------------------ |
| 54 | +void uart_putc ( unsigned int c ) |
| 55 | +{ |
| 56 | + while(1) |
| 57 | + { |
| 58 | + if((GET32(UART0_FR)&0x20)==0) break; |
| 59 | + } |
| 60 | + PUT32(UART0_DR,c); |
| 61 | +} |
| 62 | +//------------------------------------------------------------------------ |
| 63 | +unsigned int uart_getc ( void ) |
| 64 | +{ |
| 65 | + while(1) |
| 66 | + { |
| 67 | + if((GET32(UART0_FR)&0x10)==0) break; |
| 68 | + } |
| 69 | + return(GET32(UART0_DR)); |
| 70 | +} |
| 71 | +//------------------------------------------------------------------------ |
| 72 | +void uart_init ( void ) |
| 73 | +{ |
| 74 | + unsigned int ra; |
| 75 | + |
| 76 | + PUT32(UART0_CR,0); |
| 77 | + |
| 78 | + ra=GET32(GPFSEL1); |
| 79 | + ra&=~(7<<12); //gpio14 |
| 80 | + ra|=4<<12; //alt0 |
| 81 | + ra&=~(7<<15); //gpio15 |
| 82 | + ra|=4<<15; //alt0 |
| 83 | + PUT32(GPFSEL1,ra); |
| 84 | + |
| 85 | + PUT32(GPPUD,0); |
| 86 | + for(ra=0;ra<150;ra++) dummy(ra); |
| 87 | + PUT32(GPPUDCLK0,(1<<14)|(1<<15)); |
| 88 | + for(ra=0;ra<150;ra++) dummy(ra); |
| 89 | + PUT32(GPPUDCLK0,0); |
| 90 | + |
| 91 | + PUT32(UART0_ICR,0x7FF); |
| 92 | + PUT32(UART0_IBRD,1); |
| 93 | + PUT32(UART0_FBRD,40); |
| 94 | + PUT32(UART0_LCRH,0x70); |
| 95 | + PUT32(UART0_CR,0x301); |
| 96 | +} |
| 97 | +//------------------------------------------------------------------------ |
| 98 | +void hexstrings ( unsigned int d ) |
| 99 | +{ |
| 100 | + //unsigned int ra; |
| 101 | + unsigned int rb; |
| 102 | + unsigned int rc; |
| 103 | + |
| 104 | + rb=32; |
| 105 | + while(1) |
| 106 | + { |
| 107 | + rb-=4; |
| 108 | + rc=(d>>rb)&0xF; |
| 109 | + if(rc>9) rc+=0x37; else rc+=0x30; |
| 110 | + uart_putc(rc); |
| 111 | + if(rb==0) break; |
| 112 | + } |
| 113 | + uart_putc(0x20); |
| 114 | +} |
| 115 | +//------------------------------------------------------------------------ |
| 116 | +void hexstring ( unsigned int d ) |
| 117 | +{ |
| 118 | + hexstrings(d); |
| 119 | + uart_putc(0x0D); |
| 120 | + uart_putc(0x0A); |
| 121 | +} |
| 122 | +//------------------------------------------------------------------------ |
| 123 | +int notmain ( unsigned int earlypc ) |
| 124 | +{ |
| 125 | + unsigned int ra; |
| 126 | + |
| 127 | + uart_init(); |
| 128 | + hexstring(0x12345678); |
| 129 | + hexstring(earlypc); |
| 130 | + |
| 131 | + for(ra=0;ra<30000;ra++) |
| 132 | + { |
| 133 | + uart_putc(0x30|(ra&7)); |
| 134 | + } |
| 135 | + |
| 136 | + for(ra=0;ra<100;ra++) uart_putc(0x55); |
| 137 | + |
| 138 | + //probably a better way to flush the rx fifo. depending on if and |
| 139 | + //which bootloader you used you might have some stuff show up in the |
| 140 | + //rx fifo. |
| 141 | + while(1) |
| 142 | + { |
| 143 | + if(GET32(UART0_FR)&0x10) break; |
| 144 | + GET32(UART0_DR); |
| 145 | + } |
| 146 | + |
| 147 | + while(1) |
| 148 | + { |
| 149 | + ra=uart_getc(); |
| 150 | + if(ra==0x0D) uart_putc(0x0A); |
| 151 | + uart_putc(ra); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + return(0); |
| 156 | +} |
| 157 | +//------------------------------------------------------------------------- |
| 158 | +//------------------------------------------------------------------------- |
| 159 | + |
| 160 | + |
| 161 | +//------------------------------------------------------------------------- |
| 162 | +// |
| 163 | +// Copyright (c) 2012 David Welch [email protected] |
| 164 | +// |
| 165 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 166 | +// |
| 167 | +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 168 | +// |
| 169 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 170 | +// |
| 171 | +//------------------------------------------------------------------------- |
0 commit comments