paul@199 | 1 | /* |
paul@199 | 2 | * Perform SPI communication using GPIO operations. |
paul@199 | 3 | * |
paul@199 | 4 | * Copyright (C) 2018, 2020, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@199 | 5 | * |
paul@199 | 6 | * This program is free software; you can redistribute it and/or |
paul@199 | 7 | * modify it under the terms of the GNU General Public License as |
paul@199 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@199 | 9 | * the License, or (at your option) any later version. |
paul@199 | 10 | * |
paul@199 | 11 | * This program is distributed in the hope that it will be useful, |
paul@199 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@199 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@199 | 14 | * GNU General Public License for more details. |
paul@199 | 15 | * |
paul@199 | 16 | * You should have received a copy of the GNU General Public License |
paul@199 | 17 | * along with this program; if not, write to the Free Software |
paul@199 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@199 | 19 | * Boston, MA 02110-1301, USA |
paul@199 | 20 | */ |
paul@199 | 21 | |
paul@221 | 22 | #pragma once |
paul@221 | 23 | |
paul@236 | 24 | #include <l4/re/c/dma_space.h> |
paul@199 | 25 | #include <stdint.h> |
paul@199 | 26 | |
paul@199 | 27 | |
paul@199 | 28 | |
paul@199 | 29 | #ifdef __cplusplus |
paul@199 | 30 | |
paul@222 | 31 | #include <l4/devices/gpio.h> |
paul@222 | 32 | #include <l4/devices/spi.h> |
paul@199 | 33 | |
paul@222 | 34 | /* SPI channel abstraction. */ |
paul@222 | 35 | |
paul@222 | 36 | class Spi_gpio : public Spi_channel_base |
paul@199 | 37 | { |
paul@222 | 38 | uint64_t _frequency; |
paul@199 | 39 | Hw::Gpio_chip *_clock_device; |
paul@199 | 40 | int _clock_pin; |
paul@199 | 41 | Hw::Gpio_chip *_data_device; |
paul@199 | 42 | int _data_pin; |
paul@199 | 43 | Hw::Gpio_chip *_enable_device; |
paul@199 | 44 | int _enable_pin; |
paul@222 | 45 | Hw::Gpio_chip *_control_device; |
paul@222 | 46 | int _control_pin; |
paul@199 | 47 | |
paul@199 | 48 | public: |
paul@222 | 49 | explicit Spi_gpio(uint64_t frequency, |
paul@222 | 50 | Hw::Gpio_chip *clock_device, int clock_pin, |
paul@222 | 51 | Hw::Gpio_chip *data_device, int data_pin, |
paul@222 | 52 | Hw::Gpio_chip *enable_device, int enable_pin, |
paul@222 | 53 | Hw::Gpio_chip *control_device = NULL, int control_pin = 0); |
paul@222 | 54 | |
paul@199 | 55 | explicit Spi_gpio(Hw::Gpio_chip *clock_device, int clock_pin, |
paul@199 | 56 | Hw::Gpio_chip *data_device, int data_pin, |
paul@199 | 57 | Hw::Gpio_chip *enable_device, int enable_pin, |
paul@222 | 58 | Hw::Gpio_chip *control_device = NULL, int control_pin = 0); |
paul@222 | 59 | |
paul@222 | 60 | uint32_t send(uint32_t bytes, const uint8_t data[]); |
paul@199 | 61 | |
paul@236 | 62 | uint32_t send_dc(uint32_t bytes, const uint8_t data[], const int dc[], |
paul@236 | 63 | uint8_t char_size, bool big_endian); |
paul@222 | 64 | |
paul@222 | 65 | uint32_t send_units(uint32_t bytes, const uint8_t data[], uint8_t unit_size, |
paul@236 | 66 | uint8_t char_size, bool big_endian); |
paul@236 | 67 | |
paul@236 | 68 | uint32_t transfer(l4_addr_t vaddr, l4re_dma_space_dma_addr_t paddr, |
paul@236 | 69 | uint32_t count, uint8_t unit_size, uint8_t char_size, |
paul@236 | 70 | l4_addr_t desc_vaddr = 0, |
paul@236 | 71 | l4re_dma_space_dma_addr_t desc_paddr = 0); |
paul@199 | 72 | }; |
paul@199 | 73 | |
paul@199 | 74 | #endif /* __cplusplus */ |
paul@199 | 75 | |
paul@199 | 76 | |
paul@199 | 77 | |
paul@199 | 78 | /* C language interface. */ |
paul@199 | 79 | |
paul@199 | 80 | EXTERN_C_BEGIN |
paul@199 | 81 | |
paul@222 | 82 | void *spi_gpio_get_channel(uint64_t frequency, |
paul@222 | 83 | void *clock_chip, int clock_pin, |
paul@199 | 84 | void *data_chip, int data_pin, |
paul@199 | 85 | void *enable_chip, int enable_pin, |
paul@222 | 86 | void *control_chip, int control_pin); |
paul@222 | 87 | |
paul@222 | 88 | uint32_t spi_gpio_send(void *channel, uint32_t bytes, const uint8_t data[]); |
paul@199 | 89 | |
paul@222 | 90 | uint32_t spi_gpio_send_dc(void *channel, uint32_t bytes, const uint8_t data[], |
paul@236 | 91 | const int dc[], uint8_t char_size, int big_endian); |
paul@222 | 92 | |
paul@222 | 93 | uint32_t spi_gpio_send_units(void *channel, uint32_t bytes, const uint8_t data[], |
paul@236 | 94 | uint8_t unit_size, uint8_t char_size, int big_endian); |
paul@236 | 95 | |
paul@236 | 96 | uint32_t spi_gpio_transfer(void *channel, l4_addr_t vaddr, |
paul@236 | 97 | l4re_dma_space_dma_addr_t paddr, |
paul@236 | 98 | uint32_t count, uint8_t unit_size, |
paul@236 | 99 | uint8_t char_size); |
paul@236 | 100 | |
paul@236 | 101 | uint32_t spi_gpio_transfer_descriptor(void *channel, l4_addr_t vaddr, |
paul@236 | 102 | l4re_dma_space_dma_addr_t paddr, |
paul@236 | 103 | uint32_t count, uint8_t unit_size, |
paul@236 | 104 | uint8_t char_size, l4_addr_t desc_vaddr, |
paul@236 | 105 | l4re_dma_space_dma_addr_t desc_paddr); |
paul@199 | 106 | |
paul@199 | 107 | EXTERN_C_END |