From d92e4d08a786b019b5c3cbfc500945f519556572 Mon Sep 17 00:00:00 2001 From: 95384 <664090429@qq.com> Date: Tue, 14 Jan 2025 17:21:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=8E=E5=8A=9F=E8=80=97?= =?UTF-8?q?=E4=B8=B2=E5=8F=A3LPUART1=E9=A9=B1=E5=8A=A8=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=86=85=E9=83=A816M=E6=97=B6=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/Inc/uart_dev.h | 2 ++ App/Src/uart_dev.c | 25 +++++++++++++++++++++++++ Core/Inc/usart.h | 2 +- Core/Src/main.c | 7 +++++-- Core/Src/stm32l4xx_it.c | 19 +++++++++++++------ Core/Src/usart.c | 15 ++++++++------- 6 files changed, 54 insertions(+), 16 deletions(-) diff --git a/App/Inc/uart_dev.h b/App/Inc/uart_dev.h index 5169919..3947324 100644 --- a/App/Inc/uart_dev.h +++ b/App/Inc/uart_dev.h @@ -19,6 +19,7 @@ typedef u_int32_t device_handle; extern device_handle g_term_uart_handle; extern device_handle g_rs485_uart_handle; extern device_handle g_ec801_uart_handle; +extern device_handle g_rain_uart_handle; #define RS485_MAX_PACK_DATA_LEN 30 @@ -42,6 +43,7 @@ void uart_dev_write(device_handle device, void *data, int len); void init_term_uart(); void init_rs485_uart(); void init_ec801_uart(); +void init_rain_uart(); void uart_close(uartIndex_e uart_index); void term_printf(char *format, ...); int term_uart_readln(u_int8_t *buff, int buff_size, u_int32_t timeout_ms); diff --git a/App/Src/uart_dev.c b/App/Src/uart_dev.c index 65ef739..e2f7f6b 100644 --- a/App/Src/uart_dev.c +++ b/App/Src/uart_dev.c @@ -14,15 +14,18 @@ static u_int8_t uart_putchar(device_handle device, char ch); device_handle g_term_uart_handle; device_handle g_rs485_uart_handle; +device_handle g_rain_uart_handle; device_handle g_ec801_uart_handle; device_handle uart_dev_init(uartIndex_e uart_index, uint8_t *buff, int buff_size); static u_int8_t term_in_buff[200]; static u_int8_t ec801_in_buff[200]; static u_int8_t rs485_in_buff[300]; +static u_int8_t rain_in_buff[50]; u_int8_t rs485_out_buff[100]; u_int8_t ec801_out_buff[100]; +u_int8_t rain_out_buff[50]; static u_int8_t term_out_buff[100]; /** @@ -47,6 +50,11 @@ uart_device_info uart_devices[]={ .uart_index = EC801_UART_INDEX, .uart_baudrate = 115200, }, + [3] = { + .init = 0, + .uart_index = RAIN_RS485_UART_INDEX, + .uart_baudrate = 4800, + }, }; /** @@ -92,6 +100,10 @@ static void uart_init(uartIndex_e uart_index, int baud) MX_UART5_Init(baud); // MX_USART5_UART_Init(); } + else if(uart_index == RAIN_RS485_UART_INDEX){ + MX_LPUART1_UART_Init(baud); +// MX_LPUART1_UART_Init(); + } } /** @@ -107,6 +119,8 @@ void uart_close(uartIndex_e uart_index) HAL_UART_MspDeInit(&huart3); }else if(uart_index == EC801_UART_INDEX){ HAL_UART_MspDeInit(&huart5); + }else if(uart_index == RAIN_RS485_UART_INDEX){ + HAL_UART_MspDeInit(&hlpuart1); } } @@ -132,6 +146,8 @@ static u_int8_t uart_putchar(device_handle device, char ch) ret= HAL_UART_Transmit(&huart3,(uint8_t*)&ch,1,10); }else if(device_info->uart_index == EC801_UART_INDEX){ ret= HAL_UART_Transmit(&huart5,(uint8_t*)&ch,1,10); + }else if(device_info->uart_index == RAIN_RS485_UART_INDEX){ + ret= HAL_UART_Transmit(&hlpuart1,(uint8_t*)&ch,1,10); } if(ret == HAL_OK){ @@ -331,3 +347,12 @@ void init_ec801_uart() g_ec801_uart_handle = uart_dev_init(EC801_UART_INDEX, ec801_in_buff, sizeof(ec801_in_buff)); } +/** + * @brief ³õʼ»¯ÓêÁ¿´®¿Ú. + * @retval None + */ +void init_rain_uart() +{ + HAL_GPIO_WritePin(GPIO_RAIN_PWR_CTRL_GPIO_Port, GPIO_RAIN_PWR_CTRL_Pin, GPIO_PIN_SET); + g_rain_uart_handle = uart_dev_init(RAIN_RS485_UART_INDEX, rain_in_buff, sizeof(rain_in_buff)); +} diff --git a/Core/Inc/usart.h b/Core/Inc/usart.h index 7f4bb63..3e77c1d 100644 --- a/Core/Inc/usart.h +++ b/Core/Inc/usart.h @@ -45,7 +45,7 @@ extern UART_HandleTypeDef huart3; /* USER CODE END Private defines */ -void MX_LPUART1_UART_Init(void); +void MX_LPUART1_UART_Init(int baud); void MX_UART5_Init(int baud); void MX_USART1_UART_Init(int baud); void MX_USART3_UART_Init(int baud); diff --git a/Core/Src/main.c b/Core/Src/main.c index 3c1f1cd..051265d 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -152,7 +152,7 @@ void Flash_EnableReadProtection(void) MX_GPIO_Init(); MX_DMA_Init(); MX_ADC1_Init(); - MX_LPUART1_UART_Init(); +// MX_LPUART1_UART_Init(4800); // MX_USART1_UART_Init(); // MX_USART3_UART_Init(); MX_TIM2_Init(); @@ -167,6 +167,7 @@ void Flash_EnableReadProtection(void) /* USER CODE BEGIN 2 */ init_term_uart(); init_rs485_uart(); + init_rain_uart();uart_dev_write(g_rain_uart_handle, "Hello", 5); // init_ec801_uart(); term_printf("Micro Climate Station Sensor.\r\n"); term_printf("Version 1.0.0 Build: %s %s\r\n",__DATE__,__TIME__); @@ -215,8 +216,10 @@ void SystemClock_Config(void) /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 1; diff --git a/Core/Src/stm32l4xx_it.c b/Core/Src/stm32l4xx_it.c index be42d77..55af8d3 100644 --- a/Core/Src/stm32l4xx_it.c +++ b/Core/Src/stm32l4xx_it.c @@ -279,13 +279,20 @@ void UART5_IRQHandler(void) */ void LPUART1_IRQHandler(void) { - /* USER CODE BEGIN LPUART1_IRQn 0 */ - - /* USER CODE END LPUART1_IRQn 0 */ + /* USER CODE BEGIN USART3_IRQn 0 */ + uint8_t c = 0; + /* USER CODE END USART3_IRQn 0 */ HAL_UART_IRQHandler(&hlpuart1); - /* USER CODE BEGIN LPUART1_IRQn 1 */ - - /* USER CODE END LPUART1_IRQn 1 */ + /* USER CODE BEGIN USART3_IRQn 1 */ + uart_device_info *dev = (uart_device_info *)g_rain_uart_handle; + HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1); + c = rx_uart2_buf[0]; + + if(!RingQueueFull(&dev->uart_ring_queue)) + InRingQueue(&dev->uart_ring_queue, c); + + HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1); + /* USER CODE END USART3_IRQn 1 */ } /* USER CODE BEGIN 1 */ diff --git a/Core/Src/usart.c b/Core/Src/usart.c index 5888c4d..74a1ebf 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -34,7 +34,7 @@ UART_HandleTypeDef huart3; /* LPUART1 init function */ -void MX_LPUART1_UART_Init(void) +void MX_LPUART1_UART_Init(int baud) { /* USER CODE BEGIN LPUART1_Init 0 */ @@ -45,8 +45,8 @@ void MX_LPUART1_UART_Init(void) /* USER CODE END LPUART1_Init 1 */ hlpuart1.Instance = LPUART1; - hlpuart1.Init.BaudRate = 209700; - hlpuart1.Init.WordLength = UART_WORDLENGTH_7B; + hlpuart1.Init.BaudRate = baud; + hlpuart1.Init.WordLength = UART_WORDLENGTH_8B; hlpuart1.Init.StopBits = UART_STOPBITS_1; hlpuart1.Init.Parity = UART_PARITY_NONE; hlpuart1.Init.Mode = UART_MODE_TX_RX; @@ -58,7 +58,7 @@ void MX_LPUART1_UART_Init(void) Error_Handler(); } /* USER CODE BEGIN LPUART1_Init 2 */ - //HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1); + HAL_UART_Receive_IT(&hlpuart1, rx_uart2_buf,1); /* USER CODE END LPUART1_Init 2 */ } @@ -169,7 +169,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) /** Initializes the peripherals clock */ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1; - PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_SYSCLK; + PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_HSI; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); @@ -192,7 +192,8 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN LPUART1_MspInit 1 */ - + HAL_NVIC_SetPriority(LPUART1_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(LPUART1_IRQn); /* USER CODE END LPUART1_MspInit 1 */ } else if(uartHandle->Instance==UART5) @@ -338,7 +339,7 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) HAL_GPIO_DeInit(GPIOB, GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11); /* USER CODE BEGIN LPUART1_MspDeInit 1 */ - + HAL_NVIC_DisableIRQ(LPUART1_IRQn); /* USER CODE END LPUART1_MspDeInit 1 */ } else if(uartHandle->Instance==UART5)