SPI: refactored spiDevice data structure, added two utility functions

This commit is contained in:
Silvano Seva 2024-09-11 21:26:07 +02:00
parent 83f4e1584a
commit ac7e176d3b
1 changed files with 27 additions and 6 deletions

View File

@ -59,11 +59,32 @@ typedef int (*spi_transfer_impl)(const struct spiDevice *dev, const void *txBuf,
*/ */
struct spiDevice struct spiDevice
{ {
const spi_transfer_impl transfer; ///< Device-specific implementation of the SPI transfer function spi_transfer_impl transfer; ///< Device-specific implementation of the SPI transfer function
const void *priv; ///< Pointer to device driver private data const void *priv; ///< Pointer to device driver private data
const pthread_mutex_t *mutex; ///< Pointer to mutex for exclusive access pthread_mutex_t *mutex; ///< Pointer to mutex for exclusive access
}; };
/**
* Perform basic initialization of a generic SPI device.
*
* @param dev: pointer to peripheral descriptor.
*/
static inline void spi_init(const struct spiDevice *dev)
{
if(dev->mutex != NULL)
pthread_mutex_init(dev->mutex, NULL);
}
/**
* Perform basic initialization of a generic SPI device.
*
* @param dev: pointer to peripheral descriptor.
*/
static inline void spi_terminate(const struct spiDevice *dev)
{
if(dev->mutex != NULL)
pthread_mutex_destroy(dev->mutex);
}
/** /**
* Acquire exclusive ownership on an SPI peripheral. * Acquire exclusive ownership on an SPI peripheral.
@ -76,7 +97,7 @@ static inline int spi_acquire(const struct spiDevice *dev)
if(dev->mutex == NULL) if(dev->mutex == NULL)
return 0; return 0;
return pthread_mutex_lock((pthread_mutex_t *) dev->mutex); return pthread_mutex_lock(dev->mutex);
} }
/** /**
@ -90,7 +111,7 @@ static inline int spi_tryAcquire(const struct spiDevice *dev)
if(dev->mutex == NULL) if(dev->mutex == NULL)
return 0; return 0;
return pthread_mutex_trylock((pthread_mutex_t *) dev->mutex); return pthread_mutex_trylock(dev->mutex);
} }
/** /**
@ -104,7 +125,7 @@ static inline int spi_release(const struct spiDevice *dev)
if(dev->mutex == NULL) if(dev->mutex == NULL)
return 0; return 0;
return pthread_mutex_unlock((pthread_mutex_t *) dev->mutex); return pthread_mutex_unlock(dev->mutex);
} }
/** /**