From 9de7db11f620972b360b86916c257a26e195a459 Mon Sep 17 00:00:00 2001 From: zys Date: Fri, 20 Mar 2026 15:43:39 +0800 Subject: [PATCH] first commit for demo usage --- boards/nucleo_f429zi.overlay | 15 ++++ boards/rpi_pico.overlay | 25 +++++++ boards/test_potentiometer.overlay | 3 + include/modbus.hpp | 10 +++ prj.conf | 6 +- src/main.cpp | 111 +++++++++++++++++------------- 6 files changed, 116 insertions(+), 54 deletions(-) create mode 100644 boards/nucleo_f429zi.overlay create mode 100644 boards/rpi_pico.overlay create mode 100644 boards/test_potentiometer.overlay create mode 100644 include/modbus.hpp diff --git a/boards/nucleo_f429zi.overlay b/boards/nucleo_f429zi.overlay new file mode 100644 index 0000000..421dd65 --- /dev/null +++ b/boards/nucleo_f429zi.overlay @@ -0,0 +1,15 @@ +&spi0 { + status = "okay"; + potentiometer1: mcp41xxx@0 { + compatible = "microchip,mcp41xxx"; + reg = <0>; + res-kohms = <100>; + spi-max-frequency = <1000000>; + }; + potentiometer2: tpl0501@0 { + compatible = "ti,tpl0501"; + reg = <0>; + res-kohms = <100>; + spi-max-frequency = <1000000>; + }; +}; \ No newline at end of file diff --git a/boards/rpi_pico.overlay b/boards/rpi_pico.overlay new file mode 100644 index 0000000..1deaa72 --- /dev/null +++ b/boards/rpi_pico.overlay @@ -0,0 +1,25 @@ +/ { + zephyr,user { + in3-gpios = <&gpio0 7 GPIO_ACTIVE_HIGH>; + in4-gpios = <&gpio0 8 GPIO_ACTIVE_HIGH>; + enb-gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>; + }; +}; +&uart0 { + status = "okay"; + protocal: uart-com-protocal { + compatible = "uart-com-protocal"; + status = "okay"; + header = <0x7e 0xe7>; + crc16 = "none"; + freq{ + id = <0x00>; + }; + switch{ + id = <0x01>; + }; + status{ + id = <0x02>; + }; + }; +}; diff --git a/boards/test_potentiometer.overlay b/boards/test_potentiometer.overlay new file mode 100644 index 0000000..4498820 --- /dev/null +++ b/boards/test_potentiometer.overlay @@ -0,0 +1,3 @@ +/ { + +}; \ No newline at end of file diff --git a/include/modbus.hpp b/include/modbus.hpp new file mode 100644 index 0000000..a6e4b95 --- /dev/null +++ b/include/modbus.hpp @@ -0,0 +1,10 @@ +#ifndef __APP_MODBUS_HPP__ +#define __APP_MODBUS_HPP__ + +namespace zpp { + + + +} + +#endif \ No newline at end of file diff --git a/prj.conf b/prj.conf index 3da563d..59e87b8 100644 --- a/prj.conf +++ b/prj.conf @@ -1,5 +1,4 @@ CONFIG_STDOUT_CONSOLE=y -CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_STD_CPP20=y CONFIG_CPP=y CONFIG_REQUIRES_FULL_LIBCPP=y @@ -7,8 +6,5 @@ CONFIG_REQUIRES_FULL_LIBCPP=y CONFIG_CONSOLE=y CONFIG_SERIAL=y CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_PMC_COM=y -CONFIG_HWINFO=y -CONFIG_REBOOT=y -CONFIG_WATCHDOG=y \ No newline at end of file +CONFIG_LOG=y \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bcc99a3..29c6267 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,61 +1,74 @@ -#include -#include -#include - -#include #include -#include - -#include "watchdog.hpp" -#include "zpp/fmt.hpp" +#include +#include namespace { -auto sensor = DEVICE_DT_GET(DT_NODELABEL(godtek)); -auto &pmc = ZPP_DRV_GET(uart_com::Protocal, DT_NODELABEL(pm_protocal)); -auto &led_strip = ZPP_DRV_GET(ledstrip::Indicator, DT_NODELABEL(indicator)); -enum Command { kTemp = 0x00, kGetId, kRunningState }; -enum RunningState {}; -constexpr auto kDeviceIdSize = 20; -enum StatusId : uint8_t { kRunning, kPause, kError, kStandby }; -volatile bool flag{false}; +enum Cmd { + kFreq = 0x00, + kSwitch = 0x01, + kStatus = 0x02, +}; +#define ZEPHYR_USER_NODE DT_PATH(zephyr_user) +const gpio_dt_spec in3 = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, in3_gpios); +const gpio_dt_spec in4 = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, in4_gpios); +const gpio_dt_spec enb = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, enb_gpios); +volatile uint8_t duty_cycle_event{50}; +volatile bool is_enable{false}; +class Motor { +public: + Motor() { + gpio_pin_configure_dt(&in3, GPIO_OUTPUT_INACTIVE); + gpio_pin_configure_dt(&in4, GPIO_OUTPUT_INACTIVE); + gpio_pin_configure_dt(&enb, GPIO_OUTPUT_INACTIVE); + } + + auto Rotate() -> void { + gpio_pin_set_dt(&in3, 1); + gpio_pin_set_dt(&in4, 0); + }; + auto Reverse() { + gpio_pin_set_dt(&in3, 0); + gpio_pin_set_dt(&in4, 1); + }; + auto Stop() { + gpio_pin_set_dt(&in3, 0); + gpio_pin_set_dt(&in4, 0); + } +}; + +Motor motor{}; } // namespace auto main(void) -> int { - pmc.SetRxCallback(kGetId, [](uart_com::DataType data) -> void { - uint8_t buffer[kDeviceIdSize]; - auto size = hwinfo_get_device_id(buffer, sizeof(buffer)); - pmc.Send(kGetId, uart_com::DataType(buffer, size)); - }); + auto &protocal = ZPP_DRV_GET(uart_com::Protocal, DT_NODELABEL(protocal)); + protocal.SetRxCallback(kSwitch, [](uart_com::DataType data) { + if (data[0] == 0) { + motor.Stop(); + } else { + motor.Rotate(); + } - pmc.SetRxCallback(kRunningState, [](uart_com::DataType data) -> void { - auto state = static_cast(data[0]); - pmc.Send( - kRunningState, - uart_com::DataType(reinterpret_cast(&state), sizeof(state))); - led_strip.Status(state).on_error([](zpp::error_code code) {}); + printk("set switch: %d\n", data[0]); + is_enable = data[0]; + }); + protocal.SetRxCallback(kFreq, [](uart_com::DataType data) { + printk("set freq: %d\n", data[0]); + + uint8_t freq = 0; + if (data[0] == 100) { + freq = 100; + } + freq = data[0]; + duty_cycle_event = freq; }); - auto wdt = app::WatchDogConfig{}; - sensor_trigger tri{.type = SENSOR_TRIG_DATA_READY, - .chan = SENSOR_CHAN_AMBIENT_TEMP}; - if (auto r = sensor_trigger_set( - sensor, &tri, - [](const device *dev, const sensor_trigger *trig) { flag = true; }); - r != 0) { - return -ENODEV; - }; while (1) { - if (flag) { - sensor_value val{0, 0}; - if (sensor_sample_fetch_chan(sensor, SENSOR_CHAN_AMBIENT_TEMP) == 0) { - sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &val); - const uint8_t data[2] = {static_cast(val.val1), - static_cast(val.val2)}; - pmc.Send("temp", data); - } - - flag = false; + if (is_enable) { + printk("duty circle %d\n", duty_cycle_event); + gpio_pin_set_dt(&enb, 1); + k_sleep(K_MSEC(duty_cycle_event)); + gpio_pin_set_dt(&enb, 0); + k_sleep(K_MSEC(100 - duty_cycle_event)); } - wdt.Feed(); } return 0; -} \ No newline at end of file +}