2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License,
|
|
|
|
* or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <AP_HAL/HAL.h>
|
|
|
|
#include <AP_HAL/SPIDevice.h>
|
2018-08-07 05:47:10 -03:00
|
|
|
#include "AP_HAL_ChibiOS.h"
|
|
|
|
|
|
|
|
#if HAL_USE_SPI == TRUE
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#include "Semaphores.h"
|
|
|
|
#include "Scheduler.h"
|
|
|
|
#include "Device.h"
|
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
namespace ChibiOS {
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
class SPIBus : public DeviceBus {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
|
|
|
SPIBus(uint8_t bus);
|
|
|
|
struct spi_dev_s *dev;
|
|
|
|
uint8_t bus;
|
|
|
|
SPIConfig spicfg;
|
2018-03-14 03:06:30 -03:00
|
|
|
void dma_allocate(Shared_DMA *ctx);
|
|
|
|
void dma_deallocate(Shared_DMA *ctx);
|
2018-02-02 23:26:13 -04:00
|
|
|
bool spi_started;
|
2018-06-04 08:27:54 -03:00
|
|
|
|
|
|
|
// we need an additional lock in the dma_allocate and
|
|
|
|
// dma_deallocate functions to cope with 3-way contention as we
|
|
|
|
// have two DMA channels that we are handling with the shared_dma
|
|
|
|
// code
|
|
|
|
mutex_t dma_lock;
|
2018-01-05 02:19:51 -04:00
|
|
|
};
|
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
struct SPIDesc {
|
2018-01-05 02:19:51 -04:00
|
|
|
SPIDesc(const char *_name, uint8_t _bus,
|
2018-01-11 21:01:06 -04:00
|
|
|
uint8_t _device, ioline_t _pal_line,
|
2018-01-05 02:19:51 -04:00
|
|
|
uint16_t _mode, uint32_t _lowspeed, uint32_t _highspeed)
|
|
|
|
: name(_name), bus(_bus), device(_device),
|
2018-01-11 21:01:06 -04:00
|
|
|
pal_line(_pal_line), mode(_mode),
|
2018-01-05 02:19:51 -04:00
|
|
|
lowspeed(_lowspeed), highspeed(_highspeed)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
uint8_t bus;
|
|
|
|
uint8_t device;
|
2018-01-11 21:01:06 -04:00
|
|
|
ioline_t pal_line;
|
2018-01-05 02:19:51 -04:00
|
|
|
uint16_t mode;
|
|
|
|
uint32_t lowspeed;
|
|
|
|
uint32_t highspeed;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
class SPIDevice : public AP_HAL::SPIDevice {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
|
|
|
SPIDevice(SPIBus &_bus, SPIDesc &_device_desc);
|
|
|
|
|
|
|
|
virtual ~SPIDevice();
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::set_speed() */
|
|
|
|
bool set_speed(AP_HAL::Device::Speed speed) override;
|
|
|
|
|
|
|
|
// low level transfer function
|
|
|
|
void do_transfer(const uint8_t *send, uint8_t *recv, uint32_t len);
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::transfer() */
|
|
|
|
bool transfer(const uint8_t *send, uint32_t send_len,
|
|
|
|
uint8_t *recv, uint32_t recv_len) override;
|
|
|
|
|
|
|
|
/* See AP_HAL::SPIDevice::transfer_fullduplex() */
|
|
|
|
bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
|
|
|
|
uint32_t len) override;
|
|
|
|
|
2018-05-26 05:59:41 -03:00
|
|
|
/*
|
|
|
|
* send N bytes of clock pulses without taking CS. This is used
|
|
|
|
* when initialising microSD interfaces over SPI
|
|
|
|
*/
|
|
|
|
bool clock_pulse(uint32_t len) override;
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
/* See AP_HAL::Device::get_semaphore() */
|
|
|
|
AP_HAL::Semaphore *get_semaphore() override;
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::register_periodic_callback() */
|
|
|
|
AP_HAL::Device::PeriodicHandle register_periodic_callback(
|
|
|
|
uint32_t period_usec, AP_HAL::Device::PeriodicCb) override;
|
|
|
|
|
|
|
|
/* See AP_HAL::Device::adjust_periodic_callback() */
|
|
|
|
bool adjust_periodic_callback(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec) override;
|
|
|
|
|
|
|
|
bool set_chip_select(bool set) override;
|
|
|
|
|
2018-04-28 21:09:09 -03:00
|
|
|
bool acquire_bus(bool acquire, bool skip_cs);
|
|
|
|
|
|
|
|
SPIDriver * get_driver();
|
|
|
|
|
2018-03-07 17:42:35 -04:00
|
|
|
#ifdef HAL_SPI_CHECK_CLOCK_FREQ
|
|
|
|
// used to measure clock frequencies
|
|
|
|
static void test_clock_freq(void);
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
private:
|
|
|
|
SPIBus &bus;
|
|
|
|
SPIDesc &device_desc;
|
|
|
|
uint32_t frequency;
|
|
|
|
uint16_t freq_flag;
|
|
|
|
uint16_t freq_flag_low;
|
|
|
|
uint16_t freq_flag_high;
|
|
|
|
char *pname;
|
|
|
|
bool cs_forced;
|
|
|
|
static void *spi_thread(void *arg);
|
2018-05-29 08:01:58 -03:00
|
|
|
static uint16_t derive_freq_flag_bus(uint8_t busid, uint32_t _frequency);
|
2018-01-05 02:19:51 -04:00
|
|
|
uint16_t derive_freq_flag(uint32_t _frequency);
|
|
|
|
};
|
|
|
|
|
2018-03-02 00:37:10 -04:00
|
|
|
class SPIDeviceManager : public AP_HAL::SPIDeviceManager {
|
2018-01-05 02:19:51 -04:00
|
|
|
public:
|
|
|
|
friend class SPIDevice;
|
|
|
|
|
|
|
|
static SPIDeviceManager *from(AP_HAL::SPIDeviceManager *spi_mgr)
|
|
|
|
{
|
|
|
|
return static_cast<SPIDeviceManager*>(spi_mgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::SPIDevice> get_device(const char *name);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static SPIDesc device_table[];
|
|
|
|
SPIBus *buses;
|
|
|
|
};
|
2018-03-02 00:37:10 -04:00
|
|
|
}
|
2018-03-01 20:46:30 -04:00
|
|
|
|
|
|
|
#endif // HAL_USE_SPI
|