Skip to content

Commit 2033b39

Browse files
1 parent 02f067b commit 2033b39

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

smbus.h

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
25+
/**
26+
* @file smbus.h
27+
* @brief Interface definitions for the ESP32-compatible SMBus Protocol component.
28+
*
29+
* This component provides structures and functions that are useful for communicating
30+
* with SMBus-compatible I2C slave devices.
31+
*/
32+
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+
{
42+
#endif
43+
44+
#define SMBUS_DEFAULT_TIMEOUT (1000 / portTICK_PERIOD_MS) ///< Default transaction timeout in ticks
45+
/**
46+
* @brief 7-bit or 10-bit I2C slave address.
47+
*/
48+
typedef uint16_t i2c_address_t;
49+
50+
/**
51+
* @brief Structure containing information related to the SMBus protocol.
52+
*/
53+
typedef struct
54+
{
55+
bool init; ///< True if struct has been initialised, otherwise false
56+
i2c_port_t i2c_port; ///< ESP-IDF I2C port number
57+
i2c_address_t address; ///< I2C address of slave device
58+
portBASE_TYPE timeout; ///< Number of ticks until I2C operation timeout
59+
} smbus_info_t;
60+
61+
/**
62+
* @brief Construct a new SMBus info instance.
63+
* New instance should be initialised before calling other functions.
64+
* @return Pointer to new device info instance, or NULL if it cannot be created.
65+
*/
66+
smbus_info_t *smbus_malloc(void);
67+
68+
/**
69+
* @brief Delete an existing SMBus info instance.
70+
* @param[in,out] smbus_info Pointer to SMBus info instance that will be freed and set to NULL.
71+
*/
72+
void smbus_free(smbus_info_t **smbus_info);
73+
74+
/**
75+
* @brief Initialise a SMBus info instance with the specified I2C information.
76+
* The I2C timeout defaults to approximately 1 second.
77+
* @param[in] smbus_info Pointer to SMBus info instance.
78+
* @param[in] i2c_port I2C port to associate with this SMBus instance.
79+
* @param[in] address Address of I2C slave device.
80+
*/
81+
esp_err_t smbus_init(smbus_info_t *smbus_info, i2c_port_t i2c_port, i2c_address_t address);
82+
83+
/**
84+
* @brief Set the I2C timeout.
85+
* I2C transactions that do not complete within this period are considered an error.
86+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
87+
* @param[in] timeout Number of ticks to wait until the transaction is considered in error.
88+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
89+
*/
90+
esp_err_t smbus_set_timeout(smbus_info_t *smbus_info, portBASE_TYPE timeout);
91+
92+
/**
93+
* @brief Send a single bit to a slave device in the place of the read/write bit.
94+
* May be used to simply turn a device function on or off, or enable or disable
95+
* a low-power standby mode. There is no data sent or received.
96+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
97+
* @param[in] bit Data bit to send.
98+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
99+
*/
100+
esp_err_t smbus_quick(const smbus_info_t *smbus_info, bool bit);
101+
102+
/**
103+
* @brief Send a single byte to a slave device.
104+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
105+
* @param[in] data Data byte to send to slave.
106+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
107+
*/
108+
esp_err_t smbus_send_byte(const smbus_info_t *smbus_info, uint8_t data);
109+
110+
/**
111+
* @brief Receive a single byte from a slave device.
112+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
113+
* @param[out] data Data byte received from slave.
114+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
115+
*/
116+
esp_err_t smbus_receive_byte(const smbus_info_t *smbus_info, uint8_t *data);
117+
118+
/**
119+
* @brief Write a single byte to a slave device with a command code.
120+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
121+
* @param[in] command Device-specific command byte.
122+
* @param[in] data Data byte to send to slave.
123+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
124+
*/
125+
esp_err_t smbus_write_byte(const smbus_info_t *smbus_info, uint8_t command, uint8_t data);
126+
127+
/**
128+
* @brief Write a single word (two bytes) to a slave device with a command code.
129+
* The least significant byte is transmitted first.
130+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
131+
* @param[in] command Device-specific command byte.
132+
* @param[in] data Data word to send to slave.
133+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
134+
*/
135+
esp_err_t smbus_write_word(const smbus_info_t *smbus_info, uint8_t command, uint16_t data);
136+
137+
/**
138+
* @brief Read a single byte from a slave device with a command code.
139+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
140+
* @param[in] command Device-specific command byte.
141+
* @param[out] data Data byte received from slave.
142+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
143+
*/
144+
esp_err_t smbus_read_byte(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data);
145+
146+
/**
147+
* @brief Read a single word (two bytes) from a slave device with a command code.
148+
* The first byte received is the least significant byte.
149+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
150+
* @param[in] command Device-specific command byte.
151+
* @param[out] data Data byte received from slave.
152+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
153+
*/
154+
esp_err_t smbus_read_word(const smbus_info_t *smbus_info, uint8_t command, uint16_t *data);
155+
156+
/**
157+
* @brief Write up to 255 bytes to a slave device with a command code.
158+
* This uses a byte count to negotiate the length of the transaction.
159+
* The first byte in the data array is transmitted first.
160+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
161+
* @param[in] command Device-specific command byte.
162+
* @param[in] data Data bytes to send to slave.
163+
* @param[in] len Number of bytes to send to slave.
164+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
165+
*/
166+
esp_err_t smbus_write_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, uint8_t len);
167+
168+
/**
169+
* @brief Read up to 255 bytes from a slave device with a command code.
170+
* This uses a byte count to negotiate the length of the transaction.
171+
* The first byte received is placed in the first array location.
172+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
173+
* @param[in] command Device-specific command byte.
174+
* @param[out] data Data bytes received from slave.
175+
* @param[in/out] len Size of data array, and number of bytes actually received.
176+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
177+
*/
178+
esp_err_t smbus_read_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, uint8_t *len);
179+
180+
/**
181+
* @brief Write bytes to a slave device with a command code.
182+
* No byte count is used - the transaction lasts as long as the master requires.
183+
* The first byte in the data array is transmitted first.
184+
* This operation is not defined by the SMBus specification.
185+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
186+
* @param[in] command Device-specific command byte.
187+
* @param[in] data Data bytes to send to slave.
188+
* @param[in] len Number of bytes to send to slave.
189+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
190+
*/
191+
esp_err_t smbus_i2c_write_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len);
192+
193+
/**
194+
* @brief Read bytes from a slave device with a command code (combined format).
195+
* No byte count is used - the transaction lasts as long as the master requires.
196+
* The first byte received is placed in the first array location.
197+
* This operation is not defined by the SMBus specification.
198+
* @param[in] smbus_info Pointer to initialised SMBus info instance.
199+
* @param[in] command Device-specific command byte.
200+
* @param[out] data Data bytes received from slave.
201+
* @param[in/out] len Size of data array. If the slave fails to provide sufficient bytes, ESP_ERR_TIMEOUT will be returned.
202+
* @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
203+
*/
204+
esp_err_t smbus_i2c_read_block(const smbus_info_t *smbus_info, uint8_t command, uint8_t *data, size_t len);
205+
206+
#ifdef __cplusplus
207+
}
208+
#endif
209+
210+
#endif // SMBUS_H

0 commit comments

Comments
 (0)