Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TivaSPI Class Reference
Inheritance diagram for TivaSPI:
SPI Node Device FileIO

Public Member Functions

 TivaSPI (const char *name, unsigned long base, uint32_t interrupt, ChipSelectMethod cs_assert, ChipSelectMethod cs_deassert, OSMutex *bus_lock=nullptr, size_t dma_threshold=DEFAULT_DMA_THRESHOLD_BYTES, uint32_t dma_channel_index_tx=UDMA_CH11_SSI0TX, uint32_t dma_channel_index_rx=UDMA_CH10_SSI0RX)
 Constructor.
 
 ~TivaSPI ()
 Destructor.
 
void interrupt_handler ()
 Call this from the SPI device's interrupt handler.
 
OSMutexget_lock ()
 This method provides a reference to the Mutex used by this device driver.
 
- Public Member Functions inherited from SPI
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.
 

Private Member Functions

void enable () override
 Function to enable device.
 
void disable () override
 Function to disable device.
 
int update_configuration () override
 Update the configuration of the bus.
 
int transfer (struct spi_ioc_transfer *msg) override
 Method to transmit/receive the data.
 
template<typename T >
int transfer_polled (struct spi_ioc_transfer *msg)
 Method to transmit/receive the data.
 
int transfer_polled (struct spi_ioc_transfer *msg) override
 Method to transmit/receive the data.
 
void config_dma (struct spi_ioc_transfer *msg)
 Configure a DMA transaction.
 
long data_get_non_blocking (uint16_t *data)
 Receives a word from the specified port.
 
long data_put_non_blocking (uint16_t data)
 Transmits a word on the specified port.
 
void data_put (uint16_t data)
 Waits until the word is transmitted on the specified port.
 
void set_configuration ()
 Set the instance local configuration.
 
 DISALLOW_COPY_AND_ASSIGN (TivaSPI)
 

Private Attributes

OSSemsem_
 reference to the semaphore belonging to this bus
 
unsigned long base_
 base address of this device
 
unsigned long interrupt_
 interrupt of this device
 
size_t dmaThreshold_
 threshold in bytes to start using DMA
 
uint32_t dmaChannelIndexTx_
 TX DMA channel index.
 
uint32_t dmaChannelIndexRx_
 RX DMA channel index.
 
uint16_t spiCr0_
 Configuration register 0 local copy.
 
uint16_t spiCr1_
 Configuration register 1 local copy.
 
uint16_t spiPrescaler_
 Prescale register local copy.
 

Static Private Attributes

static constexpr size_t DEFAULT_DMA_THRESHOLD_BYTES = 0
 Transfers longer than this will be with dma by default.
 
static constexpr size_t MAX_DMA_TRANSFER_AMOUNT = 1024
 Maximum number of bytes transferred in a single DMA transaction.
 
static constexpr uint32_t dmaRxConfig_ []
 This lookup table is used to configure the DMA channels for the appropriate (8bit or 16bit) transfer sizes.
 
static constexpr uint32_t dmaTxConfig_ []
 This lookup table is used to configure the DMA channels for the appropriate (8bit or 16bit) transfer sizes.
 
static constexpr uint32_t dmaNullConfig_ []
 This lookup table is used to configure the DMA channels for the appropriate (8bit or 16bit) transfer sizes when either txBuf or rxBuf are NULL.
 

Additional Inherited Members

- Public Types inherited from SPI
typedef void(* ChipSelectMethod) ()
 Function point for the chip select assert and deassert methods.
 
- 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.
 
- Protected Member Functions inherited from SPI
 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.
 
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.
 
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.
 
- 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.
 
- Protected Attributes inherited from SPI
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
 
- 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

Definition at line 44 of file TivaSPI.hxx.

Constructor & Destructor Documentation

◆ TivaSPI()

TivaSPI::TivaSPI ( const char *  name,
unsigned long  base,
uint32_t  interrupt,
ChipSelectMethod  cs_assert,
ChipSelectMethod  cs_deassert,
OSMutex bus_lock = nullptr,
size_t  dma_threshold = DEFAULT_DMA_THRESHOLD_BYTES,
uint32_t  dma_channel_index_tx = UDMA_CH11_SSI0TX,
uint32_t  dma_channel_index_rx = UDMA_CH10_SSI0RX 
)

Constructor.

Parameters
namename of this device instance in the file system
basebase address of this device
interruptinterrupt number of this device
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.
dma_thresholdthe threshold in bytes to use a DMA transaction, 0 = DMA always disabled
dma_channel_index_txUDMA_CHn_SSInTX
dma_channel_index_rxUDMA_CHn_SSInRX

Definition at line 45 of file TivaSPI.cxx.

◆ ~TivaSPI()

TivaSPI::~TivaSPI ( )

Destructor.

Definition at line 85 of file TivaSPI.cxx.

Member Function Documentation

◆ config_dma()

void TivaSPI::config_dma ( struct spi_ioc_transfer *  msg)
private

Configure a DMA transaction.

Parameters
msgmessage to transact.

Definition at line 157 of file TivaSPI.cxx.

◆ data_get_non_blocking()

long TivaSPI::data_get_non_blocking ( uint16_t *  data)
inlineprivate

Receives a word from the specified port.

This function gets a SPI word from the receive FIFO for the specified port.

Parameters
datais pointer to receive data variable.
Returns
Returns the number of elements read from the receive FIFO.

Definition at line 254 of file TivaSPI.hxx.

◆ data_put()

void TivaSPI::data_put ( uint16_t  data)
inlineprivate

Waits until the word is transmitted on the specified port.

This function transmits a SPI word on the transmit FIFO for the specified port. This function waits until the space is available on transmit FIFO.

Parameters
datais data to be transmitted.

Definition at line 287 of file TivaSPI.hxx.

◆ data_put_non_blocking()

long TivaSPI::data_put_non_blocking ( uint16_t  data)
inlineprivate

Transmits a word on the specified port.

This function transmits a SPI word on the transmit FIFO for the specified port.

Parameters
datais data to be transmitted.
Returns
Returns the number of elements written to the transmit FIFO.

Definition at line 271 of file TivaSPI.hxx.

◆ disable()

void TivaSPI::disable ( )
overrideprivatevirtual

Function to disable device.

Implements Node.

Definition at line 93 of file TivaSPI.cxx.

◆ enable()

void TivaSPI::enable ( )
overrideprivatevirtual

Function to enable device.

Implements Node.

Definition at line 89 of file TivaSPI.cxx.

◆ get_lock()

OSMutex * TivaSPI::get_lock ( )
inline

This method provides a reference to the Mutex used by this device driver.

It can be passed into another SPI driver instance as a bus wide lock such that a SPI bus can be shared between this driver and another use case with another chip select.

Returns
a reference to this device driver instance lock

Definition at line 81 of file TivaSPI.hxx.

◆ interrupt_handler()

void TivaSPI::interrupt_handler ( )

Call this from the SPI device's interrupt handler.

Definition at line 163 of file TivaSPI.cxx.

◆ set_configuration()

void TivaSPI::set_configuration ( )
inlineprivate

Set the instance local configuration.

Definition at line 295 of file TivaSPI.hxx.

◆ transfer()

int TivaSPI::transfer ( struct spi_ioc_transfer *  msg)
inlineoverrideprivatevirtual

Method to transmit/receive the data.

Parameters
msgmessage to transact.

Implements SPI.

Definition at line 144 of file TivaSPI.hxx.

◆ transfer_polled() [1/2]

template<typename T >
int TivaSPI::transfer_polled ( struct spi_ioc_transfer *  msg)
inlineprivatevirtual

Method to transmit/receive the data.

This is a template in order to preserve execution speed on type specific pointer math.

Parameters
msgmessage to transact.

Implements SPI.

Definition at line 169 of file TivaSPI.hxx.

◆ transfer_polled() [2/2]

int TivaSPI::transfer_polled ( struct spi_ioc_transfer *  msg)
inlineoverrideprivatevirtual

Method to transmit/receive the data.

Parameters
msgmessage to transact.

Implements SPI.

Definition at line 226 of file TivaSPI.hxx.

◆ update_configuration()

int TivaSPI::update_configuration ( )
overrideprivatevirtual

Update the configuration of the bus.

Returns
>= 0 upon success, -errno upon failure

Implements SPI.

Definition at line 100 of file TivaSPI.cxx.

Member Data Documentation

◆ base_

unsigned long TivaSPI::base_
private

base address of this device

Definition at line 306 of file TivaSPI.hxx.

◆ DEFAULT_DMA_THRESHOLD_BYTES

constexpr size_t TivaSPI::DEFAULT_DMA_THRESHOLD_BYTES = 0
staticconstexprprivate

Transfers longer than this will be with dma by default.

Todo:
this should be 64 bytes

Definition at line 89 of file TivaSPI.hxx.

◆ dmaChannelIndexRx_

uint32_t TivaSPI::dmaChannelIndexRx_
private

RX DMA channel index.

Definition at line 310 of file TivaSPI.hxx.

◆ dmaChannelIndexTx_

uint32_t TivaSPI::dmaChannelIndexTx_
private

TX DMA channel index.

Definition at line 309 of file TivaSPI.hxx.

◆ dmaNullConfig_

constexpr uint32_t TivaSPI::dmaNullConfig_[]
staticconstexprprivate
Initial value:
=
{
UDMA_SIZE_8 | UDMA_SRC_INC_NONE | UDMA_DST_INC_NONE | UDMA_ARB_1,
UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_NONE | UDMA_ARB_1,
}

This lookup table is used to configure the DMA channels for the appropriate (8bit or 16bit) transfer sizes when either txBuf or rxBuf are NULL.

Definition at line 121 of file TivaSPI.hxx.

◆ dmaRxConfig_

constexpr uint32_t TivaSPI::dmaRxConfig_[]
staticconstexprprivate
Initial value:
=
{
UDMA_SIZE_8 | UDMA_SRC_INC_NONE | UDMA_DST_INC_8 | UDMA_ARB_1,
UDMA_SIZE_16 | UDMA_SRC_INC_NONE | UDMA_DST_INC_16 | UDMA_ARB_1,
}

This lookup table is used to configure the DMA channels for the appropriate (8bit or 16bit) transfer sizes.

Table for an SPI DMA RX channel.

Definition at line 99 of file TivaSPI.hxx.

◆ dmaThreshold_

size_t TivaSPI::dmaThreshold_
private

threshold in bytes to start using DMA

Definition at line 308 of file TivaSPI.hxx.

◆ dmaTxConfig_

constexpr uint32_t TivaSPI::dmaTxConfig_[]
staticconstexprprivate
Initial value:
=
{
UDMA_SIZE_8 | UDMA_SRC_INC_8 | UDMA_DST_INC_NONE | UDMA_ARB_1,
UDMA_SIZE_16 | UDMA_SRC_INC_16 | UDMA_DST_INC_NONE | UDMA_ARB_1,
}

This lookup table is used to configure the DMA channels for the appropriate (8bit or 16bit) transfer sizes.

Table for an SPI DMA TX channel

Definition at line 110 of file TivaSPI.hxx.

◆ interrupt_

unsigned long TivaSPI::interrupt_
private

interrupt of this device

Definition at line 307 of file TivaSPI.hxx.

◆ MAX_DMA_TRANSFER_AMOUNT

constexpr size_t TivaSPI::MAX_DMA_TRANSFER_AMOUNT = 1024
staticconstexprprivate

Maximum number of bytes transferred in a single DMA transaction.

Definition at line 92 of file TivaSPI.hxx.

◆ sem_

OSSem* TivaSPI::sem_
private

reference to the semaphore belonging to this bus

Definition at line 305 of file TivaSPI.hxx.

◆ spiCr0_

uint16_t TivaSPI::spiCr0_
private

Configuration register 0 local copy.

Definition at line 312 of file TivaSPI.hxx.

◆ spiCr1_

uint16_t TivaSPI::spiCr1_
private

Configuration register 1 local copy.

Definition at line 313 of file TivaSPI.hxx.

◆ spiPrescaler_

uint16_t TivaSPI::spiPrescaler_
private

Prescale register local copy.

Definition at line 314 of file TivaSPI.hxx.


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