Skip to content

Commit 2a5984d

Browse files
author
Julien FERAND
committed
Initial Commit
0 parents  commit 2a5984d

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
3+
build
4+
.idea

CMakeLists.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
set(CMAKE_CXX_STANDARD 17)
3+
4+
# Pull in Raspberry Pi Pico SDK (must be before project)
5+
include(pico_sdk_import.cmake)
6+
7+
project(pico_i2c_device)
8+
# Initialise the Raspberry Pi Pico SDK
9+
pico_sdk_init()
10+
11+
# Add the source file for the library
12+
add_library(pico_i2c_device STATIC i2c_device.cpp)
13+
14+
# Link the necessary libraries from the Pico SDK
15+
target_link_libraries(pico_i2c_device
16+
pico_stdlib # Standard Pico libraries
17+
hardware_i2c # The hardware_pwm library
18+
)
19+
# Add the standard include files
20+
target_include_directories(pico_i2c_device PUBLIC
21+
${CMAKE_CURRENT_LIST_DIR}
22+
)

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Pico I2C Device Library
2+
3+
The Pico I2C Device Library provides a straightforward interface for communicating with I2C devices using the Raspberry Pi Pico. It supports both read and write operations on I2C devices, making it easy to interface with a wide range of I2C-enabled components.
4+
5+
## Features
6+
7+
• Initialize I2C communication with configurable baud rate and GPIO pins.
8+
• Perform read and write operations to interact with I2C devices.
9+
• Supports flexible buffer sizes for I2C transactions.
10+
11+
## Installation
12+
13+
### Git
14+
15+
Add the Pico I2C Device Library as a submodule in your project.
16+
17+
```shell
18+
git submodule add https://github.com/julienfdev/pico-i2c-device.git lib/pico-i2c-device
19+
```
20+
21+
### CMake
22+
23+
Modify your CMakeLists.txt to include the Pico I2C Device Library.
24+
25+
- After the `pico_sdk_init` call
26+
`add_subdirectory(lib/pico-i2c-device)`
27+
28+
- Add the library to your project
29+
`target_link_libraries(your_project pico_i2c_device)`
30+
31+
## Usage
32+
33+
### Initialization
34+
35+
To use the I2C Device library, create an instance of the I2CDevice class by specifying the I2C address, I2C interface, baud rate, and the GPIO pins for SDA and SCL. Only the first parameter is required, and the rest have default values: `i2c0`, `100000` and `{4, 5}`.
36+
37+
```c++
38+
#include "i2c_device.h"
39+
40+
int main() {
41+
// I2C address 0x68, default I2C0, default baudrate 100000, pins 4 (SDA) and 5 (SCL)
42+
I2CDevice device(0x68);
43+
return 0;
44+
}
45+
```
46+
47+
48+
### Writing Data to a Register
49+
50+
To write data to a specific register of the I2C device, use the write method.
51+
```c++
52+
uint8_t data[2] = { 0x01, 0x02 };
53+
device.write(0x10, data, 2); // Write 2 bytes to register 0x10
54+
```
55+
56+
### Reading Data from a Register
57+
58+
To read data from a specific register of the I2C device, use the read method.
59+
60+
```c++
61+
uint8_t result[4];
62+
device.read(0x20, result, 4); // Read 4 bytes from register 0x20
63+
```
64+
65+
### Customizing I2C Pins and Baudrate
66+
67+
You can customize the I2C pins and baud rate when creating an instance of the I2CDevice class. Please check the pinout diagram
68+
to make sure the selected pins are valid for I2C communication based on the interface you provided (or i2c0 by default).
69+
70+
```c++
71+
I2CPins custom_pins = { 8, 9 }; // SDA = GPIO 8, SCL = GPIO 9
72+
I2CDevice custom_device(0x68, i2c0, 400000, custom_pins); // I2C0, 400kHz baudrate, custom pins
73+
```
74+
75+
### License
76+
77+
This project is licensed under the MIT License. See the LICENSE file for more details.

i2c_device.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "i2c_device.h"
2+
#include <cstring>
3+
4+
void I2CDevice::write(const uint8_t reg, const uint8_t* data, const size_t size) {
5+
m_buffer[0] = reg;
6+
for (size_t i = 0; i < size; i++) {
7+
m_buffer[i + 1] = data[i];
8+
}
9+
10+
i2c_write_blocking(m_i2c, m_addr, m_buffer, size + 1, false);
11+
12+
// wipe buffer
13+
memset(m_buffer, 0, sizeof(m_buffer));
14+
}
15+
16+
void I2CDevice::read(const uint8_t reg, uint8_t* result, const size_t size) const {
17+
i2c_write_blocking(m_i2c, m_addr, &reg, 1, true);
18+
i2c_read_blocking(m_i2c, m_addr, result, size, false);
19+
}
20+
21+

i2c_device.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef I2C_DEVICE_H
2+
#define I2C_DEVICE_H
3+
#include "pico/stdlib.h"
4+
#include "hardware/i2c.h"
5+
6+
7+
struct I2CPins {
8+
uint8_t sda;
9+
uint8_t scl;
10+
};
11+
12+
class I2CDevice {
13+
private:
14+
i2c_inst_t *m_i2c;
15+
uint8_t m_addr;
16+
uint8_t m_buffer[256];
17+
18+
public:
19+
explicit I2CDevice(const uint8_t addr, i2c_inst_t *i2c = i2c0, const uint baudrate = 100000, const I2CPins pins = { 4, 5 }): m_i2c { i2c }, m_addr { addr }, m_buffer { 0 } {
20+
i2c_init(i2c, baudrate);
21+
gpio_set_function(pins.sda, GPIO_FUNC_I2C);
22+
gpio_set_function(pins.scl, GPIO_FUNC_I2C);
23+
gpio_pull_up(pins.sda);
24+
gpio_pull_up(pins.scl);
25+
}
26+
I2CDevice(const I2CDevice&) = delete;
27+
I2CDevice() = delete;
28+
29+
void write(uint8_t reg, const uint8_t* data, size_t size = 1);
30+
void read(uint8_t reg, uint8_t* result, size_t size = 1) const;
31+
};
32+
#endif // I2C_DEVICE_H

pico_sdk_import.cmake

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
2+
3+
# This can be dropped into an external project to help locate this SDK
4+
# It should be include()ed prior to project()
5+
6+
if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
7+
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
8+
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
9+
endif ()
10+
11+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
12+
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
13+
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
14+
endif ()
15+
16+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
17+
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
18+
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
19+
endif ()
20+
21+
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
22+
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
23+
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
24+
endif ()
25+
26+
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
27+
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
28+
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
29+
endif()
30+
31+
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
32+
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
33+
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
34+
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
35+
36+
if (NOT PICO_SDK_PATH)
37+
if (PICO_SDK_FETCH_FROM_GIT)
38+
include(FetchContent)
39+
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
40+
if (PICO_SDK_FETCH_FROM_GIT_PATH)
41+
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
42+
endif ()
43+
# GIT_SUBMODULES_RECURSE was added in 3.17
44+
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
45+
FetchContent_Declare(
46+
pico_sdk
47+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
48+
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
49+
GIT_SUBMODULES_RECURSE FALSE
50+
)
51+
else ()
52+
FetchContent_Declare(
53+
pico_sdk
54+
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
55+
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
56+
)
57+
endif ()
58+
59+
if (NOT pico_sdk)
60+
message("Downloading Raspberry Pi Pico SDK")
61+
FetchContent_Populate(pico_sdk)
62+
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
63+
endif ()
64+
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
65+
else ()
66+
message(FATAL_ERROR
67+
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
68+
)
69+
endif ()
70+
endif ()
71+
72+
get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
73+
if (NOT EXISTS ${PICO_SDK_PATH})
74+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
75+
endif ()
76+
77+
set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
78+
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
79+
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
80+
endif ()
81+
82+
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
83+
84+
include(${PICO_SDK_INIT_CMAKE_FILE})

0 commit comments

Comments
 (0)