generated from LailaTheElf/rp2040_c
Compare commits
6 Commits
ee55984c93
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
8f18c13ab0
|
|||
|
b6cc668e2b
|
|||
|
34049c9e97
|
|||
|
6a8cab04bf
|
|||
|
9e771ba4fc
|
|||
|
55a1469b99
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
|
||||
/build
|
||||
|
||||
/.cache
|
||||
|
||||
9
.gitmodules
vendored
9
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
||||
[submodule "libs/pico-sdk"]
|
||||
path = libs/pico-sdk
|
||||
url = https://github.com/raspberrypi/pico-sdk.git
|
||||
[submodule "libs/adafruitGFX"]
|
||||
path = libs/adafruitGFX
|
||||
url = https://git.gay/LailaTheElf/AdafruitGFX-RaspberryPICO.git
|
||||
[submodule "libs/wiznet/ioLibrary_Driver"]
|
||||
path = libs/wiznet/ioLibrary_Driver
|
||||
url = https://github.com/Wiznet/ioLibrary_Driver.git
|
||||
|
||||
52
.vscode/launch.json
vendored
Normal file
52
.vscode/launch.json
vendored
Normal file
@@ -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}\""
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
@@ -1,21 +1,41 @@
|
||||
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_subdirectory(libs/wiznet)
|
||||
|
||||
# add_executable(temp_sensor_debug
|
||||
# src/main.cpp
|
||||
# src/max31856.cpp
|
||||
# src/w5500_rp.cpp
|
||||
# )
|
||||
|
||||
# target_link_libraries(temp_sensor_debug
|
||||
# pico_stdlib hardware_spi
|
||||
# AdafruitGFXPico
|
||||
# w5500_driver wiznet_dhcp wiznet_mqtt
|
||||
# )
|
||||
# target_compile_options(temp_sensor_debug PRIVATE -Og)
|
||||
# pico_enable_stdio_usb(temp_sensor_debug 1)
|
||||
# pico_enable_stdio_uart(temp_sensor_debug 0)
|
||||
|
||||
add_executable(temp_sensor
|
||||
src/main.cpp
|
||||
src/max31856.cpp
|
||||
src/w5500_rp.cpp
|
||||
)
|
||||
|
||||
# Add pico_stdlib library which aggregates commonly used features
|
||||
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)
|
||||
target_link_libraries(temp_sensor
|
||||
pico_stdlib hardware_spi
|
||||
AdafruitGFXPico
|
||||
w5500_driver wiznet_dhcp wiznet_mqtt)
|
||||
target_compile_options(temp_sensor PRIVATE -Ofast)
|
||||
pico_enable_stdio_usb(temp_sensor 1)
|
||||
pico_enable_stdio_uart(temp_sensor 0)
|
||||
|
||||
1
libs/adafruitGFX
Submodule
1
libs/adafruitGFX
Submodule
Submodule libs/adafruitGFX added at 15d53df14b
Submodule libs/pico-sdk deleted from 6a7db34ff6
31
libs/wiznet/CMakeLists.txt
Normal file
31
libs/wiznet/CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
add_library(w5500_driver
|
||||
ioLibrary_Driver/Ethernet/W5500/w5500.c
|
||||
ioLibrary_Driver/Ethernet/socket.c
|
||||
ioLibrary_Driver/Ethernet/wizchip_conf.c
|
||||
)
|
||||
target_include_directories(w5500_driver PUBLIC ./ioLibrary_Driver/Ethernet ./ioLibrary_Driver/Ethernet/W5500)
|
||||
target_compile_definitions(w5500_driver PUBLIC _WIZCHIP_=5500)
|
||||
|
||||
add_library(wiznet_dhcp
|
||||
ioLibrary_Driver/Internet/DHCP/dhcp.c
|
||||
)
|
||||
target_link_libraries(wiznet_dhcp w5500_driver)
|
||||
target_include_directories(wiznet_dhcp PUBLIC ./ioLibrary_Driver/Internet/DHCP)
|
||||
|
||||
add_library(wiznet_mqtt
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/mqtt_interface.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTConnectClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTConnectServer.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTDeserializePublish.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTFormat.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTPacket.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTSerializePublish.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTSubscribeClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTSubscribeServer.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c
|
||||
ioLibrary_Driver/Internet/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c
|
||||
)
|
||||
target_link_libraries(wiznet_mqtt w5500_driver)
|
||||
target_include_directories(wiznet_mqtt PUBLIC ./ioLibrary_Driver/Internet/MQTT)
|
||||
1
libs/wiznet/ioLibrary_Driver
Submodule
1
libs/wiznet/ioLibrary_Driver
Submodule
Submodule libs/wiznet/ioLibrary_Driver added at 1d2582a7d0
121
pico_sdk_import.cmake
Normal file
121
pico_sdk_import.cmake
Normal file
@@ -0,0 +1,121 @@
|
||||
# This is a copy of <PICO_SDK_PATH>/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})
|
||||
31
src/config.h
Normal file
31
src/config.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
// pin rgb led on Seeed XIAO RP2040
|
||||
#define PIN_RGB_R 17
|
||||
#define PIN_RGB_G 16
|
||||
#define PIN_RGB_B 25
|
||||
|
||||
#define SPI_DRIVER spi0
|
||||
#define SPI_BOUD 1E6
|
||||
#define PIN_SPI_CLK 2
|
||||
#define PIN_SPI_MISO 4
|
||||
#define PIN_SPI_MOSI 3
|
||||
#define PIN_MAX31856_CS 1
|
||||
#define PIN_DISPLAY_CS 0
|
||||
#define PIN_DISPLAY_DC 7
|
||||
#define PIN_RST 6
|
||||
|
||||
#define PIN_W5500_CS 29
|
||||
#define W5500_SPI_DRIVER SPI_DRIVER
|
||||
|
||||
#define DHCP_SOCKET_ID 1
|
||||
|
||||
#define MQTT_SOCKET_ID 2
|
||||
#define MQTT_BROKER_IP {10, 1, 2, 2} // broker.hivemq.com
|
||||
#define MQTT_BROKER_PORT 1883
|
||||
#define MQTT_CLIENT_ID "ELFTemp"
|
||||
#define MQTT_USERNAME "tempsens"
|
||||
#define MQTT_PASSWORD ""
|
||||
|
||||
#endif
|
||||
65
src/main.c
65
src/main.c
@@ -1,65 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
205
src/main.cpp
Normal file
205
src/main.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
#include <cstdint>
|
||||
#include <pico/time.h>
|
||||
#include <stdio.h>
|
||||
#include <pico/stdlib.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Adafruit_SPIDevice.h>
|
||||
#include <Adafruit_ILI9341.h>
|
||||
#include <dhcp.h>
|
||||
#include <MQTTClient.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "max31856.h"
|
||||
#include "w5500_rp.h"
|
||||
#include "wizchip_conf.h"
|
||||
|
||||
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_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();
|
||||
gpio_init(PIN_RST);
|
||||
gpio_set_dir(PIN_RST, GPIO_OUT);
|
||||
gpio_put(PIN_RST, 1);
|
||||
init_rbg();
|
||||
rgb_rainbow();
|
||||
gpio_put(PIN_RST, 0);
|
||||
|
||||
gpio_set_function(PIN_SPI_CLK, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PIN_SPI_MOSI, GPIO_FUNC_SPI);
|
||||
gpio_set_function(PIN_SPI_MISO, GPIO_FUNC_SPI);
|
||||
spi_init(SPI_DRIVER, SPI_BOUD);
|
||||
spi_set_format(SPI_DRIVER, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
display.begin();
|
||||
display.fillScreen(SSD1306_BLACK);
|
||||
display.setTextColor(SSD1306_WHITE);
|
||||
display.setTextSize(2); //12x16
|
||||
display.setCursor(64-18, 32-8);
|
||||
display.print("hoi");
|
||||
display.display();
|
||||
|
||||
// init temp sensors
|
||||
spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
|
||||
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_4 | 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);
|
||||
|
||||
// init ethernet
|
||||
w5500_rp_init();
|
||||
wiz_NetInfo netinfo = {
|
||||
.mac={'E', 'L', 'F', 177, 31, 252},
|
||||
.ip={192, 254, 18, 91},
|
||||
.sn={255, 255, 0, 0}, // subnet mask
|
||||
.gw={0, 0, 0, 0},
|
||||
.dns={0, 0, 0, 0},
|
||||
.dhcp=NETINFO_DHCP
|
||||
};
|
||||
wizchip_setnetinfo(&netinfo);
|
||||
w5500_rp_dhcp();
|
||||
w5500_rp_mqtt_start();
|
||||
|
||||
absolute_time_t time_read_temp = make_timeout_time_ms(0);
|
||||
absolute_time_t time_read_ip = make_timeout_time_ms(10);
|
||||
absolute_time_t time_update_display = make_timeout_time_ms(20);
|
||||
absolute_time_t time_second = make_timeout_time_ms(0);
|
||||
absolute_time_t time_msecond = make_timeout_time_ms(0);
|
||||
absolute_time_t time_mqtt = make_timeout_time_ms(0);
|
||||
|
||||
char cj_str[8] = {0, };
|
||||
char tc_str[8] = {0, };
|
||||
char ip[16] = {0, };
|
||||
char mqtt_status[20] = {0, };
|
||||
|
||||
while (true)
|
||||
{
|
||||
int64_t time = INT64_MIN;
|
||||
int64_t time_max = INT64_MIN;
|
||||
|
||||
time = absolute_time_diff_us(time_read_temp, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_read_temp = delayed_by_ms(time_read_temp, 300);
|
||||
spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
|
||||
max31856_temp_t cj = max31856_get_cj_temp_c(&temp[0]);
|
||||
snprintf(&cj_str[0], 8, "%i.%02uC", cj.i, cj.f);
|
||||
max31856_temp_t tc = max31856_get_tc_temp_c(&temp[0]);
|
||||
snprintf(&tc_str[0], 8, "%i.%02uC", tc.i, tc.f);
|
||||
|
||||
MQTTMessage msg = {
|
||||
.qos = QOS2,
|
||||
.retained = 0,
|
||||
.dup = 0,
|
||||
.id = 0,
|
||||
.payload = &tc_str[0],
|
||||
.payloadlen = strlen(&tc_str[0])
|
||||
};
|
||||
w5500_rp_mqtt_publish("/kees/tempprobe/tc", &msg);
|
||||
msg.payload = &cj_str[0];
|
||||
msg.payloadlen = strlen(&cj_str[0]);
|
||||
w5500_rp_mqtt_publish("/kees/tempprobe/cj", &msg);
|
||||
|
||||
// printf("CJ temp: %s\n", &cj_str[0]);
|
||||
// printf("TC temp: %s\n", &tc_str[0]);
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_read_temp, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_read_temp = delayed_by_ms(time_read_temp, 300);
|
||||
netinfo = {
|
||||
.mac={255, 255, 255, 255, 255, 255},
|
||||
.ip={255, 255, 255, 255},
|
||||
.sn={255, 255, 255, 255}, // subnet mask
|
||||
.gw={255, 255, 255, 255},
|
||||
.dns={255, 255, 255, 255},
|
||||
.dhcp=NETINFO_STATIC
|
||||
};
|
||||
wizchip_getnetinfo(&netinfo);
|
||||
// w5500_rp_print_netinfo(netinfo);
|
||||
snprintf(&ip[0], 16, "%u.%u.%u.%u", netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]);
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_update_display, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_update_display = delayed_by_ms(time_update_display, 300);
|
||||
spi_set_format(spi0, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
display.fillScreen(SSD1306_BLACK);
|
||||
display.setCursor(0, 0);
|
||||
display.setTextSize(1);
|
||||
display.println(&ip[0]);
|
||||
display.setTextSize(2);
|
||||
display.print("CJ ");
|
||||
display.println(&cj_str[0]);
|
||||
display.print("TC ");
|
||||
display.println(&tc_str[0]);
|
||||
display.setTextSize(1);
|
||||
display.println(&mqtt_status[0]);
|
||||
display.display();
|
||||
}
|
||||
|
||||
DHCP_run();
|
||||
time = absolute_time_diff_us(time_second, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_second = delayed_by_ms(time_second, 1000);
|
||||
DHCP_time_handler();
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_msecond, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_msecond = delayed_by_ms(time_msecond, 1);
|
||||
MilliTimer_Handler();
|
||||
}
|
||||
|
||||
time = absolute_time_diff_us(time_mqtt, get_absolute_time());
|
||||
time_max = MAX(time, time_max);
|
||||
if (time >= 0) {
|
||||
time_mqtt = delayed_by_ms(time_mqtt, 100);
|
||||
w5500_rp_mqtt_run();
|
||||
snprintf(&mqtt_status[0], 20, "MQTT: %s", w5500_rp_mqtt_get_status());
|
||||
}
|
||||
|
||||
if (time_max > 100) {
|
||||
printf("%llius\n", time_max);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/spi.h"
|
||||
#include <stdint.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;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
131
src/max31856.cpp
Normal file
131
src/max31856.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/spi.h"
|
||||
#include <cstdint>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "max31856.h"
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
uint8_t max31856_read8(max31856_t *max, uint8_t addr)
|
||||
{
|
||||
if (!(max->initilzed))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
uint8_t buff_tx = addr & 0x7F;
|
||||
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;
|
||||
}
|
||||
uint8_t buff_tx = addr & 0x7F;
|
||||
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];
|
||||
}
|
||||
|
||||
uint32_t max31856_read24(max31856_t *max, uint8_t addr)
|
||||
{
|
||||
if (!(max->initilzed))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
uint8_t buff_tx = addr & 0x7F;
|
||||
uint8_t buff_rx[] = {0, 0, 0, 0};
|
||||
gpio_put(max->pin_cs, 0);
|
||||
spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 4);
|
||||
gpio_put(max->pin_cs, 1);
|
||||
|
||||
return 0 | buff_rx[1]<<16 | buff_rx[2]<<8 | buff_rx[3];
|
||||
}
|
||||
|
||||
max31856_temp_t max31856_convert_cj_temp_c(uint16_t row) {
|
||||
bool negarive = ((row & 0x8000) == 0) ? false : true;
|
||||
if (negarive) {
|
||||
row = (~row) + 1;
|
||||
}
|
||||
|
||||
max31856_temp_t temp = {
|
||||
(int16_t)(row >> 8),
|
||||
(uint8_t)(((row & 0xFF)*100)/256)
|
||||
};
|
||||
|
||||
if (negarive) {
|
||||
temp.i = -temp.i;
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
max31856_temp_t max31856_convert_tc_temp_c(uint32_t row) {
|
||||
bool negarive = ((row & 0x800000) == 0) ? false : true;
|
||||
if (negarive) {
|
||||
row = (row^0xFFFFFF) + 1;
|
||||
}
|
||||
row &= 0xFFFFFF;
|
||||
|
||||
max31856_temp_t temp = {
|
||||
(int16_t)(row >> 12),
|
||||
(uint8_t)(((row & 0xFFF)*100)/0x1000)
|
||||
};
|
||||
|
||||
if (negarive) {
|
||||
temp.i = -temp.i;
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
max31856_temp_t max31856_get_cj_temp_c(max31856_t *max) {
|
||||
return max31856_convert_cj_temp_c(max31856_read16(max, MAX31856_REG_CJTH));
|
||||
}
|
||||
|
||||
max31856_temp_t max31856_get_tc_temp_c(max31856_t *max) {
|
||||
return max31856_convert_tc_temp_c(max31856_read24(max, MAX31856_REG_LTCBH));
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef MAX31856_H
|
||||
#define MAX31856_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct max31856_s {
|
||||
@@ -9,46 +10,68 @@ typedef struct max31856_s {
|
||||
bool autoDeinit;
|
||||
} max31856_t;
|
||||
|
||||
typedef struct max31856_temp_s {
|
||||
int16_t i;
|
||||
uint8_t f;
|
||||
} max31856_temp_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);
|
||||
uint16_t max31856_read16(max31856_t *max, uint8_t addr);
|
||||
uint32_t max31856_read24(max31856_t *max, uint8_t addr);
|
||||
max31856_temp_t max31856_convert_cj_temp_c(uint16_t row);
|
||||
max31856_temp_t max31856_convert_tc_temp_c(uint32_t row);
|
||||
max31856_temp_t max31856_get_cj_temp_c(max31856_t *max);
|
||||
max31856_temp_t max31856_get_tc_temp_c(max31856_t *max);
|
||||
|
||||
#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_CR0 ((uint8_t)0x00)
|
||||
#define MAX31856_REG_CR0_CMODE ((uint8_t)0x80) // convertion mode
|
||||
#define MAX31856_REG_CR0_1SHOT ((uint8_t)0x40) // one-shot mode
|
||||
#define MAX31856_REG_CR0_OCFAULT ((uint8_t)0x30) // open-circuit fault detection
|
||||
#define MAX31856_REG_CR0_JC ((uint8_t)0x08) // cold-junction sensor disable
|
||||
#define MAX31856_REG_CR0_FAULT ((uint8_t)0x04) // fault mode
|
||||
#define MAX31856_REG_CR0_FAULTCLR ((uint8_t)0x02) // foult status clear
|
||||
#define MAX31856_REG_CR0_50HZ ((uint8_t)0x01) // 50Hz/60Hz node regection filter selection
|
||||
|
||||
#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_CR1 ((uint8_t)0x01)
|
||||
#define MAX31856_REG_CR1_AVGSEL_1 ((uint8_t)0x70) // thermocouple voltage convertion 1 semple averaging
|
||||
#define MAX31856_REG_CR1_AVGSEL_2 ((uint8_t)0x10) // thermocouple voltage convertion 2 semples averaging
|
||||
#define MAX31856_REG_CR1_AVGSEL_4 ((uint8_t)0x20) // thermocouple voltage convertion 4 semples averaging
|
||||
#define MAX31856_REG_CR1_AVGSEL_8 ((uint8_t)0x30) // thermocouple voltage convertion 8 semples averaging
|
||||
#define MAX31856_REG_CR1_AVGSEL_16 ((uint8_t)0x40) // thermocouple voltage convertion 16 semples averaging
|
||||
#define MAX31856_REG_CR1_TC_TYPE_B ((uint8_t)0x00) // thermocouple type B
|
||||
#define MAX31856_REG_CR1_TC_TYPE_E ((uint8_t)0x01) // thermocouple type E
|
||||
#define MAX31856_REG_CR1_TC_TYPE_J ((uint8_t)0x02) // thermocouple type J
|
||||
#define MAX31856_REG_CR1_TC_TYPE_K ((uint8_t)0x03) // thermocouple type K
|
||||
#define MAX31856_REG_CR1_TC_TYPE_N ((uint8_t)0x04) // thermocouple type N
|
||||
#define MAX31856_REG_CR1_TC_TYPE_R ((uint8_t)0x05) // thermocouple type R
|
||||
#define MAX31856_REG_CR1_TC_TYPE_S ((uint8_t)0x06) // thermocouple type S
|
||||
#define MAX31856_REG_CR1_TC_TYPE_T ((uint8_t)0x07) // thermocouple type T
|
||||
#define MAX31856_REG_CR1_VM_GAIN8 ((uint8_t)0x08) // voltage mode, gain 8
|
||||
#define MAX31856_REG_CR1_VM_GAIN32 ((uint8_t)0x0C) // voltage mode, gain 32
|
||||
|
||||
#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
|
||||
#define MAX31856_REG_MASK ((uint8_t)0x02) // fault mask
|
||||
#define MAX31856_REG_CJHF ((uint8_t)0x03) // cold juntion hige fault theshold
|
||||
#define MAX31856_REG_CJLF ((uint8_t)0x04) // cold juntion low fault theshold
|
||||
#define MAX31856_REG_LTHFTH ((uint8_t)0x05)
|
||||
#define MAX31856_REG_LTHFTL ((uint8_t)0x06)
|
||||
#define MAX31856_REG_LTLFTH ((uint8_t)0x07)
|
||||
#define MAX31856_REG_LTLFTL ((uint8_t)0x08)
|
||||
#define MAX31856_REG_CJTO ((uint8_t)0x09) // cold juntion temp offset
|
||||
#define MAX31856_REG_CJTH ((uint8_t)0x0A) // cold juntion temp hige byte
|
||||
#define MAX31856_REG_CJTL ((uint8_t)0x0B) // cold juntion temp low byte
|
||||
#define MAX31856_REG_LTCBH ((uint8_t)0x0C) // thermocouple temp byte 2
|
||||
#define MAX31856_REG_LTCBM ((uint8_t)0x0D) // thermocouple temp byte 1
|
||||
#define MAX31856_REG_LTCBL ((uint8_t)0x0E) // thermocouple temp byte 0
|
||||
#define MAX31856_REG_SR ((uint8_t)0x0F) // fault status
|
||||
|
||||
// max31856 pins
|
||||
#define SPI_MAX31856 spi0
|
||||
#define SPI_MAX31856_BOUD 1000E3 // 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
|
||||
|
||||
#endif
|
||||
141
src/w5500_rp.cpp
Normal file
141
src/w5500_rp.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include <hardware/timer.h>
|
||||
#include <pico/time.h>
|
||||
#include <stdio.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "hardware/spi.h"
|
||||
#include <pico/stdlib.h>
|
||||
#include <wizchip_conf.h>
|
||||
#include <socket.h>
|
||||
#include <dhcp.h>
|
||||
#include <MQTTClient.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "w5500_rp.h"
|
||||
|
||||
void w5500_rp_spi_write_byte(uint8_t byte) {
|
||||
spi_write_blocking(W5500_SPI_DRIVER, &byte, 1);
|
||||
}
|
||||
uint8_t w5500_rp_spi_read_byte() {
|
||||
uint8_t byte;
|
||||
spi_read_blocking(W5500_SPI_DRIVER, 0, &byte, 1);
|
||||
return byte;
|
||||
}
|
||||
|
||||
void w5500_rp_spi_write_burst(uint8_t *buffer, uint16_t size) {
|
||||
spi_write_blocking(W5500_SPI_DRIVER, buffer, size);
|
||||
}
|
||||
void w5500_rp_spi_read_burst(uint8_t *buffer, uint16_t size) {
|
||||
uint8_t byte;
|
||||
spi_read_blocking(W5500_SPI_DRIVER, 0, buffer, size);
|
||||
}
|
||||
|
||||
void w5500_rp_spi_cs_select() {
|
||||
spi_set_format(W5500_SPI_DRIVER, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
|
||||
gpio_put(PIN_W5500_CS, 0);
|
||||
}
|
||||
void w5500_rp_spi_cs_deselect() {
|
||||
gpio_put(PIN_W5500_CS, 1);
|
||||
}
|
||||
|
||||
void w5500_rp_init() {
|
||||
gpio_init(PIN_W5500_CS);
|
||||
gpio_set_dir(PIN_W5500_CS, GPIO_OUT);
|
||||
|
||||
reg_wizchip_spi_cbfunc(&w5500_rp_spi_read_byte, &w5500_rp_spi_write_byte);
|
||||
reg_wizchip_spiburst_cbfunc(&w5500_rp_spi_read_burst, &w5500_rp_spi_write_burst);
|
||||
reg_wizchip_cs_cbfunc(w5500_rp_spi_cs_select, w5500_rp_spi_cs_deselect);
|
||||
reg_wizchip_cris_cbfunc(NULL, NULL);
|
||||
}
|
||||
|
||||
void w5500_rp_print_netinfo(wiz_NetInfo netinfo) {
|
||||
printf("IP: %3u.%3u.%3u.%3u\n", netinfo.ip[0], netinfo.ip[1], netinfo.ip[2], netinfo.ip[3]);
|
||||
printf("mask: %3u.%3u.%3u.%3u\n", netinfo.sn[0], netinfo.sn[1], netinfo.sn[2], netinfo.sn[3]);
|
||||
printf("gate: %3u.%3u.%3u.%3u\n", netinfo.gw[0], netinfo.gw[1], netinfo.gw[2], netinfo.gw[3]);
|
||||
printf("dns: %3u.%3u.%3u.%3u\n", netinfo.dns[0], netinfo.dns[1], netinfo.dns[2], netinfo.dns[3]);
|
||||
printf("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", netinfo.mac[0], netinfo.mac[1], netinfo.mac[2], netinfo.mac[3], netinfo.mac[4], netinfo.mac[5]);
|
||||
}
|
||||
|
||||
bool dhcp_assigned = false;
|
||||
uint8_t dhcp_buffer[1024];
|
||||
|
||||
void w5500_rp_dhcp_assigned() {
|
||||
dhcp_assigned = true;
|
||||
}
|
||||
|
||||
void w5500_rp_dhcp() {
|
||||
DHCP_init(DHCP_SOCKET_ID, &dhcp_buffer[0]);
|
||||
reg_dhcp_cbfunc(NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
enum mqtt_state {
|
||||
MQTT_INACTIVE,
|
||||
MQTT_CONNECTING,
|
||||
MQTT_CONNECTED,
|
||||
} mqtt_state = MQTT_INACTIVE;
|
||||
char mqtt_state_str_inactive[] = "inactive";
|
||||
char mqtt_state_str_connecting[] = "connecting";
|
||||
char mqtt_state_str_connected[] = "connected";
|
||||
char mqtt_state_str_invalid[] = "invlid";
|
||||
MQTTClient mqtt;
|
||||
Network mqtt_net;
|
||||
unsigned char mqtt_buff_send[1024];
|
||||
unsigned char mqtt_buff_read[1024];
|
||||
|
||||
void w5500_rp_mqtt_start() {
|
||||
mqtt_state = MQTT_CONNECTING;
|
||||
}
|
||||
void w5500_rp_mqtt_init() {
|
||||
mqtt_state = MQTT_CONNECTING;
|
||||
NewNetwork(&mqtt_net, MQTT_SOCKET_ID);
|
||||
unsigned char mqtt_broker_ip[] = MQTT_BROKER_IP;
|
||||
int err = ConnectNetwork(&mqtt_net, &mqtt_broker_ip[0], MQTT_BROKER_PORT);
|
||||
if (err == SOCK_ERROR) {
|
||||
return;
|
||||
}
|
||||
MQTTClientInit(&mqtt, &mqtt_net, 300, &mqtt_buff_send[0], 1024, &mqtt_buff_read[0], 1024);
|
||||
|
||||
|
||||
MQTTPacket_connectData mqtt_conndata = MQTTPacket_connectData_initializer;
|
||||
mqtt_conndata.willFlag = 0;
|
||||
mqtt_conndata.MQTTVersion = 3;
|
||||
mqtt_conndata.clientID.cstring = MQTT_CLIENT_ID;
|
||||
mqtt_conndata.username.cstring = MQTT_USERNAME;
|
||||
mqtt_conndata.password.cstring = MQTT_PASSWORD;
|
||||
mqtt_conndata.keepAliveInterval = 60;
|
||||
mqtt_conndata.cleansession = 1;
|
||||
if (MQTTConnect(&mqtt, &mqtt_conndata) == 0) {
|
||||
mqtt_state = MQTT_CONNECTED;
|
||||
}
|
||||
}
|
||||
|
||||
void w5500_rp_mqtt_run() {
|
||||
switch (mqtt_state) {
|
||||
case MQTT_INACTIVE:
|
||||
return;
|
||||
case MQTT_CONNECTING:
|
||||
w5500_rp_mqtt_init();
|
||||
break;
|
||||
case MQTT_CONNECTED:
|
||||
MQTTYield(&mqtt, 5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char* w5500_rp_mqtt_get_status() {
|
||||
switch (mqtt_state) {
|
||||
case MQTT_INACTIVE:
|
||||
return &mqtt_state_str_inactive[0];
|
||||
case MQTT_CONNECTING:
|
||||
return &mqtt_state_str_connecting[0];
|
||||
case MQTT_CONNECTED:
|
||||
return &mqtt_state_str_connected[0];
|
||||
}
|
||||
return &mqtt_state_str_invalid[0];
|
||||
}
|
||||
|
||||
void w5500_rp_mqtt_publish(const char *topic, MQTTMessage *msg) {
|
||||
MQTTPublish(&mqtt, topic, msg);
|
||||
}
|
||||
|
||||
16
src/w5500_rp.h
Normal file
16
src/w5500_rp.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef W5500_RP_H
|
||||
#define W5500_RP_H
|
||||
|
||||
#include <wizchip_conf.h>
|
||||
#include <MQTTClient.h>
|
||||
|
||||
void w5500_rp_init();
|
||||
void w5500_rp_print_netinfo(wiz_NetInfo netinfo);
|
||||
void w5500_rp_dhcp();
|
||||
|
||||
void w5500_rp_mqtt_run();
|
||||
char* w5500_rp_mqtt_get_status();
|
||||
void w5500_rp_mqtt_start();
|
||||
void w5500_rp_mqtt_publish(const char *topic, MQTTMessage *msg);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user