]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
printf() --> printf_P(PSTR())
[z180-stamp.git] / avr / main.c
1 /*
2 */
3
4
5 #include "common.h"
6
7 #include <util/delay.h>
8 //#include <avr/power.h>
9 //#include <avr/pgmspace.h>
10 #include <avr/interrupt.h>
11 //#include <util/atomic.h>
12 //#include <avr/sleep.h>
13 //#include <string.h>
14
15 #include <util/delay.h>
16
17 #include <stdlib.h>
18 #include <stdio.h>
19
20
21 #include "config.h"
22 #include "debug.h"
23 #include "z80-if.h"
24 #include "con-utils.h"
25 #include "serial.h"
26 #include "timer.h"
27 #include "cli.h"
28 #include "env.h"
29
30 /*--------------------------------------------------------------------------*/
31 #if DEBUG
32 void preset_ram (void) __attribute__ ((naked)) \
33 __attribute__ ((section (".init3")));
34 void
35 preset_ram (void)
36 {
37 for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++)
38 *p = 0xdd;
39
40 }
41 #endif
42 /*--------------------------------------------------------------------------*/
43
44 static uint8_t mcusr;
45
46 static
47 void setup_avr(void)
48 {
49 /* save and clear reset reason(s) */
50 mcusr = MCUSR;
51 MCUSR = 0;
52
53 /* WD */
54
55 /* CPU */
56
57 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
58 MCUCR = _BV(JTD);
59 MCUCR = _BV(JTD);
60
61 /* disable unused periphels */
62 PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC);
63 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
64 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
65
66 /* disable analog comparator */
67 ACSR = _BV(ACD);
68 /* Ports */
69
70 /* Clock */
71 CLKPR = _BV(CLKPCE);
72 CLKPR = 0;
73
74 /* Timer */
75
76 OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
77 TCCR1B = 0b00001010;
78 TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
79 }
80
81 static const FLASH char * const FLASH rreasons[] = {
82 FSTR("Power on"),
83 FSTR("External"),
84 FSTR("Brown out"),
85 FSTR("Watchdog"),
86 FSTR("JTAG"),
87 };
88
89 static
90 void print_reset_reason(void)
91 {
92 uint8_t r = mcusr & 0x1f;
93 const FLASH char * const FLASH *p = rreasons;
94
95 printf_P(PSTR("Reset reason(s): "));
96 for ( ; r; p++, r >>= 1) {
97 if (r & 1) {
98 my_puts_P(*p);
99 if (r & ~1)
100 printf_P(PSTR(", "));
101 }
102 }
103 printf_P(PSTR(".\n"));
104 }
105
106
107 /*******************************************************************************/
108
109 #define udelay(n) _delay_us(n)
110
111
112 /* Stored value of bootdelay, used by autoboot_command() */
113 static int stored_bootdelay;
114
115
116 /***************************************************************************
117 * Watch for 'delay' seconds for autoboot stop.
118 * returns: 0 - no key, allow autoboot
119 * 1 - got key, abort
120 */
121
122 static int abortboot(int bootdelay)
123 {
124 int abort = 0;
125 uint32_t ts;
126
127 if (bootdelay >= 0)
128 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
129
130 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
131 /*
132 * Check if key already pressed
133 * Don't check if bootdelay < 0
134 */
135 if (bootdelay >= 0) {
136 if (tstc()) { /* we got a key press */
137 (void) my_getchar(); /* consume input */
138 my_puts_P(PSTR("\b\b\b 0"));
139 abort = 1; /* don't auto boot */
140 }
141 }
142 #endif
143
144 while ((bootdelay > 0) && (!abort)) {
145 --bootdelay;
146 /* delay 1000 ms */
147 ts = get_timer(0);
148 do {
149 if (tstc()) { /* we got a key press */
150 abort = 1; /* don't auto boot */
151 bootdelay = 0; /* no more delay */
152 break;
153 }
154 udelay(10000);
155 } while (!abort && get_timer(ts) < 1000);
156
157 printf_P(PSTR("\b\b\b%2d "), bootdelay);
158 }
159
160 putchar('\n');
161
162 return abort;
163 }
164
165 static
166 const char *bootdelay_process(void)
167 {
168 char *s;
169 int bootdelay;
170
171 s = getenv("bootdelay");
172 bootdelay = s ? atoi(s) : CONFIG_BOOTDELAY;
173
174
175 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
176 _delay_ms(20);
177
178 s = getenv("bootcmd");
179 stored_bootdelay = bootdelay;
180 return s;
181 }
182
183 static
184 void autoboot_command(const char *s)
185 {
186 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
187 _delay_ms(20);
188
189 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
190 run_command_list(s, -1);
191 }
192 }
193
194
195 static
196 void main_loop(void)
197 {
198 const char *s;
199
200 s = bootdelay_process();
201 autoboot_command(s);
202 cli_loop();
203 }
204
205 int main(void)
206 {
207
208 setup_avr();
209 z80_setup_bus();
210
211 serial_setup();
212 sei();
213
214 #if DEBUG
215 debug("\n=========================< (RE)START DEBUG >=========================\n");
216 print_reset_reason();
217 #endif
218
219 env_init();
220
221 printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));
222
223 main_loop();
224 }