]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
6dbcdefa841a584a74eee75f32716c6d46d26e2a
[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
32 void setup_avr(void)
33 {
34 /* WD */
35
36 /* CPU */
37
38 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
39 MCUCR = _BV(JTD);
40 MCUCR = _BV(JTD);
41
42 /* disable unused periphels */
43 PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC);
44 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
45 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
46
47 /* disable analog comparator */
48 ACSR = _BV(ACD);
49 /* Ports */
50
51 /* Clock */
52 CLKPR = _BV(CLKPCE);
53 CLKPR = 0;
54
55 /* Timer */
56
57 OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
58 TCCR1B = 0b00001010;
59 TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
60 }
61
62
63
64 /*******************************************************************************/
65 /*******************************************************************************/
66
67 #define udelay(n) _delay_us(n)
68
69
70 /* Stored value of bootdelay, used by autoboot_command() */
71 static int stored_bootdelay;
72
73
74 /***************************************************************************
75 * Watch for 'delay' seconds for autoboot stop.
76 * returns: 0 - no key, allow autoboot
77 * 1 - got key, abort
78 */
79
80 static int abortboot(int bootdelay)
81 {
82 int abort = 0;
83 uint32_t ts;
84
85 if (bootdelay >= 0)
86 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
87
88 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
89 /*
90 * Check if key already pressed
91 * Don't check if bootdelay < 0
92 */
93 if (bootdelay >= 0) {
94 if (tstc()) { /* we got a key press */
95 (void) my_getchar(); /* consume input */
96 my_puts_P(PSTR("\b\b\b 0"));
97 abort = 1; /* don't auto boot */
98 }
99 }
100 #endif
101
102 while ((bootdelay > 0) && (!abort)) {
103 --bootdelay;
104 /* delay 1000 ms */
105 ts = get_timer(0);
106 do {
107 if (tstc()) { /* we got a key press */
108 abort = 1; /* don't auto boot */
109 bootdelay = 0; /* no more delay */
110 break;
111 }
112 udelay(10000);
113 } while (!abort && get_timer(ts) < 1000);
114
115 printf_P(PSTR("\b\b\b%2d "), bootdelay);
116 }
117
118 putchar('\n');
119
120 return abort;
121 }
122
123 const char *bootdelay_process(void)
124 {
125 char *s;
126 int bootdelay;
127
128 s = getenv("bootdelay");
129 bootdelay = s ? atoi(s) : CONFIG_BOOTDELAY;
130
131
132 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
133 _delay_ms(20);
134
135 s = getenv("bootcmd");
136 stored_bootdelay = bootdelay;
137 return s;
138 }
139
140 void autoboot_command(const char *s)
141 {
142 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
143 _delay_ms(20);
144
145 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
146 run_command_list(s, -1);
147 }
148 }
149
150
151 void main_loop(void)
152 {
153 const char *s;
154
155 s = bootdelay_process();
156 autoboot_command(s);
157 cli_loop();
158 }
159
160 int main(void)
161 {
162 setup_avr();
163 z80_setup_bus();
164
165 serial_setup();
166 sei();
167
168 if (env_check() == 0)
169 set_default_env();
170 env_init();
171
172 printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));
173
174 main_loop();
175 }