完整代码下载:
DMA测试.RAR
(122 KB, 下载次数: 10)
- /*******************************************************************************
- STM32学习日志(8)----DMA测试
- 编译环境: EWARM V5.30
- 硬件环境: 南京万利 EK-STM32F
- 主芯片 : STM32F103VBT6
- STM32 FW: V3.0.0
- 作者 : szlihongtao
- 时间 : 2010-06-30
- *******************************************************************************/
- /**
- ******************************************************************************
- * @file Project/Template/main.c
- * @author MCD Application Team
- * @version V3.0.0
- * @date 04/06/2009
- * @brief Main program body
- ******************************************************************************
- * @copy
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "stm32_m.h"
- /** @addtogroup Template_Project
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
- #define BufferSize 47 // >=32的任意数
- 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];
- INT16U tmr_test;
- //******************************************************************************
- // 延时50ms
- //******************************************************************************
- static void delay(void)
- {
- INT32U i;
- static INT32U jjj=5240*50;
- for (i=0; i<jjj; i++);
- }
- //******************************************************************************
- static void delayms(INT16U cnt)
- {
- INT16U i;
- while(cnt--)
- for (i=0; i<7333; i++);
- }
- //******************************************************************************
- // 时钟设置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
- RCC_ITConfig 使能或者失能指定的RCC中断
- RCC_ClearFlag 清除RCC的复位标志位
- RCC_GetITStatus 检查指定的RCC中断发生与否
- RCC_ClearITPendingBit 清除RCC的中断待处理位
- */
- /* RCC system reset(for debug purpose) */
- // 时钟系统复位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 设置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能内部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 该函数将等待直到HSE就绪,或者在超时的情况下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 设置AHB时钟(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 设置低速AHB时钟(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 设置高速AHB时钟(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 设置ADC时钟(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 设置USB时钟(USBCLK)
- // USB时钟 = PLL时钟除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 设置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能内部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 设置RTC时钟(RTCCLK)
- // 选择HSE时钟频率除以128作为RTC时钟
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC时钟
- // RTC时钟的新状态
- RCC_RTCCLKCmd(DISABLE);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- // 设置PLL时钟源及倍频系数
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 检查指定的RCC标志位设置与否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 设置系统时钟(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系统时钟的时钟源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 使能或者失能AHB外设时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);// DMA1时钟使能
- // 使能或者失能APB1外设时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 强制或者释放高速APB(APB2)外设复位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出复位状态
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- // 强制或者释放低速APB(APB1)外设复位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 强制或者释放后备域复位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能时钟安全系统
- RCC_ClockSecuritySystemCmd(DISABLE);
- }
- //******************************************************************************
- // NVIC设置
- //******************************************************************************
- static void NVIC_Configuration(void)
- {
- }
- //******************************************************************************
- // SysTick设置初始化
- //******************************************************************************
- static void SysTick_Config1(void)
- {
- #define SystemFreq 72000000.0 // 单位为Hz
- #define TB_SysTick 100000.0 // 单位为uS,与示波器实测一致
- INT32U ticks;
- ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
- ticks=ticks;
- //SysTick_Config(ticks);
- }
- //******************************************************************************
- // GPIO设置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能或者失能APB2外设时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- //******************************************************************************
- // LED5亮
- //******************************************************************************
- void Led_RW_ON(void)
- {
- GPIO_SetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // LED5灭
- //******************************************************************************
- void Led_RW_OFF(void)
- {
- GPIO_ResetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- static void DMA_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- // 注意,当程序需要多次使用dma功能的时候,下面的语句必须添加,否则,第2次的DMA无法完成
- #if 0
- DMA_DeInit(DMA1_Channel1);
- #else
- DMA_Cmd(DMA1_Channel1, DISABLE);
- #endif
- 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_Channel1, &DMA_InitStructure);
- /* Enable DMA Channel6 Transfer Complete interrupt */
- DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, DISABLE);
- /* Get Current Data Counter value before transfer begins */
- //CurrDataCounter_Begin = DMA_GetCurrDataCounter(DMA1_Channel1);
- DMA_ClearFlag(DMA1_FLAG_TC1);
- }
- /*******************************************************************************
- * Function Name : Buffercmp
- * Description : Compares two buffers.
- * Input : - pBuffer, pBuffer1: buffers to be compared.
- * : - BufferLength: buffer's length
- * Output : None
- * Return : PASSED: pBuffer identical to pBuffer1
- * FAILED: pBuffer differs from pBuffer1
- *******************************************************************************/
- TestStatus Buffercmp(uc32* pBuffer, u32* pBuffer1, u16 BufferLength)
- {
- while (BufferLength--)
- {
- if (*pBuffer++ != *pBuffer1++)
- return FAILED;
- }
- return PASSED;
- }
- //******************************************************************************
- // 主程序
- //******************************************************************************
- int main(void)
- {
- INT16U i;
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- SysTick_Config1();
- delay();
- //------------------------------------------------------------------------------
- for (;;)
- {
- for (i=0;i<BufferSize;++i)
- DST_Buffer[i]=0;
- DMA_Configuration();
- DMA_Cmd(DMA1_Channel1, ENABLE);
- while((DMA_GetFlagStatus(DMA1_IT_TC1))== RESET); // 等待DMA传送完成
- if (PASSED == Buffercmp(SRC_Const_Buffer, DST_Buffer, BufferSize))
- Led_RW_ON(); // led亮表示OK
- else
- Led_RW_OFF();
- GPIOC->ODR ^= GPIO_Pin_7; // led2 toogle
- delayms(80);
- }
- //------------------------------------------------------------------------------
- }
- //******************************************************************************
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- //******************************************************************************
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
- //******************************************************************************
- /*
- LED2---------PC7
- LED3---------PC6
- LED4---------PC5
- LED5---------PC4
- KEY2---------PD3
- KEY3---------PD4
- */
复制代码
|