paul@222 | 1 | /* |
paul@222 | 2 | * Perform SPI communication using a suitable abstraction augmented with |
paul@222 | 3 | * explicit manipulation of a control signal. |
paul@222 | 4 | * |
paul@222 | 5 | * Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> |
paul@222 | 6 | * |
paul@222 | 7 | * This program is free software; you can redistribute it and/or |
paul@222 | 8 | * modify it under the terms of the GNU General Public License as |
paul@222 | 9 | * published by the Free Software Foundation; either version 2 of |
paul@222 | 10 | * the License, or (at your option) any later version. |
paul@222 | 11 | * |
paul@222 | 12 | * This program is distributed in the hope that it will be useful, |
paul@222 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@222 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@222 | 15 | * GNU General Public License for more details. |
paul@222 | 16 | * |
paul@222 | 17 | * You should have received a copy of the GNU General Public License |
paul@222 | 18 | * along with this program; if not, write to the Free Software |
paul@222 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@222 | 20 | * Boston, MA 02110-1301, USA |
paul@222 | 21 | */ |
paul@222 | 22 | |
paul@222 | 23 | #pragma once |
paul@222 | 24 | |
paul@222 | 25 | #include <stdint.h> |
paul@222 | 26 | |
paul@222 | 27 | |
paul@222 | 28 | |
paul@222 | 29 | #ifdef __cplusplus |
paul@222 | 30 | |
paul@222 | 31 | #include <l4/devices/gpio.h> |
paul@222 | 32 | #include <l4/devices/spi.h> |
paul@222 | 33 | |
paul@222 | 34 | /* SPI channel abstraction. */ |
paul@222 | 35 | |
paul@222 | 36 | class Spi_hybrid : public Spi_channel_base, public Spi_control_base |
paul@222 | 37 | { |
paul@222 | 38 | Spi_channel_base *_channel; |
paul@222 | 39 | Hw::Gpio_chip *_control_device; |
paul@222 | 40 | int _control_pin; |
paul@222 | 41 | int _control_alt_func; |
paul@222 | 42 | |
paul@222 | 43 | public: |
paul@222 | 44 | explicit Spi_hybrid(Spi_channel_base *channel, |
paul@222 | 45 | Hw::Gpio_chip *control_device, int control_pin, |
paul@222 | 46 | int control_alt_func = -1); |
paul@222 | 47 | |
paul@222 | 48 | Spi_channel_base *get_channel(); |
paul@222 | 49 | |
paul@222 | 50 | void acquire_control(bool asserted); |
paul@222 | 51 | |
paul@222 | 52 | void release_control(); |
paul@222 | 53 | |
paul@222 | 54 | uint32_t send(uint32_t bytes, const uint8_t data[]); |
paul@222 | 55 | |
paul@222 | 56 | uint32_t send_dc(uint32_t bytes, const uint8_t data[], const int dc[]); |
paul@222 | 57 | |
paul@222 | 58 | uint32_t send_units(uint32_t bytes, const uint8_t data[], |
paul@222 | 59 | uint8_t unit_size, uint8_t char_size); |
paul@222 | 60 | }; |
paul@222 | 61 | |
paul@222 | 62 | #endif /* __cplusplus */ |
paul@222 | 63 | |
paul@222 | 64 | |
paul@222 | 65 | |
paul@222 | 66 | /* C language interface. */ |
paul@222 | 67 | |
paul@222 | 68 | EXTERN_C_BEGIN |
paul@222 | 69 | |
paul@222 | 70 | void *spi_hybrid_get_channel(void *channel, void *control_chip, int control_pin, |
paul@222 | 71 | int control_alt_func); |
paul@222 | 72 | |
paul@222 | 73 | void *spi_hybrid_get_raw_channel(void *channel); |
paul@222 | 74 | |
paul@222 | 75 | void spi_hybrid_acquire_control(void *channel, int asserted); |
paul@222 | 76 | |
paul@222 | 77 | void spi_hybrid_release_control(void *channel); |
paul@222 | 78 | |
paul@222 | 79 | uint32_t spi_hybrid_send(void *channel, uint32_t bytes, const uint8_t data[]); |
paul@222 | 80 | |
paul@222 | 81 | uint32_t spi_hybrid_send_dc(void *channel, uint32_t bytes, const uint8_t data[], |
paul@222 | 82 | const int dc[]); |
paul@222 | 83 | |
paul@222 | 84 | uint32_t spi_hybrid_send_units(void *channel, uint32_t bytes, const uint8_t data[], |
paul@222 | 85 | uint8_t unit_size, uint8_t char_size); |
paul@222 | 86 | |
paul@222 | 87 | EXTERN_C_END |