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