]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
113b4f24310f2cea292503f9e274d7b6fbdba42a
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <avr/interrupt.h>
18 #include "con-utils.h"
23 #include "z180-serv.h"
29 uint8_t mcusr
__attribute__ ((section (".noinit")));
32 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
33 void preset_ram (void)
35 for (uint8_t *p
= RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
41 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
44 /* save and clear reset reason(s) */
45 /* TODO: move to init section? */
52 /*--------------------------------------------------------------------------*/
55 static const FLASH
char * const FLASH rreasons
[] = {
64 void print_reset_reason(void)
66 uint8_t r
= mcusr
& 0x1f;
67 const FLASH
char * const FLASH
*p
= rreasons
;
69 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
70 for ( ; r
; p
++, r
>>= 1) {
77 printf_P(PSTR(".\n"));
84 Stat
|= S_MSG_PENDING
;
89 Stat
|= S_CON_PENDING
;
97 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
101 /* Disable peripherals. Enable individually in respective init function. */
103 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
104 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
106 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
107 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
110 /* disable analog comparator */
119 PRR1
&= ~_BV(PRTIM3
);
120 OCR3A
= F_CPU
/ 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
121 TCCR3B
= (0b01<<WGM32
)|(0b001<<CS30
); /* CTC Mode, Prescaler 1 */
122 TIMSK3
= _BV(OCIE3A
); /* Enable TC2.oca interrupt */
124 /* INT5, INT6: falling edge */
125 EICRB
= (EICRB
& ~((0b11 << ISC50
) | (0b11 << ISC60
))) |
126 (0b10 << ISC50
) | (0b10 << ISC60
);
127 /* Reset pending ints */
128 EIFR
|= _BV(INTF5
) | _BV(INTF6
);
129 /* Enable INT5, and INT6 */
130 EIMSK
|= _BV(INT5
) | _BV(INT6
);
134 int reset_reason_is_power_on(void)
136 return (mcusr
& _BV(PORF
)) != 0;
140 void setup_system_time(void)
145 rtc_time
.tm_isdst
= 0;
146 set_system_time(mk_gmtime(&rtc_time
) );
151 static void setup_fatfs(void)
156 f_mount(&FatFs0
, "0:", 0);
157 f_mount(&FatFs1
, "1:", 0);
160 /*--------------------------------------------------------------------------*/
162 /* Stored value of bootdelay, used by autoboot_command() */
163 static int stored_bootdelay
;
166 /***************************************************************************
167 * Watch for 'delay' seconds for autoboot stop.
168 * returns: 0 - no key, allow autoboot
172 static int abortboot(int bootdelay
)
178 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
180 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
182 * Check if key already pressed
183 * Don't check if bootdelay < 0
185 if (bootdelay
>= 0) {
186 if (tstc()) { /* we got a key press */
187 (void) my_getchar(1); /* consume input */
188 my_puts_P(PSTR("\b\b\b 0"));
189 abort
= 1; /* don't auto boot */
194 while ((bootdelay
> 0) && (!abort
)) {
199 if (tstc()) { /* we got a key press */
200 abort
= 1; /* don't auto boot */
201 bootdelay
= 0; /* no more delay */
205 } while (!abort
&& get_timer(ts
) < 1000);
207 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
216 const char *bootdelay_process(void)
221 bootdelay
= (int) getenv_ulong(PSTR(ENV_BOOTDELAY
), 10, CONFIG_BOOTDELAY
);
224 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
227 s
= getenv_char(PSTR(ENV_BOOTCMD
));
228 stored_bootdelay
= bootdelay
;
233 void autoboot_command(const char *s
)
235 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: PSTR("<UNDEFINED>"));
238 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
239 run_command_list(s
, -1);
249 s
= bootdelay_process();
256 extern void setup_mmc(void);
258 for (int i
= 0; i
< GPIO_MAX
; i
++)
259 gpio_config(i
, INPUT_PULLUP
);
265 if (reset_reason_is_power_on())
266 _delay_ms(CONFIG_PWRON_DELAY
);
268 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE
), 10, CONFIG_BAUDRATE
));
272 debug("\n=========================< (RE)START DEBUG >=========================\n");
273 print_reset_reason();
276 i2c_init(CONFIG_SYS_I2C_CLOCK
);
280 printf_P(PSTR("\n" MCU_STRING
"+Z8S180 Stamp Monitor\n\n"));