sys.stderr.write("Could not import the PySerial module. Please install it first.\n")
sys.exit(1)
+try:
+ input = raw_input
+except:
+ pass
+
REGISTER_NAMES = {
0x0: 'channel 0 offset',
0x1: 'channel 1 offset',
parser.add_argument('--write-eeprom', type=parse_uint, metavar=('ADDR','VALUE'), nargs=2, help='write uint16 value to address in eeprom; first arg: ADDR, second: VALUE')
parser.add_argument('--period', type=float, help='Set conversion period in ms')
parser.add_argument('--average', type=parse_uint, help='Number of temperature values to average over (1 to 16, reduces the output frequency).')
+ parser.add_argument('--calibrate', action='store_true', help='Run the calibration procedure for all channels')
parser.add_argument('--initialize', action='store_true', help='Initialize a new board with sensible default EEPROM values. Use with caution.')
parser.add_argument('--reload-configuration', '-r', action='store_true', help='Reload the configuration of the board')
args = parser.parse_args()
ser = serial.Serial(args.device, baudrate=38400, timeout=0.1)
+ ser.read(1024) # clear input buffers when opening device...
- def send(cmd):
- print('Sending command:', cmd.strip())
+ def send(cmd, verbose=True):
+ if verbose: print('Sending command:', cmd.strip())
ser.write(cmd.encode('ascii'))
def write_register(board_id, address, value):
print('Writing 0x{2:04X} = {2:d} to address 0x{1:02X} of board 0x{0:01X}'.format(board_id, address, value))
send('WE{:1X}0{:02X}{:04X}\n'.format(board_id, address, value))
- def read_eeprom(board_id, address):
- send('WG{:1X}0{:02X}0000\n'.format(board_id, address))
+ def read_eeprom(board_id, address, verbose=True):
+ ser.read(1024) # clear input buffers before sending new command...
+ send('WG{:1X}0{:02X}0000\n'.format(board_id, address), verbose=verbose)
start = time.time()
- print('---')
+ if verbose: print('---')
answer = False
- while (time.time() - start) < 0.3:
+ while (time.time() - start) < 0.99:
recv = ser.readline()
if recv:
+ if verbose: print('Received answer:', recv)
recv = recv.strip(b'\x00').decode('ascii').strip()
if not recv.startswith('AG'): continue
- answer = True
- print('Received answer:', recv)
addr = int(recv[4:6], 16)
val = int(recv[6:10], 16)
- print('Value stored at address 0x{:02X}: 0x{:04X}'.format(addr, val))
+ if addr != address:
+ continue
+ if verbose: print('Value stored at address 0x{:02X}: 0x{:04X}'.format(addr, val))
if addr in REGISTER_NAMES:
- print('Register name:', REGISTER_NAMES[addr])
- break
- if not answer: print('No answer received so far. Try again?')
+ if verbose: print('Register name:', REGISTER_NAMES[addr])
+ return val
+ raise TimeoutError('No answer received. Old board without read eeprom support?')
if args.read_eeprom is not None:
- read_eeprom(args.board_id, args.read_eeprom)
+ read_eeprom(args.board_id, args.read_eeprom, verbose=True)
if args.write_eeprom is not None:
write_register(args.board_id, *args.write_eeprom)
print('Do you really want to initialize the board with a new set of EEPROM configuration data?')
print('Your current values will be lost!!! This only makes sense for brand new boards!')
input('Press enter to continue or Ctrl-c to abort.')
- for i in range(8):
- write_register(args.board_id, i, 0x57)
- write_register(args.board_id, 0x8, 0xffff)
- write_register(args.board_id, 0x9, 0xd211)
- write_register(args.board_id, 0xa, 250)
- write_register(args.board_id, 0xb, 0x1)
- write_register(args.board_id, 0xc, 0x1)
+ new_vals = [
+ (0x0, 0x0057), # Channel configuration = 87
+ (0x1, 0x0057),
+ (0x2, 0x0057),
+ (0x3, 0x0057),
+ (0x4, 0x0057),
+ (0x5, 0x0057),
+ (0x6, 0x0057),
+ (0x7, 0x0057),
+ (0x8, 0x0000), # nominal offset
+ (0x9, 0xd260), # gain*current = 53856
+ (0xA, 0x00fa), # sample period 250
+ (0xB, 0x0000), # lcd off
+ (0xC, 0x0000), # averaging over (0x1 + 1 =) 2 samples
+ ]
+ print("Current values of the EEPROM:")
+ for reg_addr, new_reg_val in new_vals:
+ old_reg_val = read_eeprom(args.board_id, reg_addr, verbose=False)
+ print("Address: 0x{:02X} Old Value: 0x{:04X} New Value: 0x{:04X}".format(reg_addr, old_reg_val, new_reg_val))
+ input('Press enter to continue or Ctrl-c to abort.')
+ for reg_addr, reg_val in new_vals:
+ write_register(args.board_id, reg_addr, reg_val)
+
print("Done initializing the EEPROM")
+
if not args.reload_configuration:
print("You didn't choose to reload the configuration. The new values will only be used once reloaded or power cycled.")
print("Consider using the -r option.")
else:
time.sleep(0.1)
+ if args.calibrate:
+ print("OK, starting the calibration.")
+ for channel in range(8):
+ print("Channel", channel)
+ inp = input("Enter [c] to continue, anything else will skip this channel.")
+ if inp != 'c':
+ print("Skipping channel", channel)
+ continue
+ print("Connect the 100Ohm reference!")
+ print("Connect the 120Ohm reference!")
+ print("Done with channel", channel)
+
if args.reload_configuration:
send('WR{:1X}0000000\n'.format(args.board_id))