Skip to content

Commit 8a280a4

Browse files
committed
add: zephyr i2c wrapper
1 parent f8d7bb8 commit 8a280a4

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

i2c.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
******************************************************************************
3+
* @file i2c.c
4+
* @author - Anthony E.Raterta
5+
* @version V1.0.0
6+
* @date 13-September-2024
7+
* @brief I2C wrappers for Zephyr I2C
8+
******************************************************************************
9+
* @attention
10+
*
11+
* Copyright (c) 2024 mcu-dev
12+
* All rights reserved.
13+
*
14+
* This software is licensed under terms that can be found in the LICENSE file
15+
* in the root directory of this software component.
16+
* If no LICENSE file comes with this software, it is provided AS-IS.
17+
*
18+
******************************************************************************
19+
*/
20+
#include <stdint.h>
21+
#include <stdio.h>
22+
#include <zephyr/device.h>
23+
#include <zephyr/devicetree.h>
24+
#include <zephyr/drivers/i2c.h>
25+
#include <zephyr/kernel.h>
26+
27+
#include "i2c.h"
28+
29+
#define i2c0_master DT_NODELABEL(i2c0)
30+
31+
bool init_i2c0(struct device **bus) {
32+
*bus = DEVICE_DT_GET(i2c0_master);
33+
if (!device_is_ready(*bus)) {
34+
printk("I2C bus is not ready!\n\r");
35+
return false;
36+
}
37+
return true;
38+
}
39+
40+
int i2c0_write_bytes(struct device **bus, uint8_t address,
41+
uint8_t *data_buffer) {
42+
uint32_t bytecount = 2;
43+
return i2c_write(*bus, data_buffer, bytecount, address);
44+
}
45+
46+
int i2c0_read_byte(struct device **bus, uint8_t address,
47+
uint8_t data_read_virtual_address, uint8_t *read_data) {
48+
int ret;
49+
uint32_t bytecount = 1;
50+
51+
ret = i2c_write(*bus, &data_read_virtual_address, bytecount, address);
52+
if (ret != 0) {
53+
return ret;
54+
}
55+
56+
ret = i2c_read(*bus, read_data, sizeof(*read_data), address);
57+
if (ret != 0) {
58+
return ret;
59+
}
60+
return 0;
61+
}

i2c.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
******************************************************************************
3+
* @file i2c.h
4+
* @author - Anthony E.Raterta
5+
* @version V1.0.0
6+
* @date 13-September-2024
7+
* @brief I2C wrappers prototypes
8+
******************************************************************************
9+
* @attention
10+
*
11+
* Copyright (c) 2024 mcu-dev
12+
* All rights reserved.
13+
*
14+
* This software is licensed under terms that can be found in the LICENSE file
15+
* in the root directory of this software component.
16+
* If no LICENSE file comes with this software, it is provided AS-IS.
17+
*
18+
******************************************************************************
19+
*/
20+
#ifndef I2C_H_INCLUDED
21+
#define I2C_H_INCLUDED
22+
23+
#include <stdint.h>
24+
#include <zephyr/device.h>
25+
26+
bool init_i2c0(struct device **bus);
27+
int i2c0_write_bytes(struct device **bus, uint8_t address,
28+
uint8_t *data_buffer);
29+
int i2c0_read_byte(struct device **bus, uint8_t address,
30+
uint8_t data_read_virtual_address, uint8_t *read_data);
31+
#endif

0 commit comments

Comments
 (0)