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