]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
Add pin_alias
[z180-stamp.git] / avr / main.c
1 /*
2 */
3
4
5 #include "common.h"
6
7 #include <util/delay.h>
8 #include <avr/interrupt.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include "config.h"
13 #include "debug.h"
14 #include "z80-if.h"
15 #include "i2c.h"
16 #include "con-utils.h"
17 #include "serial.h"
18 #include "timer.h"
19 #include "cli.h"
20 #include "env.h"
21
22 #define udelay(n) _delay_us(n)
23
24 static uint8_t mcusr;
25
26 /*--------------------------------------------------------------------------*/
27 #if DEBUG
28
29 __attribute__ ((naked)) __attribute__ ((section (".init3")))
30 void preset_ram (void)
31 {
32 for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++)
33 *p = 0xdd;
34
35 }
36
37 static const FLASH char * const FLASH rreasons[] = {
38 FSTR("Power on"),
39 FSTR("External"),
40 FSTR("Brown out"),
41 FSTR("Watchdog"),
42 FSTR("JTAG"),
43 };
44
45 static
46 void print_reset_reason(void)
47 {
48 uint8_t r = mcusr & 0x1f;
49 const FLASH char * const FLASH *p = rreasons;
50
51 printf_P(PSTR("### Reset reason(s): %s"), r ? "" : "none");
52 for ( ; r; p++, r >>= 1) {
53 if (r & 1) {
54 my_puts_P(*p);
55 if (r & ~1)
56 printf_P(PSTR(", "));
57 }
58 }
59 printf_P(PSTR(".\n"));
60 }
61
62 #endif
63
64 static
65 void setup_avr(void)
66 {
67 /* save and clear reset reason(s) */
68 /* TODO: move to init section? */
69 mcusr = MCUSR;
70 MCUSR = 0;
71
72 /* WD */
73
74 /* CPU */
75
76 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
77 MCUCR = _BV(JTD);
78 MCUCR = _BV(JTD);
79
80 /* Disable peripherals. Enable individually in respective init function. */
81 PRR0 = _BV(PRTWI) |
82 _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRTIM1) |
83 _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
84
85 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
86 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
87
88
89 /* disable analog comparator */
90 ACSR = _BV(ACD);
91 /* Ports */
92
93 /* Clock */
94 CLKPR = _BV(CLKPCE);
95 CLKPR = 0;
96
97 /* Timer */
98 PRR1 &= ~_BV(PRTIM3);
99 OCR3A = F_CPU / 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
100 TCCR3B = (0b01<<WGM32)|(0b001<<CS30); /* CTC Mode, Prescaler 1 */
101 TIMSK3 = _BV(OCIE3A); /* Enable TC2.oca interrupt */
102 }
103
104 static
105 int reset_reason_is_power_on(void)
106 {
107 return (mcusr & _BV(PORF)) != 0;
108 }
109
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 bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);
172
173
174 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
175 _delay_ms(20);
176
177 s = getenv(PSTR(ENV_BOOTCMD));
178 stored_bootdelay = bootdelay;
179 return s;
180 }
181
182 static
183 void autoboot_command(const char *s)
184 {
185 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
186 _delay_ms(20);
187
188 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
189 run_command_list(s, -1);
190 }
191 }
192
193
194 static
195 void main_loop(void)
196 {
197 const char *s;
198
199 s = bootdelay_process();
200 autoboot_command(s);
201 cli_loop();
202 }
203
204 int main(void)
205 {
206
207 setup_avr();
208 z80_setup_bus();
209
210 env_init();
211
212 if (reset_reason_is_power_on())
213 _delay_ms(CONFIG_PWRON_DELAY);
214
215 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
216 sei();
217
218 #if DEBUG
219 debug("\n=========================< (RE)START DEBUG >=========================\n");
220 print_reset_reason();
221 #endif
222
223 #if DEBUG
224 unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK);
225 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed);
226 i2c_init(i_speed);
227 #else
228 i2c_init(CONFIG_SYS_I2C_CLOCK);
229 #endif
230
231 printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));
232
233
234 main_loop();
235 }