Skip to content

Commit 2ed88a9

Browse files
committed
Merge branch 'add-raspi-pico-sample-implementation' into 'master'
Add raspi pico sample implementation See merge request MSO-SW/drivers/embedded/embedded-i2c-scd4x!17
2 parents e9c5f96 + 896ff77 commit 2ed88a9

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
include(${PICO_SDK_PATH}/pico_sdk_init.cmake)
3+
4+
project(SCD4XSensor C CXX ASM)
5+
pico_sdk_init()
6+
7+
set(CMAKE_C_STANDARD 11)
8+
set(CMAKE_CXX_STANDARD 17)
9+
add_executable(main
10+
main.c
11+
sensirion_i2c.c
12+
sensirion_i2c.hal.c
13+
scd4x_i2c.c
14+
sensirion_common.c)
15+
16+
# pull in common dependencies and additional i2c hardware support
17+
target_link_libraries(main pico_stdlib hardware_i2c)
18+
19+
pico_enable_stdio_usb(main 1)
20+
21+
pico_enable_stdio_uart(main 0)
22+
23+
# create map/bin/hex file etc.
24+
pico_add_extra_outputs(main)
25+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
3+
#include "hardware/i2c.h"
4+
#include "pico/binary_info.h"
5+
#include "pico/stdlib.h"
6+
#include "scd4x_i2c.h"
7+
#include <stdio.h>
8+
9+
/// I2C address
10+
static int addr = 0x62;
11+
12+
// I2C Pins
13+
static uint sda_pin = 16;
14+
static uint scl_pin = 17;
15+
16+
// This is the main entry for your c application. U
17+
// is
18+
int main() {
19+
20+
stdio_init_all();
21+
22+
// Setup I2c using pins 16 & 17
23+
i2c_init(i2c_default, 400 * 1000);
24+
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
25+
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
26+
27+
// This variable will hold the return status of the function calls.
28+
// You can separate each function call result into their own variable or re
29+
// - use this.
30+
31+
int status = 0;
32+
33+
// Stop any readings if occuring
34+
status = scd4x_stop_periodic_measurement();
35+
36+
// Perform self test
37+
uint16_t* selfTest = 0;
38+
scd4x_perform_self_test(selfTest);
39+
40+
// Get Serial number 3 parts
41+
uint16_t one;
42+
uint16_t two;
43+
uint16_t three;
44+
45+
scd4x_get_serial_number(&one, &two, &three);
46+
47+
// Start the readings.
48+
status1 = scd4x_start_periodic_measurement();
49+
50+
while (1) {
51+
52+
// Check if data is ready to read
53+
bool dataReady;
54+
while (dataReady == false) {
55+
56+
status1 = scd4x_get_data_ready_flag(&dataReady);
57+
}
58+
59+
// Get the ticks. The scd4x_read_measurement function is giving
60+
// incorrect data due to the arthimetic
61+
uint16_t co2;
62+
uint16_t temp;
63+
uint16_t humidity;
64+
status1 = scd4x_read_measurement_ticks(&co2, &temp, &humidity);
65+
66+
// Arithemtic to change raw data into information
67+
int tempInCelsius = -45 + 175 * temp / 65536;
68+
int tempInFarenheit = tempInCelsius * 1.8 + 32;
69+
int humidityPercent = 100 * humidity / 65536;
70+
71+
// Print results to terminal (output)
72+
printf("C:%d,T:%d,H:%d", co2, tempInFarenheit, humidityPercent);
73+
74+
// Sleep for 5 seconds.
75+
sleep_ms(5000);
76+
}
77+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <hardware/i2c.h>
2+
/*
3+
* Copyright (c) 2018, Sensirion AG
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice, this
10+
* list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of Sensirion AG nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
#include "sensirion_common.h"
34+
#include "sensirion_config.h"
35+
#include "sensirion_i2c_hal.h"
36+
37+
/*
38+
* INSTRUCTIONS
39+
* ============
40+
*
41+
* Implement all functions where they are marked as IMPLEMENT.
42+
* Follow the function specification in the comments.
43+
*/
44+
45+
/**
46+
* Select the current i2c bus by index.
47+
* All following i2c operations will be directed at that bus.
48+
*
49+
* THE IMPLEMENTATION IS OPTIONAL ON SINGLE-BUS SETUPS (all sensors on the same
50+
* bus)
51+
*
52+
* @param bus_idx Bus index to select
53+
* @returns 0 on success, an error code otherwise
54+
*/
55+
int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) {
56+
/* TODO:IMPLEMENT or leave empty if all sensors are located on one single
57+
* bus
58+
*/
59+
return NOT_IMPLEMENTED_ERROR;
60+
}
61+
62+
/**
63+
* Initialize all hard- and software components that are needed for the I2C
64+
* communication.
65+
*/
66+
void sensirion_i2c_hal_init(void) {
67+
/* TODO:IMPLEMENT */
68+
}
69+
70+
/**
71+
* Release all resources initialized by sensirion_i2c_hal_init().
72+
*/
73+
void sensirion_i2c_hal_free(void) {
74+
/* TODO:IMPLEMENT or leave empty if no resources need to be freed */
75+
}
76+
77+
/**
78+
* Execute one read transaction on the I2C bus, reading a given number of bytes.
79+
* If the device does not acknowledge the read command, an error shall be
80+
* returned.
81+
*
82+
* @param address 7-bit I2C address to read from
83+
* @param data pointer to the buffer where the data is to be stored
84+
* @param count number of bytes to read from I2C and store in the buffer
85+
* @returns 0 on success, error code otherwise
86+
*/
87+
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
88+
int status = i2c_read_blocking(i2c_default, address, data, count, false);
89+
if (status == 0)
90+
return 1;
91+
else
92+
return 0;
93+
}
94+
95+
/**
96+
* Execute one write transaction on the I2C bus, sending a given number of
97+
* bytes. The bytes in the supplied buffer must be sent to the given address. If
98+
* the slave device does not acknowledge any of the bytes, an error shall be
99+
* returned.
100+
*
101+
* @param address 7-bit I2C address to write to
102+
* @param data pointer to the buffer containing the data to write
103+
* @param count number of bytes to read from the buffer and send over I2C
104+
* @returns 0 on success, error code otherwise
105+
*/
106+
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
107+
uint16_t count) {
108+
// I2C Default is used (I2C0).
109+
int status = i2c_write_blocking(i2c_default, address, data, count, true);
110+
111+
if (status == 0)
112+
return 1;
113+
else
114+
return 0;
115+
}
116+
117+
/**
118+
* Sleep for a given number of microseconds. The function should delay the
119+
* execution for at least the given time, but may also sleep longer.
120+
*
121+
* Despite the unit, a <10 millisecond precision is sufficient.
122+
*
123+
* @param useconds the sleep time in microseconds
124+
*/
125+
void sensirion_i2c_hal_sleep_usec(uint32_t useconds) {
126+
sleep_ms(useconds / 1000);
127+
}

0 commit comments

Comments
 (0)