-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
126 lines (117 loc) · 3.48 KB
/
i2c.c
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
122
123
124
125
126
/**
******************************************************************************
* @file i2c.c
* @author - Timothy Gorbunov
* - Anthoy Raterta
* @version V1.0.0
* @date 13-September-2024
* @brief I2C wrappers for Zephyr I2C
******************************************************************************
* @attention
*
* Copyright (c) 2024 Timothy Gorbunov, mcu-dev
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "i2c.h"
#ifdef PLATFORM_ZEPHYR
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>
#define i2c0_master DT_NODELABEL(i2c0)
const struct device *i2c0_dev;
#endif
/**
* @brief Initializes the I2C interface.
*
* This function sets up the I2C peripheral for communication.
*
* @param None.
*
* @return `true` if the initialization is successful,
* `false` otherwise (future implementation).
*/
bool i2c_init(void) {
#ifdef PLATFORM_ZEPHYR
i2c0_dev = DEVICE_DT_GET(i2c0_master);
if (!device_is_ready(i2c0_dev)) {
printk("I2C bus is not ready!\n\r");
return false;
}
#endif
return true;
}
/**
* @brief Writes data to a register of the device over I2C.
*
* This function writes data to a specified register of the device using
* the provided data buffer.
*
* @param dev_addr I2C address of the device.
* @param data_buffer Pointer to the buffer containing the data to be written.
*
* @return
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t i2c_write_byte(uint8_t dev_addr, uint8_t *data_buffer) {
#ifdef PLATFORM_ZEPHYR
uint32_t bytecount = 2;
return i2c_write(i2c0_dev, data_buffer, bytecount, dev_addr);
#endif
}
/**
* @brief Writes multiple data to a register of the device over I2C.
*
* This function writes data to a specified register of the device using
* the provided data buffer.
*
* @param dev_addr I2C address of the device.
* @param data_buffer Pointer to the buffer containing the data to be written.
* @param bytecount Number of bytes to be sent via I2C
*
* @return
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t i2c_write_multiple_bytes(uint8_t dev_addr, uint8_t *data_buffer,
uint32_t bytecount) {
#ifdef PLATFORM_ZEPHYR
return i2c_write(i2c0_dev, data_buffer, bytecount, dev_addr);
#endif
}
/**
* @brief Reads a register value from the device via I2C.
*
* Reads a single register from the device and stores the retrieved value
* in the specified buffer.
*
* @param dev_addr I2C address of the device
* @param data_read_virtual_address Register address to read from.
* @param read_data Pointer to the buffer where the read value will be stored.
*
* @return
* - 0 on success.
* - Non-zero error code on failure.
*/
int8_t i2c_read_byte(uint8_t dev_addr, uint8_t data_read_virtual_address,
uint8_t *read_data) {
#ifdef PLATFORM_ZEPHYR
uint32_t bytecount = 1;
if (i2c_write(i2c0_dev, &data_read_virtual_address, bytecount, dev_addr) !=
I2C_STATUS_SUCCESS) {
return I2C_STATUS_ERR;
}
if (i2c_read(i2c0_dev, read_data, sizeof(*read_data), dev_addr) !=
I2C_STATUS_SUCCESS) {
return I2C_STATUS_ERR;
}
return I2C_STATUS_SUCCESS;
#endif
}