pentabug/firmware/bootloader/main.c

331 lines
7.7 KiB
C
Raw Normal View History

2012-07-17 01:05:27 +02:00
/*****************************************************************************
2012-10-07 02:04:16 +02:00
*
* Minimal Bootloader for Atmega644(p/a)
*
*
* Based on:
*
* AVRPROG compatible boot-loader
* Version : 0.85 (Dec. 2008)
* Compiler : avr-gcc 4.1.2 / avr-libc 1.4.6
* size : depends on features and startup ( minmal features < 512 words)
* by : Martin Thomas, Kaiserslautern, Germany
* eversmith@heizung-thomas.de
* Additional code and improvements contributed by:
* - Uwe Bonnes
* - Bjoern Riemer
* - Olaf Rempel
*
* License : Copyright (c) 2006-2008 M. Thomas, U. Bonnes, O. Rempel
* Free to use. You have to mention the copyright
* owners in source-code and documentation of derived
* work. No warranty! (Yes, you can insert the BSD
* license here)
*
******************************************************************************/
2012-07-17 01:05:27 +02:00
/* MCU frequency */
#ifndef F_CPU
#define F_CPU 8000000
#endif
/* UART Baudrate */
#define BAUDRATE 115200
/* use "Double Speed Operation" */
#define UART_DOUBLESPEED
/* use second UART on mega128 / can128 / mega162 / mega324p / mega644p */
//#define UART_USE_SECOND
/* Device-Type:
2012-10-07 02:04:16 +02:00
For AVRProg the BOOT-option is prefered
which is the "correct" value for a bootloader.
avrdude may only detect the part-code for ISP */
2012-07-17 01:05:27 +02:00
#define DEVTYPE DEVTYPE_BOOT
// #define DEVTYPE DEVTYPE_ISP
/*
* Pin "STARTPIN" on port "STARTPORT" in this port has to grounded
* (active low) to start the bootloader
*/
#define BLPORT PORTD
#define BLDDR DDRD
#define BLPIN PIND
#define BLPNUM PIND0
/*
* Watchdog-reset is issued at exit
* define the timeout-value here (see avr-libc manual)
*/
#define EXIT_WDT_TIME WDTO_250MS
2012-10-07 02:04:16 +02:00
void __vector_default(void) {
;
}
2012-07-17 01:05:27 +02:00
/*
* define the following if the bootloader should not output
* itself at flash read (will fake an empty boot-section)
*/
#define READ_PROTECT_BOOTLOADER
#define VERSION_HIGH '0'
#define VERSION_LOW '8'
#ifdef UART_DOUBLESPEED
// #define UART_CALC_BAUDRATE(baudRate) (((F_CPU*10UL) / ((baudRate) *8UL) +5)/10 -1)
#define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 4UL)) / ((uint32_t)(baudRate) * 8UL) - 1)
#else
// #define UART_CALC_BAUDRATE(baudRate) (((F_CPU*10UL) / ((baudRate)*16UL) +5)/10 -1)
#define UART_CALC_BAUDRATE(baudRate) ((uint32_t)((F_CPU) + ((uint32_t)baudRate * 8UL)) / ((uint32_t)(baudRate) * 16UL) - 1)
#endif
#include <stdint.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/boot.h>
#include <avr/pgmspace.h>
//#include <avr/interrupt.h>
//#include <util/delay.h>
#include "chipdef.h"
uint8_t gBuffer[SPM_PAGESIZE];
2012-10-07 02:04:16 +02:00
static void sendchar(uint8_t data) {
while (!(UART_STATUS & (1 << UART_TXREADY)))
;
2012-07-17 01:05:27 +02:00
UART_DATA = data;
}
2012-10-07 02:04:16 +02:00
static uint8_t recvchar(void) {
while (!(UART_STATUS & (1 << UART_RXREADY)))
;
2012-07-17 01:05:27 +02:00
return UART_DATA;
}
2012-10-07 02:04:16 +02:00
static inline void eraseFlash(void) {
2012-07-17 01:05:27 +02:00
// erase only main section (bootloader protection)
uint32_t addr = 0;
while (APP_END > addr) {
2012-10-07 02:04:16 +02:00
boot_page_erase(addr);
// Perform page erase
boot_spm_busy_wait(); // Wait until the memory is erased.
2012-07-17 01:05:27 +02:00
addr += SPM_PAGESIZE;
}
boot_rww_enable();
}
2012-10-07 02:04:16 +02:00
static inline void recvBuffer(pagebuf_t size) {
2012-07-17 01:05:27 +02:00
pagebuf_t cnt;
uint8_t *tmp = gBuffer;
for (cnt = 0; cnt < sizeof(gBuffer); cnt++) {
*tmp++ = (cnt < size) ? recvchar() : 0xFF;
}
}
2012-10-07 02:04:16 +02:00
static inline uint16_t writeFlashPage(uint16_t waddr, pagebuf_t size) {
uint32_t pagestart = (uint32_t) waddr << 1;
2012-07-17 01:05:27 +02:00
uint32_t baddr = pagestart;
uint16_t data;
uint8_t *tmp = gBuffer;
do {
data = *tmp++;
data |= *tmp++ << 8;
2012-10-07 02:04:16 +02:00
if (baddr < APP_END) {
boot_page_fill(baddr, data);
// call asm routine.
2012-07-17 01:05:27 +02:00
}
2012-10-07 02:04:16 +02:00
baddr += 2; // Select next word in memory
size -= 2; // Reduce number of bytes to write by two
} while (size); // Loop until all bytes written
2012-07-17 01:05:27 +02:00
boot_page_write(pagestart);
boot_spm_busy_wait();
2012-10-07 02:04:16 +02:00
boot_rww_enable(); // Re-enable the RWW section
2012-07-17 01:05:27 +02:00
2012-10-07 02:04:16 +02:00
return baddr >> 1;
2012-07-17 01:05:27 +02:00
}
2012-10-07 02:04:16 +02:00
static inline uint16_t readFlashPage(uint16_t waddr, pagebuf_t size) {
uint32_t baddr = (uint32_t) waddr << 1;
2012-07-17 01:05:27 +02:00
uint16_t data;
do {
#ifndef READ_PROTECT_BOOTLOADER
#warning "Bootloader not read-protected"
#if defined(RAMPZ)
data = pgm_read_word_far(baddr);
#else
data = pgm_read_word_near(baddr);
#endif
#else
// don't read bootloader
2012-10-07 02:04:16 +02:00
if (baddr < APP_END) {
2012-07-17 01:05:27 +02:00
#if defined(RAMPZ)
data = pgm_read_word_far(baddr);
#else
data = pgm_read_word_near(baddr);
#endif
2012-10-07 02:04:16 +02:00
} else {
2012-07-17 01:05:27 +02:00
data = 0xFFFF; // fake empty
}
#endif
2012-10-07 02:04:16 +02:00
sendchar(data); // send LSB
sendchar((data >> 8)); // send MSB
baddr += 2; // Select next word in memory
size -= 2; // Subtract two bytes from number of bytes to read
} while (size); // Repeat until block has been read
2012-07-17 01:05:27 +02:00
2012-10-07 02:04:16 +02:00
return baddr >> 1;
2012-07-17 01:05:27 +02:00
}
2012-10-07 02:04:16 +02:00
static void send_boot(void) {
2012-07-17 01:05:27 +02:00
sendchar('A');
sendchar('V');
sendchar('R');
sendchar('B');
sendchar('O');
sendchar('O');
sendchar('T');
}
static void (*jump_to_app)(void) = 0x0000;
2012-10-07 02:04:16 +02:00
int main(void) {
2012-07-17 01:05:27 +02:00
2012-10-07 02:04:16 +02:00
bootloader_wdt_off();
2012-07-17 01:05:27 +02:00
uint16_t address = 0;
uint8_t device = 0, val;
2012-10-07 02:04:16 +02:00
BLDDR &= ~(1 << BLPNUM); // set as Input
BLPORT |= (1 << BLPNUM); // Enable pullup
2012-07-17 01:05:27 +02:00
2012-10-07 02:04:16 +02:00
UART_STATUS = (1 << UART_DOUBLE);
2012-07-17 01:05:27 +02:00
UART_BAUD_HIGH = 0;
UART_BAUD_LOW = 25;
UART_CTRL = UART_CTRL_DATA;
UART_CTRL2 = UART_CTRL2_DATA;
2012-10-07 02:04:16 +02:00
if (BLPIN & (1 << BLPNUM)) {
2012-07-17 01:05:27 +02:00
// jump to main app if pin is not grounded and GPIOR2 is zero
2012-10-07 02:04:16 +02:00
jump_to_app(); // Jump to application sector
2012-07-17 01:05:27 +02:00
}
2012-10-07 02:04:16 +02:00
for (;;) {
2012-07-17 01:05:27 +02:00
val = recvchar();
// Autoincrement?
if (val == 'a') {
2012-10-07 02:04:16 +02:00
sendchar('Y'); // Autoincrement is quicker
2012-07-17 01:05:27 +02:00
2012-10-07 02:04:16 +02:00
//write address
2012-07-17 01:05:27 +02:00
} else if (val == 'A') {
2012-10-07 02:04:16 +02:00
address = recvchar(); //read address 8 MSB
address = (address << 8) | recvchar();
2012-07-17 01:05:27 +02:00
sendchar('\r');
2012-10-07 02:04:16 +02:00
// Buffer load support
2012-07-17 01:05:27 +02:00
} else if (val == 'b') {
2012-10-07 02:04:16 +02:00
sendchar('Y'); // Report buffer load supported
sendchar((sizeof(gBuffer) >> 8) & 0xFF); // Report buffer size in bytes
2012-07-17 01:05:27 +02:00
sendchar(sizeof(gBuffer) & 0xFF);
2012-10-07 02:04:16 +02:00
// Start buffer load
2012-07-17 01:05:27 +02:00
} else if (val == 'B') {
pagebuf_t size;
2012-10-07 02:04:16 +02:00
size = recvchar() << 8; // Load high byte of buffersize
size |= recvchar(); // Load low byte of buffersize
val = recvchar(); // Load memory type ('E' or 'F')
2012-07-17 01:05:27 +02:00
recvBuffer(size);
if (device == DEVTYPE) {
if (val == 'F') {
address = writeFlashPage(address, size);
}
sendchar('\r');
} else {
sendchar(0);
}
2012-10-07 02:04:16 +02:00
// Block read
2012-07-17 01:05:27 +02:00
} else if (val == 'g') {
pagebuf_t size;
2012-10-07 02:04:16 +02:00
size = recvchar() << 8; // Load high byte of buffersize
size |= recvchar(); // Load low byte of buffersize
val = recvchar(); // Get memtype
2012-07-17 01:05:27 +02:00
if (val == 'F') {
address = readFlashPage(address, size);
}
2012-10-07 02:04:16 +02:00
// Chip erase
} else if (val == 'e') {
2012-07-17 01:05:27 +02:00
if (device == DEVTYPE) {
eraseFlash();
}
sendchar('\r');
2012-10-07 02:04:16 +02:00
// Exit upgrade
2012-07-17 01:05:27 +02:00
} else if (val == 'E') {
2012-10-07 02:04:16 +02:00
wdt_enable(EXIT_WDT_TIME);
// Enable Watchdog Timer to give reset
2012-07-17 01:05:27 +02:00
sendchar('\r');
2012-10-07 02:04:16 +02:00
// Enter programming mode
2012-07-17 01:05:27 +02:00
} else if (val == 'P') {
sendchar('\r');
2012-10-07 02:04:16 +02:00
// Leave programming mode
2012-07-17 01:05:27 +02:00
} else if (val == 'L') {
sendchar('\r');
2012-10-07 02:04:16 +02:00
// return programmer type
2012-07-17 01:05:27 +02:00
} else if (val == 'p') {
2012-10-07 02:04:16 +02:00
sendchar('S'); // always serial programmer
2012-07-17 01:05:27 +02:00
2012-10-07 02:04:16 +02:00
// Return device type
2012-07-17 01:05:27 +02:00
} else if (val == 't') {
sendchar(DEVTYPE);
sendchar(0);
2012-10-07 02:04:16 +02:00
// clear and set LED ignored
2012-07-17 01:05:27 +02:00
} else if ((val == 'x') || (val == 'y')) {
recvchar();
sendchar('\r');
2012-10-07 02:04:16 +02:00
// set device
2012-07-17 01:05:27 +02:00
} else if (val == 'T') {
device = recvchar();
sendchar('\r');
2012-10-07 02:04:16 +02:00
// Return software identifier
2012-07-17 01:05:27 +02:00
} else if (val == 'S') {
send_boot();
2012-10-07 02:04:16 +02:00
// Return Software Version
2012-07-17 01:05:27 +02:00
} else if (val == 'V') {
sendchar(VERSION_HIGH);
sendchar(VERSION_LOW);
2012-10-07 02:04:16 +02:00
// Return Signature Bytes (it seems that
// AVRProg expects the "Atmel-byte" 0x1E last
// but shows it first in the dialog-window)
2012-07-17 01:05:27 +02:00
} else if (val == 's') {
sendchar(SIG_BYTE3);
sendchar(SIG_BYTE2);
sendchar(SIG_BYTE1);
2012-10-07 02:04:16 +02:00
/* ESC */
} else if (val != 0x1b) {
2012-07-17 01:05:27 +02:00
sendchar('?');
}
}
return 0;
}