HAL  v0.6.1
Hardware Abstraction Layer
Loading...
Searching...
No Matches
SPI

Serial Peripheral Interface. More...

Collaboration diagram for SPI:

Data Structures

struct  spi_channel_attr_t
 
struct  spi_handle_t
 

Enumerations

enum  spi_result_t {
  SPI_RESULT_OK ,
  SPI_RESULT_INVALID ,
  SPI_RESULT_UNSUPPORTED ,
  SPI_RESULT_TIMEOUT ,
  SPI_RESULT_IDLE ,
  SPI_RESULT_BUSY ,
  SPI_RESULT_ERR ,
  TOTAL_SPI_RESULT
}
 
enum  spi_status_t {
  SPI_STATUS_UNKNOWN ,
  SPI_STATUS_OK ,
  SPI_STATUS_BUSY ,
  SPI_STATUS_STOP ,
  SPI_STATUS_SENDING ,
  SPI_STATUS_RECEIVING ,
  SPI_STATUS_ERR ,
  TOTAL_SPI_STATUS
}
 
enum  spi_mode_t {
  SPI_MODE_MASTER ,
  SPI_MODE_SLAVE ,
  TOTAL_SPI_MODE
}
 
enum  spi_direction_t {
  SPI_DIRECTION_MSB_FIRST ,
  SPI_DIRECTION_LSB_FIRST ,
  TOTAL_SPI_DIRECTION
}
 

Functions

spi_result_t hal_spi_init (spi_handle_t *handle, const spi_channel_attr_t attr)
 Configure SPI channel with specific attributes.
 
spi_result_t hal_spi_write (spi_handle_t *handle, const uint8_t *tx_buffer, const size_t len)
 Write data to SPI bus.
 
spi_result_t hal_spi_read (spi_handle_t *handle, uint8_t *rx_buffer, const size_t len)
 Read data from SPI bus.
 
spi_result_t hal_spi_read_write (spi_handle_t *handle, const uint8_t *tx_buffer, uint8_t *rx_buffer, const size_t len)
 Simultaneous read and write operation.
 
spi_result_t hal_spi_stop (spi_handle_t *handle)
 Stop SPI operation.
 
spi_result_t hal_spi_mutex_take (const uint8_t channel, const uint32_t timeout_ms)
 Try acquire RTOS mutex for specified channel.
 
spi_result_t hal_spi_mutex_give (const uint8_t channel)
 Release RTOS mutex for specified channel.
 

Detailed Description

Serial Peripheral Interface.

Enumeration Type Documentation

◆ spi_direction_t

#include <spi/public_inc/hal_spi.h>

Enumerator
SPI_DIRECTION_MSB_FIRST 
SPI_DIRECTION_LSB_FIRST 
TOTAL_SPI_DIRECTION 

◆ spi_mode_t

enum spi_mode_t

#include <spi/public_inc/hal_spi.h>

Enumerator
SPI_MODE_MASTER 
SPI_MODE_SLAVE 
TOTAL_SPI_MODE 

◆ spi_result_t

#include <spi/public_inc/hal_spi.h>

Enumerator
SPI_RESULT_OK 

No error.

SPI_RESULT_INVALID 

Operation invalid.

SPI_RESULT_UNSUPPORTED 

Operation not supported.

SPI_RESULT_TIMEOUT 

Operation timeout.

SPI_RESULT_IDLE 

Bus idle.

SPI_RESULT_BUSY 

Bus busy.

SPI_RESULT_ERR 

Error occurred.

TOTAL_SPI_RESULT 

◆ spi_status_t

#include <spi/public_inc/hal_spi.h>

Enumerator
SPI_STATUS_UNKNOWN 

Unknown bus status.

SPI_STATUS_OK 

No error.

SPI_STATUS_BUSY 

Bus busy.

SPI_STATUS_STOP 

Channel stopped.

SPI_STATUS_SENDING 

Channel sending.

SPI_STATUS_RECEIVING 

Channel receiving.

SPI_STATUS_ERR 

Error occurred.

TOTAL_SPI_STATUS 

Function Documentation

◆ hal_spi_init()

spi_result_t hal_spi_init ( spi_handle_t * handle,
const spi_channel_attr_t attr )

#include <spi/public_inc/hal_spi.h>

Configure SPI channel with specific attributes.

spi_handle_t handle = {
.channel = BSP_SPI_CHANNEL_1
};
.mosi_pin = 10U,
.miso_pin = 11U,
.ssel_pin = 12U,
.sclk_pin = 13U,
.baudrate = 500000U,
.low_power_enable = false
};
spi_result_t result = hal_spi_init( &handle, attr );
spi_result_t hal_spi_init(spi_handle_t *handle, const spi_channel_attr_t attr)
Configure SPI channel with specific attributes.
spi_result_t
Definition hal_spi.h:39
@ SPI_MODE_MASTER
Definition hal_spi.h:65
Definition hal_spi.h:80
gpio_pin_t mosi_pin
Definition hal_spi.h:81
Definition hal_spi.h:92
spi_mode_t mode
Definition hal_spi.h:93
Parameters
[in,out]handleSPI handler
[in]attrSPI channel attributes
Return values
SPI_RESULT_OKSuccess configure SPI channel
SPI_RESULT_INVALIDConfiguration attributes invalid
SPI_RESULT_UNSUPPORTEDOperation not supported
SPI_RESULT_ERRFailed configure SPI channel

◆ hal_spi_mutex_give()

spi_result_t hal_spi_mutex_give ( const uint8_t channel)

#include <spi/public_inc/hal_spi.h>

Release RTOS mutex for specified channel.

spi_result_t result = hal_spi_mutex_give( BSP_SPI_CHANNEL_1 );
spi_result_t hal_spi_mutex_give(const uint8_t channel)
Release RTOS mutex for specified channel.
Parameters
[in]channelSPI channel's mutex to be released
Return values
SPI_RESULT_OKSuccess release mutex
SPI_RESULT_INVALIDInvalid channel
SPI_RESULT_UNSUPPORTEDOperation not supported
SPI_RESULT_ERRFailed release mutex

◆ hal_spi_mutex_take()

spi_result_t hal_spi_mutex_take ( const uint8_t channel,
const uint32_t timeout_ms )

#include <spi/public_inc/hal_spi.h>

Try acquire RTOS mutex for specified channel.

spi_result_t result = hal_spi_mutex_take( BSP_SPI_CHANNEL_1, 10U );
spi_result_t hal_spi_mutex_take(const uint8_t channel, const uint32_t timeout_ms)
Try acquire RTOS mutex for specified channel.
Parameters
[in]channelSPI channel's mutex to be locked
[in]timeout_msMutex acquiring timeout in milliseconds
Return values
SPI_RESULT_OKSuccess acquire mutex
SPI_RESULT_INVALIDInvalid channel
SPI_RESULT_UNSUPPORTEDOperation not supported
SPI_RESULT_ERRFailed acquire mutex

◆ hal_spi_read()

spi_result_t hal_spi_read ( spi_handle_t * handle,
uint8_t * rx_buffer,
const size_t len )

#include <spi/public_inc/hal_spi.h>

Read data from SPI bus.

uint8_t rx_buffer[3U] = { 0U };
spi_result_t result = hal_spi_read( &handle,
rx_buffer,
sizeof( rx_buffer ) );
spi_result_t hal_spi_read(spi_handle_t *handle, uint8_t *rx_buffer, const size_t len)
Read data from SPI bus.
Parameters
[in,out]handleSPI handler
[out]rx_bufferReceiving buffer
[in]lenBuffer size
Return values
SPI_RESULT_OKSuccess read data from SPI bus
SPI_RESULT_TIMEOUTRead data from SPI bus timeout
SPI_RESULT_BUSYNetwork busy
SPI_RESULT_ERRFailed read data from SPI bus

◆ hal_spi_read_write()

spi_result_t hal_spi_read_write ( spi_handle_t * handle,
const uint8_t * tx_buffer,
uint8_t * rx_buffer,
const size_t len )

#include <spi/public_inc/hal_spi.h>

Simultaneous read and write operation.

uint8_t rx_buffer[3U] = { 0U };
uint8_t tx_buffer[3U] = { 10U, 20U, 30U };
spi_result_t result = hal_spi_read_write( &handle,
tx_buffer,
rx_buffer,
sizeof( rx_buffer ) );
spi_result_t hal_spi_read_write(spi_handle_t *handle, const uint8_t *tx_buffer, uint8_t *rx_buffer, const size_t len)
Simultaneous read and write operation.
Note
Transmit and receive buffer length must be same.
Parameters
[in,out]handleSPI handler
[in]tx_bufferBuffer to be transmitted
[out]rx_bufferBuffer to be received
[in]lenBuffer size
Return values
SPI_RESULT_OKSuccess read/write data
SPI_RESULT_TIMEOUTWrite to SPI bus timeout
SPI_RESULT_BUSYNetwork busy
SPI_RESULT_ERRFailed write data to SPI bus

◆ hal_spi_stop()

spi_result_t hal_spi_stop ( spi_handle_t * handle)

#include <spi/public_inc/hal_spi.h>

Stop SPI operation.

spi_result_t result = hal_spi_stop( &handle );
spi_result_t hal_spi_stop(spi_handle_t *handle)
Stop SPI operation.
Parameters
[in,out]handleSPI handler
Return values
SPI_RESULT_OKSuccess stop SPI transaction
SPI_RESULT_UNSUPPORTEDOperation not supported
SPI_RESULT_ERRFailed stop SPI transaction

◆ hal_spi_write()

spi_result_t hal_spi_write ( spi_handle_t * handle,
const uint8_t * tx_buffer,
const size_t len )

#include <spi/public_inc/hal_spi.h>

Write data to SPI bus.

uint8_t tx_buffer[] = { 10U, 20U, 30U };
spi_result_t result = hal_spi_write( &handle,
tx_buffer,
sizeof( tx_buffer ) );
spi_result_t hal_spi_write(spi_handle_t *handle, const uint8_t *tx_buffer, const size_t len)
Write data to SPI bus.
Parameters
[in,out]handleSPI handler
[in]tx_bufferBuffer to be transmitted
[in]lenBuffer size
Return values
SPI_RESULT_OKSuccess write data to SPI bus
SPI_RESULT_TIMEOUTWrite to SPI bus timeout
SPI_RESULT_BUSYNetwork busy
SPI_RESULT_ERRFailed write data to SPI bus