备份,DAC可配置
This commit is contained in:
parent
0d289f6de5
commit
5f79a8dccd
|
@ -47,7 +47,7 @@
|
|||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Variables */
|
||||
|
||||
float current_buff[16] = {0};
|
||||
/* USER CODE END Variables */
|
||||
osThreadId lwip_taskHandle;
|
||||
osThreadId dac_taskHandle;
|
||||
|
@ -58,15 +58,15 @@ osThreadId ch438_taskHandle;
|
|||
|
||||
/* USER CODE END FunctionPrototypes */
|
||||
|
||||
void start_lwip_task(void const * argument);
|
||||
void start_dac_task(void const * argument);
|
||||
void start_ch438_task(void const * argument);
|
||||
void start_lwip_task(void const *argument);
|
||||
void start_dac_task(void const *argument);
|
||||
void start_ch438_task(void const *argument);
|
||||
|
||||
extern void MX_LWIP_Init(void);
|
||||
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
|
||||
|
||||
/* GetIdleTaskMemory prototype (linked to static allocation support) */
|
||||
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
|
||||
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize);
|
||||
|
||||
/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
|
||||
static StaticTask_t xIdleTaskTCBBuffer;
|
||||
|
@ -82,11 +82,12 @@ void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackTyp
|
|||
/* USER CODE END GET_IDLE_TASK_MEMORY */
|
||||
|
||||
/**
|
||||
* @brief FreeRTOS initialization
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void MX_FREERTOS_Init(void) {
|
||||
* @brief FreeRTOS initialization
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void MX_FREERTOS_Init(void)
|
||||
{
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
@ -123,7 +124,6 @@ void MX_FREERTOS_Init(void) {
|
|||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN Header_start_lwip_task */
|
||||
|
@ -133,7 +133,7 @@ void MX_FREERTOS_Init(void) {
|
|||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_start_lwip_task */
|
||||
void start_lwip_task(void const * argument)
|
||||
void start_lwip_task(void const *argument)
|
||||
{
|
||||
/* init code for LWIP */
|
||||
MX_LWIP_Init();
|
||||
|
@ -158,7 +158,7 @@ void start_lwip_task(void const * argument)
|
|||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_start_dac_task */
|
||||
void start_dac_task(void const * argument)
|
||||
void start_dac_task(void const *argument)
|
||||
{
|
||||
/* USER CODE BEGIN start_dac_task */
|
||||
/* Infinite loop */
|
||||
|
@ -168,7 +168,7 @@ void start_dac_task(void const * argument)
|
|||
uint8_t ch = 0;
|
||||
for (ch = 0; ch < DAC_MAX_NUM; ch++)
|
||||
{
|
||||
dac161s997_output(ch, 12.0f);
|
||||
dac161s997_output(ch, current_buff[ch]);
|
||||
}
|
||||
vTaskDelay(100);
|
||||
}
|
||||
|
@ -182,7 +182,7 @@ void start_dac_task(void const * argument)
|
|||
* @retval None
|
||||
*/
|
||||
/* USER CODE END Header_start_ch438_task */
|
||||
void start_ch438_task(void const * argument)
|
||||
void start_ch438_task(void const *argument)
|
||||
{
|
||||
/* USER CODE BEGIN start_ch438_task */
|
||||
/* Infinite loop */
|
||||
|
|
|
@ -1961,6 +1961,30 @@
|
|||
<RteFlg>0</RteFlg>
|
||||
<bShared>0</bShared>
|
||||
</File>
|
||||
<File>
|
||||
<GroupNumber>12</GroupNumber>
|
||||
<FileNumber>130</FileNumber>
|
||||
<FileType>1</FileType>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\User\application\src\user_lib.c</PathWithFileName>
|
||||
<FilenameWithoutPath>user_lib.c</FilenameWithoutPath>
|
||||
<RteFlg>0</RteFlg>
|
||||
<bShared>0</bShared>
|
||||
</File>
|
||||
<File>
|
||||
<GroupNumber>12</GroupNumber>
|
||||
<FileNumber>131</FileNumber>
|
||||
<FileType>1</FileType>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\User\application\src\communication_protocol.c</PathWithFileName>
|
||||
<FilenameWithoutPath>communication_protocol.c</FilenameWithoutPath>
|
||||
<RteFlg>0</RteFlg>
|
||||
<bShared>0</bShared>
|
||||
</File>
|
||||
</Group>
|
||||
|
||||
<Group>
|
||||
|
|
|
@ -6630,6 +6630,16 @@
|
|||
<FileType>1</FileType>
|
||||
<FilePath>..\User\application\src\tcpserverc.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>user_lib.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\User\application\src\user_lib.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>communication_protocol.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\User\application\src\communication_protocol.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
<Group>
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -20,6 +20,8 @@
|
|||
#define TCP_PORT_HART15 1015
|
||||
#define TCP_PORT_HART16 1016
|
||||
|
||||
#define TCP_PORT_CONTROL 5003
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TCP_HART1 = 0,
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#include "communication_protocol.h"
|
||||
#include "user_lib.h"
|
||||
#include "ad7124.h"
|
||||
|
||||
extern float current_buff[2];
|
||||
extern ad7124_analog_t ad7124_analog[AD7124_CHANNEL_EN_MAX];
|
||||
|
||||
extern float current_buff[16];
|
||||
|
||||
/**
|
||||
* @brief 处理通信异常
|
||||
*
|
||||
|
@ -25,36 +25,7 @@ void communication_exception(uint8_t *tx_data, const uint8_t *const rx_data, com
|
|||
tx_data[6] = FRAME_TAIL; // 帧尾
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取AI数据并封装成通信帧
|
||||
*
|
||||
* 该函数从指定的AI通道获取模拟数据,并将其封装成一个通信帧。
|
||||
*
|
||||
* @param ai_data 指向communication_ai_t结构体的指针,包含AI通道信息
|
||||
* @param tx_data 指向uint8_t数组的指针,用于存储生成的通信帧
|
||||
* @param rx_data 指向const uint8_t数组的指针,包含接收到的命令数据
|
||||
*/
|
||||
void communication_get_ai(communication_ai_t *ai_data, uint8_t *tx_data, const uint8_t *const rx_data)
|
||||
{
|
||||
float32_u analog_data;
|
||||
uint8_t tx_data_len = COM_AI_DATA_SIZE;
|
||||
analog_data.f = S2B_FLOAT32(ad7124_analog[ai_data->channel].current);
|
||||
tx_data[0] = FRAME_HEAD; // 帧头
|
||||
tx_data[1] = COM_OK; // 状态
|
||||
tx_data[2] = rx_data[2]; // 设备号
|
||||
tx_data[3] = rx_data[3]; // 命令号
|
||||
tx_data[4] = 4; // 数据长度
|
||||
tx_data[5] = analog_data.c; // 数据
|
||||
tx_data[6] = analog_data.c >> 8; // 数据
|
||||
tx_data[7] = analog_data.c >> 16; // 数据
|
||||
tx_data[8] = analog_data.c >> 24; // 数据
|
||||
tx_data[9] = xor_compute(tx_data + 1, tx_data_len - 3); // 异或校验
|
||||
tx_data[10] = FRAME_TAIL; // 帧尾
|
||||
}
|
||||
|
||||
void communication_set_ao(communication_ao_t *ao_data)
|
||||
{
|
||||
current_buff[ao_data->channel] = B2S_FLOAT32(ao_data->data.f);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -17,19 +17,16 @@
|
|||
|
||||
err_t (*tcpecho_accept_hart[TCP_MAX])(void *arg, struct tcp_pcb *newpcb, err_t err);
|
||||
struct tcp_pcb *server_pcb_hart[TCP_MAX] = {0};
|
||||
|
||||
struct tcp_pcb *server_pcb_control = NULL;
|
||||
// communication_di_t *user_communication_di = NULL;
|
||||
// communication_do_t *user_communication_do = NULL;
|
||||
// communication_ai_t *user_communication_ai = NULL;
|
||||
// communication_ao_t *user_communication_ao = NULL;
|
||||
communication_ao_t *user_communication_ao = NULL;
|
||||
|
||||
uint8_t tcp_echo_flags[TCP_MAX] = {0};
|
||||
uint32_t tcp_times[TCP_MAX] = {0};
|
||||
uart_t hart_uart[TCP_MAX] = {0};
|
||||
|
||||
extern uint8_t tcp_echo_flags_control;
|
||||
extern uint8_t send_data_flag_cmd;
|
||||
|
||||
/*接收回调函数*/
|
||||
static err_t tcpecho_recv_hart1(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
{ // 对应接收数据连接的控制块 接收到的数据
|
||||
|
@ -341,6 +338,69 @@ static err_t tcpecho_recv_hart16(void *arg, struct tcp_pcb *tpcb, struct pbuf *p
|
|||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
|
||||
{
|
||||
uint8_t tcp_rx_data[128] = {0}; // 接受数据缓存区
|
||||
uint8_t tcp_tx_data[128] = {0}; // 发送数据缓存区
|
||||
uint8_t rx_data_len = 0;
|
||||
uint8_t tx_data_len = 0;
|
||||
communication_data_u communication_data;
|
||||
if (p != NULL)
|
||||
{
|
||||
/* 更新窗口*/
|
||||
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
|
||||
server_pcb_control = tpcb; // 直接赋值
|
||||
memcpy(tcp_rx_data, (int *)p->payload, p->tot_len);
|
||||
rx_data_len = p->tot_len;
|
||||
/*1. 对接收的数据做异或校验、帧头帧尾判断,校验失败返回信息,校验通过继续下一步、校验数据从帧头后面到校验位结束*/
|
||||
if (tcp_rx_data[0] != 0xAA || tcp_rx_data[rx_data_len - 1] != 0x3C) // 帧头帧尾判断
|
||||
{
|
||||
tx_data_len = COM_ERROR_CODE_SIZE;
|
||||
communication_exception(tcp_tx_data, tcp_rx_data, FRAMING_ERROR);
|
||||
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tcp_rx_data[rx_data_len - 2] != xor_compute(tcp_rx_data + 1, rx_data_len - 3)) // 异或校验
|
||||
{
|
||||
tx_data_len = COM_ERROR_CODE_SIZE;
|
||||
communication_exception(tcp_tx_data, tcp_rx_data, CHECK_ERROR);
|
||||
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]);
|
||||
if (tcp_rx_data[3] == WRITE_ANALOG_CMD) // 写模拟量指令
|
||||
{
|
||||
/*写操作,将数据写入寄存器,组包返回*/
|
||||
|
||||
user_communication_ao = &communication_data.ao_data;
|
||||
communication_set_ao(user_communication_ao);
|
||||
tcp_write(tpcb, tcp_rx_data, rx_data_len, 1);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// 返回命令号错误
|
||||
tx_data_len = COM_ERROR_CODE_SIZE;
|
||||
communication_exception(tcp_tx_data, tcp_rx_data, COMMAND_ERROR);
|
||||
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/*2. 判断所要执行的操作 读或写指令*/
|
||||
/*3. 对要发送的数据进行校验,组包,返回数据*/
|
||||
}
|
||||
pbuf_free(p);
|
||||
}
|
||||
else if (err == ERR_OK) // 检测到对方主动关闭连接时,也会调用recv函数,此时p为空
|
||||
{
|
||||
return tcp_close(tpcb);
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t tcpecho_accept_hart1(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
|
||||
// 形参的数量和类型必须一致
|
||||
{
|
||||
|
@ -452,24 +512,17 @@ static err_t tcpecho_accept_hart16(void *arg, struct tcp_pcb *newpcb, err_t err)
|
|||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static err_t tcpecho_accept_control(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
|
||||
{
|
||||
tcp_recv(newpcb, tcpecho_recv_control); // 当收到数据时,回调用户自己写的tcpecho_recv
|
||||
return ERR_OK;
|
||||
}
|
||||
void tcp_echo_init(void)
|
||||
{
|
||||
struct tcp_pcb *server_hart[TCP_MAX] = {0};
|
||||
// struct tcp_pcb *server_hart2 = NULL;
|
||||
// struct tcp_pcb *server_hart3 = NULL;
|
||||
// struct tcp_pcb *server_hart4 = NULL;
|
||||
// struct tcp_pcb *server_hart5 = NULL;
|
||||
// struct tcp_pcb *server_hart6 = NULL;
|
||||
// struct tcp_pcb *server_hart7 = NULL;
|
||||
// struct tcp_pcb *server_hart8 = NULL;
|
||||
// struct tcp_pcb *server_hart9 = NULL;
|
||||
// struct tcp_pcb *server_hart10 = NULL;
|
||||
// struct tcp_pcb *server_hart11 = NULL;
|
||||
// struct tcp_pcb *server_hart12 = NULL;
|
||||
// struct tcp_pcb *server_hart13 = NULL;
|
||||
// struct tcp_pcb *server_hart14 = NULL;
|
||||
// struct tcp_pcb *server_hart15 = NULL;
|
||||
// struct tcp_pcb *server_hart16 = NULL;
|
||||
struct tcp_pcb *server_control = NULL;
|
||||
|
||||
tcpecho_accept_hart[TCP_HART1] = tcpecho_accept_hart1;
|
||||
tcpecho_accept_hart[TCP_HART2] = tcpecho_accept_hart2;
|
||||
tcpecho_accept_hart[TCP_HART3] = tcpecho_accept_hart3;
|
||||
|
@ -499,18 +552,18 @@ void tcp_echo_init(void)
|
|||
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
|
||||
tcp_accept(server_hart[i], tcpecho_accept_hart[i]); // 侦听到连接后,回调用户编写的tcpecho_accept
|
||||
}
|
||||
// for (uint8_t i = TCP_HART9; i < TCP_MAX; i++)
|
||||
// {
|
||||
// server_hart[i] = NULL;
|
||||
// tcp_echo_flags[i] = 0;
|
||||
// server_hart[i] = tcp_new();
|
||||
// /* 绑定TCP控制块 */
|
||||
// tcp_bind(server_hart[i], IP_ADDR_ANY, TCP_PORT_HART9 + i);
|
||||
// /* 进入监听状态 */
|
||||
// server_hart[i] = tcp_listen(server_hart[i]);
|
||||
// /* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
|
||||
// tcp_accept(server_hart[i], tcpecho_accept_hart[i]); // 侦听到连接后,回调用户编写的tcpecho_accept
|
||||
// }
|
||||
|
||||
/* 创建控制块 */
|
||||
server_control = tcp_new();
|
||||
|
||||
/* 绑定TCP控制块 */
|
||||
tcp_bind(server_control, IP_ADDR_ANY, TCP_PORT_CONTROL);
|
||||
|
||||
/* 进入监听状态 */
|
||||
server_control = tcp_listen(server_control);
|
||||
|
||||
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
|
||||
tcp_accept(server_control, tcpecho_accept_control); // 侦听到连接后,回调用户编写的tcpecho_accept
|
||||
}
|
||||
|
||||
void user_send_data_hart(uint8_t ch, uint8_t *data, uint16_t len)
|
||||
|
|
|
@ -262,8 +262,8 @@ void ch438_send_data(uint8_t uart_num, uint8_t *data, uint16_t len)
|
|||
{
|
||||
hart_ht1200m_rts_io_send(uart_num);
|
||||
uint8_t ch438_num = 0;
|
||||
data[len] = 0xff; // 因为发送数据RHART的RTS需要延时,这里用多一个字节的发送来代替延时
|
||||
data[len + 1] = 0xff; // 因为发送数据RHART的RTS需要延时,这里用多两个字节的发送来代替延时
|
||||
data[len] = 0x00; // 因为发送数据RHART的RTS需要延时,这里用多一个字节的发送来代替延时
|
||||
data[len + 1] = 0x00; // 因为发送数据RHART的RTS需要延时,这里用多两个字节的发送来代替延时
|
||||
if (uart_num < 8)
|
||||
{
|
||||
ch438_num = CH438_CHIP0;
|
||||
|
|
Loading…
Reference in New Issue