Line data Source code
1 : /* 2 : * Copyright (C) 2015-2016 Intel Corporation. All rights reserved. 3 : * 4 : * This file is free software: you can redistribute it and/or modify it 5 : * under the terms of the GNU General Public License as published by the 6 : * Free Software Foundation, either version 3 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * This file is distributed in the hope that it will be useful, but 10 : * WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 : * See the GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License along 15 : * with this program. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : #pragma once 18 : 19 : #include <inttypes.h> 20 : 21 : #include "AP_HAL_Namespace.h" 22 : #include "Device.h" 23 : #include "utility/OwnPtr.h" 24 : 25 : namespace AP_HAL { 26 : 27 : class SPIDevice : public Device { 28 : public: 29 8 : SPIDevice() : Device(BUS_TYPE_SPI) { } 30 : 31 0 : virtual ~SPIDevice() { } 32 : /* Device implementation */ 33 : 34 : /* See Device::set_speed() */ 35 : virtual bool set_speed(Device::Speed speed) override = 0; 36 : 37 : /* See Device::transfer() */ 38 : virtual bool transfer(const uint8_t *send, uint32_t send_len, 39 : uint8_t *recv, uint32_t recv_len) override = 0; 40 : 41 : /* 42 : * Like #transfer(), but both @send and @recv buffers are transmitted at 43 : * the same time: because of this they need to be of the same size. 44 : */ 45 : virtual bool transfer_fullduplex(const uint8_t *send, uint8_t *recv, 46 : uint32_t len) = 0; 47 : 48 : /* 49 : * Like #transfer(), but len bytes are both transmitted and received from/into @send_recv. 50 : * This function is optimized for DMA-enabled multi-transfers without buffer copying 51 : */ 52 0 : bool transfer_fullduplex(uint8_t *send_recv, uint32_t len) override { 53 0 : return transfer(send_recv, len, send_recv, len); 54 : } 55 : 56 : /* 57 : * send N bytes of clock pulses without taking CS. This is used 58 : * when initialising microSD interfaces over SPI 59 : */ 60 0 : virtual bool clock_pulse(uint32_t len) { return false; } 61 : 62 : /* See Device::get_semaphore() */ 63 : virtual Semaphore *get_semaphore() override = 0; 64 : 65 : /* See Device::register_periodic_callback() */ 66 : virtual Device::PeriodicHandle register_periodic_callback( 67 : uint32_t period_usec, Device::PeriodicCb) override = 0; 68 : 69 : /* See Device::adjust_periodic_callback() */ 70 0 : virtual bool adjust_periodic_callback( 71 0 : PeriodicHandle h, uint32_t period_usec) override { return false; } 72 : 73 : // setup a bus clock slowdown factor (optional interface) 74 0 : virtual void set_slowdown(uint8_t slowdown) {} 75 : }; 76 : 77 : class SPIDeviceManager { 78 : public: 79 : virtual SPIDevice *get_device_ptr(const char *name) = 0; 80 5 : OwnPtr<SPIDevice> get_device(const char *name) { 81 5 : return AP_HAL::OwnPtr<AP_HAL::SPIDevice>(get_device_ptr(name)); 82 : } 83 : 84 : /* Return the number of SPI devices currently registered. */ 85 0 : virtual uint8_t get_count() { return 0; } 86 : 87 : /* Get spi device name at @idx */ 88 0 : virtual const char *get_device_name(uint8_t idx) { return nullptr; } 89 : 90 0 : virtual void set_register_rw_callback(const char* name, AP_HAL::Device::RegisterRWCb cb) {} 91 : }; 92 : 93 : }