]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/main.c
7 #include <avr/interrupt.h>
15 #include "con-utils.h"
20 #include "z180-serv.h"
28 /*--------------------------------------------------------------------------*/
31 __attribute__ ((naked
)) __attribute__ ((section (".init3")))
32 void preset_ram (void)
34 for (uint8_t *p
= RAMSTART
; p
<= (uint8_t *) RAMEND
; p
++)
39 static const FLASH
char * const FLASH rreasons
[] = {
48 void print_reset_reason(void)
50 uint8_t r
= mcusr
& 0x1f;
51 const FLASH
char * const FLASH
*p
= rreasons
;
53 printf_P(PSTR("### Reset reason(s): %s"), r
? "" : "none");
54 for ( ; r
; p
++, r
>>= 1) {
61 printf_P(PSTR(".\n"));
68 Stat
|= S_MSG_PENDING
;
73 Stat
|= S_CON_PENDING
;
79 /* save and clear reset reason(s) */
80 /* TODO: move to init section? */
88 /* Disable JTAG Interface regardless of the JTAGEN fuse setting. */
92 /* Disable peripherals. Enable individually in respective init function. */
94 _BV(PRTIM2
) | _BV(PRTIM0
) | _BV(PRTIM1
) |
95 _BV(PRSPI
) | _BV(PRUSART0
) | _BV(PRADC
);
97 PRR1
= _BV(PRTIM5
) | _BV(PRTIM4
) | _BV(PRTIM3
) |
98 _BV(PRUSART3
) | _BV(PRUSART2
) | _BV(PRUSART1
);
101 /* disable analog comparator */
110 PRR1
&= ~_BV(PRTIM3
);
111 OCR3A
= F_CPU
/ 1000 - 1; /* Timer3: 1000Hz interval (OC3A) */
112 TCCR3B
= (0b01<<WGM32
)|(0b001<<CS30
); /* CTC Mode, Prescaler 1 */
113 TIMSK3
= _BV(OCIE3A
); /* Enable TC2.oca interrupt */
117 SPI_DDR
= (SPI_DDR
& ~_BV(SPI_MISO
))
118 | _BV(SPI_MOSI
) | _BV(SPI_SCK
) | _BV(SPI_SS
);
119 SPI_PORT
= (SPI_PORT
& ~(_BV(SPI_MOSI
) | _BV(SPI_SCK
)))
122 /* INT5, INT6: falling edge */
123 EICRB
= (EICRB
& ~((0b11 << ISC50
) | (0b11 << ISC60
))) |
124 (0b10 << ISC50
) | (0b10 << ISC60
);
125 /* Reset pending ints */
126 EIFR
|= _BV(INTF5
) | _BV(INTF6
);
127 /* Enable INT5, and INT6 */
128 EIMSK
|= _BV(INT5
) | _BV(INT6
);
132 int reset_reason_is_power_on(void)
134 return (mcusr
& _BV(PORF
)) != 0;
138 void setup_system_time(void)
143 rtc_time
.tm_isdst
= 0;
144 set_system_time(mk_gmtime(&rtc_time
) );
147 /*--------------------------------------------------------------------------*/
149 /* Stored value of bootdelay, used by autoboot_command() */
150 static int stored_bootdelay
;
153 /***************************************************************************
154 * Watch for 'delay' seconds for autoboot stop.
155 * returns: 0 - no key, allow autoboot
159 static int abortboot(int bootdelay
)
165 printf_P(PSTR("Hit any key to stop autoboot: %2d "), bootdelay
);
167 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
169 * Check if key already pressed
170 * Don't check if bootdelay < 0
172 if (bootdelay
>= 0) {
173 if (tstc()) { /* we got a key press */
174 (void) my_getchar(1); /* consume input */
175 my_puts_P(PSTR("\b\b\b 0"));
176 abort
= 1; /* don't auto boot */
181 while ((bootdelay
> 0) && (!abort
)) {
186 if (tstc()) { /* we got a key press */
187 abort
= 1; /* don't auto boot */
188 bootdelay
= 0; /* no more delay */
192 } while (!abort
&& get_timer(ts
) < 1000);
194 printf_P(PSTR("\b\b\b%2d "), bootdelay
);
203 const char *bootdelay_process(void)
208 bootdelay
= (int) getenv_ulong(PSTR(ENV_BOOTDELAY
), 10, CONFIG_BOOTDELAY
);
211 debug("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
214 s
= getenv(PSTR(ENV_BOOTCMD
));
215 stored_bootdelay
= bootdelay
;
220 void autoboot_command(const char *s
)
222 debug("### main_loop: bootcmd=\"%s\"\n", s
? s
: PSTR("<UNDEFINED>"));
225 if (stored_bootdelay
!= -1 && s
&& !abortboot(stored_bootdelay
)) {
226 run_command_list(s
, -1);
236 s
= bootdelay_process();
245 for (int i
= 0; i
< GPIO_MAX
; i
++)
246 gpio_config(i
, INPUT_PULLUP
);
249 if (reset_reason_is_power_on())
250 _delay_ms(CONFIG_PWRON_DELAY
);
252 serial_setup(getenv_ulong(PSTR(ENV_BAUDRATE
), 10, CONFIG_BAUDRATE
));
256 debug("\n=========================< (RE)START DEBUG >=========================\n");
257 print_reset_reason();
261 unsigned long i_speed
= getenv_ulong(PSTR("i2c_clock"), 10, CONFIG_SYS_I2C_CLOCK
);
262 debug("### Setting I2C clock Frequency to %lu Hz.\n", i_speed
);
265 i2c_init(CONFIG_SYS_I2C_CLOCK
);
269 printf_P(PSTR("\nATMEGA1281+Z8S180 Stamp Monitor\n\n"));