add read function

This commit is contained in:
Laila van Reenen 2025-03-14 15:56:14 +01:00
parent aac2f72fa3
commit ee55984c93
Signed by: LailaTheElf
GPG Key ID: 8A3EF0226518C12D
3 changed files with 33 additions and 1 deletions

View File

@ -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()

View File

@ -1,5 +1,6 @@
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include <stdint.h>
#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];
}

View File

@ -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