5 #include <avr/interrupt.h>
11 * B2: SPI ~SS/shiftreg ~OE
13 * B4: SPI MISO (unused)
15 * C0: programming mode PGM
29 #define PC_SYSRST 0x20
31 static void proc_cmd(char *input);
32 static void start_prog(void);
33 static void end_prog(void);
34 static void write_ram(uint16_t addr, uint8_t val);
35 static uint8_t read_ram(uint16_t addr);
36 static void set_data_bus(unsigned char val);
37 static void release_data_bus(void);
38 static void set_addr_bus(uint16_t addr);
39 static void sys_reset(void);
40 static inline void iodelay(void);
46 static char input[128];
47 static unsigned char inp_cidx;
52 /* SPI (SS/MOSI/SCK) are outputs */
53 DDRB = PB_SS | PB_MOSI | PB_SCK;
54 PORTB = PB_SS; /* drive SS high when not in programming mode */
55 DDRC = 0xff; /* control signals are all outputs */
56 PORTC = PC_CS | PC_WE | PC_OE | PC_SYSRST;
57 DDRD = 0; /* tri-state the data bus by default (high 6 bits, low 2 are in DDRB) */
60 /* disable all pullups */
63 /* enable SPI (used to talk to the address bus shift registers)
64 * SPI master mode, clock / 16, CPOL=0/CPHA=0 because shiftreg clocks on the
65 * rising edge, and MSB first
67 SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
69 /* init the serial port we use to talk to the host */
80 if(c == '\r' || c == '\n') {
84 } else if(inp_cidx < sizeof input - 1) {
85 input[inp_cidx++] = c;
92 static void proc_cmd(char *input)
99 echo = input[1] == '1' ? 1 : 0;
100 printf("OK echo %s\n", echo ? "on" : "off");
105 puts("ERR already in programming mode");
108 puts("OK programming mode");
122 addr = strtol(input + 1, &endp, 0);
123 printf("OK address: %x\n", (unsigned int)addr);
128 puts("ERR not in programming mode");
131 data = strtol(input + 1, &endp, 0);
132 write_ram(addr++, data);
138 puts("ERR not in programming mode");
141 data = read_ram(addr++);
142 printf("OK %d\n", (int)data);
146 puts("OK command help");
147 puts(" e 0|1: turn echo on/off");
148 puts(" p: enter programming mode");
149 puts(" b: exit programming mode and boot up");
150 puts(" a <addr>: set address counter (0-ffff)");
151 puts(" w <data>: store data byte and increment address (0-255)");
152 puts(" r: read data byte and increment address");
153 puts(" ?: print command help");
157 printf("ERR unknown command: '%c'\n", input[0]);
161 static void start_prog(void)
163 /* hold reset low, and take over the RAM */
170 static void end_prog(void)
172 /* make extra sure we don't drive the data bus */
175 /* PGM low and return reset and SS high */
183 static void write_ram(uint16_t addr, uint8_t val)
188 PORTC &= ~(PC_WE | PC_CS);
195 static uint8_t read_ram(uint16_t addr)
200 PORTC &= ~(PC_CS | PC_OE);
202 val = (PIND & 0xfc) | (PINB & 3);
203 PORTC |= PC_CS | PC_OE;
207 static void set_data_bus(unsigned char val)
209 /* drive the data bus */
212 PORTB = (PORTB & 0xfc) | (val & 3);
216 static void release_data_bus(void)
222 static void set_addr_bus(uint16_t addr)
225 SPDR = (uint8_t)addr;
226 while(!(SPSR & (1 << SPIF)));
227 SPDR = (uint8_t)(addr >> 8);
228 while(!(SPSR & (1 << SPIF)));
232 PORTB &= ~PB_SS; /* SS must return low because the shiftreg OE is tied to it */
235 static void sys_reset(void)
242 static inline void iodelay(void)