使用uart1为host,收发初步正常。uart3无法通信,待排查

This commit is contained in:
murmur 2024-12-19 22:59:33 +08:00
parent 328adfb707
commit 6c74c2e095
9 changed files with 96 additions and 110 deletions

View File

@ -10,6 +10,7 @@ extern "C" {
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include "protocol.h"
#define HTTP_SERVER_PORT 80 #define HTTP_SERVER_PORT 80
#define HTTP_SERVER_SOCKET 1 #define HTTP_SERVER_SOCKET 1

View File

@ -63,16 +63,18 @@ void Error_Handler(void);
/* Private defines -----------------------------------------------------------*/ /* Private defines -----------------------------------------------------------*/
#define DIR2_Pin GPIO_PIN_1 #define DIR2_Pin GPIO_PIN_1
#define DIR2_GPIO_Port GPIOA #define DIR2_GPIO_Port GPIOA
#define DIR1_Pin GPIO_PIN_2
#define DIR1_GPIO_Port GPIOB
#define LED_Pin GPIO_PIN_15 #define LED_Pin GPIO_PIN_15
#define LED_GPIO_Port GPIOE #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_Pin GPIO_PIN_8
#define NET_INT_GPIO_Port GPIOD #define NET_INT_GPIO_Port GPIOD
#define NET_RST_Pin GPIO_PIN_9 #define NET_RST_Pin GPIO_PIN_9
#define NET_RST_GPIO_Port GPIOD #define NET_RST_GPIO_Port GPIOD
#define NET_CS_Pin GPIO_PIN_10 #define NET_CS_Pin GPIO_PIN_10
#define NET_CS_GPIO_Port GPIOD #define NET_CS_GPIO_Port GPIOD
#define HOST_485EN_Pin GPIO_PIN_12
#define HOST_485EN_GPIO_Port GPIOD
/* USER CODE BEGIN Private defines */ /* USER CODE BEGIN Private defines */

View File

@ -56,15 +56,15 @@ void MX_GPIO_Init(void)
/*Configure GPIO pin Output Level */ /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(DIR2_GPIO_Port, DIR2_Pin, GPIO_PIN_RESET); 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 */ /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */ /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, NET_RST_Pin|NET_CS_Pin, GPIO_PIN_SET); 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 /*Configure GPIO pins : PE2 PE3 PE4 PE5
PE6 PE7 PE8 PE9 PE6 PE7 PE8 PE9
PE10 PE11 PE12 PE13 PE10 PE11 PE12 PE13
@ -103,15 +103,22 @@ void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DIR2_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(DIR2_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 PB12 /*Configure GPIO pins : PB0 PB1 PB3 PB4
PB3 PB4 PB5 PB6 PB5 PB6 PB7 PB8
PB7 PB8 PB9 */ PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_12 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_4
|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 |GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8
|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9; |GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 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 */ /*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin; GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
@ -125,19 +132,21 @@ void MX_GPIO_Init(void)
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(NET_INT_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(NET_INT_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : 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|HOST_485EN_Pin; GPIO_InitStruct.Pin = NET_RST_Pin|NET_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : PD11 PD13 PD14 PD15 /*Configure GPIO pins : PD11 PD12 PD13 PD14
PD0 PD1 PD2 PD3 PD15 PD0 PD1 PD2
PD4 PD5 PD6 PD7 */ PD3 PD4 PD5 PD6
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15 PD7 */
|GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; |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; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

View File

@ -143,58 +143,6 @@ void http_server_init(void)
listen(HTTP_SERVER_SOCKET); 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个字节用于监控当前系统总体状态如通信异常、控制异常等<E5B8B8><E7AD89>具体错误码待定
// 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响应头 // 发送JSON响应头
static void send_json_response_header(uint8_t sn) 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) void http_server_task(void)
{ {
uint8_t buffer[2048]; uint8_t buffer[256];
uint16_t size; uint16_t size;
uint8_t status = getSn_SR(HTTP_SERVER_SOCKET); uint8_t status = getSn_SR(HTTP_SERVER_SOCKET);

View File

@ -63,9 +63,11 @@ void SystemClock_Config(void);
void transDataToHost(uint8_t *data, uint16_t len) void transDataToHost(uint8_t *data, uint16_t len)
{ {
// 开启uart3 dma // 开启uart3 dma
HAL_GPIO_WritePin(HOST_485EN_GPIO_Port, HOST_485EN_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(DIR1_GPIO_Port, DIR1_Pin, GPIO_PIN_SET);
HAL_UART_Transmit(&huart3, data, len, 1000); // HAL_Delay(100);
HAL_GPIO_WritePin(HOST_485EN_GPIO_Port, HOST_485EN_Pin, GPIO_PIN_RESET); 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 */ /* USER CODE END PFP */
@ -108,7 +110,13 @@ int main(void)
MX_USART2_UART_Init(); MX_USART2_UART_Init();
MX_SPI2_Init(); MX_SPI2_Init();
MX_USART3_UART_Init(); MX_USART3_UART_Init();
/* USER CODE BEGIN 2 */ /* 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) { if(W5500_Init() != 0) {
while(1) while(1)
{ {
@ -124,8 +132,7 @@ int main(void)
// http_test_request(); // http_test_request();
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
printf("Init success!\r\n"); printf("Init success!\r\n");
// transDataToHost("hello", 5); runPumpDemo();
// runPumpDemo();
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
@ -138,13 +145,11 @@ int main(void)
http_server_task(); http_server_task();
if (hostRxEnd == 1) { if (hostRxEnd == 1) {
hostRxEnd = 0; printf("hostRxLen: %d\r\n", hostRxLen);
transDataToHost(hostRxBuf, hostRxLen);
ProcessHostCommand(hostRxBuf, 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 */ /* USER CODE END 3 */
} }
@ -189,7 +194,6 @@ void SystemClock_Config(void)
} }
/* USER CODE BEGIN 4 */ /* USER CODE BEGIN 4 */
/* USER CODE END 4 */ /* USER CODE END 4 */
/** /**

View File

@ -266,7 +266,17 @@ void DMA1_Channel5_IRQHandler(void)
void USART1_IRQHandler(void) void USART1_IRQHandler(void)
{ {
/* USER CODE BEGIN USART1_IRQn 0 */ /* 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 */ /* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1); HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */ /* USER CODE BEGIN USART1_IRQn 1 */
@ -280,7 +290,8 @@ void USART1_IRQHandler(void)
void USART2_IRQHandler(void) void USART2_IRQHandler(void)
{ {
/* USER CODE BEGIN USART2_IRQn 0 */ /* 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 */ /* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2); HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */ /* USER CODE BEGIN USART2_IRQn 1 */
@ -294,13 +305,18 @@ void USART2_IRQHandler(void)
void USART3_IRQHandler(void) void USART3_IRQHandler(void)
{ {
/* USER CODE BEGIN USART3_IRQn 0 */ /* 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标志被置 if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != RESET) {// idle标志被置
__HAL_UART_CLEAR_IDLEFLAG(&huart3); //清除标志 __HAL_UART_CLEAR_IDLEFLAG(&huart3); //清除标志
// HAL_UART_DMAStop(&huart1); HAL_UART_DMAStop(&huart3);
hostRxLen = HOST_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);//得到已经接收的数据个数 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 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 */ /* USER CODE END USART3_IRQn 0 */

@ -1 +1 @@
Subproject commit 2877853cf132b4a87ffaa2eacbb76f021343cb78 Subproject commit e4ee1c0eecad3dcc04ef071132ef67d852a0c974

View File

@ -19,7 +19,8 @@
"*.cpp": "cpp", "*.cpp": "cpp",
"*.cxx": "cpp", "*.cxx": "cpp",
"*.cc": "cpp", "*.cc": "cpp",
"*.html": "c" "*.html": "c",
"cstdio": "c"
}, },
"[yaml]": { "[yaml]": {
"editor.insertSpaces": true, "editor.insertSpaces": true,

View File

@ -61,25 +61,26 @@ Mcu.Name=STM32F103V(C-D-E)Tx
Mcu.Package=LQFP100 Mcu.Package=LQFP100
Mcu.Pin0=OSC_IN Mcu.Pin0=OSC_IN
Mcu.Pin1=OSC_OUT Mcu.Pin1=OSC_OUT
Mcu.Pin10=PB15 Mcu.Pin10=PB13
Mcu.Pin11=PD8 Mcu.Pin11=PB14
Mcu.Pin12=PD9 Mcu.Pin12=PB15
Mcu.Pin13=PD10 Mcu.Pin13=PD8
Mcu.Pin14=PD12 Mcu.Pin14=PD9
Mcu.Pin15=PA9 Mcu.Pin15=PD10
Mcu.Pin16=PA10 Mcu.Pin16=PA9
Mcu.Pin17=PA13 Mcu.Pin17=PA10
Mcu.Pin18=PA14 Mcu.Pin18=PA13
Mcu.Pin19=VP_SYS_VS_Systick Mcu.Pin19=PA14
Mcu.Pin2=PA1 Mcu.Pin2=PA1
Mcu.Pin20=VP_SYS_VS_Systick
Mcu.Pin3=PA2 Mcu.Pin3=PA2
Mcu.Pin4=PA3 Mcu.Pin4=PA3
Mcu.Pin5=PE15 Mcu.Pin5=PB2
Mcu.Pin6=PB10 Mcu.Pin6=PE15
Mcu.Pin7=PB11 Mcu.Pin7=PB10
Mcu.Pin8=PB13 Mcu.Pin8=PB11
Mcu.Pin9=PB14 Mcu.Pin9=PB12
Mcu.PinsNb=20 Mcu.PinsNb=21
Mcu.ThirdPartyNb=0 Mcu.ThirdPartyNb=0
Mcu.UserConstants= Mcu.UserConstants=
Mcu.UserName=STM32F103VCTx Mcu.UserName=STM32F103VCTx
@ -127,21 +128,25 @@ PB10.Mode=Asynchronous
PB10.Signal=USART3_TX PB10.Signal=USART3_TX
PB11.Mode=Asynchronous PB11.Mode=Asynchronous
PB11.Signal=USART3_RX 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.Mode=Full_Duplex_Master
PB13.Signal=SPI2_SCK PB13.Signal=SPI2_SCK
PB14.Mode=Full_Duplex_Master PB14.Mode=Full_Duplex_Master
PB14.Signal=SPI2_MISO PB14.Signal=SPI2_MISO
PB15.Mode=Full_Duplex_Master PB15.Mode=Full_Duplex_Master
PB15.Signal=SPI2_MOSI 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.GPIOParameters=PinState,GPIO_Label
PD10.GPIO_Label=NET_CS PD10.GPIO_Label=NET_CS
PD10.Locked=true PD10.Locked=true
PD10.PinState=GPIO_PIN_SET PD10.PinState=GPIO_PIN_SET
PD10.Signal=GPIO_Output 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.GPIOParameters=GPIO_Label
PD8.GPIO_Label=NET_INT PD8.GPIO_Label=NET_INT
PD8.Locked=true PD8.Locked=true