2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-12 18:01:40 -04:00
|
|
|
#if (CONFIG_HAL_BOARD == HAL_BOARD_APM1 || CONFIG_HAL_BOARD == HAL_BOARD_APM2)
|
2012-11-28 21:57:20 -04:00
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-11-28 21:57:20 -04:00
|
|
|
#include "SPIDevices.h"
|
|
|
|
#include "GPIO.h"
|
2013-01-02 03:22:07 -04:00
|
|
|
#include "Semaphores.h"
|
2013-05-15 15:00:13 -03:00
|
|
|
#include "utility/pins_arduino_mega.h"
|
2012-11-28 21:57:20 -04:00
|
|
|
using namespace AP_HAL_AVR;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#define SPI0_MISO_PIN 50
|
|
|
|
#define SPI0_MOSI_PIN 51
|
|
|
|
#define SPI0_SCK_PIN 52
|
|
|
|
|
|
|
|
AVRSemaphore AVRSPI0DeviceDriver::_semaphore;
|
2013-11-15 02:45:02 -04:00
|
|
|
bool AVRSPI0DeviceDriver::_force_low_speed;
|
2012-11-28 21:57:20 -04:00
|
|
|
|
2012-12-05 21:15:49 -04:00
|
|
|
static volatile bool spi0_transferflag = false;
|
|
|
|
|
2012-11-28 21:57:20 -04:00
|
|
|
void AVRSPI0DeviceDriver::init() {
|
2014-06-01 20:26:19 -03:00
|
|
|
hal.gpio->pinMode(SPI0_MISO_PIN, HAL_GPIO_INPUT);
|
|
|
|
hal.gpio->pinMode(SPI0_MOSI_PIN, HAL_GPIO_OUTPUT);
|
|
|
|
hal.gpio->pinMode(SPI0_SCK_PIN, HAL_GPIO_OUTPUT);
|
2012-11-28 21:57:20 -04:00
|
|
|
|
2014-06-01 20:26:19 -03:00
|
|
|
_cs_pin->mode(HAL_GPIO_OUTPUT);
|
2012-11-28 21:57:20 -04:00
|
|
|
_cs_pin->write(1);
|
|
|
|
|
|
|
|
/* Enable the SPI0 peripheral as a master */
|
|
|
|
SPCR = _BV(SPE) | _BV(MSTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL::Semaphore* AVRSPI0DeviceDriver::get_semaphore() {
|
|
|
|
return &_semaphore;
|
|
|
|
}
|
|
|
|
|
2013-10-11 05:33:47 -03:00
|
|
|
void AVRSPI0DeviceDriver::_cs_assert()
|
|
|
|
{
|
2012-11-28 21:57:20 -04:00
|
|
|
const uint8_t valid_spcr_mask =
|
|
|
|
(_BV(CPOL) | _BV(CPHA) | _BV(SPR1) | _BV(SPR0));
|
2013-11-15 02:45:02 -04:00
|
|
|
if (_force_low_speed) {
|
|
|
|
_spcr = _spcr_lowspeed;
|
|
|
|
}
|
2013-10-11 05:33:47 -03:00
|
|
|
uint8_t new_spcr = (SPCR & ~valid_spcr_mask) | (_spcr & valid_spcr_mask);
|
2012-11-28 21:57:20 -04:00
|
|
|
SPCR = new_spcr;
|
|
|
|
|
|
|
|
const uint8_t valid_spsr_mask = _BV(SPI2X);
|
2013-10-11 05:33:47 -03:00
|
|
|
uint8_t new_spsr = (SPSR & ~valid_spsr_mask) | (_spsr & valid_spsr_mask);
|
2012-11-28 21:57:20 -04:00
|
|
|
SPSR = new_spsr;
|
|
|
|
|
|
|
|
_cs_pin->write(0);
|
|
|
|
}
|
|
|
|
|
2013-10-11 05:33:47 -03:00
|
|
|
void AVRSPI0DeviceDriver::_cs_release()
|
|
|
|
{
|
2012-11-28 21:57:20 -04:00
|
|
|
_cs_pin->write(1);
|
|
|
|
}
|
|
|
|
|
2013-10-11 05:33:47 -03:00
|
|
|
uint8_t AVRSPI0DeviceDriver::_transfer(uint8_t data)
|
|
|
|
{
|
2012-12-05 21:15:49 -04:00
|
|
|
if (spi0_transferflag) {
|
2012-12-17 20:28:33 -04:00
|
|
|
hal.scheduler->panic(PSTR("PANIC: SPI0 transfer collision"));
|
2012-12-05 21:15:49 -04:00
|
|
|
}
|
|
|
|
spi0_transferflag = true;
|
2012-11-28 21:57:20 -04:00
|
|
|
SPDR = data;
|
|
|
|
if (SPSR & _BV(WCOL)) {
|
2012-12-17 20:28:33 -04:00
|
|
|
hal.scheduler->panic(PSTR("PANIC: SPI0 write collision"));
|
2012-11-28 21:57:20 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while(!(SPSR & _BV(SPIF)));
|
2012-12-05 21:15:49 -04:00
|
|
|
uint8_t read_spdr = SPDR;
|
|
|
|
spi0_transferflag = false;
|
|
|
|
return read_spdr;
|
2012-11-28 21:57:20 -04:00
|
|
|
}
|
|
|
|
|
2013-10-10 07:13:24 -03:00
|
|
|
/**
|
|
|
|
a specialised transfer function for the MPU6k. This saves 2 usec
|
|
|
|
per byte
|
|
|
|
*/
|
2013-10-28 04:20:20 -03:00
|
|
|
void AVRSPI0DeviceDriver::_transfer16(const uint8_t *tx, uint8_t *rx)
|
2013-10-10 07:13:24 -03:00
|
|
|
{
|
|
|
|
spi0_transferflag = true;
|
|
|
|
#define TRANSFER1(i) do { SPDR = tx[i]; while(!(SPSR & _BV(SPIF))); rx[i] = SPDR; } while(0)
|
|
|
|
TRANSFER1(0);
|
|
|
|
TRANSFER1(1);
|
|
|
|
TRANSFER1(2);
|
|
|
|
TRANSFER1(3);
|
|
|
|
TRANSFER1(4);
|
|
|
|
TRANSFER1(5);
|
|
|
|
TRANSFER1(6);
|
|
|
|
TRANSFER1(7);
|
|
|
|
TRANSFER1(8);
|
|
|
|
TRANSFER1(9);
|
|
|
|
TRANSFER1(10);
|
|
|
|
TRANSFER1(11);
|
|
|
|
TRANSFER1(12);
|
|
|
|
TRANSFER1(13);
|
|
|
|
TRANSFER1(14);
|
2013-10-28 04:20:20 -03:00
|
|
|
TRANSFER1(15);
|
2013-10-10 07:13:24 -03:00
|
|
|
spi0_transferflag = false;
|
|
|
|
}
|
|
|
|
|
2013-01-12 07:13:04 -04:00
|
|
|
void AVRSPI0DeviceDriver::transfer(const uint8_t *tx, uint16_t len) {
|
|
|
|
for (uint16_t i = 0; i < len; i++) {
|
|
|
|
_transfer(tx[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 12:39:16 -03:00
|
|
|
bool AVRSPI0DeviceDriver::transaction(const uint8_t *tx, uint8_t *rx,
|
2012-12-17 21:07:06 -04:00
|
|
|
uint16_t len) {
|
|
|
|
_cs_assert();
|
2012-12-17 21:59:46 -04:00
|
|
|
if (rx == NULL) {
|
|
|
|
for (uint16_t i = 0; i < len; i++) {
|
|
|
|
_transfer(tx[i]);
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-28 04:20:20 -03:00
|
|
|
while (len >= 16) {
|
|
|
|
_transfer16(tx, rx);
|
|
|
|
tx += 16;
|
|
|
|
rx += 16;
|
|
|
|
len -= 16;
|
2013-10-10 07:13:24 -03:00
|
|
|
}
|
2012-12-17 21:59:46 -04:00
|
|
|
for (uint16_t i = 0; i < len; i++) {
|
|
|
|
rx[i] = _transfer(tx[i]);
|
|
|
|
}
|
2012-12-17 21:07:06 -04:00
|
|
|
}
|
|
|
|
_cs_release();
|
2015-08-19 12:39:16 -03:00
|
|
|
return true;
|
2012-12-17 21:07:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void AVRSPI0DeviceDriver::cs_assert() {
|
|
|
|
_cs_assert();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVRSPI0DeviceDriver::cs_release() {
|
|
|
|
_cs_release();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AVRSPI0DeviceDriver::transfer(uint8_t data) {
|
|
|
|
return _transfer(data);
|
|
|
|
}
|
|
|
|
|
2013-10-11 04:01:42 -03:00
|
|
|
/**
|
|
|
|
allow on the fly bus speed changes for MPU6000
|
|
|
|
*/
|
|
|
|
void AVRSPI0DeviceDriver::set_bus_speed(AVRSPI0DeviceDriver::bus_speed speed)
|
|
|
|
{
|
|
|
|
if (speed == AVRSPI0DeviceDriver::SPI_SPEED_HIGH) {
|
|
|
|
_spcr = _spcr_highspeed;
|
2013-11-15 02:45:02 -04:00
|
|
|
_force_low_speed = false;
|
2013-10-11 04:01:42 -03:00
|
|
|
} else {
|
|
|
|
_spcr = _spcr_lowspeed;
|
2013-11-15 02:45:02 -04:00
|
|
|
_force_low_speed = true;
|
2013-10-11 04:01:42 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 18:01:40 -04:00
|
|
|
#endif
|