[DemoCode下载] M451的SPI Flash DMA操作

[复制链接]
 楼主| xuanhuanzi 发表于 2019-4-30 21:24 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Date: 15/09/02 10:03a $
  5. * @brief
  6. *           NuEdu-SDK-M451 SPI Flash with PDMA sample code.
  7.                                                  Use PDMA Channel 11 to transfer Data from Memmory to SPI Flash
  8.                                                  and PDMA Channel 10 to receive Data from SPI Flash to Memmory.
  9. *           
  10. * @note
  11. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  12. *
  13. ******************************************************************************/
  14. #include <stdio.h>
  15. #include "M451Series.h"
  16. #include "NuEdu-Basic01.h"

  17. #define PLL_CLOCK           72000000
  18. #define TEST_NUMBER                                 10 /* page numbers */
  19. #define        TEST_LENGTH                                        255        /* length */
  20. #define SPI_TX_DMA_CH                         1
  21. #define SPI_RX_DMA_CH                         2
  22. unsigned char        SrcArray[256];
  23. unsigned char DestArray[256];

  24. void UART0_Init(void)
  25. {
  26.     /* Enable IP clock */
  27.     CLK_EnableModuleClock(UART0_MODULE);

  28.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  29.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  30.       /*---------------------------------------------------------------------------------------------------------*/
  31.     /* Init I/O Multi-function                                                                                 */
  32.     /*---------------------------------------------------------------------------------------------------------*/
  33.     /* Set PD multi-function pins for UART0 RXD(PD.6) and TXD(PD.1) */
  34.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD6MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  35.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD6MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Init UART                                                                                               */
  38.     /*---------------------------------------------------------------------------------------------------------*/
  39.     /* Reset UART module */
  40.     SYS_ResetModule(UART0_RST);

  41.     /* Configure UART0 and set UART0 baud rate */
  42.     UART_Open(UART0, 115200);
  43. }


  44. /*---------------------------------------------------------------------------------------------------------*/
  45. /*  Main Function                                                                                          */
  46. /*---------------------------------------------------------------------------------------------------------*/
  47. int32_t main(void)
  48. {
  49.     unsigned int u32ByteCount;
  50.     unsigned int u32PageNumber;
  51.     unsigned int u32ProgramFlashAddress = 0;
  52.     unsigned int u32VerifyFlashAddress = 0;
  53.     unsigned int MidDid;

  54.     /* Initial system */
  55.     SYS_Init();

  56.     /* Init UART0 to 115200-8n1 for print message */
  57.     UART0_Init();

  58.     printf("\nHello World.\n");
  59.     printf("PLL Clock = %d Hz\n", CLK_GetPLLClockFreq());
  60.     printf("Core Clock = %d Hz\n\n", CLK_GetHCLKFreq());
  61.     printf("+-------------------------------------------------------+\n");
  62.     printf("|        M451 Series SPI_Flash_w_PDMA Sample Code       |\n");
  63.     printf("+-------------------------------------------------------+\n");

  64.     /* Open 7-Seg */
  65.     Open_Seven_Segment();

  66.     /* Open SPI for Serial Flash */
  67.     Open_SPI_Flash();

  68.     /* Enable IP clock */
  69.     CLK_EnableModuleClock(PDMA_MODULE);
  70.     /* Reset PDMA module */
  71.     SYS_ResetModule(PDMA_RST);

  72.     /* Read MID & DID */
  73.     MidDid = SpiFlash_w_PDMA_ReadMidDid();
  74.     printf("\nMID and DID = %x", MidDid);

  75.     /* Erase SPI Flash */
  76.     SpiFlash_w_PDMA_ChipErase();
  77.     printf("\nFlash Erasing... ");       
  78.        
  79.     /* Wait ready */
  80.     SpiFlash_w_PDMA_WaitReady();
  81.     printf("Done!");

  82.     /* Fill the Source Data and clear Destination Data Buffer */
  83.     for(u32ByteCount=0; u32ByteCount<256; u32ByteCount++){
  84.         SrcArray[u32ByteCount] = u32ByteCount;
  85.         DestArray[u32ByteCount] = 0;
  86.     }
  87.         
  88.     u32ProgramFlashAddress = 0;
  89.     u32VerifyFlashAddress = 0;
  90.     for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++){
  91.         printf("\n\nTest Page Number = %d", u32PageNumber);
  92.         Show_Seven_Segment(u32PageNumber,1);
  93.         CLK_SysTickDelay(200000);

  94.         /*=== Program SPI Flash ===*/       
  95.         printf("\nFlash Programming... ");

  96.         /* Page Program */
  97.         SpiFlash_w_PDMA_PageProgram((uint32_t)SrcArray, u32ProgramFlashAddress);
  98.         SpiFlash_w_PDMA_WaitReady();
  99.         u32ProgramFlashAddress += 0x100;
  100.         printf("Done!");

  101.         /*=== Read Back and Compare Data ===*/
  102.         printf("\nFlash Verifying... ");

  103.         /* Page Read */
  104.         SpiFlash_w_PDMA_ReadData((uint32_t)DestArray, u32VerifyFlashAddress);
  105.         u32VerifyFlashAddress += 0x100;

  106.         for(u32ByteCount=0; u32ByteCount<256; u32ByteCount++){
  107.             if(DestArray[u32ByteCount]!=u32ByteCount){
  108.                 /* Error */
  109.                 printf("SPI Flash R/W Fail!");
  110.                 while(1);
  111.             }
  112.         }
  113.                        
  114.         /* Clear Destination Data Buffer */
  115.         for(u32ByteCount=0; u32ByteCount<256; u32ByteCount++)
  116.             DestArray[u32ByteCount] = 0;
  117.         printf("Done!");
  118.     }

  119.     printf("\n\nSPI Flash Test Ok!");
  120.     printf("\n\n");

  121.     while(1);
  122.        
  123. }


 楼主| xuanhuanzi 发表于 2019-4-30 21:24 | 显示全部楼层
新唐的这个其实完整叫PDMA
外设接口的DMA技术
598330983 发表于 2019-5-2 20:36 | 显示全部楼层
DMA的库函数很好用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2307

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部