diff --git a/CMakeLists.txt b/CMakeLists.txt index 39b3bbe..a497d6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,10 +11,11 @@ pico_sdk_init() add_executable(my_project src/main.c + src/max31856.c ) # Add pico_stdlib library which aggregates commonly used features -target_link_libraries(my_project pico_stdlib) +target_link_libraries(my_project pico_stdlib hardware_spi) # create map/bin/hex/uf2 file in addition to ELF. pico_add_extra_outputs(my_project) diff --git a/libs/pico-sdk b/libs/pico-sdk new file mode 160000 index 0000000..6a7db34 --- /dev/null +++ b/libs/pico-sdk @@ -0,0 +1 @@ +Subproject commit 6a7db34ff63345a7badec79ebea3aaef1712f374 diff --git a/src/main.c b/src/main.c index 411626b..58fa0e3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,35 +1,64 @@ #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 -int main() { - setup_default_uart(); - printf("Hello, world!\n"); +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) { - 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); + rgb_rainbow(); } return 0; } diff --git a/src/max31856.c b/src/max31856.c new file mode 100644 index 0000000..f88188c --- /dev/null +++ b/src/max31856.c @@ -0,0 +1,47 @@ +#include "pico/stdlib.h" +#include "hardware/spi.h" + +#include "max31856.h" + +// max31856 pins +#define SPI_MAX31856 spi0 +#define SPI_MAX31856_BOUD 1E6 // 1 MHz (max31856 can go up to 5 MHz) +#define SPI_MAX31856_SPO SPI_CPOL_0 +#define SPI_MAX31856_SPH SPI_CPHA_1 +#define SPI_MAX31856_BIT_ORDER SPI_MSB_FIRST +#define SPI_MAX31856_BLOCK_SIZE 8 + +void max31856_init(max31856_t *max) +{ + // init GPIO for CS + gpio_init(max->pin_cs); + gpio_set_dir(max->pin_cs, GPIO_OUT); + gpio_put(max->pin_cs, 1); + + // init SPI + spi_init(SPI_MAX31856, SPI_MAX31856_BOUD); + spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER); + + max->initilzed = true; +} + +void max31856_deinit(max31856_t *max) +{ + max->initilzed = false; + spi_deinit(SPI_MAX31856); + gpio_deinit(max->pin_cs); +} + +int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value) +{ + if (!(max->initilzed)) + { + return -1; + } + const 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); + + return 0; +} diff --git a/src/max31856.h b/src/max31856.h new file mode 100644 index 0000000..08fda16 --- /dev/null +++ b/src/max31856.h @@ -0,0 +1,53 @@ +#ifndef MAX31856_H +#define MAX31856_H + +#include + +typedef struct max31856_s { + uint8_t pin_cs; + bool initilzed; + bool autoDeinit; +} max31856_t; + +void max31856_init(max31856_t *max); +void max31856_deinit(max31856_t *max); +int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value); + +#define MAX31856_REG_CR0 0x00 +#define MAX31856_REG_CR0_CMODE 0x80 +#define MAX31856_REG_CR0_1SHOT 0x40 +#define MAX31856_REG_CR0_OCFAULT 0x10 +#define MAX31856_REG_CR0_JC 0x08 +#define MAX31856_REG_CR0_FAULT 0x04 +#define MAX31856_REG_CR0_FAULTCLR 0x02 +#define MAX31856_REG_CR0_50HZ 0x01 + +#define MAX31856_REG_CR1 0x01 +#define MAX31856_REG_CR1_AVGSEL_1 0x00 +#define MAX31856_REG_CR1_AVGSEL_2 0x10 +#define MAX31856_REG_CR1_AVGSEL_4 0x20 +#define MAX31856_REG_CR1_AVGSEL_8 0x30 +#define MAX31856_REG_CR1_AVGSEL_16 0x40 +#define MAX31856_REG_CR1_TC_TYPE_B 0x00 +#define MAX31856_REG_CR1_TC_TYPE_E 0x01 +#define MAX31856_REG_CR1_TC_TYPE_J 0x02 +#define MAX31856_REG_CR1_TC_TYPE_K 0x03 +#define MAX31856_REG_CR1_TC_TYPE_N 0x04 +#define MAX31856_REG_CR1_TC_TYPE_R 0x05 +#define MAX31856_REG_CR1_TC_TYPE_S 0x06 +#define MAX31856_REG_CR1_TC_TYPE_T 0x07 +#define MAX31856_REG_CR1_VM_GAIN8 0x08 +#define MAX31856_REG_CR1_VM_GAIN32 0x0C + +#define MAX31856_REG_MASK 0x02 +#define MAX31856_REG_CJHF 0x03 +#define MAX31856_REG_CJLF 0x04 +#define MAX31856_REG_LTHFTH 0x05 +#define MAX31856_REG_LTHFTL 0x06 +#define MAX31856_REG_LTLFTH 0x07 +#define MAX31856_REG_LTLFTL 0x08 +#define MAX31856_REG_CJTO 0x09 +#define MAX31856_REG_CJTH 0x0A +#define MAX31856_REG_CJTL 0x0B + +#endif \ No newline at end of file