最近在把标准库的项目移植到HAL库中,同时使用freeRTOS进行任务管理,HAL库属于初学。
硬件: STM32F103RCT6+IRDA
简介:使用SMT32F103RCT6+IRDA模块作为上位机与STM32F103C8T6+IRDA下位机通过modbus通讯
软件:STM32CUBEIDE开发工具和生成的软件,freeRTOS
问题概要:正常运行时,收到命令A的回复,对回复进行解析后,再发送命令B,会出现命令发送失败的情况,但是单步调试时,发送可以成功,添加延时函数还是不行。
还请 论坛各位同仁帮忙看看,提一下意见,谢谢
相关代码如下:
- void HAL_IRDA_MspInit(IRDA_HandleTypeDef* irdaHandle)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(irdaHandle->Instance==USART1)
- {
- /* USER CODE BEGIN USART1_MspInit 0 */
- /* USER CODE END USART1_MspInit 0 */
- /* USART1 clock enable */
- __HAL_RCC_USART1_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**USART1 GPIO Configuration
- PA9 ------> USART1_TX
- PA10 ------> USART1_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_10;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, GPIO_InitStruct);
- /* USART1 DMA Init */
- /* USART1_RX Init */
- hdma_usart1_rx.Instance = DMA1_Channel5;
- hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_usart1_rx.Init.Mode = DMA_NORMAL;
- hdma_usart1_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
- if (HAL_DMA_Init( hdma_usart1_rx) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(irdaHandle,hdmarx,hdma_usart1_rx);
- /* USART1_TX Init */
- hdma_usart1_tx.Instance = DMA1_Channel4;
- hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_usart1_tx.Init.Mode = DMA_NORMAL;
- hdma_usart1_tx.Init.Priority = DMA_PRIORITY_MEDIUM;
- if (HAL_DMA_Init( hdma_usart1_tx) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(irdaHandle,hdmatx,hdma_usart1_tx);
- /* USER CODE BEGIN USART1_MspInit 1 */
- HAL_IRDA_Receive_DMA(irdaHandle, irda1_rx_buffer, IRDA_UART_RX_SIZE); //使能DMA接受,将接受到的数据放到irda1_rx_buffer
- /* USER CODE BEGIN USART1_MspInit 1 */
- /* USART1 interrupt Init */
- HAL_NVIC_SetPriority(USART1_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(USART1_IRQn);
- /* USER CODE BEGIN USART1_MspInit 1 */
- __HAL_UART_ENABLE_IT(irdaHandle, IRDA_IT_IDLE); //usart1, 使能空闲中断
- __HAL_UART_CLEAR_IDLEFLAG(irdaHandle);
- /* USER CODE END USART1_MspInit 1 */
- }
- void USART1_IRQHandler(void)
- {
- /* USER CODE BEGIN USART1_IRQn 0 */
- if (__HAL_UART_GET_FLAG( hirda1, UART_FLAG_IDLE))
- {
- __HAL_UART_CLEAR_IDLEFLAG( hirda1);
- HAL_IRDA_DMAStop ( hirda1);
- irda1_data_cnt = IRDA_UART_RX_SIZE - __HAL_DMA_GET_COUNTER( hdma_usart1_rx);
- if (irda1_data_cnt < 6)
- {
- print_hex(irda1_rx_buffer, irda1_data_cnt);
- }
- else
- {
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
- xEventGroupSetBitsFromISR (irda_event_gourpHandler,
- EVENT_GROUP_BIT_IRDA1_DATA,
- xHigherPriorityTaskWoken);
- }
- hdma_usart1_rx.Instance->CNDTR = IRDA_UART_RX_SIZE;
- HAL_IRDA_Receive_DMA ( hirda1, irda1_rx_buffer, IRDA_UART_RX_SIZE);
- }
- /* USER CODE END USART1_IRQn 0 */
- HAL_IRDA_IRQHandler( hirda1);
- /* USER CODE BEGIN USART1_IRQn 1 */
- /* USER CODE END USART1_IRQn 1 */
- }
- void irda_modbus_parse_task(void const *argument)
- {
- irda_event_gourpHandler = xEventGroupCreate();
- if(irda_event_gourpHandler == NULL)
- {
- app_err("create irda_event_gourp fail!rn");
- goto error;
- }
- xEventGroupClearBits(irda_event_gourpHandler, EVENT_GROUP_BIT_IRDA1_DATA|EVENT_GROUP_BIT_IRDA2_DATA);
- while(1)
- {
- EventBits_t bits = xEventGroupWaitBits(irda_event_gourpHandler, EVENT_GROUP_BIT_IRDA1_DATA|EVENT_GROUP_BIT_IRDA2_DATA, pdFALSE, pdFALSE, portMAX_DELAY);
- if(bits EVENT_GROUP_BIT_IRDA1_DATA)
- {
- app_debug("irda1 recv datarn");
- xEventGroupClearBits(irda_event_gourpHandler, EVENT_GROUP_BIT_IRDA1_DATA);
- Modbus_Master_Service(irda1_rx_buffer, irda1_data_cnt, 1);
- memset(irda1_rx_buffer, 0, sizeof(irda1_rx_buffer));
- }
- else if(bits EVENT_GROUP_BIT_IRDA2_DATA)
- {
- app_debug("irda2 recv datarn");
- xEventGroupClearBits(irda_event_gourpHandler, EVENT_GROUP_BIT_IRDA2_DATA);
- }
- else
- {
- xEventGroupClearBits(irda_event_gourpHandler, ~(EVENT_GROUP_BIT_IRDA1_DATA|EVENT_GROUP_BIT_IRDA2_DATA));
- app_err("UNEXPECTED EVENTrn");
- }
- }
- error:
- vEventGroupDelete(irda_event_gourpHandler);
- osThreadTerminate (irda_modbus_parseTaskHandle);
- }
- void Modbus_SendData(uint8_t *buf, int len, char direction)
- {
- int res = 0;
- if(direction == 1)
- {
- res = HAL_IRDA_Transmit_DMA( hirda1, buf, len);
- if(res != 0)
- {
- app_err("uart1 error: %drn", res);
- }
- }
- else if(direction == 2)
- {
- HAL_IRDA_Transmit_DMA ( hirda2, buf, len);
- }
- else if(direction == 3)
- {
- HAL_IRDA_Transmit_DMA ( hirda1, buf, len);
- HAL_IRDA_Transmit_DMA ( hirda2, buf, len);
- }
- }
0
|
2个回答
|
|
|