Line data Source code
1 : /* 2 : * Copyright (C) 2021 Peter Barker. 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/HAL.h> 22 : #include <AP_HAL/SPIDevice.h> 23 : 24 : #include "Semaphores.h" 25 : 26 : class SPIBus; 27 : 28 : namespace HALSITL { 29 : 30 : struct SPIDesc { 31 4290 : SPIDesc(const char *_name, uint8_t _bus, uint8_t _cs_pin) 32 4290 : : name(_name), bus(_bus), cs_pin(_cs_pin) 33 4290 : { } 34 : 35 : const char *name; 36 : uint8_t bus; 37 : uint8_t cs_pin; // cs 38 : }; 39 : 40 : class SPIDevice : public AP_HAL::SPIDevice { 41 : public: 42 : SPIDevice(SPIBus &_bus, SPIDesc &_device_desc); 43 : 44 : // no concept of speed in SITL yet 45 0 : bool set_speed(AP_HAL::Device::Speed speed) override { return true; } 46 : 47 : bool transfer(const uint8_t *send, uint32_t send_len, 48 : uint8_t *recv, uint32_t recv_len) override; 49 : 50 0 : bool transfer_fullduplex(const uint8_t *send, uint8_t *recv, 51 : uint32_t len) override { 52 0 : abort(); 53 : } 54 : 55 0 : bool transfer_fullduplex(uint8_t *send_recv, uint32_t len) override { 56 0 : abort(); 57 : } 58 : 59 : AP_HAL::Semaphore *get_semaphore() override; 60 : 61 : AP_HAL::Device::PeriodicHandle register_periodic_callback( 62 : uint32_t period_usec, AP_HAL::Device::PeriodicCb) override; 63 : 64 : private: 65 : SPIBus &bus; 66 : SPIDesc &device_desc; 67 : 68 : Semaphore _semaphore; 69 : 70 : bool do_transfer(const uint8_t *send, uint8_t *recv, uint32_t len); 71 : }; 72 : 73 : class SPIDeviceManager : public AP_HAL::SPIDeviceManager { 74 : public: 75 : 76 : SPIDeviceManager(); 77 : 78 : AP_HAL::SPIDevice *get_device_ptr(const char *name) override; 79 : 80 : static SPIDesc device_table[]; 81 : static SPIBus *buses; 82 : }; 83 : 84 : }