Skip to content

Commit 6d5df15

Browse files
authored
Merge pull request #5 from Sensirion/update-raspi-pico-sample-implementation
Update Raspberry Pi Pico sample implementation
2 parents 4584621 + be5cf68 commit 6d5df15

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
### Project Setup
2+
3+
To program the Raspberry Pi Pico, we use the Raspberry Pi Pico VS Code extension.
4+
5+
Follow these instructions to flash the example:
6+
7+
1. Open the Raspberry Pi Pico VS Code extension and create a new **C/C++ project**.
8+
2. Select your board type.
9+
3. Enable the **I2C interface** feature.
10+
4. Enable **Console over USB**.
11+
5. Click **Create**.
12+
6. Copy all driver .h and .c files into the new pico project.
13+
7. Copy the example usage into the pico project
14+
8. Replace the sensirion_i2c_hal.c file with the file from the Raspberry Pi Pico sample implementation
15+
9. In your CMakeLists.txt, replace the add_executable block with:
16+
```cmake
17+
add_executable(<project_name>
18+
sen5x_i2c_example_usage.c
19+
sensirion_i2c.c
20+
sensirion_i2c_hal.c
21+
sen5x_i2c.c
22+
sensirion_common.c
23+
)
24+
```
25+
10. Replace <project_name> with your project name
26+
11. Connect your sensor and flash the firmware.
27+
28+
Once flashed successfully, the Raspberry Pi Pico will print sensor readings over the USB serial console.
29+
30+
### Connecting the Sensor
31+
32+
- SDA: GPIO4 (Pin 6)
33+
- SCL: GPIO5 (Pin 7)
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include "hardware/i2c.h"
2+
#include "pico/stdlib.h"
3+
/*
4+
* Copyright (c) 2018, Sensirion AG
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* * Redistributions of source code must retain the above copyright notice, this
11+
* list of conditions and the following disclaimer.
12+
*
13+
* * Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* * Neither the name of Sensirion AG nor the names of its
18+
* contributors may be used to endorse or promote products derived from
19+
* this software without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*/
33+
34+
#include "sensirion_common.h"
35+
#include "sensirion_config.h"
36+
#include "sensirion_i2c_hal.h"
37+
38+
#define I2C_PORT i2c0
39+
#define PIN_I2C_SDA 4
40+
#define PIN_I2C_SCL 5
41+
42+
/**
43+
* Select the current i2c bus by index.
44+
* All following i2c operations will be directed at that bus.
45+
*
46+
* THE IMPLEMENTATION IS OPTIONAL ON SINGLE-BUS SETUPS (all sensors on the same
47+
* bus)
48+
*
49+
* @param bus_idx Bus index to select
50+
* @returns 0 on success, an error code otherwise
51+
*/
52+
int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) {
53+
/* TODO:IMPLEMENT or leave empty if all sensors are located on one single
54+
* bus
55+
*/
56+
return NOT_IMPLEMENTED_ERROR;
57+
}
58+
59+
/**
60+
* Initialize all hard- and software components that are needed for the I2C
61+
* communication.
62+
*/
63+
void sensirion_i2c_hal_init(void) {
64+
stdio_init_all();
65+
66+
// I2C Initialisation. Using it at 400Khz.
67+
i2c_init(I2C_PORT, 400 * 1000);
68+
69+
gpio_set_function(PIN_I2C_SDA, GPIO_FUNC_I2C);
70+
gpio_set_function(PIN_I2C_SCL, GPIO_FUNC_I2C);
71+
gpio_pull_up(PIN_I2C_SDA);
72+
gpio_pull_up(PIN_I2C_SCL);
73+
}
74+
75+
/**
76+
* Release all resources initialized by sensirion_i2c_hal_init().
77+
*/
78+
void sensirion_i2c_hal_free(void) {
79+
i2c_deinit(I2C_PORT);
80+
gpio_set_function(PIN_I2C_SDA, GPIO_FUNC_NULL);
81+
gpio_set_function(PIN_I2C_SCL, GPIO_FUNC_NULL);
82+
gpio_disable_pulls(PIN_I2C_SDA);
83+
gpio_disable_pulls(PIN_I2C_SCL);
84+
}
85+
86+
/**
87+
* Execute one read transaction on the I2C bus, reading a given number of bytes.
88+
* If the device does not acknowledge the read command, an error shall be
89+
* returned.
90+
*
91+
* @param address 7-bit I2C address to read from
92+
* @param data pointer to the buffer where the data is to be stored
93+
* @param count number of bytes to read from I2C and store in the buffer
94+
* @returns 0 on success, error code otherwise
95+
*/
96+
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint8_t count) {
97+
int status = i2c_read_blocking(i2c_default, address, data, count, false);
98+
99+
if (status <= 0)
100+
return 1;
101+
else
102+
return 0;
103+
}
104+
105+
/**
106+
* Execute one write transaction on the I2C bus, sending a given number of
107+
* bytes. The bytes in the supplied buffer must be sent to the given address. If
108+
* the slave device does not acknowledge any of the bytes, an error shall be
109+
* returned.
110+
*
111+
* @param address 7-bit I2C address to write to
112+
* @param data pointer to the buffer containing the data to write
113+
* @param count number of bytes to read from the buffer and send over I2C
114+
* @returns 0 on success, error code otherwise
115+
*/
116+
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
117+
uint8_t count) {
118+
// I2C Default is used (I2C0).
119+
int status = i2c_write_blocking(i2c_default, address, data, count, true);
120+
121+
if (status <= 0)
122+
return 1;
123+
else
124+
return 0;
125+
}
126+
127+
/**
128+
* Sleep for a given number of microseconds. The function should delay the
129+
* execution for at least the given time, but may also sleep longer.
130+
*
131+
* Despite the unit, a <10 millisecond precision is sufficient.
132+
*
133+
* @param useconds the sleep time in microseconds
134+
*/
135+
void sensirion_i2c_hal_sleep_usec(uint32_t useconds) {
136+
sleep_ms(useconds / 1000);
137+
}

0 commit comments

Comments
 (0)