/*************************************************************************** * 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<