[应用方案] 请问NUVOTON厂家可以提供PDMA详细的范例吗?

[复制链接]
1891|15
 楼主| wangjiahao88 发表于 2024-2-18 10:31 | 显示全部楼层 |阅读模式
请问NUVOTON厂家可以提供PDMA详细的范例吗?
最好是可以应用于实际场景的
gaoyang9992006 发表于 2024-2-19 21:14 | 显示全部楼层
能用的是哪个型号?
643757107 发表于 2024-2-19 21:20 | 显示全部楼层
给你个PDMA 的PWM捕获
  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. * $Revision: 11 $
  5. * $Date: 18/07/19 2:18p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Capture the PWM0 Channel 0 waveform by PWM0 Channel 2, and use PDMA to transfer captured data.
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2018 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "NuMicro.h"

  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* Macro, type and constant definitions                                                                    */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. #define PLL_CLOCK       96000000

  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /* Global variables                                                                                        */
  19. /*---------------------------------------------------------------------------------------------------------*/
  20. uint16_t g_au16Count[4];
  21. volatile uint32_t g_u32IsTestOver = 0;


  22. /**
  23. * @brief       PDMA IRQ Handler
  24. *
  25. * @param       None
  26. *
  27. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  28. *
  29. * [url=home.php?mod=space&uid=1543424]@Details[/url]     ISR to handle PDMA interrupt event
  30. */
  31. void PDMA_IRQHandler(void)
  32. {
  33.     uint32_t status = PDMA_GET_INT_STATUS(PDMA);

  34.     if(status & 0x1)    /* abort */
  35.     {
  36.         if(PDMA_GET_ABORT_STS(PDMA) & 0x1)
  37.             g_u32IsTestOver = 2;
  38.         PDMA_CLR_ABORT_FLAG(PDMA, PDMA_ABTSTS_ABTIF0_Msk);
  39.     }
  40.     else if(status & 0x2)      /* done */
  41.     {
  42.         if(PDMA_GET_TD_STS(PDMA) & 0x1)
  43.             g_u32IsTestOver = 1;
  44.         PDMA_CLR_TD_FLAG(PDMA, PDMA_TDSTS_TDIF0_Msk);
  45.     }
  46.     else
  47.         printf("unknown interrupt !!\n");
  48. }

  49. /*--------------------------------------------------------------------------------------*/
  50. /* Capture function to calculate the input waveform information                         */
  51. /* g_au16Count[4] : Keep the internal counter value when input signal rising / falling     */
  52. /*               happens                                                                */
  53. /*                                                                                      */
  54. /* time    A    B     C     D                                                           */
  55. /*           ___   ___   ___   ___   ___   ___   ___   ___                              */
  56. /*      ____|   |_|   |_|   |_|   |_|   |_|   |_|   |_|   |_____                        */
  57. /* index              0 1   2 3                                                         */
  58. /*                                                                                      */
  59. /* The capture internal counter down count from 0x10000, and reload to 0x10000 after    */
  60. /* input signal falling happens (Time B/C/D)                                            */
  61. /*--------------------------------------------------------------------------------------*/
  62. void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
  63. {
  64.     uint16_t u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod;
  65.     uint32_t u32TimeOutCount;

  66.     g_u32IsTestOver = 0;

  67.     /* setup timeout */
  68.     u32TimeOutCount = SystemCoreClock;

  69.     /* Wait PDMA interrupt (g_u32IsTestOver will be set at IRQ_Handler function) */
  70.     while(g_u32IsTestOver == 0)
  71.     {
  72.         if(u32TimeOutCount == 0)
  73.         {
  74.             printf("\nSomething is wrong, please check if pin connection is correct. \n");
  75.             while(1);
  76.         }
  77.         u32TimeOutCount--;
  78.     }

  79.     u16RisingTime = g_au16Count[1];

  80.     u16FallingTime = g_au16Count[0];

  81.     u16HighPeriod = g_au16Count[1] - g_au16Count[2];

  82.     u16LowPeriod = 0x10000 - g_au16Count[1];

  83.     u16TotalPeriod = 0x10000 - g_au16Count[2];

  84.     printf("\nPWM generate: \nHigh Period=19199 ~ 19201, Low Period=44799 ~ 44801, Total Period=63999 ~ 64001\n");
  85.     printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
  86.            u16RisingTime, u16FallingTime, u16HighPeriod, u16LowPeriod, u16TotalPeriod);
  87.     if((u16HighPeriod < 19199) || (u16HighPeriod > 19201)
  88.             || (u16LowPeriod < 44799) || (u16LowPeriod > 44801)
  89.             || (u16TotalPeriod < 63999) || (u16TotalPeriod > 64001))
  90.         printf("Capture Test Fail!!\n");
  91.     else
  92.         printf("Capture Test Pass!!\n");
  93. }

  94. void SYS_Init(void)
  95. {
  96.     /*---------------------------------------------------------------------------------------------------------*/
  97.     /* Init System Clock                                                                                       */
  98.     /*---------------------------------------------------------------------------------------------------------*/
  99.     /* Enable HIRC clock */
  100.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  101.     /* Waiting for HIRC clock ready */
  102.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  103.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  104.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  105.     /* Select HIRC as the clock source of UART0 */
  106.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  107.     /* Set core clock as PLL_CLOCK from PLL (no PLL in rev. B & C) */
  108. //    CLK_SetCoreClock(PLL_CLOCK);

  109.     /* Waiting for PLL clock ready */
  110. //    CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  111.     /* Enable UART peripheral clock */
  112.     CLK_EnableModuleClock(UART0_MODULE);

  113.     /*---------------------------------------------------------------------------------------------------------*/
  114.     /* PWM clock frequency configuration                                                                       */
  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  117. //    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  118.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  119.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  120.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);

  121.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  122. //    CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PLL, 0);
  123.     /*---------------------------------------------------------------------------------------------------------*/

  124.     /* Enable PDMA module clock */
  125.     CLK_EnableModuleClock(PDMA_MODULE);

  126.     /* Enable PWM0 module clock */
  127.     CLK_EnableModuleClock(PWM0_MODULE);

  128.     /* Reset PWM0 module */
  129.     SYS_ResetModule(PWM0_RST);

  130.     /* Reset PDMA module */
  131.     SYS_ResetModule(PDMA_RST);

  132.     /* Update System Core Clock */
  133.     SystemCoreClockUpdate();

  134.     /*---------------------------------------------------------------------------------------------------------*/
  135.     /* Init I/O Multi-function                                                                                 */
  136.     /*---------------------------------------------------------------------------------------------------------*/
  137.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  138.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  139.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  140.     /* Set PB multi-function pins for PWM0 Channel 0 and 2 */
  141.     SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB5MFP_Msk | SYS_GPB_MFPL_PB3MFP_Msk)) |
  142.                     (SYS_GPB_MFPL_PB5MFP_PWM0_CH0 | SYS_GPB_MFPL_PB3MFP_PWM0_CH2);
  143. }

  144. void UART0_Init()
  145. {
  146.     /*---------------------------------------------------------------------------------------------------------*/
  147.     /* Init UART                                                                                               */
  148.     /*---------------------------------------------------------------------------------------------------------*/
  149.     /* Reset UART module */
  150.     SYS_ResetModule(UART0_RST);

  151.     /* Configure UART0 and set UART0 baud rate */
  152.     UART_Open(UART0, 115200);
  153. }

  154. /*---------------------------------------------------------------------------------------------------------*/
  155. /*  Main Function                                                                                          */
  156. /*---------------------------------------------------------------------------------------------------------*/
  157. int32_t main(void)
  158. {
  159.     uint32_t u32TimeOutCount;

  160.     /* Init System, IP clock and multi-function I/O
  161.        In the end of SYS_Init() will issue SYS_LockReg()
  162.        to lock protected register. If user want to write
  163.        protected register, please issue SYS_UnlockReg()
  164.        to unlock protected register if necessary */

  165.     /* Unlock protected registers */
  166.     SYS_UnlockReg();

  167.     /* Init System, IP clock and multi-function I/O */
  168.     SYS_Init();

  169.     /* Lock protected registers */
  170.     SYS_LockReg();

  171.     /* Init UART to 115200-8n1 for print message */
  172.     UART0_Init();

  173.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  174.     printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "PCLK" : "PLL");
  175.     printf("+------------------------------------------------------------------------+\n");
  176.     printf("|                          PWM Driver Sample Code                        |\n");
  177.     printf("|                                                                        |\n");
  178.     printf("+------------------------------------------------------------------------+\n");
  179.     printf("  This sample code will use PWM0 channel 2 to capture the signal from PWM0 channel 0.\n");
  180.     printf("  And the captured data is transferred by PDMA channel 0.\n");
  181.     printf("  I/O configuration:\n");
  182.     printf("    PWM0 channel 2(PB.3) <--> PWM0 channel 0(PB.5)\n\n");
  183.     printf("Use PWM0 Channel 2(PB.3) to capture the PWM0 Channel 0(PB.5) Waveform\n");

  184.     while(1)
  185.     {
  186.         printf("\n\nPress any key to start PWM Capture Test\n");
  187.         getchar();

  188.         /*--------------------------------------------------------------------------------------*/
  189.         /* Set the PWM0 Channel 0 as PWM output function.                                       */
  190.         /*--------------------------------------------------------------------------------------*/

  191.         /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows.
  192.            duty ratio = (CMR+1)/(CNR+1)
  193.            cycle time = CNR+1
  194.            High level = CMR+1
  195.            PWM clock source frequency from PLL is 48,000,000
  196.            (CNR+1) = PWM clock source frequency/prescaler/PWM output frequency
  197.                    = 48,000,000/3/250 = 64,000
  198.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  199.            CNR = 64,000
  200.            duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30%
  201.            CMR = 19,200
  202.            Prescale value is 4 : prescaler= 5
  203.         */

  204.         /* Set PWM0 channel 0 output configuration */
  205.         PWM_ConfigOutputChannel(PWM0, 0, 250, 30);

  206.         /* Enable PWM Output path for PWM0 channel 0 */
  207.         PWM_EnableOutput(PWM0, PWM_CH_0_MASK);

  208.         /* Enable Timer for PWM0 channel 0 */
  209.         PWM_Start(PWM0, PWM_CH_0_MASK);

  210.         /*--------------------------------------------------------------------------------------*/
  211.         /* Configure PDMA peripheral mode form PWM to memory                                    */
  212.         /*--------------------------------------------------------------------------------------*/
  213.         /* Open Channel 0 */
  214.         PDMA_Open(PDMA, 0x1);

  215.         /* Transfer width is half word(16 bit) and transfer count is 4 */
  216.         PDMA_SetTransferCnt(PDMA, 0, PDMA_WIDTH_16, 4);

  217.         /* Set source address as PWM capture channel PDMA register(no increment) and destination address as g_au16Count array(increment) */
  218.         PDMA_SetTransferAddr(PDMA, 0, (uint32_t)&PWM0->PDMACAP2_3, PDMA_SAR_FIX, (uint32_t)&g_au16Count[0], PDMA_DAR_INC);

  219.         /* Select PDMA request source as PWM RX(PWM0 channel 2 should be PWM0 pair 2) */
  220.         PDMA_SetTransferMode(PDMA, 0, PDMA_PWM0_P2_RX, FALSE, 0);

  221.         /* Set PDMA as single request type for PWM */
  222.         PDMA_SetBurstType(PDMA, 0, PDMA_REQ_SINGLE, PDMA_BURST_4);

  223.         PDMA_EnableInt(PDMA, 0, PDMA_INT_TRANS_DONE);
  224.         NVIC_EnableIRQ(PDMA_IRQn);

  225.         /* Enable PDMA for PWM0 channel 2 capture function, and set capture order as falling first, */
  226.         /* And select capture mode as both rising and falling to do PDMA transfer. */
  227.         PWM_EnablePDMA(PWM0, 2, FALSE, PWM_CAPTURE_PDMA_RISING_FALLING_LATCH);

  228.         /*--------------------------------------------------------------------------------------*/
  229.         /* Set the PWM0 channel 2 for capture function                                          */
  230.         /*--------------------------------------------------------------------------------------*/
  231.         /* If input minimum frequency is 250Hz, user can calculate capture settings by follows.
  232.            Capture clock source frequency = PLL = 48,000,000 in the sample code.
  233.            (CNR+1) = Capture clock source frequency/prescaler/minimum input frequency
  234.                    = 48,000,000/3/250 = 64,000
  235.            (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  236.            CNR = 0xFFFF
  237.            (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.)

  238.            Capture unit time = 1/Capture clock source frequency/prescaler
  239.            62.5ns = 1/48,000,000/3
  240.         */

  241.         /* Set PWM0 channel 2 capture configuration */
  242.         PWM_ConfigCaptureChannel(PWM0, 2, 62, 0);

  243.         /* Enable Timer for PWM0 channel 2 */
  244.         PWM_Start(PWM0, PWM_CH_2_MASK);

  245.         /* Enable Capture Function for PWM0 channel 2 */
  246.         PWM_EnableCapture(PWM0, PWM_CH_2_MASK);

  247.         /* Enable falling capture reload */
  248.         PWM0->CAPCTL |= PWM_CAPCTL_FCRLDEN2_Msk;

  249.         /* setup timeout */
  250.         u32TimeOutCount = SystemCoreClock;

  251.         /* Wait until PWM0 channel 2 Timer start to count */
  252.         while((PWM0->CNT[2]) == 0)
  253.         {
  254.             if(u32TimeOutCount == 0)
  255.             {
  256.                 printf("PWM encounters some errors, please check it. \n");
  257.                 while(1);
  258.             }
  259.             u32TimeOutCount--;
  260.         }

  261.         /* Capture the Input Waveform Data */
  262.         CalPeriodTime(PWM0, 2);
  263.         /*---------------------------------------------------------------------------------------------------------*/
  264.         /* Stop PWM0 channel 0 (Recommended procedure method 1)                                                    */
  265.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  266.         /*---------------------------------------------------------------------------------------------------------*/

  267.         /* Set PWM0 channel 0 loaded value as 0 */
  268.         PWM_Stop(PWM0, PWM_CH_0_MASK);

  269.         /* Wait until PWM0 channel 0 Timer Stop */
  270.         while((PWM0->CNT[0] & PWM_CNT_CNT_Msk) != 0)
  271.         {
  272.             if(u32TimeOutCount == 0)
  273.             {
  274.                 printf("PWM encounters some errors, please check it. \n");
  275.                 while(1);
  276.             }
  277.             u32TimeOutCount--;
  278.         }

  279.         /* Disable Timer for PWM0 channel 0 */
  280.         PWM_ForceStop(PWM0, PWM_CH_0_MASK);

  281.         /* Disable PWM Output path for PWM0 channel 0 */
  282.         PWM_DisableOutput(PWM0, PWM_CH_0_MASK);

  283.         /*---------------------------------------------------------------------------------------------------------*/
  284.         /* Stop PWM0 channel 2 (Recommended procedure method 1)                                                    */
  285.         /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  286.         /*---------------------------------------------------------------------------------------------------------*/

  287.         /* Set loaded value as 0 for PWM0 channel 2 */
  288.         PWM_Stop(PWM0, PWM_CH_2_MASK);

  289.         /* Wait until PWM0 channel 2 current counter reach to 0 */
  290.         while((PWM0->CNT[2] & PWM_CNT_CNT_Msk) != 0)
  291.         {
  292.             if(u32TimeOutCount == 0)
  293.             {
  294.                 printf("PWM encounters some errors, please check it. \n");
  295.                 while(1);
  296.             }
  297.             u32TimeOutCount--;
  298.         }

  299.         /* Disable Timer for PWM0 channel 2 */
  300.         PWM_ForceStop(PWM0, PWM_CH_2_MASK);

  301.         /* Disable Capture Function and Capture Input path for  PWM0 channel 2*/
  302.         PWM_DisableCapture(PWM0, PWM_CH_2_MASK);

  303.         /* Clear Capture Interrupt flag for PWM0 channel 2 */
  304.         PWM_ClearCaptureIntFlag(PWM0, 2, PWM_CAPTURE_INT_FALLING_LATCH);

  305.         /* Disable PDMA NVIC */
  306.         NVIC_DisableIRQ(PDMA_IRQn);

  307.         PDMA_Close(PDMA);
  308.     }
  309. }


643757107 发表于 2024-2-19 21:21 | 显示全部楼层
PDMA下的PWM捕获1MHz信号的方法
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V1.00
  4. * $Revision: 9 $
  5. * $Date: 18/07/05 4:58p $
  6. * @brief    Capture the PWM0 Channel 0 waveform by PWM0 Channel 2, and use PDMA to transfer captured data.
  7.              Frequency of PWM Channel 0 is 1 MHz to test maximum input frequency
  8.                          for PWM Capture function.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include "NuMicro.h"

  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Macro, type and constant definitions                                                                    */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. #define PLL_CLOCK       96000000

  19. /*---------------------------------------------------------------------------------------------------------*/
  20. /* Global variables                                                                                        */
  21. /*---------------------------------------------------------------------------------------------------------*/
  22. #define Transfer_Count 4
  23. uint16_t g_au16Count[Transfer_Count];
  24. volatile uint32_t g_u32IsTestOver = 0;


  25. /**
  26. * @brief       PDMA IRQ Handler
  27. *
  28. * @param       None
  29. *
  30. * @return      None
  31. *
  32. * @details     ISR to handle PDMA interrupt event
  33. */
  34. void PDMA_IRQHandler(void)
  35. {
  36.     uint32_t status = PDMA_GET_INT_STATUS(PDMA);

  37.     if(status & 0x1)    /* abort */
  38.     {
  39.         if(PDMA_GET_ABORT_STS(PDMA) & 0x1)
  40.             g_u32IsTestOver = 2;
  41.         PDMA_CLR_ABORT_FLAG(PDMA, PDMA_ABTSTS_ABTIF0_Msk);
  42.     }
  43.     else if(status & 0x2)      /* done */
  44.     {
  45.         if(PDMA_GET_TD_STS(PDMA) & 0x1)
  46.             g_u32IsTestOver = 1;
  47.         PDMA_CLR_TD_FLAG(PDMA, PDMA_TDSTS_TDIF0_Msk);
  48.     }
  49.     else
  50.         printf("unknown interrupt !!\n");
  51. }

  52. /*--------------------------------------------------------------------------------------*/
  53. /* Capture function to calculate the input waveform information                         */
  54. /* g_au16Count[4] : Keep the internal counter value when input signal rising / falling  */
  55. /*               happens                                                                */
  56. /*                                                                                      */
  57. /* time    A    B     C     D                                                           */
  58. /*           ___   ___   ___   ___   ___   ___   ___   ___                              */
  59. /*      ____|   |_|   |_|   |_|   |_|   |_|   |_|   |_|   |_____                        */
  60. /* index              0 1   2 3                                                         */
  61. /*                                                                                      */
  62. /* The capture internal counter down count from 0xFFFF, and reload to 0xFFFF after      */
  63. /* input signal falling happens (Time B/C/D)                                            */
  64. /*--------------------------------------------------------------------------------------*/
  65. void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
  66. {
  67.     uint16_t u16HighPeriod, u16LowPeriod, u16TotalPeriod;
  68.     uint32_t u32TimeOutCount;

  69.     /* setup timeout */
  70.     u32TimeOutCount = SystemCoreClock;

  71.     /* Wait PDMA interrupt (g_u32IsTestOver will be set at IRQ_Handler function) */
  72.     while(g_u32IsTestOver == 0)
  73.     {
  74.         if(u32TimeOutCount == 0)
  75.         {
  76.             printf("\nSomething is wrong, please check if pin connection is correct. \n");
  77.             while(1);
  78.         }
  79.         u32TimeOutCount--;
  80.     }

  81.     u16HighPeriod = g_au16Count[1] - g_au16Count[2] + 1;

  82.     u16LowPeriod = 0xFFFF - g_au16Count[1];

  83.     u16TotalPeriod = 0xFFFF - g_au16Count[2] + 1;

  84.     printf("\nHigh Period = %d ns, Low Period = %d ns , Total Period = %d ns.\n",
  85.            u16HighPeriod*1000/48, u16LowPeriod*1000/48, u16TotalPeriod*1000/48);
  86.     printf("Frequency = %d Hz, Duty = %d %%.\n\n",
  87.            48000000/u16TotalPeriod, u16HighPeriod*100/u16TotalPeriod );
  88. }

  89. void SYS_Init(void)
  90. {
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init System Clock                                                                                       */
  93.     /*---------------------------------------------------------------------------------------------------------*/
  94.     /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
  95.     GPIO_SetMode(PF, BIT2 | BIT3, GPIO_MODE_INPUT);

  96.     /* Enable HIRC clock */
  97.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  98.     /* Waiting for HIRC clock ready */
  99.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  100.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  101.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  102.     /* Select HIRC as the clock source of UART0 */
  103.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  104.     /* Set core clock as PLL_CLOCK from PLL (no PLL in rev. B & C) */
  105. //    CLK_SetCoreClock(PLL_CLOCK);

  106.     /* Waiting for PLL clock ready */
  107. //    CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  108.     /* Enable UART peripheral clock */
  109.     CLK_EnableModuleClock(UART0_MODULE);

  110.     /*---------------------------------------------------------------------------------------------------------*/
  111.     /* PWM clock frequency configuration                                                                       */
  112.     /*---------------------------------------------------------------------------------------------------------*/
  113.     /* Select HCLK clock source as PLL and and HCLK clock divider as 2 */
  114. //    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  115.     /* PWM clock frequency can be set equal or double to HCLK by choosing case 1 or case 2 */
  116.     /* case 1.PWM clock frequency is set equal to HCLK: select PWM module clock source as PCLK */
  117.     CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0);

  118.     /* case 2.PWM clock frequency is set double to HCLK: select PWM module clock source as PLL */
  119. //    CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PLL, 0);
  120.     /*---------------------------------------------------------------------------------------------------------*/

  121.     /* Enable PDMA module clock */
  122.     CLK_EnableModuleClock(PDMA_MODULE);

  123.     /* Enable PWM0 module clock */
  124.     CLK_EnableModuleClock(PWM0_MODULE);

  125.     /* Reset PWM0 module */
  126.     SYS_ResetModule(PWM0_RST);

  127.     /* Reset PDMA module */
  128.     SYS_ResetModule(PDMA_RST);

  129.     /* Update System Core Clock */
  130.     SystemCoreClockUpdate();

  131.     /*---------------------------------------------------------------------------------------------------------*/
  132.     /* Init I/O Multi-function                                                                                 */
  133.     /*---------------------------------------------------------------------------------------------------------*/
  134.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  135.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  136.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  137.     /* Set PB multi-function pins for PWM0 Channel 0 and 2 */
  138.     SYS->GPB_MFPL = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB5MFP_Msk)) |
  139.                     SYS_GPB_MFPL_PB5MFP_PWM0_CH0;
  140.     SYS->GPB_MFPL = (SYS->GPB_MFPL & (~SYS_GPB_MFPL_PB3MFP_Msk)) |
  141.                     SYS_GPB_MFPL_PB3MFP_PWM0_CH2;
  142. }

  143. void UART0_Init()
  144. {
  145.     /*---------------------------------------------------------------------------------------------------------*/
  146.     /* Init UART                                                                                               */
  147.     /*---------------------------------------------------------------------------------------------------------*/
  148.     /* Reset UART module */
  149.     SYS_ResetModule(UART0_RST);

  150.     /* Configure UART0 and set UART0 baud rate */
  151.     UART_Open(UART0, 115200);
  152. }

  153. /*---------------------------------------------------------------------------------------------------------*/
  154. /*  Main Function                                                                                          */
  155. /*---------------------------------------------------------------------------------------------------------*/
  156. int32_t main(void)
  157. {
  158.     uint8_t u8Option;

  159.     /* Init System, IP clock and multi-function I/O
  160.        In the end of SYS_Init() will issue SYS_LockReg()
  161.        to lock protected register. If user want to write
  162.        protected register, please issue SYS_UnlockReg()
  163.        to unlock protected register if necessary */

  164.     /* Unlock protected registers */
  165.     SYS_UnlockReg();

  166.     /* Init System, IP clock and multi-function I/O */
  167.     SYS_Init();

  168.     /* Lock protected registers */
  169.     SYS_LockReg();

  170.     /* Init UART to 115200-8n1 for print message */
  171.     UART0_Init();

  172.     printf("\n\nCPU @ %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  173.     printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "CPU" : "PLL");
  174.     printf("+------------------------------------------------------------------------+\n");
  175.     printf("|                          PWM Capture Sample Code                        |\n");
  176.     printf("|                                                                        |\n");
  177.     printf("+------------------------------------------------------------------------+\n");
  178.     printf("  This sample code will use PWM0 channel 2 to capture the signal from PWM0 channel 0.\n");
  179.     printf("  And the captured data is transferred by PDMA channel 0.\n");
  180.     printf("  PWM0 channel 0 is 1 MHz, duty is 50 %%.\n");
  181.     printf("  PWM0 channel 2 counter period is about 20.8(1/48,000,000) ns\n");
  182.     printf("  I/O configuration:\n");
  183.     printf("    PWM0 channel 2(PB.3) <--> PWM0 channel 0(PB.5)\n\n");
  184.     printf("Use PWM0 Channel 2(PB.3) to capture the PWM0 Channel 0(PB.5) Waveform\n");

  185.     /*--------------------------------------------------------------------------------------*/
  186.     /* Set the PWM0 Channel 0 as PWM output function.                                       */
  187.     /*--------------------------------------------------------------------------------------*/

  188.     /* Assume PWM output frequency is 1 MHz and duty ratio is 50%, user can calculate PWM settings by follows.
  189.          duty ratio = CMR+/(CNR+1)
  190.          cycle time = CNR+1
  191.          High level = CMR
  192.          PWM clock source frequency from HIRC is 48,000,000
  193.          (CNR+1) = PWM clock source frequency/prescaler/PWM output frequency
  194.                          = 48,000,000/1/48 = 1,000,000
  195.          (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  196.          CNR = 47
  197.          duty ratio = 50% ==> CMR/(CNR+1) = 50%
  198.          CMR = 24
  199.          Prescale value is 0 : prescaler = 1
  200.     */

  201.     /* PWM0 channel 0 frequency prescaler to 1 */
  202.     PWM_SET_PRESCALER(PWM0, 0, 1-1);

  203.     /* PWM0 channel 0 frequency period to 47 */
  204.     PWM_SET_CNR(PWM0, 0, 47);

  205.     /* PWM0 channel 0 frequency comparator to 24 */
  206.     PWM_SET_CMR(PWM0, 0, 24);

  207.     /* PWM0 channel 0 is edge-aligned and down counter type */
  208.     PWM_SET_ALIGNED_TYPE(PWM0, BIT0, PWM_EDGE_ALIGNED);

  209.     /* ZeroLevel: Low, CmpUpLevel: nothing, PeriodLevel: nothing, CmpDownLevel: High */
  210.     PWM_SET_OUTPUT_LEVEL(PWM0, PWM_CH_0_MASK, PWM_OUTPUT_LOW, PWM_OUTPUT_HIGH, PWM_OUTPUT_NOTHING, PWM_OUTPUT_HIGH);

  211.     /* Enable PWM Output path for PWM0 channel 0 */
  212.     PWM_EnableOutput(PWM0, PWM_CH_0_MASK);

  213.     /* Enable Timer for PWM0 channel 0 */
  214.     PWM_Start(PWM0, PWM_CH_0_MASK);

  215.     /*--------------------------------------------------------------------------------------*/
  216.     /* Configure PDMA peripheral mode form PWM to memory                                    */
  217.     /*--------------------------------------------------------------------------------------*/
  218.     /* Open Channel 0 */
  219.     PDMA_Open(PDMA, BIT0);

  220.     /* Set source address as PWM capture channel PDMA register(no increment) and destination address as g_au16Count array(increment) */
  221.     PDMA_SetTransferAddr(PDMA, 0, (uint32_t)&PWM0->PDMACAP2_3, PDMA_SAR_FIX, (uint32_t)&g_au16Count[0], PDMA_DAR_INC);

  222.     /* Set PDMA as single request type for PWM */
  223.     PDMA_SetBurstType(PDMA, 0, PDMA_REQ_SINGLE, PDMA_BURST_4);

  224.     PDMA_EnableInt(PDMA, 0, PDMA_INT_TRANS_DONE);
  225.     NVIC_EnableIRQ(PDMA_IRQn);

  226.     /*--------------------------------------------------------------------------------------*/
  227.     /* Set the PWM0 channel 2 for capture function                                          */
  228.     /*--------------------------------------------------------------------------------------*/
  229.     /* (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.)
  230.          CNR = 0xFFFF
  231.          (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.)

  232.          Capture unit time = 1/Capture clock source frequency/prescaler
  233.          20.8ns = 1/48,000,000/1
  234.     */

  235.     /* Set PWM0 channel 2 capture configuration */
  236.     PWM_ConfigCaptureChannel(PWM0, 2, 20, 0);

  237.     /* Enable falling capture reload */
  238.     PWM0->CAPCTL |= PWM_CAPCTL_FCRLDEN2_Msk;

  239.     while(1)
  240.     {
  241.         g_u32IsTestOver = 0;

  242.         printf("[0] Trigger PWM0 Channel 2 capture function\n");
  243.         printf("[Other] Exit \n");
  244.         printf("Please input key:");
  245.         u8Option = getchar();

  246.         if(u8Option != '0')
  247.         {
  248.             printf("Exit\n");
  249.             break;

  250.         }

  251.         /*--------------------------------------------------------------------------------------*/
  252.         /* Configure PDMA transfer count and enable PDMA function                               */
  253.         /*--------------------------------------------------------------------------------------*/
  254.         /* Select PDMA request source as PWM RX(PWM0 channel 2 should be PWM0 pair 2) */
  255.         PDMA_SetTransferMode(PDMA, 0, PDMA_PWM0_P2_RX, FALSE, 0);

  256.         /* Transfer width is half word(16 bit) and transfer count is 4 */
  257.         PDMA_SetTransferCnt(PDMA, 0, PDMA_WIDTH_16, Transfer_Count);

  258.         /* Enable Capture Function for PWM0 channel 2 */
  259.         PWM_EnableCapture(PWM0, PWM_CH_2_MASK);

  260.         /* Enable Timer for PWM0 channel 2 */
  261.         PWM_Start(PWM0, PWM_CH_2_MASK);

  262.         /* Enable PDMA for PWM0 channel 2 capture function, and set capture order as falling first, */
  263.         /* And select capture mode as both rising and falling to do PDMA transfer. */
  264.         PWM_EnablePDMA(PWM0, 2, FALSE, PWM_CAPTURE_PDMA_RISING_FALLING_LATCH);

  265.         /* Capture the Input Waveform Data */
  266.         CalPeriodTime(PWM0, 2);

  267.         /* Set loaded value as 0 for PWM0 channel 2 */
  268.         PWM_Stop(PWM0, PWM_CH_2_MASK);

  269.         /* Clear Capture Interrupt flag for PWM0 channel 2 */
  270.         PWM_ClearCaptureIntFlag(PWM0, 2, PWM_CAPTURE_INT_FALLING_LATCH);

  271.         /* Disable PDMA for PWM0 channel 2 capture function */
  272.         PWM_DisablePDMA(PWM0, 2);
  273.     }

  274.     /*---------------------------------------------------------------------------------------------------------*/
  275.     /* Stop PWM0 channel 2 (Recommended procedure method 1)                                                    */
  276.     /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */
  277.     /*---------------------------------------------------------------------------------------------------------*/
  278.     /* Set loaded value as 0 for PWM0 channel 2 */
  279.     PWM_Stop(PWM0, PWM_CH_2_MASK);

  280.     /* Disable Timer for PWM0 channel 2 */
  281.     PWM_ForceStop(PWM0, PWM_CH_2_MASK);

  282.     /* Stop PWM counter */
  283.     PWM_Stop(PWM0, PWM_CH_0_MASK);
  284.     /* Disable output of PWM0 channel 0 */
  285.     PWM_DisableOutput(PWM0, PWM_CH_0_MASK);

  286.     PDMA_Close(PDMA);

  287.     while(1);
  288. }


可怜的小弗朗士 发表于 2024-3-8 15:15 | 显示全部楼层
资源包中有PDMA的例程啊
yiyigirl2014 发表于 2024-3-9 12:50 | 显示全部楼层
几乎每一个系列的BSP里都有PDMA例子的。
别乱了阵脚 发表于 2024-3-21 12:31 | 显示全部楼层
肯定可以啊,这正常的例程还能不提供了?
冰春彩落下 发表于 2024-3-21 15:00 | 显示全部楼层
一般支持PDMA的芯片,都是会提供这例程的
江河千里 发表于 2024-3-21 17:00 | 显示全部楼层
我觉得肯定可以,而且你找的资料包里估计也会有
暖了夏天蓝了海 发表于 2024-3-21 21:00 | 显示全部楼层
你用的那款芯片啊?
淡漠安然 发表于 2024-3-21 23:00 | 显示全部楼层
我记得是提供的啊,好多群友都用过了呢
三生万物 发表于 2024-3-22 04:00 | 显示全部楼层
直接找代理商寻求一下资料,你就能看到PDMA的相关支持了
光辉梦境 发表于 2024-3-22 08:00 | 显示全部楼层
我觉得是提供的,这是属于芯片的基础外设,还能不好使啊
远山寻你 发表于 2024-3-22 12:00 | 显示全部楼层
你找你对应的MCU的资料开发包,里面基本都有例程
夜阑风雨 发表于 2024-3-22 16:00 | 显示全部楼层
说实话,还没用过PDMA,是不是也跟DMA差不多啊?
一秒落纱 发表于 2024-3-22 19:00 | 显示全部楼层
都会提供的,就直接要就完事儿了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

500

主题

7585

帖子

31

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