From 69ba8378d0914fa3fd29a3871464f46c6b1be749 Mon Sep 17 00:00:00 2001 From: Jonathan Vetter Date: Sat, 17 Jul 2021 16:15:32 +0200 Subject: [PATCH] Add support for AVR-USB-MCUs ATmega8u2,16u2,32u2,16u4 and 32u4 --- optiboot/bootloaders/optiboot/Makefile | 1 + .../bootloaders/optiboot/Makefile.USBMCUs | 98 ++++++++++++ .../bootloaders/optiboot/makeall.usbmcu.sh | 142 ++++++++++++++++++ optiboot/bootloaders/optiboot/makeoptions | 1 + optiboot/bootloaders/optiboot/pin_defs.h | 116 ++++++++++++++ 5 files changed, 358 insertions(+) create mode 100644 optiboot/bootloaders/optiboot/Makefile.USBMCUs create mode 100644 optiboot/bootloaders/optiboot/makeall.usbmcu.sh diff --git a/optiboot/bootloaders/optiboot/Makefile b/optiboot/bootloaders/optiboot/Makefile index 0554500b7..0fbcfc96a 100644 --- a/optiboot/bootloaders/optiboot/Makefile +++ b/optiboot/bootloaders/optiboot/Makefile @@ -682,6 +682,7 @@ include Makefile.custom include Makefile.2560 include Makefile.tiny include Makefile.MCUdude +include Makefile.USBMCUs BAUDCHECK= ifndef PRODUCTION diff --git a/optiboot/bootloaders/optiboot/Makefile.USBMCUs b/optiboot/bootloaders/optiboot/Makefile.USBMCUs new file mode 100644 index 000000000..32fe3a436 --- /dev/null +++ b/optiboot/bootloaders/optiboot/Makefile.USBMCUs @@ -0,0 +1,98 @@ +#/* +# * These are the AVR-USB-MCUs +# * soldered on Arduino Micro / Leonardo and similar boards representing the main IO-MCU +# * or being used on Arduino Uno (Wifi) / Mega (2560) as USB to UART converter, the USB-MCU +# * The USB (DFU)-Bootloader installed per default on these devices is REPLACED by Optiboot, +# * thus supporting upload via USART1 (serial1) INSTEAD of USB!!! +# * +# * https://github.com/Virtual-Java +# * +# * This file has basic CPU defintions, while many possible combinations +# * of AVR_FREQ, BAUD_RATE, and UART are built by a shell script. +# */ + + +HELPTEXT += "target atmega8/16/32u2, atmega16/32u4 - newer 32/44pin AVR-USB-MCUs \n" + +#----------------------- +# ATmega 8u2 +#----------------------- +#ATmega8u2 +atmega8u2atUART: TARGET = atmega8u2 +atmega8u2atUART: MCU_TARGET = atmega8u2 +atmega8u2atUART: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) +atmega8u2atUART: AVR_FREQ ?= 16000000L +atmega8u2atUART: LDSECTIONS = -Wl,--section-start=.text=0x1e00 -Wl,--section-start=.version=0x1ffe +atmega8u2atUART: $(PROGRAM)_atmega8u2_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).hex +ifndef PRODUCTION +atmega8u2atUART1: $(PROGRAM)_atmega8u2_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).lst +endif +atmega8u2: + "$(MAKE)" atmega8u2atUART BAUD_RATE=115200 UART=1 + +#----------------------- +# ATmega 16u2 +#----------------------- +#ATmega16u2 +atmega16u2atUART: TARGET = atmega16u2 +atmega16u2atUART: MCU_TARGET = atmega16u2 +atmega16u2atUART: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) +atmega16u2atUART: AVR_FREQ ?= 16000000L +atmega16u2atUART: LDSECTIONS = -Wl,--section-start=.text=0x3e00 -Wl,--section-start=.version=0x3ffe +atmega16u2atUART: $(PROGRAM)_atmega16u2_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).hex +ifndef PRODUCTION +atmega16u2atUART: $(PROGRAM)_atmega16u2_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).lst +endif +atmega16u2: + "$(MAKE)" atmega16u2atUART UART=1 + +#----------------------- +# ATmega 32u2 +#----------------------- +#ATmega32u2 +atmega32u2atUART: TARGET = atmega32u2 +atmega32u2atUART: MCU_TARGET = atmega32u2 +atmega32u2atUART: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) +atmega32u2atUART: AVR_FREQ ?= 16000000L +atmega32u2atUART: LDSECTIONS = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe +atmega32u2atUART: $(PROGRAM)_atmega32u2_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).hex +ifndef PRODUCTION +atmega32u2atUART: $(PROGRAM)_atmega32u2_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).lst +endif +atmega32u2: + "$(MAKE)" atmega32u2atUART UART=1 + +#----------------------- +# ATmega 16u4 +#----------------------- +#ATmega16u4 +atmega16u4atUART: TARGET = atmega16u4 +atmega16u4atUART: MCU_TARGET = atmega16u4 +atmega16u4atUART: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) +atmega16u4atUART: AVR_FREQ ?= 16000000L +atmega16u4atUART: LDSECTIONS = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe +atmega16u4atUART: $(PROGRAM)_atmega16u4_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).hex +ifndef PRODUCTION +atmega16u4atUART: $(PROGRAM)_atmega16u4_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).lst +endif +atmega16u4: +# disable blinking the LED to make the bootloader to fit in 512 Bytes Bootsection + "$(MAKE)" atmega16u4atUART UART=1 + +#----------------------- +# ATmega 32u4 +#----------------------- +#ATmega32u4 +atmega32u4atUART: TARGET = atmega32u4 +atmega32u4atUART: MCU_TARGET = atmega32u4 +atmega32u4atUART: CFLAGS += $(COMMON_OPTIONS) $(UART_CMD) +atmega32u4atUART: AVR_FREQ ?= 16000000L +atmega32u4atUART: LDSECTIONS = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe +atmega32u4atUART: $(PROGRAM)_atmega32u4_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).hex +ifndef PRODUCTION +atmega32u4atUART: $(PROGRAM)_atmega32u4_UART$(UART)_$(BAUD_RATE)_$(AVR_FREQ).lst +endif +atmega32u4: +# disable blinking the LED to make the bootloader to fit in 512 Bytes Bootsection + "$(MAKE)" atmega32u4atUART UART=1 LED_START_FLASHES=0 + diff --git a/optiboot/bootloaders/optiboot/makeall.usbmcu.sh b/optiboot/bootloaders/optiboot/makeall.usbmcu.sh new file mode 100644 index 000000000..ebfaef9db --- /dev/null +++ b/optiboot/bootloaders/optiboot/makeall.usbmcu.sh @@ -0,0 +1,142 @@ +#!/bin/bash +set -e + +################################################################################################## +## Modified to support AVR-USB-MCUs by Virtual Java ## +## https://github.com/Virtual-Java/optiboot ## +## ## +## Created by MCUdude for compiling Optiboot flash from source ## +## https://github.com/MCUdude/optiboot_flash ## +## ## +## Execute ./makeall to build all variants of this bootloader. ## +## Run $ chmod +x if this file isn't executable. ## +## ## +## This is the file contains all all make procedures for all microcontrollers. You can modify ## +## the mcu_and_params array if you want to add, remove or edit any of the settings. You can ## +## also modify the clock_and_baud array if you need other F_CPUs or baudrates. ## +## ## +## The table below shows the available precompiled bootloaders for the corresponding ## +## clock frequencies and baud rates. ## +## ## +## | | 1000000 | 500000 | 250000 | 230400 | 115200 | 57600 | 38400 | 19200 | 9600 | ## +## |-------------|---------|--------|--------|--------|--------|-------|-------|-------|------| ## +## | 20 MHz | | X | X | | X | | | X | | ## +## | 18.4320 MHz | | | | X | X | X | X | X | X | ## +## | 16 MHz | X | X | X | | X | | X | X | X | ## +## | 14.7456 MHz | | | | X | X | X | X | X | X | ## +## | 12 MHz | | X | X | | | X | | X | X | ## +## | 11.0592 MHz | | | | X | X | X | X | X | X | ## +## | 8 MHz | X | X | X | | X | X | X | X | X | ## +## | 7.3728 MHz | | | | X | X | X | X | X | X | ## +## | 4 MHz | | X | X | | | | | X | X | ## +## | 3.6864 MHz | | | | X | X | X | X | X | X | ## +## | 2 MHz | | | X | | | | | X | X | ## +## | 1.8432 MHz | | | | X | X | X | X | X | X | ## +## | 1 MHz | | | | | | | | | X | ## +## ## +################################################################################################## + +declare -a mcu_and_params=( +"atmega8u2" "LED=D5 LED_START_FLASHES=2 UART=1" +"atmega16u2" "LED=D5 LED_START_FLASHES=2 UART=1" +"atmega32u2" "LED=D5 LED_START_FLASHES=2 UART=1" +"atmega16u4" "LED=C7 LED_START_FLASHES=0 UART=1" # disable blinking the LED to make the bootloader to fit in 512 Bytes Bootsection +"atmega32u4" "LED=C7 LED_START_FLASHES=0 UART=1" # disable blinking the LED to make the bootloader to fit in 512 Bytes Bootsection +) + +declare -a clock_and_baud=( +# "20000000L" "500000" +"20000000L" "250000" +"20000000L" "115200" +# "20000000L" "19200" +# "18432000L" "230400" +# "18432000L" "115200" +# "18432000L" "57600" +# "18432000L" "38400" +# "18432000L" "19200" +# "18432000L" "9600" +# "16000000L" "1000000" +# "16000000L" "500000" +"16000000L" "250000" +"16000000L" "115200" +# "16000000L" "38400" +# "16000000L" "19200" +# "16000000L" "9600" +# "14745600L" "230400" +# "14745600L" "115200" +# "14745600L" "57600" +# "14745600L" "38400" +# "14745600L" "19200" +# "14745600L" "9600" +# "12000000L" "500000" +# "12000000L" "250000" +#"12000000L" "57600" +# "12000000L" "19200" +# "12000000L" "9600" +# "11059200L" "230400" +# "11059200L" "115200" +# "11059200L" "57600" +# "11059200L" "38400" +# "11059200L" "19200" +# "11059200L" "9600" +# "8000000L" "1000000" +# "8000000L" "500000" +"8000000L" "250000" +"8000000L" "115200" +"8000000L" "57600" +# "8000000L" "38400" +# "8000000L" "19200" +# "8000000L" "9600" +# "7372800L" "230400" +# "7372800L" "115200" +# "7372800L" "57600" +# "7372800L" "38400" +# "7372800L" "19200" +# "7372800L" "9600" +# "4000000L" "500000" +# "4000000L" "250000" +# "4000000L" "19200" +# "4000000L" "9600" +# "3686400L" "230400" +# "3686400L" "115200" +# "3686400L" "57600" +# "3686400L" "38400" +# "3686400L" "19200" +# "3686400L" "9600" +# "2000000L" "250000" +# "2000000L" "19200" +# "2000000L" "9600" +# "1843200L" "230400" +# "1843200L" "115200" +# "1843200L" "57600" +# "1843200L" "38400" +# "1843200L" "19200" +# "1843200L" "9600" +# "1000000L" "9600" +) + + +# Start out by deleting all previous generated files +echo -e "\x1B[7m\n\nDeleting all previous generated files \x1B[0m" +#make clean_all +make clean + +# Loop through all MCUs and parameters +for((i=0; i < ${#mcu_and_params[@]}; i+=2)); +do + # Loop through all clock speeds and baud rated + for((j=0; j < ${#clock_and_baud[@]}; j+=2)); + do + # Count how many builds that have been ran + COUNTER=$(($COUNTER + 1)) + echo -e "\x1B[7m\n\n$COUNTER of $((${#mcu_and_params[@]} * ${#clock_and_baud[@]} / 4))" + # Print out current build info + echo -e " ${mcu_and_params[$i]} AVR_FREQ=${clock_and_baud[$j]} BAUD_RATE=${clock_and_baud[$j+1]} ${mcu_and_params[$i+1]} \x1B[0m" + # Build + make ${mcu_and_params[$i]} AVR_FREQ=${clock_and_baud[$j]} BAUD_RATE=${clock_and_baud[$j+1]} ${mcu_and_params[$i+1]} $* + done +done + +# Finish up by removing all *.lst files +echo -e "\x1B[7m\n\nDeleting all generated *.lst files \x1B[0m" +make clean_asm diff --git a/optiboot/bootloaders/optiboot/makeoptions b/optiboot/bootloaders/optiboot/makeoptions index fad1631b3..efd6702cb 100755 --- a/optiboot/bootloaders/optiboot/makeoptions +++ b/optiboot/bootloaders/optiboot/makeoptions @@ -30,6 +30,7 @@ make atmega328 SUPPORT_EEPROM=1 make atmega328 LED=J1 # no such led make atmega328 LED=fred +# no such port make atmega328 UART=1 # #invalid bit rates diff --git a/optiboot/bootloaders/optiboot/pin_defs.h b/optiboot/bootloaders/optiboot/pin_defs.h index 983628cbf..d5ed6f9e0 100644 --- a/optiboot/bootloaders/optiboot/pin_defs.h +++ b/optiboot/bootloaders/optiboot/pin_defs.h @@ -481,6 +481,122 @@ #endif #endif +/*------------------------------------------------------------------------ */ +#if defined(__AVR_ATmega8U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) +/*------------------------------------------------------------------------ */ + +#if !defined(LED) +#define LED D5 +#endif + +/* + * Handle devices with up to 4 uarts (eg m1280.) Rather inelegantly. + * Note that mega8/m32 still needs special handling, because ubrr is handled + * differently. + */ +#if (SOFT_UART == 0) +#if UART == 0 +#error UART == 0, but no UART0 on device +#elif UART == 1 +#if !defined(UDR1) +#error UART == 1, but no UART1 on device +#endif +# define UART_SRA UCSR1A +# define UART_SRB UCSR1B +# define UART_SRC UCSR1C +# define UART_SRL UBRR1L +# define UART_UDR UDR1 +#elif UART == 2 +#error UART == 2, but no UART2 on device +#elif UART == 3 +#error UART == 3, but no UART3 on device +#endif +#endif //end #if SOFT_UART==0 + +/* Fix different register names */ +#define MPCM0 MPCM1 +#define U2X0 U2X1 +#define UPE0 UPE1 +#define DOR0 DOR1 +#define FE0 FE1 +#define UDRE0 UDRE1 +#define TXC0 TXC1 +#define RXC0 RXC1 + +#define TXB80 TXB81 +#define RXB80 RXB81 +#define UCSZ02 UCSZ21 +#define TXEN0 TXEN1 +#define RXEN0 RXEN1 +#define UDRIE0 UDRIE1 +#define TXCIE0 TXCIE1 +#define RXCIE0 RXCIE1 + +#define UCPOL0 UCPOL1 +#define UCSZ00 UCSZ01 +#define UCSZ01 UCSZ11 +#define USBS0 USBS1 +#define UPM00 UPM01 +#define UPM01 UPM11 +#define UMSEL00 UMSEL01 +#define UMSEL01 UMSEL11 + +/* Ports for soft UART */ +#if SOFT_UART +#define UART_PORT PORTD +#define UART_PIN PIND +#define UART_DDR DDRD +#define UART_TX_BIT 3 +#define UART_RX_BIT 2 +#endif +#endif + +/*------------------------------------------------------------------------ */ +#if defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) +/*------------------------------------------------------------------------ */ + +#if !defined(LED) +#define LED C7 +#endif + +/* Fix different register names */ +#define MPCM0 MPCM1 +#define U2X0 U2X1 +#define UPE0 UPE1 +#define DOR0 DOR1 +#define FE0 FE1 +#define UDRE0 UDRE1 +#define TXC0 TXC1 +#define RXC0 RXC1 + +#define TXB80 TXB81 +#define RXB80 RXB81 +#define UCSZ02 UCSZ21 +#define TXEN0 TXEN1 +#define RXEN0 RXEN1 +#define UDRIE0 UDRIE1 +#define TXCIE0 TXCIE1 +#define RXCIE0 RXCIE1 + +#define UCPOL0 UCPOL1 +#define UCSZ00 UCSZ01 +#define UCSZ01 UCSZ11 +#define USBS0 USBS1 +#define UPM00 UPM01 +#define UPM01 UPM11 +#define UMSEL00 UMSEL01 +#define UMSEL01 UMSEL11 + +/* Ports for soft UART */ +#if SOFT_UART +#define UART_PORT PORTD +#define UART_PIN PIND +#define UART_DDR DDRD +#define UART_TX_BIT 3 +#define UART_RX_BIT 2 +#endif +#endif + /*------------------------------------------------------------------------ */ #if defined(__AVR_ATtiny1634__) /*------------------------------------------------------------------------ */