OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2418|回复: 0

STM32F030 UART DMA发送与接收

[复制链接]

50

主题

1805

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
6662
金钱
6662
注册时间
2016-5-29
在线时间
910 小时
发表于 2020-1-15 09:32:33 | 显示全部楼层 |阅读模式
STM32F030  UART DMA发送与接收  
初始化程序与F1有点不同,走了一些弯路.
#include "stm32f0xx.h"
#include "UART.h"
#include <string.h>
#include "LCD.h"
typedef  unsigned char uc;
typedef  unsigned int  ui;
typedef unsigned short us;
#define vd void
#define U1L 4
#define RXLEN 32
#define MAXSENDCNT  20
uc uart2rxBuf[20];
uc uart2RXBuf[20];
ui uart2RXLen = 0;
ui uart2rxLen = 0;

uc uart2SendBuf[24];
ui uart2SendCount = 0;
uc USART_TX_Buffer[U1L][U1SB_LEN];
uc USART_RX_Buffer[RXLEN];
uc uartRecBuf[RXLEN];void uart_nvic_init(void)
{
NVIC_InitTypeDef NVIC_InitStruct;

NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPriority = 1;
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPriority = 2;
NVIC_Init(&NVIC_InitStruct);

}
vd uart_init(vd)
{
   GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_USART1Tx|SYSCFG_DMARemap_USART1Rx,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ClearFlag(USART1,USART_FLAG_TC);

DMA_DeInit(DMA1_Channel4); // DMA1 通道4
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->TDR);;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART_TX_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = U1SB_LEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
//===========================================================================================//
DMA_DeInit(DMA1_Channel5); // DMA1 通道5
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->RDR); ; //USART1->RDR
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART_RX_Buffer; // (u32)USART_RX_Buffer   
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //usart3 rx_dr:destination of data
DMA_InitStructure.DMA_BufferSize = RXLEN; //  this is DMA1_Channel3->CNDTR;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_ClearFlag(DMA1_FLAG_GL4);
DMA_ClearFlag(DMA1_FLAG_GL5);

USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
DMA_Cmd(DMA1_Channel4,DISABLE);
DMA_Cmd(DMA1_Channel5,ENABLE);

uart_nvic_init();

//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); // 串口空闲中断
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE); //DMA 完成中断
USART_Cmd(USART1,ENABLE);
}

void USART1_IRQHandler(void)
{
ui len = 0;
if (USART1->ISR & (1<<4))
{

  USART1->ICR |= 1<<4;
  
  DMA_Cmd(DMA1_Channel5,DISABLE);
  len = RXLEN - DMA1_Channel5->CNDTR;

  if (len > 0)
  {
   memcpy(uartRecBuf,USART_RX_Buffer,len);
   u1sb.rxlen = len;
   ev.uartRxOk = 1;
   DMA1_Channel5->CNDTR = RXLEN;
  }
  
  DMA_Cmd(DMA1_Channel5,ENABLE);
}
}


void DMA1_Channel4_5_IRQHandler(void)
{
if (DMA_GetFlagStatus(DMA1_FLAG_GL4))
{
  DMA_ClearFlag(DMA1_FLAG_GL4);
  DMA_Cmd(DMA1_Channel4,DISABLE);
  ev.dam_wait_send_finish = 0;
}
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-19 16:52

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表