Skip to content

Commit c5c529a

Browse files
committed
Update raspi pico sample implementation.
1 parent 0e3cc7c commit c5c529a

File tree

4 files changed

+57
-116
lines changed

4 files changed

+57
-116
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+
sen66_i2c_example_usage.c
19+
sensirion_i2c.c
20+
sensirion_i2c_hal.c
21+
sen66_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)

sample-implementations/RaspberryPi_Pico/cMakeLists.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.

sample-implementations/RaspberryPi_Pico/main.c

Lines changed: 0 additions & 77 deletions
This file was deleted.

sample-implementations/RaspberryPi_Pico/sensirion_i2c_hal.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <hardware/i2c.h>
1+
#include "hardware/i2c.h"
2+
#include "pico/stdlib.h"
23
/*
34
* Copyright (c) 2018, Sensirion AG
45
* All rights reserved.
@@ -34,13 +35,9 @@
3435
#include "sensirion_config.h"
3536
#include "sensirion_i2c_hal.h"
3637

37-
/*
38-
* INSTRUCTIONS
39-
* ============
40-
*
41-
* Implement all functions where they are marked as IMPLEMENT.
42-
* Follow the function specification in the comments.
43-
*/
38+
#define I2C_PORT i2c0
39+
#define PIN_I2C_SDA 4
40+
#define PIN_I2C_SCL 5
4441

4542
/**
4643
* Select the current i2c bus by index.
@@ -64,14 +61,26 @@ int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) {
6461
* communication.
6562
*/
6663
void sensirion_i2c_hal_init(void) {
67-
/* TODO:IMPLEMENT */
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);
6873
}
6974

7075
/**
7176
* Release all resources initialized by sensirion_i2c_hal_init().
7277
*/
7378
void sensirion_i2c_hal_free(void) {
74-
/* TODO:IMPLEMENT or leave empty if no resources need to be freed */
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);
7584
}
7685

7786
/**
@@ -84,9 +93,10 @@ void sensirion_i2c_hal_free(void) {
8493
* @param count number of bytes to read from I2C and store in the buffer
8594
* @returns 0 on success, error code otherwise
8695
*/
87-
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
96+
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint8_t count) {
8897
int status = i2c_read_blocking(i2c_default, address, data, count, false);
89-
if (status == 0)
98+
99+
if (status <= 0)
90100
return 1;
91101
else
92102
return 0;
@@ -104,11 +114,11 @@ int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
104114
* @returns 0 on success, error code otherwise
105115
*/
106116
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
107-
uint16_t count) {
117+
uint8_t count) {
108118
// I2C Default is used (I2C0).
109119
int status = i2c_write_blocking(i2c_default, address, data, count, true);
110120

111-
if (status == 0)
121+
if (status <= 0)
112122
return 1;
113123
else
114124
return 0;

0 commit comments

Comments
 (0)