Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
F
FMC ADC 100M 14b 4cha - Testing
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
image/svg+xml
Discourse
Discourse
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
FMC ADC 100M 14b 4cha - Testing
Commits
da09d702
Commit
da09d702
authored
Jan 10, 2012
by
Matthieu Cattin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add cp210x EEPROM read/write utility, to store calibration box data.
parent
a6e17fb6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
1199 additions
and
0 deletions
+1199
-0
__init__.py
test/fmcadc100m14b4cha/python/cp210x_eeprom/__init__.py
+4
-0
cp210x.py
test/fmcadc100m14b4cha/python/cp210x_eeprom/cp210x.py
+398
-0
eeprom.py
test/fmcadc100m14b4cha/python/cp210x_eeprom/eeprom.py
+180
-0
usb.py
test/fmcadc100m14b4cha/python/cp210x_eeprom/usb.py
+274
-0
valuefile.py
test/fmcadc100m14b4cha/python/cp210x_eeprom/valuefile.py
+234
-0
test21.py
test/fmcadc100m14b4cha/python/test21.py
+109
-0
No files found.
test/fmcadc100m14b4cha/python/cp210x_eeprom/__init__.py
0 → 100644
View file @
da09d702
# -*- coding: utf-8 -*-
__author__
=
"Johannes Hölzl <johannes.hoelzl@gmx.de>"
__license__
=
"GNU LGPL"
test/fmcadc100m14b4cha/python/cp210x_eeprom/cp210x.py
0 → 100644
View file @
da09d702
This diff is collapsed.
Click to expand it.
test/fmcadc100m14b4cha/python/cp210x_eeprom/eeprom.py
0 → 100644
View file @
da09d702
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Johannes Hölzl <johannes.hoelzl@gmx.de>
#
# This library is covered by the GNU LGPL, read LICENSE for details.
import
cp210x
from
cp210x
import
from_binary
,
to_binary
,
iif
,
VALUES
__all__
=
[
'EEPROM'
,
'HexFileError'
]
POS_BAUDRATE_TABLE
=
0x0000
POS_PART_NUMBER
=
0x01FF
POS_PRODUCT_STRING
=
0x0208
POS_SERIAL_NUMBER
=
0x0307
POS_PRODUCT_ID
=
0x0390
POS_VENDOR_ID
=
0x0392
POS_VERSION
=
0x0394
POS_CFG_ATTRIBUTES
=
0x03A1
POS_MAX_POWER
=
0x03A2
POS_VENDOR_STRING
=
0x03C3
POS_LOCK_VALUE
=
0x03FF
class
HexFileError
(
StandardError
):
pass
def
checksum
(
line
):
return
sum
(
ord
(
c
)
for
c
in
line
)
&
0xFF
def
_int_value
(
position
,
size
,
read
=
lambda
x
:
x
,
write
=
lambda
x
:
x
):
def
get
(
self
):
return
read
(
from_binary
(
self
.
get
(
position
,
size
)))
def
set
(
self
,
value
):
self
.
set
(
position
,
to_binary
(
write
(
value
),
size
))
return
property
(
get
,
set
)
def
_str_value
(
position
,
max_size
):
def
get
(
self
):
size
=
from_binary
(
self
.
get
(
position
,
1
))
assert
size
<=
(
max_size
+
2
)
and
size
>=
2
assert
self
.
get
(
position
+
1
,
1
)
==
'
\x03
'
,
"Missing 0x03 at
%04
X"
%
(
position
+
1
)
return
self
.
get
(
position
+
2
,
size
-
2
)
.
decode
(
'utf-16-le'
)
def
set
(
self
,
value
):
encoded
=
value
.
encode
(
'utf-16-le'
)
assert
len
(
encoded
)
<=
max_size
self
.
set
(
position
,
chr
(
len
(
encoded
)
+
2
)
+
'
\x03
'
+
encoded
)
return
property
(
get
,
set
)
class
EEPROM
(
object
):
START_ADDRESS
=
0x3600
def
__init__
(
self
,
content
=
None
):
if
isinstance
(
content
,
str
)
or
content
is
None
:
assert
content
is
None
or
len
(
content
)
==
cp210x
.
SIZE_EEPROM
self
.
content
=
content
elif
isinstance
(
content
,
cp210x
.
Cp210xProgrammer
):
self
.
content
=
content
.
get_eeprom_content
()
else
:
self
.
parse_hex_file
(
content
.
read
())
def
write_to_cp210x
(
self
,
cp210xDevice
):
cp210xDevice
.
set_eeprom_content
(
self
.
content
)
def
parse_hex_file
(
self
,
hex_content
):
self
.
content
=
''
address
=
self
.
START_ADDRESS
for
tag
in
hex_content
.
split
(
'
\n
'
):
if
not
tag
.
startswith
(
':'
):
raise
HexFileError
(
"Line doesn't start with ':'"
)
try
:
content
=
tag
[
1
:]
.
decode
(
'hex'
)
except
TypeError
:
raise
HexFileError
(
"Hex data expected"
)
if
len
(
content
)
<
5
:
raise
HexFileError
(
"Line to short"
)
if
checksum
(
content
)
!=
0
:
raise
HexFileError
(
"Checksum error"
)
size
=
from_binary
(
content
[
0
])
tag_address
=
from_binary
(
content
[
1
:
3
],
le
=
False
)
tag_type
=
from_binary
(
content
[
3
:
4
])
line
=
content
[
4
:
-
1
]
if
tag_type
==
0x00
:
if
tag_address
!=
address
:
raise
HexFileError
(
"Expected address
%04
X but found
%04
X"
%
(
address
,
tag_address
))
self
.
content
+=
line
address
+=
len
(
line
)
elif
tag_type
==
0x01
:
if
size
!=
0
or
len
(
line
)
!=
0
:
raise
HexFileError
(
"Defekt end tag"
)
break
else
:
raise
HexFileError
(
"Unknown tag type
%02
X"
%
tag_type
)
def
build_hex_file
(
self
):
for
tag_start
in
range
(
0
,
len
(
self
.
content
),
0x10
):
line
=
self
.
content
[
tag_start
:
tag_start
+
0x10
]
address
=
self
.
START_ADDRESS
+
tag_start
tag
=
(
to_binary
(
len
(
line
),
1
)
+
to_binary
(
address
,
le
=
False
)
+
'
\x00
'
+
line
)
cs
=
checksum
(
tag
)
if
cs
==
0
:
tag
+=
'
\x00
'
else
:
tag
+=
chr
(
0x100
-
cs
)
yield
":
%
s
\n
"
%
tag
.
encode
(
'hex'
)
yield
":00000001FF
\n
"
def
write_hex_file
(
self
,
f
):
if
isinstance
(
f
,
str
):
f
=
file
(
f
,
'wb'
)
do_close
=
True
else
:
do_close
=
False
for
line
in
self
.
build_hex_file
():
f
.
write
(
line
)
if
do_close
:
f
.
close
()
def
read_hex_file
(
self
,
f
):
if
isinstance
(
f
,
str
):
f
=
file
(
f
,
'rb'
)
do_close
=
True
else
:
do_close
=
False
self
.
parse_hex_file
(
f
.
read
())
if
do_close
:
f
.
close
()
def
get
(
self
,
pos
,
length
):
return
self
.
content
[
pos
:
pos
+
length
]
def
set
(
self
,
pos
,
data
):
self
.
content
=
(
self
.
content
[:
pos
]
+
data
+
self
.
content
[
pos
+
len
(
data
):])
def
_get_baudrate_table
(
self
):
dat
=
self
.
get
(
POS_BAUDRATE_TABLE
,
cp210x
.
SIZE_BAUDRATE_TABLE
)
return
[
cp210x
.
parse_baudrate_cfg
(
dat
[
pos
:
pos
+
cp210x
.
SIZE_BAUDRATE_CFG
])
for
pos
in
range
(
0
,
cp210x
.
SIZE_BAUDRATE_TABLE
,
cp210x
.
SIZE_BAUDRATE_CFG
)]
def
_set_baudrate_table
(
self
,
baudrates
):
assert
len
(
baudrates
)
==
cp210x
.
SIZE_BAUDRATES
self
.
set
(
POS_BAUDRATE_TABLE
,
''
.
join
(
cp210x
.
build_baudrate_cfg
(
*
cfg
)
for
cfg
in
baudrates
))
baudrate_table
=
property
(
_get_baudrate_table
,
_set_baudrate_table
)
product_string
=
_str_value
(
POS_PRODUCT_STRING
,
cp210x
.
SIZE_PRODUCT_STRING
)
serial_number
=
_str_value
(
POS_SERIAL_NUMBER
,
cp210x
.
SIZE_SERIAL_NUMBER
)
part_number
=
_int_value
(
POS_PART_NUMBER
,
1
)
product_id
=
_int_value
(
POS_PRODUCT_ID
,
2
)
vendor_id
=
_int_value
(
POS_VENDOR_ID
,
2
)
version
=
_int_value
(
POS_VERSION
,
2
,
cp210x
.
from_bcd2
,
cp210x
.
to_bcd2
)
bus_powered
=
_int_value
(
POS_CFG_ATTRIBUTES
,
1
,
lambda
a
:
bool
(
a
&
0x40
),
lambda
a
:
iif
(
a
,
0xC0
,
0x80
))
max_power
=
_int_value
(
POS_MAX_POWER
,
1
,
lambda
p
:
p
*
2
,
cp210x
.
to_div2
)
vendor_string
=
_str_value
(
POS_VENDOR_STRING
,
cp210x
.
SIZE_VENDOR_STRING
)
locked
=
_int_value
(
POS_LOCK_VALUE
,
1
,
lambda
l
:
l
==
cp210x
.
LCK_LOCKED
,
lambda
b
:
iif
(
b
,
cp210x
.
LCK_LOCKED
,
cp210x
.
LCK_UNLOCKED
))
def
get_values
(
self
):
return
dict
((
name
,
getattr
(
self
,
name
))
for
name
,
type
in
VALUES
)
def
set_values
(
self
,
values
):
for
name
,
value
in
values
.
items
():
setattr
(
self
,
name
,
value
)
test/fmcadc100m14b4cha/python/cp210x_eeprom/usb.py
0 → 100644
View file @
da09d702
# Python interface for "usb.h" version 0.1.4.4.4
#
# Copyright (c) 2005 Robert Hoelzl <robert.hoelzl@gmx.de>
# Copyright (c) 2007 Johannes Hoelzl <johannes.hoelzl@gmx.de>
#
# This library is covered by the GNU LGPL, read LICENSE for details.
from
ctypes
import
*
import
sys
class
LibUsbNotInstalled
(
OSError
):
pass
try
:
if
sys
.
platform
==
'darwin'
:
PATH_MAX
=
1024
dll
=
cdll
.
LoadLibrary
(
"libusb.dylib"
)
elif
sys
.
platform
==
'linux2'
:
PATH_MAX
=
4096
dll
=
cdll
.
LoadLibrary
(
"libusb.so"
)
else
:
raise
NotImplementedError
(
"Platform
%
s not supported by usb.py"
%
sys
.
platform
)
except
OSError
:
raise
LibUsbNotInstalled
()
# helper functions
def
func
(
f
,
*
args
,
**
retval
):
f
.
restype
=
retval
.
get
(
'retval'
,
None
)
f
.
argtypes
=
args
if
retval
.
has_key
(
'rename'
):
globals
()[
retval
[
'rename'
]]
=
f
else
:
globals
()[
f
.
__name__
[
4
:]]
=
f
# constants
CLASS_PER_INTERFACE
=
0
USB_CLASS_AUDIO
=
1
CLASS_COMM
=
2
CLASS_HID
=
3
CLASS_PRINTER
=
7
CLASS_PTP
=
6
CLASS_MASS_STORAGE
=
8
CLASS_HUB
=
9
CLASS_DATA
=
10
CLASS_VENDOR_SPEC
=
0xff
DT_DEVICE
=
0x01
DT_CONFIG
=
0x02
DT_STRING
=
0x03
DT_INTERFACE
=
0x04
DT_ENDPOINT
=
0x05
DT_HID
=
0x21
DT_REPORT
=
0x22
DT_PHYSICAL
=
0x23
DT_HUB
=
0x29
DT_DEVICE_SIZE
=
18
DT_CONFIG_SIZE
=
9
DT_INTERFACE_SIZE
=
9
DT_ENDPOINT_SIZE
=
7
DT_ENDPOINT_AUDIO_SIZE
=
9
# Audio extension
DT_HUB_NONVAR_SIZE
=
7
class
descriptor_header
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
)
]
class
string_descriptor
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
),
(
"wData"
,
c_uint
*
1
)
]
class
hid_descriptor
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
),
(
"bcdHID"
,
c_uint16
),
(
"bCountryCode"
,
c_uint8
),
(
"bNumDescriptors"
,
c_uint8
)
]
MAXENDPOINTS
=
32
class
endpoint_descriptor
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
),
(
"bEndpointAddress"
,
c_uint8
),
(
"bmAttributes"
,
c_uint8
),
(
"wMaxPacketSize"
,
c_uint16
),
(
"bInterval"
,
c_uint8
),
(
"bRefresh"
,
c_uint8
),
(
"bSynchAddress"
,
c_uint8
),
(
"extra"
,
POINTER
(
c_uint8
)),
(
"extralen"
,
c_int
)
]
ENDPOINT_ADDRESS_MASK
=
0x0f
# in bEndpointAddress
ENDPOINT_DIR_MASK
=
0x80
ENDPOINT_TYPE_MASK
=
0x03
# in bmAttributes
ENDPOINT_TYPE_CONTROL
=
0
ENDPOINT_TYPE_ISOCHRONOUS
=
1
ENDPOINT_TYPE_BULK
=
2
ENDPOINT_TYPE_INTERRUPT
=
3
MAXINTERFACES
=
32
class
interface_descriptor
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
),
(
"bInterfaceNumber"
,
c_uint8
),
(
"bAlternateSetting"
,
c_uint8
),
(
"bNumEndpoints"
,
c_uint8
),
(
"bInterfaceClass"
,
c_uint8
),
(
"bInterfaceSubClass"
,
c_uint8
),
(
"bInterfaceProtocol"
,
c_uint8
),
(
"iInterface"
,
c_uint8
),
(
"endpoint"
,
POINTER
(
endpoint_descriptor
)),
(
"extra"
,
POINTER
(
c_uint8
)),
(
"extralen"
,
c_int
)
]
MAXALTSETTING
=
128
# Hard limit
class
interface
(
Structure
):
_fields_
=
[
(
"altsetting"
,
POINTER
(
interface_descriptor
)),
(
"num_altsetting"
,
c_int
)
]
MAXCONFIG
=
8
class
config_descriptor
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
),
(
"wTotalLength"
,
c_uint16
),
(
"bNumInterfaces"
,
c_uint8
),
(
"bConfigurationValue"
,
c_uint8
),
(
"iConfiguration"
,
c_uint16
),
(
"bmAttributes"
,
c_uint8
),
(
"MaxPower"
,
c_uint8
),
(
"interface"
,
POINTER
(
interface
)),
(
"extra"
,
POINTER
(
c_uint8
)),
# Extra descriptors
(
"extralen"
,
c_int
)
]
class
device_descriptor
(
Structure
):
_fields_
=
[
(
"bLength"
,
c_uint8
),
(
"bDescriptorType"
,
c_uint8
),
(
"bcdUSB"
,
c_uint16
),
(
"bDeviceClass"
,
c_uint8
),
(
"bDeviceSubClass"
,
c_uint8
),
(
"bDeviceProtocol"
,
c_uint8
),
(
"bMaxPacketSize0"
,
c_uint8
),
(
"idVendor"
,
c_uint16
),
(
"idProduct"
,
c_uint16
),
(
"bcdDevice"
,
c_uint16
),
(
"iManufacturer"
,
c_uint8
),
(
"iProduct"
,
c_uint8
),
(
"iSerialNumber"
,
c_uint8
),
(
"bNumConfigurations"
,
c_uint8
)
]
class
ctrl_setup
(
Structure
):
_fields_
=
[
(
"bRequestType"
,
c_uint8
),
(
"bRequest"
,
c_uint8
),
(
"wValue"
,
c_uint16
),
(
"wIndex"
,
c_uint16
),
(
"wLength"
,
c_uint16
)
]
REQ_GET_STATUS
=
0x00
REQ_CLEAR_FEATURE
=
0x01
# 0x02 is reserved
REQ_SET_FEATURE
=
0x03
# 0x04 is reserved
REQ_SET_ADDRESS
=
0x05
REQ_GET_DESCRIPTOR
=
0x06
REQ_SET_DESCRIPTOR
=
0x07
REQ_GET_CONFIGURATION
=
0x08
REQ_SET_CONFIGURATION
=
0x09
REQ_GET_INTERFACE
=
0x0A
REQ_SET_INTERFACE
=
0x0B
REQ_SYNCH_FRAME
=
0x0C
TYPE_STANDARD
=
(
0x00
<<
5
)
TYPE_CLASS
=
(
0x01
<<
5
)
TYPE_VENDOR
=
(
0x02
<<
5
)
TYPE_RESERVED
=
(
0x03
<<
5
)
RECIP_DEVICE
=
0x00
RECIP_INTERFACE
=
0x01
RECIP_ENDPOINT
=
0x02
RECIP_OTHER
=
0x03
ENDPOINT_IN
=
0x80
ENDPOINT_OUT
=
0x00
# Error codes
ERROR_BEGIN
=
500000
#if 1
#define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0)
#else
#define USB_LE16_TO_CPU(x)
#endif
device_p
=
POINTER
(
"device"
)
bus_p
=
POINTER
(
"bus"
)
class
device
(
Structure
):
_fields_
=
[
(
"next"
,
device_p
),
(
"prev"
,
device_p
),
(
"filename"
,
c_char
*
(
PATH_MAX
+
1
)),
(
"bus"
,
bus_p
),
(
"descriptor"
,
device_descriptor
),
(
"config"
,
POINTER
(
config_descriptor
)),
(
"dev"
,
c_void_p
),
# Darwin support
(
"devnum"
,
c_char
),
(
"num_children"
,
c_uint8
),
(
"children"
,
POINTER
(
device_p
))
]
SetPointerType
(
device_p
,
device
)
class
bus
(
Structure
):
_fields_
=
[
(
"next"
,
bus_p
),
(
"prev"
,
bus_p
),
(
"dirname"
,
c_char
*
(
PATH_MAX
+
1
)),
(
"devices"
,
device_p
),
(
"location"
,
c_uint
),
(
"root_dev"
,
device_p
)
]
SetPointerType
(
bus_p
,
bus
)
dev_handle_p
=
c_void_p
func
(
dll
.
usb_open
,
device_p
,
retval
=
dev_handle_p
,
rename
=
"_open"
)
func
(
dll
.
usb_close
,
dev_handle_p
,
retval
=
c_int
)
func
(
dll
.
usb_get_string
,
dev_handle_p
,
c_int
,
c_int
,
c_char_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_get_string_simple
,
dev_handle_p
,
c_int
,
c_char_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_get_descriptor_by_endpoint
,
dev_handle_p
,
c_int
,
c_uint8
,
c_uint8
,
c_void_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_get_descriptor
,
dev_handle_p
,
c_uint8
,
c_uint8
,
c_void_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_bulk_write
,
dev_handle_p
,
c_int
,
c_char_p
,
c_int
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_bulk_read
,
dev_handle_p
,
c_int
,
c_char_p
,
c_int
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_interrupt_write
,
dev_handle_p
,
c_int
,
c_char_p
,
c_int
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_interrupt_read
,
dev_handle_p
,
c_int
,
c_char_p
,
c_int
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_control_msg
,
dev_handle_p
,
c_int
,
c_int
,
c_int
,
c_int
,
c_char_p
,
c_int
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_set_configuration
,
dev_handle_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_claim_interface
,
dev_handle_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_release_interface
,
dev_handle_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_set_altinterface
,
dev_handle_p
,
c_int
,
retval
=
c_int
)
func
(
dll
.
usb_resetep
,
dev_handle_p
,
c_uint16
,
retval
=
c_int
)
func
(
dll
.
usb_clear_halt
,
dev_handle_p
,
c_uint16
,
retval
=
c_int
)
func
(
dll
.
usb_reset
,
dev_handle_p
,
retval
=
c_int
)
func
(
dll
.
usb_strerror
,
retval
=
c_char_p
)
func
(
dll
.
usb_init
)
func
(
dll
.
usb_set_debug
,
c_int
)
func
(
dll
.
usb_find_busses
,
retval
=
c_int
)
func
(
dll
.
usb_find_devices
,
retval
=
c_int
)
func
(
dll
.
usb_device
,
dev_handle_p
,
retval
=
device_p
,
rename
=
"get_device"
)
func
(
dll
.
usb_get_busses
,
retval
=
bus_p
)
func
(
dll
.
usb_detach_kernel_driver_np
,
dev_handle_p
,
c_int
,
retval
=
c_int
)
# workaround for bug in ctypes 0.9.6 (cannot create functions with c_void_p as retval)
def
open
(
dev
):
return
cast
(
_open
(
dev
),
dev_handle_p
)
test/fmcadc100m14b4cha/python/cp210x_eeprom/valuefile.py
0 → 100644
View file @
da09d702
# -*- coding: utf-8 -*-
# Copyright (c) 2007 Johannes Hölzl <johannes.hoelzl@gmx.de>
#
# This library is covered by the GNU LGPL, read LICENSE for details.
# For documentation of the baudrate table see:
#
# [AN205] Silicon Labs Application Note 205 Rev. 0.3
# http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an205.pdf
import
re
from
ConfigParser
import
ConfigParser
import
cp210x
from
cp210x
import
VALUES
,
SIZE_BAUDRATES
__all__
=
[
'read_file'
,
'write_file'
,
'update_values'
,
'PrescalerIsZero'
,
'ValuesFileError'
]
class
ValuesError
(
StandardError
):
pass
class
PrescalerIsZero
(
ValuesError
):
pass
class
ValuesFileError
(
ValuesError
):
pass
version_pattern
=
re
.
compile
(
r'^\s*(\d\d?)\.(\d\d?)\s*$'
)
def
read_version
(
s
):
match
=
version_pattern
.
match
(
s
)
if
match
is
None
:
raise
ValueError
(
"Version does not match 'xx.yy'"
)
return
(
int
(
match
.
group
(
1
)),
int
(
match
.
group
(
2
)))
def
write_version
(
v
):
return
"
%
d.
%02
d"
%
v
def
read_hex
(
s
):
return
int
(
s
.
strip
(),
16
)
def
write_hex
(
num
):
return
"
%04
X"
%
num
def
write_bool
(
b
):
if
b
:
return
'yes'
else
:
return
'no'
def
read_bool
(
s
):
s
=
s
.
strip
()
.
lower
()
if
s
not
in
[
'true'
,
'yes'
,
'false'
,
'no'
]:
raise
ValueError
(
"Boolean must be either 'true', 'yes', 'false' or 'no'."
)
return
s
in
[
'true'
,
'yes'
]
def
read_baudrate_info
(
s
):
values
=
s
.
split
(
','
)
if
len
(
values
)
!=
3
:
raise
ValueError
(
"Baudrate info must be three comma-separated items"
)
try
:
baudgen
=
read_hex
(
values
[
0
])
except
ValueError
:
raise
ValueError
(
"The first baudrate info must be a hex-value"
)
try
:
timer0
=
read_hex
(
values
[
1
])
except
ValueError
:
raise
ValueError
(
"The second baudrate info must be a hex-value"
)
try
:
prescale
=
int
(
values
[
2
])
except
ValueError
:
raise
ValueError
(
"The thirdbaudrate info must be a number"
)
return
(
baudgen
,
timer0
,
prescale
)
TYPES
=
{
'boolean'
:
(
read_bool
,
write_bool
),
'int'
:
(
int
,
str
),
'id'
:
(
read_hex
,
write_hex
),
'string'
:
(
str
,
str
),
'version'
:
(
read_version
,
write_version
),
}
def
read_file
(
fp
):
cp
=
ConfigParser
()
if
isinstance
(
fp
,
str
):
cp
.
read
([
fp
])
else
:
cp
.
readfp
(
fp
)
values
=
{}
for
name
,
type
in
VALUES
:
if
name
is
'baudrate_table'
:
continue
reader
,
_
=
TYPES
[
type
]
if
cp
.
has_option
(
'usb device'
,
name
):
try
:
values
[
name
]
=
reader
(
cp
.
get
(
'usb device'
,
name
))
except
ValueError
,
err
:
raise
ValuesFileError
(
"Key '
%
s':
%
s"
%
(
name
,
str
(
err
)))
if
cp
.
has_section
(
'baudrate table'
):
baudrate_table
=
[]
for
name
,
value
in
cp
.
items
(
'baudrate table'
):
try
:
baudrate
=
int
(
name
)
except
ValueError
:
raise
ValuesFileError
(
"Key names in 'baudrate table' must be"
" baudrate numbers."
)
try
:
baudrate_table
.
append
(
read_baudrate_info
(
value
)
+
(
baudrate
,
))
except
ValueError
,
err
:
raise
ValuesFileError
(
"Wrong baudrate info
%
i:
%
s"
%
(
baudrate
,
str
(
err
)))
baudrate_table
.
sort
(
key
=
(
lambda
i
:
i
[
3
]),
reverse
=
True
)
values
[
'baudrate_table'
]
=
baudrate_table
return
values
def
write_file
(
fp
,
values
):
fp
.
write
(
"[usb device]
\n
"
)
for
name
,
type
in
VALUES
:
if
name
==
'baudrate_table'
:
continue
_
,
writer
=
TYPES
[
type
]
if
name
in
values
:
fp
.
write
(
"
%
s =
%
s
\n
"
%
(
name
,
writer
(
values
[
name
])))
if
'baudrate_table'
in
values
:
fp
.
write
(
"
\n
"
)
fp
.
write
(
"[baudrate table]
\n
"
)
for
(
baudgen
,
timegen
,
prescaler
,
baudrate
)
in
sorted
(
values
[
'baudrate_table'
],
key
=
(
lambda
i
:
i
[
3
]),
reverse
=
True
):
fp
.
write
(
"
%7
d =
%04
X,
%04
X,
%
d #
%
s
\n
"
%
(
baudrate
,
baudgen
,
timegen
,
prescaler
,
show_baudrate
(
baudgen
,
timegen
,
prescaler
)))
def
calc_baudrate
(
baudgen
,
timegen
,
prescaler
):
# This formulas are from AN205 page 5.
if
prescaler
==
0
:
raise
PrescalerIsZero
(
"Prescaler is 0"
)
baudrate
=
(
24000000.
/
prescaler
)
/
(
0x10000
-
baudgen
)
return
(
baudrate
,
(
0x10000
-
timegen
)
*
2
)
def
show_baudrate
(
baudgen
,
timegen
,
prescaler
):
try
:
baudrate
,
timeout
=
calc_baudrate
(
baudgen
,
timegen
,
prescaler
)
except
PrescalerIsZero
:
return
"Wrong data, Prescaler is 0."
if
timeout
>=
1000
:
timeout
=
"
%1.3
f ms"
%
(
float
(
timeout
)
/
1000
)
else
:
timeout
=
"
%
d us"
%
timeout
if
baudrate
is
None
:
return
",
%
s"
%
(
baudrate
,
timeout
)
else
:
return
"
%7.0
f Baud,
%
s"
%
(
baudrate
,
timeout
)
def
update_values
(
v
,
new
,
dev
):
old_baudrate_table
=
v
.
get
(
'baudrate_table'
)
new_baudrate_table
=
new
.
get
(
'baudrate_table'
)
v
.
update
(
new
)
# update baudrate table
# it is needed, that the baudrate table has 32 entries when it is written
# to the eeprom or device.
if
((
old_baudrate_table
is
not
None
or
new_baudrate_table
is
not
None
)
and
(
new_baudrate_table
is
None
or
len
(
new_baudrate_table
)
<
SIZE_BAUDRATES
)):
if
old_baudrate_table
is
not
None
:
if
len
(
old_baudrate_table
)
<
SIZE_BAUDRATES
:
baudrate_table
=
old_baudrate_table
else
:
baudrate_table
=
list
(
merge_baudrate_table
(
dev
.
baudrate_table
,
old_baudrate_table
))
else
:
baudrate_table
=
dev
.
baudrate_table
if
new_baudrate_table
:
baudrate_table
=
list
(
merge_baudrate_table
(
baudrate_table
,
new_baudrate_table
))
v
[
'baudrate_table'
]
=
baudrate_table
def
merge_baudrate_table
(
old
,
new
):
for
(
old_info
,
(
start
,
stop
))
in
zip
(
old
,
REQUEST_BAUDRATE_RANGES
):
for
baudgen
,
timer
,
prescaler
,
baudrate
in
new
:
if
((
start
is
None
or
baudrate
<=
start
)
and
baudrate
>=
stop
):
yield
(
baudgen
,
timer
,
prescaler
,
baudrate
)
break
else
:
yield
old_info
REQUEST_BAUDRATE_RANGES
=
[
# The table data is from AN205 Table 1 on page 1.
# Start End Default Baudrate
(
None
,
2457601
),
# Undefined
(
2457600
,
1474561
),
# Undefined
(
1474560
,
1053258
),
# Undefined
(
1053257
,
670255
),
# 921600
(
670254
,
567139
),
# 576000
(
567138
,
491521
),
# 500000
(
491520
,
273067
),
# 460800
(
273066
,
254235
),
# 256000
(
254234
,
237833
),
# 250000
(
237832
,
156869
),
# 230400
(
156868
,
129348
),
# 153600
(
129347
,
117029
),
# 128000
(
117028
,
77609
),
# 115200
(
77608
,
64112
),
# 76800
(
64111
,
58054
),
# 64000
(
58053
,
56281
),
# 57600
(
56280
,
51559
),
# 56000
(
51558
,
38602
),
# 51200
(
38601
,
28913
),
# 38400
(
28912
,
19251
),
# 28800
(
19250
,
16063
),
# 19200
(
16062
,
14429
),
# 16000
(
14428
,
9613
),
# 14400
(
9612
,
7208
),
# 9600
(
7207
,
4804
),
# 7200
(
4803
,
4001
),
# 4800
(
4000
,
2401
),
# 4000
(
2400
,
1801
),
# 2400
(
1800
,
1201
),
# 1800
(
1200
,
601
),
# 1200
(
600
,
301
),
# 600
(
300
,
57
),
# 300
]
test/fmcadc100m14b4cha/python/test21.py
0 → 100755
View file @
da09d702
#! /usr/bin/env python
# coding: utf8
# Copyright CERN, 2011
# Author: Matthieu Cattin <matthieu.cattin@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
import
sys
import
rr
import
time
import
os
import
re
from
ctypes
import
*
from
ptsexcept
import
*
from
cp210x_eeprom
import
usb
,
valuefile
,
cp210x
from
cp210x_eeprom.eeprom
import
EEPROM
"""
test21: Store calibration data to calibraton box CP2103 EEPROM
"""
# Calibration box vendor and device IDs
BOX_USB_VID
=
"10C4"
# Cygnal Integrated Products, Inc.
BOX_USB_PID
=
"EA60"
# CP210x Composite Device
CALIBR_RANGES
=
[
'10V'
,
'1V'
,
'100mV'
]
def
find_device
(
vid
,
pid
):
usb_patterns
=
[]
vid
=
int
(
vid
,
16
)
pid
=
int
(
pid
,
16
)
usb_patterns
.
append
(
dict
(
idVendor
=
vid
,
idProduct
=
pid
))
for
dev
in
cp210x
.
Cp210xProgrammer
.
list_devices
(
usb_patterns
):
return
dev
def
get_calibr_data
(
dev
):
dev
.
open
()
try
:
eeprom
=
EEPROM
(
dev
)
finally
:
dev
.
close
()
eeprom_value
=
eeprom
.
get_values
()
product_string
=
eeprom_value
[
'product_string'
]
print
"Product string:
\"
%
s
\"
"
%
product_string
calibr_string_list
=
product_string
.
split
(
' '
)
if
len
(
calibr_string_list
)
!=
3
:
raise
Exception
(
'Product string has the wrong format.'
)
calibr_data
=
{}
for
i
in
range
(
len
(
calibr_string_list
)):
pattern
=
r'\b[0-9]\.[0-9]{8}\b'
if
re
.
search
(
pattern
,
calibr_string_list
[
i
]):
calibr_data
[
CALIBR_RANGES
[
i
]]
=
calibr_string_list
[
i
]
else
:
raise
Exception
(
'Product string has the wrong format.'
)
return
calibr_data
def
set_calibr_data
(
dev
,
data
):
dev
.
open
()
product_string
=
data
[
CALIBR_RANGES
[
0
]]
+
' '
+
data
[
CALIBR_RANGES
[
1
]]
+
' '
+
data
[
CALIBR_RANGES
[
2
]]
print
"New product string value:
\"
%
s
\"
"
%
product_string
try
:
dev
.
set_product_string
(
product_string
)
print
"Calibration data written to cp210x EEPROM."
finally
:
dev
.
close
()
def
input_calibr_data
(
range_str
):
while
(
True
):
calibr_string
=
raw_input
(
"Enter calibration voltage for "
+
range_str
+
" range (in volts, max 10 char): "
)
pattern
=
r'\b[0-9]\.[0-9]{8}\b'
if
re
.
search
(
pattern
,
calibr_string
):
return
calibr_string
else
:
print
"[ERROR] Calibration string format is wrong, MUST be '
\\
b[0-9]
\
.[0-9]{8}
\\
b' (e.g 0.00000000).
\n
"
continue
def
main
():
usb
.
init
()
dev
=
find_device
(
BOX_USB_VID
,
BOX_USB_PID
)
# Ask user to input calibration data
calibr_data
=
{}
for
range_str
in
CALIBR_RANGES
:
calibr_data
[
range_str
]
=
input_calibr_data
(
range_str
)
# Write calibration data to cp210x EEPROM
set_calibr_data
(
dev
,
calibr_data
)
print
"Readback calibration data from cp210x EEPROM."
calibr_data
=
get_calibr_data
(
dev
)
for
range_str
,
value
in
calibr_data
.
iteritems
():
print
"
%5
s range calibration voltage is:
%
s V"
%
(
range_str
,
value
)
if
__name__
==
'__main__'
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment