2015-07-02 12:02:02 -03:00
|
|
|
#include <AP_Common.h>
|
2013-09-28 11:51:43 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2013-09-28 11:51:43 -03:00
|
|
|
#include "SPIDriver.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2014-11-25 19:16:39 -04:00
|
|
|
#include <errno.h>
|
2013-09-28 11:51:43 -03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/spi/spidev.h>
|
2014-06-03 05:59:39 -03:00
|
|
|
#include "GPIO.h"
|
2013-09-28 11:51:43 -03:00
|
|
|
|
2014-11-25 19:16:39 -04:00
|
|
|
#define SPI_DEBUGGING 0
|
2014-06-05 07:27:03 -03:00
|
|
|
|
2013-09-28 11:51:43 -03:00
|
|
|
using namespace Linux;
|
|
|
|
|
2014-06-03 05:59:39 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2014-07-05 08:14:11 -03:00
|
|
|
#define MHZ (1000U*1000U)
|
2015-04-01 10:02:54 -03:00
|
|
|
#define KHZ (1000U)
|
2014-07-05 08:14:11 -03:00
|
|
|
|
2014-07-13 19:44:59 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLE
|
2015-07-02 12:02:02 -03:00
|
|
|
LinuxSPIDeviceDriver LinuxSPIDeviceManager::_device[] = {
|
2014-07-13 19:44:59 -03:00
|
|
|
// different SPI tables per board subtype
|
2014-10-14 21:14:03 -03:00
|
|
|
LinuxSPIDeviceDriver(1, 0, AP_HAL::SPIDevice_LSM9DS0_AM, SPI_MODE_3, 8, BBB_P9_17, 10*MHZ,10*MHZ),
|
|
|
|
LinuxSPIDeviceDriver(1, 0, AP_HAL::SPIDevice_LSM9DS0_G, SPI_MODE_3, 8, BBB_P8_9, 10*MHZ,10*MHZ),
|
|
|
|
LinuxSPIDeviceDriver(2, 0, AP_HAL::SPIDevice_MS5611, SPI_MODE_3, 8, BBB_P9_42, 10*MHZ,10*MHZ),
|
|
|
|
LinuxSPIDeviceDriver(2, 0, AP_HAL::SPIDevice_MPU6000, SPI_MODE_3, 8, BBB_P9_28, 500*1000, 20*MHZ),
|
2014-07-07 05:32:38 -03:00
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2014-10-14 21:14:03 -03:00
|
|
|
LinuxSPIDeviceDriver(2, 0, AP_HAL::SPIDevice_MPU9250, SPI_MODE_3, 8, BBB_P9_23, 1*MHZ, 20*MHZ),
|
|
|
|
LinuxSPIDeviceDriver(2, 0, AP_HAL::SPIDevice_Dataflash, SPI_MODE_3, 8, BBB_P8_12, 6*MHZ, 6*MHZ),
|
2014-08-12 06:59:21 -03:00
|
|
|
};
|
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO
|
2015-07-02 12:02:02 -03:00
|
|
|
LinuxSPIDeviceDriver LinuxSPIDeviceManager::_device[] = {
|
2014-08-12 06:59:21 -03:00
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2015-07-09 12:34:49 -03:00
|
|
|
LinuxSPIDeviceDriver(0, 1, AP_HAL::SPIDevice_MPU9250, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 20*MHZ),
|
|
|
|
LinuxSPIDeviceDriver(0, 0, AP_HAL::SPIDevice_Ublox, SPI_MODE_0, 8, SPI_CS_KERNEL, 250*KHZ, 5*MHZ),
|
2014-06-27 06:38:32 -03:00
|
|
|
};
|
2015-01-10 19:20:38 -04:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
|
2015-07-02 12:02:02 -03:00
|
|
|
LinuxSPIDeviceDriver LinuxSPIDeviceManager::_device[] = {
|
2015-01-10 19:20:38 -04:00
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2015-02-27 16:59:56 -04:00
|
|
|
LinuxSPIDeviceDriver(2, 0, AP_HAL::SPIDevice_MPU9250, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 20*MHZ),
|
|
|
|
LinuxSPIDeviceDriver(2, 1, AP_HAL::SPIDevice_MS5611, SPI_MODE_3, 8, SPI_CS_KERNEL, 10*MHZ,10*MHZ),
|
2015-01-10 19:20:38 -04:00
|
|
|
};
|
2014-07-13 19:44:59 -03:00
|
|
|
#else
|
|
|
|
// empty device table
|
|
|
|
LinuxSPIDeviceDriver LinuxSPIDeviceManager::_device[0];
|
|
|
|
#endif
|
2014-06-27 06:38:32 -03:00
|
|
|
|
2015-07-02 12:02:02 -03:00
|
|
|
#define LINUX_SPI_DEVICE_NUM_DEVICES ARRAY_SIZE(LinuxSPIDeviceManager::_device)
|
|
|
|
|
2014-06-27 06:38:32 -03:00
|
|
|
// have a separate semaphore per bus
|
2014-08-17 23:32:57 -03:00
|
|
|
LinuxSemaphore LinuxSPIDeviceManager::_semaphore[LINUX_SPI_MAX_BUSES];
|
2014-06-27 06:38:32 -03:00
|
|
|
|
2014-10-14 21:14:03 -03:00
|
|
|
LinuxSPIDeviceDriver::LinuxSPIDeviceDriver(uint16_t bus, uint16_t subdev, enum AP_HAL::SPIDevice type, uint8_t mode, uint8_t bitsPerWord, int16_t cs_pin, uint32_t lowspeed, uint32_t highspeed):
|
2014-06-27 06:38:32 -03:00
|
|
|
_bus(bus),
|
2014-10-14 21:14:03 -03:00
|
|
|
_subdev(subdev),
|
2014-06-27 06:38:32 -03:00
|
|
|
_type(type),
|
2013-09-28 11:51:43 -03:00
|
|
|
_mode(mode),
|
2014-08-12 06:59:21 -03:00
|
|
|
_bitsPerWord(bitsPerWord),
|
2014-07-05 08:14:11 -03:00
|
|
|
_lowspeed(lowspeed),
|
|
|
|
_highspeed(highspeed),
|
|
|
|
_speed(highspeed),
|
2014-11-25 19:16:39 -04:00
|
|
|
_cs_pin(cs_pin),
|
|
|
|
_cs(NULL)
|
2014-08-12 06:59:21 -03:00
|
|
|
{
|
2014-06-03 05:59:39 -03:00
|
|
|
}
|
2013-09-28 11:51:43 -03:00
|
|
|
|
|
|
|
void LinuxSPIDeviceDriver::init()
|
|
|
|
{
|
2014-06-03 05:59:39 -03:00
|
|
|
// Init the CS
|
2014-10-14 21:14:03 -03:00
|
|
|
if(_cs_pin != SPI_CS_KERNEL) {
|
|
|
|
_cs = hal.gpio->channel(_cs_pin);
|
|
|
|
if (_cs == NULL) {
|
|
|
|
hal.scheduler->panic("Unable to instantiate cs pin");
|
|
|
|
}
|
|
|
|
_cs->mode(HAL_GPIO_OUTPUT);
|
|
|
|
_cs->write(1); // do not hold the SPI bus initially
|
|
|
|
} else {
|
|
|
|
// FIXME Anything we need to do here for kernel-managed CS?
|
2014-07-07 23:15:07 -03:00
|
|
|
}
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL::Semaphore* LinuxSPIDeviceDriver::get_semaphore()
|
|
|
|
{
|
2014-06-27 06:38:32 -03:00
|
|
|
return LinuxSPIDeviceManager::get_semaphore(_bus);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxSPIDeviceDriver::transaction(const uint8_t *tx, uint8_t *rx, uint16_t len)
|
|
|
|
{
|
2014-07-07 23:15:07 -03:00
|
|
|
LinuxSPIDeviceManager::transaction(*this, tx, rx, len);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
2014-07-05 08:14:11 -03:00
|
|
|
void LinuxSPIDeviceDriver::set_bus_speed(enum bus_speed speed)
|
|
|
|
{
|
|
|
|
if (speed == SPI_SPEED_LOW) {
|
|
|
|
_speed = _lowspeed;
|
|
|
|
} else {
|
|
|
|
_speed = _highspeed;
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 11:51:43 -03:00
|
|
|
|
|
|
|
void LinuxSPIDeviceDriver::cs_assert()
|
|
|
|
{
|
2014-06-27 06:38:32 -03:00
|
|
|
LinuxSPIDeviceManager::cs_assert(_type);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxSPIDeviceDriver::cs_release()
|
|
|
|
{
|
2014-06-27 06:38:32 -03:00
|
|
|
LinuxSPIDeviceManager::cs_release(_type);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t LinuxSPIDeviceDriver::transfer(uint8_t data)
|
|
|
|
{
|
|
|
|
uint8_t v = 0;
|
|
|
|
transaction(&data, &v, 1);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxSPIDeviceDriver::transfer(const uint8_t *data, uint16_t len)
|
|
|
|
{
|
|
|
|
transaction(data, NULL, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxSPIDeviceManager::init(void *)
|
|
|
|
{
|
2014-06-27 06:38:32 -03:00
|
|
|
for (uint8_t i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
2014-08-17 23:32:57 -03:00
|
|
|
if (_device[i]._bus >= LINUX_SPI_MAX_BUSES) {
|
|
|
|
hal.scheduler->panic("SPIDriver: invalid bus number");
|
|
|
|
}
|
2014-10-14 21:14:03 -03:00
|
|
|
char path[255];
|
2014-11-25 19:16:39 -04:00
|
|
|
snprintf(path, sizeof(path), "/dev/spidev%u.%u",
|
|
|
|
_device[i]._bus + LINUX_SPIDEV_BUS_OFFSET, _device[i]._subdev);
|
2014-10-14 21:14:03 -03:00
|
|
|
_device[i]._fd = open(path, O_RDWR);
|
|
|
|
if (_device[i]._fd == -1) {
|
2014-11-25 19:16:39 -04:00
|
|
|
printf("Unable to open %s - %s\n", path, strerror(errno));
|
2014-10-14 21:14:03 -03:00
|
|
|
hal.scheduler->panic("SPIDriver: unable to open SPI bus");
|
2014-08-17 23:32:57 -03:00
|
|
|
}
|
2014-11-25 19:16:39 -04:00
|
|
|
#if SPI_DEBUGGING
|
2014-10-14 21:14:03 -03:00
|
|
|
printf("Opened %s\n", path);
|
|
|
|
fflush(stdout);
|
2014-11-25 19:16:39 -04:00
|
|
|
#endif
|
2014-06-27 06:38:32 -03:00
|
|
|
_device[i].init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 19:44:59 -03:00
|
|
|
void LinuxSPIDeviceManager::cs_assert(enum AP_HAL::SPIDevice type)
|
2014-06-27 06:38:32 -03:00
|
|
|
{
|
2014-10-14 21:14:03 -03:00
|
|
|
uint16_t bus = 0, i;
|
2014-07-13 22:31:55 -03:00
|
|
|
for (i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
|
|
|
if (_device[i]._type == type) {
|
|
|
|
bus = _device[i]._bus;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == LINUX_SPI_DEVICE_NUM_DEVICES) {
|
|
|
|
hal.scheduler->panic("Bad device type");
|
|
|
|
}
|
2014-11-24 22:41:34 -04:00
|
|
|
|
|
|
|
// Kernel-mode CS handling
|
|
|
|
if(_device[i]._cs_pin == SPI_CS_KERNEL)
|
|
|
|
return;
|
|
|
|
|
2014-07-13 22:31:55 -03:00
|
|
|
for (i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
|
|
|
if (_device[i]._bus != bus) {
|
2014-06-27 06:38:32 -03:00
|
|
|
// not the same bus
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-13 19:44:59 -03:00
|
|
|
if (_device[i]._type != type) {
|
2014-07-07 23:15:07 -03:00
|
|
|
if (_device[i]._cs->read() != 1) {
|
2014-07-19 00:23:37 -03:00
|
|
|
hal.console->printf("two CS enabled at once i=%u %u and %u\n",
|
|
|
|
(unsigned)i, (unsigned)type, (unsigned)_device[i]._type);
|
2014-06-27 06:38:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 00:56:40 -03:00
|
|
|
for (i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
2014-07-13 19:44:59 -03:00
|
|
|
if (_device[i]._type == type) {
|
2014-07-13 22:31:55 -03:00
|
|
|
_device[i]._cs->write(0);
|
2014-07-13 19:44:59 -03:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 06:38:32 -03:00
|
|
|
}
|
|
|
|
|
2014-07-13 19:44:59 -03:00
|
|
|
void LinuxSPIDeviceManager::cs_release(enum AP_HAL::SPIDevice type)
|
2014-06-27 06:38:32 -03:00
|
|
|
{
|
2014-10-14 21:14:03 -03:00
|
|
|
uint16_t bus = 0, i;
|
2014-07-13 22:31:55 -03:00
|
|
|
for (i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
|
|
|
if (_device[i]._type == type) {
|
|
|
|
bus = _device[i]._bus;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == LINUX_SPI_DEVICE_NUM_DEVICES) {
|
|
|
|
hal.scheduler->panic("Bad device type");
|
|
|
|
}
|
2014-10-14 21:14:03 -03:00
|
|
|
|
|
|
|
// Kernel-mode CS handling
|
|
|
|
if(_device[i]._cs_pin == SPI_CS_KERNEL)
|
|
|
|
return;
|
|
|
|
|
2014-07-13 22:31:55 -03:00
|
|
|
for (i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
|
|
|
if (_device[i]._bus != bus) {
|
2014-06-27 06:38:32 -03:00
|
|
|
// not the same bus
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-07 23:15:07 -03:00
|
|
|
_device[i]._cs->write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxSPIDeviceManager::transaction(LinuxSPIDeviceDriver &driver, const uint8_t *tx, uint8_t *rx, uint16_t len)
|
|
|
|
{
|
|
|
|
// we set the mode before we assert the CS line so that the bus is
|
|
|
|
// in the correct idle state before the chip is selected
|
2014-10-14 21:14:03 -03:00
|
|
|
ioctl(driver._fd, SPI_IOC_WR_MODE, &driver._mode);
|
2014-07-07 23:15:07 -03:00
|
|
|
|
|
|
|
cs_assert(driver._type);
|
|
|
|
struct spi_ioc_transfer spi[1];
|
|
|
|
memset(spi, 0, sizeof(spi));
|
|
|
|
spi[0].tx_buf = (uint64_t)tx;
|
|
|
|
spi[0].rx_buf = (uint64_t)rx;
|
|
|
|
spi[0].len = len;
|
|
|
|
spi[0].delay_usecs = 0;
|
|
|
|
spi[0].speed_hz = driver._speed;
|
|
|
|
spi[0].bits_per_word = driver._bitsPerWord;
|
|
|
|
spi[0].cs_change = 0;
|
|
|
|
|
|
|
|
if (rx != NULL) {
|
|
|
|
// keep valgrind happy
|
|
|
|
memset(rx, 0, len);
|
2014-06-27 06:38:32 -03:00
|
|
|
}
|
2014-08-12 06:59:21 -03:00
|
|
|
|
2014-10-14 21:14:03 -03:00
|
|
|
ioctl(driver._fd, SPI_IOC_MESSAGE(1), &spi);
|
2014-07-07 23:15:07 -03:00
|
|
|
cs_release(driver._type);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return a SPIDeviceDriver for a particular device
|
|
|
|
*/
|
2014-07-13 19:44:59 -03:00
|
|
|
AP_HAL::SPIDeviceDriver *LinuxSPIDeviceManager::device(enum AP_HAL::SPIDevice dev)
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2014-07-13 19:44:59 -03:00
|
|
|
for (uint8_t i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
|
|
|
if (_device[i]._type == dev) {
|
|
|
|
return &_device[i];
|
|
|
|
}
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-27 06:38:32 -03:00
|
|
|
/*
|
|
|
|
return the bus specific semaphore
|
|
|
|
*/
|
2014-10-14 21:14:03 -03:00
|
|
|
AP_HAL::Semaphore *LinuxSPIDeviceManager::get_semaphore(uint16_t bus)
|
2014-06-27 06:38:32 -03:00
|
|
|
{
|
|
|
|
return &_semaphore[bus];
|
|
|
|
}
|
|
|
|
|
2013-09-28 11:51:43 -03:00
|
|
|
#endif // CONFIG_HAL_BOARD
|