Restructure the locking around SPI transfers

This commit is contained in:
px4dev 2014-07-06 11:40:28 -07:00
parent bd88951f6c
commit ef6c99c1ab
2 changed files with 35 additions and 35 deletions

View File

@ -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;
/* lock the bus as required */ LockMode mode = up_interrupt_context() ? LOCK_NONE : locking_mode;
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;
}
}
/* lock the bus as required */
switch (mode) {
default:
case LOCK_PREEMPTION:
{
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_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

View File

@ -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