esp32-smbus
ESP32-compatible C library for I2C SMBus Protocol.
 All Data Structures Files Functions Variables Typedefs Macros
smbus.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2017 David Antliff
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #ifndef SMBUS_H
34 #define SMBUS_H
35 
36 #include <stdbool.h>
37 #include "driver/i2c.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #define SMBUS_DEFAULT_TIMEOUT (1000 / portTICK_RATE_MS)
44 
47 typedef uint16_t i2c_address_t;
48 
52 typedef struct
53 {
54  bool init;
55  i2c_port_t i2c_port;
57  portBASE_TYPE timeout;
58 } smbus_info_t;
59 
66 
71 void smbus_free(smbus_info_t ** smbus_info);
72 
80 esp_err_t smbus_init(smbus_info_t * smbus_info, i2c_port_t i2c_port, i2c_address_t address);
81 
89 esp_err_t smbus_set_timeout(smbus_info_t * smbus_info, portBASE_TYPE timeout);
90 
99 esp_err_t smbus_quick(const smbus_info_t * smbus_info, bool bit);
100 
107 esp_err_t smbus_send_byte(const smbus_info_t * smbus_info, uint8_t data);
108 
115 esp_err_t smbus_receive_byte(const smbus_info_t * smbus_info, uint8_t * data);
116 
124 esp_err_t smbus_write_byte(const smbus_info_t * smbus_info, uint8_t command, uint8_t data);
125 
134 esp_err_t smbus_write_word(const smbus_info_t * smbus_info, uint8_t command, uint16_t data);
135 
143 esp_err_t smbus_read_byte(const smbus_info_t * smbus_info, uint8_t command, uint8_t * data);
144 
153 esp_err_t smbus_read_word(const smbus_info_t * smbus_info, uint8_t command, uint16_t * data);
154 
165 esp_err_t smbus_write_block(const smbus_info_t * smbus_info, uint8_t command, uint8_t * data, uint8_t len);
166 
177 esp_err_t smbus_read_block(const smbus_info_t * smbus_info, uint8_t command, uint8_t * data, uint8_t * len);
178 
190 esp_err_t smbus_i2c_write_block(const smbus_info_t * smbus_info, uint8_t command, uint8_t * data, size_t len);
191 
203 esp_err_t smbus_i2c_read_block(const smbus_info_t * smbus_info, uint8_t command, uint8_t * data, size_t len);
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif // SMBUS_H
bool init
True if struct has been initialised, otherwise false.
Definition: smbus.h:54
esp_err_t smbus_write_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, uint8_t len)
Write up to 255 bytes to a slave device with a command code. This uses a byte count to negotiate the ...
Definition: smbus.c:317
esp_err_t smbus_write_word(const smbus_info_t *smbus_info, uint8_t command, uint16_t data)
Write a single word (two bytes) to a slave device with a command code. The least significant byte is ...
Definition: smbus.c:284
esp_err_t smbus_write_byte(const smbus_info_t *smbus_info, uint8_t command, uint8_t data)
Write a single byte to a slave device with a command code.
Definition: smbus.c:278
esp_err_t smbus_i2c_write_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len)
Write bytes to a slave device with a command code. No byte count is used - the transaction lasts as l...
Definition: smbus.c:391
void smbus_free(smbus_info_t **smbus_info)
Delete an existing SMBus info instance.
Definition: smbus.c:186
smbus_info_t * smbus_malloc(void)
Construct a new SMBus info instance. New instance should be initialised before calling other function...
Definition: smbus.c:171
Structure containing information related to the SMBus protocol.
Definition: smbus.h:52
esp_err_t smbus_read_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, uint8_t *len)
Read up to 255 bytes from a slave device with a command code. This uses a byte count to negotiate the...
Definition: smbus.c:339
esp_err_t smbus_send_byte(const smbus_info_t *smbus_info, uint8_t data)
Send a single byte to a slave device.
Definition: smbus.c:244
esp_err_t smbus_init(smbus_info_t *smbus_info, i2c_port_t i2c_port, i2c_address_t address)
Initialise a SMBus info instance with the specified I2C information. The I2C timeout defaults to appr...
Definition: smbus.c:200
esp_err_t smbus_set_timeout(smbus_info_t *smbus_info, portBASE_TYPE timeout)
Set the I2C timeout. I2C transactions that do not complete within this period are considered an error...
Definition: smbus.c:217
i2c_address_t address
I2C address of slave device.
Definition: smbus.h:56
i2c_port_t i2c_port
ESP-IDF I2C port number.
Definition: smbus.h:55
esp_err_t smbus_i2c_read_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len)
Read bytes from a slave device with a command code (combined format). No byte count is used - the tra...
Definition: smbus.c:397
portBASE_TYPE timeout
Number of ticks until I2C operation timeout.
Definition: smbus.h:57
esp_err_t smbus_quick(const smbus_info_t *smbus_info, bool bit)
Send a single bit to a slave device in the place of the read/write bit. May be used to simply turn a ...
Definition: smbus.c:228
esp_err_t smbus_read_byte(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data)
Read a single byte from a slave device with a command code.
Definition: smbus.c:291
uint16_t i2c_address_t
7-bit or 10-bit I2C slave address.
Definition: smbus.h:47
esp_err_t smbus_receive_byte(const smbus_info_t *smbus_info, uint8_t *data)
Receive a single byte from a slave device.
Definition: smbus.c:261
esp_err_t smbus_read_word(const smbus_info_t *smbus_info, uint8_t command, uint16_t *data)
Read a single word (two bytes) from a slave device with a command code. The first byte received is th...
Definition: smbus.c:297