19from .header
import ConfigMainHeader, Data32bitHeader, ConfigSubHeader
20from .packet
import ConfigPacket, Data32bitPacket
24 """ The interface SPI creates and object though which the interface can be accessed
26 the pins on the uC are fixed and depend on the specific uC model, please consult the pin out diagram of you uC
28 to read out the state use the object funtions to get the state returned, this
is done so an state ubdate can be triggered before returning the values
31 """__init__ creates a not activate interface object
33 :param api_object: the parent manageing the conection to the uC
34 :type api_object: uC_api
35 :param interface_id: the identifier of the interface, so the interface number in the hardware it
is usulay depiced after the port name like 0 -> SCK, COPI, CIPO; 1-> SCK1, COPI1, CIPO1
and so on;
36 :type interface_id: int
53 self.
__api = api_object
56 self.
__header = [ConfigMainHeader.IN_CONF_SPI0, Data32bitHeader.IN_SPI0, Data32bitHeader.OUT_SPI0]
57 elif interface_id == 1:
58 self.
__header = [ConfigMainHeader.IN_CONF_SPI1, Data32bitHeader.IN_SPI1, Data32bitHeader.OUT_SPI1]
59 elif interface_id == 2:
60 self.
__header = [ConfigMainHeader.IN_CONF_SPI2, Data32bitHeader.IN_SPI2, Data32bitHeader.OUT_SPI2]
62 logging.error(
"SPI only up to 3 interfaces are supported at the moment")
65 """header returns the packet headers associated whith this interface
67 :return: packet headers of this interface
68 :rtype: Header (IntEnum)
73 """status returns the state of this interface,
76 - active - everything is working fine
77 - activation pending - the uC has
not acknolaged the activation yet after the request to activate the interface
78 -
not active - the interface has
not been configured
and activate
79 - error - there was an error during activation
or during use, please consult the errors using the errors function
81 :
return: the state of the interface
and the timestamp
in us
85 state_str = ("active" if self.
__status == 2
else (
"activation pending" if self.
__status == 1
else (
"not active" if self.
__status == 0
else "error" )))
89 """interface_mode the currently active mode, consult spi arduino docs for more information
91 spi has 4 modes which are numbered SPI_MODE0 ...
93 :return: the mode
and the timestamp
in us
as touple
100 """speed the speed class result of the current
102 :return: the frequency
and the timestamp
in us
109 """bit_order the bit order in which the word is send
111 :return: the active bitorder
and the timestamp
in us
112 :rtype: (string, int)
118 """number_of_bytes the width of each send word, it can be 1,2,3 or 4 bytes
120 :return: the wisth of each word
and the timestamp
in us
127 """data_from_chip will retun the data recoded by the uC send from the device under test (DUT)
129 will retun 2 lists: one with the word recoded
and one
with the time when it was recorded, linked by index
131 :
return: the words
from the DUT
and the times of those words
132 :rtype: ([int],[int])
138 """data_to_chip will retun the data send by the uC to the device under test (DUT)
140 will retun 2 lists: one with the word send
and one
with the exact time when it was send, linked by index
142 the time might differ slightly
from the time you sheduled the send word,
143 as it
is the time when it was send out
and the uC can only send one word at a time
145 :
return: the words send to the DUT
and the times of those words
146 :rtype: ([int],[int])
168 """errors all errors corresponding to this interface
170 :return: list of all errors
177 """__str__ will return the current state, properies and data as a string
179 :return: the current state, properies
and data
183 state_str = ("active" if self.
__status == 2
else (
"activation pending" if self.
__status == 1
else (
"not active" if self.
__status == 0
else "error" )))
185 "\nHeader: " + str(self.
__header) + \
193 "\nERRORS: "+str(self.
__errors) +
"\n"
196 """process_packet this function accepts packages fro this interface from the api and updates its internal state
198 :param packet: the packet to be processed
199 :type packet: Packet, or any sub
class
201 if packet.header()
in self.
__header:
202 if packet.header() == self.
__header[0]:
203 if packet.config_header() == ConfigSubHeader.CONF_ACTIVE:
207 elif packet.config_header() == ConfigSubHeader.CONF_BYTE_ORDER:
208 self.
__order = (
"MSBFIRST" if (packet.value()==1)
else "LSBFIRST")
211 elif packet.config_header() == ConfigSubHeader.CONF_SPEED_CLASS:
215 elif packet.config_header() == ConfigSubHeader.CONF_TYPE:
216 self.
__mode = packet.value()
219 elif packet.config_header() == ConfigSubHeader.CONF_WIDTH:
223 elif packet.header() == self.
__header[1] :
227 elif packet.header() == self.
__header[2]:
234 def activate(self, mode="SPI_MODE0", speed_class=0, order="LSBFIRST",number_of_bytes=1,time=0):
235 """activate activates and configures the interface,
236 if the interface
is not availible on the UC it will put its state
in en error state
and prevent further use
238 :param mode: choose between
"SPI_MODE0" "SPI_MODE1" "SPI_MODE2" or "SPI_MODE3" see arduino SPI docs, defaults to
"SPI_MODE0"
239 :type mode: str, optional
240 :param speed_class: 0-8 0:10kHz 1:50kHz 2:100kHz 3:500kHz 4:1MHz 5:2MHz 6:4MHz 7:8MHz 8:12MHz, defaults to 0
241 :type speed_class: int, optional
242 :param order: Choose between
"MSBFIRST" or "LSBFIRST" for order
in which the word
is transmitted, defaults to
"LSBFIRST"
243 :type order: str, optional
244 :param number_of_bytes: 1-4 number of bytes contained
in a word
or better the length of a word, defaults to 1
245 :type number_of_bytes: int, optional
246 :param time: the time
in us after start_experiment when this function should be executed, defaults to 0 (execute instantly)
247 :type time: int, optional
250 logging.warning(
"SPI interface "+str(self.
__header[0])+
" is already activated or waiting activation, doing nothing")
253 value=( 0
if mode ==
"SPI_MODE0" else (1
if mode ==
"SPI_MODE1" else (2
if mode ==
"SPI_MODE2" else 3))),time = time))
254 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_SPEED_CLASS,value=speed_class,time = time))
256 value=(1
if order ==
"MSBFIRST" else 0),time = time))
257 self.
__api.send_packet(
ConfigPacket(header = self.
__header[0], config_header = ConfigSubHeader.CONF_WIDTH,value=number_of_bytes,time = time))
261 def send(self, word, time = 0):
262 """send send a word via this interface
264 :param word: the word to send
266 :param time: the time in us after start_experiment when this word should be send, defaults to 0 (execute instantly)
267 :type time: int, optional
274 """update updates the internal state form the uC
276 self.__api.update_state()
The interface SPI creates and object though which the interface can be accessed.
def bit_order(self)
bit_order the bit order in which the word is send
def status(self)
status returns the state of this interface,
def process_packet(self, packet)
process_packet this function accepts packages fro this interface from the api and updates its interna...
def __str__(self)
str will return the current state, properies and data as a string
def interface_mode(self)
interface_mode the currently active mode, consult spi arduino docs for more information
def data_to_chip_and_clear(self)
def data_from_chip_and_clear(self)
def number_of_bytes()
number_of_bytes the width of each send word, it can be 1,2,3 or 4 bytes
def update(self)
update updates the internal state form the uC
def send(self, word, time=0)
send send a word via this interface
def __init__(self, api_object, interface_id)
init creates a not activate interface object
__number_of_bytes_timestamp
def speed(self)
speed the speed class result of the current
def errors(self)
errors all errors corresponding to this interface
def activate(self, mode="SPI_MODE0", speed_class=0, order="LSBFIRST", number_of_bytes=1, time=0)
activate activates and configures the interface, if the interface is not availible on the UC it will ...
def data_from_chip(self)
data_from_chip will retun the data recoded by the uC send from the device under test (DUT)
def data_to_chip(self)
data_to_chip will retun the data send by the uC to the device under test (DUT)
The ConfigPacket is used to cumunicate configuration instructions with the uC all availible instructi...
The Data32bitPacket is used to send 32bit data instructions to the uC all availible instructions are ...