From 6c74c2e09585158418f169fb253e24c4388c26b9 Mon Sep 17 00:00:00 2001 From: murmur Date: Thu, 19 Dec 2024 22:59:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8uart1=E4=B8=BAhost=EF=BC=8C?= =?UTF-8?q?=E6=94=B6=E5=8F=91=E5=88=9D=E6=AD=A5=E6=AD=A3=E5=B8=B8=E3=80=82?= =?UTF-8?q?uart3=E6=97=A0=E6=B3=95=E9=80=9A=E4=BF=A1=EF=BC=8C=E5=BE=85?= =?UTF-8?q?=E6=8E=92=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/http_server.h | 1 + Core/Inc/main.h | 6 +++-- Core/Src/gpio.c | 43 +++++++++++++++++++------------- Core/Src/http_server.c | 54 +---------------------------------------- Core/Src/main.c | 26 +++++++++++--------- Core/Src/stm32f1xx_it.c | 26 ++++++++++++++++---- Core/motor | 2 +- f1CTL.code-workspace | 3 ++- f1CTL.ioc | 45 +++++++++++++++++++--------------- 9 files changed, 96 insertions(+), 110 deletions(-) diff --git a/Core/Inc/http_server.h b/Core/Inc/http_server.h index 94d3791..9a1b255 100644 --- a/Core/Inc/http_server.h +++ b/Core/Inc/http_server.h @@ -10,6 +10,7 @@ extern "C" { #include #include #include +#include "protocol.h" #define HTTP_SERVER_PORT 80 #define HTTP_SERVER_SOCKET 1 diff --git a/Core/Inc/main.h b/Core/Inc/main.h index cb81e8d..643006b 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -63,16 +63,18 @@ void Error_Handler(void); /* Private defines -----------------------------------------------------------*/ #define DIR2_Pin GPIO_PIN_1 #define DIR2_GPIO_Port GPIOA +#define DIR1_Pin GPIO_PIN_2 +#define DIR1_GPIO_Port GPIOB #define LED_Pin GPIO_PIN_15 #define LED_GPIO_Port GPIOE +#define HOST_485EN_Pin GPIO_PIN_12 +#define HOST_485EN_GPIO_Port GPIOB #define NET_INT_Pin GPIO_PIN_8 #define NET_INT_GPIO_Port GPIOD #define NET_RST_Pin GPIO_PIN_9 #define NET_RST_GPIO_Port GPIOD #define NET_CS_Pin GPIO_PIN_10 #define NET_CS_GPIO_Port GPIOD -#define HOST_485EN_Pin GPIO_PIN_12 -#define HOST_485EN_GPIO_Port GPIOD /* USER CODE BEGIN Private defines */ diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index b421f07..f172ffc 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -56,15 +56,15 @@ void MX_GPIO_Init(void) /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(DIR2_GPIO_Port, DIR2_Pin, GPIO_PIN_RESET); + /*Configure GPIO pin Output Level */ + HAL_GPIO_WritePin(GPIOB, DIR1_Pin|HOST_485EN_Pin, GPIO_PIN_RESET); + /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOD, NET_RST_Pin|NET_CS_Pin, GPIO_PIN_SET); - /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(HOST_485EN_GPIO_Port, HOST_485EN_Pin, GPIO_PIN_RESET); - /*Configure GPIO pins : PE2 PE3 PE4 PE5 PE6 PE7 PE8 PE9 PE10 PE11 PE12 PE13 @@ -103,15 +103,22 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(DIR2_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pins : PB0 PB1 PB2 PB12 - PB3 PB4 PB5 PB6 - PB7 PB8 PB9 */ - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_12 - |GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 - |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9; + /*Configure GPIO pins : PB0 PB1 PB3 PB4 + PB5 PB6 PB7 PB8 + PB9 */ + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_4 + |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8 + |GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pins : DIR1_Pin HOST_485EN_Pin */ + GPIO_InitStruct.Pin = DIR1_Pin|HOST_485EN_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /*Configure GPIO pin : LED_Pin */ GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; @@ -125,19 +132,21 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(NET_INT_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pins : NET_RST_Pin NET_CS_Pin HOST_485EN_Pin */ - GPIO_InitStruct.Pin = NET_RST_Pin|NET_CS_Pin|HOST_485EN_Pin; + /*Configure GPIO pins : NET_RST_Pin NET_CS_Pin */ + GPIO_InitStruct.Pin = NET_RST_Pin|NET_CS_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); - /*Configure GPIO pins : PD11 PD13 PD14 PD15 - PD0 PD1 PD2 PD3 - PD4 PD5 PD6 PD7 */ - GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15 - |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 - |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; + /*Configure GPIO pins : PD11 PD12 PD13 PD14 + PD15 PD0 PD1 PD2 + PD3 PD4 PD5 PD6 + PD7 */ + GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14 + |GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2 + |GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 + |GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); diff --git a/Core/Src/http_server.c b/Core/Src/http_server.c index 16d3206..1ac7530 100644 --- a/Core/Src/http_server.c +++ b/Core/Src/http_server.c @@ -143,58 +143,6 @@ void http_server_init(void) listen(HTTP_SERVER_SOCKET); } - - -// 三通阀结构体 -typedef struct { - uint8_t angle1; // 阀门1角度 (120/210) - uint8_t angle2; // 阀门2角度 (120/210) -} ValveStatus; - -// 泵结构体 -typedef struct { - uint8_t status1; // 泵1运行状态 (停止/顺时针/逆时针) - uint8_t status2; // 泵2运行状态 (停止/顺时针/逆时针) - uint8_t speed1; // 泵1速度百分比 (0-100) - uint8_t speed2; // 泵2速度百分比 (0-100) -} PumpStatus; - -// 设备状态结构体 -// 1.下挂设备在位状态,在位为1,否则为0,当前为气泡感应器,1个字节 -// 2.两个三通阀实时角度,每个角度占用1个字节,只能为210或120 -// 3.泵状态,1表示泵顺时针转动中,2表示泵逆时针转动中,0表示停止,两个泵共2个字节 -// 4.泵速度,2个字节,每个泵的速度占用一个字节,表示当前泵设定的速度百分比 -// 5.气泡感应器读值,1表示监测到气泡,0表示没有气泡,共1个字节 -// 6.急停按键状态,1个字节,1为按下,0为正常。 -// 7.错误码1个字节,用于监控当前系统总体状态,如通信异常、控制异常等��,具体错误码待定 -// 8.初始化状态,1个字节, 0表示初始化执行中,1表示初始化执行成功,2表示初始化执行失败。 -typedef struct { - uint8_t deviceStatus; // 下挂设备状态 - ValveStatus valves; // 两个三通阀状态 - PumpStatus pumps; // 两个泵状态 - uint8_t bubbleStatus; // 气泡状态 - uint8_t stopStatus; // 急停状态 - uint8_t errorCode; // 错误码 - uint8_t initStatus; // 初始化状态 -} DeviceStatus; -DeviceStatus deviceStatus={ - .deviceStatus=1, - .valves={ - .angle1=0, - .angle2=0 - }, - .pumps={ - .status1=0, - .status2=0, - .speed1=60, - .speed2=70 - }, - .bubbleStatus=0, - .stopStatus=0, - .errorCode=0, - .initStatus=1 -}; - // 发送JSON响应头 static void send_json_response_header(uint8_t sn) { @@ -286,7 +234,7 @@ static void handle_netconfig_request(uint8_t sn) void http_server_task(void) { - uint8_t buffer[2048]; + uint8_t buffer[256]; uint16_t size; uint8_t status = getSn_SR(HTTP_SERVER_SOCKET); diff --git a/Core/Src/main.c b/Core/Src/main.c index 51f3db7..5491a13 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -63,9 +63,11 @@ void SystemClock_Config(void); void transDataToHost(uint8_t *data, uint16_t len) { // 开启uart3 dma - HAL_GPIO_WritePin(HOST_485EN_GPIO_Port, HOST_485EN_Pin, GPIO_PIN_SET); - HAL_UART_Transmit(&huart3, data, len, 1000); - HAL_GPIO_WritePin(HOST_485EN_GPIO_Port, HOST_485EN_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(DIR1_GPIO_Port, DIR1_Pin, GPIO_PIN_SET); + // HAL_Delay(100); + HAL_UART_Transmit(&huart1, data, len, 1000); + HAL_GPIO_WritePin(DIR1_GPIO_Port, DIR1_Pin, GPIO_PIN_RESET); + // HAL_Delay(100); } /* USER CODE END PFP */ @@ -108,7 +110,13 @@ int main(void) MX_USART2_UART_Init(); MX_SPI2_Init(); MX_USART3_UART_Init(); + + /* USER CODE BEGIN 2 */ + memset(hostRxBuf, 0xff, HOST_BUFFER_SIZE); + // 开启uart1中断和dma,使用uart1为host + __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); + HAL_UART_Receive_DMA(&huart1, hostRxBuf, HOST_BUFFER_SIZE); if(W5500_Init() != 0) { while(1) { @@ -124,8 +132,7 @@ int main(void) // http_test_request(); HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); printf("Init success!\r\n"); - // transDataToHost("hello", 5); - // runPumpDemo(); + runPumpDemo(); /* USER CODE END 2 */ /* Infinite loop */ @@ -138,13 +145,11 @@ int main(void) http_server_task(); if (hostRxEnd == 1) { - hostRxEnd = 0; - transDataToHost(hostRxBuf, hostRxLen); + printf("hostRxLen: %d\r\n", hostRxLen); ProcessHostCommand(hostRxBuf, hostRxLen); + hostRxEnd = 0; + HAL_UART_Receive_DMA(&huart1, hostRxBuf, HOST_BUFFER_SIZE); } - // transDataToHost("hello\r\n", 7); - // HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); - // HAL_Delay(100); } /* USER CODE END 3 */ } @@ -189,7 +194,6 @@ void SystemClock_Config(void) } /* USER CODE BEGIN 4 */ - /* USER CODE END 4 */ /** diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index da21029..eee15a2 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -266,7 +266,17 @@ void DMA1_Channel5_IRQHandler(void) void USART1_IRQHandler(void) { /* USER CODE BEGIN USART1_IRQn 0 */ - + if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET) {// idle标志被置 + __HAL_UART_CLEAR_IDLEFLAG(&huart1); //清除标志 + HAL_UART_DMAStop(&huart1); + hostRxLen = HOST_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);//得到已经接收的数据个数 + // if(hostRxLen >10) + {//避免部分主机一次不能发送完整的帧致使的误触发空闲中断 + hostRxEnd = 1; //接受完成标志位置1 + // HAL_UART_DMAStop(&huart1); + // HAL_UART_Receive_DMA(&huart1, hostRxBuf, HOST_BUFFER_SIZE); + } + } /* USER CODE END USART1_IRQn 0 */ HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART1_IRQn 1 */ @@ -280,7 +290,8 @@ void USART1_IRQHandler(void) void USART2_IRQHandler(void) { /* USER CODE BEGIN USART2_IRQn 0 */ - + HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); + printf("HAL_UART2_IRQHandler\r\n"); /* USER CODE END USART2_IRQn 0 */ HAL_UART_IRQHandler(&huart2); /* USER CODE BEGIN USART2_IRQn 1 */ @@ -294,13 +305,18 @@ void USART2_IRQHandler(void) void USART3_IRQHandler(void) { /* USER CODE BEGIN USART3_IRQn 0 */ + // printf("USART3_IRQHandler\r\n"); + HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != RESET) {// idle标志被置 __HAL_UART_CLEAR_IDLEFLAG(&huart3); //清除标志 -// HAL_UART_DMAStop(&huart1); + HAL_UART_DMAStop(&huart3); hostRxLen = HOST_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);//得到已经接收的数据个数 - if(hostRxLen >10){//避免部分主机一次不能发送完整的帧致使的误触发空闲中断 + // printf("hostRxLen: %d\r\n", hostRxLen); + // if(hostRxLen >10) + {//避免部分主机一次不能发送完整的帧致使的误触发空闲中断 hostRxEnd = 1; //接受完成标志位置1 - HAL_UART_DMAStop(&huart3); + // HAL_UART_DMAStop(&huart3); + // HAL_UART_Receive_DMA(&huart3, hostRxBuf, HOST_BUFFER_SIZE); } } /* USER CODE END USART3_IRQn 0 */ diff --git a/Core/motor b/Core/motor index 2877853..e4ee1c0 160000 --- a/Core/motor +++ b/Core/motor @@ -1 +1 @@ -Subproject commit 2877853cf132b4a87ffaa2eacbb76f021343cb78 +Subproject commit e4ee1c0eecad3dcc04ef071132ef67d852a0c974 diff --git a/f1CTL.code-workspace b/f1CTL.code-workspace index 9a2aace..2b5a759 100644 --- a/f1CTL.code-workspace +++ b/f1CTL.code-workspace @@ -19,7 +19,8 @@ "*.cpp": "cpp", "*.cxx": "cpp", "*.cc": "cpp", - "*.html": "c" + "*.html": "c", + "cstdio": "c" }, "[yaml]": { "editor.insertSpaces": true, diff --git a/f1CTL.ioc b/f1CTL.ioc index efe3d2b..b4c0348 100644 --- a/f1CTL.ioc +++ b/f1CTL.ioc @@ -61,25 +61,26 @@ Mcu.Name=STM32F103V(C-D-E)Tx Mcu.Package=LQFP100 Mcu.Pin0=OSC_IN Mcu.Pin1=OSC_OUT -Mcu.Pin10=PB15 -Mcu.Pin11=PD8 -Mcu.Pin12=PD9 -Mcu.Pin13=PD10 -Mcu.Pin14=PD12 -Mcu.Pin15=PA9 -Mcu.Pin16=PA10 -Mcu.Pin17=PA13 -Mcu.Pin18=PA14 -Mcu.Pin19=VP_SYS_VS_Systick +Mcu.Pin10=PB13 +Mcu.Pin11=PB14 +Mcu.Pin12=PB15 +Mcu.Pin13=PD8 +Mcu.Pin14=PD9 +Mcu.Pin15=PD10 +Mcu.Pin16=PA9 +Mcu.Pin17=PA10 +Mcu.Pin18=PA13 +Mcu.Pin19=PA14 Mcu.Pin2=PA1 +Mcu.Pin20=VP_SYS_VS_Systick Mcu.Pin3=PA2 Mcu.Pin4=PA3 -Mcu.Pin5=PE15 -Mcu.Pin6=PB10 -Mcu.Pin7=PB11 -Mcu.Pin8=PB13 -Mcu.Pin9=PB14 -Mcu.PinsNb=20 +Mcu.Pin5=PB2 +Mcu.Pin6=PE15 +Mcu.Pin7=PB10 +Mcu.Pin8=PB11 +Mcu.Pin9=PB12 +Mcu.PinsNb=21 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F103VCTx @@ -127,21 +128,25 @@ PB10.Mode=Asynchronous PB10.Signal=USART3_TX PB11.Mode=Asynchronous PB11.Signal=USART3_RX +PB12.GPIOParameters=GPIO_Label +PB12.GPIO_Label=HOST_485EN +PB12.Locked=true +PB12.Signal=GPIO_Output PB13.Mode=Full_Duplex_Master PB13.Signal=SPI2_SCK PB14.Mode=Full_Duplex_Master PB14.Signal=SPI2_MISO PB15.Mode=Full_Duplex_Master PB15.Signal=SPI2_MOSI +PB2.GPIOParameters=GPIO_Label +PB2.GPIO_Label=DIR1 +PB2.Locked=true +PB2.Signal=GPIO_Output PD10.GPIOParameters=PinState,GPIO_Label PD10.GPIO_Label=NET_CS PD10.Locked=true PD10.PinState=GPIO_PIN_SET PD10.Signal=GPIO_Output -PD12.GPIOParameters=GPIO_Label -PD12.GPIO_Label=HOST_485EN -PD12.Locked=true -PD12.Signal=GPIO_Output PD8.GPIOParameters=GPIO_Label PD8.GPIO_Label=NET_INT PD8.Locked=true