Skip to content

Commit

Permalink
Implement TIMEOUT option.
Browse files Browse the repository at this point in the history
ATtiny412 working.
add RSTPIN option to control the (internal) FUSE values.
More source code "prettyfication"

Observation: in MPLABX, if you set the project linker memory options
for ".postapp=0x200", MPLAB will happily double those values before
actually passing them to the linker, because you must have meant WORDS, right?

Observation 2: The ATmega4809 definition in avrdude.conf is diffrent in the Arduino distribution than in the latest avrdude, specifically WRT whether the flash is word-addressed or byte-addressed :-(
  • Loading branch information
WestfW committed Sep 11, 2019
1 parent ad0f555 commit 775cb1f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
10 changes: 4 additions & 6 deletions optiboot/bootloaders/optiboot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,10 @@ UART_CMD = -DUARTTX=$(UARTTX)
endif


# Not supported yet
# ifdef TIMEOUT
# TIMEOUT_CMD = -DTIMEOUT_MS=$(TIMEOUT)
# dummy = FORCE
# endif
#
ifdef TIMEOUT
TIMEOUT_CMD = -DWDTTIME=TIMEOUT
dummy = FORCE
endif

.PRECIOUS: %.elf

Expand Down
19 changes: 16 additions & 3 deletions optiboot/bootloaders/optiboot/Makefile.extras
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ HELPTEXT += "target atmega4809 - ATmega4809\n"
atmega4809: TARGET = atmega4809
atmega4809: PROGRAM = optiboot_x
atmega4809: MCU_TARGET = atmega4809
atmega4809: CFLAGS += $(COMMON_OPTIONS) -DLED=A7 $(UART_CMD)
atmega4809: CFLAGS += $(COMMON_OPTIONS) -DLED=A7 $(UART_CMD) -DRSTPIN=1 -DTIMEOUT=1
atmega4809: AVR_FREQ ?= 20000000L
atmega4809: LDSECTIONS = -Wl,--section-start=.text=0 -Wl,--section-start=.version=0x1fe -Wl,--sections-start=.postapp=0x200
atmega4809: LDSECTIONS = -Wl,--section-start=.text=0 -Wl,--section-start=.version=0x1fe -Wl,--section-start=.postapp=0x200
atmega4809: $(PROGRAM)_atmega4809.hex
ifndef PRODUCTION
atmega4809: $(PROGRAM)_atmega4809.lst
Expand All @@ -151,10 +151,23 @@ HELPTEXT += "target Xplained416 - ATtiny416 Xplained Nano\n"
xplained416: TARGET = attiny416
xplained416: PROGRAM = optiboot_x
xplained416: MCU_TARGET = attiny416
xplained416: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) -DLED_INVERT=1
xplained416: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) -DLED_INVERT=1 -DTIMEOUT=8
xplained416: AVR_FREQ ?= 20000000L
xplained416: LDSECTIONS = -Wl,--section-start=.text=0 -Wl,--section-start=.version=0x1fe -Wl,--section-start=.postapp=0x200
xplained416: $(PROGRAM)_attiny416.hex
ifndef PRODUCTION
xplained416: $(PROGRAM)_attiny416.lst
endif

HELPTEXT += "target attiny412 - ATtiny4x2 boards by DrAzzy\n"
attiny412: TARGET = attiny412
attiny412: PROGRAM = optiboot_x
attiny412: MCU_TARGET = attiny412
attiny412: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) -DTIMEOUT=8
attiny412: AVR_FREQ ?= 20000000L
attiny412: LDSECTIONS = -Wl,--section-start=.text=0 -Wl,--section-start=.version=0x1fe -Wl,--section-start=.postapp=0x200
attiny412: $(PROGRAM)_attiny412.hex
ifndef PRODUCTION
attiny412: $(PROGRAM)_attiny412.lst
endif

77 changes: 49 additions & 28 deletions optiboot/bootloaders/optiboot/optiboot_x.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
*/

#if !defined(OPTIBOOT_CUSTOMVER)
#define OPTIBOOT_CUSTOMVER 0
# define OPTIBOOT_CUSTOMVER 0
#endif

unsigned const int __attribute__((section(".version"))) __attribute__((used))
Expand All @@ -141,7 +141,11 @@ FUSES = {
#ifdef FUSE_TCD0CFG_DEFAULT
.TCD0CFG = FUSE_TCD0CFG_DEFAULT, /* TCD0 Configuration */
#endif
.SYSCFG0 = 0xC4, /* RESET is not yet */
#ifdef RSTPIN
.SYSCFG0 = RSTPINCFG_RST_gc | CRCSRC_NOCRC_gc, /* RESET is enabled */
#else
.SYSCFG0 = CRCSRC_NOCRC_gc | RSTPINCFG_UPDI_gc, /* RESET is not yet */
#endif
.SYSCFG1 = 0x06, /* startup 32ms */
.APPEND = 0, /* Application Code Section End */
.BOOTEND = 2 /* Boot Section End */
Expand Down Expand Up @@ -175,52 +179,68 @@ typedef union {
#include "stk500.h"

#ifndef LED_START_FLASHES
#define LED_START_FLASHES 0
# define LED_START_FLASHES 0
#endif

/* set the UART baud rate defaults */
#ifndef BAUD_RATE
#define BAUD_RATE 115200L // Highest rate Avrdude win32 will support
# define BAUD_RATE 115200L // Highest rate Avrdude win32 will support
#endif

#define BAUD_SETTING ((F_CPU*4) / (BAUD_RATE))
#define BAUD_ACTUAL ((64L*F_CPU)/(16L*BAUD_SETTING))

#if BAUD_ACTUAL <= BAUD_RATE
#define BAUD_ERROR (( 100*(BAUD_RATE - BAUD_ACTUAL) ) / BAUD_RATE)
#if BAUD_ERROR >= 5
#error BAUD_RATE off by greater than -5%
#elif BAUD_ERROR >= 2 && !defined(PRODUCTION)
#warning BAUD_RATE off by greater than -2%
#endif
# define BAUD_ERROR (( 100*(BAUD_RATE - BAUD_ACTUAL) ) / BAUD_RATE)
# if BAUD_ERROR >= 5
# error BAUD_RATE off by greater than -5%
# elif BAUD_ERROR >= 2 && !defined(PRODUCTION)
# warning BAUD_RATE off by greater than -2%
# endif
#else
#define BAUD_ERROR (( 100*(BAUD_ACTUAL - BAUD_RATE) ) / BAUD_RATE)
#if BAUD_ERROR >= 5
#error BAUD_RATE off by greater than 5%
#elif BAUD_ERROR >= 2 && !defined(PRODUCTION)
#warning BAUD_RATE off by greater than 2%
#endif
# define BAUD_ERROR (( 100*(BAUD_ACTUAL - BAUD_RATE) ) / BAUD_RATE)
# if BAUD_ERROR >= 5
# error BAUD_RATE off by greater than 5%
# elif BAUD_ERROR >= 2 && !defined(PRODUCTION)
# warning BAUD_RATE off by greater than 2%
# endif
#endif

#if BAUD_SETTING > 65635
#error Unachievable baud rate (too slow) BAUD_RATE
# error Unachievable baud rate (too slow) BAUD_RATE
#endif // baud rate slow check
#if (BAUD_SETTING - 1) < 3
#if BAUD_ERROR != 0 // permit high bitrates (ie 1Mbps@16MHz) if error is zero
#error Unachievable baud rate (too fast) BAUD_RATE
#endif
# if BAUD_ERROR != 0 // permit high bitrates (ie 1Mbps@16MHz) if error is zero
# error Unachievable baud rate (too fast) BAUD_RATE
# endif
#endif // baud rate fast check

/*
* Watchdog timeout translations from human readable to config vals
*/
#ifndef WDTTIME
# define WDTPERIOD WDT_PERIOD_1KCLK_gc // 1 second
#elif WDTTIME == 1
# define WDTPERIOD WDT_PERIOD_1KCLK_gc // 1 second
#elif WDTTIME == 2
# define WDTPERIOD WDT_PERIOD_2KCLK_gc // 2 seconds
#elif WDTTIME == 4
# define WDTPERIOD WDT_PERIOD_4KCLK_gc // 4 seconds
#elif WDTTIME == 8
# define WDTPERIOD WDT_PERIOD_8KCLK_gc // 8 seconds
#else
#endif

/*
* We can never load flash with more than 1 page at a time, so we can save
* some code space on parts with smaller pagesize by using a smaller int.
*/
#if MAPPED_PROGMEM_PAGE_SIZE > 255
typedef uint16_t pagelen_t;
#define GETLENGTH(len) len = getch()<<8; len |= getch()
# define GETLENGTH(len) len = getch()<<8; len |= getch()
#else
typedef uint8_t pagelen_t;
#define GETLENGTH(len) (void) getch() /* skip high byte */; len = getch()
# define GETLENGTH(len) (void) getch() /* skip high byte */; len = getch()
#endif


Expand All @@ -240,11 +260,12 @@ void __attribute__((noinline)) verifySpace();
void __attribute__((noinline)) watchdogConfig(uint8_t x);

static void getNch(uint8_t);

#if LED_START_FLASHES > 0
static inline void flash_led(uint8_t);
#endif
#define watchdogReset() __asm__ __volatile__ ("wdr\n")

#define watchdogReset() __asm__ __volatile__ ("wdr\n")

/*
* RAMSTART should be self-explanatory. It's bigger on parts with a
Expand All @@ -258,8 +279,8 @@ static inline void flash_led(uint8_t);
/* everything that needs to run VERY early */
void pre_main (void) {
// Allow convenient way of calling do_spm function - jump table,
// so entry to this function will always be here, indepedent of compilation,
// features etc
// so entry to this function will always be here, indepedent
// of compilation, features, etc
__asm__ __volatile__ (
" rjmp 1f\n"
#ifndef APP_NOSPM
Expand All @@ -271,7 +292,6 @@ void pre_main (void) {
);
}


/* main program starts here */
int main (void) {
uint8_t ch;
Expand Down Expand Up @@ -363,8 +383,9 @@ int main (void) {
MYUART.CTRLA = 0; // Interrupts: all off
MYUART.CTRLB = USART_RXEN_bm | USART_TXEN_bm;

// Set up watchdog to trigger after 1s
watchdogConfig(WDT_PERIOD_8KCLK_gc);
// Set up watchdog to trigger after a bit
// (nominally:, 1s for autoreset, longer for manual)
watchdogConfig(WDTPERIOD);

#if (LED_START_FLASHES > 0) || defined(LED_DATA_FLASH) || defined(LED_START_ON)
/* Set LED pin as output */
Expand Down

0 comments on commit 775cb1f

Please sign in to comment.