SPI HAL API: lock()/open() and close()/unlock() merged
This commit is contained in:
parent
2cad626360
commit
d7baa54bd0
@ -147,18 +147,6 @@ spix_wait_eorx(spi_device_t *dev)
|
|||||||
while(!(REG(spi_regs[dev->spi_controller].base + SSI_SR) & SSI_SR_RNE));
|
while(!(REG(spi_regs[dev->spi_controller].base + SSI_SR) & SSI_SR_RNE));
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
|
||||||
spi_arch_lock(spi_device_t *dev)
|
|
||||||
{
|
|
||||||
if(mutex_try_lock(&board_spi_locks_spi[dev->spi_controller].lock) == false) {
|
|
||||||
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
||||||
}
|
|
||||||
|
|
||||||
board_spi_locks_spi[dev->spi_controller].owner = dev;
|
|
||||||
|
|
||||||
return SPI_DEV_STATUS_OK;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
bool
|
bool
|
||||||
spi_arch_has_lock(spi_device_t *dev)
|
spi_arch_has_lock(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
@ -180,20 +168,7 @@ spi_arch_is_bus_locked(spi_device_t *dev)
|
|||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
spi_status_t
|
||||||
spi_arch_unlock(spi_device_t *dev)
|
spi_arch_lock_and_open(spi_device_t *dev)
|
||||||
{
|
|
||||||
if(!spi_arch_has_lock(dev)) {
|
|
||||||
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
|
||||||
}
|
|
||||||
|
|
||||||
board_spi_locks_spi[dev->spi_controller].owner = NULL;
|
|
||||||
mutex_unlock(&board_spi_locks_spi[dev->spi_controller].lock);
|
|
||||||
|
|
||||||
return SPI_DEV_STATUS_OK;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
spi_status_t
|
|
||||||
spi_arch_open(spi_device_t *dev)
|
|
||||||
{
|
{
|
||||||
const spi_regs_t *regs;
|
const spi_regs_t *regs;
|
||||||
uint32_t scr;
|
uint32_t scr;
|
||||||
@ -213,10 +188,13 @@ spi_arch_open(spi_device_t *dev)
|
|||||||
|
|
||||||
uint32_t mode = 0;
|
uint32_t mode = 0;
|
||||||
|
|
||||||
if(!spi_arch_has_lock(dev)) {
|
/* lock the SPI bus */
|
||||||
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
if(mutex_try_lock(&board_spi_locks_spi[dev->spi_controller].lock) == false) {
|
||||||
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
board_spi_locks_spi[dev->spi_controller].owner = dev;
|
||||||
|
|
||||||
/* Set SPI phase */
|
/* Set SPI phase */
|
||||||
if(dev->spi_pha != 0) {
|
if(dev->spi_pha != 0) {
|
||||||
mode = mode | SSI_CR0_SPH;
|
mode = mode | SSI_CR0_SPH;
|
||||||
@ -287,7 +265,7 @@ spi_arch_open(spi_device_t *dev)
|
|||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
spi_status_t
|
||||||
spi_arch_close(spi_device_t *dev)
|
spi_arch_close_and_unlock(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
if(!spi_arch_has_lock(dev)) {
|
if(!spi_arch_has_lock(dev)) {
|
||||||
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
||||||
@ -296,6 +274,10 @@ spi_arch_close(spi_device_t *dev)
|
|||||||
/* Disable SSI */
|
/* Disable SSI */
|
||||||
REG(SYS_CTRL_RCGCSSI) &= ~(1 << dev->spi_controller);
|
REG(SYS_CTRL_RCGCSSI) &= ~(1 << dev->spi_controller);
|
||||||
|
|
||||||
|
/* Unlock the SPI bus */
|
||||||
|
board_spi_locks_spi[dev->spi_controller].owner = NULL;
|
||||||
|
mutex_unlock(&board_spi_locks_spi[dev->spi_controller].lock);
|
||||||
|
|
||||||
return SPI_DEV_STATUS_OK;
|
return SPI_DEV_STATUS_OK;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
@ -67,18 +67,6 @@ static const board_spi_controller_t spi_controller[SPI_CONTROLLER_COUNT] = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
|
||||||
spi_arch_lock(spi_device_t *dev)
|
|
||||||
{
|
|
||||||
if(mutex_try_lock(&board_spi_locks_spi[dev->spi_controller].lock) == false) {
|
|
||||||
return SPI_DEV_STATUS_BUS_LOCKED;
|
|
||||||
}
|
|
||||||
|
|
||||||
board_spi_locks_spi[dev->spi_controller].owner = dev;
|
|
||||||
|
|
||||||
return SPI_DEV_STATUS_OK;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
bool
|
bool
|
||||||
spi_arch_has_lock(spi_device_t *dev)
|
spi_arch_has_lock(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
@ -99,19 +87,6 @@ spi_arch_is_bus_locked(spi_device_t *dev)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
|
||||||
spi_arch_unlock(spi_device_t *dev)
|
|
||||||
{
|
|
||||||
if(!spi_arch_has_lock(dev)) {
|
|
||||||
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
|
||||||
}
|
|
||||||
|
|
||||||
board_spi_locks_spi[dev->spi_controller].owner = NULL;
|
|
||||||
mutex_unlock(&board_spi_locks_spi[dev->spi_controller].lock);
|
|
||||||
|
|
||||||
return SPI_DEV_STATUS_OK;
|
|
||||||
}
|
|
||||||
/*---------------------------------------------------------------------------*/
|
|
||||||
static uint32_t
|
static uint32_t
|
||||||
get_mode(spi_device_t *dev)
|
get_mode(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
@ -128,14 +103,17 @@ get_mode(spi_device_t *dev)
|
|||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
spi_status_t
|
||||||
spi_arch_open(spi_device_t *dev)
|
spi_arch_lock_and_open(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
uint32_t c;
|
uint32_t c;
|
||||||
|
|
||||||
if(!spi_arch_has_lock(dev)) {
|
/* Lock the SPI bus */
|
||||||
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
if(mutex_try_lock(&board_spi_locks_spi[dev->spi_controller].lock) == false) {
|
||||||
|
return SPI_DEV_STATUS_BUS_LOCKED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
board_spi_locks_spi[dev->spi_controller].owner = dev;
|
||||||
|
|
||||||
/* CS pin configuration */
|
/* CS pin configuration */
|
||||||
ti_lib_ioc_pin_type_gpio_output(dev->pin_spi_cs);
|
ti_lib_ioc_pin_type_gpio_output(dev->pin_spi_cs);
|
||||||
|
|
||||||
@ -166,7 +144,7 @@ spi_arch_open(spi_device_t *dev)
|
|||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
spi_status_t
|
||||||
spi_arch_close(spi_device_t *dev)
|
spi_arch_close_and_unlock(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
if(!spi_arch_has_lock(dev)) {
|
if(!spi_arch_has_lock(dev)) {
|
||||||
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
return SPI_DEV_STATUS_BUS_NOT_OWNED;
|
||||||
@ -187,6 +165,10 @@ spi_arch_close(spi_device_t *dev)
|
|||||||
ti_lib_ioc_pin_type_gpio_input(dev->pin_spi_sck);
|
ti_lib_ioc_pin_type_gpio_input(dev->pin_spi_sck);
|
||||||
ti_lib_ioc_io_port_pull_set(dev->pin_spi_sck, IOC_IOPULL_DOWN);
|
ti_lib_ioc_io_port_pull_set(dev->pin_spi_sck, IOC_IOPULL_DOWN);
|
||||||
|
|
||||||
|
/* Unlock the SPI bus */
|
||||||
|
board_spi_locks_spi[dev->spi_controller].owner = NULL;
|
||||||
|
mutex_unlock(&board_spi_locks_spi[dev->spi_controller].lock);
|
||||||
|
|
||||||
return SPI_DEV_STATUS_OK;
|
return SPI_DEV_STATUS_OK;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
24
os/dev/spi.c
24
os/dev/spi.c
@ -45,39 +45,23 @@
|
|||||||
spi_status_t
|
spi_status_t
|
||||||
spi_acquire(spi_device_t *dev)
|
spi_acquire(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
spi_status_t r;
|
|
||||||
|
|
||||||
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
||||||
return SPI_DEV_STATUS_EINVAL;
|
return SPI_DEV_STATUS_EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* lock the bus */
|
/* lock and open the bus */
|
||||||
r = spi_arch_lock(dev);
|
return spi_arch_lock_and_open(dev);
|
||||||
if(r != SPI_DEV_STATUS_OK) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* open the bus */
|
|
||||||
return spi_arch_open(dev);
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
spi_status_t
|
||||||
spi_release(spi_device_t *dev)
|
spi_release(spi_device_t *dev)
|
||||||
{
|
{
|
||||||
spi_status_t r;
|
|
||||||
|
|
||||||
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
if(dev == NULL || dev->spi_controller >= SPI_CONTROLLER_COUNT) {
|
||||||
return SPI_DEV_STATUS_EINVAL;
|
return SPI_DEV_STATUS_EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* unlock the bus */
|
/* close and unlock the bus */
|
||||||
r = spi_arch_close(dev);
|
return spi_arch_close_and_unlock(dev);
|
||||||
if(r != SPI_DEV_STATUS_OK) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* unlock the bus */
|
|
||||||
return spi_arch_unlock(dev);
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
spi_status_t
|
spi_status_t
|
||||||
|
29
os/dev/spi.h
29
os/dev/spi.h
@ -258,24 +258,6 @@ spi_status_t spi_read_register(spi_device_t *dev, uint8_t reg,
|
|||||||
/* These are architecture-specific functions to be implemented by each CPU. */
|
/* These are architecture-specific functions to be implemented by each CPU. */
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Locks an SPI controller to device dev.
|
|
||||||
* \param dev An SPI device configuration which defines the controller
|
|
||||||
* to be locked and the device that locks it.
|
|
||||||
* \return SPI return code
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
spi_status_t spi_arch_lock(spi_device_t *dev);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Unlocks an SPI controller.
|
|
||||||
* \param dev An SPI device configuration which defines the controller
|
|
||||||
* to be unlocked and the device that unlocks it.
|
|
||||||
* \return SPI return code
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
spi_status_t spi_arch_unlock(spi_device_t *dev);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Checks if a device has locked an SPI controller
|
* \brief Checks if a device has locked an SPI controller
|
||||||
* \param dev An SPI device configuration which defines the controller
|
* \param dev An SPI device configuration which defines the controller
|
||||||
@ -295,7 +277,7 @@ bool spi_arch_has_lock(spi_device_t *dev);
|
|||||||
bool spi_arch_is_bus_locked(spi_device_t *dev);
|
bool spi_arch_is_bus_locked(spi_device_t *dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Opens an SPI controller to the configuration specified.
|
* \brief Locks and opens an SPI controller to the configuration specified.
|
||||||
* \param dev An SPI device configuration.
|
* \param dev An SPI device configuration.
|
||||||
* \return SPI return code
|
* \return SPI return code
|
||||||
*
|
*
|
||||||
@ -303,19 +285,20 @@ bool spi_arch_is_bus_locked(spi_device_t *dev);
|
|||||||
* controller.
|
* controller.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
spi_status_t spi_arch_open(spi_device_t *dev);
|
spi_status_t spi_arch_lock_and_open(spi_device_t *dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Closes an SPI controller
|
* \brief Closes and unlocks an SPI controller
|
||||||
* \param dev An SPI device configuration that specifies the controller.
|
* \param dev An SPI device configuration that specifies the controller.
|
||||||
* \return SPI return code
|
* \return SPI return code
|
||||||
*
|
*
|
||||||
* This should turn off the SPI controller to put it in low power mode.
|
* This should turn off the SPI controller to put it in low power mode
|
||||||
|
* and unlock it.
|
||||||
* It should work only if the device has already locked the SPI
|
* It should work only if the device has already locked the SPI
|
||||||
* controller.
|
* controller.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
spi_status_t spi_arch_close(spi_device_t *dev);
|
spi_status_t spi_arch_close_and_unlock(spi_device_t *dev);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Performs an SPI transfer
|
* \brief Performs an SPI transfer
|
||||||
|
Loading…
Reference in New Issue
Block a user