rpisoc.py

This is the highest level module import for the RPiSoC API, which the user should import into their scripts for full use of it. It is used to keep records of some globally used variables between all classes.

It is also used to send data to, and receive data from the RPiSoC using a user chosen communication protocol.

At the beginning of each script, insert the following two lines:

from rpisoc import * #This will handle dependencies
RPiSoC('SPI') #for SPI
#or RPiSoC('I2C')
#or RPiSoC('USB') or RPiSoC ('SERIAL') for USB UART

RPiSoC parent class

class rpisoc.RPiSoC
Class:

This class is used to define the register locations of each component on the RPiSoC, it defines which communication protocol will be utilized by the API, and it collects compiled data from the RPiSoC. The class is not to be assigned to an object; it only needs to be called so that addresses can be defined, compiled data on the RPiSoC can be verified, and so that the communication protocol can be initialized.

Optionally, one can construct the object with a DEBUG parameter, as described in the __new__() description, which will print diagnostic information to the terminal.

static __new__(protocol, baud=9600, DEBUG=False)
Method __new__
Description
  • Constructs the RPiSoC object
  • It will decide the communication protocol to be utilized by the API and it will attempt to learn what components are available on the RPiSoC
Parameters
  • string protocol
Optional Parameters
  • int baud
  • bool DEBUG
protocol USB, SPI, or I2C
baud baud rate for use use with the SERIAL protocol. Any standard baud rate supported
DEBUG
  • Defaults to False. Changing it to True will print debugging data to the terminal
  • This data includes, at least, what information is found on the rpisoc when __new__ is called
  • This can be used to identify errors in your RPiSoC firmware changes or to verify connection with the RPiSoC
Returns No return

USB communication

class rpisoc.SERIAL(baudr=9600)
Class:Provides a serial (USB UART) interface for communicating with the RPiSoC
Note:pyserial 2.7 must be available to use this class
Method __init__
Description
  • Constructs and initializes a SERIAL object
  • Searches through available serial ports to find the RPiSoCs vendor and product ID, then opens the port if found
  • If the RPiSoC is not found RPiSoC will invoke the reconnect() method until it is found
Optional Parameters int baudr
baudr
  • baud rate for serial communication
  • Supports all standard baud rates
Returns No return
PrepareData(dat)
Method PrepareData
Description Makes sure that the data being sent is correctly formatted, so that the RPiSoC is able to more easily decode the received words
Parameters tuple dat
dat
  • A tuple of either length 2 or 3, depending on the message
  • The size of the first two items of the tuple must be less than or equal to one byte each, and they correspond to a register address and command
  • The size of the third item in the tuple, if applicable, must be less than or equal to two bytes, and this is any data to be written
Returns list dat_l
dat_l
  • A list of length 4, prepared for serial transfer
  • The list is constructed as [dat[0], dat[1], dat[2]__LOW_BYTE, dat[2]__HIGH_BYTE]
cleanup()
Method cleanup
Description Forces a software reset on the RPiSoC and then closes and cleans up the serial port
Parameters None
Returns No return
disconnect()
Method disconnect
Description Forces a clean disconnection from the RPiSoC
Parameters None
Returns No return
isConnected()
Method isConnected
Description Checks to see if the serial port is open, and that it is connected to an RPiSoC
Parameters None
Returns bool status*
status
  • True if there exists a valid connection to the RPiSoC
  • False if the port is not functional, or the RPiSoC is not found
receiveData(vals, delay=None)
Method receiveData
Description Called when a returned value is desired from the RPiSoC. It will send a command, and then wait for a response
Parameters tuple vals
vals A tuple which will be sent to PrepareData() to be restructured into a list of length 4, and then sent to the RPiSoC over serial
Returns int dat
dat
  • The data packet received from the PSoC, which has been unpacked and reformatted
  • If the received packet is sufficiently large (>24 bits), it will assume the number overflowed in the negative direction, and is a negative number
Raises
reconnect()
Method reconnect
Description Used to reestablish a connection to the RPiSoC after it has been lost
Parameters None
Returns bool status
status
  • True if the reconnection was successful
  • False if the reconnection failed
sendData(vals)
Method sendData
Description Sends data to the RPiSoC, without waiting for a return
Parameters tuple vals
val A tuple which will be sent to PrepareData() to be restructured into a list of length 4, and then sent to the RPiSoC over serial
Returns No return
Raises

SPI communication

class rpisoc.SPI
Class:This class defines usage of the SPI bus for transfer between the RPi and the PSoC. __init__ is called as soon as an RPiSoC object is defined, and it opens and configures the SPI bus.
Note:The spidev libraries with its python wrapper must be installed to use this class
Method __init__
Description Constructs and initializes an SPI object
Parameters None
Returns No return
PrepareData(dat)
Method PrepareData
Description Makes sure that the data being sent is correctly formatted, so that the RPiSoC is able to more easily decode the received words
Parameters tuple dat
dat
  • A tuple of either length 2 or 3, depending on the message
  • The size of the first two items of the tuple must be less than or equal to one byte each, and they correspond to a register address and command
  • The size of the third item in the tuple, if applicable, must be less than or equal to two bytes, and this is any data to be written
Returns list dat_l
dat_l
  • A list of length 4, prepared for SPI transfer
  • The list is constructed as [dat[0], dat[1], dat[2]__LOW_BYTE, dat[2]__HIGH_BYTE]
cleanup()
Method cleanup
Description Forces a software reset on the RPiSoC and then closes and cleans up the SPI bus
Parameters None
Returns No return
receiveData(vals, delay=0.01)
Method receiveData
Description Called when the RPi desires a returned value from the RPiSoC. It will send a command, and then wait for a response
Parameters
  • tuple vals
Optional Parameters
  • float delay
vals A tuple which will be sent to PrepareData() to be restructured into a list of length 4, and then sent to the RPiSoC over SPI
delay
  • The amount of time that the Python code will wait after sending a request, and before expecting a response
  • This is to be used when one is requesting a complex operation, and more time is needed by the RPiSoC to generate a result
Returns int dat
dat
  • The data packet received from the PSoC, which has been unpacked and reformatted
  • If the received packet is sufficiently large (>24 bits), it will assume the number overflowed in the negative direction, and is a negative number
sendData(vals)
Method sendData
Description Sends data to the RPiSoC, without waiting for a return
Parameters tuple vals
val A tuple which will be sent to PrepareData() to be restructured into a list of length 4, and then sent to the RPiSoC over SPI
Returns No return

I2C communication

class rpisoc.I2C(bus_location=1, f_address=8)
Class:This class defines usage of the I2C bus for transfer between the RPi and the PSoC. __init__ is called as soon as an RPiSoC object is defined, and it opens and configures the bus.
Note:i2c must be configured and the smbus libraries must be installed to call this class
Method __init__
Description Constructs and initializes an I2C object
Optional Parameters
  • int bus_location
  • int f_address
bus_location
  • Location of the smbus on the pi, this should be 1, but confirm in the terminal with i2cdetect -y 1 and notice an address, 08, shown
  • If this command does not show the 08 address, try i2cdetect -y 0
  • If neither terminal command shows the correct bus location, be sure that i2c is configured correctly
bus_location
  • The i2c address on the RPiSoC
  • This address is 0x08 by default
  • If this is changed in your RPiSoC firmware, the change must be reflected here
Returns No return
PrepareData(dat)
Method PrepareData
Description Makes sure that the data being sent is correctly formatted, so that the RPiSoC is able to more easily decode the received words
Parameters tuple dat
dat
  • A tuple of either length 2 or 3, depending on the message
  • The size of the first two items of the tuple must be less than or equal to one byte each, and they correspond to a register address and command
  • The size of the third item in the tuple, if applicable, must be less than or equal to two bytes, and this is any data to be written
Returns list dat_l
dat_l
  • A list of length 4, prepared for I2C transfer
  • The list is constructed as [dat[0], dat[1], dat[2]__LOW_BYTE, dat[2]__HIGH_BYTE]
cleanup()
Method cleanup
Description Forces a software reset on the RPiSoC and then closes and cleans up the I2C bus
Parameters None
Returns No return
receiveData(vals, delay=0.05)
Method receiveData
Description Called when the RPi desires a returned value from the RPiSoC. It will send a command, and then wait for a response
Parameters
  • tuple vals
Optional Parameters
  • float delay
vals A tuple which will be sent to PrepareData() to be restructured into a list of length 4, and then sent to the RPiSoC over I2C
delay
  • The amount of time that the Python code will wait after sending a request, and before expecting a response
  • This is to be used when one is requesting a complex operation, and more time is needed by the RPiSoC to generate a result
Returns int dat
dat
  • The data packet received from the PSoC, which has been unpacked and reformatted
  • If the received packet is sufficiently large (>24 bits), it will assume the number overflowed in the negative direction, and is a negative number
sendData(vals)
Method sendData
Description Sends data to the RPiSoC, without waiting for a return
Parameters tuple vals
val A tuple which will be sent to PrepareData() to be restructured into a list of length 4, and then sent to the RPiSoC over I2C
Returns No return

Exceptions

class rpisoc.LostConnection
Exception LostConnection
Description Raised when connection to the RPiSoC is lost
class rpisoc.ClosedPortException
Exception ClosedPortException
Description Raised when a write or read attempt is being made on a closed port

Table Of Contents

Previous topic

API modules

Next topic

digital.py

This Page