//主程序
#include "stm32f10x.h"
#include <stdio.h>
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
//定义UART2的发送缓冲器的字节数
#define TxBufferSize1 (countof(TxBuffer1) - 1)
#define RxBufferSize1 (countof(TxBuffer1) - 1)
#define countof(a) (sizeof(a) / sizeof(*(a)))
//定义UART一个结构,用来初始化UART2
USART_InitTypeDef USART_InitStructure;
//UART2将要发送的数据 UART2接收缓冲器大小
uint8_t TxBuffer1[] = "USE USART2 DMA TRAN: USART2 DAM OK ";
uint8_t RxBuffer1[TxBufferSize1];
uint8_t NbrOfDataToRead = TxBufferSize1;
volatile TestStatus TransferStatus1 = FAILED;
/* 内部函数*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void DMA_Configuration(void);
int main(void)
{
// 配置时钟
RCC_Configuration();
// 配置NVIC
//NVIC_Configuration();
//配置GPIO口
GPIO_Configuration();
// 配置DMA
DMA_Configuration();
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 | SART_Mode_Tx;
// 配置 USART2
USART_Init(USART2, &USART_InitStructure);
// 使能USART2 DMA TX 请求
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
// 使能 USART2
USART_Cmd(USART2, ENABLE);
//允许 USART2 RX 中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//启动DMA传送数据
DMA_Cmd(DMA1_Channel7,ENABLE);
while(1){}
}
///////////////////////////////////////////////////////////////////////////
void RCC_Configuration(void)
{
//时钟初始化
SystemInit();
//允许DMA1和DMA2 的时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
//允许APB中 GPIOD,USART2和AFIO 的时钟,
//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOD,
ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,
ENABLE);
}
///////////////////////////////////////////////////////////////////////////
void GPIO_Configuration(void)
{ GPIO_InitTypeDef GPIO_InitStructure;
// 配置USART2的REMAP 功能;
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
//配置 SART2 RX 为浮空
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
///配置 SART2 TX 为推挽
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
///////////////////////////////////////////////////////////////////////////
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 允许 USART2 中断 */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
///////////////////////////////////////////////////////////////////////////
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* USART2_Tx_DMA_Channel (triggered by USART2 Tx event) Config */
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40004404;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据是8BIT
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器数据是8BIT
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//循环模式,发完就停
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//禁止存储器与存储器的传输
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
}
///////////////////////////////////////////////////////////////////
//主程序完成
//USART2中断向量
//在stm32f10x_it.c文件中加入下面代码
//USART2中断要的数据
uint8_t RxCounter = 0;
extern uint8_t RxBuffer1[];
extern uint8_t NbrOfDataToRead;
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* 串口助理发送回的数据放入接收缓冲区*/
RxBuffer1[RxCounter++] = USART_ReceiveData(USART2);
if(RxCounter == NbrOfDataToRead)
{
/* 接收完就终止接收中断*/
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
}
}
/*通过串口助理,将STM32发送过来的数据转发回去,看到DMA发数据正常,但STM在接收时进不了中断,从仿真器上看到,USART2_DR寄存器可以接收到最后一个字,但USART_SR的RXNE位一直为0 ,*/ |