]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
Remove memory test and bank manager.
[z180-stamp.git] / avr / main.c
1 /*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7
8 #include "common.h"
9 #include <avr/interrupt.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 #include "config.h"
14 #include "debug.h"
15 #include "z80-if.h"
16 #include "i2c.h"
17 #include "con-utils.h"
18 #include "serial.h"
19 #include "timer.h"
20 #include "cli.h"
21 #include "env.h"
22 #include "z180-serv.h"
23 #include "gpio.h"
24 #include "time.h"
25 #include "rtc.h"
26
27 static uint8_t mcusr;
28
29 /*--------------------------------------------------------------------------*/
30 #if DEBUG
31
32 __attribute__ ((naked)) __attribute__ ((section (".init3")))
33 void preset_ram (void)
34 {
35 for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++)
36 *p = 0xdd;
37
38 }
39
40 static const FLASH char * const FLASH rreasons[] = {
41 FSTR("Power on"),
42 FSTR("External"),
43 FSTR("Brown out"),
44 FSTR("Watchdog"),
45 FSTR("JTAG"),
46 };
47
48 static
49 void print_reset_reason(void)
50 {
51 uint8_t r = mcusr & 0x1f;
52 const FLASH char * const FLASH *p = rreasons;
53
54 printf_P(PSTR("### Reset reason(s): %s"), r ? "" : "none");
55 for ( ; r; p++, r >>= 1) {
56 if (r & 1) {
57 my_puts_P(*p);
58 if (r & ~1)
59 printf_P(PSTR(", "));
60 }
61 }
62 printf_P(PSTR(".\n"));
63 }
64
65 #endif
66
67 ISR(INT5_vect)
68 {
69 Stat |= S_MSG_PENDING;
70 }
71
72 ISR(INT6_vect)
73 {
74 Stat |= S_CON_PENDING;
75 }
76
77 static
78 void setup_avr(void)
79 {
80 /* save and clear reset reason(s) */
81 /* TODO: move to init section? */
82 mcusr = MCUSR;
83 MCUSR = 0;
84
85 /* WD */
86
87 /* CPU */
88
89 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
90 MCUCR = _BV(JTD);
91 MCUCR = _BV(JTD);
92
93 /* Disable peripherals. Enable individually in respective init function. */
94 PRR0 = _BV(PRTWI) |
95 _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRTIM1) |
96 _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
97
98 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
99 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
100
101
102 /* disable analog comparator */
103 ACSR = _BV(ACD);
104 /* Ports */
105
106 /* Clock */
107 CLKPR = _BV(CLKPCE);
108 CLKPR = 0;
109
110 /* Timer */
111 PRR1 &= ~_BV(PRTIM3);
112 OCR3A = F_CPU / 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
113 TCCR3B = (0b01<<WGM32)|(0b001<<CS30); /* CTC Mode, Prescaler 1 */
114 TIMSK3 = _BV(OCIE3A); /* Enable TC2.oca interrupt */
115
116 /* INT5, INT6: falling edge */
117 EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
118 (0b10 << ISC50) | (0b10 << ISC60);
119 /* Reset pending ints */
120 EIFR |= _BV(INTF5) | _BV(INTF6);
121 /* Enable INT5, and INT6 */
122 EIMSK |= _BV(INT5) | _BV(INT6);
123 }
124
125 static
126 int reset_reason_is_power_on(void)
127 {
128 return (mcusr & _BV(PORF)) != 0;
129 }
130
131 static
132 void setup_system_time(void)
133 {
134 struct tm rtc_time;
135
136 rtc_get(&rtc_time);
137 rtc_time.tm_isdst = 0;
138 set_system_time(mk_gmtime(&rtc_time) );
139 }
140
141 /*--------------------------------------------------------------------------*/
142
143 /* Stored value of bootdelay, used by autoboot_command() */
144 static int stored_bootdelay;
145
146
147 /***************************************************************************
148 * Watch for 'delay' seconds for autoboot stop.
149 * returns: 0 - no key, allow autoboot
150 * 1 - got key, abort
151 */
152
153 static int abortboot(int bootdelay)
154 {
155 int abort = 0;
156 uint32_t ts;
157
158 if (bootdelay >= 0)
159 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
160
161 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
162 /*
163 * Check if key already pressed
164 * Don't check if bootdelay < 0
165 */
166 if (bootdelay >= 0) {
167 if (tstc()) { /* we got a key press */
168 (void) my_getchar(1); /* consume input */
169 my_puts_P(PSTR("\b\b\b 0"));
170 abort = 1; /* don't auto boot */
171 }
172 }
173 #endif
174
175 while ((bootdelay > 0) && (!abort)) {
176 --bootdelay;
177 /* delay 1000 ms */
178 ts = get_timer(0);
179 do {
180 if (tstc()) { /* we got a key press */
181 abort = 1; /* don't auto boot */
182 bootdelay = 0; /* no more delay */
183 break;
184 }
185 udelay(10000);
186 } while (!abort && get_timer(ts) < 1000);
187
188 printf_P(PSTR("\b\b\b%2d "), bootdelay);
189 }
190
191 putchar('\n');
192
193 return abort;
194 }
195
196 static
197 const char *bootdelay_process(void)
198 {
199 char *s;
200 int bootdelay;
201
202 bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);
203
204
205 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
206 _delay_ms(20);
207
208 s = getenv(PSTR(ENV_BOOTCMD));
209 stored_bootdelay = bootdelay;
210 return s;
211 }
212
213 static
214 void autoboot_command(const char *s)
215 {
216 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
217 _delay_ms(20);
218
219 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
220 run_command_list(s, -1);
221 }
222 }
223
224
225 static
226 void main_loop(void)
227 {
228 const char *s;
229
230 s = bootdelay_process();
231 autoboot_command(s);
232 cli_loop();
233 }
234
235 int main(void)
236 {
237 extern void setup_mmc(void);
238
239 for (int i = 0; i < GPIO_MAX; i++)
240 gpio_config(i, INPUT_PULLUP);
241 setup_avr();
242 setup_mmc();
243 z80_setup_bus();
244 env_init();
245
246 if (reset_reason_is_power_on())
247 _delay_ms(CONFIG_PWRON_DELAY);
248
249 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
250 sei();
251
252 #if DEBUG
253 debug("\n=========================< (RE)START DEBUG >=========================\n");
254 print_reset_reason();
255 #endif
256
257 #if DEBUG
258 unsigned long i_speed = getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK);
259 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed);
260 i2c_init(i_speed);
261 #else
262 i2c_init(CONFIG_SYS_I2C_CLOCK);
263 #endif
264 setup_system_time();
265
266 printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));
267
268 setup_z180_serv();
269
270 main_loop();
271 }