Some DLN-series adapters support the SPI Slave Interface.
To configure the slave for the communication, follow these steps:
Configure SS idle timeout. When you enable the SPI slave, the SS line may be busy. The DLN adapter waits until the SS line drives high. If the SS line is not released during the SS idle timeout, the SPI slave cannot be enabled. Read SS Idle Timeout.
Configure the transmission mode (clock polarity and clock phase). The clock polarity (CPOL) and clock phase (CPHA) configuration must be the same for both SPI master and SPI slave devices.
Configure loading data to the reply buffer. The reply mode and reply type parameters can prevent from losing data if the frame size, configured by you for the SPI slave, does not correspond the frame size set on the SPI master. Read SPI Slave Reply Buffer.
If needed, configure events. DLN adapters provide event-driven interface for SPI slave devices. Read SPI Slave Events.
When you try to enable the SPI slave module, the SS line can appear busy. To prevent transmitting incorrect data, the DLN adapter idles until the SS line is released (drives high). Only then, the SPI slave can be enabled.
To limit the idle time, you can specify the idle timeout value in milliseconds (ms) by calling the DlnSpiSlaveSetSSIdleTimeout() function. The default value is 100ms. The idle timeout can be from 1ms to 1000ms. If during the specified idle timeout the SS line was not released, the DlnSpiSlaveEnable() function returns the DLN_RES_SPI_SLAVE_SS_IDLE_TIMEOUT
error.
For details about idle event timeout, read DLN_SPI_SLAVE_EVENT_IDLE Events.
When the master initiates transmission, the DLN adapter-slave fills the reply buffer with data that should be sent to the master. To fill the reply buffer, you can use the following functions: DlnSpiSlaveLoadReply() or DlnSpiSlaveEnqueueReply(). The DlnSpiSlaveLoadReply() function clears the buffer and makes the current reply the first in the queue. In contrast, the DlnSpiSlaveEnqueueReply() function puts the current reply to the end of the queue.
Transmitted data is limited to the number of frames received by the master. The sizes of slave buffer (reply buffer) and master buffer (received buffer) may differ.
If the reply buffer size exceeds the received buffer size, the last bytes of the reply buffer can be lost or sent with the following transmission. It depends on the reply mode you configure. For more information, read Reply Modes.
If the reply buffer size is less than the received buffer size, the last bytes can be filled by zeroes or by repeated reply bytes. It depends on the shortage action you configure. For details, read Reply Shortage Actions.
If the reply buffer size exceeds the size of the buffer received by the master, the following options are possible:
If the last bytes of the reply are not weighty, they can be rejected. In this case, the master receives the bytes from the reply buffer until the SS line rises. The last bytes of the reply buffer are lost. This mode is called DLN_SPI_SLAVE_REPLY_MODE_SS_BASED
.
If you do not want to lose any data, the last buffers can be added to the queue. In this case, the master receives the bytes from the reply buffer until the SS line rises. The last bytes of the reply buffer are not lost, they are added to the queue and the master receives them with the next transmission. This mode is called DLN_SPI_SLAVE_REPLY_MODE_COUNT_BASED
.
You have two loaded replies: ABCD
and EF12
. The master initiates two transmissions of 3 words in each. From the slave, the master receives the following:
In the DLN_SPI_SLAVE_REPLY_MODE_SS_BASED
mode, the master receives ABC
and EF1
. The last bytes of every reply are lost.
In the DLN_SPI_SLAVE_REPLY_MODE_COUNT_BASED
mode, the master receives ABC
and DEF
. The last bytes of the second reply is queued for the following transmissions.
To configure the reply mode, use the DlnSpiSlaveSetReplyMode() function. The DlnSpiSlaveGetSupportedReplyModes() function retrieves all supported reply modes.
If the reply buffer size is less than the size of the buffer received by the master, the missed bytes can be filled by zeroes (the DLN_SPI_SLAVE_REPLY_SHORTAGE_SEND_ZEROES
reply type) or by repeating the reply bytes (the DLN_SPI_SLAVE_REPLY_SHORTAGE_REUSE
reply type).
You have a loaded reply: ABCD
. The master initiates a transmission of 6 words. From the slave, the master receives the following:
If the DLN_SPI_SLAVE_REPLY_SHORTAGE_SEND_ZEROES
reply type is set, the master receives ABCD00
. The last bytes are filled with zeroes.
If the DLN_SPI_SLAVE_REPLY_SHORTAGE_REUSE
reply type is set, the master receives ABCDAB
. The last bytes are filled by repeating the first bytes of the reply.
DLN adapters-slaves can be configured to send events. The events are generated when the slave meets the certain predefined conditions.
DLN adapters-slaves support the following events:
Event Type | Description |
| A DLN adapter does not generate any events. |
| A DLN adapter generates events when the level on the SS line rises. For details, read DLN_SPI_SLAVE_EVENT_SS_RISE Events. |
| A DLN adapter generates events when the buffer is full. For details, read DLN_SPI_SLAVE_EVENT_BUFFER_FULL Events. |
| A DLN adapter generates events when the slave idles for the configured time. For details, read DLN_SPI_SLAVE_EVENT_IDLE Events. |
When an event occurs, your application is notified (see the Notifications section). Call the DlnGetMessage() function to obtain the event details. The DLN_SPI_SLAVE_DATA_RECEIVED_EV
structure describes this information.
By default, event generation is disabled (DLN_SPI_SLAVE_EVENT_NONE
).
A DLN adapter generates the DLN_SPI_SLAVE_EVENT_SS_RISE
events each time the level on the SS line rises.
When the transmission from/to the SPI master completes, the master rises the level on the SS line. At this moment, a DLN adapter-slave generates the DLN_SPI_SLAVE_EVENT_SS_RISE
event. After receiving this event, your application can perform certain actions, for example, show received data or start processing them.
The eventCount
field of the first event is set to zero. For every new DLN_SPI_SLAVE_EVENT_SS_RISE
event, the eventCount
field increments. When you reset the slave or change its configuration, the eventCount
filed is reset to zero.
Use the DlnSpiSlaveEnableSSRiseEvent() function to enable generating these events. To disable this event, use the DlnSpiSlaveDisableSSRiseEvent() function. The DlnSpiSlaveIsSSRiseEventEnabled() function allows to check whether this event is enabled.
A DLN adapter generates the DLN_SPI_SLAVE_EVENT_BUFFER_FULL
events each time the buffer becomes full.
You can configure the buffer size by calling the DlnSpiSlaveSetEventSize() function. The supported values are from 1 to 256 bytes. By default, the size parameter is set to 256 bytes. To check the current value, use the DlnSpiSlaveGetEventSize() function.
During the transmission from the SPI master, if the buffer is overfilled, a DLN adapter-slave generates the DLN_SPI_SLAVE_EVENT_BUFFER_FULL
event.
The eventCount
field of the first event is set to zero. For every new DLN_SPI_SLAVE_EVENT_SS_RISE
event, the eventCount
field increments. When you reset the slave or change its configuration, the eventCount
filed is reset to zero.
Use the DlnSpiSlaveEnableEvent() function to enable generating these events. To disable this event, use the DlnSpiSlaveDisableEvent() function. The DlnSpiSlaveIsEventEnabled() function allows to check whether this event is enabled.
The DLN_SPI_SLAVE_EVENT_BUFFER_FULL
event does not conflict with other events.
A DLN adapter generates the DLN_SPI_SLAVE_EVENT_IDLE
events each time the SS line stays raised for a certain time.
When the transmission from/to the SPI master completes, the master rises the level on the SS line. If the level stays high for the time set in the timeout parameter, a DLN adapter-slave generates the DLN_SPI_SLAVE_EVENT_IDLE
event. If the SS line stays in the high level less than the required idle timeout, the event does not occur.
To configure the timeout parameter, use the DlnSpiSlaveSetIdleEventTimeout() function. The DlnSpiSlaveSetIdleEventTimeout() function allows to check the current value of this parameter.
Use the DlnSpiSlaveEnableIdleEvent() function to enable generating DLN_SPI_SLAVE_EVENT_IDLE
events. To disable this event, use the DlnSpiSlaveDisableIdleEvent() function. The DlnSpiSlaveIsIdleEventEnabled() function allows to check whether this event is enabled.
The default configuration for the SPI slave port is as 8-bit SPI slave with CPOL=0 and CPHA=0 transmission parameters. By default, no events are generated.
Using the SPI slave functions allows you to change and check the current SPI slave configuration, to control data transmission.
The SPI slave functions include the following:
General port information:
Retrieves the number of SPI slave ports available in the DLN adapter.
Assigns the selected port to the SPI slave module.
Releases the selected SPI slave port.
Retrieves whether the selected port is assigned to the SPI slave module.
Configuration functions:
Specifies idle timeout for releasing the SS line.
Retrieves the idle timeout setting.
Specifies the data frame size for the selected SPI slave port.
Retrieves the current frame size for the selected SPI slave port.
Retrieves the supported frame sizes.
Specifies transmission parameters (CPOL and CPHA) for the selected SPI slave port.
Retrieves the current transmission parameters (CPOL and CPHA) for the selected SPI slave port.
Retrieves the supported transmission parameters (CPOL and CPHA).
Specifies CPHA value for the selected SPI slave port.
Retrieves the current CPHA value for the selected SPI slave port.
Retrieves the supported CPHA values.
Specifies CPOL value for the selected SPI slave port.
Retrieves the current CPOL value for the selected SPI slave port.
Retrieves the supported CPOL values.
Reply functions:
Clears the buffer and makes the current reply the first in the queue.
Puts the current reply to the end of the queue.
Specifies actions if the reply buffer size exceeds the size of the buffer received by the master.
Retrieves the current reply configuration.
Retrieves the supported reply modes.
Specifies actions if the reply buffer size is less than the size of the buffer received by the master.
Retrieves the current for reply shortage action settings.
Retrieves the supported reply shortage actions.
Event configuration:
Enables generating events when the level on the SS line rises.
Disables generating events when the level on the SS line rises.
Retrieves the current configuration for generating events when the level on the SS line rises.
Enables generating events when the buffer is full.
Disables generating events when the buffer is full.
Retrieves the current configuration for generating events when the buffer is full.
Specifies the buffer size.
Retrieves the current buffer size.
Enables generating events when the slave idles for the configured time.
Disables generating events when the slave idles for the configured time.
Retrieves the current configuration for generating events when the slave idles for the configured time.
Specifies the timeout value.
Retrieves the current timeout value.
Retrieves the minimum timeout value.
Retrieves the maximum timeout value.
The dln_spi_slave.h
file declares the SPI Salve Interface functions.
The DlnSpiSlaveGetPortCount()
function retrieves the total number of SPI slave ports available at your DLN-series adapter.
The DlnSpiSlaveEnable()
function activates the specified SPI slave port at your DLN-series adapter.
The DlnSpiSlaveDisable()
function deactivates the specified SPI slave port on your DLN adapter.
The DlnSpiSlaveIsEnabled()
function retrieves information whether the specified SPI slave port is active or not.
The DlnSpiSlaveSetSSIdleTimeout()
function sets SS idle timeout. For details, read SS Idle Timeout.
The DlnSpiSlaveGetSSIdleTimeout()
function retrieves the current value of SS idle timeout.
The DlnSpiSlaveSetFrameSize()
function sets the size of a single SPI data frame.
The DlnSpiSlaveGetFrameSize()
function retrieves the current size setting for SPI data frames.
The DlnSpiSlaveGetSupportedFrameSizes()
function returns all supported frame size values.
The DlnSpiSlaveSetMode()
function sets SPI transmission parameters (CPOL and CPHA). For more information, read Clock Phase and Polarity.
The DlnSpiSlaveGetMode()
function retrieves the current transmission parameters (CPOL and CPHA) for the specified SPI slave port. For details, read Clock Phase and Polarity.
The DlnSpiSlaveGetSupportedModes()
function retrieves all supported SPI slave modes.
The DlnSpiSlaveSetCpha()
function allows to set the clock phase (CPHA) value.
The DlnSpiSlaveGetCpha()
function retrieves the current value of clock phase (CPHA).
The DlnSpiSlaveGetSupportedCphaValues()
function retrieves all supported CPHA (clock phase) values.
The DlnSpiSlaveSetCpol()
function allows to set the clock polarity (CPOL) value.
The DlnSpiSlaveGetCpol()
function retrieves the current value of clock polarity (CPOL).
The DlnSpiSlaveGetSupportedCpolValues()
function retrieves all supported CPOL (clock polarity) values.
The DlnSpiSlaveLoadReply()
function loads data (loads a number of bytes to reply buffer) to be transmitted to an SPI-master device. This function clears the buffer and makes the current reply the first in the queue.
The DlnSpiSlaveEnqueueReply()
function adds a buffer to queue.
The DlnSpiSlaveSetReplyMode()
function sets SPI slave reply mode.
The DlnSpiSlaveGetReplyMode()
function retrieves the current SPI slave reply mode.
The DlnSpiSlaveGetSupportedReplyModes()
function retrieves the supported SPI slave reply modes.
The DlnSpiSlaveSetReplyShortageAction()
function sets the reply shortage action value. When the master sends a data block that is larger than the loaded reply, this function specifies how to fill empty bytes (repeat the reply bytes or send zeroes).
The DlnSpiSlaveGetReplyShortageAction()
functions retrieves the current value of reply shortage action.
The DlnSpiSlaveGetSupportedShortageActions()
function retrieves supported shortage action values.
The DlnSpiSlaveEnableSSRiseEvent()
function activates event on SS rising edge.
The DlnSpiSlaveDisableSSRiseEvent()
function deactivates SS rise events.
The DlnSpiSlaveIsSSRiseEventEnabled()
function retrieves information whether the SS rise events are active or not.
The DlnSpiSlaveEnableEvent()
function activates SPI slave buffer full event.
The DlnSpiSlaveDisableEvent()
function deactivates SPI slave buffer full event.
The DlnSpiSlaveIsEventEnabled()
function retrieves information whether the SPI slave events are active or not.
The DlnSpiSlaveSetEventSize()
function sets the event buffer size.
The DlnSpiSlaveGetEventSize()
function retrieves value of current event buffer size.
The DlnSpiSlaveEnableIdleEvent()
function activates the SPI slave idle event.
The DlnSpiSlaveDisableIdleEvent()
function deactivates the SPI slave idle event.
The DlnSpiSlaveIsIdleEventEnabled()
function activates the SPI slave idle event.
The DlnSpiSlaveSetIdleEventTimeout()
function sets idle event timeout.
The DlnSpiSlaveGetIdleEventTimeout()
function retrieves the current value of idle event timeout.
The DlnSpiSlaveGetMinIdleEventTimeout()
function retrieves the minimum value of idle event timeout.
The DlnSpiSlaveGetMaxIdleEventTimeout()
function retrieves the maximum value of idle event timeout.