From 0e3f40e062b1dca5be7ae7bceec763c417d7b6f1 Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Sat, 21 Mar 2015 18:16:08 +1100 Subject: [PATCH 1/9] Add XMEGA E5 processor definitions (boot section start addresses) to Makefile. --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 34a703a..fde56b7 100644 --- a/Makefile +++ b/Makefile @@ -531,10 +531,13 @@ endif # Sizes in bytes, not works (datasheet generally in words) # xmega MCU_S = $(subst atxmega,x,$(MCU)) -ifneq ($(filter $(MCU_S), x16a4 x16d4 x16a4u),) +ifneq ($(filter $(MCU_S), x8e5),) + BOOT_SECTION_START =0x002000 +endif +ifneq ($(filter $(MCU_S), x16a4 x16d4 x16a4u x16e5),) BOOT_SECTION_START =0x004000 endif -ifneq ($(filter $(MCU_S), x32a4 x32d4 x32a4u),) +ifneq ($(filter $(MCU_S), x32a4 x32d4 x32a4u x32e5),) BOOT_SECTION_START =0x008000 endif ifneq ($(filter $(MCU_S), x64a1 x64a3 x64a4 x64d3 x64d4 x64a3u x64a4u x64b1 x64b3),) From 5a92799f2bda181889a22b5092d7802e57986733 Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Sat, 21 Mar 2015 18:20:12 +1100 Subject: [PATCH 2/9] Ensure 'make' gets called correctly if there are spaces in the path. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index fde56b7..8a57880 100644 --- a/Makefile +++ b/Makefile @@ -776,7 +776,7 @@ sym: $(TARGET).sym # Configuration support %.conf %.conf.mk: force cp $@ config.mk - $(MAKE) + "$(MAKE)" -include config.h.mk From 81eeb1038bd576d820e56befe70c9ff6ea6bb5ce Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Sat, 21 Mar 2015 18:32:45 +1100 Subject: [PATCH 3/9] Remove USE_DFLL define from xboot.h - this define will instead be set via config.h (this is because the XMEGA E5 does not have DFLL calibration for the 2 MHz clock). --- xboot.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/xboot.h b/xboot.h index 9449a0e..6b7611a 100644 --- a/xboot.h +++ b/xboot.h @@ -58,8 +58,6 @@ // clock config #ifdef __AVR_XMEGA__ -// DFLL for better stability -#define USE_DFLL // use 32MHz osc if makefile calls for it #if (F_CPU == 32000000L) // defaults to 2MHz RC oscillator From 2ebeaafa94189d7839cf3a76f93f63ec049075bc Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Sat, 21 Mar 2015 18:36:24 +1100 Subject: [PATCH 4/9] Add USE_DFLL setting to sample configs. --- conf/akafuino32a4.conf.mk | 3 +++ conf/arduino328p.conf.mk | 3 +++ conf/mongoose.conf.mk | 3 +++ conf/x32a4.conf.mk | 3 +++ conf/x64a3.conf.mk | 3 +++ 5 files changed, 15 insertions(+) diff --git a/conf/akafuino32a4.conf.mk b/conf/akafuino32a4.conf.mk index 7716c07..7ded790 100644 --- a/conf/akafuino32a4.conf.mk +++ b/conf/akafuino32a4.conf.mk @@ -11,6 +11,9 @@ MCU = atxmega32a4 # Use 2 MHz internal RC oscillator F_CPU = 2000000 +# DFLL for better stability +USE_DFLL = yes + # Programmer settings OVERRIDE_AVRDUDE_PROGRAMMER = yes AVRDUDE_PROGRAMMER = jtag2pdi diff --git a/conf/arduino328p.conf.mk b/conf/arduino328p.conf.mk index 9f75474..2bf1df2 100644 --- a/conf/arduino328p.conf.mk +++ b/conf/arduino328p.conf.mk @@ -24,6 +24,9 @@ BOOTSZ=0 # Use 16 MHz external crystal oscillator F_CPU = 16000000 +# DFLL for better stability +USE_DFLL = yes + # Programmer settings OVERRIDE_AVRDUDE_PROGRAMMER = yes AVRDUDE_PROGRAMMER = jtag2isp diff --git a/conf/mongoose.conf.mk b/conf/mongoose.conf.mk index 1f6aea5..8fdc2e4 100644 --- a/conf/mongoose.conf.mk +++ b/conf/mongoose.conf.mk @@ -24,6 +24,9 @@ BOOTSZ=0 # Use 16 MHz external crystal oscillator F_CPU = 16000000 +# DFLL for better stability +USE_DFLL = yes + # Programmer settings OVERRIDE_AVRDUDE_PROGRAMMER = yes AVRDUDE_PROGRAMMER = jtag2isp diff --git a/conf/x32a4.conf.mk b/conf/x32a4.conf.mk index 9481cea..6a977dd 100644 --- a/conf/x32a4.conf.mk +++ b/conf/x32a4.conf.mk @@ -10,6 +10,9 @@ MCU = atxmega32a4 # Use 2 MHz internal RC oscillator F_CPU = 2000000 +# DFLL for better stability +USE_DFLL = yes + # Programmer settings OVERRIDE_AVRDUDE_PROGRAMMER = yes AVRDUDE_PROGRAMMER = jtag2pdi diff --git a/conf/x64a3.conf.mk b/conf/x64a3.conf.mk index 6a32b68..22e6178 100644 --- a/conf/x64a3.conf.mk +++ b/conf/x64a3.conf.mk @@ -10,6 +10,9 @@ MCU = atxmega64a3 # Use 2 MHz internal RC oscillator F_CPU = 2000000 +# DFLL for better stability +USE_DFLL = yes + # Programmer settings OVERRIDE_AVRDUDE_PROGRAMMER = yes AVRDUDE_PROGRAMMER = jtag2pdi From 9829ddd3a693087ed91b55e388546c79d3235681 Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Sun, 22 Mar 2015 22:31:15 +1100 Subject: [PATCH 5/9] Fix EEPROM driver compilation. --- eeprom_driver.c | 124 ------------------------------------------------ eeprom_driver.h | 20 +------- 2 files changed, 1 insertion(+), 143 deletions(-) diff --git a/eeprom_driver.c b/eeprom_driver.c index fc7f31d..14e67c9 100644 --- a/eeprom_driver.c +++ b/eeprom_driver.c @@ -118,130 +118,6 @@ void wait_for_nvm(void) while (NVM.STATUS & NVM_NVMBUSY_bm) { }; } -void flush_buffer(void) -{ - wait_for_nvm(); - - if ((NVM.STATUS & NVM_EELOAD_bm) != 0) { - NVM.CMD = NVM_CMD_ERASE_EEPROM_BUFFER_gc; - NVM_EXEC(); - } -} - - -uint8_t EEPROM_read_byte(uint16_t addr) -{ - wait_for_nvm(); - - NVM.ADDR0 = addr & 0xFF; - NVM.ADDR1 = (addr >> 8) & 0x1F; - NVM.ADDR2 = 0; - - NVM.CMD = NVM_CMD_READ_EEPROM_gc; - NVM_EXEC(); - - return NVM.DATA0; -} - - -void EEPROM_write_byte(uint16_t addr, uint8_t byte) -{ - flush_buffer(); - NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc; - - NVM.ADDR0 = addr & 0xFF; - NVM.ADDR1 = (addr >> 8) & 0x1F; - NVM.ADDR2 = 0; - - NVM.DATA0 = byte; - - NVM.CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc; - NVM_EXEC_WRAPPER(); -} - - -uint16_t EEPROM_read_block(uint16_t addr, uint8_t *dest, uint16_t len) -{ - uint16_t cnt = 0; - - NVM.ADDR2 = 0; - - wait_for_nvm(); - - while (len > 0) - { - NVM.ADDR0 = addr & 0xFF; - NVM.ADDR1 = (addr >> 8) & 0x1F; - - NVM.CMD = NVM_CMD_READ_EEPROM_gc; - NVM_EXEC(); - - *(dest++) = NVM.DATA0; addr++; - - len--; cnt++; - } - return cnt; -} - - -uint16_t EEPROM_write_block(uint16_t addr, const uint8_t *src, uint16_t len) -{ - uint8_t byte_addr = addr % EEPROM_PAGE_SIZE; - uint16_t page_addr = addr - byte_addr; - uint16_t cnt = 0; - - flush_buffer(); - wait_for_nvm(); - NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc; - - NVM.ADDR1 = 0; - NVM.ADDR2 = 0; - - while (len > 0) - { - NVM.ADDR0 = byte_addr; - - NVM.DATA0 = *(src++); - - byte_addr++; - len--; - - if (len == 0 || byte_addr >= EEPROM_PAGE_SIZE) - { - NVM.ADDR0 = page_addr & 0xFF; - NVM.ADDR1 = (page_addr >> 8) & 0x1F; - - NVM.CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc; - NVM_EXEC(); - - page_addr += EEPROM_PAGE_SIZE; - byte_addr = 0; - - wait_for_nvm(); - - NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc; - } - - cnt++; - } - - return cnt; -} - - -void EEPROM_erase_page(uint16_t addr) -{ - NVM.ADDR0 = addr & 0xFF; - NVM.ADDR1 = (addr >> 8) & 0x1F; - NVM.ADDR2 = 0; - - wait_for_nvm(); - - NVM.CMD = NVM_CMD_ERASE_EEPROM_PAGE_gc; - NVM_EXEC_WRAPPER(); -} - - void EEPROM_erase_all(void) { wait_for_nvm(); diff --git a/eeprom_driver.h b/eeprom_driver.h index dc4b392..4c04938 100644 --- a/eeprom_driver.h +++ b/eeprom_driver.h @@ -34,10 +34,7 @@ #include #include - -#ifndef __AVR_XMEGA__ #include -#endif // __AVR_XMEGA__ #include "xboot.h" @@ -52,17 +49,6 @@ // Prototypes -#ifdef __AVR_XMEGA__ - -uint8_t EEPROM_read_byte(uint16_t addr); -void EEPROM_write_byte(uint16_t addr, uint8_t byte); -uint16_t EEPROM_read_block(uint16_t addr, uint8_t *dest, uint16_t len); -uint16_t EEPROM_write_block(uint16_t addr, const uint8_t *src, uint16_t len); - -void EEPROM_erase_page(uint16_t addr); - -#else // __AVR_XMEGA__ - #define EEPROM_read_byte(addr) eeprom_read_byte((const uint8_t *)((uint16_t)(addr))) #define EEPROM_write_byte(addr, value) eeprom_write_byte((uint8_t *)((uint16_t)(addr)), (value)) #define EEPROM_read_block(addr, dest, len) eeprom_read_block((dest), (void *)((uint16_t)(addr)), (len)) @@ -73,12 +59,8 @@ uint8_t enter_eeprom_check(void); void enter_eeprom_reset(void); #endif // USE_ENTER_EEPROM -#endif // __AVR_XMEGA__ void EEPROM_erase_all(void); -#endif // __EEPROM_DRIVER_H - - - +#endif // __EEPROM_DRIVER_H \ No newline at end of file From 0099a93bd837fab265c0a0edaae47ade0ab82803 Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Mon, 6 Apr 2015 23:23:14 +1000 Subject: [PATCH 6/9] Add configuration setting to remap UART pins (for XMEGA E5). --- conf/akafuino32a4.conf.mk | 1 + conf/arduino328p.conf.mk | 1 + conf/mongoose.conf.mk | 1 + conf/x32a4.conf.mk | 1 + conf/x64a3.conf.mk | 1 + xboot.c | 23 +++++++++++++++++++++-- 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/conf/akafuino32a4.conf.mk b/conf/akafuino32a4.conf.mk index 7ded790..71f9f4e 100644 --- a/conf/akafuino32a4.conf.mk +++ b/conf/akafuino32a4.conf.mk @@ -166,6 +166,7 @@ UART_BAUD_RATE = 115200 UART_PORT_NAME = C UART_NUMBER = 0 UART_RX_PUEN = yes +UART_REMAP = no # UART RS485 Enable Output UART_EN_PORT_NAME = C diff --git a/conf/arduino328p.conf.mk b/conf/arduino328p.conf.mk index 2bf1df2..9699193 100644 --- a/conf/arduino328p.conf.mk +++ b/conf/arduino328p.conf.mk @@ -141,6 +141,7 @@ UART_BAUD_RATE = 115200 UART_NUMBER = 0 UART_U2X = yes UART_RX_PUEN = yes +UART_REMAP = no # UART RS485 Enable Output UART_EN_PORT_NAME = C diff --git a/conf/mongoose.conf.mk b/conf/mongoose.conf.mk index 8fdc2e4..4e80654 100644 --- a/conf/mongoose.conf.mk +++ b/conf/mongoose.conf.mk @@ -137,6 +137,7 @@ UART_BAUD_RATE = 115200 UART_NUMBER = 0 UART_U2X = yes UART_RX_PUEN = yes +UART_REMAP = no # UART RS485 Enable Output UART_EN_PORT_NAME = C diff --git a/conf/x32a4.conf.mk b/conf/x32a4.conf.mk index 6a977dd..e4b1eff 100644 --- a/conf/x32a4.conf.mk +++ b/conf/x32a4.conf.mk @@ -165,6 +165,7 @@ UART_BAUD_RATE = 115200 UART_PORT_NAME = C UART_NUMBER = 0 UART_RX_PUEN = yes +UART_REMAP = no # UART RS485 Enable Output UART_EN_PORT_NAME = C diff --git a/conf/x64a3.conf.mk b/conf/x64a3.conf.mk index 22e6178..81ba655 100644 --- a/conf/x64a3.conf.mk +++ b/conf/x64a3.conf.mk @@ -165,6 +165,7 @@ UART_BAUD_RATE = 115200 UART_PORT_NAME = D UART_NUMBER = 1 UART_RX_PUEN = yes +UART_REMAP = no # UART RS485 Enable Output UART_EN_PORT_NAME = C diff --git a/xboot.c b/xboot.c index 6bc79e2..9d878d6 100644 --- a/xboot.c +++ b/xboot.c @@ -238,7 +238,7 @@ int main(void) // Enable RX pin pullup UART_RX_PIN_CTRL = 0x18; #endif // UART_RX_PUEN - + #else // __AVR_XMEGA__ #ifdef UART_RX_PUEN @@ -246,6 +246,17 @@ int main(void) UART_PORT |= (1 << UART_RX_PIN); #endif // UART_RX_PUEN +#endif // __AVR_XMEGA__ + + // Remap UART pins + +#ifdef __AVR_XMEGA__ + + #ifdef UART_REMAP + // Remap UART pins + UART_PORT.REMAP |= (1 << PORT_USART0_bp); + #endif // UART_REMAP + #endif // __AVR_XMEGA__ // Initialize UART EN pin @@ -932,7 +943,15 @@ int main(void) UART_PORT &= ~(1 << UART_RX_PIN); #endif // UART_RX_PUEN #endif // __AVR_XMEGA__ - + + // Remove UART remap +#ifdef __AVR_XMEGA__ + #ifdef UART_REMAP + // Remove UART remap + UART_PORT.REMAP &= ~(1 << PORT_USART0_bp); + #endif // UART_REMAP +#endif // __AVR_XMEGA__ + // Shut down UART EN pin #ifdef USE_UART_EN_PIN #ifdef __AVR_XMEGA__ From 1f0dc48c71d96eb18b5c68fc181f531470192342 Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Tue, 7 Apr 2015 13:13:54 +1000 Subject: [PATCH 7/9] Ensure correct UART pins are used when UART remap is enabled (for XMEGA E5). --- xboot.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xboot.h b/xboot.h index 6b7611a..d306db4 100644 --- a/xboot.h +++ b/xboot.h @@ -238,8 +238,13 @@ #ifdef __AVR_XMEGA__ #if (UART_NUMBER == 0) +#ifdef UART_REMAP +#define UART_RX_PIN 6 +#define UART_TX_PIN 7 +#else #define UART_RX_PIN 2 #define UART_TX_PIN 3 +#endif #else #define UART_RX_PIN 6 #define UART_TX_PIN 7 From 577d7dbd924ae7707850ec85b8ea755f6fe237d7 Mon Sep 17 00:00:00 2001 From: Nick D'Ademo Date: Thu, 16 Apr 2015 17:29:14 +1000 Subject: [PATCH 8/9] XMEGA E5: ERASE_APP NVM command (0x20) erases the entire flash - as a workaround, we erase page-by-page. From Atmel Support: "The NVM controller design is such that the entire flash will get erased always when application/bootloader erase is called." --- xboot.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/xboot.c b/xboot.c index 9d878d6..27d8f23 100644 --- a/xboot.c +++ b/xboot.c @@ -494,19 +494,40 @@ int main(void) else if (val == CMD_CHIP_ERASE) { // Erase the application section - Flash_EraseApplicationSection(); + // XMEGA E5: ERASE_APP NVM command (0x20) erases the entire flash - as a workaround, we erase page-by-page. + // From Atmel Support: "The NVM controller design is such that the entire flash will get erased always when application/bootloader erase is called." + #if defined(__AVR_ATxmega8E5__) || defined(__AVR_ATxmega16E5__) || defined(__AVR_ATxmega32E5__) + for(uint32_t addr = APP_SECTION_START; addr < APP_SECTION_END; addr += SPM_PAGESIZE) + { + Flash_EraseWriteApplicationPage(addr); + // Wait for completion + #ifdef __AVR_XMEGA__ + #ifdef USE_WATCHDOG + while (NVM_STATUS & NVM_NVMBUSY_bp) + { + // reset watchdog while waiting for erase completion + WDT_Reset(); + } + #else // USE_WATCHDOG + SP_WaitForSPM(); + #endif // USE_WATCHDOG + #endif // __AVR_XMEGA__ + } + #else + Flash_EraseApplicationSection(); // Wait for completion -#ifdef __AVR_XMEGA__ + #ifdef __AVR_XMEGA__ #ifdef USE_WATCHDOG while (NVM_STATUS & NVM_NVMBUSY_bp) { - // reset watchdog while waiting for erase completion - WDT_Reset(); + // reset watchdog while waiting for erase completion + WDT_Reset(); } #else // USE_WATCHDOG SP_WaitForSPM(); #endif // USE_WATCHDOG -#endif // __AVR_XMEGA__ + #endif // __AVR_XMEGA__ + #endif // Erase EEPROM EEPROM_erase_all(); From 7bc1e62aebf5513283e9fb1c0992a15dae01021b Mon Sep 17 00:00:00 2001 From: Chris Pavlina Date: Tue, 15 Sep 2015 16:28:18 -0400 Subject: [PATCH 9/9] Move UART_REMAP to uart.c --- uart.c | 6 ++++++ xboot.c | 19 ------------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/uart.c b/uart.c index 45197d7..635b5f6 100644 --- a/uart.c +++ b/uart.c @@ -72,6 +72,9 @@ void uart_init(void) { #ifdef __AVR_XMEGA__ UART_PORT.DIRSET = (1 << UART_TX_PIN); + #ifdef UART_REMAP + UART_PORT.REMAP |= PORT_USART0_bm; + #endif // UART_REMAP UART_DEVICE.BAUDCTRLA = (UART_BSEL_VALUE & USART_BSEL_gm); UART_DEVICE.BAUDCTRLB = ((UART_BSCALE_VALUE << USART_BSCALE_gp) & USART_BSCALE_gm) | ((UART_BSEL_VALUE >> 8) & ~USART_BSCALE_gm); #if UART_CLK2X @@ -104,6 +107,9 @@ void uart_deinit(void) #endif // USE_INTERRUPTS UART_DEVICE.BAUDCTRLA = 0; UART_DEVICE.BAUDCTRLB = 0; + #ifdef UART_REMAP + UART_PORT.REMAP &= ~PORT_USART0_bm; + #endif // UART_REMAP UART_PORT.DIRCLR = (1 << UART_TX_PIN); #else // __AVR_XMEGA__ UART_UCSRA = 0; diff --git a/xboot.c b/xboot.c index 27d8f23..7263c76 100644 --- a/xboot.c +++ b/xboot.c @@ -248,17 +248,6 @@ int main(void) #endif // __AVR_XMEGA__ - // Remap UART pins - -#ifdef __AVR_XMEGA__ - - #ifdef UART_REMAP - // Remap UART pins - UART_PORT.REMAP |= (1 << PORT_USART0_bp); - #endif // UART_REMAP - -#endif // __AVR_XMEGA__ - // Initialize UART EN pin #ifdef __AVR_XMEGA__ @@ -965,14 +954,6 @@ int main(void) #endif // UART_RX_PUEN #endif // __AVR_XMEGA__ - // Remove UART remap -#ifdef __AVR_XMEGA__ - #ifdef UART_REMAP - // Remove UART remap - UART_PORT.REMAP &= ~(1 << PORT_USART0_bp); - #endif // UART_REMAP -#endif // __AVR_XMEGA__ - // Shut down UART EN pin #ifdef USE_UART_EN_PIN #ifdef __AVR_XMEGA__