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