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 /* PGM low and return reset high */
179 static void write_ram(uint16_t addr, uint8_t val)
184 PORTC &= ~(PC_WE | PC_CS);
191 static uint8_t read_ram(uint16_t addr)
196 PORTC &= ~(PC_CS | PC_OE);
198 val = (PIND & 0xfc) | (PINB & 3);
199 PORTC |= PC_CS | PC_OE;
203 static void set_data_bus(unsigned char val)
205 /* drive the data bus */
208 PORTB = (PORTB & 0xfc) | (val & 3);
212 static void release_data_bus(void)
218 static void set_addr_bus(uint16_t addr)
221 SPDR = (uint8_t)addr;
222 while(!(SPSR & (1 << SPIF)));
223 SPDR = (uint8_t)(addr >> 8);
224 while(!(SPSR & (1 << SPIF)));
228 PORTB &= ~PB_SS; /* SS must return low because the shiftreg OE is tied to it */
231 static void sys_reset(void)
238 static inline void iodelay(void)