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