|
| 1 | +/* |
| 2 | + * This file is part of the Arduino_ThreadsafeIO library. |
| 3 | + * Copyright (c) 2021 Arduino SA. |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + */ |
| 18 | + |
| 19 | +/************************************************************************************** |
| 20 | + * INCLUDE |
| 21 | + **************************************************************************************/ |
| 22 | + |
| 23 | +#include "SpiBusDevice.h" |
| 24 | + |
| 25 | +#include "SpiDispatcher.h" |
| 26 | + |
| 27 | +/************************************************************************************** |
| 28 | + * CTOR/DTOR |
| 29 | + **************************************************************************************/ |
| 30 | + |
| 31 | +SpiBusDevice::SpiBusDevice(SpiBusDeviceConfig const & config) |
| 32 | +: _config{config} |
| 33 | +{ |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +/************************************************************************************** |
| 38 | + * PUBLIC MEMBER FUNCTIONS |
| 39 | + **************************************************************************************/ |
| 40 | + |
| 41 | +IoResponse SpiBusDevice::transfer(IoRequest & req) |
| 42 | +{ |
| 43 | + return SpiDispatcher::instance().dispatch(&req, &_config); |
| 44 | +} |
| 45 | + |
| 46 | +bool SpiBusDevice::read(uint8_t * buffer, size_t len, uint8_t sendvalue) |
| 47 | +{ |
| 48 | + SpiBusDeviceConfig config(_config.spi(), _config.settings(), _config.select_func(), _config.deselect_func(), sendvalue); |
| 49 | + IoRequest req(nullptr, 0, buffer, len); |
| 50 | + IoResponse rsp = SpiDispatcher::instance().dispatch(&req, &config); |
| 51 | + rsp->wait(); |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +bool SpiBusDevice::write(uint8_t * buffer, size_t len) |
| 56 | +{ |
| 57 | + IoRequest req(buffer, len, nullptr, 0); |
| 58 | + IoResponse rsp = SpiDispatcher::instance().dispatch(&req, &_config); |
| 59 | + rsp->wait(); |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +bool SpiBusDevice::write_then_read(uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, uint8_t sendvalue) |
| 64 | +{ |
| 65 | + SpiBusDeviceConfig config(_config.spi(), _config.settings(), _config.select_func(), _config.deselect_func(), sendvalue); |
| 66 | + IoRequest req(write_buffer, write_len, read_buffer, read_len); |
| 67 | + IoResponse rsp = SpiDispatcher::instance().dispatch(&req, &config); |
| 68 | + rsp->wait(); |
| 69 | + return true; |
| 70 | +} |
0 commit comments