mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-26 02:53:34 +08:00
Use the generic device::SPI locking strategy.
This commit is contained in:
@@ -67,6 +67,7 @@ SPI::SPI(const char *name,
|
||||
CDev(name, devname, irq),
|
||||
// public
|
||||
// protected
|
||||
locking_mode(LOCK_PREEMPTION),
|
||||
// private
|
||||
_bus(bus),
|
||||
_device(device),
|
||||
@@ -132,13 +133,25 @@ SPI::probe()
|
||||
int
|
||||
SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
|
||||
{
|
||||
irqstate_t state;
|
||||
|
||||
if ((send == nullptr) && (recv == nullptr))
|
||||
return -EINVAL;
|
||||
|
||||
/* do common setup */
|
||||
if (!up_interrupt_context())
|
||||
SPI_LOCK(_dev, true);
|
||||
/* lock the bus as required */
|
||||
if (!up_interrupt_context()) {
|
||||
switch (locking_mode) {
|
||||
default:
|
||||
case LOCK_PREEMPTION:
|
||||
state = irqsave();
|
||||
break;
|
||||
case LOCK_THREADS:
|
||||
SPI_LOCK(_dev, true);
|
||||
break;
|
||||
case LOCK_NONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SPI_SETFREQUENCY(_dev, _frequency);
|
||||
SPI_SETMODE(_dev, _mode);
|
||||
@@ -151,8 +164,19 @@ SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
|
||||
/* and clean up */
|
||||
SPI_SELECT(_dev, _device, false);
|
||||
|
||||
if (!up_interrupt_context())
|
||||
SPI_LOCK(_dev, false);
|
||||
if (!up_interrupt_context()) {
|
||||
switch (locking_mode) {
|
||||
default:
|
||||
case LOCK_PREEMPTION:
|
||||
irqrestore(state);
|
||||
break;
|
||||
case LOCK_THREADS:
|
||||
SPI_LOCK(_dev, false);
|
||||
break;
|
||||
case LOCK_NONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -101,6 +101,17 @@ protected:
|
||||
*/
|
||||
int transfer(uint8_t *send, uint8_t *recv, unsigned len);
|
||||
|
||||
/**
|
||||
* Locking modes supported by the driver.
|
||||
*/
|
||||
enum LockMode {
|
||||
LOCK_PREEMPTION, /**< the default; lock against all forms of preemption. */
|
||||
LOCK_THREADS, /**< lock only against other threads, using SPI_LOCK */
|
||||
LOCK_NONE /**< perform no locking, only safe if the bus is entirely private */
|
||||
};
|
||||
|
||||
LockMode locking_mode; /**< selected locking mode */
|
||||
|
||||
private:
|
||||
int _bus;
|
||||
enum spi_dev_e _device;
|
||||
|
||||
Reference in New Issue
Block a user