From 198d3ba064bc2fa9194f72d3b70aa5c2d69b4f3d Mon Sep 17 00:00:00 2001 From: maniacbug Date: Tue, 22 Nov 2011 21:36:25 -0800 Subject: [PATCH] import --- .gitignore | 5 + boards.txt | 18 + bootloaders/standard/ATmegaBOOT.c | 1091 +++++++++++++++++++++ bootloaders/standard/ATmegaBOOT_1284P.hex | 117 +++ bootloaders/standard/Makefile | 56 ++ variants/standard/pins_arduino.h | 221 +++++ 6 files changed, 1508 insertions(+) create mode 100644 .gitignore create mode 100644 boards.txt create mode 100644 bootloaders/standard/ATmegaBOOT.c create mode 100644 bootloaders/standard/ATmegaBOOT_1284P.hex create mode 100644 bootloaders/standard/Makefile create mode 100644 variants/standard/pins_arduino.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48154ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.*.swp +*.orig +*.bak +*.o +*.elf diff --git a/boards.txt b/boards.txt new file mode 100644 index 0000000..60839bc --- /dev/null +++ b/boards.txt @@ -0,0 +1,18 @@ + +############################################################## + +mighty.name=Mighty 1284p +mighty.upload.protocol=stk500v1 +mighty.upload.maximum_size=129024 +mighty.upload.speed=57600 +mighty.bootloader.low_fuses=0xff +mighty.bootloader.high_fuses=0xd8 +mighty.bootloader.extended_fuses=0xfd +mighty.bootloader.path=standard +mighty.bootloader.file=ATmegaBOOT_1284P.hex +mighty.bootloader.unlock_bits=0x3F +mighty.bootloader.lock_bits=0x0F +mighty.build.mcu=atmega1284p +mighty.build.f_cpu=16000000L +mighty.build.core=arduino:arduino +mighty.build.variant=standard diff --git a/bootloaders/standard/ATmegaBOOT.c b/bootloaders/standard/ATmegaBOOT.c new file mode 100644 index 0000000..0841a1a --- /dev/null +++ b/bootloaders/standard/ATmegaBOOT.c @@ -0,0 +1,1091 @@ +/**********************************************************/ +/* Serial Bootloader for Atmel megaAVR Controllers */ +/* */ +/* tested with ATmega8, ATmega128 and ATmega168 */ +/* should work with other mega's, see code for details */ +/* */ +/* ATmegaBOOT.c */ +/* */ +/* */ +/* 20090308: integrated Mega changes into main bootloader */ +/* source by D. Mellis */ +/* 20080930: hacked for Arduino Mega (with the 1280 */ +/* processor, backwards compatible) */ +/* by D. Cuartielles */ +/* 20070626: hacked for Arduino Diecimila (which auto- */ +/* resets when a USB connection is made to it) */ +/* by D. Mellis */ +/* 20060802: hacked for Arduino by D. Cuartielles */ +/* based on a previous hack by D. Mellis */ +/* and D. Cuartielles */ +/* */ +/* Monitor and debug functions were added to the original */ +/* code by Dr. Erik Lins, chip45.com. (See below) */ +/* */ +/* Thanks to Karl Pitrich for fixing a bootloader pin */ +/* problem and more informative LED blinking! */ +/* */ +/* For the latest version see: */ +/* http://www.chip45.com/ */ +/* */ +/* ------------------------------------------------------ */ +/* */ +/* based on stk500boot.c */ +/* Copyright (c) 2003, Jason P. Kyle */ +/* All rights reserved. */ +/* see avr1.org for original file and information */ +/* */ +/* This program is free software; you can redistribute it */ +/* and/or modify it under the terms of the GNU General */ +/* Public License as published by the Free Software */ +/* Foundation; either version 2 of the License, or */ +/* (at your option) any later version. */ +/* */ +/* This program is distributed in the hope that it will */ +/* be useful, but WITHOUT ANY WARRANTY; without even the */ +/* implied warranty of MERCHANTABILITY or FITNESS FOR A */ +/* PARTICULAR PURPOSE. See the GNU General Public */ +/* License for more details. */ +/* */ +/* You should have received a copy of the GNU General */ +/* Public License along with this program; if not, write */ +/* to the Free Software Foundation, Inc., */ +/* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/* */ +/* Licence can be viewed at */ +/* http://www.fsf.org/licenses/gpl.txt */ +/* */ +/* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */ +/* m8515,m8535. ATmega161 has a very small boot block so */ +/* isn't supported. */ +/* */ +/* Tested with m168 */ +/**********************************************************/ + +/* $Id$ */ + + +/* some includes */ +#include +#include +#include +#include +#include +#include + +/* the current avr-libc eeprom functions do not support the ATmega168 */ +/* own eeprom write/read functions are used instead */ +#if !defined(__AVR_ATmega168__) || !defined(__AVR_ATmega328P__) +#include +#endif + +/* Use the F_CPU defined in Makefile */ + +/* 20060803: hacked by DojoCorp */ +/* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */ +/* set the waiting time for the bootloader */ +/* get this from the Makefile instead */ +/* #define MAX_TIME_COUNT (F_CPU>>4) */ + +/* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */ +#define MAX_ERROR_COUNT 5 +#define NUM_LED_FLASHES 3 +/* set the UART baud rate */ +/* 20060803: hacked by DojoCorp */ +//#define BAUD_RATE 115200 +#ifndef BAUD_RATE +#define BAUD_RATE 19200 +#endif + + +/* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */ +/* never allow AVR Studio to do an update !!!! */ +#define HW_VER 0x02 +#define SW_MAJOR 0x01 +#define SW_MINOR 0x10 + + +/* Adjust to suit whatever pin your hardware uses to enter the bootloader */ +/* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */ +/* ATmega1280 has four UARTS, but for Arduino Mega, we will only use RXD0 to get code */ +/* BL0... means UART0, BL1... means UART1 */ +#ifdef __AVR_ATmega128__ +#define BL_DDR DDRF +#define BL_PORT PORTF +#define BL_PIN PINF +#define BL0 PINF7 +#define BL1 PINF6 +#elif defined __AVR_ATmega1280__ +/* we just don't do anything for the MEGA and enter bootloader on reset anyway*/ +#elif defined __AVR_ATmega1284P__ + +#else +/* other ATmegas have only one UART, so only one pin is defined to enter bootloader */ +#define BL_DDR DDRD +#define BL_PORT PORTD +#define BL_PIN PIND +#define BL PIND6 +#endif + + +/* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */ +/* if monitor functions are included, LED goes on after monitor was entered */ +#if defined __AVR_ATmega128__ || defined __AVR_ATmega1280__ +/* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128, Arduino Mega) */ +#define LED_DDR DDRB +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED PINB7 +#elif defined __AVR_ATmega1284P__ +#define LED_DDR DDRB +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED PINB0 +#else +/* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duomilanuove */ +/* other boards like e.g. Crumb8, Crumb168 are using PB2 */ +#define LED_DDR DDRB +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED PINB5 +#endif + + +/* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */ +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) +#define MONITOR 1 +#endif + + +/* define various device id's */ +/* manufacturer byte is always the same */ +#define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :( + +#if defined __AVR_ATmega1280__ +#define SIG2 0x97 +#define SIG3 0x03 +#define PAGE_SIZE 0x80U //128 words + +#elif defined __AVR_ATmega1284P__ +#define SIG2 0x97 +#define SIG3 0x05 +#define PAGE_SIZE 0x080U //128 words + +#elif defined __AVR_ATmega1281__ +#define SIG2 0x97 +#define SIG3 0x04 +#define PAGE_SIZE 0x80U //128 words + +#elif defined __AVR_ATmega128__ +#define SIG2 0x97 +#define SIG3 0x02 +#define PAGE_SIZE 0x80U //128 words + +#elif defined __AVR_ATmega64__ +#define SIG2 0x96 +#define SIG3 0x02 +#define PAGE_SIZE 0x80U //128 words + +#elif defined __AVR_ATmega32__ +#define SIG2 0x95 +#define SIG3 0x02 +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega16__ +#define SIG2 0x94 +#define SIG3 0x03 +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega8__ +#define SIG2 0x93 +#define SIG3 0x07 +#define PAGE_SIZE 0x20U //32 words + +#elif defined __AVR_ATmega88__ +#define SIG2 0x93 +#define SIG3 0x0a +#define PAGE_SIZE 0x20U //32 words + +#elif defined __AVR_ATmega168__ +#define SIG2 0x94 +#define SIG3 0x06 +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega328P__ +#define SIG2 0x95 +#define SIG3 0x0F +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega162__ +#define SIG2 0x94 +#define SIG3 0x04 +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega163__ +#define SIG2 0x94 +#define SIG3 0x02 +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega169__ +#define SIG2 0x94 +#define SIG3 0x05 +#define PAGE_SIZE 0x40U //64 words + +#elif defined __AVR_ATmega8515__ +#define SIG2 0x93 +#define SIG3 0x06 +#define PAGE_SIZE 0x20U //32 words + +#elif defined __AVR_ATmega8535__ +#define SIG2 0x93 +#define SIG3 0x08 +#define PAGE_SIZE 0x20U //32 words +#endif + + +/* function prototypes */ +void putch(char); +char getch(void); +void getNch(uint8_t); +void byte_response(uint8_t); +void nothing_response(void); +char gethex(void); +void puthex(char); +void flash_led(uint8_t); + +/* some variables */ +union address_union +{ + uint16_t word; + uint8_t byte[2]; +} address; + +union length_union +{ + uint16_t word; + uint8_t byte[2]; +} length; + +struct flags_struct +{ + unsigned eeprom : 1; + unsigned rampz : 1; +} flags; + +uint8_t buff[256]; +uint8_t address_high; + +uint8_t pagesz=0x80; + +uint8_t i; +uint8_t bootuart = 0; + +uint8_t error_count = 0; + +void (*app_start)(void) = 0x0000; + + +/* main program starts here */ +int main(void) +{ + uint8_t ch,ch2; + uint16_t w; +#ifdef WATCHDOG_MODS + ch = MCUSR; + MCUSR = 0; + WDTCSR |= _BV(WDCE) | _BV(WDE); + WDTCSR = 0; + // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot. + if (! (ch & _BV(EXTRF))) // if its a not an external reset... + app_start(); // skip bootloader +#else + asm volatile("nop\n\t"); +#endif + /* set pin direction for bootloader pin and enable pullup */ + /* for ATmega128, two pins need to be initialized */ +#ifdef __AVR_ATmega128__ + BL_DDR &= ~_BV(BL0); + BL_DDR &= ~_BV(BL1); + BL_PORT |= _BV(BL0); + BL_PORT |= _BV(BL1); +#else + /* We run the bootloader regardless of the state of this pin. Thus, don't + put it in a different state than the other pins. --DAM, 070709 + This also applies to Arduino Mega -- DC, 080930 + BL_DDR &= ~_BV(BL); + BL_PORT |= _BV(BL); + */ +#endif +#ifdef __AVR_ATmega128__ + /* check which UART should be used for booting */ + if(bit_is_clear(BL_PIN, BL0)) + { + bootuart = 1; + } + else if(bit_is_clear(BL_PIN, BL1)) + { + bootuart = 2; + } +#endif +#if defined __AVR_ATmega1280__ || defined __AVR_ATmega1284P__ + /* the mega1280 chip has four serial ports ... we could eventually use any of them, or not? */ + /* however, we don't wanna confuse people, to avoid making a mess, we will stick to RXD0, TXD0 */ + bootuart = 1; +#endif + /* check if flash is programmed already, if not start bootloader anyway */ + if(pgm_read_byte_near(0x0000) != 0xFF) + { +#ifdef __AVR_ATmega128__ + /* no UART was selected, start application */ + if(!bootuart) + { + app_start(); + } +#else + /* check if bootloader pin is set low */ + /* we don't start this part neither for the m8, nor m168 */ + //if(bit_is_set(BL_PIN, BL)) { + // app_start(); + // } +#endif + } +#ifdef __AVR_ATmega128__ + /* no bootuart was selected, default to uart 0 */ + if(!bootuart) + { + bootuart = 1; + } +#endif + /* initialize UART(s) depending on CPU defined */ +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__) + if(bootuart == 1) + { + UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1); + UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8; + UCSR0A = 0x00; + UCSR0C = 0x06; + UCSR0B = _BV(TXEN0)|_BV(RXEN0); + } + if(bootuart == 2) + { + UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1); + UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8; + UCSR1A = 0x00; + UCSR1C = 0x06; + UCSR1B = _BV(TXEN1)|_BV(RXEN1); + } +#elif defined __AVR_ATmega163__ + UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1); + UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8; + UCSRA = 0x00; + UCSRB = _BV(TXEN)|_BV(RXEN); +#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) +#ifdef DOUBLE_SPEED + UCSR0A = (1<> 8; +#else + UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1); + UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8; +#endif + UCSR0B = (1<>8; // set baud rate + UBRRL = (((F_CPU/BAUD_RATE)/16)-1); + UCSRB = (1<> 8; + UCSRA = 0x00; + UCSRC = 0x06; + UCSRB = _BV(TXEN)|_BV(RXEN); +#endif +#if defined __AVR_ATmega1280__ + /* Enable internal pull-up resistor on pin D0 (RX), in order + to supress line noise that prevents the bootloader from + timing out (DAM: 20070509) */ + /* feature added to the Arduino Mega --DC: 080930 */ + DDRE &= ~_BV(PINE0); + PORTE |= _BV(PINE0); +#endif + /* set LED pin as output */ + LED_DDR |= _BV(LED); + /* flash onboard LED to signal entering of bootloader */ +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__) + // 4x for UART0, 5x for UART1 + flash_led(NUM_LED_FLASHES + bootuart); +#else + flash_led(NUM_LED_FLASHES); +#endif + /* 20050803: by DojoCorp, this is one of the parts provoking the + system to stop listening, cancelled from the original */ + //putch('\0'); + /* forever loop */ + for (;;) + { + /* get character from UART */ + ch = getch(); + /* A bunch of if...else if... gives smaller code than switch...case ! */ + /* Hello is anyone home ? */ + if(ch=='0') + { + nothing_response(); + } + /* Request programmer ID */ + /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */ + /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */ + else if(ch=='1') + { + if (getch() == ' ') + { + putch(0x14); + putch('A'); + putch('V'); + putch('R'); + putch(' '); + putch('I'); + putch('S'); + putch('P'); + putch(0x10); + } + else + { + if (++error_count == MAX_ERROR_COUNT) + app_start(); + } + } + /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */ + else if(ch=='@') + { + ch2 = getch(); + if (ch2>0x85) getch(); + nothing_response(); + } + /* AVR ISP/STK500 board requests */ + else if(ch=='A') + { + ch2 = getch(); + if(ch2==0x80) byte_response(HW_VER); // Hardware version + else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version + else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version + else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56 + else byte_response(0x00); // Covers various unnecessary responses we don't care about + } + /* Device Parameters DON'T CARE, DEVICE IS FIXED */ + else if(ch=='B') + { + getNch(20); + nothing_response(); + } + /* Parallel programming stuff DON'T CARE */ + else if(ch=='E') + { + getNch(5); + nothing_response(); + } + /* P: Enter programming mode */ + /* R: Erase device, don't care as we will erase one page at a time anyway. */ + else if(ch=='P' || ch=='R') + { + nothing_response(); + } + /* Leave programming mode */ + else if(ch=='Q') + { + nothing_response(); +#ifdef WATCHDOG_MODS + // autoreset via watchdog (sneaky!) + WDTCSR = _BV(WDE); + while (1); // 16 ms +#endif + } + /* Set address, little endian. EEPROM in bytes, FLASH in words */ + /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */ + /* This might explain why little endian was used here, big endian used everywhere else. */ + else if(ch=='U') + { + address.byte[0] = getch(); + address.byte[1] = getch(); + nothing_response(); + } + /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */ + else if(ch=='V') + { + if (getch() == 0x30) + { + getch(); + ch = getch(); + getch(); + if (ch == 0) + { + byte_response(SIG1); + } + else if (ch == 1) + { + byte_response(SIG2); + } + else + { + byte_response(SIG3); + } + } + else + { + getNch(3); + byte_response(0x00); + } + } + /* Write memory, length is big endian and is in bytes */ + else if(ch=='d') + { + length.byte[1] = getch(); + length.byte[0] = getch(); + flags.eeprom = 0; + if (getch() == 'E') flags.eeprom = 1; + for (w=0; w127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME + else address_high = 0x00; +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__) + RAMPZ = address_high; +#endif + address.word = address.word << 1; //address * 2 -> byte location + /* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */ + if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes + cli(); //Disable interrupts, just to be sure +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__) + while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete +#else + while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete +#endif + asm volatile( + "clr r17 \n\t" //page_word_count + "lds r30,address \n\t" //Address of FLASH location (in bytes) + "lds r31,address+1 \n\t" + "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM + "ldi r29,hi8(buff) \n\t" + "lds r24,length \n\t" //Length of data to be written (in bytes) + "lds r25,length+1 \n\t" + "length_loop: \n\t" //Main loop, repeat for number of words in block + "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page + "brne no_page_erase \n\t" + "wait_spm1: \n\t" + "lds r16,%0 \n\t" //Wait for previous spm to complete + "andi r16,1 \n\t" + "cpi r16,1 \n\t" + "breq wait_spm1 \n\t" + "ldi r16,0x03 \n\t" //Erase page pointed to by Z + "sts %0,r16 \n\t" + "spm \n\t" +#ifdef __AVR_ATmega163__ + ".word 0xFFFF \n\t" + "nop \n\t" +#endif + "wait_spm2: \n\t" + "lds r16,%0 \n\t" //Wait for previous spm to complete + "andi r16,1 \n\t" + "cpi r16,1 \n\t" + "breq wait_spm2 \n\t" + "ldi r16,0x11 \n\t" //Re-enable RWW section + "sts %0,r16 \n\t" + "spm \n\t" +#ifdef __AVR_ATmega163__ + ".word 0xFFFF \n\t" + "nop \n\t" +#endif + "no_page_erase: \n\t" + "ld r0,Y+ \n\t" //Write 2 bytes into page buffer + "ld r1,Y+ \n\t" + "wait_spm3: \n\t" + "lds r16,%0 \n\t" //Wait for previous spm to complete + "andi r16,1 \n\t" + "cpi r16,1 \n\t" + "breq wait_spm3 \n\t" + "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer + "sts %0,r16 \n\t" + "spm \n\t" + "inc r17 \n\t" //page_word_count++ + "cpi r17,%1 \n\t" + "brlo same_page \n\t" //Still same page in FLASH + "write_page: \n\t" + "clr r17 \n\t" //New page, write current one first + "wait_spm4: \n\t" + "lds r16,%0 \n\t" //Wait for previous spm to complete + "andi r16,1 \n\t" + "cpi r16,1 \n\t" + "breq wait_spm4 \n\t" +#ifdef __AVR_ATmega163__ + "andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write +#endif + "ldi r16,0x05 \n\t" //Write page pointed to by Z + "sts %0,r16 \n\t" + "spm \n\t" +#ifdef __AVR_ATmega163__ + ".word 0xFFFF \n\t" + "nop \n\t" + "ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write) +#endif + "wait_spm5: \n\t" + "lds r16,%0 \n\t" //Wait for previous spm to complete + "andi r16,1 \n\t" + "cpi r16,1 \n\t" + "breq wait_spm5 \n\t" + "ldi r16,0x11 \n\t" //Re-enable RWW section + "sts %0,r16 \n\t" + "spm \n\t" +#ifdef __AVR_ATmega163__ + ".word 0xFFFF \n\t" + "nop \n\t" +#endif + "same_page: \n\t" + "adiw r30,2 \n\t" //Next word in FLASH + "sbiw r24,2 \n\t" //length-2 + "breq final_write \n\t" //Finished + "rjmp length_loop \n\t" + "final_write: \n\t" + "cpi r17,0 \n\t" + "breq block_done \n\t" + "adiw r24,2 \n\t" //length+2, fool above check on length after short page write + "rjmp write_page \n\t" + "block_done: \n\t" + "clr __zero_reg__ \n\t" //restore zero register +#if defined __AVR_ATmega168__ || __AVR_ATmega328P__ || __AVR_ATmega128__ || __AVR_ATmega1280__ || __AVR_ATmega1281__ || __AVR_ATmega1284P__ + : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31" +#else + : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31" +#endif + ); + /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */ + /* exit the bootloader without a power cycle anyhow */ + } + putch(0x14); + putch(0x10); + } + else + { + if (++error_count == MAX_ERROR_COUNT) + app_start(); + } + } + /* Read memory block mode, length is big endian. */ + else if(ch=='t') + { + length.byte[1] = getch(); + length.byte[0] = getch(); +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__) + if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME + else flags.rampz = 0; +#endif + address.word = address.word << 1; // address * 2 -> byte location + if (getch() == 'E') flags.eeprom = 1; + else flags.eeprom = 0; + if (getch() == ' ') // Command terminator + { + putch(0x14); + for (w=0; w < length.word; w++) // Can handle odd and even lengths okay + { + if (flags.eeprom) // Byte access EEPROM read + { +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) + while(EECR & (1<= 'a') + { + return (a - 'a' + 0x0a); + } + else if(a >= '0') + { + return(a - '0'); + } + return a; +} + + +char gethex(void) +{ + return (gethexnib() << 4) + gethexnib(); +} + + +void puthex(char ch) +{ + char ah; + ah = ch >> 4; + if(ah >= 0x0a) + { + ah = ah - 0x0a + 'a'; + } + else + { + ah += '0'; + } + ch &= 0x0f; + if(ch >= 0x0a) + { + ch = ch - 0x0a + 'a'; + } + else + { + ch += '0'; + } + putch(ah); + putch(ch); +} + + +void putch(char ch) +{ +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__) + if(bootuart == 1) + { + while (!(UCSR0A & _BV(UDRE0))); + UDR0 = ch; + } + else if (bootuart == 2) + { + while (!(UCSR1A & _BV(UDRE1))); + UDR1 = ch; + } +#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) + while (!(UCSR0A & _BV(UDRE0))); + UDR0 = ch; +#else + /* m8,16,32,169,8515,8535,163 */ + while (!(UCSRA & _BV(UDRE))); + UDR = ch; +#endif +} + + +char getch(void) +{ +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__) + uint32_t count = 0; + if(bootuart == 1) + { + while(!(UCSR0A & _BV(RXC0))) + { + /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/ + /* HACKME:: here is a good place to count times*/ + count++; + if (count > MAX_TIME_COUNT) + app_start(); + } + return UDR0; + } + else if(bootuart == 2) + { + while(!(UCSR1A & _BV(RXC1))) + { + /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/ + /* HACKME:: here is a good place to count times*/ + count++; + if (count > MAX_TIME_COUNT) + app_start(); + } + return UDR1; + } + return 0; +#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) + uint32_t count = 0; + while(!(UCSR0A & _BV(RXC0))) + { + /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/ + /* HACKME:: here is a good place to count times*/ + count++; + if (count > MAX_TIME_COUNT) + app_start(); + } + return UDR0; +#else + /* m8,16,32,169,8515,8535,163 */ + uint32_t count = 0; + while(!(UCSRA & _BV(RXC))) + { + /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/ + /* HACKME:: here is a good place to count times*/ + count++; + if (count > MAX_TIME_COUNT) + app_start(); + } + return UDR; +#endif +} + + +void getNch(uint8_t count) +{ + while(count--) + { +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__) + if(bootuart == 1) + { + while(!(UCSR0A & _BV(RXC0))); + UDR0; + } + else if(bootuart == 2) + { + while(!(UCSR1A & _BV(RXC1))); + UDR1; + } +#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) + getch(); +#else + /* m8,16,32,169,8515,8535,163 */ + /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/ + //while(!(UCSRA & _BV(RXC))); + //UDR; + getch(); // need to handle time out +#endif + } +} + + +void byte_response(uint8_t val) +{ + if (getch() == ' ') + { + putch(0x14); + putch(val); + putch(0x10); + } + else + { + if (++error_count == MAX_ERROR_COUNT) + app_start(); + } +} + + +void nothing_response(void) +{ + if (getch() == ' ') + { + putch(0x14); + putch(0x10); + } + else + { + if (++error_count == MAX_ERROR_COUNT) + app_start(); + } +} + +void flash_led(uint8_t count) +{ + while (count--) + { + LED_PORT |= _BV(LED); + _delay_ms(100); + LED_PORT &= ~_BV(LED); + _delay_ms(100); + } +} + + +/* end of file ATmegaBOOT.c */ diff --git a/bootloaders/standard/ATmegaBOOT_1284P.hex b/bootloaders/standard/ATmegaBOOT_1284P.hex new file mode 100644 index 0000000..9d35965 --- /dev/null +++ b/bootloaders/standard/ATmegaBOOT_1284P.hex @@ -0,0 +1,117 @@ +:020000021000EC +:10F800000C9446FC0C9465FC0C9465FC0C9465FC13 +:10F810000C9465FC0C9465FC0C9465FC0C9465FCE4 +:10F820000C9465FC0C9465FC0C9465FC0C9465FCD4 +:10F830000C9465FC0C9465FC0C9465FC0C9465FCC4 +:10F840000C9465FC0C9465FC0C9465FC0C9465FCB4 +:10F850000C9465FC0C9465FC0C9465FC0C9465FCA4 +:10F860000C9465FC0C9465FC0C9465FC0C9465FC94 +:10F870000C9465FC0C9465FC0C9465FC0C9465FC84 +:10F880000C9465FC0C9465FC0C9465FC11241FBE63 +:10F89000CFEFD0E4DEBFCDBF11E0A0E0B1E0E6E005 +:10F8A000FFEF01E00BBF02C007900D92A230B1073D +:10F8B000D9F712E0A2E0B1E001C01D92AD30B1076E +:10F8C000E1F70E945FFD0C9481FF0C9400FC909185 +:10F8D0000201913039F49091C00095FFFCCF8093E4 +:10F8E000C6000895923031F49091C80095FFFCCF86 +:10F8F0008093CE0008951F93982F95959595959593 +:10F900009595905D182F1F701A3014F0195A01C088 +:10F91000105D892F0E9467FC812F0E9467FC1F9158 +:10F920000895EF92FF920F931F938091020181300F +:10F93000F1F4EE24FF24870113C00894E11CF11CAC +:10F94000011D111D81E0E81682E1F8068AE708072B +:10F9500080E0180728F0E0910401F0910501099575 +:10F960008091C00087FFE9CF8091C60021C082301E +:10F97000F1F4EE24FF24870113C00894E11CF11C6C +:10F98000011D111D81E0E81682E1F8068AE70807EB +:10F9900080E0180728F0E0910401F0910501099535 +:10F9A0008091C80087FFE9CF8091CE0001C080E040 +:10F9B0001F910F91FF90EF9008951F930E9491FC6B +:10F9C000182F0E9467FC113614F0175503C010332E +:10F9D0000CF01053812F1F9108951F930E94DDFC9E +:10F9E000182F0E94DDFC1295107F810F1F91089542 +:10F9F0009091020112C0913039F42091C00027FF8C +:10FA0000FCCF2091C60008C0923031F42091C8008C +:10FA100027FFFCCF2091CE008150882361F7089505 +:10FA20001F93182F0E9491FC803251F484E10E94B0 +:10FA300067FC812F0E9467FC80E10E9467FC0CC07C +:10FA4000809103018F5F80930301853029F4E09159 +:10FA50000401F091050109951F9108950E9491FC00 +:10FA6000803239F484E10E9467FC80E10E9467FCE7 +:10FA70000895809103018F5F80930301853029F4FD +:10FA8000E0910401F09105010995089515C0289AA7 +:10FA90002FEF31EE44E0215030404040E1F700C00C +:10FAA000000028982FEF31EE44E0215030404040D4 +:10FAB000E1F700C000008150882349F70895BF9204 +:10FAC000CF92DF92EF92FF920F931F93CF93DF932A +:10FAD00094B714BE8091600088618093600010929A +:10FAE000600091FD05C0E0910401F09105010995C8 +:10FAF00081E08093020180E18093C4001092C500F0 +:10FB00001092C00086E08093C20088E18093C1001B +:10FB1000209A84E00E9446FDBB24B3940E9491FC8D +:10FB2000803309F441C08133E1F40E9491FC8032BA +:10FB300009F0C3C184E10E9467FC81E40E9467FC74 +:10FB400086E50E9467FC82E50E9467FC80E20E94D5 +:10FB500067FC89E40E9467FC83E50E9467FC80E5FE +:10FB600029C1803439F40E9491FC8638E8F00E9463 +:10FB700091FC1AC0813499F40E9491FC803811F4F0 +:10FB800082E098C1813811F481E094C1823811F487 +:10FB900080E190C1883909F08CC183E08BC1823447 +:10FBA00031F484E10E94F8FC0E942EFDB7CF853429 +:10FBB00011F485E0F7CF8035B9F38235A9F38135AB +:10FBC00031F40E942EFD88E080936000FFCF8535E0 +:10FBD00049F40E9491FC809306010E9491FC80935D +:10FBE0000701E2CF8635C9F40E9491FC803389F485 +:10FBF0000E9491FC0E9491FC082F0E9491FC00231E +:10FC000011F48EE157C1013011F487E953C185E049 +:10FC100051C183E00E94F8FC4CC1843609F0D0C089 +:10FC20000E9491FC809309020E9491FC809308023B +:10FC300080910C028E7F80930C020E9491FC85348F +:10FC400029F480910C02816080930C0258E0C52E4B +:10FC500051E0D52E760100E010E007C00E9491FC33 +:10FC6000F70181937F010F5F1F4F809108029091F0 +:10FC700009020817190790F30E9491FC803209F0DD +:10FC80001CC180910C0280FF29C0809106019091D7 +:10FC90000701880F991F909307018093060100E0E8 +:10FCA00010E014C0F60161916F01809106019091FE +:10FCB00007010E9473FF8091060190910701019650 +:10FCC00090930701809306010F5F1F4F80910802F8 +:10FCD000909109020817190728F36BC0809107015A +:10FCE000880F880B8B2180930B028BBF80910601BC +:10FCF00090910701880F991F909307018093060147 +:10FD00008091080280FF09C0809108029091090249 +:10FD100001969093090280930802F894F999FECF16 +:10FD20001127E0910601F0910701C8E0D1E0809130 +:10FD3000080290910902103091F40091570001706F +:10FD40000130D9F303E000935700E8950091570084 +:10FD500001700130D9F301E100935700E895099053 +:10FD600019900091570001700130D9F301E0009320 +:10FD70005700E8951395103898F011270091570017 +:10FD800001700130D9F305E000935700E895009128 +:10FD9000570001700130D9F301E100935700E89555 +:10FDA0003296029709F0C7CF103011F00296E5CFD6 +:10FDB000112484E10E9467FC80E10E9467FCAECEC2 +:10FDC000843709F063C00E9491FC809309020E946D +:10FDD00091FC809308028091060190910701209187 +:10FDE0000C0297FF02C0226001C02D7F20930C02FD +:10FDF000880F991F90930701809306010E9491FC40 +:10FE000020910C02853411F4216001C02E7F2093D3 +:10FE10000C020E9491FC803209F080CE84E10E94A5 +:10FE200067FC00E010E02AC080910C0280FF07C050 +:10FE300080910601909107010E946BFF12C0E09132 +:10FE40000601F091070181FD02C084910AC0CF0133 +:10FE5000A0E0B0E080509040AF4FBF4FABBFFC017F +:10FE600087910E9467FC809106019091070101969D +:10FE700090930701809306010F5F1F4F8091080246 +:10FE8000909109020817190778F296CF853779F40F +:10FE90000E9491FC803289F484E10E9467FC8EE12B +:10FEA0000E9467FC87E90E9467FC85E083CF863764 +:10FEB00021F480E00E9410FD31CE809103018F5F1C +:10FEC00080930301853009F029CEE0910401F0917F +:10FED0000501099523CEF999FECF92BD81BDF89A0F +:10FEE000992780B50895262FF999FECF1FBA92BDA4 +:10FEF00081BD20BD0FB6F894FA9AF99A0FBE01960B +:06FF00000895F894FFCF04 +:02FF0600800079 +:040000031000F800F1 +:00000001FF diff --git a/bootloaders/standard/Makefile b/bootloaders/standard/Makefile new file mode 100644 index 0000000..b9ad603 --- /dev/null +++ b/bootloaders/standard/Makefile @@ -0,0 +1,56 @@ +# Makefile for ATmegaBOOT +# E.Lins, 18.7.2005 +# $Id$ + + +# program name should not be changed... +PROGRAM = ATmegaBOOT_1284P + +# enter the target CPU frequency +AVR_FREQ = 16000000L + +MCU_TARGET = atmega1284p +LDSECTION = --section-start=.text=0x1F800 + +OBJ = $(PROGRAM).o +OPTIMIZE = -Os + +DEFS = -DWATCHDOG_MODS -DBAUD_RATE=57600 +LIBS = + +CC = avr-gcc + + +# Override is only needed by avr-lib build system. + +override CFLAGS = -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -DF_CPU=$(AVR_FREQ) $(DEFS) +override LDFLAGS = -Wl,$(LDSECTION) +#override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION) + +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump + +all: CFLAGS += '-DMAX_TIME_COUNT=16000000L>>1' -DADABOOT +all: $(PROGRAM).hex + +$(PROGRAM).hex: $(PROGRAM).elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +$(PROGRAM).elf: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +$(OBJ): ATmegaBOOT.c + avr-gcc $(CFLAGS) $(LDFLAGS) -c -Wall -mmcu=$(MCU_TARGET) ATmegaBOOT.c -o $(PROGRAM).o + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +%.srec: %.elf + $(OBJCOPY) -j .text -j .data -O srec $< $@ + +%.bin: %.elf + $(OBJCOPY) -j .text -j .data -O binary $< $@ + +clean: + rm -rf *.o *.elf *.lst *.map *.sym *.lss *.eep *.srec *.bin *.hex + diff --git a/variants/standard/pins_arduino.h b/variants/standard/pins_arduino.h new file mode 100644 index 0000000..d9779d6 --- /dev/null +++ b/variants/standard/pins_arduino.h @@ -0,0 +1,221 @@ +#ifndef Pins_Arduino_h +#define Pins_Arduino_h + +#include +// Avoid spurious warnings +#undef PROGMEM +#define PROGMEM __attribute__(( section(".progmem.data") )) +#undef PSTR +#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];})) + +// ATMEL ATMEGA1284P +// +// +---\/---+ +// (D 0) PB0 1| |40 PA0 (AI 0 / D24) +// (D 1) PB1 2| |39 PA1 (AI 1 / D25) +// INT2 (D 2) PB2 3| |38 PA2 (AI 2 / D26) +// PWM (D 3) PB3 4| |37 PA3 (AI 3 / D27) +// PWM/SS (D 4) PB4 5| |36 PA4 (AI 4 / D28) +// MOSI (D 5) PB5 6| |35 PA5 (AI 5 / D29) +// PWM/MISO (D 6) PB6 7| |34 PA6 (AI 6 / D30) +// PWM/SCK (D 7) PB7 8| |33 PA7 (AI 7 / D31) +// RST 9| |32 AREF +// VCC 10| |31 GND +// GND 11| |30 AVCC +// XTAL2 12| |29 PC7 (D 23) +// XTAL1 13| |28 PC6 (D 22) +// RX0 (D 8) PD0 14| |27 PC5 (D 21) TDI +// TX0 (D 9) PD1 15| |26 PC4 (D 20) TDO +// RX1/INT0 (D 10) PD2 16| |25 PC3 (D 19) TMS +// TX1/INT1 (D 11) PD3 17| |24 PC2 (D 18) TCK +// PWM (D 12) PD4 18| |23 PC1 (D 17) SDA +// PWM (D 13) PD5 19| |22 PC0 (D 16) SCL +// PWM (D 14) PD6 20| |21 PD7 (D 15) PWM +// +--------+ +// + +/* + PCINT15-8: D7-0 : bit 1 + PCINT31-24: D15-8 : bit 3 + PCINT23-16: D23-16 : bit 2 + PCINT7-0: D31-24 : bit 0 + */ + +#define NUM_DIGITAL_PINS 31 +#define NUM_ANALOG_INPUTS 8 +#define analogInputToDigitalPin(p) ((p < NUM_ANALOG_INPUTS) ? (p) + 24 : -1) + +#define digitalPinHasPWM(p) ((p) == 3 || (p) == 4 || (p) == 7 || (p) == 8 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15) + +static const uint8_t SS = 4; +static const uint8_t MOSI = 5; +static const uint8_t MISO = 6; +static const uint8_t SCK = 7; + +static const uint8_t SDA = 17; +static const uint8_t SCL = 16; +static const uint8_t LED = 7; + +static const uint8_t A0 = 24; +static const uint8_t A1 = 25; +static const uint8_t A2 = 26; +static const uint8_t A3 = 27; +static const uint8_t A4 = 28; +static const uint8_t A5 = 29; +static const uint8_t A6 = 30; +static const uint8_t A7 = 31; + +#define digitalPinToPCICR(p) (((p) >= 0 && (p) < NUM_DIGITAL_PINS) ? (&PCICR) : ((uint8_t *)0)) +#define digitalPinToPCICRbit(p) (((p) <= 7) ? 1 : (((p) <= 15) ? 3 : (((p) <= 23) ? 2 : 0))) +#define digitalPinToPCMSK(p) (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0)))) +#define digitalPinToPCMSKbit(p) ((p) % 8) + +#ifdef ARDUINO_MAIN + +#define PA 1 +#define PB 2 +#define PC 3 +#define PD 4 + +// these arrays map port names (e.g. port B) to the +// appropriate addresses for various functions (e.g. reading +// and writing) +const uint16_t PROGMEM port_to_mode_PGM[] = +{ + NOT_A_PORT, + (uint16_t) &DDRA, + (uint16_t) &DDRB, + (uint16_t) &DDRC, + (uint16_t) &DDRD, +}; + +const uint16_t PROGMEM port_to_output_PGM[] = +{ + NOT_A_PORT, + (uint16_t) &PORTA, + (uint16_t) &PORTB, + (uint16_t) &PORTC, + (uint16_t) &PORTD, +}; + +const uint16_t PROGMEM port_to_input_PGM[] = +{ + NOT_A_PORT, + (uint16_t) &PINA, + (uint16_t) &PINB, + (uint16_t) &PINC, + (uint16_t) &PIND, +}; + +const uint8_t PROGMEM digital_pin_to_port_PGM[] = +{ + PB, /* 0 */ + PB, + PB, + PB, + PB, + PB, + PB, + PB, + PD, /* 8 */ + PD, + PD, + PD, + PD, + PD, + PD, + PD, + PC, /* 16 */ + PC, + PC, + PC, + PC, + PC, + PC, + PC, + PA, /* 24 */ + PA, + PA, + PA, + PA, + PA, + PA, + PA /* 31 */ +}; + +const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = +{ + _BV(0), /* 0, port B */ + _BV(1), + _BV(2), + _BV(3), + _BV(4), + _BV(5), + _BV(6), + _BV(7), + _BV(0), /* 8, port D */ + _BV(1), + _BV(2), + _BV(3), + _BV(4), + _BV(5), + _BV(6), + _BV(7), + _BV(0), /* 16, port C */ + _BV(1), + _BV(2), + _BV(3), + _BV(4), + _BV(5), + _BV(6), + _BV(7), + _BV(0), /* 24, port A */ + _BV(1), + _BV(2), + _BV(3), + _BV(4), + _BV(5), + _BV(6), + _BV(7) +}; + +const uint8_t PROGMEM digital_pin_to_timer_PGM[] = +{ + NOT_ON_TIMER, /* 0 - PB0 */ + NOT_ON_TIMER, /* 1 - PB1 */ + NOT_ON_TIMER, /* 2 - PB2 */ + TIMER0A, /* 3 - PB3 */ + TIMER0B, /* 4 - PB4 */ + NOT_ON_TIMER, /* 5 - PB5 */ + NOT_ON_TIMER, /* 6 - PB6 */ + TIMER3A, /* 7 - PB7 */ + TIMER3B, /* 8 - PD0 */ + NOT_ON_TIMER, /* 9 - PD1 */ + NOT_ON_TIMER, /* 10 - PD2 */ + NOT_ON_TIMER, /* 11 - PD3 */ + TIMER1B, /* 12 - PD4 */ + TIMER1A, /* 13 - PD5 */ + TIMER2B, /* 14 - PD6 */ + TIMER2A, /* 15 - PD7 */ + NOT_ON_TIMER, /* 16 - PC0 */ + NOT_ON_TIMER, /* 17 - PC1 */ + NOT_ON_TIMER, /* 18 - PC2 */ + NOT_ON_TIMER, /* 19 - PC3 */ + NOT_ON_TIMER, /* 20 - PC4 */ + NOT_ON_TIMER, /* 21 - PC5 */ + NOT_ON_TIMER, /* 22 - PC6 */ + NOT_ON_TIMER, /* 23 - PC7 */ + NOT_ON_TIMER, /* 24 - PA0 */ + NOT_ON_TIMER, /* 25 - PA1 */ + NOT_ON_TIMER, /* 26 - PA2 */ + NOT_ON_TIMER, /* 27 - PA3 */ + NOT_ON_TIMER, /* 28 - PA4 */ + NOT_ON_TIMER, /* 29 - PA5 */ + NOT_ON_TIMER, /* 30 - PA6 */ + NOT_ON_TIMER /* 31 - PA7 */ +}; + +#endif // ARDUINO_MAIN + +#endif // Pins_Arduino_h +// vim:ai:cin:sts=2 sw=2 ft=cpp