cleanup source

This commit is contained in:
Olaf Rempel 2008-03-27 20:06:02 +01:00
parent b7dc9c9984
commit e1c00dc403

328
ispprog.c
View File

@ -1,7 +1,7 @@
/* /*
* C based avr910 / avr109 ISP Adapter * C based avr910 / avr109 ISP Adapter
* *
* (c) 05/2006 by Olaf Rempel * (c) 2006-2008 by Olaf Rempel
* <razzor at kopf minus tisch dot de> * <razzor at kopf minus tisch dot de>
* *
* using ATmega16 @7.3728MHz: * using ATmega16 @7.3728MHz:
@ -14,74 +14,120 @@
* *
*/ */
#include <avr/io.h> #include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#define F_CPU 7372800 #define F_CPU 7372800
#define BAUDRATE 115200 #define BAUDRATE 115200
#define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1) /* SPI Clock: F_CPU /4 (1.8432MHz) or /128 (57.6kHz) */
#define SPI_MODE ((1<<SPE) | (1<<MSTR))
//#define SPI_MODE ((1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0))
#define ISP_RESET PORTB1
#define ISP_LED PORTB3
#define ISP_MOSI PORTB5
#define ISP_MISO PORTB6
#define ISP_SCK PORTB7
#define RESET_IN PORTD3
struct {
uint8_t id; /* device id */
uint8_t pagemask; /* bitmask of one flash-page */
uint8_t pollcode; /* value of an empty flash-cell */
} devices[] = {
// { 0x20, 0x00, 0x7F }, /* at90s2313 (no paging, reads 0x7F back) */
{ 0x20, 0x0F, 0xFF }, /* tiny24 (16 words/page) */
{ 0x38, 0x00, 0x7F }, /* at90s8515 (no paging, reads 0x7F back) */
{ 0x43, 0x7F, 0xFF }, /* mega128 (128 words/page) */
{ 0x72, 0x3F, 0xFF }, /* mega32 (64 words/page) */
{ 0x74, 0x3F, 0xFF }, /* mega16 (64 words/page) */
{ 0x76, 0x1F, 0xFF }, /* mega8 (32 words/page) */
{ 0x00, 0x00, 0x00 },
};
// needs F_CPU
#include <util/delay.h> #include <util/delay.h>
#define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1)
#define LED_OFF 0x00 #define LED_OFF 0x00
#define LED_ON 0x01 #define LED_ON 0x01
#define LED_FAST 0x02 #define LED_FAST 0x02
#define LED_SLOW 0x03 #define LED_SLOW 0x03
struct { #define CMD_PROG_ENABLE_1 0xAC
uint8_t id; // device id #define CMD_PROG_ENABLE_2 0x53
uint8_t pagemask; // bitmask of one flash-page #define CMD_CHIP_ERASE_1 0xAC
uint8_t pollcode; // value of an empty flash-cell #define CMD_CHIP_ERASE_2 0x80
#define CMD_POLL_BUSY_1 0xF0 /* not used */
#define CMD_POLL_BUSY_2 0x00 /* not used */
} devices[] = { #define CMD_LOAD_EADDR_1 0x4D /* not used */
#define CMD_LOAD_EADDR_2 0x00 /* not used */
#define CMD_LOAD_FLASH_HI 0x48
#define CMD_LOAD_FLASH_LO 0x40
#define CMD_LOAD_EEPROM_PAGE 0xC1 /* not used */
// { 0x20, 0x00, 0x7F }, // at90s2313 (no paging, reads 0x7F back) #define CMD_READ_FLASH_LO 0x20
{ 0x20, 0x0F, 0xFF }, // tiny24 (16 words/page) #define CMD_READ_FLASH_HI 0x28
{ 0x38, 0x00, 0x7F }, // at90s8515 (no paging, reads 0x7F back) #define CMD_READ_EEPROM 0xA0
{ 0x43, 0x7F, 0xFF }, // mega128 (128 words/page) #define CMD_READ_LOCK_1 0x58
{ 0x72, 0x3F, 0xFF }, // mega32 (64 words/page) #define CMD_READ_LOCK_2 0x00
{ 0x74, 0x3F, 0xFF }, // mega16 (64 words/page) #define CMD_READ_SIG_1 0x30
{ 0x76, 0x1F, 0xFF }, // mega8 (32 words/page) #define CMD_READ_SIG_2 0x00
{ 0x00, 0x00, 0x00 }, #define CMD_READ_FUSE_1 0x50
}; #define CMD_READ_FUSE_2 0x00
#define CMD_READ_FUSE_H_1 0x58
#define CMD_READ_FUSE_H_2 0x08
#define CMD_READ_FUSE_E_1 0x50
#define CMD_READ_FUSE_E_2 0x08
#define CMD_READ_CAL 0x38 /* not used */
uint16_t lastaddr = 0; #define CMD_WRITE_FLASH_PAGE 0x4C
uint8_t lastcmd = 0, lastval = 0, pollcode = 0xFF; #define CMD_WRITE_EEPROM 0xC0
#define CMD_WRITE_EEPROM_PAGE 0xC2 /* not used */
#define CMD_WRITE_LOCK_1 0xAC
#define CMD_WRITE_LOCK_2 0xE0
#define CMD_WRITE_FUSE_1 0xAC
#define CMD_WRITE_FUSE_2 0xA0
#define CMD_WRITE_FUSE_H_1 0xAC /* not used */
#define CMD_WRITE_FUSE_H_2 0xA8 /* not used */
#define CMD_WRITE_FUSE_E_1 0xAC /* not used */
#define CMD_WRITE_FUSE_E_2 0xA4 /* not used */
uint8_t page_buf[256]; static uint16_t lastaddr;
static uint8_t lastcmd;
static uint8_t lastval;
static uint8_t pollcode = 0xFF;
/* toggle LED */
ISR(SIG_OUTPUT_COMPARE1A) ISR(SIG_OUTPUT_COMPARE1A)
{ {
// toggle LED PORTB ^= (1<<ISP_LED);
PORTB ^= (1<<PORTB3);
} }
/* Send one byte to PC */ /* Send one byte to PC */
void ser_send(uint8_t data) static void ser_send(uint8_t data)
{ {
loop_until_bit_is_set(UCSRA, UDRIE); loop_until_bit_is_set(UCSRA, UDRIE);
UDR = data; UDR = data;
} }
/* Receive one byte from PC */ /* Receive one byte from PC */
uint8_t ser_recv(void) static uint8_t ser_recv(void)
{ {
loop_until_bit_is_set(UCSRA, RXC); loop_until_bit_is_set(UCSRA, RXC);
return UDR; return UDR;
} }
/* Check if receiver ready */ /* Check if receiver ready */
uint8_t ser_recv_ready(void) static uint8_t ser_recv_ready(void)
{ {
return bit_is_set(UCSRA, RXC); return bit_is_set(UCSRA, RXC);
} }
/* Send one byte to target, and return received one */ /* Send one byte to target, and return received one */
uint8_t spi_rxtx(uint8_t val) static uint8_t spi_rxtx(uint8_t val)
{ {
SPDR = val; SPDR = val;
loop_until_bit_is_set(SPSR, SPIF); loop_until_bit_is_set(SPSR, SPIF);
@ -89,7 +135,7 @@ uint8_t spi_rxtx(uint8_t val)
} }
/* Set LED mode */ /* Set LED mode */
void led_mode(uint8_t mode) static void led_mode(uint8_t mode)
{ {
static uint8_t oldmode = LED_OFF; static uint8_t oldmode = LED_OFF;
@ -98,66 +144,59 @@ void led_mode(uint8_t mode)
} else if (mode == LED_ON) { } else if (mode == LED_ON) {
TCCR1B = 0x00; TCCR1B = 0x00;
PORTB &= ~(1<<PORTB3); PORTB &= ~(1<<ISP_LED);
} else if (mode == LED_OFF) { } else if (mode == LED_OFF) {
TCCR1B = 0x00; TCCR1B = 0x00;
PORTB |= (1<<PORTB3); PORTB |= (1<<ISP_LED);
} else if (mode == LED_FAST) { } else if (mode == LED_FAST) {
// 100ms reload /* timer1: F_CPU /64, CTC mode via OutputCompare A, 10Hz */
OCR1A = 0x2D00; OCR1A = (F_CPU / (64 * 10));
// timer1 prescaler 64, CTC mode via OutputCompare A
TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10); TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10);
} else if (mode == LED_SLOW) { } else if (mode == LED_SLOW) {
// 250ms reload /* timer1: FCPU /64, CTC mode via OutputCompare A, 4Hz */
OCR1A = 0x7080; OCR1A = (F_CPU / (64 * 4));
// timer1 prescaler 64, CTC mode via OutputCompare A
TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10); TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10);
} }
oldmode = mode; oldmode = mode;
} }
/* Control reset and SPI lines */ /* Control reset and SPI lines */
void set_reset(uint8_t mode) static void set_reset(uint8_t mode)
{ {
// make RESET, SCK and MOSI inputs
if (mode) { if (mode) {
DDRB &= ~((1<<PORTB7) | (1<<PORTB5) | (1<<PORTB1)); /* ISP_SCK, ISP_MOSI and ISP_RESET are inputs */
PORTB |= (1<<PORTB1); DDRB &= ~((1<<ISP_SCK) | (1<<ISP_MOSI) | (1<<ISP_RESET));
PORTB |= (1<<ISP_RESET);
// make RESET, SCK & MOSI outputs, set RESET low
} else { } else {
DDRB |= ((1<<PORTB7) | (1<<PORTB5) | (1<<PORTB1)); /*ISP_SCK, ISP_MOSI and ISP_RESET are outputs, set ISP_RESET low */
PORTB &= ~(1<<PORTB1); DDRB |= ((1<<ISP_SCK) | (1<<ISP_MOSI) | (1<<ISP_RESET));
PORTB &= ~(1<<ISP_RESET);
} }
// wait 50ms
_delay_ms(25); _delay_ms(25);
_delay_ms(25); _delay_ms(25);
} }
/* /* writes a byte to target flash/eeprom */
* writes a byte to target flash/eeprom static void mem_write(uint8_t cmd, uint16_t addr, uint8_t val)
* remeber the values for polling
*/
void mem_write(uint8_t cmd, uint16_t addr, uint8_t val)
{ {
spi_rxtx(cmd); spi_rxtx(cmd);
spi_rxtx(addr >> 8); spi_rxtx(addr >> 8);
spi_rxtx(addr & 0xFF); spi_rxtx(addr & 0xFF);
spi_rxtx(val); spi_rxtx(val);
/* remember values for polling */
lastcmd = cmd; lastcmd = cmd;
lastaddr = addr; lastaddr = addr;
lastval = val; lastval = val;
} }
/* read a byte from target flash/eeprom */ /* read a byte from target flash/eeprom */
uint8_t mem_read(uint8_t cmd, uint16_t addr) static uint8_t mem_read(uint8_t cmd, uint16_t addr)
{ {
spi_rxtx(cmd); spi_rxtx(cmd);
spi_rxtx(addr >> 8); spi_rxtx(addr >> 8);
@ -166,28 +205,31 @@ uint8_t mem_read(uint8_t cmd, uint16_t addr)
} }
/* wait until byte/page is written to target memory */ /* wait until byte/page is written to target memory */
void poll(void) static void poll(void)
{ {
uint8_t cmd, val, poll = 0xFF; uint8_t cmd, val, poll = 0xFF;
if (lastcmd == 0xC0) { if (lastcmd == CMD_WRITE_EEPROM) {
// we can not poll, wait default value /* check if we can poll */
if (lastval == pollcode || lastval == 0x7F || lastval == 0x80) { if (lastval == pollcode || lastval == 0x7F || lastval == 0x80) {
/* wait default time */
_delay_ms(10); _delay_ms(10);
return; return;
} }
cmd = 0xA0; cmd = CMD_READ_EEPROM;
} else { } else {
// we can not poll, wait default value /* check if we can poll */
if (lastval == pollcode) { if (lastval == pollcode) {
/* wait default time */
_delay_ms(10); _delay_ms(10);
return; return;
} }
/* CMD_WRITE_FLASH -> CMD_READ_FLASH */
cmd = (lastcmd & 0x08) | 0x20; cmd = (lastcmd & 0x08) | 0x20;
} }
/* read until we get correct value */ /* poll until we get correct value */
do { do {
val = mem_read(cmd, lastaddr); val = mem_read(cmd, lastaddr);
} while ((val != lastval) && poll--); } while ((val != lastval) && poll--);
@ -195,38 +237,38 @@ void poll(void)
int main(void) int main(void)
{ {
static uint8_t page_buf[256];
uint16_t addr = 0; uint16_t addr = 0;
uint8_t device = 0, pagemask = 0; uint8_t device = 0, pagemask = 0;
// reset & activity as outputs, pullup SlaveSelect /* ISP_RESET and ISP_LED are outputs, pullup SlaveSelect */
PORTB = (1<<PORTB1) | (1<<PORTB3) | (1<<PORTB4); PORTB = (1<<ISP_RESET) | (1<<ISP_LED) | (1<<PORTB4);
DDRB = (1<<PORTB1) | (1<<PORTB3); DDRB = (1<<ISP_RESET) | (1<<ISP_LED);
// Set baud rate /* Set baud rate */
UBRRH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF; UBRRH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF); UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
// enable rx/tx, 8n1 /* enable usart with 8n1 */
UCSRB = (1<<TXEN) | (1<<RXEN); UCSRB = (1<<TXEN) | (1<<RXEN);
UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0); UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
// SPI enabled, Master mode, F_OSC /4 (F_OSC /128) /* enable SPI master mode */
SPCR = (1<<SPE) | (1<<MSTR); SPCR = SPI_MODE;
// SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);
// enable timer1 OutputCompare A interrupt (CTC hit) /* enable timer1 OutputCompare A interrupt (CTC hit) */
TIMSK = (1<<OCIE1A); TIMSK = (1<<OCIE1A);
sei(); sei();
// disable reset /* disable ISP_RESET */
set_reset(1); set_reset(1);
while (1) { while (1) {
uint8_t pulse = 0; uint8_t pulse = 0;
while (!ser_recv_ready()) { while (!ser_recv_ready()) {
// reset the target /* reset the target */
if (PIND & (1<<PIND3)) { if (PIND & (1<<RESET_IN)) {
if (!pulse) { if (!pulse) {
led_mode(LED_ON); led_mode(LED_ON);
set_reset(0); set_reset(0);
@ -241,7 +283,7 @@ int main(void)
} }
switch (ser_recv()) { switch (ser_recv()) {
// Enter programming mode /* Enter programming mode */
case 'P': { case 'P': {
uint8_t sync, count = 0x20; uint8_t sync, count = 0x20;
led_mode(LED_ON); led_mode(LED_ON);
@ -249,8 +291,8 @@ int main(void)
set_reset(1); set_reset(1);
set_reset(0); set_reset(0);
spi_rxtx(0xAC); spi_rxtx(CMD_PROG_ENABLE_1);
spi_rxtx(0x53); spi_rxtx(CMD_PROG_ENABLE_2);
sync = spi_rxtx(0x00); sync = spi_rxtx(0x00);
spi_rxtx(0x00); spi_rxtx(0x00);
@ -260,36 +302,36 @@ int main(void)
break; break;
} }
// Autoincrement address /* Autoincrement address */
case 'a': case 'a':
ser_send('Y'); ser_send('Y');
break; break;
// Set address /* Set address */
case 'A': case 'A':
addr = (ser_recv() << 8); addr = (ser_recv() << 8);
addr |= ser_recv(); addr |= ser_recv();
ser_send('\r'); ser_send('\r');
break; break;
// Write program memory, low byte /* Write program memory, low byte */
case 'c': case 'c':
led_mode(LED_FAST); led_mode(LED_FAST);
mem_write(0x40, addr, ser_recv()); mem_write(CMD_LOAD_FLASH_LO, addr, ser_recv());
// poll on byte addressed targets /* poll on byte addressed targets */
if (!pagemask) if (!pagemask)
poll(); poll();
ser_send('\r'); ser_send('\r');
break; break;
// Write program memory, high byte /* Write program memory, high byte */
case 'C': case 'C':
led_mode(LED_FAST); led_mode(LED_FAST);
mem_write(0x48, addr, ser_recv()); mem_write(CMD_LOAD_FLASH_HI, addr, ser_recv());
// poll on byte addressed targets /* poll on byte addressed targets */
if (!pagemask) if (!pagemask)
poll(); poll();
@ -297,10 +339,10 @@ int main(void)
ser_send('\r'); ser_send('\r');
break; break;
// Issue Page Write /* Issue Page Write */
case 'm': case 'm':
led_mode(LED_FAST); led_mode(LED_FAST);
spi_rxtx(0x4C); spi_rxtx(CMD_WRITE_FLASH_PAGE);
spi_rxtx(lastaddr >> 8); spi_rxtx(lastaddr >> 8);
spi_rxtx(lastaddr & 0xFF); spi_rxtx(lastaddr & 0xFF);
spi_rxtx(0x00); spi_rxtx(0x00);
@ -310,41 +352,41 @@ int main(void)
ser_send('\r'); ser_send('\r');
break; break;
// Read Lock Bits /* Read Lock Bits */
case 'r': case 'r':
ser_send(mem_read(0x58, 0x0000)); ser_send(mem_read(CMD_READ_LOCK_1, CMD_READ_LOCK_2 << 8));
ser_send('\r'); ser_send('\r');
break; break;
// Read program memory /* Read program memory */
case 'R': case 'R':
led_mode(LED_SLOW); led_mode(LED_SLOW);
ser_send(mem_read(0x28, addr)); ser_send(mem_read(CMD_READ_FLASH_HI, addr));
ser_send(mem_read(0x20, addr)); ser_send(mem_read(CMD_READ_FLASH_LO, addr));
addr++; addr++;
break; break;
// Read data memory /* Read data memory */
case 'd': case 'd':
led_mode(LED_SLOW); led_mode(LED_SLOW);
ser_send(mem_read(0xA0, addr)); ser_send(mem_read(CMD_READ_EEPROM, addr));
addr++; addr++;
break; break;
// Write data memory /* Write data memory */
case 'D': case 'D':
led_mode(LED_FAST); led_mode(LED_FAST);
mem_write(0xC0, addr, ser_recv()); mem_write(CMD_WRITE_EEPROM, addr, ser_recv());
poll(); poll();
addr++; addr++;
ser_send('\r'); ser_send('\r');
break; break;
// Chip erase /* Chip erase */
case 'e': case 'e':
spi_rxtx(0xAC); spi_rxtx(CMD_CHIP_ERASE_1);
spi_rxtx(0x80); spi_rxtx(CMD_CHIP_ERASE_2);
spi_rxtx(0x00); spi_rxtx(0x00);
spi_rxtx(0x00); spi_rxtx(0x00);
@ -352,11 +394,11 @@ int main(void)
ser_send('\r'); ser_send('\r');
break; break;
// Write lock bits /* Write lock bits */
case 'l': { case 'l': {
uint8_t val = ser_recv(); uint8_t val = ser_recv();
spi_rxtx(0xAC); spi_rxtx(CMD_WRITE_LOCK_1);
spi_rxtx(0xE0); spi_rxtx(CMD_WRITE_LOCK_2);
spi_rxtx(0x00); spi_rxtx(0x00);
spi_rxtx(val); spi_rxtx(val);
@ -365,41 +407,32 @@ int main(void)
break; break;
} }
// Read fuse bits /* Read fusebits */
case 'F': case 'F':
spi_rxtx(0x50); ser_send(mem_read(CMD_READ_FUSE_1, CMD_READ_FUSE_2 << 8));
spi_rxtx(0x00);
spi_rxtx(0x00);
ser_send(spi_rxtx(0x00));
break; break;
// Return High Fusebits /* Read high fusebits */
case 'N': case 'N':
spi_rxtx(0x58); ser_send(mem_read(CMD_READ_FUSE_H_1, CMD_READ_FUSE_H_2 << 8));
spi_rxtx(0x08);
spi_rxtx(0x00);
ser_send(spi_rxtx(0x00));
break; break;
// Return extendet Fusebits /* Read extended fusebits */
case 'Q': case 'Q':
spi_rxtx(0x50); ser_send(mem_read(CMD_READ_FUSE_E_1, CMD_READ_FUSE_E_2 << 8));
spi_rxtx(0x08);
spi_rxtx(0x00);
ser_send(spi_rxtx(0x00));
break; break;
// Leave programming mode /* Leave programming mode */
case 'L': case 'L':
// Exit /* Exit Bootloader */
case 'E': case 'E':
set_reset(1); set_reset(1);
led_mode(LED_OFF); led_mode(LED_OFF);
ser_send('\r'); ser_send('\r');
break; break;
// Select device type /* Select device type */
case 'T': { case 'T': {
uint8_t val, i = 0; uint8_t val, i = 0;
val = ser_recv(); val = ser_recv();
@ -418,19 +451,16 @@ int main(void)
break; break;
} }
// Read signature bytes /* Read signature bytes */
case 's': { case 's': {
uint8_t i = 2; uint8_t i = 2;
do { do {
spi_rxtx(0x30); ser_send(mem_read(CMD_READ_SIG_1, (CMD_READ_SIG_2 << 8) | i));
spi_rxtx(0x00);
spi_rxtx(i);
ser_send(spi_rxtx(0x00));
} while (i--); } while (i--);
break; break;
} }
// Return supported device codes /* Return supported device codes */
case 't': { case 't': {
uint8_t val, i = 0; uint8_t val, i = 0;
do { do {
@ -440,7 +470,7 @@ int main(void)
break; break;
} }
// Return software identifier /* Return software identifier */
case 'S': case 'S':
ser_send('A'); ser_send('A');
ser_send('V'); ser_send('V');
@ -451,43 +481,43 @@ int main(void)
ser_send('P'); ser_send('P');
break; break;
// Return software version /* Return software version */
case 'V': case 'V':
ser_send('3'); ser_send('3');
ser_send('8'); ser_send('8');
break; break;
// Return hardware version /* Return hardware version */
case 'v': case 'v':
ser_send('1'); ser_send('1');
ser_send('2'); ser_send('2');
break; break;
// Return programmer type /* Return programmer type */
case 'p': case 'p':
ser_send('S'); ser_send('S');
break; break;
// Set LED /* Set LED */
case 'x': case 'x':
ser_recv(); ser_recv();
led_mode(LED_ON); led_mode(LED_ON);
break; break;
// Clear LED /* Clear LED */
case 'y': case 'y':
ser_recv(); ser_recv();
led_mode(LED_OFF); led_mode(LED_OFF);
break; break;
// Report Block write Mode /* Report Block write Mode */
case 'b': case 'b':
ser_send('Y'); ser_send('Y');
ser_send(sizeof(page_buf) >> 8); ser_send(sizeof(page_buf) >> 8);
ser_send(sizeof(page_buf) & 0xFF); ser_send(sizeof(page_buf) & 0xFF);
break; break;
// Block Write /* Block Write */
case 'B': { case 'B': {
uint16_t size, i; uint16_t size, i;
uint8_t type; uint8_t type;
@ -503,14 +533,14 @@ int main(void)
if (type == 'F') { if (type == 'F') {
for (i = 0; i < size; i += 2) { for (i = 0; i < size; i += 2) {
mem_write(0x40, addr, page_buf[i]); mem_write(CMD_LOAD_FLASH_LO, addr, page_buf[i]);
mem_write(0x48, addr, page_buf[i+1]); mem_write(CMD_LOAD_FLASH_HI, addr, page_buf[i+1]);
addr++; addr++;
// page write on page-boundry /* page write on page-boundry */
if ((addr & pagemask) == 0x00) { if ((addr & pagemask) == 0x00) {
spi_rxtx(0x4C); spi_rxtx(CMD_WRITE_FLASH_PAGE);
spi_rxtx(lastaddr >> 8); spi_rxtx(lastaddr >> 8);
spi_rxtx(lastaddr & 0xFF); spi_rxtx(lastaddr & 0xFF);
spi_rxtx(0x00); spi_rxtx(0x00);
@ -519,9 +549,9 @@ int main(void)
} }
} }
// last page /* last page */
if (size != sizeof(page_buf)) { if (size != sizeof(page_buf)) {
spi_rxtx(0x4C); spi_rxtx(CMD_WRITE_FLASH_PAGE);
spi_rxtx(lastaddr >> 8); spi_rxtx(lastaddr >> 8);
spi_rxtx(lastaddr & 0xFF); spi_rxtx(lastaddr & 0xFF);
spi_rxtx(0x00); spi_rxtx(0x00);
@ -531,7 +561,7 @@ int main(void)
} else if (type == 'E') { } else if (type == 'E') {
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
mem_write(0xC0, addr, page_buf[i]); mem_write(CMD_WRITE_EEPROM, addr, page_buf[i]);
poll(); poll();
addr++; addr++;
} }
@ -540,7 +570,7 @@ int main(void)
break; break;
} }
// Block Read /* Block Read */
case 'g': { case 'g': {
uint16_t size, i; uint16_t size, i;
uint8_t type; uint8_t type;
@ -553,25 +583,25 @@ int main(void)
if (type == 'F') { if (type == 'F') {
for (i = 0; i < size; i += 2) { for (i = 0; i < size; i += 2) {
ser_send(mem_read(0x20, addr)); ser_send(mem_read(CMD_READ_FLASH_LO, addr));
ser_send(mem_read(0x28, addr)); ser_send(mem_read(CMD_READ_FLASH_HI, addr));
addr++; addr++;
} }
} else if (type == 'E') { } else if (type == 'E') {
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
ser_send(mem_read(0xA0, addr)); ser_send(mem_read(CMD_READ_EEPROM, addr));
addr++; addr++;
} }
} }
break; break;
} }
// Write fuse bits /* Write fusebits */
case 'f': { case 'f': {
uint8_t val = ser_recv(); uint8_t val = ser_recv();
spi_rxtx(0xAC); spi_rxtx(CMD_WRITE_FUSE_1);
spi_rxtx(0xA0); spi_rxtx(CMD_WRITE_FUSE_2);
spi_rxtx(0x00); spi_rxtx(0x00);
spi_rxtx(val); spi_rxtx(val);
@ -580,7 +610,7 @@ int main(void)
break; break;
} }
// Universial command /* Universial command */
case ':': { case ':': {
uint8_t val[3]; uint8_t val[3];
val[0] = ser_recv(); val[0] = ser_recv();
@ -596,7 +626,7 @@ int main(void)
break; break;
} }
// New universal command /* New universal command */
case '.': { case '.': {
uint8_t val[4]; uint8_t val[4];
val[0] = ser_recv(); val[0] = ser_recv();
@ -614,7 +644,7 @@ int main(void)
break; break;
} }
// ESC /* ESC */
case 0x1B: case 0x1B:
break; break;