標(biāo)題:
STM32F103 UART1 DMA不定長(zhǎng)收發(fā)源碼
[打印本頁]
作者:
annko2005
時(shí)間:
2018-12-12 17:14
標(biāo)題:
STM32F103 UART1 DMA不定長(zhǎng)收發(fā)源碼
在使用STM32F103做串口收發(fā)時(shí),如果采用中斷方式可能會(huì)丟包,但是采用DMA方式就可以避免這種問題,案例就是用DMA方式傳輸數(shù)據(jù)
DMA是一種不使用CPU而將數(shù)據(jù)從一片地址空間復(fù)制到另一片地址空間的總線,這樣就減少了CPU的負(fù)擔(dān),使其能夠更加專注于數(shù)據(jù)運(yùn)算。
為了能夠減少CPU的負(fù)擔(dān),DMA應(yīng)該采取中斷方式而非查詢模式。
但是非常不幸的是,STM32F103只為DMA提供了三種中斷:半步中斷、完成中斷和錯(cuò)誤中斷。
如果UART接收的是定幀長(zhǎng)的數(shù)據(jù),則可以開啟DMA半步中斷,并且目標(biāo)地址長(zhǎng)度為幀長(zhǎng)兩倍。這樣每接收完一幀進(jìn)一次中斷,進(jìn)行某些操作,是很理想的。
然而當(dāng)遇到如同GPS一樣不定幀長(zhǎng)的數(shù)據(jù)時(shí),如果仍用半步中斷則難以確定目標(biāo)地址的長(zhǎng)度。
所以需要放棄使用DMA的中斷,轉(zhuǎn)而使用的是另一種比較特別的中斷:UART空閑中斷。
先來介紹一下UART空閑中斷。
UART常用的接收中斷響應(yīng)有:接收數(shù)據(jù) 就緒可讀中斷RXNE(這是最常用的)、數(shù)據(jù)溢出中斷(ORE)、奇偶校驗(yàn)錯(cuò)中斷(PE)和空閑中斷(IDLE)。
空閑中斷是指當(dāng)總線檢測(cè)到一幀發(fā)完后, 總線空閑則會(huì)將此位置一,如果USART_CR1中的IDLEIE為’1’,則產(chǎn)生中斷。
空閑中斷有兩個(gè)比較有意思的特點(diǎn):
1、清零方式:軟件清零,先讀USART_SR,然后讀USART_DR
2、直到RXNE被置一后IDLE才能被重讀置一,即IDLE被置一并軟件清零后,只有之后再次接收到數(shù)據(jù),IDLE才能被置一。
這樣就防止了總線長(zhǎng)時(shí)間空閑而多次引發(fā)空閑中斷。
單片機(jī)源程序如下:
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
#include "stdarg.h"
#include "string.h"
//#include <stdlib.h>
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
/* Private variables ---------------------------------------------------------*/
#define BUFFER_SIZE 100
uint8_t rx_len=0; //接收一幀數(shù)據(jù)的長(zhǎng)度
uint8_t recv_end_flag=0; //一幀數(shù)據(jù)接收完成標(biāo)志
uint8_t rx_buffer[BUFFER_SIZE]={"123"}; //接收數(shù)據(jù)緩存
uint8_t tx_buffer[BUFFER_SIZE];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
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_TIM2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
//printf("tst uart DMA begin\r\n");
//__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); //使能IDLE中斷
HAL_UART_Receive_DMA(&huart1,rx_buffer,BUFFER_SIZE);//啟動(dòng)DMA接收
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(Tst_Pin_GPIO_Port, Tst_Pin_Pin, GPIO_PIN_RESET);
if(recv_end_flag ==1)
{
HAL_UART_Transmit(&huart1,rx_buffer, rx_len,200);//接收數(shù)據(jù)打印出來
rx_len=0;
recv_end_flag=0;
//memset(rx_buffer,0x00,BUFFER_SIZE);
HAL_UART_Receive_DMA(&huart1,rx_buffer,BUFFER_SIZE); //重啟DMA接收
}
HAL_GPIO_WritePin(Tst_Pin_GPIO_Port, Tst_Pin_Pin, GPIO_PIN_SET);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* USER CODE BEGIN 4 */
void UART_IDLE_Callback(UART_HandleTypeDef *huart)
{
uint16_t i = 0;
if((__HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE) != RESET))
{
if(huart->Instance == USART1)
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
i = huart->Instance->SR;
i = huart->Instance->DR;
i = hdma_usart1_rx.Instance->CNDTR; //讀取DMA剩余傳輸數(shù)量
HAL_UART_DMAStop(huart);
/* 此處處理數(shù)據(jù),主要是拷貝和置位標(biāo)志位 */
if((recv_end_flag == 0)&&(i!=BUFFER_SIZE))
{
rx_len = BUFFER_SIZE - i;
//memcpy(tx_buffer,rx_buffer,rx_len);
recv_end_flag = 1;
}
else
{
/* 清空緩存,重新接收 */
//memset(rx_buffer,0x00,BUFFER_SIZE);
HAL_UART_Receive_DMA(huart,(uint8_t *)&rx_buffer,BUFFER_SIZE);
}
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復(fù)制代碼
所有資料51hei提供下載:
103_UARTDMA.rar
(445.2 KB, 下載次數(shù): 104)
2018-12-13 04:00 上傳
點(diǎn)擊文件名下載附件
UART DMA
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1