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