-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathi2c.h
More file actions
121 lines (106 loc) · 4.22 KB
/
i2c.h
File metadata and controls
121 lines (106 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*****************************************************************************
* | File : i2c.h
* | Author : Waveshare team
* | Function : Hardware underlying interface
* | Info :
* | I2C driver code for I2C communication.
* ----------------
* | This version : V1.0
* | Date : 2024-11-26
* | Info : Basic version
*
******************************************************************************/
#ifndef I2C_H
#define I2C_H
#include <stdio.h> // Standard input/output library
#include <string.h> // String manipulation functions
#include "driver/i2c_master.h" // ESP32 I2C master driver library
#include "esp_log.h" // ESP32 logging library for debugging
// Define the SDA (data) and SCL (clock) pins for I2C communication
#define EXAMPLE_I2C_MASTER_SDA GPIO_NUM_7 // SDA pin
#define EXAMPLE_I2C_MASTER_SCL GPIO_NUM_8 // SCL pin
// Define the I2C frequency (400 kHz)
#define EXAMPLE_I2C_MASTER_FREQUENCY (400 * 1000) // I2C speed
// Define the I2C master port number (I2C_NUM_0 in this case)
#define EXAMPLE_I2C_MASTER_NUM I2C_NUM_0
typedef struct {
i2c_master_bus_handle_t bus;
i2c_master_dev_handle_t dev;
} DEV_I2C_Port;
// Function prototypes for I2C communication
/**
* @brief Initialize the I2C master interface.
*
* This function sets up the I2C master interface, including the SDA/SCL pins,
* clock frequency, and device address.
*
* @return A DEV_I2C_Port with bus/dev handles. Check bus != NULL for success.
*/
DEV_I2C_Port DEV_I2C_Init();
/**
* @brief Set a new I2C slave address for the device.
*
* This function allows you to change the slave address of an I2C device during runtime.
* It removes the old device before adding the new one to prevent handle leaks.
*
* @param dev_handle The handle to the I2C device.
* @param Addr The new I2C address to set for the device.
*/
void DEV_I2C_Set_Slave_Addr(i2c_master_dev_handle_t *dev_handle, uint8_t Addr);
/**
* @brief Write a single byte to the I2C device.
*
* This function sends a command byte and a data byte to the I2C device.
*
* @param dev_handle The handle to the I2C device.
* @param Cmd The command byte to send to the device.
* @param value The value byte to send to the device.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t DEV_I2C_Write_Byte(i2c_master_dev_handle_t dev_handle, uint8_t Cmd, uint8_t value);
/**
* @brief Read a single byte from the I2C device.
*
* This function reads one byte of data from the I2C device.
*
* @param dev_handle The handle to the I2C device.
* @param out Pointer to store the byte read from the I2C device.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t DEV_I2C_Read_Byte(i2c_master_dev_handle_t dev_handle, uint8_t *out);
/**
* @brief Read a word (2 bytes) from the I2C device.
*
* This function sends a command byte and reads two bytes from the I2C device.
* It combines the two bytes into a 16-bit word.
*
* @param dev_handle The handle to the I2C device.
* @param Cmd The command byte to send.
* @param out Pointer to store the 16-bit word read from the device.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t DEV_I2C_Read_Word(i2c_master_dev_handle_t dev_handle, uint8_t Cmd, uint16_t *out);
/**
* @brief Write multiple bytes to the I2C device.
*
* This function sends a block of data (multiple bytes) to the I2C device.
*
* @param dev_handle The handle to the I2C device.
* @param pdata A pointer to the data to write.
* @param len The number of bytes to write.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t DEV_I2C_Write_Nbyte(i2c_master_dev_handle_t dev_handle, uint8_t *pdata, uint8_t len);
/**
* @brief Read multiple bytes from the I2C device.
*
* This function sends a command byte and reads multiple bytes from the I2C device.
*
* @param dev_handle The handle to the I2C device.
* @param Cmd The command byte to send.
* @param pdata A pointer to the buffer to store the received data.
* @param len The number of bytes to read.
* @return ESP_OK on success, or an error code on failure.
*/
esp_err_t DEV_I2C_Read_Nbyte(i2c_master_dev_handle_t dev_handle, uint8_t Cmd, uint8_t *pdata, uint8_t len);
#endif // I2C_H