- #include "stm32f10x.h"
- #include "stdio.h"
- #include "string.h"
- #define PRINTF_ON 1
- #define BufferSize 32
- vu16 LeftDataCounter;
- vu32 Tick;
- uc32 SRC_Const_Buffer[BufferSize] =
- {
- 0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,
- 0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
- 0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
- 0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
- 0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
- 0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
- 0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
- 0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80
- };
- u32 DST_Buffer[BufferSize];
- u8 i=0,DMASpendTime=0,CPUSpendTime=0;
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void USART_Configuration(void);
- void DMA_Configuration(void);
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- USART_Configuration();
- DMA_Configuration();
- SysTick_Config(72);
- Tick = 0;
- while(i<BufferSize)
- {
- DST_Buffer[i]=SRC_Const_Buffer[i];
- CPUSpendTime = Tick;
- i++;
- }
- i=0;
- while(i<BufferSize)
- {
- DST_Buffer[i]=0;
- i++;
- }
- Tick = 0;
- DMA_Cmd(DMA1_Channel6,ENABLE);
- while(LeftDataCounter != 0); //等待传输完成
- DMASpendTime = Tick;
-
-
- if(strncmp((const char *)SRC_Const_Buffer,(const char *)DST_Buffer,BufferSize) ==0)
- {
- printf("\r\n Transmit Success! \r\n");
- }else{
- printf("\r\n Transmit Fail! \r\n");
- }
- printf("\r\n the CPU spend : %dus! \r\n",CPUSpendTime);
- printf("\r\n the DMA spend : %dus! \r\n",DMASpendTime);
- }
- void DMA_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- DMA_DeInit(DMA1_Channel6);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32) SRC_Const_Buffer;
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32) DST_Buffer;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = BufferSize;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;
- DMA_Init(DMA1_Channel6,&DMA_InitStructure);
- DMA_ITConfig(DMA1_Channel6,DMA_IT_TC,ENABLE);
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- }
- void RCC_Configuration(void)
- {
- /* 定义枚举类型变量 HSEStartUpStatus */
- ErrorStatus HSEStartUpStatus;
- /* 复位系统时钟设置*/
- RCC_DeInit();
- /* 开启HSE*/
- RCC_HSEConfig(RCC_HSE_ON);
- /* 等待HSE起振并稳定*/
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- /* 判断HSE起是否振成功,是则进入if()内部 */
- if(HSEStartUpStatus == SUCCESS)
- {
- /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* 设置FLASH延时周期数为2 */
- FLASH_SetLatency(FLASH_Latency_2);
- /* 使能FLASH预取缓存 */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* 使能PLL */
- RCC_PLLCmd(ENABLE);
- /* 等待PLL输出稳定 */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* 选择SYSCLK时钟源为PLL */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* 等待PLL成为SYSCLK时钟源 */
- while(RCC_GetSYSCLKSource() != 0x08);
- }
- /* 打开APB2总线上的GPIOA时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP|RCC_APB1Periph_WWDG, ENABLE);
-
- }
-
- void USART_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- USART_ClockInit(USART1 , &USART_ClockInitStructure);
- USART_InitStructure.USART_BaudRate = 9600;
- 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_Cmd(USART1,ENABLE);
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- #if PRINTF_ON
- int fputc(int ch,FILE *f)
- {
- USART_SendData(USART1,(u8) ch);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
- return ch;
- }
- #endif