/* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/ typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/ #define USART1_DR_Base 0x40013804 #define USART2_DR_Base 0x40004404 #define TxBufferSize1 (countof(TxBuffer1) - 1) #define TxBufferSize2 (countof(TxBuffer2) - 1)
/* Private macro -------------------------------------------------------------*/ #define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/ USART_InitTypeDef USART_InitStructure; u8 TxBuffer1[] = "123456789"; u8 TxBuffer2[] = "USART Example 6: USART2 -> USART1 using DMA Tx and Rx Interrupt";
u8 RxBuffer1[TxBufferSize2]; u8 RxBuffer2[TxBufferSize1]; u8 RxBuffer[4];
u8 NbrOfDataToRead = TxBufferSize1; u8 index = 0,dmaflag=0,i; u16 CurrDataCounter_End = 0,CurrDataCounter_End1,err,err1,err2,err3; volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED; ErrorStatus HSEStartUpStatus1; DMA_InitTypeDef DMA_InitStructure,DMA_InitStructure1; /* Private function prototypes -----------------------------------------------*/ extern void SetupUSART(void); extern int GetKey (void) ; extern void delay (int cnt) ; void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void DMA_Configuration(void); TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);
int main(void) { #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration();
/* Configure the GPIO ports */ GPIO_Configuration(); /* DMA Channel4 (triggered by USART1 Tx event) Config */ DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)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; 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_Channel4, &DMA_InitStructure);
DMA_DeInit(DMA1_Channel5); DMA_InitStructure1.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure1.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure1.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure1.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure1.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure1.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure1.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure1.DMA_M2M = DMA_M2M_Disable; DMA_InitStructure1.DMA_MemoryBaseAddr = (u32)&RxBuffer; DMA_InitStructure1.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure1.DMA_BufferSize = 4; DMA_Init(DMA1_Channel5, &DMA_InitStructure1);
SetupUSART(); /* Enable USART1 DMA TX request */ USART_DMACmd(USART1, USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE); /* Enable DMA Channel5 Transfer Complete interrupt */ DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); /* Enable the USART1 Receive Interrupt */ //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable DMA Channel4 */ // DMA_Cmd(DMA1_Channel4, ENABLE); /* Enable DMA Channel5 */ DMA_Cmd(DMA1_Channel5, ENABLE);
CurrDataCounter_End = DMA_GetCurrDataCounter(DMA1_Channel5);
/* Wait until DMA_Channel 4 Transfer Complete */ while(1) { if(dmaflag == 1) { /*USART_Cmd(USART1, DISABLE); delay(100); DMA_Cmd(DMA1_Channel5, ENABLE); DMA_ClearITPendingBit(DMA1_IT_GL5); USART_Cmd(USART1, ENABLE); if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==1) GetKey(); dmaflag =0;*/ index = 9; } } } void DMAChannel5_IRQHandler(void) { if(DMA_GetITStatus(DMA1_IT_TC5)) { DMA_ClearITPendingBit(DMA1_IT_GL5); CurrDataCounter_End = DMA_GetCurrDataCounter(DMA1_Channel5);
USART_Cmd(USART1, DISABLE); USART_Cmd(USART1, ENABLE);
DMA_DeInit(DMA1_Channel5); DMA_InitStructure1.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure1.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure1.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure1.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure1.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure1.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure1.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure1.DMA_M2M = DMA_M2M_Disable; DMA_InitStructure1.DMA_MemoryBaseAddr = (u32)&RxBuffer; DMA_InitStructure1.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure1.DMA_BufferSize = 4; DMA_Init(DMA1_Channel5, &DMA_InitStructure1); /*USART_DeInit(USART1); SetupUSART(); USART_ClearFlag(USART1, USART_FLAG_RXNE);*/ USART_DMACmd(USART1, USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE);; DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE); if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==1) GetKey(); DMA_Cmd(DMA1_Channel5, ENABLE); DMA_ClearITPendingBit(DMA1_IT_GL5); dmaflag =1; for(i=0;i<5;i++) RxBuffer1=0;
} } |