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