ardupilot/libraries/AP_HAL_Linux/SPIDriver.h

63 lines
1.8 KiB
C
Raw Normal View History

2013-09-28 11:51:43 -03:00
#ifndef __AP_HAL_EMPTY_SPIDRIVER_H__
#define __AP_HAL_EMPTY_SPIDRIVER_H__
#include "AP_HAL_Linux.h"
2013-09-28 11:51:43 -03:00
#include "Semaphores.h"
// Most platforms won't need to declare the spidev bus offset
#ifndef LINUX_SPIDEV_BUS_OFFSET
#define LINUX_SPIDEV_BUS_OFFSET 0
#endif
#define LINUX_SPI_MAX_BUSES 3
// Fake CS pin to indicate in-kernel handling
#define SPI_CS_KERNEL -1
class Linux::SPIDeviceDriver : public AP_HAL::SPIDeviceDriver {
2013-09-28 11:51:43 -03:00
public:
friend class Linux::SPIDeviceManager;
SPIDeviceDriver(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);
2013-09-28 11:51:43 -03:00
void init();
AP_HAL::Semaphore *get_semaphore();
bool transaction(const uint8_t *tx, uint8_t *rx, uint16_t len);
2013-09-28 11:51:43 -03:00
void cs_assert();
void cs_release();
uint8_t transfer (uint8_t data);
void transfer (const uint8_t *data, uint16_t len);
void set_bus_speed(enum bus_speed speed);
2013-09-28 11:51:43 -03:00
private:
uint16_t _bus;
uint16_t _subdev;
int16_t _cs_pin;
AP_HAL::DigitalSource *_cs;
2013-09-28 11:51:43 -03:00
uint8_t _mode;
uint8_t _bitsPerWord;
uint32_t _lowspeed;
uint32_t _highspeed;
2013-09-28 11:51:43 -03:00
uint32_t _speed;
enum AP_HAL::SPIDevice _type;
int _fd; // Per-device FD.
2013-09-28 11:51:43 -03:00
};
class Linux::SPIDeviceManager : public AP_HAL::SPIDeviceManager {
2013-09-28 11:51:43 -03:00
public:
void init();
AP_HAL::SPIDeviceDriver* device(enum AP_HAL::SPIDevice, uint8_t index = 0);
static AP_HAL::Semaphore *get_semaphore(uint16_t bus);
static void cs_assert(enum AP_HAL::SPIDevice type);
static void cs_release(enum AP_HAL::SPIDevice type);
static bool transaction(SPIDeviceDriver &driver, const uint8_t *tx, uint8_t *rx, uint16_t len);
2013-09-28 11:51:43 -03:00
private:
static SPIDeviceDriver _device[];
static Semaphore _semaphore[LINUX_SPI_MAX_BUSES];
2013-09-28 11:51:43 -03:00
};
#endif // __AP_HAL_LINUX_SPIDRIVER_H__