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@199 | 24 | #include <stdint.h> |
paul@199 | 25 | |
paul@199 | 26 | |
paul@199 | 27 | |
paul@199 | 28 | #ifdef __cplusplus |
paul@199 | 29 | |
paul@222 | 30 | #include <l4/devices/gpio.h> |
paul@222 | 31 | #include <l4/devices/spi.h> |
paul@199 | 32 | |
paul@222 | 33 | /* SPI channel abstraction. */ |
paul@222 | 34 | |
paul@222 | 35 | class Spi_gpio : public Spi_channel_base |
paul@199 | 36 | { |
paul@222 | 37 | uint64_t _frequency; |
paul@199 | 38 | Hw::Gpio_chip *_clock_device; |
paul@199 | 39 | int _clock_pin; |
paul@199 | 40 | Hw::Gpio_chip *_data_device; |
paul@199 | 41 | int _data_pin; |
paul@199 | 42 | Hw::Gpio_chip *_enable_device; |
paul@199 | 43 | int _enable_pin; |
paul@222 | 44 | Hw::Gpio_chip *_control_device; |
paul@222 | 45 | int _control_pin; |
paul@199 | 46 | |
paul@199 | 47 | public: |
paul@222 | 48 | explicit Spi_gpio(uint64_t frequency, |
paul@222 | 49 | Hw::Gpio_chip *clock_device, int clock_pin, |
paul@222 | 50 | Hw::Gpio_chip *data_device, int data_pin, |
paul@222 | 51 | Hw::Gpio_chip *enable_device, int enable_pin, |
paul@222 | 52 | Hw::Gpio_chip *control_device = NULL, int control_pin = 0); |
paul@222 | 53 | |
paul@199 | 54 | explicit Spi_gpio(Hw::Gpio_chip *clock_device, int clock_pin, |
paul@199 | 55 | Hw::Gpio_chip *data_device, int data_pin, |
paul@199 | 56 | Hw::Gpio_chip *enable_device, int enable_pin, |
paul@222 | 57 | Hw::Gpio_chip *control_device = NULL, int control_pin = 0); |
paul@222 | 58 | |
paul@222 | 59 | uint32_t send(uint32_t bytes, const uint8_t data[]); |
paul@199 | 60 | |
paul@222 | 61 | uint32_t send_dc(uint32_t bytes, const uint8_t data[], const int dc[]); |
paul@222 | 62 | |
paul@222 | 63 | uint32_t send_units(uint32_t bytes, const uint8_t data[], uint8_t unit_size, |
paul@222 | 64 | uint8_t char_size); |
paul@199 | 65 | }; |
paul@199 | 66 | |
paul@199 | 67 | #endif /* __cplusplus */ |
paul@199 | 68 | |
paul@199 | 69 | |
paul@199 | 70 | |
paul@199 | 71 | /* C language interface. */ |
paul@199 | 72 | |
paul@199 | 73 | EXTERN_C_BEGIN |
paul@199 | 74 | |
paul@222 | 75 | void *spi_gpio_get_channel(uint64_t frequency, |
paul@222 | 76 | void *clock_chip, int clock_pin, |
paul@199 | 77 | void *data_chip, int data_pin, |
paul@199 | 78 | void *enable_chip, int enable_pin, |
paul@222 | 79 | void *control_chip, int control_pin); |
paul@222 | 80 | |
paul@222 | 81 | uint32_t spi_gpio_send(void *channel, uint32_t bytes, const uint8_t data[]); |
paul@199 | 82 | |
paul@222 | 83 | uint32_t spi_gpio_send_dc(void *channel, uint32_t bytes, const uint8_t data[], |
paul@222 | 84 | const int dc[]); |
paul@222 | 85 | |
paul@222 | 86 | uint32_t spi_gpio_send_units(void *channel, uint32_t bytes, const uint8_t data[], |
paul@222 | 87 | uint8_t unit_size, uint8_t char_size); |
paul@199 | 88 | |
paul@199 | 89 | EXTERN_C_END |