Internal Interface Wrap
API reference
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
StmSpiDevice Class Reference

Soft-CS SPI slave on a shared StmSpiBus. More...

#include <StmSpi.h>

Inheritance diagram for StmSpiDevice:
[legend]
Collaboration diagram for StmSpiDevice:
[legend]

Public Member Functions

 StmSpiDevice (StmSpiBus *parent, const hf_spi_device_config_t &config) noexcept
 
 ~StmSpiDevice () noexcept override
 
bool Initialize () noexcept override
 Initialize the SPI bus.
 
bool Deinitialize () noexcept override
 Deinitialize the SPI bus.
 
hf_spi_err_t Transfer (const hf_u8_t *tx_data, hf_u8_t *rx_data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept override
 One soft-CS frame: bus lock + mode + FIFO flush + CS RAII + HAL.
 
hf_spi_err_t TransferChain (const hf_u8_t *const *tx_frames, hf_u8_t *const *rx_frames, hf_u16_t frame_length, hf_u16_t frame_count, hf_u32_t inter_frame_gap_us, hf_u32_t timeout_ms=0) noexcept override
 N soft-CS frames under one bus lock (CS still toggles per frame).
 
const void * GetDeviceConfig () const noexcept override
 Get the device configuration for this SPI device.
 
void IdleChipSelect () noexcept override
 Force soft-CS idle-high (shared-bus peer park before Mode1 identity).
 
void HoldChipSelectMs (hf_u32_t ms) noexcept override
 Bus-locked soft-CS hold for pad proof (peers idle-high).
 
bool SetTransferMode (hf_u8_t mode_0_to_3) noexcept override
 Set CPOL/CPHA for the next transfer (0=Mode0 … 3=Mode3).
 
bool SetIoSwap (bool enable) noexcept override
 Set MOSI↔MISO IOSwap for the next transfer (CFG2 IOSWP).
 
const hf_spi_device_config_t & GetConfig () const noexcept
 Device configuration (CS port/pin, mode, clock hint).
 
- Public Member Functions inherited from BaseSpi
virtual ~BaseSpi () noexcept=default
 Virtual destructor ensures proper cleanup in derived classes.
 
 BaseSpi (const BaseSpi &)=delete
 
BaseSpi & operator= (const BaseSpi &)=delete
 
 BaseSpi (BaseSpi &&)=delete
 
BaseSpi & operator= (BaseSpi &&)=delete
 
bool EnsureInitialized () noexcept
 Ensures that the SPI bus is initialized (lazy initialization).
 
bool EnsureDeinitialized () noexcept
 Ensures that the SPI bus is deinitialized (lazy deinitialization).
 
bool IsInitialized () const noexcept
 Checks if the bus is initialized.
 
virtual bool Open () noexcept
 Legacy compatibility: Open and initialize the SPI bus.
 
virtual bool Close () noexcept
 Legacy compatibility: Close and de-initialize the SPI bus.
 
virtual bool Transfer (const hf_u8_t *tx_data, hf_u8_t *rx_data, hf_u16_t length) noexcept
 Legacy compatibility: Transfer with boolean return.
 
virtual hf_spi_err_t Write (const hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept
 Write data to SPI bus.
 
virtual hf_spi_err_t Read (hf_u8_t *data, hf_u16_t length, hf_u32_t timeout_ms=0) noexcept
 Read data from SPI bus.
 
virtual bool Write (const hf_u8_t *data, hf_u16_t length) noexcept
 Legacy compatibility: Write with boolean return.
 
virtual bool Read (hf_u8_t *data, hf_u16_t length) noexcept
 Legacy compatibility: Read with boolean return.
 
virtual bool WriteByte (hf_u8_t data) noexcept
 Write single byte to SPI bus.
 
virtual bool ReadByte (hf_u8_t &data) noexcept
 Read single byte from SPI bus.
 
virtual bool TransferByte (hf_u8_t tx_data, hf_u8_t &rx_data) noexcept
 Write single byte and read response.
 
virtual hf_spi_err_t ResetStatistics () noexcept
 Reset SPI operation statistics.
 
virtual hf_spi_err_t ResetDiagnostics () noexcept
 Reset SPI diagnostic information.
 
virtual hf_spi_err_t GetStatistics (hf_spi_statistics_t &statistics) const noexcept
 Get SPI operation statistics.
 
virtual hf_spi_err_t GetDiagnostics (hf_spi_diagnostics_t &diagnostics) const noexcept
 Get SPI diagnostic information.
 

Private Member Functions

void AssertCS () noexcept
 Drive CS to the active level (active-low → GPIO low).
 
void DeassertCS () noexcept
 Drive CS to the idle level (active-low → GPIO high).
 
hf_spi_err_t TransferLocked (const hf_u8_t *tx_data, hf_u8_t *rx_data, hf_u16_t length, hf_u32_t effective_timeout, bool park_mode3_after=true, bool bus_already_armed=false) noexcept
 One CS-framed HAL transfer; caller must hold StmSpiBusLock.
 
hf_u32_t GetEffectiveTimeout (hf_u32_t requested_ms) const noexcept
 Resolve requested_ms against the parent bus default.
 

Static Private Member Functions

static hf_spi_err_t ConvertHalStatus (hf_u32_t hal_status) noexcept
 Map STM32 HAL status codes to hf_spi_err_t.
 

Private Attributes

StmSpiBus * parent_bus_
 Parent bus reference.
 
hf_spi_device_config_t config_
 Device configuration.
 
bool io_swap_ {false}
 CFG2 IOSWP for next transfer.
 

Friends

class StmSpiBus
 

Additional Inherited Members

- Protected Member Functions inherited from BaseSpi
 BaseSpi () noexcept
 Protected default constructor.
 
- Protected Attributes inherited from BaseSpi
bool initialized_
 Initialization state.
 
hf_spi_statistics_t statistics_
 SPI operation statistics.
 
hf_spi_diagnostics_t diagnostics_
 SPI diagnostic information.
 

Detailed Description

Soft-CS SPI slave on a shared StmSpiBus.

Owns one GPIO chip-select and a per-device SPI mode (hf_spi_device_config_t::mode). Each Transfer:

  1. Takes StmSpiBusLock (RTOS mutex — peers wait),
  2. Parks all soft-CS on the bus idle-high,
  3. Applies this device's CPOL/CPHA (SPE toggle only with every CS high),
  4. Flushes the RX FIFO,
  5. Scopes only this CS with SoftChipSelectGuard,
  6. Releases the bus lock.

Mixed Mode 0/1/3 slaves may share one CubeMX SPI peripheral. Chip handlers and external drivers must not poke CS — only Transfer / TransferChain.

Note
CS low during a Mode1 (TLE) frame is expected. CS must be high for every other slave while Mode0/1/3 SPE/CPOL is being rewritten.

Constructor & Destructor Documentation

◆ StmSpiDevice()

StmSpiDevice::StmSpiDevice ( StmSpiBus *  parent,
const hf_spi_device_config_t &  config 
)
noexcept

◆ ~StmSpiDevice()

StmSpiDevice::~StmSpiDevice ( )
overridenoexcept

Member Function Documentation

◆ AssertCS()

void StmSpiDevice::AssertCS ( )
privatenoexcept

Drive CS to the active level (active-low → GPIO low).

◆ ConvertHalStatus()

hf_spi_err_t StmSpiDevice::ConvertHalStatus ( hf_u32_t  hal_status)
staticprivatenoexcept

Map STM32 HAL status codes to hf_spi_err_t.

◆ DeassertCS()

void StmSpiDevice::DeassertCS ( )
privatenoexcept

Drive CS to the idle level (active-low → GPIO high).

◆ Deinitialize()

bool StmSpiDevice::Deinitialize ( )
overridevirtualnoexcept

Deinitialize the SPI bus.

Returns
true if successful, false otherwise
Note
Must be implemented by concrete classes.

Implements BaseSpi.

◆ GetConfig()

const hf_spi_device_config_t & StmSpiDevice::GetConfig ( ) const
inlinenoexcept

Device configuration (CS port/pin, mode, clock hint).

◆ GetDeviceConfig()

const void * StmSpiDevice::GetDeviceConfig ( ) const
overridevirtualnoexcept

Get the device configuration for this SPI device.

Returns
Device-specific configuration information

Implements BaseSpi.

◆ GetEffectiveTimeout()

hf_u32_t StmSpiDevice::GetEffectiveTimeout ( hf_u32_t  requested_ms) const
privatenoexcept

Resolve requested_ms against the parent bus default.

◆ HoldChipSelectMs()

void StmSpiDevice::HoldChipSelectMs ( hf_u32_t  ms)
overridevirtualnoexcept

Bus-locked soft-CS hold for pad proof (peers idle-high).

Reimplemented from BaseSpi.

◆ IdleChipSelect()

void StmSpiDevice::IdleChipSelect ( )
overridevirtualnoexcept

Force soft-CS idle-high (shared-bus peer park before Mode1 identity).

Reimplemented from BaseSpi.

◆ Initialize()

bool StmSpiDevice::Initialize ( )
overridevirtualnoexcept

Initialize the SPI bus.

Returns
true if successful, false otherwise
Note
Must be implemented by concrete classes.

Implements BaseSpi.

◆ SetIoSwap()

bool StmSpiDevice::SetIoSwap ( bool  enable)
overridevirtualnoexcept

Set MOSI↔MISO IOSwap for the next transfer (CFG2 IOSWP).

Reimplemented from BaseSpi.

◆ SetTransferMode()

bool StmSpiDevice::SetTransferMode ( hf_u8_t  mode_0_to_3)
overridevirtualnoexcept

Set CPOL/CPHA for the next transfer (0=Mode0 … 3=Mode3).

Reimplemented from BaseSpi.

◆ Transfer()

hf_spi_err_t StmSpiDevice::Transfer ( const hf_u8_t *  tx_data,
hf_u8_t *  rx_data,
hf_u16_t  length,
hf_u32_t  timeout_ms = 0 
)
overridevirtualnoexcept

One soft-CS frame: bus lock + mode + FIFO flush + CS RAII + HAL.

Parameters
tx_dataTransmit buffer, or nullptr for RX-only.
rx_dataReceive buffer, or nullptr for TX-only.
lengthByte count (both directions when both buffers set).
timeout_ms0 → bus default timeout (bus-mutex wait).

Implements BaseSpi.

◆ TransferChain()

hf_spi_err_t StmSpiDevice::TransferChain ( const hf_u8_t *const *  tx_frames,
hf_u8_t *const *  rx_frames,
hf_u16_t  frame_length,
hf_u16_t  frame_count,
hf_u32_t  inter_frame_gap_us,
hf_u32_t  timeout_ms = 0 
)
overridevirtualnoexcept

N soft-CS frames under one bus lock (CS still toggles per frame).

Holds StmSpiBusLock across the chain so Mode0/1/3 peers cannot interleave mid-pipeline (e.g. TLE command + dummy). Each frame uses its own SoftChipSelectGuard — matching datasheets that require CSN rise between 32-bit accesses.

Reimplemented from BaseSpi.

◆ TransferLocked()

hf_spi_err_t StmSpiDevice::TransferLocked ( const hf_u8_t *  tx_data,
hf_u8_t *  rx_data,
hf_u16_t  length,
hf_u32_t  effective_timeout,
bool  park_mode3_after = true,
bool  bus_already_armed = false 
)
privatenoexcept

One CS-framed HAL transfer; caller must hold StmSpiBusLock.

Parameters
park_mode3_afterIf false, leave Mode3 SPE/CPOL intact after the frame (TMC9660 TMCL cmd+NO_OP). Single Transfer parks; TransferChain parks only after the last frame.
bus_already_armedSet for the 2nd..Nth frame of a TransferChain. The bus lock is held for the whole chain and every frame targets this same device, so no peer can have changed CPOL/CPHA/MBR or lowered a peer CS since the first frame armed them. Re-running IdleAllChipSelects
  • ApplyDeviceMode + the post-mode settle per frame cost more than the frame's own wire time on register bursts.

Friends And Related Symbol Documentation

◆ StmSpiBus

Member Data Documentation

◆ config_

hf_spi_device_config_t StmSpiDevice::config_
private

Device configuration.

◆ io_swap_

bool StmSpiDevice::io_swap_ {false}
private

CFG2 IOSWP for next transfer.

◆ parent_bus_

StmSpiBus* StmSpiDevice::parent_bus_
private

Parent bus reference.


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