20from .header
import ConfigMainHeader, DataI2CHeader, ConfigSubHeader
21from .packet
import ConfigPacket, DataI2CPacket
25 """ This class is the API for the I2C interfaces of the uC
26 It is used to configure, send
and recive data
from the I2C interfaces
29 """ Constructor for the I2C interface
30 @param api_object: the parent API object
31 @param interface_id: the id of the I2C interface
59 self.
__api = api_object
62 self.
__header = [ConfigMainHeader.IN_CONF_I2C0, DataI2CHeader.IN_I2C0, DataI2CHeader.OUT_I2C0]
63 elif interface_id == 1:
64 self.
__header = [ConfigMainHeader.IN_CONF_I2C1, DataI2CHeader.IN_I2C1, DataI2CHeader.OUT_I2C1]
65 elif interface_id == 2:
66 self.
__header = [ConfigMainHeader.IN_CONF_I2C2, DataI2CHeader.IN_I2C2, DataI2CHeader.OUT_I2C2]
68 logging.error(
"I2C only up to 3 interfaces are supported at the moment")
71 """ Returns the headers this I2C interface object is responsible for
72 @return: the headers this I2C interface object
is responsible
for
77 """ Returns the status of the I2C interface, and the time of when this status was processed by the uC
78 @return: (state_str,timestamp) where state_str
is the status of the I2C interface,
and timestamp
is the time of when this status was processed by the uC
81 state_str = ("active" if self.
__status == 2
else (
"activation pending" if self.
__status == 1
else (
"not active" if self.
__status == 0
else "error" )))
85 """ Returns the protocol data word width of the I2C interface, and the time of when this status was processed by the uC
86 @return: (number_of_bytes,timestamp) where number_of_bytes
is the protocol data word width of the I2C interface,
and timestamp
is the time of when this status was processed by the uC
92 """ Returns the speed of the I2C interface, and the time of when this status was processed by the uC
93 @return: (speed,timestamp) where speed
is the speed of the I2C interface,
and timestamp
is the time of when this status was processed by the uC
99 """ Returns the byte order of the I2C interface, and the time of when this status was processed by the uC
100 @return: (order,timestamp) where order
is the byte order of the I2C interface,
and timestamp
is the time of when this status was processed by the uC
106 """ Returns the data recived from the chip, and the time it was processed by the uC
108 will retun 2 lists: one with the word recoded
and one
with the time when it was recorded, linked by index
110 @return: ([data_from_chip], [data_from_chip_times]) where data_from_chip
is the data recived
from the chip,
and data_from_chip_times
is the time it was processed by the uC
116 """ Returns the data send to the chip, and the time it was processed by the uC
118 data_to_chip will retun the data send by the uC to the device under test (DUT)
120 will retun 2 lists: one with the word send
and one
with the exact time when it was send, linked by index
122 the time might differ slightly
from the time you sheduled the send word,
123 as it
is the time when it was send out
and the uC can only send one word at a time
125 @return: ([data_to_chip], [data_to_chip_times]) where data_to_chip
is the data send to the chip,
and data_to_chip_times
is the time it was processed by the uC
131 """ Returns the data recived from the chip, and the time it was processed by the uC, and clears the data
133 will retun 2 lists: one with the word recoded
and one
with the time when it was recorded, linked by index
135 @return: ([data_from_chip], [data_from_chip_times]) where data_from_chip
is the data recived
from the chip,
and data_from_chip_times
is the time it was processed by the uC
145 """ Returns the data send to the chip, and the time it was processed by the uC, and clears the data
146 data_to_chip will retun the data send by the uC to the device under test (DUT)
148 will retun 2 lists: one with the word send
and one
with the exact time when it was send, linked by index
150 the time might differ slightly
from the time you sheduled the send word,
151 as it
is the time when it was send out
and the uC can only send one word at a time
153 @return: ([data_to_chip], [data_to_chip_times]) where data_to_chip
is the data send to the chip,
and data_to_chip_times
is the time it was processed by the uC
163 """ Returns the errors and unhandled packets reported by the uC for this interface
164 @return: [errors] where errors
is the errors
and unhandled packets reported by the uC
for this interface
171 state_str = (
"active" if self.
__status == 2
else (
"activation pending" if self.
__status == 1
else (
"not active" if self.
__status == 0
else "error" )))
173 "\nHeader: " + str(self.
__header) + \
180 "\nERRORS: "+str(self.
__errors) +
"\n"
183 """ Processes a packet from the uC, and updates the status and stores the data of/in the I2C interface object
184 @param packet: the packet to process
186 if packet.header()
in self.
__header:
188 if packet.header() == self.
__header[0]:
190 if packet.config_header() == ConfigSubHeader.CONF_ACTIVE:
195 elif packet.config_header() == ConfigSubHeader.CONF_BYTE_ORDER:
196 self.
__order =
"MSBFIRST" if (packet.value() > 0)
else "LSBFIRST"
200 elif packet.config_header() == ConfigSubHeader.CONF_WIDTH:
205 elif packet.config_header() == ConfigSubHeader.CONF_SPEED_CLASS:
206 if packet.value() == 4:
208 elif packet.value() == 0:
210 elif packet.value() == 1:
212 elif packet.value() == 2:
214 elif packet.value() == 3:
221 elif packet.header() == self.
__header[1]:
222 self.
__data_to_chip.append((packet.read(),packet.device_address(),packet.register_address(),packet.value()))
225 elif packet.header() == self.
__header[2]:
226 self.
__data_from_chip.append((packet.read(),packet.device_address(),packet.register_address(),packet.value()))
233 def activate(self, speed=400000, order="LSBFIRST", number_of_bytes=1, time=0):
234 """ Activat the I2C interface and configure it with the given speed, byte order and number of bytes
235 @param speed: the speed of the I2C interface possible values are 10000, 100000, 400000, 1000000, 3400000
236 @param order: the byte order of the I2C interface possible values are
"LSBFIRST" or "MSBFIRST"
237 @param number_of_bytes: the protocol data word width of the I2C interface possible values are 1
or 2
238 @param time: the time when the activation should be processed by the uC (0 means
as soon
as possible)
240 self.__api.update_state()
243 logging.warning(
"I2C interface "+str(self.
__header[0])+
" is already activated or waiting activation, doing nothing")
246 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_BYTE_ORDER, value= (1
if (order ==
"MSBFIRST")
else 0),time = time))
249 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_SPEED_CLASS, value=4,time = time))
250 elif speed == 100000:
251 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_SPEED_CLASS, value=0,time = time))
252 elif speed == 400000:
253 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_SPEED_CLASS, value=1,time = time))
254 elif speed == 1000000:
255 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_SPEED_CLASS, value=2,time = time))
256 elif speed == 3400000:
257 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_SPEED_CLASS, value=3,time = time))
259 logging.warning(
"I2C only supports 10000,100000,400000,1000000,3400000 Hz, defaulting to 100000")
261 if number_of_bytes > 2:
262 logging.warning(
"I2C ony supports one or two bytes, defaulting to 1")
264 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_WIDTH, value=number_of_bytes,time = time))
269 def send_write(self,device_address, register_address, word, time = 0):
270 """ Send a write request on the I2C interface
271 @param device_address: the address of the device to write to (7bit)
272 @param register_address: the address of the register to write to (8bit)
273 @param word: the word to write (8
or 16 bit depending on the number of bytes configured)
274 @param time: the time when the write request should be processed by the uC (0 means
as soon
as possible)
277 self.
__api.send_packet(
DataI2CPacket(self.
__header[1], device_address=device_address, register_address=register_address,read=0,value=word,time=time))
281 """ Send a read request on the I2C interface
282 @param device_address: the address of the device to read
from (7bit)
283 @param register_address: the address of the register to read
from (8bit)
285 @param time: the time when the read request should be processed by the uC (0 means
as soon
as possible)
288 self.
__api.send_packet(
DataI2CPacket(self.
__header[1], device_address=device_address, register_address=register_address,read=1,value=word,time=time))
291 """ update the data repersentation of the API object
293 self.__api.update_state()
This class is the API for the I2C interfaces of the uC It is used to configure, send and recive data ...
def status(self)
Returns the status of the I2C interface, and the time of when this status was processed by the uC.
def process_packet(self, packet)
Processes a packet from the uC, and updates the status and stores the data of/in the I2C interface ob...
def data_to_chip_and_clear(self)
Returns the data send to the chip, and the time it was processed by the uC, and clears the data data_...
def data_from_chip_and_clear(self)
Returns the data recived from the chip, and the time it was processed by the uC, and clears the data.
def activate(self, speed=400000, order="LSBFIRST", number_of_bytes=1, time=0)
Activat the I2C interface and configure it with the given speed, byte order and number of bytes.
def number_of_bytes(self)
Returns the protocol data word width of the I2C interface, and the time of when this status was proce...
def update(self)
update the data repersentation of the API object
def __init__(self, api_object, interface_id)
Constructor for the I2C interface.
__number_of_bytes_timestamp
def byte_order(self)
Returns the byte order of the I2C interface, and the time of when this status was processed by the uC...
def speed(self)
Returns the speed of the I2C interface, and the time of when this status was processed by the uC.
def send_write(self, device_address, register_address, word, time=0)
Send a write request on the I2C interface.
def errors(self)
Returns the errors and unhandled packets reported by the uC for this interface.
def data_from_chip(self)
Returns the data recived from the chip, and the time it was processed by the uC.
def data_to_chip(self)
Returns the data send to the chip, and the time it was processed by the uC.
def send_read_request(self, device_address, register_address, word, time=0)
Send a read request on the I2C interface.
The ConfigPacket is used to cumunicate configuration instructions with the uC all availible instructi...
The DataI2CPacket is used for I2C communication and all types which need 3x 8bit values instread of 1...