This commit is contained in:
王绪洁 2025-03-26 14:02:40 +08:00
parent 6fc09e9371
commit 4742fd3962
14 changed files with 4407 additions and 7887 deletions

View File

@ -7,6 +7,7 @@
"fsmc.h": "c",
"ch438q.h": "c",
"ht1200m.h": "c",
"main.h": "c"
"main.h": "c",
"etharp.h": "c"
}
}

View File

@ -27,6 +27,7 @@
/* USER CODE BEGIN Includes */
#include "dac161s997.h"
#include "ch438q.h"
#include "tcpserverc.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@ -113,11 +114,11 @@ void MX_FREERTOS_Init(void)
lwip_taskHandle = osThreadCreate(osThread(lwip_task), NULL);
/* definition and creation of dac_task */
osThreadDef(dac_task, start_dac_task, osPriorityIdle, 0, 512);
osThreadDef(dac_task, start_dac_task, osPriorityNormal, 0, 512);
dac_taskHandle = osThreadCreate(osThread(dac_task), NULL);
/* definition and creation of ch438_task */
osThreadDef(ch438_task, start_ch438_task, osPriorityHigh, 0, 512);
osThreadDef(ch438_task, start_ch438_task, osPriorityNormal, 0, 512);
ch438_taskHandle = osThreadCreate(osThread(ch438_task), NULL);
/* USER CODE BEGIN RTOS_THREADS */
@ -138,9 +139,10 @@ void start_lwip_task(void const *argument)
MX_LWIP_Init();
/* USER CODE BEGIN start_lwip_task */
/* Infinite loop */
tcp_echo_init();
for (;;)
{
osThreadTerminate(NULL);
// osThreadTerminate(NULL);
}
/* USER CODE END start_lwip_task */
}
@ -160,7 +162,7 @@ void start_dac_task(void const *argument)
for (;;)
{
uint8_t ch = 0;
for (; ch < DAC_MAX_NUM; ch++)
for (ch = 0; ch < DAC_MAX_NUM; ch++)
{
dac161s997_output(ch, 12.0f);
}
@ -183,8 +185,8 @@ void start_ch438_task(void const *argument)
ch438_init();
for (;;)
{
uint8_t data[9] = {0xFF, 0xff, 0xff, 0xff, 0xff, 0x34, 0x56, 0x78, 0x9a};
ch438_send_data(CH438_UART2, data, 9);
// uint8_t data[9] = {0xFF, 0xff, 0xff, 0xff, 0xff, 0x34, 0x56, 0x78, 0x9a};
// ch438_send_data(CH438_UART0, data, 9);
vTaskDelay(500);
}
/* USER CODE END start_ch438_task */

View File

@ -65,9 +65,9 @@ void MX_FREERTOS_Init(void);
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
@ -118,22 +118,22 @@ int main(void)
}
/**
* @brief System Clock Configuration
* @retval None
*/
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
@ -148,8 +148,9 @@ void SystemClock_Config(void)
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
@ -173,20 +174,19 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
/* USER CODE END 4 */
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM4 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM4 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM4)
{
if (htim->Instance == TIM4) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
@ -195,9 +195,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
@ -209,14 +209,14 @@ void Error_Handler(void)
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */

View File

@ -61,7 +61,7 @@ void MX_LWIP_Init(void)
IP_ADDRESS[0] = 192;
IP_ADDRESS[1] = 168;
IP_ADDRESS[2] = 1;
IP_ADDRESS[3] = 200;
IP_ADDRESS[3] = 29;
NETMASK_ADDRESS[0] = 255;
NETMASK_ADDRESS[1] = 255;
NETMASK_ADDRESS[2] = 255;

View File

@ -1,48 +0,0 @@
// File: STM32F405_415_407_417_427_437_429_439.dbgconf
// Version: 1.0.0
// Note: refer to STM32F405/415 STM32F407/417 STM32F427/437 STM32F429/439 reference manual (RM0090)
// refer to STM32F40x STM32F41x datasheets
// refer to STM32F42x STM32F43x datasheets
// <<< Use Configuration Wizard in Context Menu >>>
// <h> Debug MCU configuration register (DBGMCU_CR)
// <o.2> DBG_STANDBY <i> Debug Standby Mode
// <o.1> DBG_STOP <i> Debug Stop Mode
// <o.0> DBG_SLEEP <i> Debug Sleep Mode
// </h>
DbgMCU_CR = 0x00000007;
// <h> Debug MCU APB1 freeze register (DBGMCU_APB1_FZ)
// <i> Reserved bits must be kept at reset value
// <o.26> DBG_CAN2_STOP <i> CAN2 stopped when core is halted
// <o.25> DBG_CAN1_STOP <i> CAN2 stopped when core is halted
// <o.23> DBG_I2C3_SMBUS_TIMEOUT <i> I2C3 SMBUS timeout mode stopped when core is halted
// <o.22> DBG_I2C2_SMBUS_TIMEOUT <i> I2C2 SMBUS timeout mode stopped when core is halted
// <o.21> DBG_I2C1_SMBUS_TIMEOUT <i> I2C1 SMBUS timeout mode stopped when core is halted
// <o.12> DBG_IWDG_STOP <i> Independent watchdog stopped when core is halted
// <o.11> DBG_WWDG_STOP <i> Window watchdog stopped when core is halted
// <o.10> DBG_RTC_STOP <i> RTC stopped when core is halted
// <o.8> DBG_TIM14_STOP <i> TIM14 counter stopped when core is halted
// <o.7> DBG_TIM13_STOP <i> TIM13 counter stopped when core is halted
// <o.6> DBG_TIM12_STOP <i> TIM12 counter stopped when core is halted
// <o.5> DBG_TIM7_STOP <i> TIM7 counter stopped when core is halted
// <o.4> DBG_TIM6_STOP <i> TIM6 counter stopped when core is halted
// <o.3> DBG_TIM5_STOP <i> TIM5 counter stopped when core is halted
// <o.2> DBG_TIM4_STOP <i> TIM4 counter stopped when core is halted
// <o.1> DBG_TIM3_STOP <i> TIM3 counter stopped when core is halted
// <o.0> DBG_TIM2_STOP <i> TIM2 counter stopped when core is halted
// </h>
DbgMCU_APB1_Fz = 0x00000000;
// <h> Debug MCU APB2 freeze register (DBGMCU_APB2_FZ)
// <i> Reserved bits must be kept at reset value
// <o.18> DBG_TIM11_STOP <i> TIM11 counter stopped when core is halted
// <o.17> DBG_TIM10_STOP <i> TIM10 counter stopped when core is halted
// <o.16> DBG_TIM9_STOP <i> TIM9 counter stopped when core is halted
// <o.1> DBG_TIM8_STOP <i> TIM8 counter stopped when core is halted
// <o.0> DBG_TIM1_STOP <i> TIM1 counter stopped when core is halted
// </h>
DbgMCU_APB2_Fz = 0x00000000;
// <<< end of configuration section >>>

File diff suppressed because one or more lines are too long

View File

@ -165,12 +165,17 @@
<WinNumber>1</WinNumber>
<ItemText>ch438_uart_data,0x0A</ItemText>
</Ww>
<Ww>
<count>3</count>
<WinNumber>1</WinNumber>
<ItemText>gnetif</ItemText>
</Ww>
</WatchWindow1>
<MemoryWindow1>
<Mm>
<WinNumber>1</WinNumber>
<SubType>0</SubType>
<ItemText>pSrcBuffer</ItemText>
<ItemText>0x60000000</ItemText>
<AccSizeX>0</AccSizeX>
</Mm>
</MemoryWindow1>
@ -1858,6 +1863,26 @@
</File>
</Group>
<Group>
<GroupName>User/application</GroupName>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>
<File>
<GroupNumber>12</GroupNumber>
<FileNumber>129</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\User\application\src\tcpserverc.c</PathWithFileName>
<FilenameWithoutPath>tcpserverc.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>
<GroupName>::CMSIS</GroupName>
<tvExp>0</tvExp>

View File

@ -339,7 +339,7 @@
<MiscControls></MiscControls>
<Define>USE_HAL_DRIVER,STM32F407xx</Define>
<Undefine></Undefine>
<IncludePath>../Core/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Drivers/CMSIS/Include;../LWIP/App;../LWIP/Target;../Middlewares/Third_Party/LwIP/src/include;../Middlewares/Third_Party/LwIP/system;../Middlewares/Third_Party/FreeRTOS/Source/include;../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS;../Middlewares/Third_Party/FreeRTOS/Source/portable/RVDS/ARM_CM4F;../Drivers/BSP/Components/lan8742;../Middlewares/Third_Party/LwIP/src/include/netif/ppp;../Middlewares/Third_Party/LwIP/src/include/lwip;../Middlewares/Third_Party/LwIP/src/include/lwip/apps;../Middlewares/Third_Party/LwIP/src/include/lwip/priv;../Middlewares/Third_Party/LwIP/src/include/lwip/prot;../Middlewares/Third_Party/LwIP/src/include/netif;../Middlewares/Third_Party/LwIP/src/include/compat/posix;../Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa;../Middlewares/Third_Party/LwIP/src/include/compat/posix/net;../Middlewares/Third_Party/LwIP/src/include/compat/posix/sys;../Middlewares/Third_Party/LwIP/src/include/compat/stdc;../Middlewares/Third_Party/LwIP/system/arch;../User/driver;../User/system</IncludePath>
<IncludePath>../Core/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Drivers/CMSIS/Include;../LWIP/App;../LWIP/Target;../Middlewares/Third_Party/LwIP/src/include;../Middlewares/Third_Party/LwIP/system;../Middlewares/Third_Party/FreeRTOS/Source/include;../Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS;../Middlewares/Third_Party/FreeRTOS/Source/portable/RVDS/ARM_CM4F;../Drivers/BSP/Components/lan8742;../Middlewares/Third_Party/LwIP/src/include/netif/ppp;../Middlewares/Third_Party/LwIP/src/include/lwip;../Middlewares/Third_Party/LwIP/src/include/lwip/apps;../Middlewares/Third_Party/LwIP/src/include/lwip/priv;../Middlewares/Third_Party/LwIP/src/include/lwip/prot;../Middlewares/Third_Party/LwIP/src/include/netif;../Middlewares/Third_Party/LwIP/src/include/compat/posix;../Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa;../Middlewares/Third_Party/LwIP/src/include/compat/posix/net;../Middlewares/Third_Party/LwIP/src/include/compat/posix/sys;../Middlewares/Third_Party/LwIP/src/include/compat/stdc;../Middlewares/Third_Party/LwIP/system/arch;../User/driver;../User/system;..\User\application\inc</IncludePath>
</VariousControls>
</Cads>
<Aads>
@ -6622,6 +6622,16 @@
</File>
</Files>
</Group>
<Group>
<GroupName>User/application</GroupName>
<Files>
<File>
<FileName>tcpserverc.c</FileName>
<FileType>1</FileType>
<FilePath>..\User\application\src\tcpserverc.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>::CMSIS</GroupName>
</Group>

View File

@ -1,2 +0,0 @@
[EXTDLL]
Count=0

File diff suppressed because it is too large Load Diff

View File

@ -3,16 +3,28 @@
#include "main.h"
#define TCP_PORT_HART1 5001
#define TCP_PORT_HART2 5002
#define TCP_PORT_BLE1 6001
#define TCP_PORT_BLE2 6002
#define TCP_PORT_HART1 1001
#define TCP_PORT_HART9 2002
#define TCP_PORT_CONTROL 5003
extern uint8_t tcp_echo_flags_hart1;
extern uint8_t tcp_echo_flags_hart9;
typedef struct
{
uint16_t rx_num;
uint8_t rx_data[512];
uint8_t rx_data_temp[512];
uint8_t tx_data[512];
} uart_t;
extern uart_t hart1_uart;
extern uart_t hart9_uart;
extern void tcp_echo_init(void);
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_ble1(uint8_t *data, uint16_t len);
extern void user_send_data_ble2(uint8_t *data, uint16_t len);
extern void user_send_data_hart9(uint8_t *data, uint16_t len);
extern void user_send_data_control(uint8_t *data, uint16_t len);
#endif

View File

@ -8,17 +8,16 @@
#include "lwip/pbuf.h"
#include <stdio.h>
#include <string.h>
#include "usart.h"
#include "main.h"
#include "ht1200m.h"
#include "user_lib.h"
#include "communication_protocol.h"
#include "user_gpio.h"
#include "ch438q.h"
struct tcp_pcb *server_pcb_hart1 = NULL;
struct tcp_pcb *server_pcb_hart2 = NULL;
struct tcp_pcb *server_pcb_ble1 = NULL;
struct tcp_pcb *server_pcb_ble2 = NULL;
struct tcp_pcb *server_pcb_hart9 = NULL;
struct tcp_pcb *server_pcb_control = NULL;
communication_di_t *user_communication_di = NULL;
@ -26,10 +25,12 @@ communication_do_t *user_communication_do = NULL;
communication_ai_t *user_communication_ai = NULL;
communication_ao_t *user_communication_ao = NULL;
extern uint8_t tcp_echo_flags_hart1;
extern uint8_t tcp_echo_flags_hart2;
extern uint8_t tcp_echo_flags_ble1;
extern uint8_t tcp_echo_flags_ble2;
uint8_t tcp_echo_flags_hart1 = 0;
uint8_t tcp_echo_flags_hart9 = 0;
uart_t hart9_uart = {0};
uart_t hart1_uart = {0};
extern uint8_t tcp_echo_flags_control;
extern uint8_t send_data_flag_cmd;
@ -42,12 +43,9 @@ static err_t tcpecho_recv_hart1(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,
tcp_echo_flags_hart1 = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
server_pcb_hart1 = tpcb; // 直接赋值
memcpy(hart1_uart5.tx_data, (int *)p->payload, p->tot_len);
if (huart5.gState == HAL_UART_STATE_READY)
{
HART1_RTS_SEND;
dma_usart_send(&huart5, hart1_uart5.tx_data, p->tot_len);
}
memcpy(hart1_uart.tx_data, (int *)p->payload, p->tot_len);
ch438_send_data(0, hart1_uart.tx_data, p->tot_len);
pbuf_free(p);
}
else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
@ -57,21 +55,16 @@ static err_t tcpecho_recv_hart1(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,
return ERR_OK;
}
static err_t tcpecho_recv_hart2(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
static err_t tcpecho_recv_hart9(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{ // 对应接收数据连接的控制块 接收到的数据
if (p != NULL)
{
/* 更新窗口*/
tcp_echo_flags_hart2 = 1;
tcp_echo_flags_hart9 = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
server_pcb_hart2 = tpcb; // 直接赋值
memcpy(hart2_uart2.tx_data, (int *)p->payload, p->tot_len);
if (huart2.gState == HAL_UART_STATE_READY)
{
HART2_RTS_SEND;
dma_usart_send(&huart2, hart2_uart2.tx_data, p->tot_len);
}
server_pcb_hart9 = tpcb; // 直接赋值
memcpy(hart9_uart.tx_data, (int *)p->payload, p->tot_len);
ch438_send_data(8, hart9_uart.tx_data, p->tot_len);
pbuf_free(p);
}
else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
@ -81,145 +74,97 @@ static err_t tcpecho_recv_hart2(void *arg, struct tcp_pcb *tpcb, struct pbuf *p,
return ERR_OK;
}
static err_t tcpecho_recv_ble1(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{ // 对应接收数据连接的控制块 接收到的数据
if (p != NULL)
{
/* 更新窗口*/
tcp_echo_flags_ble1 = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
server_pcb_ble1 = tpcb; // 直接赋值
// 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)
// {
memcpy(ble1_uart6.tx_data, (int *)p->payload, p->tot_len);
if (huart6.gState == HAL_UART_STATE_READY)
{
dma_usart_send(&huart6, ble1_uart6.tx_data, p->tot_len);
}
// /* 更新窗口*/
// tcp_echo_flags_control = 1;
// 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] == READ_ANALOG_CMD) // 读模拟量指令
// {
// /*读操作,从寄存器读取数据,组包返回*/
// tx_data_len = COM_AI_DATA_SIZE;
// user_communication_ai = &communication_data.ai_data;
// communication_get_ai(user_communication_ai, tcp_tx_data, tcp_rx_data);
// tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
// }
// else if (tcp_rx_data[3] == WRITE_ANALOG_CMD) // 写模拟量指令
// {
// /*写操作,将数据写入寄存器,组包返回*/
pbuf_free(p);
}
else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
{
return tcp_close(tpcb);
}
return ERR_OK;
}
// user_communication_ao = &communication_data.ao_data;
// communication_set_ao(user_communication_ao);
// tcp_write(tpcb, tcp_rx_data, rx_data_len, 1);
// }
// else if (tcp_rx_data[3] == READ_DIGITAL_CMD) // 读数字量指令
// {
// /*读操作,从寄存器读取数据,组包返回*/
static err_t tcpecho_recv_ble2(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{ // 对应接收数据连接的控制块 接收到的数据
if (p != NULL)
{
/* 更新窗口*/
tcp_echo_flags_ble2 = 1;
tcp_recved(tpcb, p->tot_len); // 读取数据的控制块 得到所有数据的长度
server_pcb_ble2 = tpcb; // 直接赋值
memcpy(ble2_uart3.tx_data, (int *)p->payload, p->tot_len);
if (huart3.gState == HAL_UART_STATE_READY)
{
dma_usart_send(&huart3, ble2_uart3.tx_data, p->tot_len);
}
pbuf_free(p);
}
else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
{
return tcp_close(tpcb);
}
return ERR_OK;
}
// user_communication_di = &communication_data.di_data;
// tx_data_len = 7 + user_communication_di->num;
// user_read_gpio(user_communication_di, tcp_tx_data, tcp_rx_data);
// tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
// }
// else if (tcp_rx_data[3] == WRITE_DIGITAL_CMD) // 写数字量指令
// {
// /*写操作,将数据写入寄存器,组包返回*/
// user_communication_do = &communication_data.do_data;
// user_write_gpio(user_communication_do);
// tcp_write(tpcb, tcp_rx_data, rx_data_len, 1);
// }
// else if (tcp_rx_data[3] == SEND_STATE_CMD)
// {
// send_data_flag_cmd = 0; // 上位机返回数据,发送状态标志位清零
// }
// 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);
// }
// }
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)
{
HAL_GPIO_TogglePin(LED3_G_GPIO_Port, LED3_G_Pin);
/* 更新窗口*/
tcp_echo_flags_control = 1;
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] == READ_ANALOG_CMD) // 读模拟量指令
{
/*读操作,从寄存器读取数据,组包返回*/
tx_data_len = COM_AI_DATA_SIZE;
user_communication_ai = &communication_data.ai_data;
communication_get_ai(user_communication_ai, tcp_tx_data, tcp_rx_data);
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
}
else if (tcp_rx_data[3] == WRITE_ANALOG_CMD) // 写模拟量指令
{
/*写操作,将数据写入寄存器,组包返回*/
// /*2. 判断所要执行的操作 读或写指令*/
// /*3. 对要发送的数据进行校验,组包,返回数据*/
// }
// pbuf_free(p);
// }
// else if (err == ERR_OK) // 检测到对方主动关闭连接时也会调用recv函数此时p为空
// {
// return tcp_close(tpcb);
// }
user_communication_ao = &communication_data.ao_data;
communication_set_ao(user_communication_ao);
tcp_write(tpcb, tcp_rx_data, rx_data_len, 1);
}
else if (tcp_rx_data[3] == READ_DIGITAL_CMD) // 读数字量指令
{
/*读操作,从寄存器读取数据,组包返回*/
user_communication_di = &communication_data.di_data;
tx_data_len = 7 + user_communication_di->num;
user_read_gpio(user_communication_di, tcp_tx_data, tcp_rx_data);
tcp_write(tpcb, tcp_tx_data, tx_data_len, 1);
}
else if (tcp_rx_data[3] == WRITE_DIGITAL_CMD) // 写数字量指令
{
/*写操作,将数据写入寄存器,组包返回*/
user_communication_do = &communication_data.do_data;
user_write_gpio(user_communication_do);
tcp_write(tpcb, tcp_rx_data, rx_data_len, 1);
}
else if (tcp_rx_data[3] == SEND_STATE_CMD)
{
send_data_flag_cmd = 0; // 上位机返回数据,发送状态标志位清零
}
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;
}
// return ERR_OK;
// }
static err_t tcpecho_accept_hart1(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
// 形参的数量和类型必须一致
{
@ -228,38 +173,26 @@ static err_t tcpecho_accept_hart1(void *arg, struct tcp_pcb *newpcb, err_t err)
return ERR_OK;
}
static err_t tcpecho_accept_hart2(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
static err_t tcpecho_accept_hart9(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
{
tcp_recv(newpcb, tcpecho_recv_hart2); // 当收到数据时回调用户自己写的tcpecho_recv
tcp_recv(newpcb, tcpecho_recv_hart9); // 当收到数据时回调用户自己写的tcpecho_recv
return ERR_OK;
}
static err_t tcpecho_accept_ble1(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
{
tcp_recv(newpcb, tcpecho_recv_ble1); // 当收到数据时回调用户自己写的tcpecho_recv
// 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;
}
static err_t tcpecho_accept_ble2(void *arg, struct tcp_pcb *newpcb, err_t err) // 由于这个函数是*tcp_accept_fn类型的
{
tcp_recv(newpcb, tcpecho_recv_ble2); // 当收到数据时回调用户自己写的tcpecho_recv
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;
}
// return ERR_OK;
// }
void tcp_echo_init(void)
{
struct tcp_pcb *server_hart1 = NULL;
struct tcp_pcb *server_hart2 = NULL;
struct tcp_pcb *server_ble1 = NULL;
struct tcp_pcb *server_ble2 = NULL;
struct tcp_pcb *server_control = NULL;
struct tcp_pcb *server_hart9 = NULL;
// struct tcp_pcb *server_ble1 = NULL;
// struct tcp_pcb *server_ble2 = NULL;
// struct tcp_pcb *server_control = NULL;
/* 创建一路HART */
server_hart1 = tcp_new();
@ -274,52 +207,28 @@ void tcp_echo_init(void)
tcp_accept(server_hart1, tcpecho_accept_hart1); // 侦听到连接后回调用户编写的tcpecho_accept
/* 创建二路HART */
server_hart2 = tcp_new();
server_hart9 = tcp_new();
/* 绑定TCP控制块 */
tcp_bind(server_hart2, IP_ADDR_ANY, TCP_PORT_HART2);
tcp_bind(server_hart9, IP_ADDR_ANY, TCP_PORT_HART9);
/* 进入监听状态 */
server_hart2 = tcp_listen(server_hart2);
server_hart9 = tcp_listen(server_hart9);
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
tcp_accept(server_hart2, tcpecho_accept_hart2); // 侦听到连接后回调用户编写的tcpecho_accept
tcp_accept(server_hart9, tcpecho_accept_hart9); // 侦听到连接后回调用户编写的tcpecho_accept
/* 创建一路蓝牙 */
server_ble1 = tcp_new();
// /* 创建控制块 */
// server_control = tcp_new();
/* 绑定TCP控制块 */
tcp_bind(server_ble1, IP_ADDR_ANY, TCP_PORT_BLE1);
// /* 绑定TCP控制块 */
// tcp_bind(server_control, IP_ADDR_ANY, TCP_PORT_CONTROL);
/* 进入监听状态 */
server_ble1 = tcp_listen(server_ble1);
// /* 进入监听状态 */
// server_control = tcp_listen(server_control);
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
tcp_accept(server_ble1, tcpecho_accept_ble1); // 侦听到连接后回调用户编写的tcpecho_accept
/* 创建二路蓝牙 */
server_ble2 = tcp_new();
/* 绑定TCP控制块 */
tcp_bind(server_ble2, IP_ADDR_ANY, TCP_PORT_BLE2);
/* 进入监听状态 */
server_ble2 = tcp_listen(server_ble2);
/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
tcp_accept(server_ble2, tcpecho_accept_ble2); // 侦听到连接后回调用户编写的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
// /* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */
// tcp_accept(server_control, tcpecho_accept_control); // 侦听到连接后回调用户编写的tcpecho_accept
}
void user_send_data_hart1(uint8_t *data, uint16_t len)
@ -327,25 +236,14 @@ void user_send_data_hart1(uint8_t *data, uint16_t len)
tcp_write(server_pcb_hart1, data, len, 1);
}
void user_send_data_hart2(uint8_t *data, uint16_t len)
void user_send_data_hart9(uint8_t *data, uint16_t len)
{
tcp_write(server_pcb_hart2, data, len, 1);
tcp_write(server_pcb_hart9, data, len, 1);
}
void user_send_data_ble1(uint8_t *data, uint16_t len)
{
// void user_send_data_control(uint8_t *data, uint16_t len)
// {
tcp_write(server_pcb_ble1, data, len, 1);
}
void user_send_data_ble2(uint8_t *data, uint16_t len)
{
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);
}
// tcp_write(server_pcb_control, data, len, 1);
// }

View File

@ -1,6 +1,7 @@
#include "ch438q.h"
#include "fsmc.h"
#include "ht1200m.h"
#include "tcpserverc.h"
#define CH438_CLK 1843200 /* CH438的内部时钟频率默认外部晶振的12分频 */
const uint8_t offsetadd[] = {
@ -340,11 +341,18 @@ void ch438_interrupt_handler_ch438_chip_1(void)
break;
case INT_RCV_OVERTIME: /* 接收超时中断 */
ch438_uart_data[i].receive_data_length = ch438_recv_data(i, ch438_uart_data[i].receive_data_buff);
ch438_send_data(i, ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
// ch438_send_data(i, ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
if (tcp_echo_flags_hart1 == 1)
{
user_send_data_hart1(ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
}
break;
case INT_RCV_SUCCESS: /* 接收数据可用中断 */
ch438_uart_data[i].receive_data_length = ch438_recv_data(i, ch438_uart_data[i].receive_data_buff);
ch438_send_data(i, ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
if (tcp_echo_flags_hart1 == 1)
{
user_send_data_hart1(ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
}
break;
case INT_RCV_LINES: /* 接收线路状态中断 */
ch438_read_reg(ch438_num, offsetadd[i] | REG_LSR_ADDR, 1);
@ -387,11 +395,17 @@ void ch438_interrupt_handler_ch438_chip_2(void)
break;
case INT_RCV_OVERTIME: /* 接收超时中断 */
ch438_uart_data[i].receive_data_length = ch438_recv_data(i, ch438_uart_data[i].receive_data_buff);
ch438_send_data(i, ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
if (tcp_echo_flags_hart9 == 1)
{
user_send_data_hart9(ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
}
break;
case INT_RCV_SUCCESS: /* 接收数据可用中断 */
ch438_uart_data[i].receive_data_length = ch438_recv_data(i, ch438_uart_data[i].receive_data_buff);
ch438_send_data(i, ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
if (tcp_echo_flags_hart9 == 1)
{
user_send_data_hart9(ch438_uart_data[i].receive_data_buff, ch438_uart_data[i].receive_data_length);
}
break;
case INT_RCV_LINES: /* 接收线路状态中断 */
ch438_read_reg(ch438_num, offsetadd[i - 8] | REG_LSR_ADDR, 1);
@ -416,5 +430,6 @@ void ch438_init(void)
{
ch438_init_uart(i, 1200);
ch438_init_config(i);
hart_ht1200m_rts_io_receive(i);
}
}

View File

@ -9,7 +9,7 @@ ETH.RxBuffLen=1536
ETH.RxMode=ETH_RXINTERRUPT_MODE
FREERTOS.FootprintOK=true
FREERTOS.IPParameters=Tasks01,configENABLE_FPU,configMAX_PRIORITIES,configMAX_TASK_NAME_LEN,FootprintOK
FREERTOS.Tasks01=lwip_task,0,512,start_lwip_task,Default,NULL,Dynamic,NULL,NULL;dac_task,-3,512,start_dac_task,Default,NULL,Dynamic,NULL,NULL;ch438_task,2,512,start_ch438_task,Default,NULL,Dynamic,NULL,NULL
FREERTOS.Tasks01=lwip_task,0,512,start_lwip_task,Default,NULL,Dynamic,NULL,NULL;dac_task,0,512,start_dac_task,Default,NULL,Dynamic,NULL,NULL;ch438_task,0,512,start_ch438_task,Default,NULL,Dynamic,NULL,NULL
FREERTOS.configENABLE_FPU=1
FREERTOS.configMAX_PRIORITIES=32
FREERTOS.configMAX_TASK_NAME_LEN=24
@ -26,7 +26,7 @@ KeepUserPlacement=false
LWIP.BSP.number=1
LWIP.GATEWAY_ADDRESS=192.168.001.222
LWIP.IPParameters=LWIP_DHCP,IP_ADDRESS,NETMASK_ADDRESS,GATEWAY_ADDRESS
LWIP.IP_ADDRESS=192.168.001.200
LWIP.IP_ADDRESS=192.168.001.029
LWIP.LWIP_DHCP=0
LWIP.NETMASK_ADDRESS=255.255.255.000
LWIP.Version=v2.1.2_Cube