From 55a1469b99998d10590c7f9f28d529fb2593d848 Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Fri, 21 Nov 2025 17:39:07 +0100 Subject: [PATCH] update --- .gitignore | 2 +- .gitmodules | 6 +- .vscode/launch.json | 52 +++++++++++++ CMakeLists.txt | 32 +++++--- libs/adafruitGFX | 1 + libs/pico-sdk | 1 - pico_sdk_import.cmake | 121 +++++++++++++++++++++++++++++++ src/main.c | 65 ----------------- src/main.cpp | 91 +++++++++++++++++++++++ src/{max31856.c => max31856.cpp} | 10 +-- 10 files changed, 295 insertions(+), 86 deletions(-) create mode 100644 .vscode/launch.json create mode 160000 libs/adafruitGFX delete mode 160000 libs/pico-sdk create mode 100644 pico_sdk_import.cmake delete mode 100644 src/main.c create mode 100644 src/main.cpp rename src/{max31856.c => max31856.cpp} (88%) diff --git a/.gitignore b/.gitignore index 5a8d6a8..d3dd07e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /build - +/.cache diff --git a/.gitmodules b/.gitmodules index 1891415..2a31476 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "libs/pico-sdk"] - path = libs/pico-sdk - url = https://github.com/raspberrypi/pico-sdk.git +[submodule "libs/adafruitGFX"] + path = libs/adafruitGFX + url = https://github.com/MarkWalter94/AdafruitGFX-RaspberryPICO.git diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6723e82 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,52 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Pico Debug (Cortex-Debug)", + "cwd": "/usr/shere/openocd/scripts", + "type": "cortex-debug", + "executable": "${command:cmake.launchTargetPath}", + "request": "launch", + "servertype": "openocd", + // "serverpath": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe", + // "gdbPath": "${command:raspberry-pi-pico.getGDBPath}", + "gdbPath": "/usr/bin/arm-none-eabi-gdb", + "device": "RP2040", + "configFiles": [ + "interface/cmsis-dap.cfg", + "target/rp2040.cfg" + ], + "svdFile": "${PICO_SDK_PATH}/src/rp2040/hardware_regs/RP2040.svd", + "runToEntryPoint": "main", + // Fix for no_flash binaries, where monitor reset halt doesn't do what is expected + // Also works fine for flash binaries + "overrideLaunchCommands": [ + "monitor reset init", + "load \"${command:cmake.launchTargetPath}\"" + ], + "openOCDLaunchCommands": [ + "adapter speed 5000" + ] + }, + { + "name": "Pico Debug (Cortex-Debug with external OpenOCD)", + "cwd": "${workspaceRoot}", + "type": "cortex-debug", + "executable": "${command:cmake.launchTargetPath}", + "request": "launch", + "servertype": "external", + "gdbTarget": "localhost:3333", + // "gdbPath": "${command:raspberry-pi-pico.getGDBPath}", + "gdbPath": "/usr/bin/arm-none-eabi-gdb", + "device": "RP2040", + "svdFile": "${PICO_SDK_PATH}/src/rp2040/hardware_regs/RP2040.svd", + "runToEntryPoint": "main", + // Fix for no_flash binaries, where monitor reset halt doesn't do what is expected + // Also works fine for flash binaries + "overrideLaunchCommands": [ + "monitor reset init", + "load \"${command:cmake.launchTargetPath}\"" + ] + }, + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 04c795b..0a479af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,21 +1,31 @@ cmake_minimum_required(VERSION 3.13) -# initialize the SDK based on PICO_SDK_PATH -# note: this must happen before project() -include(libs/pico-sdk/pico_sdk_init.cmake) +set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) +include(pico_sdk_import.cmake) +project(temp_sensor_debug) project(temp_sensor) -# initialize the Raspberry Pi Pico SDK pico_sdk_init() -add_executable(my_project - src/main.c - src/max31856.c +add_subdirectory(libs/adafruitGFX/AdafruitGFXPico) + +add_executable(temp_sensor_debug + src/main.cpp + src/max31856.cpp ) -# Add pico_stdlib library which aggregates commonly used features -target_link_libraries(my_project pico_stdlib hardware_spi) +target_link_libraries(temp_sensor_debug pico_stdlib hardware_spi AdafruitGFXPico) +target_compile_options(temp_sensor_debug PRIVATE -Og) +pico_enable_stdio_usb(temp_sensor_debug 1) +pico_enable_stdio_uart(temp_sensor_debug 0) -# create map/bin/hex/uf2 file in addition to ELF. -pico_add_extra_outputs(my_project) +add_executable(temp_sensor + src/main.cpp + src/max31856.cpp +) + +target_link_libraries(temp_sensor pico_stdlib hardware_spi AdafruitGFXPico) +target_compile_options(temp_sensor PRIVATE -Ofast) +pico_enable_stdio_usb(temp_sensor 1) +pico_enable_stdio_uart(temp_sensor 0) diff --git a/libs/adafruitGFX b/libs/adafruitGFX new file mode 160000 index 0000000..07bb0fa --- /dev/null +++ b/libs/adafruitGFX @@ -0,0 +1 @@ +Subproject commit 07bb0fae1bb9ccaed17612c19235fec4dc87c76b diff --git a/libs/pico-sdk b/libs/pico-sdk deleted file mode 160000 index 6a7db34..0000000 --- a/libs/pico-sdk +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6a7db34ff63345a7badec79ebea3aaef1712f374 diff --git a/pico_sdk_import.cmake b/pico_sdk_import.cmake new file mode 100644 index 0000000..d493cc2 --- /dev/null +++ b/pico_sdk_import.cmake @@ -0,0 +1,121 @@ +# This is a copy of /external/pico_sdk_import.cmake + +# This can be dropped into an external project to help locate this SDK +# It should be include()ed prior to project() + +# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the +# following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following +# disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) + set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) + message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) + set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) + message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) + set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) + message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG)) + set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG}) + message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')") +endif () + +if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG) + set(PICO_SDK_FETCH_FROM_GIT_TAG "master") + message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG") +endif() + +set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") +set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") +set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") +set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK") + +if (NOT PICO_SDK_PATH) + if (PICO_SDK_FETCH_FROM_GIT) + include(FetchContent) + set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) + if (PICO_SDK_FETCH_FROM_GIT_PATH) + get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") + endif () + FetchContent_Declare( + pico_sdk + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + ) + + if (NOT pico_sdk) + message("Downloading Raspberry Pi Pico SDK") + # GIT_SUBMODULES_RECURSE was added in 3.17 + if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + GIT_SUBMODULES_RECURSE FALSE + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + else () + FetchContent_Populate( + pico_sdk + QUIET + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} + + SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src + BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build + SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild + ) + endif () + + set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) + endif () + set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) + else () + message(FATAL_ERROR + "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." + ) + endif () +endif () + +get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") +if (NOT EXISTS ${PICO_SDK_PATH}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") +endif () + +set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) +if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") +endif () + +set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) + +include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 58fa0e3..0000000 --- a/src/main.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include "pico/stdlib.h" - -#include "max31856.h" - -// pin rgb led on Seeed XIAO RP2040 -#define PIN_RGB_R 17 -#define PIN_RGB_G 16 -#define PIN_RGB_B 25 - -max31856_t temp[2]; - -void init_rbg() -{ - gpio_init(PIN_RGB_R); - gpio_set_dir(PIN_RGB_R, GPIO_OUT); - gpio_init(PIN_RGB_G); - gpio_set_dir(PIN_RGB_G, GPIO_OUT); - gpio_init(PIN_RGB_B); - gpio_set_dir(PIN_RGB_B, GPIO_OUT); -} - -void rgb_rainbow() -{ - gpio_put(PIN_RGB_G, 1); - sleep_ms(250); - gpio_put(PIN_RGB_R, 0); - sleep_ms(250); - gpio_put(PIN_RGB_B, 1); - sleep_ms(250); - gpio_put(PIN_RGB_G, 0); - sleep_ms(250); - gpio_put(PIN_RGB_R, 1); - sleep_ms(250); - gpio_put(PIN_RGB_B, 0); - sleep_ms(250); - gpio_put(PIN_RGB_R, 0); -} - -int main() { - setup_default_uart(); - printf("Hello, world!\n"); - - init_rbg(); - rgb_rainbow(); - - // init temp sensors - temp[0].pin_cs = 4; - max31856_init(&(temp[0])); - temp[1].pin_cs = 5; - max31856_init(&(temp[1])); - // config temp sensor 0 - max31856_write8(&(temp[0]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); - max31856_write8(&(temp[0]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K); - // config temp sensor 1 - max31856_write8(&(temp[1]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); - max31856_write8(&(temp[1]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K); - - while (true) - { - rgb_rainbow(); - } - return 0; -} - diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8c1ff4e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +#include "max31856.h" + +// pin rgb led on Seeed XIAO RP2040 +#define PIN_RGB_R 17 +#define PIN_RGB_G 16 +#define PIN_RGB_B 25 + +#define PIN_SPI_MISO 0 +#define PIN_SPI_CLK 2 +#define PIN_SPI_MOSI 3 +#define PIN_MAX31856_CS 1 +#define PIN_DISPLAY_CS 4 +#define PIN_DISPLAY_DC 5 +#define PIN_DISPLAY_RST 6 + +max31856_t temp[2]; +Adafruit_SPIDevice spidev(PIN_DISPLAY_CS, PIN_SPI_CLK, PIN_SPI_MISO, PIN_SPI_MOSI); +Adafruit_SSD1306 display(128, 64, &spidev, PIN_DISPLAY_DC, PIN_DISPLAY_RST, PIN_DISPLAY_CS); + +void init_rbg() +{ + gpio_init(PIN_RGB_R); + gpio_set_dir(PIN_RGB_R, GPIO_OUT); + gpio_init(PIN_RGB_G); + gpio_set_dir(PIN_RGB_G, GPIO_OUT); + gpio_init(PIN_RGB_B); + gpio_set_dir(PIN_RGB_B, GPIO_OUT); +} + +void rgb_rainbow() +{ + gpio_put(PIN_RGB_G, 1); + sleep_ms(250); + gpio_put(PIN_RGB_R, 0); + sleep_ms(250); + gpio_put(PIN_RGB_B, 1); + sleep_ms(250); + gpio_put(PIN_RGB_G, 0); + sleep_ms(250); + gpio_put(PIN_RGB_R, 1); + sleep_ms(250); + gpio_put(PIN_RGB_B, 0); + sleep_ms(250); +} + +int main() { + stdio_init_all(); + rgb_rainbow(); + rgb_rainbow(); + printf("Hello, world!\n"); + + init_rbg(); + rgb_rainbow(); + rgb_rainbow(); + + display.begin(); + + // init temp sensors + // temp[0].pin_cs = PIN_MAX31856_CS; + // max31856_init(&(temp[0])); + // // temp[1].pin_cs = 5; + // //max31856_init(&(temp[1])); + // // config temp sensor 0 + // max31856_write8(&(temp[0]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); + // max31856_write8(&(temp[0]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K); + // // config temp sensor 1 + // //max31856_write8(&(temp[1]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); + // //max31856_write8(&(temp[1]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K); + + display.fillScreen(SSD1306_WHITE); + display.setTextColor(SSD1306_BLACK); + display.print("Henlo doggo"); + + rgb_rainbow(); + + while (true) + { + display.fillScreen(SSD1306_WHITE); + rgb_rainbow(); + display.fillScreen(SSD1306_BLACK); + rgb_rainbow(); + } + return 0; +} + diff --git a/src/max31856.c b/src/max31856.cpp similarity index 88% rename from src/max31856.c rename to src/max31856.cpp index 9c48e5c..70f2d74 100644 --- a/src/max31856.c +++ b/src/max31856.cpp @@ -39,7 +39,7 @@ int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value) { return -1; } - const uint8_t buff[2] = {addr | 0x80, value}; + uint8_t buff[2] = {addr | 0x80, value}; gpio_put(max->pin_cs, 0); spi_write_blocking(SPI_MAX31856, &buff[0], 2); gpio_put(max->pin_cs, 1); @@ -53,8 +53,8 @@ uint8_t max31856_read8(max31856_t *max, uint8_t addr) { return -1; } - const uint16_t buff_tx = (addr & 0x7F)<<8; - const uint8_t buff_rx[] = {0, 0}; + uint16_t buff_tx = (addr & 0x7F)<<8; + uint8_t buff_rx[] = {0, 0}; gpio_put(max->pin_cs, 0); spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 2); gpio_put(max->pin_cs, 1); @@ -68,8 +68,8 @@ uint16_t max31856_read16(max31856_t *max, uint8_t addr) { return -1; } - const uint16_t buff_tx = (addr & 0x7F)<<8; - const uint8_t buff_rx[] = {0, 0, 0}; + uint16_t buff_tx = (addr & 0x7F)<<8; + uint8_t buff_rx[] = {0, 0, 0}; gpio_put(max->pin_cs, 0); spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 3); gpio_put(max->pin_cs, 1);