-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.go
317 lines (275 loc) · 8.7 KB
/
eeprom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright (C) 2014 Steven Stallion <sstallion@gmail.com>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
// Package eeprom provides an idiomatic interface to USB EEPROM programmers
// that conform to http://github.com/sstallion/usb-eeprom/wiki/Protocol. Due
// to the chip-agnostic nature of the protocol, constraints such as capacity
// and alignment must be enforced by the caller.
package eeprom
/*
#cgo LDFLAGS: -lusb-1.0
#include <libusb-1.0/libusb.h>
*/
import "C"
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"reflect"
"time"
"unsafe"
)
const (
// MaxBytes is the maximum amount of addressable data.
MaxBytes = 1 << 16
)
const (
idVendor = 0x04d8 // Microchip Technology, Inc.
idProduct = 0xf4cd // 28Cxxx EEPROM Programmer
interfaceNum = 0
endpointNum = 1
endpointIN = endpointNum | C.LIBUSB_ENDPOINT_IN
endpointOUT = endpointNum | C.LIBUSB_ENDPOINT_OUT
)
type libusbError struct {
code C.int
}
func (e *libusbError) Error() string {
return fmt.Sprintf("%s (%s)",
C.GoString(C.libusb_strerror(C.enum_libusb_error(e.code))),
C.GoString(C.libusb_error_name(e.code)))
}
// Device represents an attached USB EEPROM programmer.
type Device struct {
dev *C.libusb_device
handle *C.libusb_device_handle
pagesize int
}
// ID returns a string suitable for uniquely identifying the device.
func (d *Device) ID() string {
return fmt.Sprintf("%d:%d",
C.libusb_get_bus_number(d.dev),
C.libusb_get_device_address(d.dev))
}
// SetPageSize sets the number of bytes written per page by WritePages. By
// default, the maximum packet size supported by the endpoint is used.
func (d *Device) SetPageSize(pagesize int) { d.pagesize = pagesize }
// Open opens an attached device and claims the interface. To ensure proper
// reference counting, Open must be called within the context of a Walk.
func (d *Device) Open() error {
if err := C.libusb_open(d.dev, &d.handle); err != C.LIBUSB_SUCCESS {
return &libusbError{err}
}
if err := C.libusb_claim_interface(d.handle, interfaceNum); err != C.LIBUSB_SUCCESS {
C.libusb_close(d.handle)
return &libusbError{err}
}
return nil
}
// Close releases the interface and closes the device. A device may not be
// opened again after calling this method. Returned errors may be safely
// ignored.
func (d *Device) Close() error {
defer C.libusb_close(d.handle)
if err := C.libusb_release_interface(d.handle, interfaceNum); err != C.LIBUSB_SUCCESS {
return &libusbError{err}
}
return nil
}
// Reset issues a device reset. This method may be called after a failed
// transfer to reset the interface. Returned errors may be safely ignored.
func (d *Device) Reset() error {
defer time.Sleep(500 * time.Millisecond) // wait for device to settle
if err := C.libusb_reset_device(d.handle); err != C.LIBUSB_SUCCESS {
return &libusbError{err}
}
return nil
}
// Read reads into the given slice at the supplied starting address.
func (d *Device) Read(start uint16, data []byte) error {
var b bytes.Buffer
var n = uint16(len(data))
b.WriteByte('R')
binary.Write(&b, binary.LittleEndian, start)
binary.Write(&b, binary.LittleEndian, n-1)
if err := d.validate(start, data); err != nil {
return err
}
if err := d.transfer(endpointOUT, b.Bytes()); err != nil {
return err
}
if err := d.transfer(endpointIN, data); err != nil {
return err
}
return d.verify(start + n)
}
// WriteBytes writes the given slice starting at the supplied starting address.
func (d *Device) WriteBytes(start uint16, data []byte) error {
var b bytes.Buffer
var n = uint16(len(data))
b.WriteByte('W')
binary.Write(&b, binary.LittleEndian, start)
binary.Write(&b, binary.LittleEndian, n-1)
if err := d.validate(start, data); err != nil {
return err
}
if err := d.transfer(endpointOUT, b.Bytes()); err != nil {
return err
}
if err := d.transfer(endpointOUT, data); err != nil {
return err
}
return d.verify(start + n)
}
// WritePages writes the given slice starting at the supplied starting address.
func (d *Device) WritePages(start uint16, data []byte) error {
var b bytes.Buffer
var n = uint16(len(data))
b.WriteByte('P')
binary.Write(&b, binary.LittleEndian, start)
binary.Write(&b, binary.LittleEndian, n-1)
if err := d.validate(start, data); err != nil {
return err
}
if err := d.transfer(endpointOUT, b.Bytes()); err != nil {
return err
}
if err := d.transferN(endpointOUT, data, d.pagesize); err != nil {
return err
}
return d.verify(start + n)
}
// Erase issues a Chip Erase.
func (d *Device) Erase() error {
var b bytes.Buffer
b.WriteByte('Z')
if err := d.transfer(endpointOUT, b.Bytes()); err != nil {
return err
}
return d.verify(0)
}
func (d *Device) validate(start uint16, data []byte) error {
if len(data) == 0 {
return errors.New("no data")
}
if int(start)+len(data) > MaxBytes {
return errors.New("too much data")
}
return nil
}
func (d *Device) transfer(endpoint uint8, data []byte) error {
return d.transferN(endpoint, data, 0)
}
func (d *Device) transferN(endpoint uint8, data []byte, n int) error {
if m := int(C.libusb_get_max_packet_size(d.dev, C.uchar(endpoint))); n == 0 {
n = m
} else if n > m {
return errors.New("invalid packet size")
}
for len, off := len(data), 0; len > 0; {
var transferred int
if n > len {
n = len
}
if err := C.libusb_bulk_transfer(d.handle, C.uchar(endpoint), (*C.uchar)(&data[off]), C.int(n),
(*C.int)(unsafe.Pointer(&transferred)), 2500); err != C.LIBUSB_SUCCESS {
return &libusbError{err}
}
len -= transferred
off += transferred
}
return nil
}
func (d *Device) verify(expected uint16) error {
var status uint16
var data = []byte{0xff, 0xff}
if err := d.transfer(endpointIN, data); err != nil {
return err
}
binary.Read(bytes.NewReader(data), binary.LittleEndian, &status)
if status != expected {
return fmt.Errorf("expected status %#x; got %#x", expected, status)
}
return nil
}
var context *C.libusb_context
func init() {
if err := C.libusb_init(&context); err != C.LIBUSB_SUCCESS {
panic(&libusbError{err})
}
}
/*
func fini() {
C.libusb_exit(context)
}
*/
// First returns the first supported device attached to the host. Unlike Walk,
// the returned Device is opened automatically. This function exists primarily
// for testing.
func First() (*Device, error) {
handle := C.libusb_open_device_with_vid_pid(context, idVendor, idProduct)
if handle == nil {
return nil, errors.New("no devices found")
}
if err := C.libusb_claim_interface(handle, interfaceNum); err != C.LIBUSB_SUCCESS {
C.libusb_close(handle)
return nil, &libusbError{err}
}
return &Device{
dev: C.libusb_get_device(handle),
handle: handle,
}, nil
}
// Walk calls the specified function for each supported device attached to the
// host. To ensure proper reference counting, Open must be called within the
// context of a Walk.
func Walk(fn func(*Device) error) error {
var list **C.libusb_device
var found int
n := C.libusb_get_device_list(context, &list)
if n < C.LIBUSB_SUCCESS {
return &libusbError{C.int(n)}
}
defer C.libusb_free_device_list(list, 1)
h := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(list)),
Len: int(n),
Cap: int(n),
}
for _, dev := range *(*[]*C.libusb_device)(unsafe.Pointer(&h)) {
var desc C.struct_libusb_device_descriptor
if err := C.libusb_get_device_descriptor(dev, &desc); err != C.LIBUSB_SUCCESS {
return &libusbError{err}
}
if desc.idVendor == idVendor && desc.idProduct == idProduct {
if err := fn(&Device{dev: dev}); err != nil {
return err
}
found++
}
}
if found == 0 {
return errors.New("no devices found")
}
return nil
}