STM32CubeMX stm32不限长度使用DMA收发串口数据

STM32CubeMX 配置

在这里插入图片描述
在这里插入图片描述

代码

stm32h7xx_it.c

/**
 * @brief This function handles UART7 global interrupt.
 */
void UART7_IRQHandler(void)
{
    /* USER CODE BEGIN UART7_IRQn 0 */
    if (UART7 == huart7.Instance) // 判断是否是空闲中断
    {
        if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_RXNE) != RESET) // 接收中断的标志位
        {
            printf("\r\nUART7 接收中断 可能超过缓冲区长度了 重启串口 \r\n");
            MX_UART7_Init();    //因为溢出 造成 串口不能恢复正常接收 所以重新初始化一下
            receives_uaru_7();        // 启用中断
        }

        if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_IDLE) == SET) // 触发空闲中断
        {
            HAL_UART_DMAStop(&huart7); // 停止本次DMA传输
            //printf("\r\nUART7 检测到空闲\r\n");
            UART_7_Callback(&huart7); // 调用用户空闲中断回调函数
            receives_uaru_7();        // 启用中断
        }
    }

    /* USER CODE END UART7_IRQn 0 */
    HAL_UART_IRQHandler(&huart7);
    /* USER CODE BEGIN UART7_IRQn 1 */

    /* USER CODE END UART7_IRQn 1 */
}

在这里插入图片描述

加入头文件
在这里插入图片描述
uart.h


//*********************串口7 */
// UART接收中断变量
#define RXBUFFERSIZE_7 1024          // 缓冲区长度
extern uint8_t RxBuff_7[RXBUFFERSIZE_7]; // 接收后的数据
void UART_7_Callback(UART_HandleTypeDef *huart);//空闲中断
void receives_uaru_7(void);//接收
void uart7_printf(const char *format, ...);//打印
//*********************串口7 */

在这里插入图片描述
uart.c

uint8_t RxBuff_7[RXBUFFERSIZE_7]; // 接收后的数据

在这里插入图片描述

    /* USER CODE BEGIN UART7_Init 2 */

    __HAL_UART_ENABLE_IT(&huart7, UART_IT_RXNE); // 开启接收中断
    __HAL_UART_ENABLE_IT(&huart7, UART_IT_IDLE); // 使能空闲中断
    receives_uaru_7();                           // 接收数据
    /* USER CODE END UART7_Init 2 */

在这里插入图片描述

/*
    用户自定义串口空闲中断回调函数
*/
void UART_7_Callback(UART_HandleTypeDef *huart)
{
    // 计算接收到的数据长度
    uint32_t data_length = RXBUFFERSIZE_7 - __HAL_DMA_GET_COUNTER(&hdma_uart7_rx);
    printf("数据长度(length = %d): ", data_length);
    printf("\r\n");
    // HAL_UART_Transmit(&huart1, RxBuff_7, data_length, 0x200);//串口1发送 0x200超时时间
    RxBuff_7[data_length] = '\0';//尾部加0
    //{"a":6}
    cJSON *json;
    json = cJSON_Parse((const char *)RxBuff_7);
    if (json != NULL)
    {
        cJSON *robj = cJSON_GetObjectItem(json, "a");
        printf("a: %d\r\n", robj->valueint);
        cJSON_Delete(json);
    }
}
// 删除中断标志 启用 DMA 接收
void receives_uaru_7(void)
{
    __HAL_UART_CLEAR_IDLEFLAG(&huart7); // 清除空闲中断标志)
    // 重启开始DMA传输
    HAL_UART_Receive_DMA(&huart7, (uint8_t *)RxBuff_7, RXBUFFERSIZE_7 - 1);//预留一个字节尾部放'0'
}

在这里插入图片描述

完整代码

usart.c

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    usart.c
 * @brief   This file provides code for the configuration
 *          of the USART instances.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"

/* USER CODE BEGIN 0 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

// #include "stdio.h"
// #include <string.h>
// #include <stdarg.h>
void cmd(char *str);
//*********************串口1 */
#define RXBUFFERSIZE_1 1          // 每接收1个字节中断1次
uint8_t RxBuff_1[RXBUFFERSIZE_1]; // 中断后数据储存位置
// 启动中断
void receives_uaru_1(void);
// UART2接收缓冲区
#define MAX_LEN_1 10
char data1[MAX_LEN_1];
// 已经接收的长度
uint8_t data_len_1 = 0;



//串口7
uint8_t RxBuff_7[RXBUFFERSIZE_7]; // 接收后的数据
/* USER CODE END 0 */

UART_HandleTypeDef huart7;
UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_uart7_rx;
DMA_HandleTypeDef hdma_uart7_tx;

/* UART7 init function */
void MX_UART7_Init(void)
{

    /* USER CODE BEGIN UART7_Init 0 */

    /* USER CODE END UART7_Init 0 */

    /* USER CODE BEGIN UART7_Init 1 */

    /* USER CODE END UART7_Init 1 */
    huart7.Instance = UART7;
    huart7.Init.BaudRate = 750000;
    huart7.Init.WordLength = UART_WORDLENGTH_8B;
    huart7.Init.StopBits = UART_STOPBITS_1;
    huart7.Init.Parity = UART_PARITY_NONE;
    huart7.Init.Mode = UART_MODE_TX_RX;
    huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart7.Init.OverSampling = UART_OVERSAMPLING_16;
    huart7.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart7.Init.ClockPrescaler = UART_PRESCALER_DIV1;
    huart7.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart7) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetTxFifoThreshold(&huart7, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetRxFifoThreshold(&huart7, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_DisableFifoMode(&huart7) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN UART7_Init 2 */

    __HAL_UART_ENABLE_IT(&huart7, UART_IT_RXNE); // 开启接收中断
    __HAL_UART_ENABLE_IT(&huart7, UART_IT_IDLE); // 使能空闲中断
    receives_uaru_7();                           // 接收数据
    /* USER CODE END UART7_Init 2 */
}
/* 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 = 750000;
    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.Init.ClockPrescaler = UART_PRESCALER_DIV1;
    huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart1) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
    {
        Error_Handler();
    }
    if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN USART1_Init 2 */
    // 启用接收中断
    receives_uaru_1();
    /* USER CODE END USART1_Init 2 */
}

void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
{

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
    if (uartHandle->Instance == UART7)
    {
        /* USER CODE BEGIN UART7_MspInit 0 */

        /* USER CODE END UART7_MspInit 0 */

        /** Initializes the peripherals clock
         */
        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_UART7;
        PeriphClkInitStruct.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_D2PCLK1;
        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
        {
            Error_Handler();
        }

        /* UART7 clock enable */
        __HAL_RCC_UART7_CLK_ENABLE();

        __HAL_RCC_GPIOF_CLK_ENABLE();
        /**UART7 GPIO Configuration
        PF6     ------> UART7_RX
        PF7     ------> UART7_TX
        */
        GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF7_UART7;
        HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

        /* UART7 DMA Init */
        /* UART7_RX Init */
        hdma_uart7_rx.Instance = DMA1_Stream5;
        hdma_uart7_rx.Init.Request = DMA_REQUEST_UART7_RX;
        hdma_uart7_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_uart7_rx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_uart7_rx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_uart7_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_uart7_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_uart7_rx.Init.Mode = DMA_NORMAL;
        hdma_uart7_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
        hdma_uart7_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_uart7_rx) != HAL_OK)
        {
            Error_Handler();
        }

        __HAL_LINKDMA(uartHandle, hdmarx, hdma_uart7_rx);

        /* UART7_TX Init */
        hdma_uart7_tx.Instance = DMA2_Stream4;
        hdma_uart7_tx.Init.Request = DMA_REQUEST_UART7_TX;
        hdma_uart7_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
        hdma_uart7_tx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_uart7_tx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_uart7_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_uart7_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_uart7_tx.Init.Mode = DMA_NORMAL;
        hdma_uart7_tx.Init.Priority = DMA_PRIORITY_HIGH;
        hdma_uart7_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_uart7_tx) != HAL_OK)
        {
            Error_Handler();
        }

        __HAL_LINKDMA(uartHandle, hdmatx, hdma_uart7_tx);

        /* UART7 interrupt Init */
        HAL_NVIC_SetPriority(UART7_IRQn, 3, 0);
        HAL_NVIC_EnableIRQ(UART7_IRQn);
        /* USER CODE BEGIN UART7_MspInit 1 */

        /* USER CODE END UART7_MspInit 1 */
    }
    else if (uartHandle->Instance == USART1)
    {
        /* USER CODE BEGIN USART1_MspInit 0 */

        /* USER CODE END USART1_MspInit 0 */

        /** Initializes the peripherals clock
         */
        PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1;
        PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
        {
            Error_Handler();
        }

        /* USART1 clock enable */
        __HAL_RCC_USART1_CLK_ENABLE();

        __HAL_RCC_GPIOB_CLK_ENABLE();
        /**USART1 GPIO Configuration
        PB14     ------> USART1_TX
        PB15     ------> USART1_RX
        */
        GPIO_InitStruct.Pin = GPIO_PIN_14 | GPIO_PIN_15;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

        /* USART1 interrupt Init */
        HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
        HAL_NVIC_EnableIRQ(USART1_IRQn);
        /* USER CODE BEGIN USART1_MspInit 1 */

        /* USER CODE END USART1_MspInit 1 */
    }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
{

    if (uartHandle->Instance == UART7)
    {
        /* USER CODE BEGIN UART7_MspDeInit 0 */

        /* USER CODE END UART7_MspDeInit 0 */
        /* Peripheral clock disable */
        __HAL_RCC_UART7_CLK_DISABLE();

        /**UART7 GPIO Configuration
        PF6     ------> UART7_RX
        PF7     ------> UART7_TX
        */
        HAL_GPIO_DeInit(GPIOF, GPIO_PIN_6 | GPIO_PIN_7);

        /* UART7 DMA DeInit */
        HAL_DMA_DeInit(uartHandle->hdmarx);
        HAL_DMA_DeInit(uartHandle->hdmatx);

        /* UART7 interrupt Deinit */
        HAL_NVIC_DisableIRQ(UART7_IRQn);
        /* USER CODE BEGIN UART7_MspDeInit 1 */

        /* USER CODE END UART7_MspDeInit 1 */
    }
    else if (uartHandle->Instance == USART1)
    {
        /* USER CODE BEGIN USART1_MspDeInit 0 */

        /* USER CODE END USART1_MspDeInit 0 */
        /* Peripheral clock disable */
        __HAL_RCC_USART1_CLK_DISABLE();

        /**USART1 GPIO Configuration
        PB14     ------> USART1_TX
        PB15     ------> USART1_RX
        */
        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14 | GPIO_PIN_15);

        /* USART1 interrupt Deinit */
        HAL_NVIC_DisableIRQ(USART1_IRQn);
        /* USER CODE BEGIN USART1_MspDeInit 1 */

        /* USER CODE END USART1_MspDeInit 1 */
    }
}

/* USER CODE BEGIN 1 */
int fputc(int ch, FILE *f)
{
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
    return ch;
}
/*串口7发送数据*/
void uart7_printf(const char *format, ...)
{
    char buffer[256];
    va_list args;
    va_start(args, format);
    vsnprintf(buffer, sizeof(buffer), format, args);
    va_end(args);
    // HAL_UART_Transmit(&huart7, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);//串口发送
    HAL_UART_Transmit_DMA(&huart7, (uint8_t *)buffer, strlen(buffer)); // DMA发送
    // uint8_t send_char[]="所想要发送的内容\n";//发送的字符串
    // HAL_UART_Transmit_DMA(&huart7,(uint8_t *)send_char, sizeof(send_char));//DMA发送 字节发送
}

/*
    重定义串口接收回调函数
*/
// UART接收完成回调函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

    // UART接收完成回调函数
    // 每接收一个字节中断一次
    if (huart->Instance == USART1)
    {

        data1[data_len_1] = (char)RxBuff_1[0];
        data_len_1++;
        // 接收完成
        if (RxBuff_1[0] == '\n' || data_len_1 == MAX_LEN_1 - 1)
        {                             // 接收结束为换行符
            data1[data_len_1] = '\0'; // 字符串结束符
            printf("%s", data1);
            cmd(data1);     // 将命令转到处理函数
            data_len_1 = 0; // 清空缓存
        }
        // 启用接收中断
        receives_uaru_1();
    }
    else if (huart->Instance == UART7) // 中断7
    {
        printf("中断\n");
        // receives_uaru_7(); // 启用中断
    }
}

/*
    用户自定义串口空闲中断回调函数
*/
void UART_7_Callback(UART_HandleTypeDef *huart)
{
    // 计算接收到的数据长度
    uint32_t data_length = RXBUFFERSIZE_7 - __HAL_DMA_GET_COUNTER(&hdma_uart7_rx);
    printf("数据长度(length = %d): ", data_length);
    printf("\r\n");
    // HAL_UART_Transmit(&huart1, RxBuff_7, data_length, 0x200);//串口1发送 0x200超时时间
    RxBuff_7[data_length] = '\0';//尾部加0
    //{"a":6}
    cJSON *json;
    json = cJSON_Parse((const char *)RxBuff_7);
    if (json != NULL)
    {
        cJSON *robj = cJSON_GetObjectItem(json, "a");
        printf("a: %d\r\n", robj->valueint);
        cJSON_Delete(json);
    }
}
// 删除中断标志 启用 DMA 接收
void receives_uaru_7(void)
{
    __HAL_UART_CLEAR_IDLEFLAG(&huart7); // 清除空闲中断标志)
    // 重启开始DMA传输
    HAL_UART_Receive_DMA(&huart7, (uint8_t *)RxBuff_7, RXBUFFERSIZE_7 - 1);//预留一个字节尾部放'0'
}
// 串口1启用中断服务程序
void receives_uaru_1(void)
{
    HAL_UART_Receive_IT(&huart1, (uint8_t *)RxBuff_1, RXBUFFERSIZE_1);
    // 该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
}

void cmd(char *str)
{
    printf("处理数据\n");

    printf("\n");
}

//__use_no_semihosting was requested, but _ttywrch was
void _ttywrch(int ch)
{
    ch = ch;
}

/* USER CODE END 1 */


stm32h7xx_it.c

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    stm32h7xx_it.c
 * @brief   Interrupt Service Routines.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32h7xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_uart7_rx;
extern DMA_HandleTypeDef hdma_uart7_tx;
extern UART_HandleTypeDef huart7;
extern UART_HandleTypeDef huart1;
extern TIM_HandleTypeDef htim6;

/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
 * @brief This function handles Non maskable interrupt.
 */
void NMI_Handler(void)
{
    /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

    /* USER CODE END NonMaskableInt_IRQn 0 */
    /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
    while (1)
    {
    }
    /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
 * @brief This function handles Hard fault interrupt.
 */
void HardFault_Handler(void)
{
    /* USER CODE BEGIN HardFault_IRQn 0 */

    /* USER CODE END HardFault_IRQn 0 */
    while (1)
    {
        /* USER CODE BEGIN W1_HardFault_IRQn 0 */
        /* USER CODE END W1_HardFault_IRQn 0 */
    }
}

/**
 * @brief This function handles Memory management fault.
 */
void MemManage_Handler(void)
{
    /* USER CODE BEGIN MemoryManagement_IRQn 0 */

    /* USER CODE END MemoryManagement_IRQn 0 */
    while (1)
    {
        /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
        /* USER CODE END W1_MemoryManagement_IRQn 0 */
    }
}

/**
 * @brief This function handles Pre-fetch fault, memory access fault.
 */
void BusFault_Handler(void)
{
    /* USER CODE BEGIN BusFault_IRQn 0 */

    /* USER CODE END BusFault_IRQn 0 */
    while (1)
    {
        /* USER CODE BEGIN W1_BusFault_IRQn 0 */
        /* USER CODE END W1_BusFault_IRQn 0 */
    }
}

/**
 * @brief This function handles Undefined instruction or illegal state.
 */
void UsageFault_Handler(void)
{
    /* USER CODE BEGIN UsageFault_IRQn 0 */

    /* USER CODE END UsageFault_IRQn 0 */
    while (1)
    {
        /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
        /* USER CODE END W1_UsageFault_IRQn 0 */
    }
}

/**
 * @brief This function handles System service call via SWI instruction.
 */
void SVC_Handler(void)
{
    /* USER CODE BEGIN SVCall_IRQn 0 */

    /* USER CODE END SVCall_IRQn 0 */
    /* USER CODE BEGIN SVCall_IRQn 1 */

    /* USER CODE END SVCall_IRQn 1 */
}

/**
 * @brief This function handles Debug monitor.
 */
void DebugMon_Handler(void)
{
    /* USER CODE BEGIN DebugMonitor_IRQn 0 */

    /* USER CODE END DebugMonitor_IRQn 0 */
    /* USER CODE BEGIN DebugMonitor_IRQn 1 */

    /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
 * @brief This function handles Pendable request for system service.
 */
void PendSV_Handler(void)
{
    /* USER CODE BEGIN PendSV_IRQn 0 */

    /* USER CODE END PendSV_IRQn 0 */
    /* USER CODE BEGIN PendSV_IRQn 1 */

    /* USER CODE END PendSV_IRQn 1 */
}

/**
 * @brief This function handles System tick timer.
 */
void SysTick_Handler(void)
{
    /* USER CODE BEGIN SysTick_IRQn 0 */

    /* USER CODE END SysTick_IRQn 0 */

    /* USER CODE BEGIN SysTick_IRQn 1 */

    /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32H7xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32h7xx.s).                    */
/******************************************************************************/

/**
 * @brief This function handles DMA1 stream5 global interrupt.
 */
void DMA1_Stream5_IRQHandler(void)
{
    /* USER CODE BEGIN DMA1_Stream5_IRQn 0 */

    /* USER CODE END DMA1_Stream5_IRQn 0 */
    HAL_DMA_IRQHandler(&hdma_uart7_rx);
    /* USER CODE BEGIN DMA1_Stream5_IRQn 1 */

    /* USER CODE END DMA1_Stream5_IRQn 1 */
}

/**
 * @brief This function handles USART1 global interrupt.
 */
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 */

    /* USER CODE END USART1_IRQn 1 */
}

/**
 * @brief This function handles TIM6 global interrupt, DAC1_CH1 and DAC1_CH2 underrun error interrupts.
 */
void TIM6_DAC_IRQHandler(void)
{
    /* USER CODE BEGIN TIM6_DAC_IRQn 0 */

    /* USER CODE END TIM6_DAC_IRQn 0 */
    HAL_TIM_IRQHandler(&htim6);
    /* USER CODE BEGIN TIM6_DAC_IRQn 1 */

    /* USER CODE END TIM6_DAC_IRQn 1 */
}

/**
 * @brief This function handles DMA2 stream4 global interrupt.
 */
void DMA2_Stream4_IRQHandler(void)
{
    /* USER CODE BEGIN DMA2_Stream4_IRQn 0 */

    /* USER CODE END DMA2_Stream4_IRQn 0 */
    HAL_DMA_IRQHandler(&hdma_uart7_tx);
    /* USER CODE BEGIN DMA2_Stream4_IRQn 1 */

    /* USER CODE END DMA2_Stream4_IRQn 1 */
}

/**
 * @brief This function handles UART7 global interrupt.
 */
void UART7_IRQHandler(void)
{
    /* USER CODE BEGIN UART7_IRQn 0 */
    if (UART7 == huart7.Instance) // 判断是否是空闲中断
    {
        if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_RXNE) != RESET) // 接收中断的标志位
        {
            printf("\r\nUART7 接收中断 可能超过缓冲区长度了 重启串口 \r\n");
            MX_UART7_Init();    //因为溢出 造成 串口不能恢复正常接收 所以重新初始化一下
            receives_uaru_7();        // 启用中断
        }

        if (__HAL_UART_GET_FLAG(&huart7, UART_FLAG_IDLE) == SET) // 触发空闲中断
        {
            HAL_UART_DMAStop(&huart7); // 停止本次DMA传输
            //printf("\r\nUART7 检测到空闲\r\n");
            UART_7_Callback(&huart7); // 调用用户空闲中断回调函数
            receives_uaru_7();        // 启用中断
        }
    }

    /* USER CODE END UART7_IRQn 0 */
    HAL_UART_IRQHandler(&huart7);
    /* USER CODE BEGIN UART7_IRQn 1 */

    /* USER CODE END UART7_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */


usart.h


/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    usart.h
  * @brief   This file contains all the function prototypes for
  *          the usart.c file
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <string.h>
#include <stdarg.h>
/* USER CODE END Includes */

extern UART_HandleTypeDef huart7;

extern UART_HandleTypeDef huart1;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_UART7_Init(void);
void MX_USART1_UART_Init(void);

/* USER CODE BEGIN Prototypes */



//*********************串口7 */
// UART接收中断变量
#define RXBUFFERSIZE_7 8          // 缓冲区长度
extern uint8_t RxBuff_7[RXBUFFERSIZE_7]; // 接收后的数据
void UART_7_Callback(UART_HandleTypeDef *huart);//空闲中断
void receives_uaru_7(void);//接收
void uart7_printf(const char *format, ...);//打印
//*********************串口7 */


/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __USART_H__ */


使用STM32CubeMX进行串口DMA收发时,可以按照以下步骤进行设置: 1. 首先,打开STM32CubeMX,并创建一个新的工程。 2. 在"Pinout & Configuration"选项中,选择需要使用串口引脚。 3. 在"Peripherals"选项中,找到并启用DMA控制器和相应的串口。 4. 在"Configuration"选项中,配置串口的波特率、数据位、停止位等参数。 5. 在"Configuration"选项中,选择需要使用DMA通道,并将其分配给相应的串口发送和接收功能。 6. 在生成代码后,将生成的代码导入到你的工程中。 7. 在代码中,使用相关的HAL库函数来初始化和配置串口DMA发送和接收。 8. 在需要发送数据时,使用HAL_UART_Transmit_DMA函数将数据发送到串口。 9. 在需要接收数据时,使用HAL_UART_Receive_DMA函数来启动接收DMA,并在接收完成时触发相应的DMA中断。 10. 在DMA中断处理函数中,根据需要处理接收到的数据。 以上是使用STM32CubeMX进行串口DMA收发的基本步骤。你可以参考这些步骤来进行具体的设置和应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [HAL库 STM32CubeMX——DMA /中断串口发送与接收](https://blog.csdn.net/qq_59128292/article/details/121180289)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [使用stm32cubeMX进行串口DMA收发](https://blog.csdn.net/boomboomy/article/details/121147873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ547176052

觉得对你有帮助就多多鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值