]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
Reduce static RAM usage
[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 #include "cmd_fat.h"
28
29
30 uint8_t mcusr __attribute__ ((section (".noinit")));
31
32 #if DEBUG
33 __attribute__ ((naked)) __attribute__ ((section (".init3")))
34 void 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")))
43 void 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
56 static 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
64 static
65 void 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
85 ISR(INT5_vect)
86 {
87 Stat |= S_MSG_PENDING;
88 }
89
90 ISR(INT6_vect)
91 {
92 Stat |= S_CON_PENDING;
93 }
94
95 static
96 void 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 /* Timer */
122 PRR1 &= ~_BV(PRTIM4);
123 OCR4A = F_CPU / 1000 - 1; /* Timer4: 1000Hz interval */
124 TCCR4B = (0b00<<WGM42)|(0b001<<CS40); /* Normal Mode, Prescaler 1 */
125 TIMSK4 = _BV(OCIE4A); /* Enable Output Compare A interrupt */
126
127 /* INT5, INT6: falling edge */
128 EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
129 (0b10 << ISC50) | (0b10 << ISC60);
130 /* Reset pending ints */
131 EIFR |= _BV(INTF5) | _BV(INTF6);
132 /* Enable INT5, and INT6 */
133 EIMSK |= _BV(INT5) | _BV(INT6);
134 }
135
136 static
137 int reset_reason_is_power_on(void)
138 {
139 return (mcusr & _BV(PORF)) != 0;
140 }
141
142 static
143 void setup_system_time(void)
144 {
145 struct tm rtc_time;
146
147 rtc_get(&rtc_time);
148 rtc_time.tm_isdst = 0;
149 set_system_time(mk_gmtime(&rtc_time) );
150 }
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_str(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 : "");
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 __malloc_margin = CONFIG_SYS_MALLOC_MARGIN;
252 setup_avr();
253 for (int i = 0; i < GPIO_MAX; i++)
254 gpio_config(i, INPUT_PULLUP);
255 setup_mmc();
256 env_init();
257 z80_setup_bus();
258
259 if (reset_reason_is_power_on())
260 _delay_ms(CONFIG_PWRON_DELAY);
261
262 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE), 10, CONFIG_BAUDRATE));
263 sei();
264
265 #if DEBUG
266 debug("\n=========================< (RE)START DEBUG >=========================\n");
267 print_reset_reason();
268 #endif
269
270 i2c_init(CONFIG_SYS_I2C_CLOCK);
271 setup_system_time();
272 setup_fatfs();
273
274 printf_P(PSTR("\n" MCU_STRING "+Z8S180 Stamp Monitor - Version: " VERSION " \n\n"));
275
276 setup_z180_serv();
277
278 main_loop();
279 }