Ultibo API
C/C++ API for Ultibo Core
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1/*
2 * This file is part of the Ultibo project, https://ultibo.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2026 Garry Wood <garry@softoz.com.au>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26#ifndef _ULTIBO_I2C_H
27#define _ULTIBO_I2C_H
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#include "ultibo/globaltypes.h"
34#include "ultibo/globalconst.h"
35#include "ultibo/devices.h"
36
38#define I2C_NAME_PREFIX "I2C"
39#define I2CSLAVE_NAME_PREFIX "I2CSlave"
40
42#define I2C_TYPE_NONE 0
43#define I2C_TYPE_MASTER 1
44#define I2C_TYPE_SLAVE 2
45
46#define I2C_TYPE_MAX 2
47
49#define I2C_STATE_DISABLED 0
50#define I2C_STATE_ENABLED 1
51
52#define I2C_STATE_MAX 1
53
55#define I2C_FLAG_NONE 0x00000000
56#define I2C_FLAG_SLAVE 0x00000001
57#define I2C_FLAG_10BIT 0x00000002
58#define I2C_FLAG_16BIT 0x00000004
59#define I2C_FLAG_DMA 0x00000008
60
62#define I2C_TRANSFER_NONE 0x00000000
63#define I2C_TRANSFER_DMA 0x00000001
64#define I2C_TRANSFER_IGNORE_NAK 0x00000002
65
67
71{
72 uint32_t flags;
73 uint32_t maxsize;
74 uint32_t minclock;
75 uint32_t maxclock;
76 uint32_t clockrate;
77 uint16_t slaveaddress;
78};
79
81typedef struct _I2C_DEVICE I2C_DEVICE;
82
84typedef uint32_t STDCALL (*i2c_enumerate_cb)(I2C_DEVICE *i2c, void *data);
86typedef uint32_t STDCALL (*i2c_notification_cb)(DEVICE *device, void *data, uint32_t notification);
87
89typedef uint32_t STDCALL (*i2c_device_start_proc)(I2C_DEVICE *i2c, uint32_t rate);
90typedef uint32_t STDCALL (*i2c_device_stop_proc)(I2C_DEVICE *i2c);
91
92typedef uint32_t STDCALL (*i2c_device_read_proc)(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
93typedef uint32_t STDCALL (*i2c_device_write_proc)(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
94typedef uint32_t STDCALL (*i2c_device_write_read_proc)(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count);
95typedef uint32_t STDCALL (*i2c_device_write_write_proc)(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count);
96
98typedef uint32_t STDCALL (*i2c_device_set_rate_proc)(I2C_DEVICE *i2c, uint32_t rate);
99
101typedef uint32_t STDCALL (*i2c_device_set_address_proc)(I2C_DEVICE *i2c, uint16_t address);
102
104
138
140
147uint32_t STDCALL i2c_device_start(I2C_DEVICE *i2c, uint32_t rate);
148
155
165uint32_t STDCALL i2c_device_read(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t *count);
166
177uint32_t STDCALL i2c_device_read_ex(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
178
188uint32_t STDCALL i2c_device_write(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t *count);
189
200uint32_t STDCALL i2c_device_write_ex(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count);
201
214uint32_t STDCALL i2c_device_write_read(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t *count);
215
229uint32_t STDCALL i2c_device_write_read_ex(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count);
230
243uint32_t STDCALL i2c_device_write_write(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t *count);
244
258uint32_t STDCALL i2c_device_write_write_ex(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count);
259
266
273uint32_t STDCALL i2c_device_set_rate(I2C_DEVICE *i2c, uint32_t rate);
274
281
288uint32_t STDCALL i2c_device_set_address(I2C_DEVICE *i2c, uint16_t address);
289
298
306
312
319
326
333
340
347
354
361
368uint32_t STDCALL i2c_device_enumerate(i2c_enumerate_cb callback, void *data);
369
378uint32_t STDCALL i2c_device_notification(I2C_DEVICE *i2c, i2c_notification_cb callback, void *data, uint32_t notification, uint32_t flags);
379
381
388
395
404uint32_t STDCALL i2c_slave_read(I2C_DEVICE *i2c, void *buffer, uint32_t size, uint32_t *count);
405
414uint32_t STDCALL i2c_slave_write(I2C_DEVICE *i2c, void *buffer, uint32_t size, uint32_t *count);
415
422
429uint32_t STDCALL i2c_slave_set_address(I2C_DEVICE *i2c, uint16_t address);
430
438
444
451
458
465
472
479
486
493
495
499uint32_t STDCALL i2c_get_count(void);
500
505
510
515
520
524uint32_t STDCALL i2c_type_to_string(uint32_t i2ctype, char *string, uint32_t len);
525
529uint32_t STDCALL i2c_state_to_string(uint32_t i2cstate, char *string, uint32_t len);
530
535
540
541#ifdef __cplusplus
542}
543#endif
544
545#endif // _ULTIBO_I2C_H
struct _DEVICE DEVICE
Definition devices.h:373
#define STDCALL
Definition globaltypes.h:45
_Bool BOOL
Built in Boolean type (1 byte) (Redeclared here for flexibility).
Definition globaltypes.h:55
HANDLE SEMAPHORE_HANDLE
Definition globaltypes.h:107
HANDLE MUTEX_HANDLE
Definition globaltypes.h:105
uint32_t STDCALL i2c_device_properties(I2C_DEVICE *i2c, I2C_PROPERTIES *properties)
Get the properties for the specified I2C device.
uint32_t STDCALL i2c_device_destroy(I2C_DEVICE *i2c)
Destroy an existing I2C entry.
BOOL STDCALL i2c_is7bit_address(uint16_t address)
Determine if the supplied address is a 7bit address.
uint32_t STDCALL(* i2c_device_write_proc)(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Definition i2c.h:93
uint32_t STDCALL i2c_type_to_string(uint32_t i2ctype, char *string, uint32_t len)
Convert an I2C type value to a string.
uint32_t STDCALL i2c_device_write_read_ex(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count)
Write data to and Read data from the specified I2C device in one operation Useful for devices that re...
uint32_t STDCALL i2c_device_write_ex(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Write data to the specified I2C device.
uint32_t STDCALL(* i2c_device_start_proc)(I2C_DEVICE *i2c, uint32_t rate)
Definition i2c.h:89
uint32_t STDCALL i2c_device_get_properties(I2C_DEVICE *i2c, I2C_PROPERTIES *properties)
Get the properties for the specified I2C device.
uint32_t STDCALL i2c_device_register(I2C_DEVICE *i2c)
Register a new I2C in the I2C table.
uint32_t STDCALL(* i2c_device_read_proc)(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Definition i2c.h:92
uint32_t STDCALL(* i2c_device_get_rate_proc)(I2C_DEVICE *i2c)
Definition i2c.h:97
uint32_t STDCALL i2c_device_start(I2C_DEVICE *i2c, uint32_t rate)
Start the specified I2C device ready for reading and writing.
I2C_DEVICE *STDCALL i2c_device_find_by_description(const char *description)
Find an I2C device by description in the device table.
I2C_DEVICE *STDCALL i2c_device_find(uint32_t i2cid)
Find an I2C device by ID in the I2C table.
I2C_DEVICE *STDCALL i2c_slave_find_by_description(const char *description)
Find an I2C slave by description in the device table.
uint32_t STDCALL i2c_device_deregister(I2C_DEVICE *i2c)
Deregister an I2C from the I2C table.
uint32_t STDCALL i2c_device_notification(I2C_DEVICE *i2c, i2c_notification_cb callback, void *data, uint32_t notification, uint32_t flags)
Register a notification for I2C device changes.
uint32_t STDCALL i2c_slave_destroy(I2C_DEVICE *i2c)
Destroy an existing I2C slave entry.
uint32_t STDCALL i2c_device_write_write_ex(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count)
Write 2 data blocks to the specified I2C device in one operation Useful for devices that require a re...
uint32_t STDCALL i2c_device_write_write(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t *count)
Write 2 data blocks to the specified I2C device in one operation Useful for devices that require a re...
uint32_t STDCALL i2c_slave_register(I2C_DEVICE *i2c)
Register a new I2C slave in the I2C table.
I2C_DEVICE *STDCALL i2c_device_create(void)
Create a new I2C entry.
struct _I2C_PROPERTIES I2C_PROPERTIES
Definition i2c.h:69
uint32_t STDCALL i2c_get_count(void)
Get the current I2C count.
uint32_t STDCALL i2c_device_write(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t *count)
Write data to the specified I2C device.
uint32_t STDCALL i2c_device_stop(I2C_DEVICE *i2c)
Stop the specified I2C device and terminate reading and writing.
uint32_t STDCALL(* i2c_device_write_read_proc)(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count)
Definition i2c.h:94
I2C_DEVICE *STDCALL i2c_device_find_by_name(const char *name)
Find an I2C device by name in the device table.
uint32_t STDCALL i2c_slave_read(I2C_DEVICE *i2c, void *buffer, uint32_t size, uint32_t *count)
Read data from the specified I2C slave.
uint32_t STDCALL i2c_state_to_string(uint32_t i2cstate, char *string, uint32_t len)
Convert an I2C state value to a string.
uint32_t STDCALL(* i2c_enumerate_cb)(I2C_DEVICE *i2c, void *data)
Definition i2c.h:84
I2C_DEVICE *STDCALL i2c_slave_find(uint32_t i2cid)
Find an I2C slave by ID in the I2C table.
I2C_DEVICE *STDCALL i2c_slave_find_by_name(const char *name)
Find an I2C slave by name in the device table.
uint32_t STDCALL(* i2c_device_stop_proc)(I2C_DEVICE *i2c)
Definition i2c.h:90
uint32_t STDCALL i2c_device_enumerate(i2c_enumerate_cb callback, void *data)
Enumerate all I2C devices in the I2C table.
uint32_t STDCALL i2c_slave_deregister(I2C_DEVICE *i2c)
Deregister an I2C slave from the I2C table.
I2C_DEVICE *STDCALL i2c_device_check(I2C_DEVICE *i2c)
Check if the supplied I2C is in the I2C table.
uint32_t STDCALL i2c_slave_start(I2C_DEVICE *i2c)
Start the specified I2C slave ready for reading and writing.
uint32_t STDCALL(* i2c_notification_cb)(DEVICE *device, void *data, uint32_t notification)
Definition i2c.h:86
uint32_t STDCALL(* i2c_device_set_rate_proc)(I2C_DEVICE *i2c, uint32_t rate)
Definition i2c.h:98
uint32_t STDCALL i2c_device_write_read(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t *count)
Write data to and Read data from the specified I2C device in one operation Useful for devices that re...
I2C_DEVICE *STDCALL i2c_device_get_default(void)
Get the current default I2C device.
uint32_t STDCALL(* i2c_device_get_properties_proc)(I2C_DEVICE *i2c, I2C_PROPERTIES *properties)
Definition i2c.h:103
uint32_t STDCALL i2c_device_get_rate(I2C_DEVICE *i2c)
Get the clock rate of the specified I2C device.
I2C_DEVICE *STDCALL i2c_slave_create_ex(uint32_t size)
Create a new I2C slave entry.
uint32_t STDCALL(* i2c_device_set_address_proc)(I2C_DEVICE *i2c, uint16_t address)
Definition i2c.h:101
uint32_t STDCALL i2c_slave_stop(I2C_DEVICE *i2c)
Stop the specified I2C slave and terminate reading and writing.
uint16_t STDCALL(* i2c_device_get_address_proc)(I2C_DEVICE *i2c)
Definition i2c.h:100
I2C_DEVICE *STDCALL i2c_device_create_ex(uint32_t size)
Create a new I2C entry.
uint16_t STDCALL i2c_slave_get_address(I2C_DEVICE *i2c)
Get the address for the specified I2C slave.
struct _I2C_DEVICE I2C_DEVICE
Definition i2c.h:81
uint32_t STDCALL i2c_device_set_default(I2C_DEVICE *i2c)
Set the current default I2C device.
BOOL STDCALL i2c_device_is_slave(I2C_DEVICE *i2c)
Check if the supplied I2C is a slave device.
uint32_t STDCALL i2c_slave_write(I2C_DEVICE *i2c, void *buffer, uint32_t size, uint32_t *count)
Write data to the specified I2C slave.
uint32_t STDCALL i2c_slave_set_address(I2C_DEVICE *i2c, uint16_t address)
Set the address for the specified I2C slave.
uint16_t STDCALL i2c_device_get_address(I2C_DEVICE *i2c)
Get the slave address for the specified I2C device.
uint32_t STDCALL i2c_device_read(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t *count)
Read data from the specified I2C device.
I2C_DEVICE *STDCALL i2c_slave_create(void)
Create a new I2C slave entry.
BOOL STDCALL i2c_is10bit_address(uint16_t address)
Determine if the supplied address is a 10bit address.
uint32_t STDCALL(* i2c_device_write_write_proc)(I2C_DEVICE *i2c, uint16_t address, void *initial, uint32_t len, void *data, uint32_t size, uint32_t flags, uint32_t *count)
Definition i2c.h:95
uint32_t STDCALL i2c_device_read_ex(I2C_DEVICE *i2c, uint16_t address, void *buffer, uint32_t size, uint32_t flags, uint32_t *count)
Read data from the specified I2C device.
uint32_t STDCALL i2c_slave_get_properties(I2C_DEVICE *i2c, I2C_PROPERTIES *properties)
Get the properties for the specified I2C slave.
uint32_t STDCALL i2c_device_set_rate(I2C_DEVICE *i2c, uint32_t rate)
Set the clock rate for the specified I2C device.
uint32_t STDCALL i2c_device_set_address(I2C_DEVICE *i2c, uint16_t address)
Set the slave address for the specified I2C device.
Definition i2c.h:106
MUTEX_HANDLE lock
Device lock.
Definition i2c.h:129
uint32_t writecount
Definition i2c.h:125
uint32_t i2cstate
I2C state (eg I2C_STATE_ENABLED).
Definition i2c.h:111
i2c_device_get_properties_proc devicegetproperties
A Device specific DeviceGetProperties method implementing the standard I2C device interface (Or nil i...
Definition i2c.h:122
DEVICE device
The Device entry for this I2C.
Definition i2c.h:108
i2c_device_set_address_proc devicesetaddress
A Device specific DeviceSetAddress method implementing the standard I2C device interface (Or nil if t...
Definition i2c.h:121
I2C_PROPERTIES properties
Device properties.
Definition i2c.h:133
uint32_t writeerrors
Definition i2c.h:127
i2c_device_write_proc devicewrite
A Device specific DeviceWrite method implementing the standard I2C device interface (Mandatory).
Definition i2c.h:115
I2C_DEVICE * next
Next entry in I2C table.
Definition i2c.h:136
i2c_device_stop_proc devicestop
A Device specific DeviceStop method implementing the standard I2C device interface (Mandatory).
Definition i2c.h:113
i2c_device_write_read_proc devicewriteread
A Device specific DeviceWriteRead method implementing the standard I2C device interface (Or nil if th...
Definition i2c.h:116
uint32_t readerrors
Definition i2c.h:126
I2C_DEVICE * prev
Previous entry in I2C table.
Definition i2c.h:135
i2c_device_read_proc deviceread
A Device specific DeviceRead method implementing the standard I2C device interface (Mandatory).
Definition i2c.h:114
uint32_t readcount
Definition i2c.h:124
i2c_device_set_rate_proc devicesetrate
A Device specific DeviceSetRate method implementing the standard I2C device interface (Or nil if the ...
Definition i2c.h:119
i2c_device_get_rate_proc devicegetrate
A Device specific DeviceGetRate method implementing the standard I2C device interface (Or nil if the ...
Definition i2c.h:118
uint32_t clockrate
Clock rate (Hz).
Definition i2c.h:131
SEMAPHORE_HANDLE wait
Read/Write wait event.
Definition i2c.h:130
i2c_device_write_write_proc devicewritewrite
A Device specific DeviceWriteWrite method implementing the standard I2C device interface (Or nil if t...
Definition i2c.h:117
uint32_t i2cid
Unique Id of this I2C in the I2C table.
Definition i2c.h:110
uint16_t slaveaddress
Slave address.
Definition i2c.h:132
i2c_device_start_proc devicestart
A Device specific DeviceStart method implementing the standard I2C device interface (Mandatory).
Definition i2c.h:112
i2c_device_get_address_proc devicegetaddress
A Device specific DeviceGetAddress method implementing the standard I2C device interface (Or nil if t...
Definition i2c.h:120
Definition i2c.h:71
uint32_t maxclock
Maximum supported clock rate.
Definition i2c.h:75
uint32_t flags
Device flags (eg I2C_FLAG_SLAVE).
Definition i2c.h:72
uint32_t minclock
Minimum supported clock rate.
Definition i2c.h:74
uint32_t maxsize
Maximum supported data transfer size.
Definition i2c.h:73
uint32_t clockrate
Current clock rate.
Definition i2c.h:76
uint16_t slaveaddress
Current slave address.
Definition i2c.h:77