]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/main.c
bit operation optimizing
[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{
dd1cc6f0
L
87 Stat |= S_MSG_PENDING;
88 Stat |= S_IO_0X40;
bad2d92d
L
89}
90
89adce76
L
91ISR(INT6_vect)
92{
93 Stat |= S_CON_PENDING;
94}
95
f338df2a 96static
d684c216
L
97void setup_avr(void)
98{
d684c216
L
99 /* CPU */
100
101 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
102 MCUCR = _BV(JTD);
103 MCUCR = _BV(JTD);
104
04a63b0d
L
105 /* Disable peripherals. Enable individually in respective init function. */
106 PRR0 = _BV(PRTWI) |
107 _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRTIM1) |
108 _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
109
f76ca346 110 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
d684c216
L
111 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
112
04a63b0d 113
89826c56 114 /* Disable analog comparator */
d684c216
L
115 ACSR = _BV(ACD);
116 /* Ports */
117
118 /* Clock */
119 CLKPR = _BV(CLKPCE);
120 CLKPR = 0;
121
89826c56
L
122 /* System timer */
123 setup_timer();
bad2d92d 124
89adce76 125 /* INT5, INT6: falling edge */
7f552300 126 EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
89adce76
L
127 (0b10 << ISC50) | (0b10 << ISC60);
128 /* Reset pending ints */
129 EIFR |= _BV(INTF5) | _BV(INTF6);
130 /* Enable INT5, and INT6 */
131 EIMSK |= _BV(INT5) | _BV(INT6);
d684c216
L
132}
133
f338df2a 134static
35e9ec0c 135int reset_reason_is_power_on(void)
f338df2a 136{
35e9ec0c 137 return (mcusr & _BV(PORF)) != 0;
f338df2a 138}
d684c216 139
be5cfb4b
L
140static
141void setup_system_time(void)
142{
143 struct tm rtc_time;
144
145 rtc_get(&rtc_time);
146 rtc_time.tm_isdst = 0;
147 set_system_time(mk_gmtime(&rtc_time) );
148}
149
bbd45c46 150
35e9ec0c 151/*--------------------------------------------------------------------------*/
d684c216
L
152
153/* Stored value of bootdelay, used by autoboot_command() */
154static int stored_bootdelay;
155
156
157/***************************************************************************
158 * Watch for 'delay' seconds for autoboot stop.
f76ca346 159 * returns: 0 - no key, allow autoboot
d684c216
L
160 * 1 - got key, abort
161 */
162
163static int abortboot(int bootdelay)
164{
165 int abort = 0;
166 uint32_t ts;
167
168 if (bootdelay >= 0)
169 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
170
171#if defined CONFIG_ZERO_BOOTDELAY_CHECK
172 /*
173 * Check if key already pressed
174 * Don't check if bootdelay < 0
175 */
176 if (bootdelay >= 0) {
177 if (tstc()) { /* we got a key press */
424b184a 178 (void) my_getchar(1); /* consume input */
d684c216
L
179 my_puts_P(PSTR("\b\b\b 0"));
180 abort = 1; /* don't auto boot */
181 }
182 }
183#endif
184
185 while ((bootdelay > 0) && (!abort)) {
186 --bootdelay;
187 /* delay 1000 ms */
188 ts = get_timer(0);
189 do {
190 if (tstc()) { /* we got a key press */
191 abort = 1; /* don't auto boot */
192 bootdelay = 0; /* no more delay */
193 break;
194 }
195 udelay(10000);
196 } while (!abort && get_timer(ts) < 1000);
197
198 printf_P(PSTR("\b\b\b%2d "), bootdelay);
199 }
200
201 putchar('\n');
202
203 return abort;
204}
205
f338df2a 206static
d684c216
L
207const char *bootdelay_process(void)
208{
209 char *s;
210 int bootdelay;
211
70c99491 212 bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);
d684c216
L
213
214
215 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
216 _delay_ms(20);
217
bddc7e77 218 s = getenv_str(PSTR(ENV_BOOTCMD));
d684c216
L
219 stored_bootdelay = bootdelay;
220 return s;
221}
222
f338df2a 223static
d684c216
L
224void autoboot_command(const char *s)
225{
209025e8 226 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "");
d684c216
L
227 _delay_ms(20);
228
229 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
230 run_command_list(s, -1);
231 }
232}
233
234
f338df2a 235static
d684c216
L
236void main_loop(void)
237{
238 const char *s;
239
240 s = bootdelay_process();
241 autoboot_command(s);
242 cli_loop();
243}
244
245int main(void)
246{
15e476bc
L
247 extern void setup_mmc(void);
248
fb9b17a9 249 __malloc_margin = CONFIG_SYS_MALLOC_MARGIN;
a1595a8e 250 setup_avr();
c79c80b4
L
251 for (int i = 0; i < GPIO_MAX; i++)
252 gpio_config(i, INPUT_PULLUP);
15e476bc 253 setup_mmc();
f14850db 254 env_init();
a1595a8e 255 z80_setup_bus();
f14850db 256
35e9ec0c 257 if (reset_reason_is_power_on())
f14850db 258 _delay_ms(CONFIG_PWRON_DELAY);
f76ca346 259
70c99491 260 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
d684c216 261 sei();
323398b1 262
f338df2a 263#if DEBUG
69988dc1 264 debug("\n=========================< (RE)START DEBUG >=========================\n");
f338df2a
L
265 print_reset_reason();
266#endif
f76ca346 267
35e9ec0c 268 i2c_init(CONFIG_SYS_I2C_CLOCK);
be5cfb4b 269 setup_system_time();
bbd45c46 270 setup_fatfs();
d684c216 271
e4344855 272 printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n"));
f76ca346 273
89adce76 274 setup_z180_serv();
f76ca346 275
d684c216
L
276 main_loop();
277}