Commit cd42db44 authored by Paolo Baesso's avatar Paolo Baesso

Added class for PCA9548ADW I2C multiplexer

parent 98f792f7
......@@ -13,7 +13,6 @@ from si5345 import si5345 # Library for clock chip
from AD5665R import AD5665R # Library for DAC
from PCA9539PW import PCA9539PW # Library for serial line expander
from I2CDISP import CFA632 #Library for display
from I2CDISP import LCD09052 #Library for display
from TLU_powermodule import PWRLED
class TLU:
......@@ -58,6 +57,7 @@ class TLU:
enableCore= True #Only need to run this once, after power-up
self.enableCore()
self.eepromAX3read()
# Instantiate clock chip and configure it (if necessary)
#self.zeClock=si5345(self.TLU_I2C, 0x68)
......@@ -105,8 +105,7 @@ class TLU:
self.IC7.setOutputs(1, 0xB0)# If output, set to XX
#Instantiate Display
#self.DISP=CFA632(self.TLU_I2C, 0x2A) #
self.DISP=LCD09052(self.TLU_I2C, 0x3A) #
self.DISP=CFA632(self.TLU_I2C, 0x2A) #
#Instantiate Power/Led Module
dac_addr_module= int(parsed_cfg.get(section_name, "I2C_DACModule_Addr"), 16)
......@@ -118,31 +117,26 @@ class TLU:
self.pwdled.setVch(1, 0.9, True)
self.pwdled.setVch(2, 0.8, True)
self.pwdled.setVch(3, 0.1, True)
self.pwdled.setIndicatorRGB(1, [0, 0, 1])
self.pwdled.setIndicatorRGB(2, [0, 0, 1])
self.pwdled.setIndicatorRGB(3, [0, 0, 1])
self.pwdled.setIndicatorRGB(4, [0, 0, 1])
self.pwdled.setIndicatorRGB(5, [0, 0, 1])
self.pwdled.setIndicatorRGB(6, [0, 0, 1])
self.pwdled.setIndicatorRGB(7, [0, 0, 1])
self.pwdled.setIndicatorRGB(8, [0, 0, 1])
self.pwdled.setIndicatorRGB(9, [0, 0, 1])
self.pwdled.setIndicatorRGB(10, [0, 0, 1])
self.pwdled.setIndicatorRGB(11, [0, 0, 1])
#self.pwdled.setIndicatorRGB(1, [0, 0, 1])
#self.pwdled.setIndicatorRGB(2, [0, 0, 1])
#self.pwdled.setIndicatorRGB(3, [0, 0, 1])
#self.pwdled.setIndicatorRGB(4, [0, 0, 1])
#self.pwdled.setIndicatorRGB(5, [0, 0, 1])
#self.pwdled.setIndicatorRGB(6, [0, 0, 1])
#self.pwdled.setIndicatorRGB(7, [0, 0, 1])
#self.pwdled.setIndicatorRGB(8, [0, 0, 1])
#self.pwdled.setIndicatorRGB(9, [0, 0, 1])
#self.pwdled.setIndicatorRGB(10, [0, 0, 1])
#self.pwdled.setIndicatorRGB(11, [0, 0, 1])
self.pwdled.allGreen()
#time.sleep(0.5)
#self.pwdled.allRed()
#time.sleep(0.5)
self.pwdled.allBlue()
#time.sleep(0.5)
self.pwdled.allBlack()
#self.pwdled.allWhite()
#time.sleep(0.5)
#self.pwdled.kitt()
self.pwdled.kitt()
self.pwdled.allBlack()
##################################################################################################################################
##################################################################################################################################
def DUTOutputs_old(self, dutN, enable=False, verbose=False):
......@@ -235,6 +229,23 @@ class TLU:
self.IC7.setOutputs(bank, newStatus)
return newStatus
def eepromAX3read(self):
mystop=True
print " Reading AX3 eeprom (not working 100% yet):"
myslave= 0x64
self.TLU_I2C.write(myslave, [0x02, 0x00])
nwords= 5
res= self.TLU_I2C.read( myslave, nwords)
print "\tAX3 awake: ", res
mystop=True
nwords= 7
#mycmd= [0x03, 0x07, 0x02, 0x00, 0x00, 0x00, 0x1e, 0x2d]#conf 0?
mycmd= [0x03, 0x07, 0x02, 0x00, 0x01, 0x00, 0x17, 0xad]#conf 1 <<< seems to reply with correct error code (0)
#mycmd= [0x03, 0x07, 0x02, 0x02, 0x00, 0x00, 0x1d, 0xa8]#data 0?
self.TLU_I2C.write(myslave, mycmd, mystop)
res= self.TLU_I2C.read( myslave, nwords)
print "\tAX3 EEPROM: ", res
def enableClkLEMO(self, enable= False, verbose= False):
## Enable or disable the output clock to the differential LEMO output
bank=1
......
# -*- coding: utf-8 -*-
import uhal
from I2CuHal import I2CCore
import StringIO
import numpy as np
class ATSHA204A:
#Class for Atmel ATSHA204A eeprom
def __init__(self, i2c, slaveaddr= 0x64):
self.i2c = i2c
self.slaveaddr = slaveaddr
#Slot size, in bytes.
self.SLOT_SIZE_BYTES = 32;
#Word size, in bytes. This is the base unit for all reads and writes.
self.WORD_SIZE_BYTES = 4;
#Maximum word offset per slot
self.MAX_WORD_OFFSET = 7;
#Size of the configuration zone, in bytes
self.CONFIGURATION_ZONE_SIZE_BYTES = 88;
#Number of slots in the configuration zone
self.CONFIGURATION_ZONE_SIZE_SLOTS = 3;
#Slot 3 in the configuration zone is only 24 bytes rather than 32, so the max word offset is limited to 5.
self.CONFIGURATION_ZONE_SLOT_2_MAX_WORD_OFFSET = 5;
#Size of the OTP zone, in bytes
self.OTP_ZONE_SIZE_BYTES = 64;
#Number of slots in the OTP zone
self.OTP_ZONE_SIZE_SLOTS = 2;
#Size of the data zone, in bytes
self.DATA_ZONE_SIZE_BYTES = 512;
#Number of slots in the data zone
self.DATA_ZONE_SIZE_SLOTS = 16;
#The data slot used for module configuration data
self.DATA_ZONE_SLOT_MODULE_CONFIGURATION = 0;
#Byte index of the OTP mode byte within its configuration word.
self.OTP_MODE_WORD_BYTE_INDEX = 2;
#-------------------------------------------------------------------------------------------------
# Command packets and I/O
#-------------------------------------------------------------------------------------------------
#Command execution status response block size
self.STATUS_RESPONSE_BLOCK_SIZE_BYTES = 4;
#Byte index of count in response block
self.STATUS_RESPONSE_COUNT_BYTE_INDEX = 0;
#Byte index of status code in response block
self.STATUS_RESPONSE_STATUS_BYTE_INDEX = 1;
#Checksum size
self.CHECKSUM_LENGTH_BYTES = 2;
#Index of the count byte in a command packet
self.COMMAND_PACKET_COUNT_BYTE_INDEX = 0;
#Size of count in a command packet
self.COMMAND_PACKET_COUNT_SIZE_BYTES = 1;
#Index of the opcode byte in a command packet
self.COMMAND_PACKET_OPCODE_BYTE_INDEX = 1;
#Size of the opcode byte in a command packet
self.COMMAND_PACKET_OPCODE_LENGTH_BYTES = 1;
#Index of param 1 in a command packet
self.COMMAND_PACKET_PARAM1_BYTE_INDEX = 2;
#Size of param 1 in a command packet
self.COMMAND_PACKET_PARAM1_SIZE_BYTES = 1;
#Index of param 2 in a command packet
self.COMMAND_PACKET_PARAM2_BYTE_INDEX = 3;
#Size of param 2 in a command packet
self.COMMAND_PACKET_PARAM2_SIZE_BYTES = 2;
def _CalculateCrc(self, pData, dataLengthBytes):
# Calculate a CRC-16 used when communicating with the device. Code taken from Atmel's library.
#The Atmel documentation only specifies that the CRC algorithm used on the ATSHA204A is CRC-16 with polynomial
#0x8005; compared to a standard CRC-16, however, the used algorithm doesn't use remainder reflection.
#@param pData The data to calculate the CRC for
#@param dataLengthBytes The number of bytes to process
#@return The CRC
polynomial = 0x8005
crcRegister = 0
if not pData:
print "_CalculateCrc: No data to process"
return 0
for counter in range(0, dataLengthBytes):
shiftRegister= 0x01
for iShift in range(0, 8):
if (pData[counter] & shiftRegister) :
dataBit= 1
else:
dataBit=0
crcBit= ((crcRegister) >> 15)
crcRegister <<= 1
crcRegister= crcRegister & 0xffff
#print shiftRegister, "\t", dataBit, "\t", crcBit, "\t", crcRegister
shiftRegister= shiftRegister << 1
if (dataBit != crcBit):
#print "poly"
crcRegister ^= polynomial;
return crcRegister
def _wake(self, verifyDeviceIsAtmelAtsha204a, debug):
dummyWriteData = 0x00
mystop=True
self.i2c.write( self.slaveaddr, [dummyWriteData], mystop)
if (verifyDeviceIsAtmelAtsha204a):
expectedStatusBlock= [ 0x04, 0x11, 0x33, 0x43 ];
nwords= 4
res= self.i2c.read( self.slaveaddr, nwords)
if (res != expectedStatusBlock):
print "Attempt to awake Atmel ATSHA204A failed"
print res
def _GetCommandPacketSize(self, additionalDataLengthBytes):
packetSizeBytes = self.COMMAND_PACKET_COUNT_SIZE_BYTES + self.COMMAND_PACKET_OPCODE_LENGTH_BYTES \
+ self.COMMAND_PACKET_PARAM1_SIZE_BYTES + self.COMMAND_PACKET_PARAM2_SIZE_BYTES \
+ additionalDataLengthBytes + self.CHECKSUM_LENGTH_BYTES;
return packetSizeBytes
# -*- coding: utf-8 -*-
import uhal
from I2CuHal import I2CCore
import StringIO
class PCA9539PW:
#Class to configure the I2C multiplexer
def __init__(self, i2c, slaveaddr=0x74):
self.i2c = i2c
self.slaveaddr = slaveaddr
def setActiveChannel(self, channel, verbose=False):
#Basic functionality to activate one channel
# In principle multiple channels can be active at the same time but for now
# we are not implementing this option
if (channel < 0) | (channel > 7):
print "PCA9539PW - ERROR: channel number should be in range [0:7]"
return
mystop=True
cmd= [0x1 << channel]
self.i2c.write( self.slaveaddr, cmd, mystop)
def getChannelStatus(self, verbose=False):
#Basic functionality to read the status of the control register and determine
# which channel is currently enabled.
mystop=False
self.i2c.write( self.slaveaddr, [0xFF], mystop)
res= self.i2c.read( self.slaveaddr, 1)
return res
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment