Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SPI Class Referenceabstract

Private data for an SPI device. More...

#include <SPI.hxx>

Inheritance diagram for SPI:
Node Device FileIO CC32xxSPI Stm32SPI TivaSPI

Public Types

typedef void(* ChipSelectMethod) ()
 Function point for the chip select assert and deassert methods.
 

Public Member Functions

int transfer_with_cs_assert (struct spi_ioc_transfer *msg)
 Method to transmit/receive the data.
 
int transfer_with_cs_assert_polled (struct spi_ioc_transfer *msgs, int num=1)
 Method to transmit/receive the data.
 
int transfer_messages (struct spi_ioc_transfer *msgs, int num)
 Conduct multiple message transfers with one stop at the end.
 
- Public Member Functions inherited from Device
 Device (const char *name)
 Constructor.
 
virtual ~Device ()
 Destructor.
 

Protected Member Functions

 SPI (const char *name, ChipSelectMethod cs_assert, ChipSelectMethod cs_deassert, OSMutex *bus_lock=nullptr)
 Constructor.
 
 ~SPI ()
 Destructor.
 
void bus_lock ()
 Lock the bus shared by many chip selects.
 
void bus_unlock ()
 Unlock the bus shared by many chip selects.
 
virtual int transfer (struct spi_ioc_transfer *msg)=0
 Method to transmit/receive the data.
 
virtual int transfer_polled (struct spi_ioc_transfer *msg)=0
 Method to transmit/receive the data, but always in polled mode.
 
virtual int update_configuration ()=0
 Update the configuration of the bus.
 
int ioctl (File *file, unsigned long int key, unsigned long data) override
 Request an ioctl transaction.
 
- Protected Member Functions inherited from Node
 Node (const char *name)
 Constructor.
 
virtual ~Node ()
 Destructor.
 
virtual void enable ()=0
 This will be called once when reference-count goes from 0 to positive.
 
virtual void disable ()=0
 This will be called when reference count goes from non-zero to 0.
 
int open (File *, const char *, int, int) OVERRIDE
 Open method.
 
int close (File *) OVERRIDE
 Close method.
 
virtual int fstat (File *file, struct stat *stat) override
 Get the status information of a file or device.
 
- Protected Member Functions inherited from FileIO
 FileIO (const char *name)
 Constructor.
 
virtual ~FileIO ()
 Destructor.
 
virtual off_t lseek (File *f, off_t offset, int whence)
 Seek method.
 
virtual int fcntl (File *file, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
virtual bool select (File *file, int mode)
 Device select method.
 

Protected Attributes

ChipSelectMethod csAssert
 function pointer to a method that asserts chip select.
 
ChipSelectMethod csDeassert
 function pointer to a method that deasserts chip select.
 
uint32_t speedHz
 Max default speed in Hz.
 
uint8_t bitsPerWord
 number of bits per word transaction
 
uint8_t mode
 one of four SPI modes
 
bool lsbFirst
 transmit LSB first if true
 
- Protected Attributes inherited from Node
OSMutex lock_
 protects internal structures.
 
mode_t mode_
 File open mode, such as O_NONBLOCK.
 
unsigned int references_
 number of open references
 
- Protected Attributes inherited from FileIO
const char * name
 device name
 

Private Member Functions

ssize_t read (File *file, void *buf, size_t count) override
 Read from a file or device.
 
ssize_t write (File *file, const void *buf, size_t count) override
 Write to a file or device.
 
void flush_buffers () override
 Discards all pending buffers.
 
 SPI ()
 Default constructor.
 
 DISALLOW_COPY_AND_ASSIGN (SPI)
 

Private Attributes

OSMutexbusLock
 Mutual exclusion for the bus among many chip selects.
 

Additional Inherited Members

- Static Public Member Functions inherited from Device
static int open (struct _reent *reent, const char *path, int flags, int mode)
 Open a file or device.
 
static int close (struct _reent *reent, int fd)
 Close a file or device.
 
static int stat (struct _reent *reent, const char *path, struct stat *stat)
 Get the status information of a file or device.
 
static int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout)
 POSIX select().
 
static void select_clear ()
 Clears the current thread's select bits.
 
- Static Public Member Functions inherited from FileIO
static ssize_t read (struct _reent *reent, int fd, void *buf, size_t count)
 Read from a file or device.
 
static ssize_t write (struct _reent *reent, int fd, const void *buf, size_t count)
 Write to a file or device.
 
static _off_t lseek (struct _reent *reent, int fd, _off_t offset, int whence)
 Change the offset index of a file or device.
 
static int fstat (struct _reent *reent, int fd, struct stat *stat)
 Get the status information of a file or device.
 
static int ioctl (int fd, unsigned long int key, unsigned long data)
 Request and ioctl transaction.
 
static int fcntl (int fd, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
static bool is_device (int fd)
 Test if the file descriptor belongs to a device.
 
- Static Protected Member Functions inherited from Device
static void select_insert (SelectInfo *info)
 Add client to list of clients needing woken.
 
static void select_wakeup (SelectInfo *info)
 Wakeup the list of clients needing woken.
 
static void select_wakeup_from_isr (SelectInfo *info, int *woken)
 Wakeup the list of clients needing woken.
 
- Static Protected Member Functions inherited from FileIO
static int fd_alloc (void)
 Allocate a free file descriptor.
 
static void fd_free (int fd)
 Free up a file descriptor.
 
static Filefile_lookup (int fd)
 Looks up a reference to a File corresponding to a given file descriptor.
 
static int fd_lookup (File *file)
 Looks up a file descriptor corresponding to a given File reference.
 
- Static Protected Attributes inherited from FileIO
static const unsigned int numOpenFiles = 20
 
static File files []
 File descriptor pool.
 
static OSMutex mutex
 mutual exclusion for fileio
 

Detailed Description

Private data for an SPI device.

Unlike the Linux model this interface is based on, speed, bits per word, mode, and lsb first will always be the same for both read and write. Changing one of these parameters for read will also reflect the change in write and vice versa.

Definition at line 52 of file SPI.hxx.

Member Typedef Documentation

◆ ChipSelectMethod

typedef void(* SPI::ChipSelectMethod) ()

Function point for the chip select assert and deassert methods.

Definition at line 56 of file SPI.hxx.

Constructor & Destructor Documentation

◆ SPI()

SPI::SPI ( const char *  name,
ChipSelectMethod  cs_assert,
ChipSelectMethod  cs_deassert,
OSMutex bus_lock = nullptr 
)
inlineprotected

Constructor.

Parameters
namedevice name in file system
cs_assertfunction pointer to a method that asserts chip select
cs_deassertfunction pointer to a method that deasserts chip select
bus_lockthe user must provide a shared mutex if the device instance represents more than one chip select on the same bus interface.

Definition at line 120 of file SPI.hxx.

◆ ~SPI()

SPI::~SPI ( )
inlineprotected

Destructor.

Definition at line 135 of file SPI.hxx.

Member Function Documentation

◆ bus_lock()

void SPI::bus_lock ( )
inlineprotected

Lock the bus shared by many chip selects.

Definition at line 141 of file SPI.hxx.

◆ bus_unlock()

void SPI::bus_unlock ( )
inlineprotected

Unlock the bus shared by many chip selects.

Definition at line 150 of file SPI.hxx.

◆ flush_buffers()

void SPI::flush_buffers ( )
inlineoverrideprivatevirtual

Discards all pending buffers.

Called after disable().

Implements Node.

Definition at line 219 of file SPI.hxx.

◆ ioctl()

int SPI::ioctl ( File file,
unsigned long int  key,
unsigned long  data 
)
overrideprotectedvirtual

Request an ioctl transaction.

Parameters
filefile reference for this device
keyioctl key
datakey data
Returns
>= 0 upon success, -errno upon failure
Parameters
filefile reference for this device
keyioctl key
datakey data
Returns
0 upon success, -errno upon failure

Reimplemented from FileIO.

Definition at line 165 of file SPI.cxx.

◆ read()

ssize_t SPI::read ( File file,
void *  buf,
size_t  count 
)
overrideprivatevirtual

Read from a file or device.

Parameters
filefile reference for this device
buflocation to place read data
countnumber of bytes to read
Returns
number of bytes read upon success, -errno upon failure

Implements FileIO.

Definition at line 93 of file SPI.cxx.

◆ transfer()

virtual int SPI::transfer ( struct spi_ioc_transfer *  msg)
protectedpure virtual

Method to transmit/receive the data.

Parameters
msgmessage(s) to transact.
Returns
bytes transfered upon success, -errno upon failure

Implemented in Stm32SPI, CC32xxSPI, and TivaSPI.

◆ transfer_messages()

int SPI::transfer_messages ( struct spi_ioc_transfer *  msgs,
int  num 
)

Conduct multiple message transfers with one stop at the end.

Parameters
msgsarray of messages to transfer
numnumber of messages to transfer
Returns
total number of bytes transfered, -errno upon failure

Definition at line 49 of file SPI.cxx.

◆ transfer_polled()

virtual int SPI::transfer_polled ( struct spi_ioc_transfer *  msg)
protectedpure virtual

Method to transmit/receive the data, but always in polled mode.

Parameters
msgmessage(s) to transact.
Returns
bytes transfered upon success, -errno upon failure

Implemented in CC32xxSPI, TivaSPI, Stm32SPI, CC32xxSPI, and TivaSPI.

◆ transfer_with_cs_assert()

int SPI::transfer_with_cs_assert ( struct spi_ioc_transfer *  msg)
inline

Method to transmit/receive the data.

Chip select will be asserted at start of the transfer and deasserted at the end of the transfer.

Parameters
msgmessage(s) to transact.
Returns
bytes transfered upon success, -errno upon failure

Definition at line 64 of file SPI.hxx.

◆ transfer_with_cs_assert_polled()

int SPI::transfer_with_cs_assert_polled ( struct spi_ioc_transfer *  msgs,
int  num = 1 
)
inline

Method to transmit/receive the data.

Chip select will be asserted at start of the transfer and deasserted at the end of the transfer. This will always be a polled transaction no matter what.

Parameters
msgmessage(s) to transact.
numnumber of messages to transfer
Returns
bytes transfered upon success, -errno upon failure

Definition at line 80 of file SPI.hxx.

◆ update_configuration()

virtual int SPI::update_configuration ( )
protectedpure virtual

Update the configuration of the bus.

Returns
>= 0 upon success, -errno upon failure

Implemented in Stm32SPI, CC32xxSPI, and TivaSPI.

◆ write()

ssize_t SPI::write ( File file,
const void *  buf,
size_t  count 
)
overrideprivatevirtual

Write to a file or device.

Parameters
filefile reference for this device
buflocation to find write data
countnumber of bytes to write
Returns
number of bytes written upon success, -errno upon failure

Implements FileIO.

Definition at line 129 of file SPI.cxx.

Member Data Documentation

◆ bitsPerWord

uint8_t SPI::bitsPerWord
protected

number of bits per word transaction

Definition at line 193 of file SPI.hxx.

◆ busLock

OSMutex* SPI::busLock
private

Mutual exclusion for the bus among many chip selects.

Definition at line 222 of file SPI.hxx.

◆ csAssert

ChipSelectMethod SPI::csAssert
protected

function pointer to a method that asserts chip select.

Definition at line 184 of file SPI.hxx.

◆ csDeassert

ChipSelectMethod SPI::csDeassert
protected

function pointer to a method that deasserts chip select.

Definition at line 187 of file SPI.hxx.

◆ lsbFirst

bool SPI::lsbFirst
protected

transmit LSB first if true

Definition at line 199 of file SPI.hxx.

◆ mode

uint8_t SPI::mode
protected

one of four SPI modes

Definition at line 196 of file SPI.hxx.

◆ speedHz

uint32_t SPI::speedHz
protected

Max default speed in Hz.

Definition at line 190 of file SPI.hxx.


The documentation for this class was generated from the following files: