修改驱动程序

This commit is contained in:
2584532475@qq.com committed 2023-05-19 18:02:00 +08:00
1 parent 063c3fcc07
commit a69c1a58d6
39 files changed
+1627 -641

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
#ifndef MODBUS_ADDR_H
#define MODBUS_ADDR_H
#include <stdint.h>
#define REG_GOPENH 0X0000
#define REG_GOPENL 0X0001
#define REG_ALOADH 0X0002
#define REG_ALOADL 0X0003
#define REG_BLOADH 0X0004
#define REG_BLOADL 0X0005
#define REG_SOPEN 0X0007
#define REG_SLOPT 0X0008 // 执行闸门动作
#define REG_MAX 0X0009
#endif
-4
View File
@@ -2,9 +2,6 @@
#define _NET_APP_H
// 远程升级宏
#define ETH_MQTT 1
#define EC200_MQTT 0
@@ -17,7 +14,6 @@ typedef struct netWorkInterface_t
int (*IsReady) (void);
int (*IsIdle) (void);
int (*ReSubTopic) (char *);
void (*Task) (void);
} netWorkInterface_t;
+67 -18
View File
@@ -6,12 +6,42 @@
#include <stdio.h>
#include <rtthread.h>
#include <net-app.h>
#include <easyflash.h>
#include <string.h>
#include "usart-board.h"
ALIGN(RT_ALIGN_SIZE)
static char thread_stack[4096];
static struct rt_thread netapp_thread;
static rt_event_t gnetEvents;
#define EVENT_COMM_TX (1U << 0)
#define EVENT_COMM_RX (1U << 1) //
// case ssKHYSet:
// {
// vv = value*10;
// dbg_printf ("MbusSetValue vv:%d\r\n",vv);
// sendcmd[type][4] = ((vv >> 8) & 0xff);
// sendcmd[type][5] = (vv & 0xff);
// vv = bsp_crc_calc16((uint8_t *)sendcmd[type],6);
// sendcmd[type][7] = ((vv >> 8) & 0xff);
// sendcmd[type][6] = (vv & 0xff);
// for(int i = 0;i < 8;i++)
// {
// dbg_printf("%02X ",sendcmd[type][i]);
// }
// dbg_printf("\r\n");
// }
// {0x01, 0x03, 0x00, 0x00, 0x00, 0x06, 0xC5, 0xC8}, // 闸位计读取开度
// {0x01, 0x06, 0x00, 0x07, 0x00, 0x02, 0xE6, 0x69}, // 闸位计设置开度
// {0x01, 0x06, 0x00, 0x04, 0x01, 0x00, 0xC9, 0x9B}, // 启动闸位计
// {0x01, 0x06, 0x00, 0x04, 0x00, 0x00, 0xC8, 0x0B}, // 停止闸位计
static enum {
netDrvInit = 0, // 初始化网络驱动
@@ -22,45 +52,62 @@ static enum {
}eDrvStatus;
static netWorkInterface_t interface;
static void net_send_frame(void)
{
rt_event_send(gnetEvents,EVENT_COMM_TX) ;
}
// 网络链接成功
static void net_connected(void)
{
}
static void thread_entry(void * argument)
{
eDrvStatus = netDrvInit;
rt_kprintf("%s was start!\r\n",__FILE__);
gnetEvents = rt_event_create("gnetEvents", RT_IPC_FLAG_FIFO) ;
// 读取网络驱动类型
char * drv = ef_get_env ("net_drv"); // 读取网络类型
if(strcmp("dtu",drv) == 0) // 驱动程序位DTU模式,就是不通过本地配置,直接透传
{
DTUThreadStart();
// 初始化串口驱动
// interface.Init = NULL;
// interface.IsConnected = NULL;
// interface.IsIdle = NULL;
// interface.IsReady = NULL;
// interface.ReadData = NULL;
// interface.ReSubTopic = NULL;
// interface.WriteData = NULL;
}
// 注册网络驱动
// 注册网络驱动
while (1)
{
switch (eDrvStatus)
// 发送数据
if( RT_EOK == rt_event_recv( gnetEvents, EVENT_COMM_TX,RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, RT_NULL ))
{
case netDrvInit:
break;
case netDrvJoin:
break;
case netDrvSend:
break;
case netDrvRecv:
break;
case netDrvIdle:
break;
default :
break;
}
rt_thread_delay(10);
if ( RT_EOK == rt_event_recv( gnetEvents, EVENT_COMM_RX,RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, RT_NULL ))
{
}
rt_thread_delay(10);
}
}
void NET_APPThreadStart(void )
{
/* 创建线程1,名称是thread1,入口是thread1_entry*/
/* 创建线程1,名称是thread1,入口是thread1_entry*/
rt_thread_init(&netapp_thread, "netapp_thread",
thread_entry, RT_NULL,
&thread_stack[0],
sizeof(thread_stack),
NET_APP_THREAD_PRIORITY, NET_APP_THREAD_TIMESLICE);
rt_thread_startup(&netapp_thread);
return ;
@@ -75,3 +122,5 @@ void NET_APPThreadStart(void )
+172
View File
@@ -0,0 +1,172 @@
#include "main.h"
#include "os-app.h"
#include <stdio.h>
#include <rtthread.h>
#include <net-app.h>
#include <easyflash.h>
#include <string.h>
#include "usart-board.h"
#include "modbus-s.h"
#include "modbusaddr.h"
ALIGN(RT_ALIGN_SIZE)
static char thread_stack[4096];
static struct rt_thread probus_thread;
uint16_t REG_DATA[REG_MAX] = {0x00};
// 寄存器对应的数据
// case ssKHYSet:
// {
// vv = value*10;
// dbg_printf ("MbusSetValue vv:%d\r\n",vv);
// sendcmd[type][4] = ((vv >> 8) & 0xff);
// sendcmd[type][5] = (vv & 0xff);
// vv = bsp_crc_calc16((uint8_t *)sendcmd[type],6);
// sendcmd[type][7] = ((vv >> 8) & 0xff);
// sendcmd[type][6] = (vv & 0xff);
// for(int i = 0;i < 8;i++)
// {
// dbg_printf("%02X ",sendcmd[type][i]);
// }
// dbg_printf("\r\n");
// }
// {0x01, 0x03, 0x00, 0x00, 0x00, 0x06, 0xC5, 0xC8}, // 闸位计读取开度
// {0x01, 0x06, 0x00, 0x07, 0x00, 0x02, 0xE6, 0x69}, // 闸位计设置开度
// {0x01, 0x06, 0x00, 0x04, 0x01, 0x00, 0xC9, 0x9B}, // 启动闸位计
// {0x01, 0x06, 0x00, 0x04, 0x00, 0x00, 0xC8, 0x0B}, // 停止闸位计
static ModBusSlave_t modbus_slave;
static uint8_t modbus_rx_data[32];
static void eth2_data_rxdone(void)
{
rt_event_send(gUartEvents,EVENT_UART_ETH2_RX);
//rt_kprintf("net_data_rxdone\r\n");
}
static void probus_handle(uint16_t addr,uint8_t * data)
{
switch (addr)
{
case REG_SOPEN: // 设定闸门开度
break;
case REG_SLOPT: // 执行闸门动作
if (data[0] == 0x01) // 执行
{
rt_event_send(gCtrlEvents,EVENT_CTRL_SET_OPEN);
rt_kprintf("probus_handle set open\r\n");
}
else
{
}
break;
default:
break;
}
}
static void thread_entry(void * argument)
{
int k = 0;
uint8_t txbuff[128] = {0};
int tx_size = 0;
rt_kprintf("%s was start!\r\n",__FILE__);
g_ETH2Drv.rxdone = eth2_data_rxdone;
g_ETH2Drv.BaudRate = 115200;
MX_UART_Init(&g_ETH2Drv);
ModbusSlaveInit(&modbus_slave,0x01,modbus_rx_data,REG_DATA,REG_MAX);
// 注册网络驱动
// 注册网络驱动
while (1)
{
if( RT_EOK == rt_event_recv( gUartEvents, EVENT_UART_ETH2_RX,RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, RT_NULL ))
{
// rt_kprintf("<<<<<<<<<<<<<<<<<<<<probus2:\r\n");
// for(int i =0; i < g_ETH2Drv.rx_size;i++)
// {
// rt_kprintf("%02X ",g_ETH2Drv.rx_buffer[i]);
// }
// rt_kprintf("\r\n");
k = ModbusSlaveHandle(&modbus_slave,(uint8_t *)g_ETH2Drv.rx_buffer,g_ETH2Drv.rx_size);
//rt_kprintf("k:%d\r\n",k);
if( k == 0)
{
switch (modbus_slave.func)
{
case 0x03:
tx_size = ModbusSlavePacket(&modbus_slave,txbuff);
// 直接返回当前寄存器中的数据
break;
case 0x06:
//处理对应的事件
probus_handle(modbus_slave.Addr,modbus_slave.data);
// 数据打包回复
tx_size = ModbusSlavePacket(&modbus_slave,txbuff);
break;
default:
tx_size = 0;
break;
}
}
else
{
tx_size = 0;
}
if (tx_size > 0)
{
MX_UART_PutBuffer(&g_ETH2Drv,(char *)txbuff,tx_size);
}
MX_UART_Clear(&g_ETH2Drv);
}
rt_thread_delay(10);
}
}
void PROBUS_APPThreadStart(void )
{
/* 创建线程1,名称是thread1,入口是thread1_entry*/
rt_thread_init(&probus_thread, "probus_thread",
thread_entry, RT_NULL,
&thread_stack[0],
sizeof(thread_stack),
PROBUS_APP_THREAD_PRIORITY, PROBUS_APP_THREAD_TIMESLICE);
rt_thread_startup(&probus_thread);
return ;
}