]> cloudbase.mooo.com Git - z180-stamp.git/blame_incremental - avr/main.c
rewrite of cmd_cpu/do_cpu_freq
[z180-stamp.git] / avr / main.c
... / ...
CommitLineData
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 <avr/wdt.h>
11#include <stdlib.h>
12
13#include "config.h"
14#include "ff.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#include "debug.h"
27#include "cmd_fat.h"
28
29
30uint8_t mcusr __attribute__ ((section (".noinit")));
31
32#if DEBUG
33__attribute__ ((naked)) __attribute__ ((section (".init3")))
34void preset_ram (void)
35{
36 for (uint8_t *p = (uint8_t *) RAMSTART; p <= (uint8_t *) RAMEND; p++)
37 *p = 0xdd;
38
39}
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
55
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
70 my_puts_P(PSTR("### Reset reason(s): "));
71 if (r == 0)
72 my_puts_P(PSTR("none"));
73 for ( ; r; p++, r >>= 1) {
74 if (r & 1) {
75 my_puts_P(*p);
76 if (r & ~1)
77 my_puts_P(PSTR(", "));
78 }
79 }
80 my_puts_P(PSTR(".\n"));
81}
82
83#endif
84
85ISR(INT5_vect)
86{
87 Stat |= S_MSG_PENDING;
88}
89
90ISR(INT6_vect)
91{
92 Stat |= S_CON_PENDING;
93}
94
95static
96void setup_avr(void)
97{
98 /* CPU */
99
100 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
101 MCUCR = _BV(JTD);
102 MCUCR = _BV(JTD);
103
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
109 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
110 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
111
112
113 /* Disable analog comparator */
114 ACSR = _BV(ACD);
115 /* Ports */
116
117 /* Clock */
118 CLKPR = _BV(CLKPCE);
119 CLKPR = 0;
120
121 /* System timer */
122 setup_timer();
123
124 /* INT5, INT6: falling edge */
125 EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
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);
131}
132
133static
134int reset_reason_is_power_on(void)
135{
136 return (mcusr & _BV(PORF)) != 0;
137}
138
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
149
150/*--------------------------------------------------------------------------*/
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.
158 * returns: 0 - no key, allow autoboot
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 */
177 (void) my_getchar(1); /* consume input */
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
205static
206const char *bootdelay_process(void)
207{
208 char *s;
209 int bootdelay;
210
211 bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);
212
213
214 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
215 _delay_ms(20);
216
217 s = getenv_str(PSTR(ENV_BOOTCMD));
218 stored_bootdelay = bootdelay;
219 return s;
220}
221
222static
223void autoboot_command(const char *s)
224{
225 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "");
226 _delay_ms(20);
227
228 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
229 run_command_list(s, -1);
230 }
231}
232
233
234static
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{
246 extern void setup_mmc(void);
247
248 __malloc_margin = CONFIG_SYS_MALLOC_MARGIN;
249 setup_avr();
250 for (int i = 0; i < GPIO_MAX; i++)
251 gpio_config(i, INPUT_PULLUP);
252 setup_mmc();
253 env_init();
254 z80_setup_bus();
255
256 if (reset_reason_is_power_on())
257 _delay_ms(CONFIG_PWRON_DELAY);
258
259 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
260 sei();
261
262#if DEBUG
263 debug("\n=========================< (RE)START DEBUG >=========================\n");
264 print_reset_reason();
265#endif
266
267 i2c_init(CONFIG_SYS_I2C_CLOCK);
268 setup_system_time();
269 setup_fatfs();
270
271 printf_P(PSTR("\n" stringify(DEVICE_NAME) "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n"));
272
273 setup_z180_serv();
274
275 main_loop();
276}