ispprog/ispprog.c

880 lines
23 KiB
C
Raw Normal View History

2011-06-25 16:19:18 +02:00
/***************************************************************************
* C based avr910 / avr109 ISP Adapter *
* *
2019-12-31 17:12:58 +01:00
* Copyright (C) 2006 - 2020 by Olaf Rempel *
2011-06-25 16:19:18 +02:00
* razzor AT kopf MINUS tisch DOT de *
* *
* 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; version 2 of the License, *
* *
* 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. *
***************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
2019-12-30 20:26:58 +01:00
#include "avrdevice.h"
2020-01-08 22:55:25 +01:00
#include "display.h"
2020-01-12 18:12:49 +01:00
#include "target.h"
2008-03-27 20:06:02 +01:00
#include <util/delay.h>
#define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1)
2006-05-06 22:55:42 +02:00
2011-06-25 16:19:18 +02:00
/* F_CPU /4 (1.8432MHz) */
2012-11-18 13:12:27 +01:00
#define SPI_MODE4 ((1<<SPE) | (1<<MSTR))
2011-06-25 16:19:18 +02:00
/* F_CPU /16 (460.8kHz) */
#define SPI_MODE3 ((1<<SPE) | (1<<MSTR) | (1<<SPR0))
2011-06-25 16:19:18 +02:00
/* F_CPU /64 (115.2kHz) */
#define SPI_MODE2 ((1<<SPE) | (1<<MSTR) | (1<<SPR1))
2011-06-25 16:19:18 +02:00
/* F_CPU /128 (57.6kHz) */
2012-11-18 13:12:27 +01:00
#define SPI_MODE1 ((1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0))
2011-06-25 16:19:18 +02:00
static const uint8_t spi_modes[4] = { SPI_MODE1, SPI_MODE2, SPI_MODE3, SPI_MODE4 };
#define SPI_SPEED_PROBE 0xFF
static uint8_t spi_speed = SPI_SPEED_PROBE;
#define EV_NONE 0x00
#define EV_STATE_ENTER 0x01
#define EV_BUTTON_PRESSED 0x02
#define EV_BUTTON_RELEASED 0x03
#define EV_TIMEOUT 0x04
#define EV_PROG_ENTER 0x11
#define EV_PROG_LEAVE 0x12
#define STATE_IDLE 0x00 /* nothing */
#define STATE_RESET_SYNC 0x01
#define STATE_RESET_RETRY 0x02
#define STATE_RESET_PROGMODE 0x03
2012-11-18 13:12:27 +01:00
#define LED_OFF 0x00
#define LED_SLOW 0x20
#define LED_FAST 0x08
#define LED_ON 0x80
#define LED_SPEED1 0x20
#define LED_SPEED2 0x10
#define LED_SPEED3 0x08
#define LED_SPEED4 0x04
#define CMD_PROG_ENABLE_1 0xAC
#define CMD_PROG_ENABLE_2 0x53
#define CMD_CHIP_ERASE_1 0xAC
#define CMD_CHIP_ERASE_2 0x80
#define CMD_POLL_BUSY_1 0xF0 /* not used */
#define CMD_POLL_BUSY_2 0x00 /* not used */
#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 */
#define CMD_READ_FLASH_LO 0x20
#define CMD_READ_FLASH_HI 0x28
#define CMD_READ_EEPROM 0xA0
#define CMD_READ_LOCK_1 0x58
#define CMD_READ_LOCK_2 0x00
#define CMD_READ_SIG_1 0x30
#define CMD_READ_SIG_2 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 */
#define CMD_WRITE_FLASH_PAGE 0x4C
#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 */
2008-03-27 20:06:02 +01:00
2012-02-19 12:54:54 +01:00
2014-10-12 11:13:30 +02:00
static volatile uint8_t led_mode = LED_OFF;
2008-03-27 20:06:02 +01:00
2019-12-30 20:26:58 +01:00
static avr_device_t m_device;
2011-06-25 16:19:18 +02:00
static uint8_t last_cmd;
static uint8_t last_val;
static uint16_t last_addr;
2006-05-07 15:22:11 +02:00
/* Send one byte to PC */
2008-03-27 20:06:02 +01:00
static void ser_send(uint8_t data)
2006-05-05 19:02:04 +02:00
{
2014-10-12 11:13:30 +02:00
#if defined(__AVR_ATmega16__)
loop_until_bit_is_set(UCSRA, UDRE);
2012-11-18 13:12:27 +01:00
UDR = data;
2014-10-12 11:13:30 +02:00
#elif defined(__AVR_ATmega328P__)
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = data;
#endif
2014-10-16 21:29:43 +02:00
} /* ser_send */
2006-05-05 19:02:04 +02:00
2006-05-07 15:22:11 +02:00
/* Receive one byte from PC */
2008-03-27 20:06:02 +01:00
static uint8_t ser_recv(void)
2006-05-05 19:02:04 +02:00
{
2014-10-12 11:13:30 +02:00
#if defined(__AVR_ATmega16__)
2012-11-18 13:12:27 +01:00
loop_until_bit_is_set(UCSRA, RXC);
return UDR;
2014-10-12 11:13:30 +02:00
#elif defined(__AVR_ATmega328P__)
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
#endif
2014-10-16 21:29:43 +02:00
} /* ser_recv */
2006-05-05 19:02:04 +02:00
2014-10-16 22:03:22 +02:00
2006-05-07 15:22:11 +02:00
/* Send one byte to target, and return received one */
2008-03-27 20:06:02 +01:00
static uint8_t spi_rxtx(uint8_t val)
2006-05-06 22:55:42 +02:00
{
2012-11-18 13:12:27 +01:00
SPDR = val;
loop_until_bit_is_set(SPSR, SPIF);
return SPDR;
2014-10-16 21:29:43 +02:00
} /* spi_rxtx */
2006-05-06 22:55:42 +02:00
2006-05-07 15:22:11 +02:00
/* Control reset and SPI lines */
2008-03-27 20:06:02 +01:00
static void set_reset(uint8_t mode)
2006-05-06 22:55:42 +02:00
{
2012-11-18 13:12:27 +01:00
if (mode) {
2014-10-12 11:13:30 +02:00
ISP_INACTIVE();
2012-11-18 13:12:27 +01:00
} else {
2014-10-12 11:13:30 +02:00
ISP_ACTIVE();
2012-11-18 13:12:27 +01:00
}
2014-10-16 21:29:43 +02:00
} /* set_reset */
2006-05-06 22:55:42 +02:00
2008-03-27 20:06:02 +01:00
/* writes a byte to target flash/eeprom */
static void mem_write(uint8_t cmd, uint16_t addr, uint8_t val)
2006-05-06 22:55:42 +02:00
{
2012-11-18 13:12:27 +01:00
spi_rxtx(cmd);
spi_rxtx(addr >> 8);
spi_rxtx(addr & 0xFF);
spi_rxtx(val);
/* remember values for polling */
last_cmd = cmd;
last_addr = addr;
last_val = val;
2014-10-16 21:29:43 +02:00
} /* mem_write */
2006-05-06 22:55:42 +02:00
2006-05-07 15:22:11 +02:00
/* read a byte from target flash/eeprom */
2008-03-27 20:06:02 +01:00
static uint8_t mem_read(uint8_t cmd, uint16_t addr)
2006-05-06 22:55:42 +02:00
{
2012-11-18 13:12:27 +01:00
spi_rxtx(cmd);
spi_rxtx(addr >> 8);
spi_rxtx(addr & 0xFF);
return spi_rxtx(0x00);
2014-10-16 21:29:43 +02:00
} /* mem_read */
2006-05-06 22:55:42 +02:00
2006-05-07 15:22:11 +02:00
/* wait until byte/page is written to target memory */
2008-03-27 20:06:02 +01:00
static void poll(void)
2006-05-06 22:55:42 +02:00
{
2012-11-18 13:12:27 +01:00
uint8_t cmd, val, poll = 0xFF;
2019-12-30 20:26:58 +01:00
if (((last_val == 0x00) && (m_device.flags & POLL_00)) ||
((last_val == 0x7F) && (m_device.flags & POLL_7F)) ||
((last_val == 0x80) && (m_device.flags & POLL_80)) ||
((last_val == 0xFF) && (m_device.flags & POLL_FF))
2012-11-18 13:12:27 +01:00
) {
/* wait default time */
_delay_ms(15);
return;
}
if (last_cmd == CMD_WRITE_EEPROM) {
cmd = CMD_READ_EEPROM;
} else {
/* CMD_WRITE_FLASH -> CMD_READ_FLASH */
cmd = (last_cmd & 0x08) | 0x20;
}
/* poll until we get correct value */
do {
val = mem_read(cmd, last_addr);
} while ((val != last_val) && poll--);
2014-10-16 21:29:43 +02:00
} /* poll */
2006-05-06 22:55:42 +02:00
2011-06-25 16:19:18 +02:00
static void mem_pagewrite(uint16_t addr)
{
2012-11-18 13:12:27 +01:00
spi_rxtx(CMD_WRITE_FLASH_PAGE);
spi_rxtx(addr >> 8);
spi_rxtx(addr & 0xFF);
spi_rxtx(0x00);
2006-05-05 19:02:04 +02:00
2012-11-18 13:12:27 +01:00
poll();
2014-10-16 21:29:43 +02:00
} /* mem_pagewrite */
2006-05-06 22:55:42 +02:00
static void reset_statemachine(uint8_t event);
static volatile uint16_t reset_timer = 0x0000;
static volatile uint8_t reset_state;
2014-10-16 22:03:22 +02:00
static uint16_t addr = 0x0000;
2014-10-16 21:29:43 +02:00
2012-02-19 12:54:54 +01:00
static void cmdloop(void) __attribute__ ((noreturn));
2011-06-25 16:19:18 +02:00
static void cmdloop(void)
{
2012-11-18 13:12:27 +01:00
static uint8_t page_buf[256];
while (1) {
2020-01-08 22:55:25 +01:00
#if (USE_DISPLAY)
if (reset_state == STATE_RESET_PROGMODE)
{
uint16_t byte_address;
byte_address = (addr << 1);
display_show_hex(byte_address >> 8, 0);
display_show_hex(byte_address & 0xFF, 1);
display_set_mode(DISPLAY_MODE_STATIC);
}
#endif /* (USE_DISPLAY) */
2012-11-18 13:12:27 +01:00
switch (ser_recv()) {
/* Enter programming mode */
case 'P': {
reset_statemachine(EV_PROG_ENTER);
2012-11-18 13:12:27 +01:00
while (1) {
if (reset_state == STATE_IDLE) {
2014-10-16 21:58:36 +02:00
/* device not supported */
ser_send('!');
break;
2012-11-18 13:12:27 +01:00
} else if (reset_state == STATE_RESET_PROGMODE) {
2019-12-30 20:26:58 +01:00
if (m_device.flags & POLL_UNTESTED) {
2014-10-16 21:58:36 +02:00
reset_statemachine(EV_PROG_LEAVE);
/* untested device */
ser_send('!');
} else {
/* supported device */
ser_send('\r');
}
break;
2012-11-18 13:12:27 +01:00
}
}
break;
}
/* Autoincrement address */
case 'a':
ser_send('Y');
break;
/* Set address */
case 'A':
addr = (ser_recv() << 8);
addr |= ser_recv();
ser_send('\r');
break;
/* Write program memory, low byte */
case 'c':
led_mode = LED_FAST;
mem_write(CMD_LOAD_FLASH_LO, addr, ser_recv());
/* poll on byte addressed targets */
2019-12-30 20:26:58 +01:00
if (m_device.pagemask == 0x00) {
2012-11-18 13:12:27 +01:00
poll();
}
ser_send('\r');
break;
/* Write program memory, high byte */
case 'C':
led_mode = LED_FAST;
mem_write(CMD_LOAD_FLASH_HI, addr, ser_recv());
/* poll on byte addressed targets */
2019-12-30 20:26:58 +01:00
if (m_device.pagemask == 0x00) {
2012-11-18 13:12:27 +01:00
poll();
}
addr++;
ser_send('\r');
break;
/* Issue Page Write */
case 'm':
led_mode = LED_FAST;
mem_pagewrite(last_addr);
ser_send('\r');
break;
/* Read Lock Bits */
case 'r':
ser_send(mem_read(CMD_READ_LOCK_1, CMD_READ_LOCK_2 << 8));
ser_send('\r');
break;
/* Read program memory */
case 'R':
led_mode = LED_SLOW;
ser_send(mem_read(CMD_READ_FLASH_HI, addr));
ser_send(mem_read(CMD_READ_FLASH_LO, addr));
addr++;
break;
/* Read data memory */
case 'd':
led_mode = LED_SLOW;
ser_send(mem_read(CMD_READ_EEPROM, addr));
addr++;
break;
/* Write data memory */
case 'D':
led_mode = LED_FAST;
mem_write(CMD_WRITE_EEPROM, addr, ser_recv());
poll();
addr++;
ser_send('\r');
break;
/* Chip erase */
case 'e':
spi_rxtx(CMD_CHIP_ERASE_1);
spi_rxtx(CMD_CHIP_ERASE_2);
spi_rxtx(0x00);
spi_rxtx(0x00);
_delay_ms(10);
ser_send('\r');
break;
/* Write lock bits */
case 'l': {
uint8_t val = ser_recv();
spi_rxtx(CMD_WRITE_LOCK_1);
spi_rxtx(CMD_WRITE_LOCK_2);
spi_rxtx(0x00);
spi_rxtx(val);
_delay_ms(10);
ser_send('\r');
break;
}
/* Read fusebits */
case 'F':
ser_send(mem_read(CMD_READ_FUSE_1, CMD_READ_FUSE_2 << 8));
break;
/* Read high fusebits */
case 'N':
ser_send(mem_read(CMD_READ_FUSE_H_1, CMD_READ_FUSE_H_2 << 8));
break;
/* Read extended fusebits */
case 'Q':
ser_send(mem_read(CMD_READ_FUSE_E_1, CMD_READ_FUSE_E_2 << 8));
break;
/* Leave programming mode */
case 'L':
/* Exit Bootloader */
case 'E':
reset_statemachine(EV_PROG_LEAVE);
2012-11-18 13:12:27 +01:00
ser_send('\r');
break;
/* Select device type */
case 'T': {
ser_recv(); // ignore
ser_send('\r');
break;
}
/* Read signature bytes */
case 's': {
uint8_t i = 2;
do {
2019-12-30 20:26:58 +01:00
ser_send(m_device.sig[i]);
2012-11-18 13:12:27 +01:00
} while (i--);
break;
}
/* Return supported device codes */
2019-12-30 20:26:58 +01:00
case 't':
avrdevice_iterate_devcodes(ser_send);
2012-11-18 13:12:27 +01:00
ser_send(0x00);
break;
/* Return software identifier */
case 'S':
ser_send('A');
ser_send('V');
ser_send('R');
ser_send('-');
ser_send('I');
ser_send('S');
ser_send('P');
break;
/* Return software version */
case 'V':
ser_send('3');
ser_send('8');
break;
/* Return hardware version */
case 'v':
ser_send('1');
ser_send('2');
break;
/* Return programmer type */
case 'p':
ser_send('S');
break;
/* Set LED */
case 'x':
ser_recv();
led_mode = LED_ON;
break;
/* Clear LED */
case 'y':
ser_recv();
led_mode = LED_OFF;
break;
/* Report Block write Mode */
case 'b': {
ser_send('Y');
ser_send(sizeof(page_buf) >> 8);
ser_send(sizeof(page_buf) & 0xFF);
break;
}
/* Block Write */
case 'B': {
uint16_t size, i;
uint8_t type;
led_mode = LED_FAST;
size = ser_recv() << 8;
size |= ser_recv();
type = ser_recv();
for (i = 0; i < size; i++)
page_buf[i] = ser_recv();
if (type == 'F') {
for (i = 0; i < size; i += 2) {
mem_write(CMD_LOAD_FLASH_LO, addr, page_buf[i]);
mem_write(CMD_LOAD_FLASH_HI, addr, page_buf[i+1]);
addr++;
2019-12-30 20:26:58 +01:00
if ((addr & m_device.pagemask) == 0x00) {
2012-11-18 13:12:27 +01:00
mem_pagewrite(last_addr);
}
}
2019-12-30 20:26:58 +01:00
if ((m_device.pagemask != 0x00) &&
(size != ((m_device.pagemask +1) << 1))
2016-05-16 12:51:06 +02:00
) {
2012-11-18 13:12:27 +01:00
mem_pagewrite(last_addr);
}
} else if (type == 'E') {
for (i = 0; i < size; i++) {
mem_write(CMD_WRITE_EEPROM, addr, page_buf[i]);
poll();
addr++;
}
}
ser_send('\r');
break;
}
/* Block Read */
case 'g': {
uint16_t size, i;
uint8_t type;
led_mode = LED_SLOW;
size = ser_recv() << 8;
size |= ser_recv();
type = ser_recv();
if (type == 'F') {
for (i = 0; i < size; i += 2) {
ser_send(mem_read(CMD_READ_FLASH_LO, addr));
ser_send(mem_read(CMD_READ_FLASH_HI, addr));
addr++;
}
} else if (type == 'E') {
for (i = 0; i < size; i++) {
ser_send(mem_read(CMD_READ_EEPROM, addr));
addr++;
}
}
break;
}
/* Write fusebits */
case 'f': {
uint8_t val = ser_recv();
spi_rxtx(CMD_WRITE_FUSE_1);
spi_rxtx(CMD_WRITE_FUSE_2);
spi_rxtx(0x00);
spi_rxtx(val);
_delay_ms(10);
ser_send('\r');
break;
}
/* Universial command */
case ':': {
uint8_t val[3];
val[0] = ser_recv();
val[1] = ser_recv();
val[2] = ser_recv();
spi_rxtx(val[0]);
spi_rxtx(val[1]);
ser_send(spi_rxtx(val[2]));
_delay_ms(10);
ser_send('\r');
break;
}
/* New universal command */
case '.': {
uint8_t val[4];
val[0] = ser_recv();
val[1] = ser_recv();
val[2] = ser_recv();
val[3] = ser_recv();
spi_rxtx(val[0]);
spi_rxtx(val[1]);
spi_rxtx(val[2]);
ser_send(spi_rxtx(val[3]));
2016-05-16 12:51:36 +02:00
/* most CMD_WRITE_* commands need delay */
if (val[0] == CMD_WRITE_LOCK_1)
{
_delay_ms(10);
}
2012-11-18 13:12:27 +01:00
ser_send('\r');
break;
}
/* ESC */
case 0x1B:
break;
default:
ser_send('?');
break;
}
}
2014-10-16 21:29:43 +02:00
} /* cmdloop */
2011-06-25 16:19:18 +02:00
static void reset_statemachine(uint8_t event)
2011-06-25 16:19:18 +02:00
{
static uint8_t reset_retries;
static uint8_t reset_cause;
uint8_t state;
uint8_t oldstate;
uint16_t timer;
cli();
/* copy state, disable timer */
state = reset_state;
timer = reset_timer;
reset_timer = 0x0000;
sei();
2012-11-18 13:12:27 +01:00
do {
oldstate = state;
2012-11-18 13:12:27 +01:00
switch (state) {
case STATE_IDLE:
if (event == EV_STATE_ENTER) {
led_mode = LED_OFF;
timer = 0; /* stop timer */
2012-11-18 13:12:27 +01:00
/* put device in RUN mode */
2012-11-18 13:12:27 +01:00
set_reset(1);
} else if ((event == EV_BUTTON_PRESSED) || (event == EV_PROG_ENTER)) {
reset_retries = 5;
reset_cause = event;
2012-11-18 13:12:27 +01:00
/* probe SPI speed of device */
if (spi_speed == SPI_SPEED_PROBE) {
spi_speed = 3;
2012-11-18 13:12:27 +01:00
}
state = STATE_RESET_SYNC;
2012-11-18 13:12:27 +01:00
}
break;
case STATE_RESET_SYNC:
2012-11-18 13:12:27 +01:00
if (event == EV_STATE_ENTER) {
led_mode = LED_ON;
2016-05-16 12:51:36 +02:00
timer = 1; /* timeout 50ms */
2012-11-18 13:12:27 +01:00
/* set SPI speed */
SPCR = spi_modes[spi_speed];
/* put device in ISP mode */
set_reset(0);
2012-11-18 13:12:27 +01:00
} else if (event == EV_TIMEOUT) {
uint8_t sync;
spi_rxtx(CMD_PROG_ENABLE_1);
spi_rxtx(CMD_PROG_ENABLE_2);
sync = spi_rxtx(0x00);
spi_rxtx(0x00);
2019-12-30 20:26:58 +01:00
memset(&m_device, 0x00, sizeof(avr_device_t));
if (sync == CMD_PROG_ENABLE_2) {
uint8_t i;
for (i = 0; i < 3; i++) {
2019-12-30 20:26:58 +01:00
m_device.sig[i] = mem_read(CMD_READ_SIG_1, (CMD_READ_SIG_2 << 8) | i);
}
2019-12-30 20:26:58 +01:00
avrdevice_get_by_signature(&m_device, m_device.sig);
2020-01-08 22:55:25 +01:00
state = STATE_RESET_PROGMODE;
} else {
state = STATE_RESET_RETRY;
}
2012-11-18 13:12:27 +01:00
}
break;
case STATE_RESET_RETRY:
2012-11-18 13:12:27 +01:00
if (event == EV_STATE_ENTER) {
led_mode = LED_OFF;
timer = 5; /* timeout 50ms */
2012-11-18 13:12:27 +01:00
/* put device in RUN mode */
set_reset(1);
2012-11-18 13:12:27 +01:00
} else if (event == EV_TIMEOUT) {
reset_retries--;
if (reset_retries > 0) {
/* try lower frequency */
if (spi_speed > 0) {
spi_speed--;
}
state = STATE_RESET_SYNC;
} else {
/* got no sync, probe speed again next time */
spi_speed = SPI_SPEED_PROBE;
state = STATE_IDLE;
}
2012-11-18 13:12:27 +01:00
}
break;
case STATE_RESET_PROGMODE:
2012-11-18 13:12:27 +01:00
if (event == EV_STATE_ENTER) {
2020-01-08 22:55:25 +01:00
if (reset_cause == EV_BUTTON_PRESSED)
{
state = STATE_IDLE;
}
2012-11-18 13:12:27 +01:00
} else if (event == EV_PROG_LEAVE) {
/* was in prog mode (osc changed?), probe speed next time */
spi_speed = SPI_SPEED_PROBE;
state = STATE_IDLE;
2012-11-18 13:12:27 +01:00
} else if (event == EV_BUTTON_PRESSED) {
state = STATE_IDLE;
2012-11-18 13:12:27 +01:00
}
break;
default:
state = STATE_IDLE;
break;
}
2020-01-08 22:55:25 +01:00
#if (USE_DISPLAY)
if ((m_state == STATE_IDLE) &&
((oldstate == STATE_RESET_RETRY) ||
(oldstate == STATE_RESET_PROGMODE)
))
{
if (m_device.name[0] != '\0')
{
display_show_string(m_device.name, 0);
if (m_device.flags & POLL_UNTESTED)
{
display_show_string(" untested", 1);
}
}
else
{
display_show_string("unknown 0X", 0);
display_show_hex(m_device.sig[0], 1);
display_show_hex(m_device.sig[1], 1);
display_show_hex(m_device.sig[2], 1);
}
display_set_mode(DISPLAY_MODE_SCROLL_ONCE);
}
#endif /* (USE_DISPLAY) */
event = (oldstate != state) ? EV_STATE_ENTER
: EV_NONE;
2012-11-18 13:12:27 +01:00
} while (oldstate != state);
2012-11-18 13:12:27 +01:00
cli();
/* copy state back */
reset_timer = timer;
reset_state = state;
sei();
2014-10-16 21:29:43 +02:00
} /* reset_statemachine */
2011-06-25 16:19:18 +02:00
/* time keeping */
ISR(TIMER0_OVF_vect)
{
2012-11-18 13:12:27 +01:00
uint8_t event = EV_NONE;
/* restart timer */
TCNT0 = TIMER_RELOAD;
static uint8_t prev_pressed;
2014-10-12 11:13:30 +02:00
if (ISP_CHECK()) {
2012-11-18 13:12:27 +01:00
if (!prev_pressed) {
event = EV_BUTTON_PRESSED;
prev_pressed = 1;
}
} else {
if (prev_pressed) {
event = EV_BUTTON_RELEASED;
prev_pressed = 0;
}
}
if (reset_timer) {
reset_timer--;
if (reset_timer == 0) {
2012-11-18 13:12:27 +01:00
event = EV_TIMEOUT;
}
}
if (event != EV_NONE) {
reset_statemachine(event);
2012-11-18 13:12:27 +01:00
}
/* update LED */
static uint8_t led_timer;
if (led_mode & ((led_timer++ & 0xFF) | 0x80)) {
2014-10-12 11:13:30 +02:00
ISP_LED_ON();
2012-11-18 13:12:27 +01:00
} else {
2014-10-12 11:13:30 +02:00
ISP_LED_OFF();
2012-11-18 13:12:27 +01:00
}
2014-10-16 22:03:22 +02:00
2020-01-08 22:55:25 +01:00
#if (USE_DISPLAY)
display_update();
#endif /* (USE_DISPLAY) */
2014-10-16 21:29:43 +02:00
} /* TIMER0_OVF_vect */
2011-06-25 16:19:18 +02:00
2014-10-16 22:03:22 +02:00
2014-10-12 11:13:30 +02:00
#if defined(__AVR_ATmega328P__)
/*
* For newer devices the watchdog timer remains active even after a
* system reset. So disable it as soon as possible.
* automagically called on startup
*/
void disable_wdt_timer(void) __attribute__((naked, section(".init3")));
void disable_wdt_timer(void)
{
MCUSR = 0;
WDTCSR = (1<<WDCE) | (1<<WDE);
WDTCSR = (0<<WDE);
2014-10-16 21:29:43 +02:00
} /* disable_wdt_timer */
#endif /* defined(__AVR_ATmega328P__) */
2014-10-12 11:13:30 +02:00
2011-06-25 16:19:18 +02:00
int main(void)
{
2014-10-12 11:13:30 +02:00
GPIO_INIT();
#if defined(OSCCAL_VALUE)
OSCCAL = OSCCAL_VALUE;
#endif /* defined(OSCCAL_VALUE) */
2011-06-25 16:19:18 +02:00
2014-10-12 11:13:30 +02:00
#if defined(__AVR_ATmega16__)
2012-11-18 13:12:27 +01:00
/* Set baud rate */
UBRRH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
2011-06-25 16:19:18 +02:00
2012-11-18 13:12:27 +01:00
/* enable usart with 8n1 */
UCSRB = (1<<TXEN) | (1<<RXEN);
UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
2014-10-12 11:13:30 +02:00
#elif defined(__AVR_ATmega328P__)
/* Set baud rate */
UBRR0H = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
UBRR0L = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
/* enable usart with 8n1 */
UCSR0B = (1<<TXEN0) | (1<<RXEN0);
UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
#endif
2011-06-25 16:19:18 +02:00
2012-11-18 13:12:27 +01:00
/* enable SPI master mode */
SPCR = SPI_MODE4;
2011-06-25 16:19:18 +02:00
2020-01-12 18:12:49 +01:00
TIMER_INIT();
2011-06-25 16:19:18 +02:00
/* init statemachine */
2014-10-16 22:03:22 +02:00
reset_statemachine(EV_BUTTON_PRESSED);
2012-11-18 13:12:27 +01:00
sei();
2011-06-25 16:19:18 +02:00
2012-11-18 13:12:27 +01:00
cmdloop();
2014-10-16 21:29:43 +02:00
} /* main */