forked from Archive/PX4-Autopilot
Restructure the locking around SPI transfers
This commit is contained in:
parent
bd88951f6c
commit
ef6c99c1ab
|
@ -133,26 +133,44 @@ SPI::probe()
|
|||
int
|
||||
SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
|
||||
{
|
||||
irqstate_t state;
|
||||
int result;
|
||||
|
||||
if ((send == nullptr) && (recv == nullptr))
|
||||
return -EINVAL;
|
||||
|
||||
LockMode mode = up_interrupt_context() ? LOCK_NONE : locking_mode;
|
||||
|
||||
/* lock the bus as required */
|
||||
if (!up_interrupt_context()) {
|
||||
switch (locking_mode) {
|
||||
switch (mode) {
|
||||
default:
|
||||
case LOCK_PREEMPTION:
|
||||
state = irqsave();
|
||||
{
|
||||
irqstate_t state = irqsave();
|
||||
result = _transfer(send, recv, len);
|
||||
irqrestore(state);
|
||||
}
|
||||
break;
|
||||
case LOCK_THREADS:
|
||||
SPI_LOCK(_dev, true);
|
||||
result = _transfer(send, recv, len);
|
||||
SPI_LOCK(_dev, false);
|
||||
break;
|
||||
case LOCK_NONE:
|
||||
result = _transfer(send, recv, len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
SPI::set_frequency(uint32_t frequency)
|
||||
{
|
||||
_frequency = frequency;
|
||||
}
|
||||
|
||||
int
|
||||
SPI::_transfer(uint8_t *send, uint8_t *recv, unsigned len)
|
||||
{
|
||||
SPI_SETFREQUENCY(_dev, _frequency);
|
||||
SPI_SETMODE(_dev, _mode);
|
||||
SPI_SETBITS(_dev, 8);
|
||||
|
@ -164,27 +182,7 @@ SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
|
|||
/* and clean up */
|
||||
SPI_SELECT(_dev, _device, 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;
|
||||
}
|
||||
|
||||
void
|
||||
SPI::set_frequency(uint32_t frequency)
|
||||
{
|
||||
_frequency = frequency;
|
||||
}
|
||||
|
||||
} // namespace device
|
||||
|
|
|
@ -129,6 +129,8 @@ private:
|
|||
enum spi_mode_e _mode;
|
||||
uint32_t _frequency;
|
||||
struct spi_dev_s *_dev;
|
||||
|
||||
int _transfer(uint8_t *send, uint8_t *recv, unsigned len);
|
||||
};
|
||||
|
||||
} // namespace device
|
||||
|
|
Loading…
Reference in New Issue