2013-09-28 11:51:43 -03:00
|
|
|
#include "SPIDriver.h"
|
2016-05-17 23:26:57 -03:00
|
|
|
|
|
|
|
#include <errno.h>
|
2013-09-28 11:51:43 -03:00
|
|
|
#include <fcntl.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <linux/spi/spidev.h>
|
2013-09-28 11:51:43 -03:00
|
|
|
#include <stdint.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdio.h>
|
2013-09-28 11:51:43 -03:00
|
|
|
#include <sys/ioctl.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2014-06-03 05:59:39 -03:00
|
|
|
#include "GPIO.h"
|
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
|
|
|
|
2015-09-06 18:35:13 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBOARD
|
2015-10-20 18:13:25 -03:00
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
2014-07-13 19:44:59 -03:00
|
|
|
// different SPI tables per board subtype
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("lsm9ds0_am", 1, 0, AP_HAL::SPIDevice_LSM9DS0_AM, SPI_MODE_3, 8, BBB_P9_17, 10*MHZ,10*MHZ),
|
|
|
|
SPIDeviceDriver("lsm9ds0_g", 1, 0, AP_HAL::SPIDevice_LSM9DS0_G, SPI_MODE_3, 8, BBB_P8_9, 10*MHZ,10*MHZ),
|
|
|
|
SPIDeviceDriver("ms5611", 2, 0, AP_HAL::SPIDevice_MS5611, SPI_MODE_3, 8, BBB_P9_42, 10*MHZ,10*MHZ),
|
|
|
|
SPIDeviceDriver("mpu6000", 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 */
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("mpu9250", 2, 0, AP_HAL::SPIDevice_MPU9250, SPI_MODE_3, 8, BBB_P9_23, 1*MHZ, 20*MHZ),
|
|
|
|
SPIDeviceDriver("dataflash", 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
|
|
|
};
|
2015-08-27 01:40:25 -03:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_MINLURE
|
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("mpu6000", 0, 0, AP_HAL::SPIDevice_MPU6000, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 15*MHZ)
|
2015-08-27 01:40:25 -03:00
|
|
|
};
|
2016-01-20 09:49:31 -04:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO || CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
|
2015-10-20 18:13:25 -03:00
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
2014-08-12 06:59:21 -03:00
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("mpu9250", 0, 1, AP_HAL::SPIDevice_MPU9250, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 20*MHZ),
|
2016-02-12 12:41:20 -04:00
|
|
|
SPIDeviceDriver("ublox", 0, 0, AP_HAL::SPIDevice_Ublox, SPI_MODE_0, 8, SPI_CS_KERNEL, 5*MHZ, 5*MHZ),
|
2016-04-25 14:16:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_NAVIO2
|
|
|
|
SPIDeviceDriver("lsm9ds1_m", 0, 2, AP_HAL::SPIDevice_LSM9DS1_M, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 10*MHZ),
|
|
|
|
#endif
|
2014-06-27 06:38:32 -03:00
|
|
|
};
|
2016-01-05 06:33:06 -04:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_ERLEBRAIN2 || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXFMINI
|
2015-11-01 07:24:03 -04:00
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("mpu9250", 0, 1, AP_HAL::SPIDevice_MPU9250, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*MHZ, 20*MHZ),
|
|
|
|
SPIDeviceDriver("ms5611", 0, 0, AP_HAL::SPIDevice_MS5611, SPI_MODE_0, 8, SPI_CS_KERNEL, 1*KHZ, 10*MHZ),
|
2015-11-01 07:24:03 -04:00
|
|
|
};
|
2015-01-10 19:20:38 -04:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
|
2015-10-20 18:13:25 -03:00
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
2015-01-10 19:20:38 -04:00
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("mpu9250", 2, 0, AP_HAL::SPIDevice_MPU9250, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 20*MHZ),
|
2016-04-24 05:06:57 -03:00
|
|
|
SPIDeviceDriver("mpu9250ext", 1, 0, AP_HAL::SPIDevice_MPU9250, SPI_MODE_3, 8, SPI_CS_KERNEL, 1*MHZ, 20*MHZ),
|
|
|
|
SPIDeviceDriver("ms5611", 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
|
|
|
};
|
2015-08-17 23:37:55 -03:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_RASPILOT
|
2015-10-20 18:13:25 -03:00
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
2015-08-17 23:37:55 -03:00
|
|
|
/* MPU9250 is restricted to 1MHz for non-data and interrupt registers */
|
2015-09-21 14:27:24 -03:00
|
|
|
SPIDeviceDriver("mpu6000", 0, 0, AP_HAL::SPIDevice_MPU6000, SPI_MODE_3, 8, RPI_GPIO_25, 1*MHZ, 20*MHZ),
|
|
|
|
SPIDeviceDriver("ms5611", 0, 0, AP_HAL::SPIDevice_MS5611, SPI_MODE_3, 8, RPI_GPIO_23, 10*MHZ, 10*MHZ),
|
|
|
|
SPIDeviceDriver("lsm9ds0_am", 0, 0, AP_HAL::SPIDevice_LSM9DS0_AM, SPI_MODE_3, 8, RPI_GPIO_22, 10*MHZ, 10*MHZ),
|
|
|
|
SPIDeviceDriver("lsm9ds0_g", 0, 0, AP_HAL::SPIDevice_LSM9DS0_G, SPI_MODE_3, 8, RPI_GPIO_12, 10*MHZ, 10*MHZ),
|
|
|
|
SPIDeviceDriver("dataflash", 0, 0, AP_HAL::SPIDevice_Dataflash, SPI_MODE_3, 8, RPI_GPIO_5, 6*MHZ, 6*MHZ),
|
|
|
|
SPIDeviceDriver("raspio", 0, 0, AP_HAL::SPIDevice_RASPIO, SPI_MODE_3, 8, RPI_GPIO_7, 10*MHZ, 10*MHZ),
|
2015-08-17 23:37:55 -03:00
|
|
|
};
|
2015-11-28 05:38:56 -04:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BH
|
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver("mpu9250", 0, 0, AP_HAL::SPIDevice_MPU9250, SPI_MODE_0, 8, RPI_GPIO_7, 1*MHZ, 20*MHZ),
|
|
|
|
SPIDeviceDriver("ublox", 0, 0, AP_HAL::SPIDevice_Ublox, SPI_MODE_0, 8, RPI_GPIO_8, 250*KHZ, 5*MHZ),
|
2016-04-15 14:31:24 -03:00
|
|
|
#elif CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BEBOP
|
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = {
|
|
|
|
SPIDeviceDriver("bebop", 1, 0, AP_HAL::SPIDevice_Bebop, SPI_MODE_0, 8, SPI_CS_KERNEL, 320*KHZ, 320*KHZ),
|
2015-11-28 05:38:56 -04:00
|
|
|
};
|
2014-07-13 19:44:59 -03:00
|
|
|
#else
|
|
|
|
// empty device table
|
AP_HAL_Linux: fix use of 0-length array
0-length arrays are supported in C but forbidden in C++. GCC allows it
but clang is more strict:
../../libraries/AP_HAL_Linux/SPIDriver.cpp:75:35: fatal error: no matching constructor for initialization of 'Linux::SPIDeviceDriver [0]'
SPIDeviceDriver SPIDeviceManager::_device[0];
^
../../libraries/AP_HAL_Linux/SPIDriver.h:20:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
class SPIDeviceDriver : public AP_HAL::SPIDeviceDriver {
^
../../libraries/AP_HAL_Linux/SPIDriver.h:20:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
../../libraries/AP_HAL_Linux/SPIDriver.h:25:5: note: candidate constructor not viable: requires 9 arguments, but 0 were provided
SPIDeviceDriver(const char *name, uint16_t bus, uint16_t subdev, enum AP_HAL::SPIDeviceType type, uint8_t mode, uint8_t bitsPerWord, int16_t cs_pin, uint32_t lowspeed, uint32_t highspeed);
^
1 error generated.
2016-03-05 08:48:54 -04:00
|
|
|
SPIDeviceDriver SPIDeviceManager::_device[] = { };
|
|
|
|
#define LINUX_SPI_DEVICE_NUM_DEVICES 0
|
2014-07-13 19:44:59 -03:00
|
|
|
#endif
|
2014-06-27 06:38:32 -03:00
|
|
|
|
AP_HAL_Linux: fix use of 0-length array
0-length arrays are supported in C but forbidden in C++. GCC allows it
but clang is more strict:
../../libraries/AP_HAL_Linux/SPIDriver.cpp:75:35: fatal error: no matching constructor for initialization of 'Linux::SPIDeviceDriver [0]'
SPIDeviceDriver SPIDeviceManager::_device[0];
^
../../libraries/AP_HAL_Linux/SPIDriver.h:20:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
class SPIDeviceDriver : public AP_HAL::SPIDeviceDriver {
^
../../libraries/AP_HAL_Linux/SPIDriver.h:20:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
../../libraries/AP_HAL_Linux/SPIDriver.h:25:5: note: candidate constructor not viable: requires 9 arguments, but 0 were provided
SPIDeviceDriver(const char *name, uint16_t bus, uint16_t subdev, enum AP_HAL::SPIDeviceType type, uint8_t mode, uint8_t bitsPerWord, int16_t cs_pin, uint32_t lowspeed, uint32_t highspeed);
^
1 error generated.
2016-03-05 08:48:54 -04:00
|
|
|
#ifndef LINUX_SPI_DEVICE_NUM_DEVICES
|
2015-10-20 18:13:25 -03:00
|
|
|
#define LINUX_SPI_DEVICE_NUM_DEVICES ARRAY_SIZE(SPIDeviceManager::_device)
|
AP_HAL_Linux: fix use of 0-length array
0-length arrays are supported in C but forbidden in C++. GCC allows it
but clang is more strict:
../../libraries/AP_HAL_Linux/SPIDriver.cpp:75:35: fatal error: no matching constructor for initialization of 'Linux::SPIDeviceDriver [0]'
SPIDeviceDriver SPIDeviceManager::_device[0];
^
../../libraries/AP_HAL_Linux/SPIDriver.h:20:7: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
class SPIDeviceDriver : public AP_HAL::SPIDeviceDriver {
^
../../libraries/AP_HAL_Linux/SPIDriver.h:20:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
../../libraries/AP_HAL_Linux/SPIDriver.h:25:5: note: candidate constructor not viable: requires 9 arguments, but 0 were provided
SPIDeviceDriver(const char *name, uint16_t bus, uint16_t subdev, enum AP_HAL::SPIDeviceType type, uint8_t mode, uint8_t bitsPerWord, int16_t cs_pin, uint32_t lowspeed, uint32_t highspeed);
^
1 error generated.
2016-03-05 08:48:54 -04:00
|
|
|
#endif
|
|
|
|
|
2015-12-22 16:19:17 -04:00
|
|
|
const uint8_t SPIDeviceManager::_n_device_desc = LINUX_SPI_DEVICE_NUM_DEVICES;
|
2015-07-02 12:02:02 -03:00
|
|
|
|
2015-12-07 21:08:42 -04:00
|
|
|
SPIDeviceDriver::SPIDeviceDriver(const char *name, uint16_t bus, uint16_t subdev, enum AP_HAL::SPIDeviceType type, uint8_t mode, uint8_t bitsPerWord, int16_t cs_pin, uint32_t lowspeed, uint32_t highspeed):
|
|
|
|
_name(name),
|
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
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void SPIDeviceDriver::init()
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
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) {
|
2015-11-19 23:10:58 -04:00
|
|
|
AP_HAL::panic("Unable to instantiate cs pin");
|
2014-10-14 21:14:03 -03:00
|
|
|
}
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
2016-01-01 10:35:04 -04:00
|
|
|
AP_HAL::Semaphore *SPIDeviceDriver::get_semaphore()
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2016-01-01 10:35:04 -04:00
|
|
|
return _fake_dev->get_semaphore();
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool SPIDeviceDriver::transaction(const uint8_t *tx, uint8_t *rx, uint16_t len)
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
return SPIDeviceManager::transaction(*this, tx, rx, len);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void SPIDeviceDriver::set_bus_speed(enum bus_speed speed)
|
2014-07-05 08:14:11 -03:00
|
|
|
{
|
|
|
|
if (speed == SPI_SPEED_LOW) {
|
|
|
|
_speed = _lowspeed;
|
|
|
|
} else {
|
|
|
|
_speed = _highspeed;
|
|
|
|
}
|
|
|
|
}
|
2013-09-28 11:51:43 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void SPIDeviceDriver::cs_assert()
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
SPIDeviceManager::cs_assert(_type);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void SPIDeviceDriver::cs_release()
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2015-10-20 18:13:25 -03:00
|
|
|
SPIDeviceManager::cs_release(_type);
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
uint8_t SPIDeviceDriver::transfer(uint8_t data)
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
|
|
|
uint8_t v = 0;
|
|
|
|
transaction(&data, &v, 1);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void SPIDeviceDriver::transfer(const uint8_t *data, uint16_t len)
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
|
|
|
transaction(data, NULL, len);
|
|
|
|
}
|
|
|
|
|
2015-12-02 11:14:20 -04:00
|
|
|
void SPIDeviceManager::init()
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2014-06-27 06:38:32 -03:00
|
|
|
for (uint8_t i=0; i<LINUX_SPI_DEVICE_NUM_DEVICES; i++) {
|
2016-01-01 10:35:04 -04:00
|
|
|
_device[i]._fake_dev = SPIDeviceManager::from(hal.spi)->get_device(_device[i]);
|
|
|
|
if (!_device[i]._fake_dev) {
|
|
|
|
AP_HAL::panic("SPIDriver: couldn't use spidev%u.%u for %s",
|
|
|
|
_device[i]._bus, _device[i]._subdev, _device[i]._name);
|
2014-08-17 23:32:57 -03:00
|
|
|
}
|
2014-06-27 06:38:32 -03:00
|
|
|
_device[i].init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 12:19:26 -04:00
|
|
|
void SPIDeviceManager::cs_assert(enum AP_HAL::SPIDeviceType 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) {
|
2015-11-19 23:10:58 -04:00
|
|
|
AP_HAL::panic("Bad device type");
|
2014-07-13 22:31:55 -03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-12-07 12:19:26 -04:00
|
|
|
void SPIDeviceManager::cs_release(enum AP_HAL::SPIDeviceType 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) {
|
2015-11-19 23:10:58 -04:00
|
|
|
AP_HAL::panic("Bad device type");
|
2014-07-13 22:31:55 -03:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool SPIDeviceManager::transaction(SPIDeviceDriver &driver, const uint8_t *tx, uint8_t *rx, uint16_t len)
|
2014-07-07 23:15:07 -03:00
|
|
|
{
|
2015-08-19 12:36:45 -03:00
|
|
|
int r;
|
2014-07-07 23:15:07 -03:00
|
|
|
// 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
|
2016-01-01 10:35:04 -04:00
|
|
|
int fd = driver._fake_dev->get_fd();
|
|
|
|
r = ioctl(fd, SPI_IOC_WR_MODE, &driver._mode);
|
2015-08-19 12:36:45 -03:00
|
|
|
if (r == -1) {
|
|
|
|
hal.console->printf("SPI: error on setting mode\n");
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
2016-01-01 10:35:04 -04:00
|
|
|
r = ioctl(fd, SPI_IOC_MESSAGE(1), &spi);
|
2014-07-07 23:15:07 -03:00
|
|
|
cs_release(driver._type);
|
2015-08-19 12:36:45 -03:00
|
|
|
|
|
|
|
if (r == -1) {
|
|
|
|
hal.console->printf("SPI: error on doing transaction\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return a SPIDeviceDriver for a particular device
|
|
|
|
*/
|
2015-12-07 12:19:26 -04:00
|
|
|
AP_HAL::SPIDeviceDriver *SPIDeviceManager::device(enum AP_HAL::SPIDeviceType dev, uint8_t index)
|
2013-09-28 11:51:43 -03:00
|
|
|
{
|
2015-09-14 15:43:50 -03:00
|
|
|
uint8_t count = 0;
|
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) {
|
2015-09-14 15:43:50 -03:00
|
|
|
if (count == index) {
|
|
|
|
return &_device[i];
|
|
|
|
} else {
|
|
|
|
count++;
|
|
|
|
}
|
2014-07-13 19:44:59 -03:00
|
|
|
}
|
2013-09-28 11:51:43 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|