1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/lib/spi/src/gpio.cc Sun Oct 15 22:24:25 2023 +0200
1.3 @@ -0,0 +1,116 @@
1.4 +/*
1.5 + * Perform SPI communication using GPIO operations.
1.6 + *
1.7 + * Copyright (C) 2018, 2020, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/devices/spi-gpio.h>
1.26 +#include <time.h>
1.27 +
1.28 +
1.29 +
1.30 +Spi_gpio::Spi_gpio(Hw::Gpio_chip *clock_device, int clock_pin,
1.31 + Hw::Gpio_chip *data_device, int data_pin,
1.32 + Hw::Gpio_chip *enable_device, int enable_pin,
1.33 + uint32_t frequency)
1.34 +: _clock_device(clock_device),
1.35 + _clock_pin(clock_pin),
1.36 + _data_device(data_device),
1.37 + _data_pin(data_pin),
1.38 + _enable_device(enable_device),
1.39 + _enable_pin(enable_pin),
1.40 + _frequency(frequency)
1.41 +{
1.42 + _clock_device->setup(_clock_pin, Hw::Gpio_chip::Output, 1);
1.43 + _data_device->setup(_data_pin, Hw::Gpio_chip::Output, 0);
1.44 + _enable_device->setup(_enable_pin, Hw::Gpio_chip::Output, 1);
1.45 +}
1.46 +
1.47 +/* Send a SPI command. */
1.48 +
1.49 +void Spi_gpio::send(int bytes, uint8_t data[])
1.50 +{
1.51 + struct timespec ts;
1.52 + uint8_t mask;
1.53 + int bit, byte;
1.54 +
1.55 + if (_frequency)
1.56 + {
1.57 + ts.tv_sec = 0;
1.58 + ts.tv_nsec = 1000000000 / _frequency;
1.59 + }
1.60 +
1.61 + /* Initialise pin levels. */
1.62 +
1.63 + _enable_device->set(_enable_pin, 1);
1.64 + _clock_device->set(_clock_pin, 1);
1.65 + _data_device->set(_data_pin, 0);
1.66 +
1.67 + /* Enter the transmission state. */
1.68 +
1.69 + _enable_device->set(_enable_pin, 0);
1.70 +
1.71 + /* Clock data using the clock and data outputs. */
1.72 +
1.73 + for (byte = 0; byte < bytes; byte++)
1.74 + {
1.75 + mask = 0x80;
1.76 +
1.77 + for (bit = 0; bit < 8; bit++)
1.78 + {
1.79 + /* NOTE: Data presented on falling clock level and sampled on rising clock
1.80 + level. This is SPI mode 3, or 0 given that the enable level is
1.81 + driven low immediately before the first bit is presented. */
1.82 +
1.83 + _clock_device->set(_clock_pin, 0);
1.84 + _data_device->set(_data_pin, data[byte] & mask ? 1 : 0);
1.85 +
1.86 + if (_frequency)
1.87 + nanosleep(&ts, NULL);
1.88 +
1.89 + _clock_device->set(_clock_pin, 1);
1.90 +
1.91 + if (_frequency)
1.92 + nanosleep(&ts, NULL);
1.93 +
1.94 + mask >>= 1;
1.95 + }
1.96 + }
1.97 +
1.98 + _enable_device->set(_enable_pin, 1);
1.99 +}
1.100 +
1.101 +
1.102 +
1.103 +/* C language interface. */
1.104 +
1.105 +void *spi_gpio_get_channel(void *clock_chip, int clock_pin,
1.106 + void *data_chip, int data_pin,
1.107 + void *enable_chip, int enable_pin,
1.108 + uint32_t frequency)
1.109 +{
1.110 + return (void *) new Spi_gpio(reinterpret_cast<Hw::Gpio_chip *>(clock_chip), clock_pin,
1.111 + reinterpret_cast<Hw::Gpio_chip *>(data_chip), data_pin,
1.112 + reinterpret_cast<Hw::Gpio_chip *>(enable_chip), enable_pin,
1.113 + frequency);
1.114 +}
1.115 +
1.116 +void spi_gpio_send(void *channel, int bytes, uint8_t data[])
1.117 +{
1.118 + static_cast<Spi_gpio *>(channel)->send(bytes, data);
1.119 +}