]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <avr/interrupt.h>
17 #include "con-utils.h"
22 #include "z180-serv.h"
29 /*--------------------------------------------------------------------------*/
32 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
33 void preset_ram (void)
35 for (uint8_t *p
= RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
40 static const FLASH
char * const FLASH rreasons
[] = {
49 void print_reset_reason(void)
51 uint8_t r
= mcusr
& 0x1f;
52 const FLASH
char * const FLASH
*p
= rreasons
;
54 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
55 for ( ; r
; p
++, r
>>= 1) {
62 printf_P(PSTR(".\n"));
69 Stat
|= S_MSG_PENDING
;
74 Stat
|= S_CON_PENDING
;
80 /* save and clear reset reason(s) */
81 /* TODO: move to init section? */
89 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
93 /* Disable peripherals. Enable individually in respective init function. */
95 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
96 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
98 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
99 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
102 /* disable analog comparator */
111 PRR1
&= ~_BV(PRTIM3
);
112 OCR3A
= F_CPU
/ 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
113 TCCR3B
= (0b01<<WGM32
)|(0b001<<CS30
); /* CTC Mode, Prescaler 1 */
114 TIMSK3
= _BV(OCIE3A
); /* Enable TC2.oca interrupt */
116 /* INT5, INT6: falling edge */
117 EICRB
= (EICRB
& ~((0b11 << ISC50
) | (0b11 << ISC60
))) |
118 (0b10 << ISC50
) | (0b10 << ISC60
);
119 /* Reset pending ints */
120 EIFR
|= _BV(INTF5
) | _BV(INTF6
);
121 /* Enable INT5, and INT6 */
122 EIMSK
|= _BV(INT5
) | _BV(INT6
);
126 int reset_reason_is_power_on(void)
128 return (mcusr
& _BV(PORF
)) != 0;
132 void setup_system_time(void)
137 rtc_time
.tm_isdst
= 0;
138 set_system_time(mk_gmtime(&rtc_time
) );
141 /*--------------------------------------------------------------------------*/
143 /* Stored value of bootdelay, used by autoboot_command() */
144 static int stored_bootdelay
;
147 /***************************************************************************
148 * Watch for 'delay' seconds for autoboot stop.
149 * returns: 0 - no key, allow autoboot
153 static int abortboot(int bootdelay
)
159 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
161 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
163 * Check if key already pressed
164 * Don't check if bootdelay < 0
166 if (bootdelay
>= 0) {
167 if (tstc()) { /* we got a key press */
168 (void) my_getchar(1); /* consume input */
169 my_puts_P(PSTR("\b\b\b 0"));
170 abort
= 1; /* don't auto boot */
175 while ((bootdelay
> 0) && (!abort
)) {
180 if (tstc()) { /* we got a key press */
181 abort
= 1; /* don't auto boot */
182 bootdelay
= 0; /* no more delay */
186 } while (!abort
&& get_timer(ts
) < 1000);
188 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
197 const char *bootdelay_process(void)
202 bootdelay
= (int) getenv_ulong(PSTR(ENV_BOOTDELAY
), 10, CONFIG_BOOTDELAY
);
205 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
208 s
= getenv(PSTR(ENV_BOOTCMD
));
209 stored_bootdelay
= bootdelay
;
214 void autoboot_command(const char *s
)
216 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: PSTR("<UNDEFINED>"));
219 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
220 run_command_list(s
, -1);
230 s
= bootdelay_process();
237 extern void setup_mmc(void);
239 for (int i
= 0; i
< GPIO_MAX
; i
++)
240 gpio_config(i
, INPUT_PULLUP
);
246 if (reset_reason_is_power_on())
247 _delay_ms(CONFIG_PWRON_DELAY
);
249 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE
), 10, CONFIG_BAUDRATE
));
253 debug("\n=========================< (RE)START DEBUG >=========================\n");
254 print_reset_reason();
258 unsigned long i_speed
= getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK
);
259 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed
);
262 i2c_init(CONFIG_SYS_I2C_CLOCK
);
266 printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));