From 5999a468ac68be56c2ee95b82e5715e4e7b7bb7e Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 15 Nov 2013 17:45:02 +1100 Subject: [PATCH] HAL_AVR: force all devices on SPI0 to low speed when one is low speed this forces MS5611 to low speed when MPU6K is low speed --- libraries/AP_HAL_AVR/SPIDevice_SPI0.cpp | 6 ++++++ libraries/AP_HAL_AVR/SPIDevices.h | 1 + 2 files changed, 7 insertions(+) diff --git a/libraries/AP_HAL_AVR/SPIDevice_SPI0.cpp b/libraries/AP_HAL_AVR/SPIDevice_SPI0.cpp index e52b95421c..4cc2afcefe 100644 --- a/libraries/AP_HAL_AVR/SPIDevice_SPI0.cpp +++ b/libraries/AP_HAL_AVR/SPIDevice_SPI0.cpp @@ -17,6 +17,7 @@ extern const AP_HAL::HAL& hal; #define SPI0_SCK_PIN 52 AVRSemaphore AVRSPI0DeviceDriver::_semaphore; +bool AVRSPI0DeviceDriver::_force_low_speed; static volatile bool spi0_transferflag = false; @@ -40,6 +41,9 @@ void AVRSPI0DeviceDriver::_cs_assert() { const uint8_t valid_spcr_mask = (_BV(CPOL) | _BV(CPHA) | _BV(SPR1) | _BV(SPR0)); + if (_force_low_speed) { + _spcr = _spcr_lowspeed; + } uint8_t new_spcr = (SPCR & ~valid_spcr_mask) | (_spcr & valid_spcr_mask); SPCR = new_spcr; @@ -145,8 +149,10 @@ void AVRSPI0DeviceDriver::set_bus_speed(AVRSPI0DeviceDriver::bus_speed speed) { if (speed == AVRSPI0DeviceDriver::SPI_SPEED_HIGH) { _spcr = _spcr_highspeed; + _force_low_speed = false; } else { _spcr = _spcr_lowspeed; + _force_low_speed = true; } } diff --git a/libraries/AP_HAL_AVR/SPIDevices.h b/libraries/AP_HAL_AVR/SPIDevices.h index 021fd645ce..f28059c242 100644 --- a/libraries/AP_HAL_AVR/SPIDevices.h +++ b/libraries/AP_HAL_AVR/SPIDevices.h @@ -39,6 +39,7 @@ private: void _transfer16(const uint8_t *tx, uint8_t *rx); static AP_HAL_AVR::AVRSemaphore _semaphore; + static bool _force_low_speed; AP_HAL_AVR::AVRDigitalSource *_cs_pin; const uint8_t _spcr_lowspeed;