]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
Use timer 4 instead of 3 for systick (1ms).
[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 <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
28 uint8_t mcusr __attribute__ ((section (".noinit")));
29
30 #if DEBUG
31 __attribute__ ((naked)) __attribute__ ((section (".init3")))
32 void preset_ram (void)
33 {
34 for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++)
35 *p = 0xdd;
36
37 }
38 #endif
39
40 __attribute__ ((naked)) __attribute__ ((section (".init3")))
41 void 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
53
54 static 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
62 static
63 void 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
80
81 ISR(INT5_vect)
82 {
83 Stat |= S_MSG_PENDING;
84 }
85
86 ISR(INT6_vect)
87 {
88 Stat |= S_CON_PENDING;
89 }
90
91 static
92 void setup_avr(void)
93 {
94 /* CPU */
95
96 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
97 MCUCR = _BV(JTD);
98 MCUCR = _BV(JTD);
99
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
105 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
106 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
107
108
109 /* disable analog comparator */
110 ACSR = _BV(ACD);
111 /* Ports */
112
113 /* Clock */
114 CLKPR = _BV(CLKPCE);
115 CLKPR = 0;
116
117 /* Timer */
118 PRR1 &= ~_BV(PRTIM4);
119 OCR4A = F_CPU / 1000 - 1; /* Timer4: 1000Hz interval */
120 TCCR4B = (0b00<<WGM42)|(0b001<<CS40); /* Normal Mode, Prescaler 1 */
121 TIMSK4 = _BV(OCIE4A); /* Enable Output Compare A interrupt */
122
123 /* INT5, INT6: falling edge */
124 EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
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);
130 }
131
132 static
133 int reset_reason_is_power_on(void)
134 {
135 return (mcusr & _BV(PORF)) != 0;
136 }
137
138 static
139 void 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
148
149
150 static void setup_fatfs(void)
151 {
152 static FATFS FatFs0;
153 static FATFS FatFs1;
154
155 f_mount(&FatFs0, "0:", 0);
156 f_mount(&FatFs1, "1:", 0);
157 }
158
159 /*--------------------------------------------------------------------------*/
160
161 /* Stored value of bootdelay, used by autoboot_command() */
162 static int stored_bootdelay;
163
164
165 /***************************************************************************
166 * Watch for 'delay' seconds for autoboot stop.
167 * returns: 0 - no key, allow autoboot
168 * 1 - got key, abort
169 */
170
171 static int abortboot(int bootdelay)
172 {
173 int abort = 0;
174 uint32_t ts;
175
176 if (bootdelay >= 0)
177 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
178
179 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
180 /*
181 * Check if key already pressed
182 * Don't check if bootdelay < 0
183 */
184 if (bootdelay >= 0) {
185 if (tstc()) { /* we got a key press */
186 (void) my_getchar(1); /* consume input */
187 my_puts_P(PSTR("\b\b\b 0"));
188 abort = 1; /* don't auto boot */
189 }
190 }
191 #endif
192
193 while ((bootdelay > 0) && (!abort)) {
194 --bootdelay;
195 /* delay 1000 ms */
196 ts = get_timer(0);
197 do {
198 if (tstc()) { /* we got a key press */
199 abort = 1; /* don't auto boot */
200 bootdelay = 0; /* no more delay */
201 break;
202 }
203 udelay(10000);
204 } while (!abort && get_timer(ts) < 1000);
205
206 printf_P(PSTR("\b\b\b%2d "), bootdelay);
207 }
208
209 putchar('\n');
210
211 return abort;
212 }
213
214 static
215 const char *bootdelay_process(void)
216 {
217 char *s;
218 int bootdelay;
219
220 bootdelay = (int) getenv_ulong(PSTR(ENV_BOOTDELAY), 10, CONFIG_BOOTDELAY);
221
222
223 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
224 _delay_ms(20);
225
226 s = getenv_char(PSTR(ENV_BOOTCMD));
227 stored_bootdelay = bootdelay;
228 return s;
229 }
230
231 static
232 void autoboot_command(const char *s)
233 {
234 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
235 _delay_ms(20);
236
237 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
238 run_command_list(s, -1);
239 }
240 }
241
242
243 static
244 void main_loop(void)
245 {
246 const char *s;
247
248 s = bootdelay_process();
249 autoboot_command(s);
250 cli_loop();
251 }
252
253 int main(void)
254 {
255 extern void setup_mmc(void);
256
257 setup_avr();
258 for (int i = 0; i < GPIO_MAX; i++)
259 gpio_config(i, INPUT_PULLUP);
260 setup_mmc();
261 env_init();
262 z80_setup_bus();
263
264 if (reset_reason_is_power_on())
265 _delay_ms(CONFIG_PWRON_DELAY);
266
267 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
268 sei();
269
270 #if DEBUG
271 debug("\n=========================< (RE)START DEBUG >=========================\n");
272 print_reset_reason();
273 #endif
274
275 i2c_init(CONFIG_SYS_I2C_CLOCK);
276 setup_system_time();
277 setup_fatfs();
278
279 printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor\n\n"));
280
281 setup_z180_serv();
282
283 main_loop();
284 }