diff --git a/.gitignore b/.gitignore index 49438b2..6eaf0f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ -.pio .vscode/.browse.c_cpp.db* .vscode/c_cpp_properties.json .vscode/launch.json .vscode/ipch -prv/ \ No newline at end of file +prv/ +ch32_node.elf +ch32_node.lst +ch32_node.map +ch32_node.bin +ch32_node.hex diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6176b1e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ch32v003fun"] + path = ch32v003fun + url = https://github.com/cnlohr/ch32v003fun.git diff --git a/.vscode/settings.json b/.vscode/settings.json index 77ea6f0..5a40669 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -33,6 +33,8 @@ "modbus.h": "c", "network.h": "c", "cstdint": "c", - "utils.h": "c" + "utils.h": "c", + "onewire.h": "c", + "string.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6415047 --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +TARGET ?= ch32_node +TARGET_MCU ?= CH32V203 +TARGET_MCU_PACKAGE ?= CH32V203C8T6 + +CH32V003FUN ?= ./ch32v003fun/ch32v003fun +MINICHLINK ?= ./ch32v003fun/minichlink + +PREFIX ?= riscv64-none-elf +NEWLIB ?= /usr/riscv64-none-elf/include/ + +INCLUDE_DIRS ?= \ + -I./include \ + -I./ioLibrary_Driver \ + -I./ioLibrary_Driver/MQTT \ + -I./ioLibrary_Driver/MQTT/MQTTPacket/src + +PROJECT_C_FILES := $(filter-out ./ch32_node.c, $(wildcard ./*.c)) +LIB_C_FILES := \ + ./ioLibrary_Driver/socket.c \ + ./ioLibrary_Driver/wizchip_conf.c \ + ./ioLibrary_Driver/W5500/w5500.c \ + ./ioLibrary_Driver/DHCP/dhcp.c \ + ./ioLibrary_Driver/MQTT/mqtt_interface.c \ + ./ioLibrary_Driver/MQTT/MQTTClient.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTDeserializePublish.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSerializePublish.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectClient.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeClient.c \ + ./ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c + +ADDITIONAL_C_FILES ?= $(PROJECT_C_FILES) $(LIB_C_FILES) + +include $(CH32V003FUN)/ch32v003fun.mk + +CFLAGS += -Wall -Wextra $(INCLUDE_DIRS) + +all: flash +flash: cv_flash +clean: cv_clean +.PHONY: all flash clean diff --git a/README.md b/README.md index fe46b26..fa5de33 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ Firmware for a CH32V203 MCU with a W5500 Ethernet controller. ## TODO: -- UnplatformIOify this - Set up interrupts for RS485 publish - ... @@ -18,10 +17,8 @@ Firmware for a CH32V203 MCU with a W5500 Ethernet controller. ## Development Environment -- Platform: CH32V - Board: CH32V203C8T6 - Framework: ch32v003fun -- Build System: PlatformIO - Compiler: RISC-V GCC 14.2.0 ## Project Structure diff --git a/src/main.c b/ch32_node.c similarity index 98% rename from src/main.c rename to ch32_node.c index 4a6392a..ce49e4d 100644 --- a/src/main.c +++ b/ch32_node.c @@ -17,7 +17,7 @@ static mqtt_state_t mqtt_state; -void system_init(void) { +void sysinit(void) { SystemInit(); gpio_init(); systick_init(); @@ -37,7 +37,7 @@ static void on_modbus_value(uint8_t device_idx, const char* property, } int main(void) { - system_init(); + sysinit(); Delay_Ms(W5500_INIT_DELAY_MS); configure_network(); diff --git a/ch32v003fun b/ch32v003fun new file mode 160000 index 0000000..69801af --- /dev/null +++ b/ch32v003fun @@ -0,0 +1 @@ +Subproject commit 69801af8824d1aad74dd2898d7e4b9a6e6612186 diff --git a/src/config.c b/config.c similarity index 100% rename from src/config.c rename to config.c diff --git a/src/gpio.c b/gpio.c similarity index 100% rename from src/gpio.c rename to gpio.c diff --git a/include/README b/include/README deleted file mode 100644 index 194dcd4..0000000 --- a/include/README +++ /dev/null @@ -1,39 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the usual convention is to give header files names that end with `.h'. -It is most portable to use only letters, digits, dashes, and underscores in -header file names, and at most one dot. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/src/funconfig.h b/include/funconfig.h similarity index 85% rename from src/funconfig.h rename to include/funconfig.h index 71ac866..6d037a2 100644 --- a/src/funconfig.h +++ b/include/funconfig.h @@ -1,8 +1,7 @@ #ifndef _FUNCONFIG_H #define _FUNCONFIG_H -// board definition file will already take care of this -// #define CH32V003 1 +#define CH32V20x 1 #define FUNCONF_USE_HSI 0 // Use HSI Internal Oscillator #define FUNCONF_USE_HSE 1 // Use External Oscillator diff --git a/lib/onewire/onewire.h b/include/onewire.h similarity index 100% rename from lib/onewire/onewire.h rename to include/onewire.h diff --git a/lib/ioLibrary_Driver/.gitattributes b/ioLibrary_Driver/.gitattributes similarity index 100% rename from lib/ioLibrary_Driver/.gitattributes rename to ioLibrary_Driver/.gitattributes diff --git a/lib/ioLibrary_Driver/.gitignore b/ioLibrary_Driver/.gitignore similarity index 100% rename from lib/ioLibrary_Driver/.gitignore rename to ioLibrary_Driver/.gitignore diff --git a/lib/ioLibrary_Driver/.gitmodules b/ioLibrary_Driver/.gitmodules similarity index 100% rename from lib/ioLibrary_Driver/.gitmodules rename to ioLibrary_Driver/.gitmodules diff --git a/lib/ioLibrary_Driver/DHCP/dhcp.c b/ioLibrary_Driver/DHCP/dhcp.c similarity index 99% rename from lib/ioLibrary_Driver/DHCP/dhcp.c rename to ioLibrary_Driver/DHCP/dhcp.c index 1a2d9cd..9a802b3 100644 --- a/lib/ioLibrary_Driver/DHCP/dhcp.c +++ b/ioLibrary_Driver/DHCP/dhcp.c @@ -601,7 +601,9 @@ int8_t parseDHCPMSG(void) printf("DHCP message : %d.%d.%d.%d(%d) %d received. \r\n",svr_addr[0],svr_addr[1],svr_addr[2], svr_addr[3],svr_port, len); #endif } - else return 0; + else { + return 0; + } if (svr_port == DHCP_SERVER_PORT) { // compare mac address if ( (pDHCPMSG->chaddr[0] != DHCP_CHADDR[0]) || (pDHCPMSG->chaddr[1] != DHCP_CHADDR[1]) || diff --git a/lib/ioLibrary_Driver/DHCP/dhcp.h b/ioLibrary_Driver/DHCP/dhcp.h similarity index 100% rename from lib/ioLibrary_Driver/DHCP/dhcp.h rename to ioLibrary_Driver/DHCP/dhcp.h diff --git a/lib/ioLibrary_Driver/DNS/dns.c b/ioLibrary_Driver/DNS/dns.c similarity index 100% rename from lib/ioLibrary_Driver/DNS/dns.c rename to ioLibrary_Driver/DNS/dns.c diff --git a/lib/ioLibrary_Driver/DNS/dns.h b/ioLibrary_Driver/DNS/dns.h similarity index 100% rename from lib/ioLibrary_Driver/DNS/dns.h rename to ioLibrary_Driver/DNS/dns.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTClient.c b/ioLibrary_Driver/MQTT/MQTTClient.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTClient.c rename to ioLibrary_Driver/MQTT/MQTTClient.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTClient.h b/ioLibrary_Driver/MQTT/MQTTClient.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTClient.h rename to ioLibrary_Driver/MQTT/MQTTClient.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnect.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnect.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnect.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnect.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectClient.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectClient.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectClient.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectClient.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectServer.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectServer.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectServer.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTConnectServer.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTDeserializePublish.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTDeserializePublish.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTDeserializePublish.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTDeserializePublish.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTFormat.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPacket.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPublish.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPublish.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPublish.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTPublish.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSerializePublish.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSerializePublish.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSerializePublish.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSerializePublish.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribe.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribe.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribe.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribe.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeClient.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeClient.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeClient.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeClient.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeServer.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeServer.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeServer.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTSubscribeServer.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribe.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribe.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribe.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribe.h diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeClient.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c b/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c rename to ioLibrary_Driver/MQTT/MQTTPacket/src/MQTTUnsubscribeServer.c diff --git a/lib/ioLibrary_Driver/MQTT/MQTTPacket/src/StackTrace.h b/ioLibrary_Driver/MQTT/MQTTPacket/src/StackTrace.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/MQTTPacket/src/StackTrace.h rename to ioLibrary_Driver/MQTT/MQTTPacket/src/StackTrace.h diff --git a/lib/ioLibrary_Driver/MQTT/mqtt_interface.c b/ioLibrary_Driver/MQTT/mqtt_interface.c similarity index 100% rename from lib/ioLibrary_Driver/MQTT/mqtt_interface.c rename to ioLibrary_Driver/MQTT/mqtt_interface.c diff --git a/lib/ioLibrary_Driver/MQTT/mqtt_interface.h b/ioLibrary_Driver/MQTT/mqtt_interface.h similarity index 100% rename from lib/ioLibrary_Driver/MQTT/mqtt_interface.h rename to ioLibrary_Driver/MQTT/mqtt_interface.h diff --git a/lib/ioLibrary_Driver/README.md b/ioLibrary_Driver/README.md similarity index 100% rename from lib/ioLibrary_Driver/README.md rename to ioLibrary_Driver/README.md diff --git a/lib/ioLibrary_Driver/Socket_APIs_V3.0.3.chm b/ioLibrary_Driver/Socket_APIs_V3.0.3.chm similarity index 100% rename from lib/ioLibrary_Driver/Socket_APIs_V3.0.3.chm rename to ioLibrary_Driver/Socket_APIs_V3.0.3.chm diff --git a/lib/ioLibrary_Driver/W5500/w5500.c b/ioLibrary_Driver/W5500/w5500.c similarity index 100% rename from lib/ioLibrary_Driver/W5500/w5500.c rename to ioLibrary_Driver/W5500/w5500.c diff --git a/lib/ioLibrary_Driver/W5500/w5500.h b/ioLibrary_Driver/W5500/w5500.h similarity index 100% rename from lib/ioLibrary_Driver/W5500/w5500.h rename to ioLibrary_Driver/W5500/w5500.h diff --git a/lib/ioLibrary_Driver/httpServer/httpParser.c b/ioLibrary_Driver/httpServer/httpParser.c similarity index 100% rename from lib/ioLibrary_Driver/httpServer/httpParser.c rename to ioLibrary_Driver/httpServer/httpParser.c diff --git a/lib/ioLibrary_Driver/httpServer/httpParser.h b/ioLibrary_Driver/httpServer/httpParser.h similarity index 100% rename from lib/ioLibrary_Driver/httpServer/httpParser.h rename to ioLibrary_Driver/httpServer/httpParser.h diff --git a/lib/ioLibrary_Driver/httpServer/httpServer.c b/ioLibrary_Driver/httpServer/httpServer.c similarity index 100% rename from lib/ioLibrary_Driver/httpServer/httpServer.c rename to ioLibrary_Driver/httpServer/httpServer.c diff --git a/lib/ioLibrary_Driver/httpServer/httpServer.h b/ioLibrary_Driver/httpServer/httpServer.h similarity index 100% rename from lib/ioLibrary_Driver/httpServer/httpServer.h rename to ioLibrary_Driver/httpServer/httpServer.h diff --git a/lib/ioLibrary_Driver/httpServer/httpUtil.c b/ioLibrary_Driver/httpServer/httpUtil.c similarity index 100% rename from lib/ioLibrary_Driver/httpServer/httpUtil.c rename to ioLibrary_Driver/httpServer/httpUtil.c diff --git a/lib/ioLibrary_Driver/httpServer/httpUtil.h b/ioLibrary_Driver/httpServer/httpUtil.h similarity index 100% rename from lib/ioLibrary_Driver/httpServer/httpUtil.h rename to ioLibrary_Driver/httpServer/httpUtil.h diff --git a/lib/ioLibrary_Driver/iolibrary.chm b/ioLibrary_Driver/iolibrary.chm similarity index 100% rename from lib/ioLibrary_Driver/iolibrary.chm rename to ioLibrary_Driver/iolibrary.chm diff --git a/lib/ioLibrary_Driver/license.txt b/ioLibrary_Driver/license.txt similarity index 100% rename from lib/ioLibrary_Driver/license.txt rename to ioLibrary_Driver/license.txt diff --git a/lib/ioLibrary_Driver/loopback/loopback.c b/ioLibrary_Driver/loopback/loopback.c similarity index 100% rename from lib/ioLibrary_Driver/loopback/loopback.c rename to ioLibrary_Driver/loopback/loopback.c diff --git a/lib/ioLibrary_Driver/loopback/loopback.h b/ioLibrary_Driver/loopback/loopback.h similarity index 100% rename from lib/ioLibrary_Driver/loopback/loopback.h rename to ioLibrary_Driver/loopback/loopback.h diff --git a/lib/ioLibrary_Driver/multicast/multicast.c b/ioLibrary_Driver/multicast/multicast.c similarity index 100% rename from lib/ioLibrary_Driver/multicast/multicast.c rename to ioLibrary_Driver/multicast/multicast.c diff --git a/lib/ioLibrary_Driver/multicast/multicast.h b/ioLibrary_Driver/multicast/multicast.h similarity index 100% rename from lib/ioLibrary_Driver/multicast/multicast.h rename to ioLibrary_Driver/multicast/multicast.h diff --git a/lib/ioLibrary_Driver/socket.c b/ioLibrary_Driver/socket.c similarity index 100% rename from lib/ioLibrary_Driver/socket.c rename to ioLibrary_Driver/socket.c diff --git a/lib/ioLibrary_Driver/socket.h b/ioLibrary_Driver/socket.h similarity index 100% rename from lib/ioLibrary_Driver/socket.h rename to ioLibrary_Driver/socket.h diff --git a/lib/ioLibrary_Driver/wizchip_conf.c b/ioLibrary_Driver/wizchip_conf.c similarity index 100% rename from lib/ioLibrary_Driver/wizchip_conf.c rename to ioLibrary_Driver/wizchip_conf.c diff --git a/lib/ioLibrary_Driver/wizchip_conf.h b/ioLibrary_Driver/wizchip_conf.h similarity index 100% rename from lib/ioLibrary_Driver/wizchip_conf.h rename to ioLibrary_Driver/wizchip_conf.h diff --git a/lib/README b/lib/README deleted file mode 100644 index 2593a33..0000000 --- a/lib/README +++ /dev/null @@ -1,46 +0,0 @@ - -This directory is intended for project specific (private) libraries. -PlatformIO will compile them to static libraries and link into executable file. - -The source code of each library should be placed in an own separate directory -("lib/your_library_name/[here are source files]"). - -For example, see a structure of the following two libraries `Foo` and `Bar`: - -|--lib -| | -| |--Bar -| | |--docs -| | |--examples -| | |--src -| | |- Bar.c -| | |- Bar.h -| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html -| | -| |--Foo -| | |- Foo.c -| | |- Foo.h -| | -| |- README --> THIS FILE -| -|- platformio.ini -|--src - |- main.c - -and a contents of `src/main.c`: -``` -#include -#include - -int main (void) -{ - ... -} - -``` - -PlatformIO Library Dependency Finder will find automatically dependent -libraries scanning project source files. - -More information about PlatformIO Library Dependency Finder -- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/lmao.sh b/lmao.sh deleted file mode 100755 index 8bac121..0000000 --- a/lmao.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -SERIAL_PORT="/dev/ttyACM0" -BAUD_RATE="115200" - -{ - stty -F $SERIAL_PORT $BAUD_RATE - COUNT=0 - PREVIOUS_TIME="" - - while IFS= read -r LINE; do - CURRENT_TIME=$(date +%s.%N) - - # 5 msgs - if [ $COUNT -lt 5 ]; then - echo "msg $COUNT: $LINE" - - if [[ -n $PREVIOUS_TIME ]]; then - INTERVAL=$(echo "$CURRENT_TIME - $PREVIOUS_TIME" | bc) - echo "interval: ${INTERVAL}s" - fi - - PREVIOUS_TIME=$CURRENT_TIME - COUNT=$((COUNT + 1)) - else - break - fi - done -} <$SERIAL_PORT diff --git a/src/modbus.c b/modbus.c similarity index 100% rename from src/modbus.c rename to modbus.c diff --git a/src/mqtt_handler.c b/mqtt_handler.c similarity index 100% rename from src/mqtt_handler.c rename to mqtt_handler.c diff --git a/src/network.c b/network.c similarity index 100% rename from src/network.c rename to network.c diff --git a/mosquitto.conf b/notes/mosquitto.conf similarity index 100% rename from mosquitto.conf rename to notes/mosquitto.conf diff --git a/lib/onewire/onewire.c b/onewire.c similarity index 100% rename from lib/onewire/onewire.c rename to onewire.c diff --git a/src/onewire_temp.c b/onewire_temp.c similarity index 70% rename from src/onewire_temp.c rename to onewire_temp.c index cfc6df2..255f1ca 100644 --- a/src/onewire_temp.c +++ b/onewire_temp.c @@ -1,12 +1,13 @@ #include "onewire_temp.h" -#include #include #include "debug.h" +#include "onewire.h" #include "systick.h" #define ONEWIRE_CONVERSION_TIME_MS 750 +#define ONEWIRE_MAX_RETRIES 255 typedef struct { uint8_t address[8]; @@ -14,6 +15,8 @@ typedef struct { onewire_state_t state; uint32_t convert_start_time; bool valid; + uint8_t error_count; + uint32_t last_success_time; } onewire_sensor_t; typedef struct { @@ -56,6 +59,8 @@ void onewire_temp_init(void) { sensor->valid = true; sensor->state = ONEWIRE_STATE_READY; sensor->temperature = ONEWIRE_TEMP_INVALID; + sensor->error_count = 0; + sensor->last_success_time = millis(); ow_sys.count++; } } @@ -89,7 +94,7 @@ static float convert_temperature(const uint8_t* data, uint8_t family_code) { } static bool start_conversion(onewire_sensor_t* sensor) { - if (!sensor->valid || sensor->state != ONEWIRE_STATE_READY) { + if (!sensor->state != ONEWIRE_STATE_READY) { return false; } @@ -121,6 +126,8 @@ static bool read_temperature(onewire_sensor_t* sensor) { } sensor->temperature = convert_temperature(data, sensor->address[0]); + sensor->error_count = 0; + sensor->last_success_time = millis(); return true; } @@ -137,7 +144,13 @@ void onewire_temp_process(void) { switch (sensor->state) { case ONEWIRE_STATE_READY: if (!ow_sys.parallel_mode) { - start_conversion(sensor); + if (!start_conversion(sensor)) { + sensor->error_count++; + if (sensor->error_count >= ONEWIRE_MAX_RETRIES) { + sensor->valid = false; + DEBUG_PRINT("sensor %d marked temporarily invalid after %d retries\n", i, ONEWIRE_MAX_RETRIES); + } + } } break; @@ -149,7 +162,14 @@ void onewire_temp_process(void) { case ONEWIRE_STATE_READ: if (!read_temperature(sensor)) { - sensor->valid = false; + sensor->error_count++; + if (sensor->error_count >= ONEWIRE_MAX_RETRIES) { + sensor->valid = false; + DEBUG_PRINT("sensor %d marked temporarily invalid after %d retries\n", i, ONEWIRE_MAX_RETRIES); + } + } else { + sensor->valid = true; // re-enable + sensor->error_count = 0; } sensor->state = ONEWIRE_STATE_READY; break; @@ -256,7 +276,7 @@ void onewire_temp_publish_discovery(MQTTClient* client, const char* node_id) { publish_retained(client, topic, display_name); snprintf(topic, sizeof(topic), "homie/%s/%s/$properties", node_id, sensor_name); - publish_retained(client, topic, "temperature,address"); + publish_retained(client, topic, "temperature,address,status,error_count"); // temperature properties snprintf(topic, sizeof(topic), "homie/%s/%s/temperature/$name", node_id, sensor_name); @@ -279,6 +299,18 @@ void onewire_temp_publish_discovery(MQTTClient* client, const char* node_id) { addr[4], addr[5], addr[6], addr[7]); snprintf(topic, sizeof(topic), "homie/%s/%s/address", node_id, sensor_name); publish_retained(client, topic, addr_str); + + snprintf(topic, sizeof(topic), "homie/%s/%s/status/$name", node_id, sensor_name); + publish_retained(client, topic, "Sensor Status"); + snprintf(topic, sizeof(topic), "homie/%s/%s/status/$datatype", node_id, sensor_name); + publish_retained(client, topic, "enum"); + snprintf(topic, sizeof(topic), "homie/%s/%s/status/$format", node_id, sensor_name); + publish_retained(client, topic, "valid,invalid"); + + snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$name", node_id, sensor_name); + publish_retained(client, topic, "Error Count"); + snprintf(topic, sizeof(topic), "homie/%s/%s/error_count/$datatype", node_id, sensor_name); + publish_retained(client, topic, "integer"); } } @@ -289,36 +321,53 @@ void onewire_temp_publish_values(MQTTClient* client, const char* node_id) { uint8_t sensor_count = onewire_temp_count(); for (uint8_t i = 0; i < sensor_count; i++) { - if (!onewire_temp_valid(i)) continue; - - float temp = onewire_temp_get(i); - if (temp == ONEWIRE_TEMP_INVALID) continue; - - // fixed-point conversion - int16_t temp_fixed = (int16_t)(temp * 100); - + bool is_valid = onewire_temp_valid(i); const uint8_t* addr = onewire_temp_address(i); - snprintf(topic, sizeof(topic), - "homie/%s/temp_%02x%02x%02x%02x%02x%02x%02x%02x/temperature", - node_id, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], - addr[6], addr[7]); - uint8_t neg = temp_fixed < 0; - if (neg) temp_fixed = -temp_fixed; + char base_topic[MAX_TOPIC_LENGTH]; + snprintf(base_topic, sizeof(base_topic), + "homie/%s/temp_%02x%02x%02x%02x%02x%02x%02x%02x", + node_id, addr[0], addr[1], addr[2], addr[3], + addr[4], addr[5], addr[6], addr[7]); - uint8_t idx = 0; - if (neg) value[idx++] = '-'; - - uint16_t whole = temp_fixed / 100; - uint8_t decimal = temp_fixed % 100; - - if (whole >= 10) value[idx++] = '0' + (whole / 10); - value[idx++] = '0' + (whole % 10); - value[idx++] = '.'; - value[idx++] = '0' + (decimal / 10); - value[idx++] = '0' + (decimal % 10); - value[idx] = '\0'; + // publish status + snprintf(topic, sizeof(topic), "%s/status", base_topic); + publish_message(client, is_valid ? "valid" : "invalid", topic); + // publish error cnt + snprintf(topic, sizeof(topic), "%s/error_count", base_topic); + snprintf(value, sizeof(value), "%u", ow_sys.sensors[i].error_count); publish_message(client, value, topic); + + snprintf(topic, sizeof(topic), "%s/temperature", base_topic); + + if (is_valid) { + float temp = onewire_temp_get(i); + if (temp == ONEWIRE_TEMP_INVALID) { + publish_message(client, "0.00", topic); + } else { + // temp to str + int16_t temp_fixed = (int16_t)(temp * 100); + uint8_t neg = temp_fixed < 0; + if (neg) temp_fixed = -temp_fixed; + + uint8_t idx = 0; + if (neg) value[idx++] = '-'; + + uint16_t whole = temp_fixed / 100; + uint8_t decimal = temp_fixed % 100; + + if (whole >= 10) value[idx++] = '0' + (whole / 10); + value[idx++] = '0' + (whole % 10); + value[idx++] = '.'; + value[idx++] = '0' + (decimal / 10); + value[idx++] = '0' + (decimal % 10); + value[idx] = '\0'; + + publish_message(client, value, topic); + } + } else { + publish_message(client, "0.00", topic); + } } } diff --git a/platformio.ini b/platformio.ini deleted file mode 100644 index e274320..0000000 --- a/platformio.ini +++ /dev/null @@ -1,26 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html - -[env] -platform = https://git.hye.su/mira/platform-ch32v.git -platform_packages = - toolchain-riscv @ https://git.hye.su/mira/toolchain-riscv-linux.git ; gcc 14.2.0 - framework-ch32v003fun @ https://github.com/cnlohr/ch32v003fun.git ; upstream cnlohr repo -board = genericCH32V203C8T6 -framework = ch32v003fun -upload_protocol = wlink # isp, minichlink, wch-link, wlink -build_unflags = -march=rv32imacxw -build_flags = -march=rv32imac_zicsr -Wall -Wextra -debug_build_flags = -O0 -ggdb3 -g3 - -[env:local] -platform_packages = - toolchain-riscv @ symlink:///home/mira/src/xpack-riscv-none-elf-gcc-14.2.0-2 ; gcc 14.2.0 - framework-ch32v003fun @ https://github.com/cnlohr/ch32v003fun.git ; upstream cnlohr repo diff --git a/src/spi_dma.c b/spi_dma.c similarity index 100% rename from src/spi_dma.c rename to spi_dma.c diff --git a/src/systick.c b/systick.c similarity index 100% rename from src/systick.c rename to systick.c diff --git a/test/README b/test/README deleted file mode 100644 index 9b1e87b..0000000 --- a/test/README +++ /dev/null @@ -1,11 +0,0 @@ - -This directory is intended for PlatformIO Test Runner and project tests. - -Unit Testing is a software testing method by which individual units of -source code, sets of one or more MCU program modules together with associated -control data, usage procedures, and operating procedures, are tested to -determine whether they are fit for use. Unit testing finds problems early -in the development cycle. - -More information about PlatformIO Unit Testing: -- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html diff --git a/src/timer.c b/timer.c similarity index 94% rename from src/timer.c rename to timer.c index a5ce524..939a032 100644 --- a/src/timer.c +++ b/timer.c @@ -1,7 +1,6 @@ #include "timer.h" #include -#include #include "ch32v003fun.h" #include "debug.h" @@ -37,6 +36,5 @@ void TIM2_IRQHandler(void) { // DEBUG_PRINT("TIM2 IRQ\n"); DHCP_time_handler(); - DNS_time_handler(); } } diff --git a/src/uart.c b/uart.c similarity index 100% rename from src/uart.c rename to uart.c