GD32F450串口DMA收发

Uart0_F450.cpp

#include "UART0_F450.hpp"
#include "main.h"

//#include "usart.h"
//#include "dma.h"

//extern UART_HandleTypeDef huart1;
//extern DMA_HandleTypeDef hdma_usart1_rx;
//extern DMA_HandleTypeDef hdma_usart1_tx;

#define USART0_DATA_ADDRESS      ((uint32_t)&USART_DATA(USART0))

void UART0_F450::Init(uint32_t baud)
{	
	/* initialize clock */
	rcu_periph_clock_enable(RCU_DMA1);
	rcu_periph_clock_enable(RCU_USART0);
	rcu_periph_clock_enable(RCU_GPIOA);
	
	/* initialize port */
	gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9|GPIO_PIN_10);
	gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_9|GPIO_PIN_10);
	gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, 
    GPIO_OSPEED_50MHZ,GPIO_PIN_9|GPIO_PIN_10);
	
	/* USART configure */
	usart_deinit(USART0);
	usart_oversample_config(USART0, USART_OVSMOD_8);
	usart_baudrate_set(USART0,baud);			          //波特率
	usart_parity_config(USART0, USART_PM_NONE);		  //校验位:NONE
	usart_word_length_set(USART0, USART_WL_8BIT);		//数据位:8
	usart_stop_bit_set(USART0, USART_STB_1BIT);		  //停止位:1
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
	/* enable USART0 IDLEIE interrupt */
	usart_interrupt_enable(USART0, USART_INT_IDLE);
	/* USART interrupt configuration */
	nvic_irq_enable(USART0_IRQn, 1, 1);
	usart_enable(USART0);
	
	/* USART DMA enable*/
	usart_dma_receive_config(USART0, USART_DENR_ENABLE);
	usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
	
	/*configure DMA0 interrupt*/
	nvic_irq_enable(DMA1_Channel7_IRQn, 0, 0);
    nvic_irq_enable(DMA1_Channel2_IRQn, 0, 1);
	
	dma_single_data_parameter_struct dma_init_struct;
    
	/* deinitialize DMA1 channel7(USART0 tx) */
	dma_single_data_para_struct_init(&dma_init_struct);
	dma_deinit(DMA1, DMA_CH7);
	dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
	dma_init_struct.memory0_addr = (uint32_t)0;
	dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
	dma_init_struct.number = 0;
	dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
	dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
	dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
	
	/* configure DMA mode */
	dma_circulation_disable(DMA1, DMA_CH7);
	dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4);
	/* enable DMA1 channel7 transfer complete interrupt */
	dma_interrupt_enable(DMA1, DMA_CH7, DMA_CHXCTL_FTFIE);
	/* enable DMA1 channel7 */
	dma_channel_enable(DMA1, DMA_CH7);

	/* deinitialize DMA1 channel2 (USART0 rx) */
	dma_deinit(DMA1, DMA_CH2);
	dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
	dma_init_struct.memory0_addr = (uint32_t)RxBuf;
	dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init_struct.number = 128;
	dma_init_struct.periph_addr = (uint32_t)&USART_DATA(USART0);
	dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
	dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
	dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
	
	/* configure DMA mode */
	dma_circulation_disable(DMA1, DMA_CH2);
	dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
	/* enable DMA1 channel2 transfer complete interrupt */
	dma_interrupt_enable(DMA1, DMA_CH2, DMA_CHXCTL_FTFIE);
	/* enable DMA1 channel2 */
	dma_channel_enable(DMA1, DMA_CH2);
}
uint8_t UART0_F450::Write(uint8_t *buf, uint16_t len)
{
	dma_single_data_parameter_struct dma_init_struct;
	
	dma_single_data_para_struct_init(&dma_init_struct);
	dma_deinit(DMA1, DMA_CH7);
	dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
	dma_init_struct.memory0_addr = (uint32_t)buf;
	dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
	dma_init_struct.number = len;
	dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
	dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
	dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
	dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4);
	dma_channel_enable(DMA1, DMA_CH7);
	while(!dma_flag_get(DMA1,DMA_CH7,DMA_FLAG_FTF));
}
uint16_t UART0_F450::GetRxLenth(void)
{
	return 128 - dma_transfer_number_get(DMA1, DMA_CH2);
}
void UART0_F450::ReadResume(void)
{
	dma_single_data_parameter_struct dma_init_struct;
	/* deinitialize DMA channel2 (USART0 rx) */
	dma_deinit(DMA1, DMA_CH2);
	dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
	dma_init_struct.memory0_addr = (uint32_t)RxBuf;
	dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init_struct.number = 128;
	dma_init_struct.periph_addr = (uint32_t)&USART_DATA(USART0);
	dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
	dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
	dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
	/* configure DMA mode */
	dma_circulation_disable(DMA1, DMA_CH2);
	dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
	/* enable DMA channel2 */
	dma_channel_enable(DMA1, DMA_CH2);
}
void UART0_F450::Read(uint8_t *buf, uint16_t *len)
{
	*len = 128 - dma_transfer_number_get(DMA1, DMA_CH2);
	if(*len > 0)
	{
		for(int i = 0; i < *len; i++)
		{
			buf[i] = RxBuf[i];
		}
		dma_single_data_parameter_struct dma_init_struct;
	/* deinitialize DMA channel2 (USART0 rx) */
	dma_deinit(DMA1, DMA_CH2);
	dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
	dma_init_struct.memory0_addr = (uint32_t)RxBuf;
	dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init_struct.number = 128;
	dma_init_struct.periph_addr = (uint32_t)&USART_DATA(USART0);
	dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
	dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
	dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
	/* configure DMA mode */
	dma_circulation_disable(DMA1, DMA_CH2);
	dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
	/* enable DMA channel2 */
	dma_channel_enable(DMA1, DMA_CH2);
	}
}
void UART0_F450::Write(uint16_t len)
{
	dma_single_data_parameter_struct dma_init_struct;
	
	dma_single_data_para_struct_init(&dma_init_struct);
	dma_deinit(DMA1, DMA_CH7);
	dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
	dma_init_struct.memory0_addr = (uint32_t)TxBuf;
	dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
	dma_init_struct.number = len;
	dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
	dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
	dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
	dma_channel_enable(DMA1, DMA_CH7);
	while(RESET == dma_flag_get(DMA1, DMA_CH7, DMA_FLAG_FTF));
}
int16_t UART0_F450::Read(void)
{
	return 1;
}	

UART0_F450.hpp

#pragma once

#include <stdint.h>

#include "Bsp/bsp.hpp"

class UART0_F450: public UART
{
public:
	UART0_F450(){}
	void Init(uint32_t baud = 115200);
	uint8_t Write(uint8_t *buf, uint16_t len);
	void Read(uint8_t *buf, uint16_t *len);
	void Write(uint16_t len);
	void ReadResume(void);
	int16_t Read(void);  
	uint16_t GetRxLenth(void);		
};

main.h

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 __MAIN_H
#define __MAIN_H

//#include "bsp.hpp"
//#include "os.hpp"




//extern BSP *gbsp;
//extern OS *gos;

#ifdef __cplusplus
extern "C" {
#endif

	
/* Includes ------------------------------------------------------------------*/
#include "gd32f4xx.h"
#include "gd32f4xx_gpio.h"
#include "gd32f4xx.h"
#include "gd32f4xx_dma.h"
#include "system_gd32f4xx.h"
#include "gd32f4xx_rcu.h"
 	




	

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
//void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嗨!对于使用GD32F450微控制器的ADC DMA,您可以按照以下步骤进行配置: 1. 首先,确保您已经初始化了ADC和DMA外设,并启用了它们的时钟。 2. 配置ADC的通道和采样时间: - 使用`adc_channel_length`函数设置ADC的通道数。 - 使用`adc_regular_channel_config`函数配置每个通道的采样时间、转换模式和触发源。 3. 配置DMA: - 使用`dma_deinit`函数重置DMA外设。 - 使用`dma_init_struct`结构体配置DMA的通道、数据传输方向、数据宽度和传输模式。 - 使用`dma_mode_config`函数配置DMA的传输模式。 - 使用`dma_circulation_disable`函数禁用DMA的循环传输模式(如果不需要循环传输)。 - 使用`dma_memory_address_config`函数配置DMA的内存地址和外设地址。 - 使用`dma_memory_width_config`函数配置DMA的内存和外设数据宽度。 - 使用`dma_memory_increase_enable`函数启用DMA的内存递增模式。 - 使用`dma_channel_enable`函数启用DMA通道。 4. 启动ADC和DMA: - 使用`adc_software_trigger_enable`函数启动ADC转换。 - 使用`dma_channel_enable`函数启动DMA传输。 5. 在DMA传输完成后,您可以使用DMA的中断或轮询方法来处理接收到的数据。 这是一个基本的ADC DMA配置过程的概述。具体的代码实现可能会有所不同,具体取决于您使用的开发环境和库。确保参考GD32F450微控制器的参考手册和相关的库文档以获取更详细的信息和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值