]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
Add date rtc i2c
[z180-stamp.git] / avr / main.c
1 /*
2 */
3
4
5 #include "common.h"
6
7 #include <util/delay.h>
8 #include <avr/interrupt.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include "config.h"
13 #include "debug.h"
14 #include "z80-if.h"
15 #include "i2c.h"
16 #include "con-utils.h"
17 #include "serial.h"
18 #include "timer.h"
19 #include "cli.h"
20 #include "env.h"
21
22 /*--------------------------------------------------------------------------*/
23 #if DEBUG
24 void preset_ram (void) __attribute__ ((naked)) \
25 __attribute__ ((section (".init3")));
26 void
27 preset_ram (void)
28 {
29 for (uint8_t *p = RAMSTART; p <= (uint8_t *) RAMEND; p++)
30 *p = 0xdd;
31
32 }
33 #endif
34 /*--------------------------------------------------------------------------*/
35
36 static uint8_t mcusr;
37
38 static
39 void setup_avr(void)
40 {
41 /* save and clear reset reason(s) */
42 mcusr = MCUSR;
43 MCUSR = 0;
44
45 /* WD */
46
47 /* CPU */
48
49 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
50 MCUCR = _BV(JTD);
51 MCUCR = _BV(JTD);
52
53 /* disable unused periphels */
54 PRR0 = _BV(PRTIM2) | _BV(PRTIM0) | _BV(PRADC);
55 PRR1 = _BV(PRTIM5) | _BV(PRTIM4) | _BV(PRTIM3) |
56 _BV(PRUSART3) | _BV(PRUSART2) | _BV(PRUSART1);
57
58 /* disable analog comparator */
59 ACSR = _BV(ACD);
60 /* Ports */
61
62 /* Clock */
63 CLKPR = _BV(CLKPCE);
64 CLKPR = 0;
65
66 /* Timer */
67
68 OCR1A = F_CPU / 8 / 1000 - 1; // Timer1: 1000Hz interval (OC1A)
69 TCCR1B = 0b00001010;
70 TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
71 }
72
73 static const FLASH char * const FLASH rreasons[] = {
74 FSTR("Power on"),
75 FSTR("External"),
76 FSTR("Brown out"),
77 FSTR("Watchdog"),
78 FSTR("JTAG"),
79 };
80
81 static
82 void print_reset_reason(void)
83 {
84 uint8_t r = mcusr & 0x1f;
85 const FLASH char * const FLASH *p = rreasons;
86
87 printf_P(PSTR("Reset reason(s): %s"), r ? "" : "none");
88 for ( ; r; p++, r >>= 1) {
89 if (r & 1) {
90 my_puts_P(*p);
91 if (r & ~1)
92 printf_P(PSTR(", "));
93 }
94 }
95 printf_P(PSTR(".\n"));
96 }
97
98
99 /*******************************************************************************/
100
101 #define udelay(n) _delay_us(n)
102
103
104 /* Stored value of bootdelay, used by autoboot_command() */
105 static int stored_bootdelay;
106
107
108 /***************************************************************************
109 * Watch for 'delay' seconds for autoboot stop.
110 * returns: 0 - no key, allow autoboot
111 * 1 - got key, abort
112 */
113
114 static int abortboot(int bootdelay)
115 {
116 int abort = 0;
117 uint32_t ts;
118
119 if (bootdelay >= 0)
120 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay);
121
122 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
123 /*
124 * Check if key already pressed
125 * Don't check if bootdelay < 0
126 */
127 if (bootdelay >= 0) {
128 if (tstc()) { /* we got a key press */
129 (void) my_getchar(); /* consume input */
130 my_puts_P(PSTR("\b\b\b 0"));
131 abort = 1; /* don't auto boot */
132 }
133 }
134 #endif
135
136 while ((bootdelay > 0) && (!abort)) {
137 --bootdelay;
138 /* delay 1000 ms */
139 ts = get_timer(0);
140 do {
141 if (tstc()) { /* we got a key press */
142 abort = 1; /* don't auto boot */
143 bootdelay = 0; /* no more delay */
144 break;
145 }
146 udelay(10000);
147 } while (!abort && get_timer(ts) < 1000);
148
149 printf_P(PSTR("\b\b\b%2d "), bootdelay);
150 }
151
152 putchar('\n');
153
154 return abort;
155 }
156
157 static
158 const char *bootdelay_process(void)
159 {
160 char *s;
161 int bootdelay;
162
163 s = getenv("bootdelay");
164 bootdelay = s ? atoi(s) : CONFIG_BOOTDELAY;
165
166
167 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay);
168 _delay_ms(20);
169
170 s = getenv("bootcmd");
171 stored_bootdelay = bootdelay;
172 return s;
173 }
174
175 static
176 void autoboot_command(const char *s)
177 {
178 debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
179 _delay_ms(20);
180
181 if (stored_bootdelay != -1 && s && !abortboot(stored_bootdelay)) {
182 run_command_list(s, -1);
183 }
184 }
185
186
187 static
188 void main_loop(void)
189 {
190 const char *s;
191
192 s = bootdelay_process();
193 autoboot_command(s);
194 cli_loop();
195 }
196
197 int main(void)
198 {
199
200 setup_avr();
201 z80_setup_bus();
202
203 serial_setup();
204 sei();
205
206 #if DEBUG
207 debug("\n=========================< (RE)START DEBUG >=========================\n");
208 print_reset_reason();
209 #endif
210
211 env_init();
212 i2c_init(34920);
213
214 printf_P(PSTR("\n(ATMEGA1281+HD64180)_stamp Tester\n"));
215
216 main_loop();
217 }