1
0
mirror of https://github.com/Optiboot/optiboot.git synced 2025-08-17 21:41:03 +03:00

Modifications for ATtiny4x2, at least as far as compilation.

Eliminate RTC-based LED flashing code in favor of busy loop, which is significantly shorter now that peripherals are not in range of quick IO instructions.
Minor formatting changes while the code is young (notably spaces after the function name at the spot where they're actually defined, which makes them easier to search for.  Ancient corporate coding style guideline...)
This commit is contained in:
WestfW
2019-09-04 01:21:27 -07:00
parent bb9ae67aee
commit 58a71b130f
2 changed files with 55 additions and 27 deletions

View File

@@ -254,7 +254,7 @@ static inline void flash_led(uint8_t);
#endif #endif
/* everything that needs to run VERY early */ /* everything that needs to run VERY early */
void pre_main(void) { void pre_main (void) {
// Allow convenient way of calling do_spm function - jump table, // Allow convenient way of calling do_spm function - jump table,
// so entry to this function will always be here, indepedent of compilation, // so entry to this function will always be here, indepedent of compilation,
// features etc // features etc
@@ -271,7 +271,7 @@ void pre_main(void) {
/* main program starts here */ /* main program starts here */
int main(void) { int main (void) {
uint8_t ch; uint8_t ch;
/* /*
@@ -340,20 +340,12 @@ int main(void) {
} }
} }
#if LED_START_FLASHES > 0
// Set up RTC counting at about 1/8s (input is 32kHz)
while (RTC.STATUS & RTC_CTRLABUSY_bm)
; // RTC is used by startup logic (!) and might be busy.
RTC.CTRLA= RTC_PRESCALER_DIV4096_gc | RTC_RTCEN_bm;
RTC.DBGCTRL = 1; // enable during debug
#endif
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0); // full speed clock _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0); // full speed clock
MYUART_TXPORT.DIR |= MYUART_TXPIN; // set TX pin to output MYUART_TXPORT.DIR |= MYUART_TXPIN; // set TX pin to output
MYUART_TXPORT.OUT |= MYUART_TXPIN; // and "1" as per datasheet MYUART_TXPORT.OUT |= MYUART_TXPIN; // and "1" as per datasheet
#if defined (MYUART_PMUX) #if defined (MYUART_PMUX)
PORTMUX.USARTROUTEA |= MYUART_PMUX; // alternate pinout to use MYPMUX |= MYUART_PMUX; // alternate pinout to use
#endif #endif
MYUART.BAUD = BAUD_SETTING; MYUART.BAUD = BAUD_SETTING;
MYUART.DBGCTRL = 1; // run during debug MYUART.DBGCTRL = 1; // run during debug
@@ -361,8 +353,8 @@ int main(void) {
MYUART.CTRLA = 0; // Interrupts: all off MYUART.CTRLA = 0; // Interrupts: all off
MYUART.CTRLB = USART_RXEN_bm | USART_TXEN_bm; MYUART.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
// Set up watchdog to trigger after 1s // Set up watchdog to trigger after 8s
// watchdogConfig(WDT_PERIOD_1KCLK_gc); watchdogConfig(WDT_PERIOD_8KCLK_gc);
#if (LED_START_FLASHES > 0) || defined(LED_DATA_FLASH) || defined(LED_START_ON) #if (LED_START_FLASHES > 0) || defined(LED_DATA_FLASH) || defined(LED_START_ON)
/* Set LED pin as output */ /* Set LED pin as output */
@@ -431,7 +423,7 @@ int main(void) {
} }
/* Write memory, length is big endian and is in bytes */ /* Write memory, length is big endian and is in bytes */
else if(ch == STK_PROG_PAGE) { else if(ch == STK_PROG_PAGE) {
// PROGRAM PAGE - we support flash programming only, not EEPROM // PROGRAM PAGE - any kind of page!
uint8_t desttype; uint8_t desttype;
GETLENGTH(length); GETLENGTH(length);
@@ -442,6 +434,7 @@ int main(void) {
} else { } else {
address.word += MAPPED_EEPROM_START; address.word += MAPPED_EEPROM_START;
} }
// TODO: user row?
do { do {
*(address.bptr++) = getch(); *(address.bptr++) = getch();
@@ -469,6 +462,8 @@ int main(void) {
} else { } else {
address.word += MAPPED_EEPROM_START; address.word += MAPPED_EEPROM_START;
} }
// TODO: user row?
do { do {
putch(*(address.bptr++)); putch(*(address.bptr++));
} while (--length); } while (--length);
@@ -495,13 +490,13 @@ int main(void) {
} }
} }
void putch(char ch) { void putch (char ch) {
while (0 == (MYUART.STATUS & USART_DREIF_bm)) while (0 == (MYUART.STATUS & USART_DREIF_bm))
; ;
MYUART.TXDATAL = ch; MYUART.TXDATAL = ch;
} }
uint8_t getch(void) { uint8_t getch (void) {
uint8_t ch, flags; uint8_t ch, flags;
while (!(MYUART.STATUS & USART_RXCIF_bm)) while (!(MYUART.STATUS & USART_RXCIF_bm))
; ;
@@ -516,12 +511,12 @@ uint8_t getch(void) {
return ch; return ch;
} }
void getNch(uint8_t count) { void getNch (uint8_t count) {
do getch(); while (--count); do getch(); while (--count);
verifySpace(); verifySpace();
} }
void verifySpace() { void verifySpace () {
if (getch() != CRC_EOP) { if (getch() != CRC_EOP) {
watchdogConfig(WDT_PERIOD_8CLK_gc); // shorten WD timeout watchdogConfig(WDT_PERIOD_8CLK_gc); // shorten WD timeout
while (1) // and busy-loop so that WD causes while (1) // and busy-loop so that WD causes
@@ -531,12 +526,16 @@ void verifySpace() {
} }
#if LED_START_FLASHES > 0 #if LED_START_FLASHES > 0
void flash_led(uint8_t count) { void flash_led (uint8_t count) {
uint8_t last; #ifdef __INT24_MAX__
__uint24 delay;
#else
uint32_t delay;
# warning no uin24
#endif
while (count--) { while (count--) {
LED_PORT.IN |= LED; LED_PORT.IN |= LED;
last = RTC.CNTL & 1; for (delay = (F_CPU/200); delay; delay--) {
while ((RTC.CNTL & 1) == last) {
watchdogReset(); watchdogReset();
if (MYUART.STATUS & USART_RXCIF_bm) if (MYUART.STATUS & USART_RXCIF_bm)
return; return;
@@ -545,13 +544,11 @@ void flash_led(uint8_t count) {
} }
#endif #endif
void watchdogConfig(uint8_t x) { void watchdogConfig (uint8_t x) {
_PROTECTED_WRITE(WDT.CTRLA, x); _PROTECTED_WRITE(WDT.CTRLA, x);
} }
#ifndef APP_NOSPM #ifndef APP_NOSPM
/* /*
@@ -572,7 +569,7 @@ void watchdogConfig(uint8_t x) {
* data=0 in WRITE * data=0 in WRITE
*/ */
static void do_nvmctrl(uint16_t address, uint8_t command, uint16_t data) __attribute__ ((used)); static void do_nvmctrl(uint16_t address, uint8_t command, uint16_t data) __attribute__ ((used));
static void do_nvmctrl(uint16_t address, uint8_t command, uint16_t data) { static void do_nvmctrl (uint16_t address, uint8_t command, uint16_t data) {
// Do spm stuff // Do spm stuff
} }
#endif #endif

View File

@@ -670,6 +670,7 @@
#define USART_ALTPMUX 1 #define USART_ALTPMUX 1
#if defined(__AVR_ATmega4809__) #if defined(__AVR_ATmega4809__)
#define MYPMUX PORTMUX.USARTROUTEA
# if (UARTTX == A0) # if (UARTTX == A0)
# ifndef USART0 # ifndef USART0
# error Pin on USART0, but no USART0 exists # error Pin on USART0, but no USART0 exists
@@ -731,7 +732,37 @@
# define MYUART_TXPIN (1<<PORT4) # define MYUART_TXPIN (1<<PORT4)
# define MYUART_PMUX (USART_ALTPMUX<<PORTMUX_USART2_gp) # define MYUART_PMUX (USART_ALTPMUX<<PORTMUX_USART2_gp)
# endif # endif
#endif // ATmega4809 #endif // ATmega4809
/*
* 8pin Tiny0 and Tiny1
*/
#if defined(__AVR_ATtiny402__) || defined(__AVR_ATtiny202__) || \
defined(__AVR_ATtiny412__) || defined(__AVR_ATtiny212__)
#define MYPMUX PORTMUX.CTRLB
# if (UARTTX == A6)
# ifndef USART0
# error Pin on USART0, but no USART0 exists
# endif
# define MYUART USART0
# define MYUART_TXPORT VPORTA
# define MYUART_TXPIN (1<<PORT0)
# elif (UARTTX == A1)
# ifndef USART0
# error Pin on USART0, but no USART0 exists
# endif
# define MYUART USART0
# define MYUART_TXPORT VPORTA
# define MYUART_TXPIN (1<<PORT4)
# define MYUART_PMUX (USART_ALTPMUX)
# endif
#endif // Tiny402/etc
#if defined(__ATtiny3216__) || defined(__ATtiny1606__)
#endif
#if defined(__ATtiny1614__) || defined(__ATtiny1604__)
#endif
#ifndef MYUART #ifndef MYUART
# warning No UARTTX pin specified. # warning No UARTTX pin specified.