diff --git a/sample-implementations/RaspberryPi_Pico/cMakeLists.txt b/sample-implementations/RaspberryPi_Pico/cMakeLists.txt index 4a9c75a..d762dbf 100644 --- a/sample-implementations/RaspberryPi_Pico/cMakeLists.txt +++ b/sample-implementations/RaspberryPi_Pico/cMakeLists.txt @@ -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) \ No newline at end of file diff --git a/sample-implementations/RaspberryPi_Pico/main.c b/sample-implementations/RaspberryPi_Pico/main.c deleted file mode 100644 index 195febe..0000000 --- a/sample-implementations/RaspberryPi_Pico/main.c +++ /dev/null @@ -1,77 +0,0 @@ - - -#include "hardware/i2c.h" -#include "pico/binary_info.h" -#include "pico/stdlib.h" -#include "scd4x_i2c.h" -#include - -/// I2C address -static int addr = 0x62; - -// I2C Pins -static uint sda_pin = 16; -static uint scl_pin = 17; - -// This is the main entry for your c application. U -// is -int main() { - - stdio_init_all(); - - // Setup I2c using pins 16 & 17 - i2c_init(i2c_default, 400 * 1000); - gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); - gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); - - // This variable will hold the return status of the function calls. - // You can separate each function call result into their own variable or re - // - use this. - - int status = 0; - - // Stop any readings if occuring - status = scd4x_stop_periodic_measurement(); - - // Perform self test - uint16_t* selfTest = 0; - scd4x_perform_self_test(selfTest); - - // Get Serial number 3 parts - uint16_t one; - uint16_t two; - uint16_t three; - - scd4x_get_serial_number(&one, &two, &three); - - // Start the readings. - status1 = scd4x_start_periodic_measurement(); - - while (1) { - - // Check if data is ready to read - bool dataReady; - while (dataReady == false) { - - status1 = scd4x_get_data_ready_flag(&dataReady); - } - - // Get the ticks. The scd4x_read_measurement function is giving - // incorrect data due to the arthimetic - uint16_t co2; - uint16_t temp; - uint16_t humidity; - status1 = scd4x_read_measurement_ticks(&co2, &temp, &humidity); - - // Arithemtic to change raw data into information - int tempInCelsius = -45 + 175 * temp / 65536; - int tempInFarenheit = tempInCelsius * 1.8 + 32; - int humidityPercent = 100 * humidity / 65536; - - // Print results to terminal (output) - printf("C:%d,T:%d,H:%d", co2, tempInFarenheit, humidityPercent); - - // Sleep for 5 seconds. - sleep_ms(5000); - } -} diff --git a/sample-implementations/RaspberryPi_Pico/sensirion_i2c_hal.c b/sample-implementations/RaspberryPi_Pico/sensirion_i2c_hal.c index d7299ba..d1eede9 100644 --- a/sample-implementations/RaspberryPi_Pico/sensirion_i2c_hal.c +++ b/sample-implementations/RaspberryPi_Pico/sensirion_i2c_hal.c @@ -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; @@ -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; diff --git a/stc3x_i2c.c b/stc3x_i2c.c index 0e160f6..a515598 100644 --- a/stc3x_i2c.c +++ b/stc3x_i2c.c @@ -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; } diff --git a/stc3x_i2c_example_usage.c b/stc3x_i2c_example_usage.c index 9ea0fc4..6ec26a4 100644 --- a/stc3x_i2c_example_usage.c +++ b/stc3x_i2c_example_usage.c @@ -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; @@ -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); @@ -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;