- 硬件
- STM32L475
- 方案
- 使用通過修改HAL庫,加入IDLE中斷,實(shí)現(xiàn)不定長數(shù)據(jù)接收
- 使用通過修改HAL庫,加入IDLE中斷,實(shí)現(xiàn)不定長數(shù)據(jù)接收
- 參考資料
- LAT0534_UART_IDLE中斷使用_接收不定長串口數(shù)據(jù)_V0.3
- cubemx配置
- 實(shí)現(xiàn)過程
- 首先接入接受的數(shù)據(jù)緩沖區(qū),并設(shè)置緩沖區(qū)的大小
//Store the revceived bytes number uint32_t Rev_Size = 0; //Receive buffer uint8_t UART_RX_Buf[15];?
- 然后修改HAL庫的usart.c文件
- 加入extern uint32_t Rev_Size;變量聲明
- 修改HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)該函數(shù)
#else //修改后的代碼 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE| USART_CR1_IDLEIE)); // CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));//修改前的代碼 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); #endif /* USART_CR1_FIFOEN */
- 修改HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)該函數(shù),這里需要注意一下加入代碼的位置。
__HAL_UNLOCK(huart); /* Enable the UART IDLE Interrupt*/ //加入的代碼 SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); /* Enable the UART Parity Error Interrupt */ SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); ?
- 修改void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)函數(shù),光放文檔里面的是 USART_SR_IDLE需要換成 USART_ISR_IDLE,NDTR換成CNDTR(具體原因看技術(shù)手冊(cè)和源碼)
#if defined(USART_CR1_FIFOEN) if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) || ((cr3its & USART_CR3_RXFTIE) != 0U))) #else //加入的代碼 if(((isrflags & USART_ISR_IDLE) != RESET) && ((cr1its & USART_CR1_IDLEIE) != RESET)) { //Record the received bytes number Rev_Size = huart->RxXferSize - huart->hdmarx->Instance->CNDTR; //clear the IDLE flag __HAL_UART_CLEAR_IDLEFLAG(huart); //Abord the received process HAL_UART_AbortReceive_IT(huart); return; } if (((isrflags & USART_ISR_RXNE) != 0U) && ((cr1its & USART_CR1_RXNEIE) != 0U)) #endif /* USART_CR1_FIFOEN */ { if (huart->RxISR != NULL) { huart->RxISR(huart); } return; } }?
- 在main.c里面進(jìn)行函數(shù)修改
- 添加中斷,該中斷是在開啟接收后,當(dāng)接收到的數(shù)據(jù)沒有到達(dá)最大的接收位的時(shí)候,但是總線上停止了數(shù)據(jù)傳輸,總線進(jìn)入空閑狀態(tài),則產(chǎn)生中止中斷。,并進(jìn)入該函數(shù)進(jìn)行數(shù)據(jù)處理。
void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart) { //Print received Bytes printf("\n\r[IDLE]Received %d Bytes:",Rev_Size); for(uint16_t i = 0; i < Rev_Size; i++) { printf(" 0x%02X", UART_RX_Buf[i]); } //Re-start receiving HAL_UART_Receive_DMA(&huart1, UART_RX_Buf, 15); /* NOTE : This function should not be modified, when the callback is needed, the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file. */ }?
- 該中斷的解釋
/** * @brief Abort ongoing Receive transfer (Interrupt mode). * @param huart UART handle. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be * considered as completed only when user abort complete callback is executed (not when exiting function). * @retval HAL status */?
- HAL_UART_Receive_DMA(&huart1, UART_RX_Buf, 15);接收中斷的啟動(dòng)函數(shù),通過啟動(dòng)接收,并在中止中斷里面再次開啟實(shí)現(xiàn)循環(huán)接收。其中15是最大的可接受的數(shù)據(jù)。
- 具體使用,需要注意的是,printf函數(shù)需要自己加入printf支持。
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ printf("123456789\r\n"); HAL_UART_Receive_DMA(&huart1, UART_RX_Buf, 15); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } ?
- 結(jié)果
- 添加中斷,該中斷是在開啟接收后,當(dāng)接收到的數(shù)據(jù)沒有到達(dá)最大的接收位的時(shí)候,但是總線上停止了數(shù)據(jù)傳輸,總線進(jìn)入空閑狀態(tài),則產(chǎn)生中止中斷。,并進(jìn)入該函數(shù)進(jìn)行數(shù)據(jù)處理。
- 首先接入接受的數(shù)據(jù)緩沖區(qū),并設(shè)置緩沖區(qū)的大小
STM32接收不定長數(shù)據(jù)
聲明:本內(nèi)容為作者獨(dú)立觀點(diǎn),不代表電子星球立場。未經(jīng)允許不得轉(zhuǎn)載。授權(quán)事宜與稿件投訴,請(qǐng)聯(lián)系:editor@netbroad.com
本篇所含全部資料,點(diǎn)擊此處留下郵箱我會(huì)發(fā)給你
資料明細(xì):使用IDLE中斷實(shí)現(xiàn)不定長數(shù)據(jù)接收。
覺得內(nèi)容不錯(cuò)的朋友,別忘了一鍵三連哦!
全部留言
0/200
- dy-xGqRDsfq 2024-10-29 21:51老師,能不能發(fā)我一下資料,謝謝! 11****@****.com回復(fù) 1條回復(fù)
- zhouspace 2022-03-27 09:07老師,能不能發(fā)我一下資料,謝謝! 14****@****.com回復(fù) 3條回復(fù)
- 阿飛的秘術(shù)屋 2021-08-27 18:39老師,能不能發(fā)我一下資料,謝謝! 56****@****.com回復(fù) 3條回復(fù)