1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/lib/i2c/include/i2c-gpio.h Sat Oct 14 01:55:20 2023 +0200
1.3 @@ -0,0 +1,76 @@
1.4 +/*
1.5 + * Manual GPIO-based I2C communication.
1.6 + *
1.7 + * Copyright (C) 2013, 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 +#pragma once
1.26 +
1.27 +#include <l4/devices/gpio.h>
1.28 +#include <stdint.h>
1.29 +
1.30 +/* I2C modifiers. */
1.31 +
1.32 +#define I2C_READ 1
1.33 +#define I2C_WRITE 0
1.34 +
1.35 +#ifdef __cplusplus
1.36 +
1.37 +class I2c_gpio
1.38 +{
1.39 + Hw::Gpio_chip *_scl_chip;
1.40 + int I2C_SCL;
1.41 + Hw::Gpio_chip *_sda_chip;
1.42 + int I2C_SDA;
1.43 +
1.44 + void CLR(int pin);
1.45 + void SET(int pin);
1.46 + void IN(int pin);
1.47 + int PIN(int pin);
1.48 + void wait();
1.49 +
1.50 +public:
1.51 + explicit I2c_gpio(Hw::Gpio_chip *scl_chip, int scl, Hw::Gpio_chip *sda_chip, int sda);
1.52 +
1.53 + void start();
1.54 + void stop();
1.55 + void ack(bool ack);
1.56 + uint8_t recv();
1.57 + void recvmany(uint8_t *data, uint8_t len);
1.58 + bool send(uint8_t data);
1.59 + bool sendmany(uint8_t *data, uint8_t len);
1.60 +};
1.61 +
1.62 +#endif /* __cplusplus */
1.63 +
1.64 +
1.65 +
1.66 +/* C language interface. */
1.67 +
1.68 +EXTERN_C_BEGIN
1.69 +
1.70 +void *i2c_gpio_get_channel(void *scl_chip, int scl, void *sda_chip, int sda);
1.71 +void i2c_gpio_start(void *channel);
1.72 +void i2c_gpio_stop(void *channel);
1.73 +void i2c_gpio_ack(void *channel, bool ack);
1.74 +uint8_t i2c_gpio_recv(void *channel);
1.75 +void i2c_gpio_recvmany(void *channel, uint8_t *data, uint8_t len);
1.76 +bool i2c_gpio_send(void *channel, uint8_t data);
1.77 +bool i2c_gpio_sendmany(void *channel, uint8_t *data, uint8_t len);
1.78 +
1.79 +EXTERN_C_END