From b47cfa680d50b95a830fc92fd4f629e18172e482 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Mon, 29 May 2017 21:06:08 +0200 Subject: [PATCH] initial commit --- .gitignore | 7 + Makefile | 48 +++++++ main.c | 387 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 442 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d53dd72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o +*.elf +*.bin +*.hex +*.lst +*.lss +*.map diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1c07130 --- /dev/null +++ b/Makefile @@ -0,0 +1,48 @@ +CC := avr-gcc +LD := avr-ld +OBJCOPY := avr-objcopy +OBJDUMP := avr-objdump +SIZE := avr-size + +TARGET = e2promprog +SOURCE = $(wildcard *.c) + +MCU=attiny2313 +AVRDUDE_MCU=attiny2313 + +AVRDUDE_FUSES=lfuse:w:0xe4:m hfuse:w:0xdf:m efuse:w:0xff:m +AVRDUDE_PROG := -c avr910 -b 115200 -P /dev/ispprog +#AVRDUDE_PROG := -c dragon_isp -P usb + +# --------------------------------------------------------------------------- + +CFLAGS = -pipe -g -Os -mmcu=$(MCU) -Wall +CFLAGS += -fdata-sections -ffunction-sections +CFLAGS += -Wa,-adhlns=$(*F).lst +LDFLAGS = -Wl,-Map,$(@:.elf=.map),--cref +LDFLAGS += -Wl,--relax,--gc-sections + +# --------------------------------------------------------------------------- + +$(TARGET): $(TARGET).elf + @$(SIZE) -B -x --mcu=$(MCU) $< + +$(TARGET).elf: $(SOURCE:.c=.o) + @echo " Linking file: $@" + @$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ 2> /dev/null + @$(OBJDUMP) -h -S $@ > $(@:.elf=.lss) + @$(OBJCOPY) -j .text -j .data -O ihex $@ $(@:.elf=.hex) + @$(OBJCOPY) -j .text -j .data -O binary $@ $(@:.elf=.bin) + +%.o: %.c $(MAKEFILE_LIST) + @echo " Building file: $<" + @$(CC) $(CFLAGS) -o $@ -c $< + +clean: + rm -rf $(SOURCE:.c=.o) $(SOURCE:.c=.lst) $(addprefix $(TARGET), .elf .map .lss .hex .bin) + +install: $(TARGET).elf + avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) -U flash:w:$(<:.elf=.hex) + +fuses: + avrdude $(AVRDUDE_PROG) -p $(AVRDUDE_MCU) $(patsubst %,-U %, $(AVRDUDE_FUSES)) diff --git a/main.c b/main.c new file mode 100644 index 0000000..edfa054 --- /dev/null +++ b/main.c @@ -0,0 +1,387 @@ +/*************************************************************************** + * Copyright (C) 02/2017 by Olaf Rempel * + * razzor@kopf-tisch.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 + +#define F_CPU 8000000 +#include + +#define USE_HEX 0 + +/* + * attiny2313 + * lfuse: 0xe4 (int rc osc, max startup time) + * hfuse: 0xdf (no BOD) + * efuse: 0xff (no self programming) + * + * PA0 -> HC590 /MRC (Counter Reset) + * PB0:7 <-> D0:7 + * PD0 <- RXD + * PD1 -> TXD + * PD2 -> HC590 CPC (Counter Clock) + * PD3 -> HC590 CPR (Register Clock) + * PD4 -> EEPROM /CS + * PD5 -> EEPROM /WR + * PD6 -> EEPROM /OE + */ + +#define nCNTRES PORTA0 + +#define RXD PORTD0 +#define TXD PORTD1 +#define CNTCLK PORTD2 +#define REGCLK PORTD3 +#define nCHIPSELECT PORTD4 +#define nWRITE PORTD5 +#define nREAD PORTD6 + +#define PAGE_SIZE 64 + +#define BAUDRATE 115200 + +#define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1) + +#define NOP asm volatile ("nop") + +static uint16_t g_address; + +/* ************************************************************************* + * send one byte to UART + * ************************************************************************* */ +static void ser_send(uint8_t data) +{ + loop_until_bit_is_set(UCSRA, UDRIE); + UDR = data; +} /* ser_send */ + + +/* ************************************************************************* + * receive one byte from UART + * ************************************************************************* */ +static uint8_t ser_recv(void) +{ + loop_until_bit_is_set(UCSRA, RXC); + return UDR; +} /* ser_recv */ + + +#if (USE_HEX) +/* ************************************************************************* + * + * ************************************************************************* */ +static void ser_send_hexnibble(uint8_t value) +{ + value += (value < 0x0A) ? '0' : ('A' - 0x0A); + + ser_send(value); +} /* ser_send_hexnibble */ + + +/* ************************************************************************* + * + * ************************************************************************* */ +static void ser_send_hex(uint8_t value) +{ + ser_send_hexnibble(value >> 4); + ser_send_hexnibble(value & 0x0F); +} /* ser_send_hex */ + + +/* ************************************************************************* + * + * ************************************************************************* */ +static uint8_t ascii2hexnibble(uint8_t value) +{ + if (value >= '0' && value <= '9') + { + return (value - '0'); + } + else + { + value &= ~(0x20); + if (value >= 'A' && value <= 'F') + { + return (value - 'A' + 0x0A); + } + } + + return 0; +} /* ascii2hexnibble */ + + +/* ************************************************************************* + * + * ************************************************************************* */ +static uint8_t ser_recv_hex(void) +{ + uint8_t result; + + result = ascii2hexnibble(ser_recv()) << 4; + result |= ascii2hexnibble(ser_recv()); + + return result; +} /* ser_recv_hex */ + +#define ser_send_comm(x) ser_send_hex(x) +#define ser_recv_comm() ser_recv_hex() +#else +#define ser_send_comm(x) ser_send(x) +#define ser_recv_comm() ser_recv() +#endif /* (USE_HEX) */ + + +/* ************************************************************************* + * + * ************************************************************************* */ +static void address_clear(void) +{ + /* reset address counter */ + PORTA &= ~(1< address) + { + /* reset address counter */ + PORTA &= ~(1<> 8); +// ser_send_hex(address & 0xFF); +} /* address_set */ + + +/* ************************************************************************* + * increase address by one + * ************************************************************************* */ +static void address_inc(void) +{ + /* increase address counter */ + PORTD |= (1< PAGE_SIZE) + { + count = PAGE_SIZE; + } + + for (i = 0; i < count; i++) + { + buffer[i] = ser_recv_comm(); + } + + DDRB = 0xFF; + + for (i = 0; i < count; i++) + { + PORTB = buffer[i]; + + PORTD &= ~((1< start polling */ + if ((g_address & (sizeof(buffer) -1)) == (sizeof(buffer) -1)) + { + data_poll(buffer[i]); + DDRB = 0xFF; + } + + address_inc(); + } + + DDRB = 0x00; + PORTB = 0x00; +} /* data_write */ + + +/* ************************************************************************* + * + * ************************************************************************* */ +int main(void) __attribute__ ((noreturn)); +int main(void) +{ + PORTA = (1<>8) & 0xFF; + UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF); + UCSRC = (1<