[3587f7f] | 1 |
|
---|
| 2 | /* This project is targetted at the VUSBDev hacking board. */
|
---|
| 3 |
|
---|
| 4 | #include <avr/io.h>
|
---|
| 5 | #include <avr/interrupt.h>
|
---|
| 6 | #include <avr/wdt.h>
|
---|
| 7 | #include <avr/pgmspace.h>
|
---|
| 8 | #include <util/delay.h>
|
---|
| 9 |
|
---|
| 10 | #include <string.h>
|
---|
| 11 | #include <stdbool.h>
|
---|
| 12 |
|
---|
| 13 | #include "usbdrv/usbdrv.h"
|
---|
[8659a17] | 14 | #include "shared.h"
|
---|
[3587f7f] | 15 |
|
---|
| 16 | // IO pins:
|
---|
| 17 | // PIC <> AVR
|
---|
| 18 | // PGC PB3
|
---|
| 19 | // PGD PB2
|
---|
| 20 | // PGM PB1
|
---|
| 21 | // VPP PB0
|
---|
| 22 | // VDD VCC
|
---|
| 23 | // VSS GND
|
---|
| 24 | #define PGC 8
|
---|
| 25 | #define PGD 4
|
---|
| 26 | #define PGM 2
|
---|
| 27 | #define VPP 1
|
---|
| 28 |
|
---|
[c8ec8ce] | 29 | // VUSB leds (PD6 and PD7)
|
---|
| 30 | #define RED 64
|
---|
| 31 | #define GREEN 128
|
---|
[3587f7f] | 32 |
|
---|
| 33 | // TODO is it wise to use a timer or two for bit patterns generation ? (see
|
---|
| 34 | // CrO2 architecture)
|
---|
| 35 | //
|
---|
| 36 | // As usual the SPI, TWI and USART are not useable, because we need 6 and 16 bit
|
---|
| 37 | // communcations in the ICSP protocol. So we are bit banging bhe ICSP protocol.
|
---|
| 38 | // Timers may make it easier to do that in parallel with the USB handling, since
|
---|
| 39 | // timings are in the nanosecond range.
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | uint8_t command;
|
---|
| 43 | uint16_t ICSP_data;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | int main() {
|
---|
| 47 | // Init
|
---|
| 48 | wdt_enable(WDTO_2S);
|
---|
| 49 |
|
---|
| 50 | // USB
|
---|
| 51 | usbInit();
|
---|
| 52 | sei();
|
---|
| 53 |
|
---|
| 54 | // Enter Low Voltage ICSP mode on the PIC
|
---|
| 55 | PORTB &= ~(VPP|PGM|PGD|PGC); // Start with all pin low
|
---|
| 56 | DDRB |= VPP|PGM|PGD|PGC; // set them as outputs
|
---|
| 57 | _delay_us(4); // Wait for some time
|
---|
| 58 | PORTB |= PGM; // Enable low voltage programming
|
---|
| 59 | PORTB |= VPP; // Enter programming mode
|
---|
| 60 |
|
---|
[c8ec8ce] | 61 | // Configure leds
|
---|
| 62 | DDRD |= RED | GREEN;
|
---|
| 63 | PORTD |= RED | GREEN;
|
---|
| 64 |
|
---|
| 65 | int k = 0;
|
---|
| 66 |
|
---|
[3587f7f] | 67 | while(1) {
|
---|
[c8ec8ce] | 68 | if (k-- < 0)
|
---|
| 69 | {
|
---|
| 70 | k = 20000;
|
---|
| 71 | PORTD ^= RED;
|
---|
| 72 | }
|
---|
[3587f7f] | 73 | wdt_reset();
|
---|
| 74 | usbPoll();
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | void ICSP_command(uint8_t cmd)
|
---|
| 80 | {
|
---|
| 81 | for(int i = 6; --i>= 0;)
|
---|
| 82 | {
|
---|
| 83 | if(cmd & 1)
|
---|
| 84 | PORTB |= PGD;
|
---|
| 85 | else
|
---|
| 86 | PORTB &= ~PGD;
|
---|
[c8ec8ce] | 87 | PORTB |= PGC;
|
---|
[3587f7f] | 88 | PORTB &= ~PGC;
|
---|
| 89 |
|
---|
| 90 | cmd >>= 1;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | _delay_us(1);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | void ICSP_write(uint8_t cmd, uint16_t data)
|
---|
| 98 | {
|
---|
| 99 | ICSP_command(cmd);
|
---|
| 100 |
|
---|
| 101 | // send data
|
---|
| 102 | for(int i = 16; --i>= 0;)
|
---|
| 103 | {
|
---|
| 104 | if(data & 1)
|
---|
| 105 | PORTB |= PGD;
|
---|
| 106 | else
|
---|
| 107 | PORTB &= ~PGD;
|
---|
[c8ec8ce] | 108 | PORTB |= PGC;
|
---|
[3587f7f] | 109 | PORTB &= ~PGC;
|
---|
| 110 |
|
---|
| 111 | data >>= 1;
|
---|
| 112 | }
|
---|
| 113 | _delay_us(1);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | void ICSP_read(uint8_t cmd, uint16_t *const data)
|
---|
| 118 | {
|
---|
| 119 | ICSP_command(cmd);
|
---|
| 120 |
|
---|
[c8ec8ce] | 121 | // PGD as input
|
---|
| 122 | DDRB &= ~PGD;
|
---|
| 123 | PORTB |= PGD; //pull up
|
---|
| 124 |
|
---|
[3587f7f] | 125 | // get data
|
---|
| 126 | for(int i = 16; --i>= 0;)
|
---|
| 127 | {
|
---|
| 128 | PORTB |= PGC;
|
---|
[c8ec8ce] | 129 | *data |= (PINB & PGD) / PGD;
|
---|
[3587f7f] | 130 | PORTB &= ~PGC;
|
---|
| 131 |
|
---|
| 132 | *data <<= 1;
|
---|
| 133 | }
|
---|
| 134 | _delay_us(1);
|
---|
[c8ec8ce] | 135 |
|
---|
| 136 | // PGD as output
|
---|
| 137 | PORTB &= ~PGD;
|
---|
| 138 | DDRB |= PGD;
|
---|
[3587f7f] | 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | uint8_t usbFunctionSetup(uint8_t data[8]) {
|
---|
| 143 | usbRequest_t *rq = (void *)data;
|
---|
| 144 |
|
---|
[c8ec8ce] | 145 | PORTD &= ~GREEN;
|
---|
| 146 |
|
---|
| 147 | if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR)
|
---|
[3587f7f] | 148 | switch(rq->bRequest)
|
---|
| 149 | {
|
---|
| 150 | /* HOST to DEVICE data transfers requests */
|
---|
| 151 | case LoadConfigWord:
|
---|
| 152 | case LoadCodeWord:
|
---|
| 153 | case LoadDataWord:
|
---|
| 154 | {
|
---|
| 155 | // TODO check there is only and exactly 16 bits of data
|
---|
| 156 |
|
---|
| 157 | /* Read the 16-bit data */
|
---|
| 158 | ICSP_data = (rq->wValue.bytes[0] << 8) | rq->wValue.bytes[0];
|
---|
| 159 | command = rq->bRequest;
|
---|
| 160 |
|
---|
| 161 | // send the command and data
|
---|
| 162 | ICSP_write(command, ICSP_data);
|
---|
| 163 |
|
---|
| 164 | // Return the number of bytes we wrote (none here)
|
---|
[c8ec8ce] | 165 | PORTD |= GREEN;
|
---|
[3587f7f] | 166 | return 0;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /* CONFIGURATION commands (no data is transferred) */
|
---|
| 170 | case NextAddress:
|
---|
| 171 | case BeginEraseProgram:
|
---|
| 172 | case BeginProgramOnly:
|
---|
| 173 | case BulkEraseCode:
|
---|
| 174 | case BulkEraseData:
|
---|
| 175 | case BulkEraseConfig1:
|
---|
| 176 | case BulkEraseConfig2:
|
---|
| 177 | {
|
---|
| 178 | command = rq->bRequest;
|
---|
| 179 |
|
---|
| 180 | // send the command
|
---|
[c8ec8ce] | 181 | ICSP_write(command, 0);
|
---|
[3587f7f] | 182 |
|
---|
[c8ec8ce] | 183 | PORTD |= GREEN;
|
---|
[3587f7f] | 184 | return 0;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /* DEVICE to HOST data transfers requests */
|
---|
| 188 | case ReadCodeWord:
|
---|
| 189 | case ReadDataWord:
|
---|
| 190 | {
|
---|
| 191 | command = rq->bRequest;
|
---|
| 192 | // TODO send the command and get the result
|
---|
| 193 | ICSP_read(command, &ICSP_data);
|
---|
| 194 | usbMsgPtr = (uint8_t*)&ICSP_data;
|
---|
| 195 |
|
---|
[c8ec8ce] | 196 | PORTD |= GREEN;
|
---|
[3587f7f] | 197 | return 2; // We send 2 bytes back
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
[c8ec8ce] | 200 |
|
---|
| 201 | // Unhandled commands leave the green led on.
|
---|
[3587f7f] | 202 | return 0;
|
---|
| 203 | }
|
---|
| 204 |
|
---|