参考文档:野火STM32F103
1. 物理层
STM32见下图
1、TTL电平标准:
输出L:<0.8V;H:>2.4V。
输入L:<1.2V;H:>2.0V。
2、CMOS电平标准:
输出L:<0.1Vcc;H:>0.9Vcc。
输入L:<0.3Vcc;H:>0.7Vcc
3、RS232标准
逻辑1的电平为-3~-15V,逻辑0的电平为+3~+15V
4、RS485标准
输出A、B之间的电压差:H:+2+6V,L:-2-6V
输入A、B之间的电压差:H:>+200m V,L:<+200m V
2. 协议层
起始位:低电平
数据位:通常8位
校验位:奇偶校验 奇校验:01101001 校验位:1
停止位: 0.5 ,1, 1.5,2个电平
串口通信一般的配置:1起始位+8数据位+1停止位
速率计算: 115200波特率
1秒可传输: 115200/10 = 11520Byte
1毫秒可传输: 11520/1000 = 11Byte
简单区分同步和异步就是看通信时需不需要对外提供时钟输出,我们平时用的串口通信基本都是UART
3. 软件
typedef struct {uint32_t BaudRate; //波特率uint32_t WordLength; //字长uint32_t StopBits; //停止位uint32_t Parity; //校验位uint32_t Mode; //UART模式uint32_t HwFlowCtl; //硬件流控制uint32_t OverSampling; // 过采样模式uint32_t CLKLastBit; // 最尾位时钟脉冲
} USART_InitTypeDef;
4. 常用USB转UART芯片: CH340
5. 串口重定向
#include <stdio.h>//重定向c库函数printf到串口USARTx,重定向后可使用printf函数int fputc(int ch, FILE *f){/* 发送一个字节数据到串口USARTx */HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);return (ch);}
在C语言HAL库中,fputc函数是printf函数内部的一个函数,功能是将字符ch写入到文件指针f所指向文件的当前写指针位置,简单理解就是把字符写入到特定文件中。我们使用USART函数重新修改fputc函数内容,达到类似“写入”的功能。
还有一点需要注意的,使用fput和fgetc函数达到重定向C语言HAL库输入输出函数必须在MDK的工程选项把“Use MicroLIB”勾选上,MicoroLIB是缺省C库的备选库,它对标准C库进行了高度优化使代码更少,占用更少资源。
6. 串口中断接收
1. 串口初始化
/* USART1 init function */void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(uartHandle->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 ConfigurationPA9 ------> USART1_TXPA10 ------> USART1_RX*/GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF4_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART1 interrupt Init */HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);HAL_NVIC_EnableIRQ(USART1_IRQn);/* USER CODE BEGIN USART1_MspInit 1 *//* USER CODE END USART1_MspInit 1 */}
}
2. 开启中断接收
HAL_UART_Receive_IT(&huart1, &rx_data, 1);3. 在中断回调函数中判断处理-重新开启中断接收
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(huart->Instance == USART1) {rx_buff[rx_cnt++] = rx_data;HAL_UART_Receive_IT(&huart1, &rx_data, 1);}
}
7. 串口空闲中断
数据发送间隔一个空数据
数据帧接收之后,从停止位开始一直为高电平,一直持续一帧的时间(包含停止位)为高电平,那时就会产生空闲中断
为了误进入串口空闲中断,RX的IO管脚设置为上拉模式
8. 空闲中断+DMA接收
- 串口DMA接收初始化
UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;/* USART1 init function */void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(uartHandle->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 ConfigurationPA9 ------> USART1_TXPA10 ------> USART1_RX*/GPIO_InitStruct.Pin = GPIO_PIN_9;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF4_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_10;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_PULLUP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF4_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART1 DMA Init *//* USART1_RX Init */hdma_usart1_rx.Instance = DMA1_Channel3;hdma_usart1_rx.Init.Request = DMA_REQUEST_3;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_LOW;if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK){Error_Handler();}__HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);/* USART1 interrupt Init */HAL_NVIC_SetPriority(USART1_IRQn, 2, 0);HAL_NVIC_EnableIRQ(USART1_IRQn);/* USER CODE BEGIN USART1_MspInit 1 *//* USER CODE END USART1_MspInit 1 */}
}
- DMA初始化
/*** Enable DMA controller clock*/
void MX_DMA_Init(void)
{/* DMA controller clock enable */__HAL_RCC_DMA1_CLK_ENABLE();/* DMA interrupt init *//* DMA1_Channel2_3_IRQn interrupt configuration */HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 1, 0);HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);}
- 注意看主函数初始化顺序
先DMA后UART
MX_GPIO_Init();MX_DMA_Init();MX_RTC_Init();MX_USART1_UART_Init();
- 中断函数中设置
stm32f0xx_it.c 文件中
void USART1_IRQHandler(void)
{/* USER CODE BEGIN USART1_IRQn 0 *//* USER CODE END USART1_IRQn 0 */HAL_UART_IRQHandler(&huart1);/* USER CODE BEGIN USART1_IRQn 1 *///新添加的函数,用来处理串口空闲中断extern void USER_UART_IRQHandler(UART_HandleTypeDef *huart);USER_UART_IRQHandler(&huart1);/* USER CODE END USART1_IRQn 1 */
}
- 对空闲中断检测处理
extern uint8_t rx_buff[300];void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
{if(huart->Instance == USART1) //判断是否是串口1{if(RESET != __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE)) //判断是否是空闲中断{__HAL_UART_CLEAR_IDLEFLAG(&huart1); //清楚空闲中断标志(否则会一直不断进入中断)USAR_UART_IDLECallback(huart); //调用中断处理函数//停止本次DMA传输HAL_UART_DMAStop(&huart1);//设置DMA接收HAL_UART_Receive_DMA(&huart1, (uint8_t *)rx_buff, 300);}}
}
- 用户自己的回调处理函数
void USAR_UART_IDLECallback(UART_HandleTypeDef *huart)
{uint8_t len = 0;//计算接收到的数据长度len = 300 - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx); //把数据拷贝memcpy(rx_buff_read+rx_len, rx_buff, len);rx_len += len;
}
- 在主函数初始化,轮询判断接收长度打印出接收数据
//使能串UART2 IDLE中断__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//设置DMA接收HAL_UART_Receive_DMA(&huart1, (uint8_t *)rx_buff, 300);printf("uart_dma_receive init ok\r\n");while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_Delay(100);printf("rx_cnt=%d\r\n", rx_cnt);printf("rx_len=%d\r\n", rx_len);if(rx_len >0 ){ for(uint8_t i=0; i<rx_len; i++){printf("[%x]",rx_buff_read[i]);}rx_len = 0;}}
9. 函数解析
* 发送
1. DMA发送函数
HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
2. 上面这个代码里调用
启动DMA传输并开启中断
HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t *)tmp, (uint32_t)&huart->Instance->DR, Size);
3. 传输完成进入中断服务函数
HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
4. 进入回调函数
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
* 接收
1. DMA接收函数
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
2. 上面这个代码里调用
启动DMA传输并开启中断
HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t *)tmp, (uint32_t)&huart->Instance->DR, Size);
3. 接收完成进入中断服务函数
HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
4. 进入回调函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//使能串UART2 IDLE中断
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//判断是否为空闲中断,若是空闲中断则进入空闲中断处理函数,空闲中断处理函数是自己写的
if(RESET != __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE)) //判断是否是空闲中断
{__HAL_UART_CLEAR_IDLEFLAG(&huart1); //清楚空闲中断标志(否则会一直不断进入中断)USAR_UART_IDLECallback(huart); //调用中断处理函数
}
//判断接收数据的长度
__HAL_DMA_GET_COUNTER(__HANDLE__)
此函数的功能:获取当前DMA通道传输中 receive_buff[BUFFER_SIZE]缓存还剩余多少个数据单元。这样就能算出这一帧数据到底接收了多少单元的数据(数据长度=缓存总长度-缓存剩余的长度),
length = BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
参考链接