forked from EmbeddedTeam/app_photomagnetic
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab818974f3 | ||
|
|
9de7db11f6 |
15
boards/nucleo_f429zi.overlay
Normal file
15
boards/nucleo_f429zi.overlay
Normal file
@ -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>;
|
||||
};
|
||||
};
|
||||
21
boards/rpi_pico.overlay
Normal file
21
boards/rpi_pico.overlay
Normal file
@ -0,0 +1,21 @@
|
||||
/ {
|
||||
dc_motor: dc-motor0 {
|
||||
status = "okay";
|
||||
compatible = "st,l298n";
|
||||
freq-hz = <1000>;
|
||||
in1-gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>;
|
||||
in2-gpios = <&gpio0 11 GPIO_ACTIVE_HIGH>;
|
||||
en-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
aliases {
|
||||
modbus-server-pc = &modbus_pc;
|
||||
};
|
||||
};
|
||||
|
||||
&uart1 {
|
||||
status = "okay";
|
||||
current-speed = <115200>;
|
||||
modbus_pc: modbus-server0 {
|
||||
status = "okay";
|
||||
};
|
||||
};
|
||||
3
boards/test_potentiometer.overlay
Normal file
3
boards/test_potentiometer.overlay
Normal file
@ -0,0 +1,3 @@
|
||||
/ {
|
||||
|
||||
};
|
||||
10
include/modbus.hpp
Normal file
10
include/modbus.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef __APP_MODBUS_HPP__
|
||||
#define __APP_MODBUS_HPP__
|
||||
|
||||
namespace zpp {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
8
prj.conf
8
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,7 @@ 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
|
||||
CONFIG_MODBUS=y
|
||||
CONFIG_MODBUS_ROLE_SERVER=y
|
||||
CONFIG_LOG=y
|
||||
143
src/main.cpp
143
src/main.cpp
@ -1,61 +1,96 @@
|
||||
#include <etl/vector.h>
|
||||
#include <zephyr/drivers/hwinfo.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
|
||||
#include <led_strip_indicator/led_strip_indicator.hpp>
|
||||
#include <uart_com/protocal.hpp>
|
||||
#include <zpp/device.hpp>
|
||||
|
||||
#include "watchdog.hpp"
|
||||
#include "zpp/fmt.hpp"
|
||||
#include <dc_motor/motor.hpp>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/modbus/modbus.h>
|
||||
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};
|
||||
|
||||
class ModbusServer {
|
||||
public:
|
||||
struct Addr {
|
||||
enum Coil : uint16_t { MOTOR_RUNNING = 0 };
|
||||
enum Reg : uint16_t {
|
||||
SPEED = 0,
|
||||
};
|
||||
};
|
||||
ModbusServer() {
|
||||
m_iface =
|
||||
modbus_iface_get_by_name(DEVICE_DT_NAME(DT_ALIAS(modbus_server_pc)));
|
||||
modbus_init_server(m_iface, m_param);
|
||||
}
|
||||
|
||||
private:
|
||||
static auto CoilWr(uint16_t addr, bool state) -> int {
|
||||
switch (addr) {
|
||||
case Addr::Coil::MOTOR_RUNNING: {
|
||||
if (0 == state) {
|
||||
m_motor.SetSpeed(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
static auto CoilRd(uint16_t addr, bool *state) -> int {
|
||||
switch (addr) {
|
||||
case Addr::Coil::MOTOR_RUNNING: {
|
||||
const auto s = m_motor.GetState();
|
||||
*state = (s == driver::DcMotorState::RUNNING);
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
static auto RegWr(uint16_t addr, uint16_t reg) -> int {
|
||||
switch (addr) {
|
||||
case Addr::Reg::SPEED: {
|
||||
m_current_speed = reg;
|
||||
return int(m_motor.SetSpeed(reg).code_id());
|
||||
}
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
static auto RedRd(uint16_t addr, uint16_t *reg) -> int {
|
||||
switch (addr) {
|
||||
case Addr::Reg::SPEED:
|
||||
*reg = m_current_speed;
|
||||
return 0;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
inline static modbus_user_callbacks m_cbs = {.coil_rd = CoilRd,
|
||||
.coil_wr = CoilWr,
|
||||
.holding_reg_rd = RedRd,
|
||||
.holding_reg_wr = RegWr};
|
||||
inline static modbus_iface_param m_param = {
|
||||
.mode = MODBUS_MODE_RTU,
|
||||
.server =
|
||||
{
|
||||
.user_cb = &m_cbs,
|
||||
.unit_id = 1,
|
||||
},
|
||||
.serial =
|
||||
{
|
||||
.baud =
|
||||
DT_PROP(DT_PARENT(DT_ALIAS(modbus_server_pc)), current_speed),
|
||||
|
||||
.parity = UART_CFG_PARITY_NONE,
|
||||
},
|
||||
};
|
||||
inline static int m_iface;
|
||||
inline static int m_current_speed{0};
|
||||
inline static auto &m_motor =
|
||||
ZPP_DRV_GET(driver::DcMotor, DT_NODELABEL(dc_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));
|
||||
});
|
||||
|
||||
pmc.SetRxCallback(kRunningState, [](uart_com::DataType data) -> void {
|
||||
auto state = static_cast<StatusId>(data[0]);
|
||||
pmc.Send(
|
||||
kRunningState,
|
||||
uart_com::DataType(reinterpret_cast<uint8_t *>(&state), sizeof(state)));
|
||||
led_strip.Status(state).on_error([](zpp::error_code code) {});
|
||||
});
|
||||
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;
|
||||
};
|
||||
|
||||
ModbusServer server{};
|
||||
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<uint8_t>(val.val1),
|
||||
static_cast<uint8_t>(val.val2)};
|
||||
pmc.Send("temp", data);
|
||||
}
|
||||
|
||||
flag = false;
|
||||
}
|
||||
wdt.Feed();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user