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@282 | 5 | * Copyright (C) 2023, 2024 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@236 | 25 | #include <l4/re/c/dma_space.h> |
paul@222 | 26 | #include <stdint.h> |
paul@222 | 27 | |
paul@222 | 28 | |
paul@222 | 29 | |
paul@222 | 30 | #ifdef __cplusplus |
paul@222 | 31 | |
paul@222 | 32 | #include <l4/devices/gpio.h> |
paul@222 | 33 | #include <l4/devices/spi.h> |
paul@222 | 34 | |
paul@222 | 35 | /* SPI channel abstraction. */ |
paul@222 | 36 | |
paul@222 | 37 | class Spi_hybrid : public Spi_channel_base, public Spi_control_base |
paul@222 | 38 | { |
paul@222 | 39 | Spi_channel_base *_channel; |
paul@222 | 40 | Hw::Gpio_chip *_control_device; |
paul@222 | 41 | int _control_pin; |
paul@222 | 42 | int _control_alt_func; |
paul@222 | 43 | |
paul@222 | 44 | public: |
paul@222 | 45 | explicit Spi_hybrid(Spi_channel_base *channel, |
paul@222 | 46 | Hw::Gpio_chip *control_device, int control_pin, |
paul@222 | 47 | int control_alt_func = -1); |
paul@222 | 48 | |
paul@222 | 49 | Spi_channel_base *get_channel(); |
paul@222 | 50 | |
paul@222 | 51 | void acquire_control(bool asserted); |
paul@222 | 52 | |
paul@222 | 53 | void release_control(); |
paul@222 | 54 | |
paul@222 | 55 | uint32_t send(uint32_t bytes, const uint8_t data[]); |
paul@222 | 56 | |
paul@236 | 57 | uint32_t send_dc(uint32_t bytes, const uint8_t data[], const int dc[], |
paul@282 | 58 | uint8_t char_size, bool big_endian = true); |
paul@222 | 59 | |
paul@222 | 60 | uint32_t send_units(uint32_t bytes, const uint8_t data[], |
paul@282 | 61 | uint8_t unit_size, uint8_t char_size, |
paul@282 | 62 | bool big_endian = true); |
paul@236 | 63 | |
paul@236 | 64 | uint32_t transfer(l4_addr_t vaddr, l4re_dma_space_dma_addr_t paddr, |
paul@236 | 65 | uint32_t count, uint8_t unit_size, uint8_t char_size, |
paul@236 | 66 | l4_addr_t desc_vaddr = 0, |
paul@236 | 67 | l4re_dma_space_dma_addr_t desc_paddr = 0); |
paul@222 | 68 | }; |
paul@222 | 69 | |
paul@222 | 70 | #endif /* __cplusplus */ |
paul@222 | 71 | |
paul@222 | 72 | |
paul@222 | 73 | |
paul@222 | 74 | /* C language interface. */ |
paul@222 | 75 | |
paul@222 | 76 | EXTERN_C_BEGIN |
paul@222 | 77 | |
paul@222 | 78 | void *spi_hybrid_get_channel(void *channel, void *control_chip, int control_pin, |
paul@222 | 79 | int control_alt_func); |
paul@222 | 80 | |
paul@222 | 81 | void *spi_hybrid_get_raw_channel(void *channel); |
paul@222 | 82 | |
paul@222 | 83 | void spi_hybrid_acquire_control(void *channel, int asserted); |
paul@222 | 84 | |
paul@222 | 85 | void spi_hybrid_release_control(void *channel); |
paul@222 | 86 | |
paul@222 | 87 | uint32_t spi_hybrid_send(void *channel, uint32_t bytes, const uint8_t data[]); |
paul@222 | 88 | |
paul@222 | 89 | uint32_t spi_hybrid_send_dc(void *channel, uint32_t bytes, const uint8_t data[], |
paul@236 | 90 | const int dc[], uint8_t char_size, int big_endian); |
paul@222 | 91 | |
paul@222 | 92 | uint32_t spi_hybrid_send_units(void *channel, uint32_t bytes, const uint8_t data[], |
paul@236 | 93 | uint8_t unit_size, uint8_t char_size, int big_endian); |
paul@236 | 94 | |
paul@236 | 95 | uint32_t spi_hybrid_transfer(void *channel, l4_addr_t vaddr, |
paul@236 | 96 | l4re_dma_space_dma_addr_t paddr, uint32_t count, |
paul@236 | 97 | uint8_t unit_size, uint8_t char_size); |
paul@236 | 98 | |
paul@236 | 99 | uint32_t spi_hybrid_transfer_descriptor(void *channel, l4_addr_t vaddr, |
paul@236 | 100 | l4re_dma_space_dma_addr_t paddr, |
paul@236 | 101 | uint32_t count, uint8_t unit_size, |
paul@236 | 102 | uint8_t char_size, l4_addr_t desc_vaddr, |
paul@236 | 103 | l4re_dma_space_dma_addr_t desc_paddr); |
paul@222 | 104 | |
paul@222 | 105 | EXTERN_C_END |