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