Created
February 7, 2021 20:58
-
-
Save ludekstepan/1463643d743db24fdf051b121b5b8968 to your computer and use it in GitHub Desktop.
max31855 Raspberry Pi Pico lib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pico/stdlib.h" | |
#include "hardware/spi.h" | |
#include "max31855.h" | |
static inline void max31855_cs_select(uint pin_cs) { | |
asm volatile("nop \n nop \n nop"); | |
gpio_put(pin_cs, 0); // Active low | |
asm volatile("nop \n nop \n nop"); | |
} | |
static inline void max31855_cs_deselect(uint pin_cs) { | |
asm volatile("nop \n nop \n nop"); | |
gpio_put(pin_cs, 1); | |
asm volatile("nop \n nop \n nop"); | |
} | |
void max31855_init(const max31855_config *config) { | |
spi_init(config->spi, 5000 * 1000); // 5 MHz | |
gpio_set_function(config->pin_data, GPIO_FUNC_SPI); | |
gpio_set_function(config->pin_sck, GPIO_FUNC_SPI); | |
// Chip select is active-low, so we'll initialise it to a driven-high state | |
gpio_init(config->pin_cs); | |
gpio_set_dir(config->pin_cs, GPIO_OUT); | |
gpio_put(config->pin_cs, 1); | |
} | |
void max31855_read(const max31855_config *config, struct max31855_result *result) { | |
uint8_t buffer[4]; | |
max31855_cs_select(config->pin_cs); | |
sleep_ms(1); | |
spi_read_blocking(config->spi, 0, buffer, 4); | |
max31855_cs_deselect(config->pin_cs); | |
sleep_ms(1); | |
int16_t thermocouple = MAX31855_SIGN_EXTEND((buffer[0] << 6) | (buffer[1] >> 2), 14); | |
int16_t internal = MAX31855_SIGN_EXTEND((buffer[2] << 4) | (buffer[3] >> 4), 12); | |
result->thermocouple = (1.0f * thermocouple) / 4; | |
result->internal = (1.0f * internal) / 16; | |
result->faults = ((buffer[1] & 0x01) << 8) | (buffer[3] & 0x07); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _MAX31855_H | |
#define _MAX31855_H | |
#include <stdint.h> | |
#define MAX31855_FAULT 0x100 // 1 when any of the SCV, SCG, or OC faults are active | |
#define MAX31855_SCV_FAULT 0x04 // short-circuited to VCC | |
#define MAX31855_SCG_FAULT 0x02 // short-circuited to GND | |
#define MAX31855_OC_FAULT 0x01 // open (no connections) | |
#define MAX31855_SIGN_EXTEND(value, bit) (((value) ^ (1u << ((bit) - 1))) - (1u << ((bit) - 1))) | |
typedef struct { | |
spi_inst_t *spi; | |
uint pin_data; | |
uint pin_cs; | |
uint pin_sck; | |
} max31855_config; | |
struct max31855_result | |
{ | |
float thermocouple; | |
float internal; | |
uint16_t faults; | |
}; | |
void max31855_init(const max31855_config *config); | |
void max31855_read(const max31855_config *config, struct max31855_result *result); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment