Rishka
RISC-V virtual runtime in C/C++ made for ESP32-WROVER
i2c.h
Go to the documentation of this file.
1/*
2 * This file is part of the Rishka distribution (https://github.com/nthnn/rishka).
3 * Copyright (c) 2024 Nathanne Isip.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
27#ifndef LIBRISHKA_I2C_H
28#define LIBRISHKA_I2C_H
29
30#include <librishka/types.h>
31
42class I2C final {
43public:
53 static bool begin(u8 address);
54
63 static bool end();
64
75 static bool pins(u8 sda, u8 scl);
76
83 static void flush();
84
93 static void begin_transmission(u8 address);
94
105 static u8 end_transmission(bool stop_bit);
106
117 static usize write(u8* data, usize size);
118
129 static usize slave_write(u8* data, usize size);
130
140 static usize set_buffersize(usize size);
141
150 static i32 read();
151
160 static i32 peek();
161
170 static i32 available();
171
183 static usize request(u8 address, usize size, bool stop_bit);
184
194 static void on_receive(void (*callback)(int));
195
205 static void on_request(void (*callback)(void));
206
215 static void set_timeout(u16 timeout);
216
224 static u16 get_timeout();
225
234 static bool set_clock(u32 clock);
235
243 static u32 get_clock();
244};
245
246#endif /* LIBRISHKA_I2C_H */
Class for handling I2C operations in Rishka applications.
Definition: i2c.h:42
static usize set_buffersize(usize size)
Set the buffer size for I2C transactions.
Definition: librishka_i2c.cpp:53
static bool begin(u8 address)
Initialize the I2C communication with the specified address.
Definition: librishka_i2c.cpp:21
static u32 get_clock()
Get the current clock frequency for I2C communication.
Definition: librishka_i2c.cpp:93
static bool pins(u8 sda, u8 scl)
Set the SDA and SCL pins for the I2C communication.
Definition: librishka_i2c.cpp:29
static void on_request(void(*callback)(void))
Register a callback function for I2C request events.
Definition: librishka_i2c.cpp:77
static void begin_transmission(u8 address)
Begin an I2C transmission to the specified device address.
Definition: librishka_i2c.cpp:37
static i32 available()
Get the number of bytes available for reading on the I2C bus.
Definition: librishka_i2c.cpp:65
static bool set_clock(u32 clock)
Set the clock frequency for I2C communication.
Definition: librishka_i2c.cpp:89
static void flush()
Flush the I2C buffer.
Definition: librishka_i2c.cpp:33
static i32 read()
Read data from the I2C bus.
Definition: librishka_i2c.cpp:57
static i32 peek()
Peek at the next byte on the I2C bus.
Definition: librishka_i2c.cpp:61
static usize write(u8 *data, usize size)
Write data to the I2C bus.
Definition: librishka_i2c.cpp:45
static usize slave_write(u8 *data, usize size)
Write data to the I2C bus as a slave device.
Definition: librishka_i2c.cpp:49
static u8 end_transmission(bool stop_bit)
End an I2C transmission with an optional stop bit.
Definition: librishka_i2c.cpp:41
static usize request(u8 address, usize size, bool stop_bit)
Request data from a remote I2C device.
Definition: librishka_i2c.cpp:69
static void set_timeout(u16 timeout)
Set the timeout for I2C operations.
Definition: librishka_i2c.cpp:81
static bool end()
End the I2C communication.
Definition: librishka_i2c.cpp:25
static u16 get_timeout()
Get the current timeout for I2C operations.
Definition: librishka_i2c.cpp:85
static void on_receive(void(*callback)(int))
Register a callback function for I2C receive events.
Definition: librishka_i2c.cpp:73
Header file for common data types used in Rishka applications.
signed int i32
Alias for the signed 32-bit integer type.
Definition: types.h:58
unsigned int u32
Alias for the unsigned 32-bit integer type.
Definition: types.h:82
unsigned short int u16
Alias for the unsigned 16-bit integer type.
Definition: types.h:76
unsigned char u8
Alias for the unsigned 8-bit integer type.
Definition: types.h:70
u32 usize
Alias for the unsigned integer type representing size.
Definition: types.h:94