一、cubemx配置工程
CAN1参数设置
USART1设置
DMA设置
GPIO设置
定时器设置
中断分组设置
工程设置
生成工程
工程生成成功打开工程
二、代码完善
2.1串口功能完善
采用DMA+串口空闲中断方式实现串口收发不定长数
2.1.1 新添加工程文件bsp_usart.c及bsp_usart.h
/***********************************************************************************
* 文 件 名 : bsp_usart.c
* 负 责 人 : jishubao
* 创建日期 : 2019年3月6日
* 文件描述 : 板载串口功能文件
* 版权说明 : Copyright (c) 2008-2019
* 其 他 :
* 修改日志 :
***********************************************************************************/
#include "bsp_usart.h"
#include "usart.h"
#include "string.h"
USART_RECEIVETYPE UsartType;
/*****************************************************************************
* 函 数 名 : HAL_UART_TxCpltCallback
* 负 责 人 : jishubao
* 创建日期 : 2019年2月27日
* 函数功能 : 串口发送完成回调函数
* 输入参数 : UART_HandleTypeDef *huart 串口操作句柄
* 输出参数 : 无
* 返 回 值 :
* 调用关系 :
* 其 它 : 使用DMA时需要将发送完成状态更新,否则DMA会出现bug
*****************************************************************************/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
huart->gState = HAL_UART_STATE_READY;
}
/*****************************************************************************
* 函 数 名 : UsartReceive_IDLE
* 负 责 人 : jishubao
* 创建日期 : 2019年2月19日
* 函数功能 : 串口接受空闲中断处理函数
* 输入参数 : UART_HandleTypeDef *huart 串口句柄
* 输出参数 : 无
* 返 回 值 :
* 调用关系 :
* 其 它 :
*****************************************************************************/
void UsartReceive_IDLE(UART_HandleTypeDef *huart)
{
uint32_t temp;
if((__HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE) != RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(huart);
HAL_UART_DMAStop(huart);
temp = (*huart).hdmarx->Instance->NDTR;
if(huart->Instance==USART1)
{
UsartType.RX_Size = RX_LEN - temp;
UsartType.RX_flag = 1;
HAL_UART_Receive_DMA(huart,UsartType.RX_pData,RX_LEN);
}
}
}
/*****************************************************************************
* 函 数 名 : Analysis_Serial_Data
* 负 责 人 : jishubao
* 创建日期 : 2019年2月19日
* 函数功能 : 串口接收数据直接转发函数
* 输入参数 : void 无
* 输出参数 : 无
* 返 回 值 : void
* 调用关系 :
* 其 它 :
*****************************************************************************/
void Analysis_Serial_Data(void)
{
if(UsartType.RX_flag) // Receive flag
{
UsartType.RX_flag=0; // clean flag
HAL_UART_Transmit_DMA(&huart1, UsartType.RX_pData, UsartType.RX_Size);
HAL_GPIO_TogglePin(GPIOF,LED1_Pin);
}
}
//实现printf函数重定向到串口1,即支持printf信息到USART1
#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__ */
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/***********************************************************************************
* 文 件 名 : bsp_usart.h
* 负 责 人 : jishubao
* 创建日期 : 2019年3月6日
* 文件描述 : bsp_usart.c 的头文件
* 版权说明 : Copyright (c) 2008-2019
* 其 他 :
* 修改日志 :
***********************************************************************************/
#ifndef __BSP_USART_H__
#define __BSP_USART_H__
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "main.h"
#include "usart.h"
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
/* BEGIN Private defines */
#define RX_LEN 1024
typedef struct
{
uint8_t RX_flag:1; //IDLE receive flag
uint8_t TX_flag:1; //IDLE send flag
uint16_t RX_Size; //receive length
uint8_t RX_pData[RX_LEN]; //DMA receive buffer
}USART_RECEIVETYPE;
extern USART_RECEIVETYPE UsartType;
/* END Private defines */
void Analysis_Serial_Data(void);
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
void UsartReceive_IDLE(UART_HandleTypeDef *huart);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif
在stm32f4xx_it.c文件中添加如下代码
在main.c中添加部分代码
至此串口功能代码完善完成,下载到开发板后重启看到LED0闪烁(10Hz),可以看到通过串口工具发送给开发板数据,开发板会将原数据发送回串口工具,而且LED随着串口发送数据闪烁。
程序源码 密码:kbmj