Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make example work on Raspberry Pi Pico #2

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 69 additions & 25 deletions sample-implementations/RaspberryPi_Pico/cMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
cmake_minimum_required(VERSION 3.12)
include(${PICO_SDK_PATH}/pico_sdk_init.cmake)

project(SCD4XSensor C CXX ASM)
pico_sdk_init()

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
add_executable(main
main.c
sensirion_i2c.c
sensirion_i2c.hal.c
scd4x_i2c.c
sensirion_common.c)

# pull in common dependencies and additional i2c hardware support
target_link_libraries(main pico_stdlib hardware_i2c)

pico_enable_stdio_usb(main 1)

pico_enable_stdio_uart(main 0)

# create map/bin/hex file etc.
pico_add_extra_outputs(main)

# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)

# == DO NEVER EDIT THE NEXT LINES for Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
set(USERHOME $ENV{USERPROFILE})
else()
set(USERHOME $ENV{HOME})
endif()
set(sdkVersion 1.5.1)
set(toolchainVersion 13_2_Rel1)
set(picotoolVersion 2.0.0)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
include(${picoVscode})
endif()
# ====================================================================================
set(PICO_BOARD pico CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project(main C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

add_executable(main
stc3x_i2c_example_usage.c
sensirion_i2c.c
sensirion_i2c_hal.c
stc3x_i2c.c
sensirion_common.c
)

pico_set_program_name(main "main")
pico_set_program_version(main "0.1")

# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(main 0)
pico_enable_stdio_usb(main 0)

# Add the standard library to the build
target_link_libraries(main
hardware_i2c
pico_stdlib)

# Add the standard include files to the build
target_include_directories(main PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
)

# Add any user requested libraries
target_link_libraries(main

)

pico_add_extra_outputs(main)
77 changes: 0 additions & 77 deletions sample-implementations/RaspberryPi_Pico/main.c

This file was deleted.

8 changes: 4 additions & 4 deletions sample-implementations/RaspberryPi_Pico/sensirion_i2c_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ void sensirion_i2c_hal_free(void) {
* @param count number of bytes to read from I2C and store in the buffer
* @returns 0 on success, error code otherwise
*/
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint8_t count) {
int status = i2c_read_blocking(i2c_default, address, data, count, false);
if (status == 0)
if (status <= 0)
return 1;
else
return 0;
Expand All @@ -104,11 +104,11 @@ int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
* @returns 0 on success, error code otherwise
*/
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
uint16_t count) {
uint8_t count) {
// I2C Default is used (I2C0).
int status = i2c_write_blocking(i2c_default, address, data, count, true);

if (status == 0)
if (status <= 0)
return 1;
else
return 0;
Expand Down
5 changes: 3 additions & 2 deletions stc3x_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ int16_t stc3x_get_product_id(uint32_t* product_id, uint64_t* serial_number) {
return local_error;
}
*product_id = product_number;
*serial_number =
((uint64_t)(serial_number_high)*4294967296) + serial_number_low;
*serial_number = (uint64_t)serial_number_high;
*serial_number *= 4294967296;
*serial_number += serial_number_low;
return local_error;
}

Expand Down
25 changes: 11 additions & 14 deletions stc3x_i2c_example_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/**
* TO USE CONSOLE OUTPUT (PRINTF) IF NOT PRESENT ON YOUR PLATFORM
*/
//#define printf(...)
// #define printf(...)

int main(void) {
int16_t error = 0;
Expand All @@ -53,26 +53,21 @@ int main(void) {
}

uint32_t product_number;
uint8_t serial[8];
error = stc3x_read_product_identifier(&product_number, serial, 8);
uint64_t serial_number = 0;
error = stc3x_get_product_id(&product_number, &serial_number);
if (error) {
printf("Error executing stc3x_read_product_identifier(): %i\n", error);
} else {
// uint64_t serial_number =
// (uint64_t)serial[0] << 56 | (uint64_t)serial[1] << 48 |
// (uint64_t)serial[2] << 40 | (uint64_t)serial[3] << 32 |
// (uint64_t)serial[4] << 24 | (uint64_t)serial[5] << 16 |
// (uint64_t)serial[6] << 8 | (uint64_t)serial[7];
printf("Product Number: 0x%08x\n", product_number);
// printf("Serial Number: %" PRIu64 "\n", serial_number);
printf("Serial Number: %" PRIu64 "\n", serial_number);
}

uint16_t self_test_output;
stc3x_test_result_t self_test_output;
error = stc3x_self_test(&self_test_output);
if (error) {
printf("Error executing stc3x_self_test(): %i\n", error);
} else {
printf("Self Test: 0x%04x (OK = 0x0000)\n", self_test_output);
printf("Self Test: 0x%04x (OK = 0x0000)\n", self_test_output.value);
}

error = stc3x_set_binary_gas(0x0001);
Expand All @@ -90,10 +85,12 @@ int main(void) {

for (;;) {
// Read Measurement
error = stc3x_measure_gas_concentration(&gas_ticks, &temperature_ticks);
error =
stc3x_measure_gas_concentration_raw(&gas_ticks, &temperature_ticks);
if (error) {
printf("Error executing stc3x_measure_gas_concentration(): %i\n",
error);
printf(
"Error executing stc3x_measure_gas_concentration_raw(): %i\n",
error);
} else {
gas = 100 * ((float)gas_ticks - 16384.0) / 32768.0;
temperature = (float)temperature_ticks / 200.0;
Expand Down