From ee55984c936fba8abc7b3c8478ffda954de3195e Mon Sep 17 00:00:00 2001 From: LailaTheElf Date: Fri, 14 Mar 2025 15:56:14 +0100 Subject: [PATCH] add read function --- CMakeLists.txt | 2 +- src/max31856.c | 31 +++++++++++++++++++++++++++++++ src/max31856.h | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a497d6d..04c795b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.13) # note: this must happen before project() include(libs/pico-sdk/pico_sdk_init.cmake) -project(my_project) +project(temp_sensor) # initialize the Raspberry Pi Pico SDK pico_sdk_init() diff --git a/src/max31856.c b/src/max31856.c index f88188c..9c48e5c 100644 --- a/src/max31856.c +++ b/src/max31856.c @@ -1,5 +1,6 @@ #include "pico/stdlib.h" #include "hardware/spi.h" +#include #include "max31856.h" @@ -45,3 +46,33 @@ int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value) return 0; } + +uint8_t max31856_read8(max31856_t *max, uint8_t addr) +{ + if (!(max->initilzed)) + { + return -1; + } + const uint16_t buff_tx = (addr & 0x7F)<<8; + const 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); + + return buff_rx[1]; +} + +uint16_t max31856_read16(max31856_t *max, uint8_t addr) +{ + if (!(max->initilzed)) + { + return -1; + } + const uint16_t buff_tx = (addr & 0x7F)<<8; + const 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); + + return buff_rx[1]<<8 | buff_rx[2]; +} diff --git a/src/max31856.h b/src/max31856.h index 08fda16..f338a1c 100644 --- a/src/max31856.h +++ b/src/max31856.h @@ -12,6 +12,7 @@ typedef struct max31856_s { void max31856_init(max31856_t *max); void max31856_deinit(max31856_t *max); int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value); +uint16_t max31856_read16(max31856_t *max, uint8_t addr); #define MAX31856_REG_CR0 0x00 #define MAX31856_REG_CR0_CMODE 0x80