找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2824|回复: 0
收起左侧

STM32学习-DMA测试

[复制链接]
ID:81272 发表于 2015-5-27 16:52 | 显示全部楼层 |阅读模式
完整代码下载: DMA测试.RAR (122 KB, 下载次数: 10)
  1. /*******************************************************************************

  2. STM32学习日志(8)----DMA测试

  3. 编译环境:        EWARM V5.30
  4. 硬件环境:        南京万利 EK-STM32F
  5. 主芯片        :        STM32F103VBT6
  6. STM32 FW:   V3.0.0
  7. 作者        :        szlihongtao
  8. 时间        :          2010-06-30
  9. *******************************************************************************/
  10. /**
  11.   ******************************************************************************
  12.   * @file    Project/Template/main.c
  13.   * @author  MCD Application Team
  14.   * @version V3.0.0
  15.   * @date    04/06/2009
  16.   * @brief   Main program body
  17.   ******************************************************************************
  18.   * @copy
  19.   *
  20.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  21.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  22.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  23.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  24.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  25.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  26.   *
  27.   * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
  28.   */

  29. /* Includes ------------------------------------------------------------------*/
  30. #include "stm32f10x.h"
  31. #include "stm32_m.h"

  32. /** @addtogroup Template_Project
  33.   * @{
  34.   */

  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. /* Private function prototypes -----------------------------------------------*/
  40. /* Private functions ---------------------------------------------------------*/
  41. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  42. #define BufferSize  47                        // >=32的任意数

  43. uc32 SRC_Const_Buffer[BufferSize]= {0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,
  44.                                     0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
  45.                                     0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
  46.                                     0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
  47.                                     0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
  48.                                     0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
  49.                                     0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
  50.                                     0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80};
  51. u32 DST_Buffer[BufferSize];
  52. INT16U tmr_test;
  53. //******************************************************************************
  54. // 延时50ms
  55. //******************************************************************************
  56. static void delay(void)
  57. {
  58.         INT32U i;
  59.     static INT32U jjj=5240*50;

  60.         for (i=0; i<jjj; i++);
  61. }
  62. //******************************************************************************
  63. static void delayms(INT16U cnt)
  64. {
  65.         INT16U i;

  66.         while(cnt--)
  67.                 for (i=0; i<7333; i++);
  68. }
  69. //******************************************************************************
  70. // 时钟设置初始化
  71. //******************************************************************************
  72. static void RCC_Configuration(void)
  73. {
  74.   ErrorStatus HSEStartUpStatus;
  75. /*
  76. RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
  77. RCC_ITConfig 使能或者失能指定的RCC中断
  78. RCC_ClearFlag 清除RCC的复位标志位
  79. RCC_GetITStatus 检查指定的RCC中断发生与否
  80. RCC_ClearITPendingBit 清除RCC的中断待处理位
  81. */
  82.           /* RCC system reset(for debug purpose) */
  83.           // 时钟系统复位
  84.           RCC_DeInit();

  85.         // 使能外部的8M晶振
  86.         // 设置外部高速晶振(HSE)
  87.           /* Enable HSE */
  88.           RCC_HSEConfig(RCC_HSE_ON);

  89.         // 使能或者失能内部高速晶振(HSI)
  90.         RCC_HSICmd(DISABLE);

  91.         // 等待HSE起振
  92.         // 该函数将等待直到HSE就绪,或者在超时的情况下退出
  93.           /* Wait till HSE is ready */
  94.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  95.           if(HSEStartUpStatus == SUCCESS)
  96.           {
  97.             /* HCLK = SYSCLK */
  98.                 // 设置AHB时钟(HCLK)
  99.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  100.             /* PCLK1 = HCLK/2 */
  101.                 // 设置低速AHB时钟(PCLK1)
  102.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  103.             /* PCLK2 = HCLK */
  104.                 // 设置高速AHB时钟(PCLK2)
  105.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  106.             /* ADCCLK = PCLK2/8 */
  107.                 // 设置ADC时钟(ADCCLK)
  108.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  109.                 // 设置USB时钟(USBCLK)
  110.                 // USB时钟 = PLL时钟除以1.5
  111.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  112.                 // 设置外部低速晶振(LSE)
  113.                 RCC_LSEConfig(RCC_LSE_OFF);

  114.                 // 使能或者失能内部低速晶振(LSI)
  115.                 // LSE晶振OFF
  116.                 RCC_LSICmd(DISABLE);

  117.                 // 设置RTC时钟(RTCCLK)
  118.                 // 选择HSE时钟频率除以128作为RTC时钟
  119.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  120.                 // 使能或者失能RTC时钟
  121.                 // RTC时钟的新状态
  122.                 RCC_RTCCLKCmd(DISABLE);

  123.             /* Flash 2 wait state */
  124.             FLASH_SetLatency(FLASH_Latency_2);

  125.             /* Enable Prefetch Buffer */
  126.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  127.             /* PLLCLK = 8MHz * 9 = 72 MHz */
  128.                 // 设置PLL时钟源及倍频系数
  129.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  130.             /* Enable PLL */
  131.                 // 使能或者失能PLL
  132.             RCC_PLLCmd(ENABLE);

  133.             /* Wait till PLL is ready */
  134.                 // 检查指定的RCC标志位设置与否
  135.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  136.             {
  137.             }

  138.             /* Select PLL as system clock source */
  139.                 // 设置系统时钟(SYSCLK)
  140.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  141.             /* Wait till PLL is used as system clock source */
  142.                 // 返回用作系统时钟的时钟源
  143.             while(RCC_GetSYSCLKSource() != 0x08)
  144.             {
  145.             }
  146.   }

  147.         // 使能或者失能AHB外设时钟
  148.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
  149.                                                         |RCC_AHBPeriph_DMA2
  150.                                                         |RCC_AHBPeriph_SRAM
  151.                                                         |RCC_AHBPeriph_FLITF
  152.                                                         |RCC_AHBPeriph_CRC
  153.                                                         |RCC_AHBPeriph_FSMC
  154.                                                         |RCC_AHBPeriph_SDIO,DISABLE);
  155.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);// DMA1时钟使能

  156.         // 使能或者失能APB1外设时钟
  157.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  158.         // 强制或者释放高速APB(APB2)外设复位
  159.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  160.         // 退出复位状态
  161.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  162.         // 强制或者释放低速APB(APB1)外设复位
  163.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);

  164.         // 强制或者释放后备域复位
  165.         RCC_BackupResetCmd(ENABLE);

  166.         // 使能或者失能时钟安全系统
  167.         RCC_ClockSecuritySystemCmd(DISABLE);
  168. }
  169. //******************************************************************************
  170. // NVIC设置
  171. //******************************************************************************
  172. static void NVIC_Configuration(void)
  173. {
  174. }
  175. //******************************************************************************
  176. // SysTick设置初始化
  177. //******************************************************************************
  178. static void SysTick_Config1(void)
  179. {
  180.         #define SystemFreq                72000000.0        // 单位为Hz
  181.         #define TB_SysTick                100000.0                // 单位为uS,与示波器实测一致

  182.         INT32U ticks;

  183.         ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
  184.         ticks=ticks;
  185.         //SysTick_Config(ticks);
  186. }
  187. //******************************************************************************
  188. // GPIO设置
  189. //******************************************************************************
  190. static void GPIO_Configuration(void)
  191. {
  192.         GPIO_InitTypeDef GPIO_InitStructure;

  193.         // 使能或者失能APB2外设时钟
  194.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);

  195.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  196.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  197.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  198.           GPIO_Init(GPIOC, &GPIO_InitStructure);

  199.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
  200.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  201.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  202.           GPIO_Init(GPIOD, &GPIO_InitStructure);
  203. }
  204. //******************************************************************************
  205. // LED5亮
  206. //******************************************************************************
  207. void Led_RW_ON(void)
  208. {
  209.           GPIO_SetBits(GPIOC,GPIO_Pin_4);
  210. }
  211. //******************************************************************************
  212. // LED5灭
  213. //******************************************************************************
  214. void Led_RW_OFF(void)
  215. {
  216.           GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  217. }
  218. //******************************************************************************
  219. static void DMA_Configuration(void)
  220. {
  221.         DMA_InitTypeDef DMA_InitStructure;

  222.         // 注意,当程序需要多次使用dma功能的时候,下面的语句必须添加,否则,第2次的DMA无法完成
  223. #if 0
  224.           DMA_DeInit(DMA1_Channel1);
  225. #else
  226.           DMA_Cmd(DMA1_Channel1, DISABLE);
  227. #endif

  228.           DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SRC_Const_Buffer;
  229.           DMA_InitStructure.DMA_MemoryBaseAddr = (u32)DST_Buffer;
  230.           DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  231.           DMA_InitStructure.DMA_BufferSize = BufferSize;
  232.           DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
  233.           DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  234.           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  235.           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  236.           DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  237.           DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  238.           DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;
  239.           DMA_Init(DMA1_Channel1, &DMA_InitStructure);

  240.           /* Enable DMA Channel6 Transfer Complete interrupt */
  241.           DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, DISABLE);

  242.           /* Get Current Data Counter value before transfer begins */
  243.           //CurrDataCounter_Begin = DMA_GetCurrDataCounter(DMA1_Channel1);

  244.         DMA_ClearFlag(DMA1_FLAG_TC1);
  245. }
  246. /*******************************************************************************
  247. * Function Name  : Buffercmp
  248. * Description    : Compares two buffers.
  249. * Input          : - pBuffer, pBuffer1: buffers to be compared.
  250. *                : - BufferLength: buffer's length
  251. * Output         : None
  252. * Return         : PASSED: pBuffer identical to pBuffer1
  253. *                  FAILED: pBuffer differs from pBuffer1
  254. *******************************************************************************/
  255. TestStatus Buffercmp(uc32* pBuffer, u32* pBuffer1, u16 BufferLength)
  256. {
  257.   while (BufferLength--)
  258.   {
  259.             if (*pBuffer++ != *pBuffer1++)
  260.                       return FAILED;
  261.   }

  262.   return PASSED;
  263. }
  264. //******************************************************************************
  265. // 主程序
  266. //******************************************************************************
  267. int main(void)
  268. {
  269.         INT16U i;

  270.         RCC_Configuration();
  271.           GPIO_Configuration();
  272.         NVIC_Configuration();
  273.           SysTick_Config1();
  274.         delay();
  275. //------------------------------------------------------------------------------
  276.         for (;;)
  277.         {
  278.                 for (i=0;i<BufferSize;++i)
  279.                         DST_Buffer[i]=0;

  280.                 DMA_Configuration();
  281.                   DMA_Cmd(DMA1_Channel1, ENABLE);

  282.                 while((DMA_GetFlagStatus(DMA1_IT_TC1))==        RESET); // 等待DMA传送完成

  283.                   if (PASSED == Buffercmp(SRC_Const_Buffer, DST_Buffer, BufferSize))
  284.                           Led_RW_ON();                                // led亮表示OK
  285.                 else
  286.                           Led_RW_OFF();

  287.                 GPIOC->ODR ^= GPIO_Pin_7;                // led2 toogle
  288.                 delayms(80);
  289.         }
  290. //------------------------------------------------------------------------------
  291. }
  292. //******************************************************************************
  293. #ifdef  USE_FULL_ASSERT

  294. /**
  295.   * @brief  Reports the name of the source file and the source line number
  296.   *   where the assert_param error has occurred.
  297.   * @param file: pointer to the source file name
  298.   * @param line: assert_param error line source number
  299.   * @retval : None
  300.   */
  301. void assert_failed(uint8_t* file, uint32_t line)
  302. {
  303.   /* User can add his own implementation to report the file name and line number,
  304.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  305.   /* Infinite loop */
  306.   while (1)
  307.   {
  308.   }
  309. }
  310. #endif

  311. /**
  312.   * @}
  313.   */
  314. //******************************************************************************
  315. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  316. //******************************************************************************
  317. /*
  318.         LED2---------PC7
  319.         LED3---------PC6
  320.         LED4---------PC5
  321.         LED5---------PC4

  322.         KEY2---------PD3
  323.         KEY3---------PD4
  324. */
复制代码



回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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