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