找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2831|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

STM32學(xué)習(xí)-DMA測試

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:81272 發(fā)表于 2015-5-27 16:52 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
完整代碼下載: DMA測試.RAR (122 KB, 下載次數(shù): 10)
  1. /*******************************************************************************

  2. STM32學(xué)習(xí)日志(8)----DMA測試

  3. 編譯環(huán)境:        EWARM V5.30
  4. 硬件環(huán)境:        南京萬利 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的任意數(shù)

  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. // 時鐘設(shè)置初始化
  71. //******************************************************************************
  72. static void RCC_Configuration(void)
  73. {
  74.   ErrorStatus HSEStartUpStatus;
  75. /*
  76. RCC_AdjustHSICalibrationValue 調(diào)整內(nèi)部高速晶振(HSI)校準(zhǔn)值
  77. RCC_ITConfig 使能或者失能指定的RCC中斷
  78. RCC_ClearFlag 清除RCC的復(fù)位標(biāo)志位
  79. RCC_GetITStatus 檢查指定的RCC中斷發(fā)生與否
  80. RCC_ClearITPendingBit 清除RCC的中斷待處理位
  81. */
  82.           /* RCC system reset(for debug purpose) */
  83.           // 時鐘系統(tǒng)復(fù)位
  84.           RCC_DeInit();

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

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

  91.         // 等待HSE起振
  92.         // 該函數(shù)將等待直到HSE就緒,或者在超時的情況下退出
  93.           /* Wait till HSE is ready */
  94.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  95.           if(HSEStartUpStatus == SUCCESS)
  96.           {
  97.             /* HCLK = SYSCLK */
  98.                 // 設(shè)置AHB時鐘(HCLK)
  99.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  100.             /* PCLK1 = HCLK/2 */
  101.                 // 設(shè)置低速AHB時鐘(PCLK1)
  102.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  103.             /* PCLK2 = HCLK */
  104.                 // 設(shè)置高速AHB時鐘(PCLK2)
  105.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  106.             /* ADCCLK = PCLK2/8 */
  107.                 // 設(shè)置ADC時鐘(ADCCLK)
  108.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  109.                 // 設(shè)置USB時鐘(USBCLK)
  110.                 // USB時鐘 = PLL時鐘除以1.5
  111.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

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

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

  117.                 // 設(shè)置RTC時鐘(RTCCLK)
  118.                 // 選擇HSE時鐘頻率除以128作為RTC時鐘
  119.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  120.                 // 使能或者失能RTC時鐘
  121.                 // RTC時鐘的新狀態(tài)
  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.                 // 設(shè)置PLL時鐘源及倍頻系數(shù)
  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標(biāo)志位設(shè)置與否
  135.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  136.             {
  137.             }

  138.             /* Select PLL as system clock source */
  139.                 // 設(shè)置系統(tǒng)時鐘(SYSCLK)
  140.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  141.             /* Wait till PLL is used as system clock source */
  142.                 // 返回用作系統(tǒng)時鐘的時鐘源
  143.             while(RCC_GetSYSCLKSource() != 0x08)
  144.             {
  145.             }
  146.   }

  147.         // 使能或者失能AHB外設(shè)時鐘
  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外設(shè)時鐘
  157.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  158.         // 強(qiáng)制或者釋放高速APB(APB2)外設(shè)復(fù)位
  159.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  160.         // 退出復(fù)位狀態(tài)
  161.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  162.         // 強(qiáng)制或者釋放低速APB(APB1)外設(shè)復(fù)位
  163.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);

  164.         // 強(qiáng)制或者釋放后備域復(fù)位
  165.         RCC_BackupResetCmd(ENABLE);

  166.         // 使能或者失能時鐘安全系統(tǒng)
  167.         RCC_ClockSecuritySystemCmd(DISABLE);
  168. }
  169. //******************************************************************************
  170. // NVIC設(shè)置
  171. //******************************************************************************
  172. static void NVIC_Configuration(void)
  173. {
  174. }
  175. //******************************************************************************
  176. // SysTick設(shè)置初始化
  177. //******************************************************************************
  178. static void SysTick_Config1(void)
  179. {
  180.         #define SystemFreq                72000000.0        // 單位為Hz
  181.         #define TB_SysTick                100000.0                // 單位為uS,與示波器實(shí)測一致

  182.         INT32U ticks;

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

  193.         // 使能或者失能APB2外設(shè)時鐘
  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.         // 注意,當(dāng)程序需要多次使用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. */
復(fù)制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表