1 /* 2 * Manual GPIO-based I2C communication. 3 * 4 * Copyright (C) 2013, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #pragma once 23 24 #include <l4/devices/gpio.h> 25 #include <stdint.h> 26 27 /* I2C modifiers. */ 28 29 #define I2C_READ 1 30 #define I2C_WRITE 0 31 32 #ifdef __cplusplus 33 34 class I2c_gpio 35 { 36 Hw::Gpio_chip *_scl_chip; 37 int I2C_SCL; 38 Hw::Gpio_chip *_sda_chip; 39 int I2C_SDA; 40 41 void CLR(int pin); 42 void SET(int pin); 43 void IN(int pin); 44 int PIN(int pin); 45 void wait(); 46 47 public: 48 explicit I2c_gpio(Hw::Gpio_chip *scl_chip, int scl, Hw::Gpio_chip *sda_chip, int sda); 49 50 void start(); 51 void stop(); 52 void ack(bool ack); 53 uint8_t recv(); 54 void recvmany(uint8_t *data, uint8_t len); 55 bool send(uint8_t data); 56 bool sendmany(uint8_t *data, uint8_t len); 57 }; 58 59 #endif /* __cplusplus */ 60 61 62 63 /* C language interface. */ 64 65 EXTERN_C_BEGIN 66 67 void *i2c_gpio_get_channel(void *scl_chip, int scl, void *sda_chip, int sda); 68 void i2c_gpio_start(void *channel); 69 void i2c_gpio_stop(void *channel); 70 void i2c_gpio_ack(void *channel, bool ack); 71 uint8_t i2c_gpio_recv(void *channel); 72 void i2c_gpio_recvmany(void *channel, uint8_t *data, uint8_t len); 73 bool i2c_gpio_send(void *channel, uint8_t data); 74 bool i2c_gpio_sendmany(void *channel, uint8_t *data, uint8_t len); 75 76 EXTERN_C_END