1 | /* HEREPIC - AVR-based ICSP programmer for microchip PIC16F6xx and 16F8xx devices
|
---|
2 | * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.tk>
|
---|
3 | *
|
---|
4 | * This file is distributed under the terms of the MIT licence.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <string.h>
|
---|
8 |
|
---|
9 | class ICSP
|
---|
10 | {
|
---|
11 | public:
|
---|
12 | ICSP(BUSBDevice& device)
|
---|
13 | : fDevice(device)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 | uint16_t Execute(ICSPCommands command, uint16_t param = 0)
|
---|
18 | {
|
---|
19 | // TODO send the command to the device
|
---|
20 | uint16 length;
|
---|
21 | uint8 reqType = USB_REQTYPE_VENDOR;
|
---|
22 | if(command == ReadCodeWord || command == ReadDataWord)
|
---|
23 | {
|
---|
24 | length = 2;
|
---|
25 | reqType |= USB_REQTYPE_DEVICE_OUT;
|
---|
26 | } else {
|
---|
27 | length = 0;
|
---|
28 | reqType |= USB_REQTYPE_DEVICE_IN;
|
---|
29 | }
|
---|
30 |
|
---|
31 | ssize_t result = fDevice.ControlTransfer(reqType, command, param, 0,
|
---|
32 | length, ¶m);
|
---|
33 | if (result == length) {
|
---|
34 | // We transferred as much bytes as we wanted.
|
---|
35 | // Everyone is happy !
|
---|
36 | return param;
|
---|
37 | } else {
|
---|
38 | // Not enough bytes transferred, or the result is an error code
|
---|
39 | std::cerr << "USB communication error " << result
|
---|
40 | << "(" << strerror(result) << ")" << std::endl;;
|
---|
41 | return result;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | void Write(const char* filename)
|
---|
46 | {
|
---|
47 | std::cerr << "Sorry, this doesn't work yet !" << std::endl;
|
---|
48 | exit(-4);
|
---|
49 | }
|
---|
50 |
|
---|
51 | void Read(const char* filename)
|
---|
52 | {
|
---|
53 | std::cerr << "Sorry, this doesn't work yet !" << std::endl;
|
---|
54 | exit(-4);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void Erase()
|
---|
58 | {
|
---|
59 | // Let's test some stuff...
|
---|
60 | std::cout << std::hex;
|
---|
61 |
|
---|
62 | Execute(LoadConfigWord); // sets PC to 0x2000 to read CFG words
|
---|
63 | for(int i = 8; --i >= 0;)
|
---|
64 | {
|
---|
65 | uint16_t word = Execute(ReadCodeWord);
|
---|
66 | Execute(NextAddress);
|
---|
67 |
|
---|
68 | std::cout << word << " ";
|
---|
69 | }
|
---|
70 | std::cout << std::endl;
|
---|
71 | }
|
---|
72 |
|
---|
73 | private:
|
---|
74 | BUSBDevice& fDevice;
|
---|
75 | };
|
---|