新增下位机主动上发状态,只要DI有电平变化主动上发一次

This commit is contained in:
王绪洁 2025-02-25 10:51:18 +08:00
parent 5f91224ebb
commit bd55a7ca61
8 changed files with 84 additions and 225 deletions

View File

@ -41,6 +41,7 @@
"freertosconfig.h": "c", "freertosconfig.h": "c",
"communication_protocol.h": "c", "communication_protocol.h": "c",
"user_gpio.h": "c", "user_gpio.h": "c",
"user_lib.h": "c" "user_lib.h": "c",
"init.h": "c"
} }
} }

View File

@ -30,6 +30,8 @@
#include "usart.h" #include "usart.h"
#include "communication_protocol.h" #include "communication_protocol.h"
#include "tim.h" #include "tim.h"
#include "gpio.h"
#include "tcpserverc.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
@ -61,22 +63,25 @@ osThreadId ec11_taskHandle;
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */ /* USER CODE BEGIN FunctionPrototypes */
extern float current_buff[2]; extern float current_buff[2];
extern uint8_t tcp_echo_flags_control;
int direction = 0; int direction = 0;
int encode_num = 0; int encode_num = 0;
uint8_t di_state_last[DI_MAX] = {0};
uint8_t di_state_now[DI_MAX] = {0};
/* USER CODE END FunctionPrototypes */ /* USER CODE END FunctionPrototypes */
void start_tcp_task(void const * argument); void start_tcp_task(void const *argument);
void start_led_toggle_task(void const * argument); void start_led_toggle_task(void const *argument);
void start_dac_task(void const * argument); void start_dac_task(void const *argument);
void start_adc_task(void const * argument); void start_adc_task(void const *argument);
void start_gpio_di_do_task(void const * argument); void start_gpio_di_do_task(void const *argument);
void start_ec11_task(void const * argument); void start_ec11_task(void const *argument);
extern void MX_LWIP_Init(void); extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* GetIdleTaskMemory prototype (linked to static allocation support) */ /* 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 */ /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer; static StaticTask_t xIdleTaskTCBBuffer;
@ -96,7 +101,8 @@ void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackTyp
* @param None * @param None
* @retval None * @retval None
*/ */
void MX_FREERTOS_Init(void) { void MX_FREERTOS_Init(void)
{
/* USER CODE BEGIN Init */ /* USER CODE BEGIN Init */
/* USER CODE END Init */ /* USER CODE END Init */
@ -145,7 +151,6 @@ void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN RTOS_THREADS */ /* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */ /* add threads, ... */
/* USER CODE END RTOS_THREADS */ /* USER CODE END RTOS_THREADS */
} }
/* USER CODE BEGIN Header_start_tcp_task */ /* USER CODE BEGIN Header_start_tcp_task */
@ -155,7 +160,7 @@ void MX_FREERTOS_Init(void) {
* @retval None * @retval None
*/ */
/* USER CODE END Header_start_tcp_task */ /* USER CODE END Header_start_tcp_task */
void start_tcp_task(void const * argument) void start_tcp_task(void const *argument)
{ {
/* init code for LWIP */ /* init code for LWIP */
MX_LWIP_Init(); MX_LWIP_Init();
@ -176,7 +181,7 @@ void start_tcp_task(void const * argument)
* @retval None * @retval None
*/ */
/* USER CODE END Header_start_led_toggle_task */ /* USER CODE END Header_start_led_toggle_task */
void start_led_toggle_task(void const * argument) void start_led_toggle_task(void const *argument)
{ {
/* USER CODE BEGIN start_led_toggle_task */ /* USER CODE BEGIN start_led_toggle_task */
/* Infinite loop */ /* Infinite loop */
@ -195,7 +200,7 @@ void start_led_toggle_task(void const * argument)
* @retval None * @retval None
*/ */
/* USER CODE END Header_start_dac_task */ /* 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 */ /* USER CODE BEGIN start_dac_task */
dac161s997_init(); dac161s997_init();
@ -218,7 +223,7 @@ void start_dac_task(void const * argument)
* @retval None * @retval None
*/ */
/* USER CODE END Header_start_adc_task */ /* USER CODE END Header_start_adc_task */
void start_adc_task(void const * argument) void start_adc_task(void const *argument)
{ {
/* USER CODE BEGIN start_adc_task */ /* USER CODE BEGIN start_adc_task */
ad7124_setup(); ad7124_setup();
@ -253,13 +258,39 @@ void start_adc_task(void const * argument)
* @retval None * @retval None
*/ */
/* USER CODE END Header_start_gpio_di_do_task */ /* USER CODE END Header_start_gpio_di_do_task */
void start_gpio_di_do_task(void const * argument) void start_gpio_di_do_task(void const *argument)
{ {
/* USER CODE BEGIN start_gpio_di_do_task */ /* USER CODE BEGIN start_gpio_di_do_task */
/* Infinite loop */ /* Infinite loop */
for (;;) for (;;)
{ {
osThreadTerminate(NULL); uint8_t di_flag = 0;
uint8_t di_ch = 0;
uint8_t tx_data_len = 7 + DI_MAX;
uint8_t tx_data[32] = {0};
tx_data[0] = FRAME_HEAD; // 帧头
tx_data[1] = COM_OK; // 状态
tx_data[2] = DEVICE_NUM; // 设备号
tx_data[3] = SEND_STATE_CMD; // 命令号
tx_data[4] = DI_MAX; // 数据长度
for (di_ch = 0; di_ch < DI_MAX; di_ch++)
{
di_state_now[di_ch] = gpio_di_test(di_ch);
if (di_state_last[di_ch] != di_state_now[di_ch])
{
di_state_last[di_ch] = di_state_now[di_ch];
di_flag = 1;
}
tx_data[5 + di_ch] = di_state_now[di_ch];
}
if ((1 == di_flag) && (1 == tcp_echo_flags_control))
{
tx_data[5 + DI_MAX] = xor_compute(tx_data + 1, tx_data_len - 3); // 异或校验
tx_data[6 + DI_MAX] = FRAME_TAIL; // 帧尾
user_send_data_control(tx_data, tx_data_len);
di_flag = 0;
}
vTaskDelay(10);
} }
/* USER CODE END start_gpio_di_do_task */ /* USER CODE END start_gpio_di_do_task */
} }
@ -271,7 +302,7 @@ void start_gpio_di_do_task(void const * argument)
* @retval None * @retval None
*/ */
/* USER CODE END Header_start_ec11_task */ /* USER CODE END Header_start_ec11_task */
void start_ec11_task(void const * argument) void start_ec11_task(void const *argument)
{ {
/* USER CODE BEGIN start_ec11_task */ /* USER CODE BEGIN start_ec11_task */
/* Infinite loop */ /* Infinite loop */
@ -279,7 +310,7 @@ void start_ec11_task(void const * argument)
{ {
direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim1); direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim1);
encode_num = (short)__HAL_TIM_GET_COUNTER(&htim1); encode_num = (short)__HAL_TIM_GET_COUNTER(&htim1);
vTaskDelay(100); vTaskDelay(10);
} }
/* USER CODE END start_ec11_task */ /* USER CODE END start_ec11_task */
} }

View File

@ -210,6 +210,11 @@
<WinNumber>1</WinNumber> <WinNumber>1</WinNumber>
<ItemText>ec11_taskHandle</ItemText> <ItemText>ec11_taskHandle</ItemText>
</Ww> </Ww>
<Ww>
<count>12</count>
<WinNumber>1</WinNumber>
<ItemText>di_state_last</ItemText>
</Ww>
</WatchWindow1> </WatchWindow1>
<Tracepoint> <Tracepoint>
<THDelay>0</THDelay> <THDelay>0</THDelay>

View File

@ -1,194 +0,0 @@
<html>
<body>
<pre>
<h1>µVision Build Log</h1>
<h2>Tool Versions:</h2>
IDE-Version: ¦ÌVision V5.36.0.0
Copyright (C) 2021 ARM Ltd and ARM Germany GmbH. All rights reserved.
License Information: aaa Administrator, aaa, LIC=6XJT4-F8J98-8YUVV-P833R-DBAKX-Y8EU6
Tool Versions:
Toolchain: MDK-ARM Professional Version: 5.36.0.0
Toolchain Path: C:\Keil_v5\ARM\ARMCC\Bin
C Compiler: Armcc.exe V5.06 update 7 (build 960)
Assembler: Armasm.exe V5.06 update 7 (build 960)
Linker/Locator: ArmLink.exe V5.06 update 7 (build 960)
Library Manager: ArmAr.exe V5.06 update 7 (build 960)
Hex Converter: FromElf.exe V5.06 update 7 (build 960)
CPU DLL: SARMCM3.DLL V5.36.0.0
Dialog DLL: DCM.DLL V1.17.3.0
Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V3.0.9.0
Dialog DLL: TCM.DLL V1.53.0.0
<h2>Project:</h2>
D:\WORK\positioner_testing\Semi-finished product testing\MDK-ARM\semi-finished_product_testing.uvprojx
Project File Date: 02/21/2025
<h2>Output:</h2>
*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'semi-finished_product_testing'
assembling startup_stm32f407xx.s...
compiling lan8742.c...
compiling main.c...
compiling tim.c...
compiling usart.c...
compiling stm32f4xx_hal_msp.c...
compiling freertos.c...
compiling lwip.c...
compiling stm32f4xx_hal_timebase_tim.c...
compiling stm32f4xx_hal_rcc.c...
compiling spi.c...
compiling dma.c...
compiling gpio.c...
compiling stm32f4xx_it.c...
compiling stm32f4xx_hal_rcc_ex.c...
compiling ethernetif.c...
compiling stm32f4xx_hal_flash.c...
compiling stm32f4xx_hal_flash_ramfunc.c...
compiling stm32f4xx_hal_flash_ex.c...
compiling stm32f4xx_hal_gpio.c...
compiling stm32f4xx_hal_dma.c...
compiling stm32f4xx_hal_pwr.c...
compiling stm32f4xx_hal_dma_ex.c...
compiling stm32f4xx_hal_cortex.c...
compiling stm32f4xx_hal_pwr_ex.c...
compiling stm32f4xx_hal_exti.c...
compiling stm32f4xx_hal.c...
compiling stm32f4xx_hal_spi.c...
compiling croutine.c...
compiling stm32f4xx_hal_tim_ex.c...
compiling event_groups.c...
compiling list.c...
compiling stm32f4xx_hal_eth.c...
compiling stm32f4xx_hal_uart.c...
compiling queue.c...
compiling stream_buffer.c...
compiling stm32f4xx_hal_tim.c...
compiling timers.c...
compiling heap_4.c...
compiling tasks.c...
compiling system_stm32f4xx.c...
compiling port.c...
compiling ccp.c...
compiling cmsis_os.c...
compiling auth.c...
compiling chap_ms.c...
compiling chap-md5.c...
compiling eui64.c...
compiling eap.c...
compiling ipcp.c...
compiling demand.c...
compiling ipv6cp.c...
compiling chap-new.c...
compiling fsm.c...
compiling lcp.c...
compiling magic.c...
compiling mppe.c...
compiling multilink.c...
compiling pppapi.c...
compiling ppp.c...
compiling pppcrypt.c...
compiling pppos.c...
compiling pppol2tp.c...
compiling pppoe.c...
compiling upap.c...
compiling utils.c...
compiling vj.c...
compiling bridgeif_fdb.c...
compiling ethernet.c...
compiling bridgeif.c...
compiling lowpan6.c...
compiling slipif.c...
compiling lowpan6_ble.c...
compiling lowpan6_common.c...
compiling zepif.c...
compiling ecp.c...
compiling err.c...
compiling netdb.c...
compiling api_lib.c...
compiling if_api.c...
compiling netifapi.c...
compiling netbuf.c...
compiling api_msg.c...
compiling altcp.c...
compiling tcpip.c...
compiling sockets.c...
compiling altcp_alloc.c...
compiling altcp_tcp.c...
compiling def.c...
compiling dns.c...
compiling inet_chksum.c...
compiling init.c...
compiling raw.c...
compiling ip.c...
compiling stats.c...
compiling memp.c...
compiling mem.c...
compiling sys.c...
compiling netif.c...
compiling pbuf.c...
compiling tcp.c...
compiling udp.c...
compiling tcp_in.c...
compiling timeouts.c...
compiling tcp_out.c...
compiling autoip.c...
compiling dhcp.c...
compiling igmp.c...
compiling icmp.c...
compiling etharp.c...
compiling ip4_addr.c...
compiling ip4.c...
compiling ip4_frag.c...
compiling dhcp6.c...
compiling ethip6.c...
compiling icmp6.c...
compiling ble_mx_02.c...
compiling inet6.c...
compiling user_lib.c...
compiling ip6.c...
compiling ip6_addr.c...
compiling ip6_frag.c...
compiling mld6.c...
compiling nd6.c...
compiling leds.c...
compiling sys_arch.c...
compiling communication_protocol.c...
compiling mqtt.c...
compiling tcpserverc.c...
compiling tcpclient.c...
compiling ad7124.c...
compiling user_gpio.c...
compiling dac161s997.c...
compiling user_spi.c...
compiling ht1200m.c...
linking...
Program Size: Code=89400 RO-data=1472 RW-data=1540 ZI-data=62964
"semi-finished_product_testing\semi-finished_product_testing.axf" - 0 Error(s), 0 Warning(s).
<h2>Software Packages used:</h2>
Package Vendor: ARM
http://www.keil.com/pack/ARM.CMSIS.5.8.0.pack
ARM.CMSIS.5.8.0
CMSIS (Common Microcontroller Software Interface Standard)
* Component: CORE Version: 5.5.0
Package Vendor: Keil
https://www.keil.com/pack/Keil.STM32F4xx_DFP.2.17.1.pack
Keil.STM32F4xx_DFP.2.17.1
STMicroelectronics STM32F4 Series Device Support, Drivers and Examples
<h2>Collection of Component include folders:</h2>
.\RTE\_semi-finished_product_testing
C:\Users\Administrator\AppData\Local\Arm\Packs\ARM\CMSIS\5.8.0\CMSIS\Core\Include
C:\Users\Administrator\AppData\Local\Arm\Packs\Keil\STM32F4xx_DFP\2.17.1\Drivers\CMSIS\Device\ST\STM32F4xx\Include
<h2>Collection of Component Files used:</h2>
* Component: ARM::CMSIS:CORE:5.5.0
Include file: CMSIS\Core\Include\tz_context.h
Build Time Elapsed: 00:00:08
</pre>
</body>
</html>

View File

@ -7,6 +7,12 @@
#define COM_AI_DATA_SIZE 11 // 模拟量数据返回帧长度 #define COM_AI_DATA_SIZE 11 // 模拟量数据返回帧长度
#define FRAME_HEAD 0xAA // 帧头 #define FRAME_HEAD 0xAA // 帧头
#define FRAME_TAIL 0x3C // 帧尾 #define FRAME_TAIL 0x3C // 帧尾
#define DEVICE_NUM 0x00 // 设备号
#define READ_ANALOG_CMD 0x00 // 读模拟量命令
#define WRITE_ANALOG_CMD 0x01 // 写模拟量命令
#define READ_DIGITAL_CMD 0x02 // 读数字量命令
#define WRITE_DIGITAL_CMD 0x03 // 写数字量命令
#define SEND_STATE_CMD 0x04 // 主动上发状态命令
typedef enum typedef enum
{ {

View File

@ -14,4 +14,5 @@ extern void user_send_data_hart1(uint8_t *data, uint16_t len);
extern void user_send_data_hart2(uint8_t *data, uint16_t len); extern void user_send_data_hart2(uint8_t *data, uint16_t len);
extern void user_send_data_ble1(uint8_t *data, uint16_t len); extern void user_send_data_ble1(uint8_t *data, uint16_t len);
extern void user_send_data_ble2(uint8_t *data, uint16_t len); extern void user_send_data_ble2(uint8_t *data, uint16_t len);
extern void user_send_data_control(uint8_t *data, uint16_t len);
#endif #endif

View File

@ -47,3 +47,5 @@ void communication_set_ao(communication_ao_t *ao_data)
{ {
current_buff[ao_data->channel] = B2S_FLOAT32(ao_data->data.f); current_buff[ao_data->channel] = B2S_FLOAT32(ao_data->data.f);
} }

View File

@ -137,6 +137,7 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
{ {
HAL_GPIO_TogglePin(LED3_G_GPIO_Port, LED3_G_Pin); HAL_GPIO_TogglePin(LED3_G_GPIO_Port, LED3_G_Pin);
/* 更新窗口*/ /* 更新窗口*/
tcp_echo_flags_control = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度 tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
server_pcb_control = tpcb; // 直接赋值 server_pcb_control = tpcb; // 直接赋值
memcpy(tcp_rx_data, (int *)p->payload, p->tot_len); memcpy(tcp_rx_data, (int *)p->payload, p->tot_len);
@ -159,7 +160,7 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
else else
{ {
memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]); memcpy(communication_data.data, tcp_rx_data + 5, tcp_rx_data[4]);
if (tcp_rx_data[3] == 0x00) // 读模拟量指令 if (tcp_rx_data[3] == READ_ANALOG_CMD) // 读模拟量指令
{ {
/*读操作,从寄存器读取数据,组包返回*/ /*读操作,从寄存器读取数据,组包返回*/
tx_data_len = COM_AI_DATA_SIZE; tx_data_len = COM_AI_DATA_SIZE;
@ -167,15 +168,15 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
communication_get_ai(user_communication_ai, tcp_tx_data, tcp_rx_data); communication_get_ai(user_communication_ai, tcp_tx_data, tcp_rx_data);
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
} }
else if (tcp_rx_data[3] == 0x01) // 写模拟量指令 else if (tcp_rx_data[3] == WRITE_ANALOG_CMD) // 写模拟量指令
{ {
/*写操作,将数据写入寄存器,组包返回*/ /*写操作,将数据写入寄存器,组包返回*/
tcp_echo_flags_control = 1;
user_communication_ao = &communication_data.ao_data; user_communication_ao = &communication_data.ao_data;
communication_set_ao(user_communication_ao); communication_set_ao(user_communication_ao);
tcp_write(tpcb, tcp_rx_data, rx_data_len, 1); tcp_write(tpcb, tcp_rx_data, rx_data_len, 1);
} }
else if (tcp_rx_data[3] == 0x02) // 读数字量指令 else if (tcp_rx_data[3] == READ_DIGITAL_CMD) // 读数字量指令
{ {
/*读操作,从寄存器读取数据,组包返回*/ /*读操作,从寄存器读取数据,组包返回*/
@ -184,7 +185,7 @@ static err_t tcpecho_recv_control(void *arg, struct tcp_pcb *tpcb, struct pbuf *
user_read_gpio(user_communication_di, tcp_tx_data, tcp_rx_data); user_read_gpio(user_communication_di, tcp_tx_data, tcp_rx_data);
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1); tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
} }
else if (tcp_rx_data[3] == 0x03) // 写数字量指令 else if (tcp_rx_data[3] == WRITE_DIGITAL_CMD) // 写数字量指令
{ {
/*写操作,将数据写入寄存器,组包返回*/ /*写操作,将数据写入寄存器,组包返回*/
user_communication_do = &communication_data.do_data; user_communication_do = &communication_data.do_data;
@ -336,3 +337,9 @@ void user_send_data_ble2(uint8_t *data, uint16_t len)
tcp_write(server_pcb_ble2, data, len, 1); tcp_write(server_pcb_ble2, data, len, 1);
} }
void user_send_data_control(uint8_t *data, uint16_t len)
{
tcp_write(server_pcb_control, data, len, 1);
}