[范例教程] 【M0】 MG32F02A 学习笔记⑨ SPI0 DMA标准发送与接收

[复制链接]
7712|125
 楼主| noctor 发表于 2018-9-30 13:54 | 显示全部楼层 |阅读模式
本帖最后由 noctor 于 2018-9-30 13:54 编辑
     上回我们说到了MG32F02A的SPI0通过标准四线制全双工方式进行数据通信。帖子详情:https://bbs.21ic.com/icview-2561446-1-1.html

      这次我们就按照老样子,搭载DMA功能进行传输。
      上次我们说过,UART的DMA功能感觉并没有多简化,然而我发现在SPI的发送端上也是一样的结果,同样需要手动每次传输数据之前使能一次DMA外设发送功能才能发送,而且一样要使用while等待TCF_FLAG的到来。、
      主机发送代码:
  1. #include "MG32x02z_DRV.H"
  2. #include <stdio.h>

  3. typedef uint8_t u8;
  4. typedef uint16_t u16;
  5. typedef uint32_t u32;
  6. typedef uint64_t u64;

  7. #define Dummy_Data         0xFFFFFFFF

  8. #define SPI_NSS         PB0     // SPI_NSS
  9. #define URTX URT0
  10. #define MYBINARYIMAGE2_LENGTH 20

  11. uint8_t SendBuf[MYBINARYIMAGE2_LENGTH]={0};//__attribute__((at(0x20001000)));

  12. void CSC_Init (void)
  13. {
  14.         CSC_PLL_TyprDef CSC_PLL_CFG;

  15.   UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  16.   MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  17.   ProtectModuleReg(MEMprotect);

  18.   UnProtectModuleReg(CSCprotect);
  19.         CSC_CK_APB_Divider_Select(APB_DIV_1);        // Modify CK_APB divider        APB=CK_MAIN/1
  20.         CSC_CK_AHB_Divider_Select(AHB_DIV_1);        // Modify CK_AHB divider        AHB=APB/1
  21.         
  22.         /* CK_HS selection */
  23.         CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
  24.         CSC_IHRCO_Cmd(ENABLE);
  25.         while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
  26.         CSC_ClearFlag(CSC_IHRCOF);
  27.         CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO

  28.         /* PLL */
  29.         /**********************************************************/
  30.         CSC_PLL_CFG.InputDivider=PLLI_DIV_2;        // 12M/2=6M
  31.         CSC_PLL_CFG.Multiplication=PLLIx16;                // 6M*16=96M
  32.         CSC_PLL_CFG.OutputDivider=PLLO_DIV_2;        // PLLO=96M/2=48M
  33.         CSC_PLL_Config(&CSC_PLL_CFG);
  34.         CSC_PLL_Cmd(ENABLE);
  35.         while(CSC_GetSingleFlagStatus(CSC_PLLF) == DRV_Normal);
  36.         CSC_ClearFlag(CSC_PLLF);
  37.         /**********************************************************/
  38.         
  39.         /* CK_MAIN */
  40.         CSC_CK_MAIN_Select(MAIN_CK_HS);        

  41.         /* Configure ICKO function */
  42.                
  43.         /* Configure peripheral clock */
  44.         CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);
  45.         CSC_PeriphOnModeClock_Config(CSC_ON_SPI0,ENABLE);
  46.         CSC_PeriphOnModeClock_Config(CSC_ON_PortB,ENABLE);
  47.         CSC_PeriphOnModeClock_Config(CSC_ON_PortE,ENABLE);
  48.         CSC_PeriphOnModeClock_Config(CSC_ON_DMA,ENABLE);

  49.   ProtectModuleReg(CSCprotect);
  50.    
  51. }

  52. void InitSPI0(void)
  53. {  
  54.         
  55.                 PIN_InitTypeDef PINX_InitStruct;
  56.         
  57.         //===Set CSC init====
  58.         //MG32x02z_CSC_Init.h(Configuration Wizard)
  59.         //Select CK_HS source = CK_IHRCO
  60.         //Select IHRCO = 12Mz
  61.         //Select CK_MAIN Source = CK_HS
  62.         //Configure PLL->Select APB Prescaler = CK_MAIN/1
  63.         
  64.         /*=== 1. Enable CSC to SPI clock ===*/
  65.         //[A] When Use Wizard
  66.         //Configure Peripheral On Mode Clock->SPI0 = Enable and Select SPI0_PR Source = CK_APB
  67.         //Configure Peripheral On Mode Clock->Port B = Enable
  68.         //[B] When Use Driver
  69. //          UnProtectModuleReg(CSCprotect);                                                          // Unprotect CSC module
  70. //          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0, ENABLE);                  // Enable SPI0 module clock
  71. //          CSC_PeriphOnModeClock_Config(CSC_ON_PortB, ENABLE);                  // Enable PortB clock
  72. //          CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);  // CK_SPIx_PR = CK_APB = 12MHz
  73. //          ProtectModuleReg(CSCprotect);                                                           // protect CSC module
  74.            
  75.         /*=== 2. Default Initial SPI ===*/
  76.         SPI_DeInit(SPI0);
  77.         
  78.         /*=== 3. Configure clock divider ===*/                                                // SPI clock = 1MHz
  79.         SPI_Clock_Select(SPI0, SPI_CK_SPIx_PR);                                         // CK_SPIx = CK_SPIx_PR
  80.         SPI_PreDivider_Select(SPI0, SPI_PDIV_2);                                        // PDIV outpu = CK_SPIx /2
  81.         SPI_Prescaler_Select(SPI0, SPI_PSC_3);                                                // Prescaler outpu = PDIV outpu /3
  82.         SPI_Divider_Select(SPI0, SPI_DIV_2);                                                // DIV outpu = PDIV outpu /2
  83.         
  84.         /*=== 4. Configure SPI data line, mode and data size... ===*/
  85.         SPI_DataLine_Select(SPI0, SPI_Standard);                        // SPI data line 1-line Bidirectional~ SPI0_MOSI
  86.         SPI_ModeAndNss_Select(SPI0, SPI_MasterWithNss);                                        // Master
  87.         SPI_ClockPhase_Select(SPI0, SPI_LeadingEdge);                                // CPHA = 0
  88.         SPI_ClockPolarity_Select(SPI0, SPI_Low);                                        // CPOL = 0
  89.         SPI_FirstBit_Select(SPI0, SPI_MSB);                                                 // MSB first
  90.         SPI_DataSize_Select(SPI0, SPI_8bits);                                                // Data size 8bits
  91.         
  92.         /*=== 5. Config SPI0 IO ===*/
  93.         PINX_InitStruct.PINX_Mode                                = PINX_Mode_PushPull_O;         // Pin select pusu pull mode
  94.         PINX_InitStruct.PINX_PUResistant                = PINX_PUResistant_Enable;        // Enable pull up resistor
  95.         PINX_InitStruct.PINX_Speed                                = PINX_Speed_Low;                        
  96.         PINX_InitStruct.PINX_OUTDrive                        = PINX_OUTDrive_Level0;         // Pin output driver full strength.
  97.         PINX_InitStruct.PINX_FilterDivider                = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  98.         PINX_InitStruct.PINX_Inverse                        = PINX_Inverse_Disable;         // Pin input data not inverse
  99.         
  100.         PINX_InitStruct.PINX_Alternate_Function = 2;                                                // Pin AFS = 2
  101.         GPIO_PinMode_Config(PINB(2),&PINX_InitStruct);                                                // CLK setup at PB2
  102.         
  103.   PINX_InitStruct.PINX_Mode                                = PINX_Mode_PushPull_O;         // Pin select pusu pull mode
  104.         PINX_InitStruct.PINX_OUTDrive                        = PINX_OUTDrive_Level0;         // Pin output drive strength 1/4
  105.         GPIO_PinMode_Config(PINB(3),&PINX_InitStruct);                                                // MOSI setup at PB3

  106.         PINX_InitStruct.PINX_Alternate_Function = 2;                                                // Pin AFS = 0
  107.         GPIO_PinMode_Config(PINB(0),&PINX_InitStruct);                                                // NSS setup at PB0
  108.         
  109.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_OpenDrain_O;
  110.         GPIO_PinMode_Config(PINB(1),&PINX_InitStruct);                                                // MISO setup at PB1

  111.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;                  // Pin select Push Pull mode
  112.         PINX_InitStruct.PINX_Alternate_Function  = 0;                                                         // Pin AFS = 0
  113.         GPIO_PinMode_Config(PINE(13),&PINX_InitStruct);                                                  // PE13
  114.         GPIO_PinMode_Config(PINE(14),&PINX_InitStruct);                                                  // PE14
  115.         GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                                  // PE15

  116.         /*=== 6. Enable SPI ===*/
  117.         SPI_Cmd(SPI0, ENABLE);               

  118. }

  119. void DMA_Init(void)
  120. {
  121.          DMA_BaseInitTypeDef DMATestPattern;

  122.     // ------------------------------------------------------------------------
  123.     // 1.Enable DMA
  124.     DMA_Cmd(ENABLE);
  125.    
  126.     // ------------------------------------------------------------------------
  127.     // 2.Enable Channel0
  128.     DMA_Channel_Cmd(DMAChannel0, ENABLE);
  129.    
  130.     // ------------------------------------------------------------------------
  131.     DMA_BaseInitStructure_Init(&DMATestPattern);
  132.    
  133.     // 3.initial & modify parameter
  134.       
  135.         // DMA channel select
  136.         DMATestPattern.DMAChx = DMAChannel0;
  137.         
  138.         // channel x source/destination auto increase address
  139.         DMATestPattern.SrcSINCSel = ENABLE;
  140.         DMATestPattern.DestDINCSel = DISABLE;
  141.         
  142.         // DMA source peripheral config
  143.         DMATestPattern.SrcSymSel = DMA_MEM_Read;
  144.         
  145.         // DMA destination peripheral config
  146.         DMATestPattern.DestSymSel = DMA_SPI0_TX;
  147.         
  148.         // DMA Burst size config
  149.         DMATestPattern.BurstDataSize = DMA_BurstSize_1Byte;
  150.         
  151.         // DMA transfer data count initial number
  152.         DMATestPattern.DMATransferNUM = MYBINARYIMAGE2_LENGTH;
  153.    
  154.         // source/destination config
  155.         DMATestPattern.DMASourceAddr = (uint32_t *)&SendBuf;
  156.         DMATestPattern.DMADestinationAddr = &SPI0->TDAT;
  157.                                 
  158.                                 SPI_DMASend_Cmd(SPI0, ENABLE);
  159.                                 DMA_Channel_Cmd(DMAChannel0, ENABLE);
  160.                                 DMA_Base_Init(&DMATestPattern);
  161. }


  162. int main()
  163. {
  164.         u32 i;
  165.         u8 x,y;
  166.         PIN_InitTypeDef PINX_InitStruct;
  167.         CSC_Init();
  168.         InitSPI0();        
  169.         
  170.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;          // Pin select digital input mode
  171.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
  172.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  173.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
  174.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  175.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
  176.         PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  177.         GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15
  178.         DMA_Init();
  179.         DMA_StartRequest(DMAChannel0);
  180.         i=0;
  181.     while(1)
  182.     {
  183.                   
  184.             i++;
  185.             if(i>=500000)
  186.             {
  187.                                 SPI_DMASend_Cmd(SPI0, ENABLE);      //每次发送前都要使能一次
  188.                                 SPI_NSS=1;
  189.                                 SPI_NSS=0;
  190.                                 PE15=0;
  191.                                 SendBuf[0]=0x05;
  192.                                 PE15=0;
  193.                                 i=1;
  194.                                 DMA_StartRequest(DMAChannel0);
  195.                                 while (DMA_GetSingleFlagStatus(DMA, DMA_FLAG_CH0_TCF) == DRV_UnHappened);
  196.                                 DMA_ClearFlag(DMA, DMA_FLAG_CH0_TCF);
  197.                                 
  198.                                 DMA_Channel_Cmd(DMAChannel0, DISABLE);
  199.                                 DMA_SetSourceAddress(DMAChannel0, SendBuf);
  200.                                 DMA_Channel_Cmd(DMAChannel0, ENABLE);
  201.                                 memset(SendBuf,0,sizeof(u8));
  202.                                 PE15=1;
  203.                         }
  204.     }

  205.                
  206. }


     这样就能实现SPI的DMA发送功能了,但是要记住,NSS还是需要自己手动拉高拉低的。
      从机接收程序:
  1. #include "MG32x02z_DRV.H"

  2. #define URTX URT0
  3. #define SPI_NSS         PB0     // SPI_NSS
  4. #define MYBINARYIMAGE2_LENGTH 20

  5. uint8_t RcvBuf[MYBINARYIMAGE2_LENGTH] = {0};// __attribute__((at(0x20003800)));

  6. void CSC_Init (void)
  7. {
  8.         CSC_PLL_TyprDef CSC_PLL_CFG;
  9.    
  10.         
  11.     UnProtectModuleReg(MEMprotect);             // Setting flash wait state
  12.     MEM_SetFlashWaitState(MEM_FWAIT_ONE);        // 50MHz> Sysclk >=25MHz
  13.     ProtectModuleReg(MEMprotect);

  14.   UnProtectModuleReg(CSCprotect);
  15.         CSC_CK_APB_Divider_Select(APB_DIV_1);        // Modify CK_APB divider        APB=CK_MAIN/1
  16.         CSC_CK_AHB_Divider_Select(AHB_DIV_1);        // Modify CK_AHB divider        AHB=APB/1

  17.         
  18.         /* CK_HS selection */
  19.         CSC_IHRCO_Select(IHRCO_12MHz);                        // IHRCO Sel 12MHz
  20.         CSC_IHRCO_Cmd(ENABLE);
  21.         while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
  22.         CSC_ClearFlag(CSC_IHRCOF);
  23.         CSC_CK_HS_Select(HS_CK_IHRCO);                        // CK_HS select IHRCO


  24.         /* PLL */
  25.         /**********************************************************/
  26.         CSC_PLL_CFG.InputDivider=PLLI_DIV_2;        // 12M/2=6M
  27.         CSC_PLL_CFG.Multiplication=PLLIx16;                // 6M*16=96M
  28.         CSC_PLL_CFG.OutputDivider=PLLO_DIV_2;        // PLLO=96M/2=48M
  29.         CSC_PLL_Config(&CSC_PLL_CFG);
  30.         CSC_PLL_Cmd(ENABLE);
  31.         while(CSC_GetSingleFlagStatus(CSC_PLLF) == DRV_Normal);
  32.         CSC_ClearFlag(CSC_PLLF);
  33.         /**********************************************************/

  34.         
  35.         /* CK_MAIN */
  36.         CSC_CK_MAIN_Select(MAIN_CK_HS);        


  37.         /* Configure ICKO function */
  38.                
  39.         /* Configure peripheral clock */
  40.         CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);
  41.         CSC_PeriphProcessClockSource_Config(CSC_UART0_CKS, CK_APB);
  42.          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0,ENABLE);
  43.         CSC_PeriphOnModeClock_Config(CSC_ON_UART0,ENABLE);
  44.          CSC_PeriphOnModeClock_Config(CSC_ON_PortB,ENABLE);
  45.         CSC_PeriphOnModeClock_Config(CSC_ON_PortE,ENABLE);
  46.         CSC_PeriphOnModeClock_Config(CSC_ON_DMA,ENABLE);


  47.         
  48.     ProtectModuleReg(CSCprotect);
  49.    
  50. }

  51. void InitSPI0(void)
  52. {  
  53.    
  54.         PIN_InitTypeDef PINX_InitStruct;

  55.         //===Set CSC init====
  56.         //MG32x02z_CSC_Init.h(Configuration Wizard)
  57.         //Select CK_HS source = CK_IHRCO
  58.         //Select IHRCO = 12Mz
  59.         //Select CK_MAIN Source = CK_HS
  60.         //Configure PLL->Select APB Prescaler = CK_MAIN/1

  61.         /*=== 1. Enable CSC to SPI clock ===*/
  62.         //[A] When Use Wizard
  63.         //Configure Peripheral On Mode Clock->SPI0 = Enable and Select SPI0_PR Source = CK_APB
  64.         //Configure Peripheral On Mode Clock->Port B = Enable
  65.         //[B] When Use Driver
  66.         //          UnProtectModuleReg(CSCprotect);                                                          // Unprotect CSC module
  67.         //          CSC_PeriphOnModeClock_Config(CSC_ON_SPI0, ENABLE);                  // Enable SPI0 module clock
  68.         //          CSC_PeriphOnModeClock_Config(CSC_ON_PortB, ENABLE);                  // Enable PortB clock
  69.         //          CSC_PeriphProcessClockSource_Config(CSC_SPI0_CKS, CK_APB);  // CK_SPIx_PR = CK_APB = 12MHz
  70.         //          ProtectModuleReg(CSCprotect);                                                           // protect CSC module

  71.         /*=== 2. Default Initial SPI ===*/
  72.         SPI_DeInit(SPI0);

  73.         /*=== 3. Configure clock divider ===*/                                                // SPI clock = 1MHz
  74.         SPI_Clock_Select(SPI0, SPI_CK_SPIx_PR);                                         // CK_SPIx = CK_SPIx_PR
  75.         SPI_PreDivider_Select(SPI0, SPI_PDIV_2);                                        // PDIV outpu = CK_SPIx /2
  76.         SPI_Prescaler_Select(SPI0, SPI_PSC_3);                                                // Prescaler outpu = PDIV outpu /3
  77.         SPI_Divider_Select(SPI0, SPI_DIV_2);                                                // DIV outpu = PDIV outpu /2

  78.         /*=== 4. Configure SPI data line, mode and data size... ===*/
  79.         SPI_DataLine_Select(SPI0, SPI_Standard);                                        // SPI data line 1-line Bidirectional~ SPI0_MOSI
  80.         SPI_ModeAndNss_Select(SPI0, SPI_SlaveWithNss);                                // Slave with NSS
  81.         //SPI_ModeAndNss_Select(SPI0, SPI_Slave);                                        // Slave
  82.         SPI_NSSInputSignal_Select(SPI0,SPI_NssPin);                                        // Nss
  83.         SPI_ClockPhase_Select(SPI0, SPI_LeadingEdge);                                // CPHA = 0
  84.         SPI_ClockPolarity_Select(SPI0, SPI_Low);                                        // CPOL = 0
  85.         SPI_FirstBit_Select(SPI0, SPI_MSB);                                                 // MSB first
  86.         SPI_DataSize_Select(SPI0, SPI_8bits);                                                // Data size 8bits
  87.         SPI_SlaveModeReceivedThreshold_Select(SPI0, SPI_1Byte);     // Set SPI0 received data buffer high threshold

  88.         /*=== 5. Config SPI0 IO ===*/
  89.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_Digital_I;          // Pin select digital input mode
  90.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;  // Enable pull up resistor
  91.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  92.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;         // Pin output driver full strength.
  93.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;// Pin input deglitch filter clock divider bypass
  94.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;         // Pin input data not inverse
  95.         
  96.         PINX_InitStruct.PINX_Alternate_Function = 2;                                                 // Pin AFS = 2
  97.         GPIO_PinMode_Config(PINB(0),&PINX_InitStruct);                                          // NSS setup at PB0
  98.         GPIO_PinMode_Config(PINB(2),&PINX_InitStruct);                                          // CLK setup at PB26
  99.         
  100.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_OpenDrain_O; ; //PINX_Mode_PushPull_O;// PINX_Mode_OpenDrain_O;                  // Pin select Push Pull mode
  101.         GPIO_PinMode_Config(PINB(3),&PINX_InitStruct);                                          // MOSI setup at PB3


  102.          PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;   //PINX_Mode_PushPull_O;         // Setting pusu pull mode
  103.          GPIO_PinMode_Config(PINB(1),&PINX_InitStruct);                                          // MISO setup at PB1

  104.          
  105.          PINX_InitStruct.PINX_Alternate_Function = 0;                                                 // Pin AFS = 0
  106.          GPIO_PinMode_Config(PINE(13),&PINX_InitStruct);                                          // D4 setup at PE13
  107.          GPIO_PinMode_Config(PINE(14),&PINX_InitStruct);                                          // D4 setup at PE13
  108.          GPIO_PinMode_Config(PINE(15),&PINX_InitStruct);                                          // D6 setup at PE15

  109.         /*=== 6. Enable SPI ===*/

  110.         
  111.         SPI_Cmd(SPI0, ENABLE);                                                                                          // Enable SPI
  112.   SPI_IT_Config(SPI0, SPI_INT_RX,ENABLE);
  113. //        SPI_ITEA_Cmd(SPI0, ENABLE);


  114. }

  115. void Sample_URT0_Init(void)
  116. {
  117.     URT_BRG_TypeDef  URT_BRG;
  118.     URT_Data_TypeDef DataDef;
  119.             PIN_InitTypeDef PINX_InitStruct;
  120.     //==Set CSC init
  121.     //MG32x02z_CSC_Init.h(Configuration Wizard)
  122.     //Select CK_HS source = CK_IHRCO
  123.     //Select IHRCO = 11.0592M
  124.     //Select CK_MAIN Source = CK_HS
  125.     //Configure PLL->Select APB Prescaler = CK_MAIN/1
  126.     //Configure Peripheral On Mode Clock->Port B/URT0 = Enable
  127.     //Configure Peripheral On Mode Clock->URT0->Select URT0_PR Source = CK_APB(11.0592)
  128.    
  129.     //==Set GPIO init
  130.     //MG32x02z_GPIO_Init.h(Configuration Wizard)->Use GPIOB->Pin8/9
  131.     //GPIO port initial is 0xFFFF
  132.     //Pin8 mode is PPO/Pin9 mode is ODO
  133.     //Pin8/9 pull-up resister Enable
  134.     //Pin8/9 function URT0_TX/RX
  135.           PINX_InitStruct.PINX_Mode                                 = PINX_Mode_PushPull_O;                  // Pin select Push Pull mode
  136.         PINX_InitStruct.PINX_PUResistant                 = PINX_PUResistant_Enable;          // Enable pull up resistor
  137.         PINX_InitStruct.PINX_Speed                                   = PINX_Speed_Low;                        
  138.         PINX_InitStruct.PINX_OUTDrive                         = PINX_OUTDrive_Level0;                 // Pin output driver full strength.
  139.         PINX_InitStruct.PINX_FilterDivider                   = PINX_FilterDivider_Bypass;        // Pin input deglitch filter clock divider bypass
  140.         PINX_InitStruct.PINX_Inverse                         = PINX_Inverse_Disable;                 // Pin input data not inverse
  141.         PINX_InitStruct.PINX_Alternate_Function  = 3;                                // Pin AFS = URT0_TX
  142.         GPIO_PinMode_Config(PINB(8),&PINX_InitStruct);                                                          // TXD at PB8

  143.         PINX_InitStruct.PINX_Mode                                 = PINX_Mode_OpenDrain_O;                 // Pin select Open Drain mode
  144.         PINX_InitStruct.PINX_Alternate_Function  = 3;                                // Pin AFS = URT0_RX
  145.         GPIO_PinMode_Config(PINB(9),&PINX_InitStruct);                                                          // RXD at PB9  
  146.         
  147.         
  148.    
  149.     //=====Set Clock=====//
  150.     //---Set BaudRate---//
  151.     URT_BRG.URT_InteranlClockSource = URT_BDClock_PROC;
  152.     URT_BRG.URT_BaudRateMode = URT_BDMode_Separated;
  153.     URT_BRG.URT_PrescalerCounterReload = 0;                        //Set PSR
  154.     URT_BRG.URT_BaudRateCounterReload = 3;                        //Set RLR
  155.     URT_BaudRateGenerator_Config(URTX, &URT_BRG);                    //BR115200 = f(CK_URTx)/(PSR+1)/(RLR+1)/(OS_NUM+1)
  156.     URT_BaudRateGenerator_Cmd(URTX, ENABLE);                    //Enable BaudRateGenerator
  157.     //---TX/RX Clock---//
  158.     URT_TXClockSource_Select(URTX, URT_TXClock_Internal);        //URT_TX use BaudRateGenerator
  159.     URT_RXClockSource_Select(URTX, URT_RXClock_Internal);        //URT_RX use BaudRateGenerator
  160.     URT_TXOverSamplingSampleNumber_Select(URTX, 25);                //Set TX OS_NUM
  161.     URT_RXOverSamplingSampleNumber_Select(URTX, 25);                //Set RX OS_NUM
  162.     URT_RXOverSamplingMode_Select(URTX, URT_RXSMP_3TIME);
  163.     URT_TX_Cmd(URTX, ENABLE);                                    //Enable TX
  164.     URT_RX_Cmd(URTX, ENABLE);                                    //Enable RX
  165.    
  166.    

  167.     //=====Set Mode=====//
  168.     //---Set Data character config---//
  169.     DataDef.URT_TX_DataLength  = URT_DataLength_8;
  170.     DataDef.URT_RX_DataLength  = URT_DataLength_8;
  171.     DataDef.URT_TX_DataOrder   = URT_DataTyped_LSB;
  172.     DataDef.URT_RX_DataOrder   = URT_DataTyped_LSB;
  173.     DataDef.URT_TX_Parity      = URT_Parity_No;
  174.     DataDef.URT_RX_Parity      = URT_Parity_No;
  175.     DataDef.URT_TX_StopBits    = URT_StopBits_1_0;
  176.     DataDef.URT_RX_StopBits    = URT_StopBits_1_0;
  177.     DataDef.URT_RX_DataInverse = DISABLE;
  178.     DataDef.URT_RX_DataInverse = DISABLE;
  179.     URT_DataCharacter_Config(URTX, &DataDef);
  180.     //---Set Mode Select---//
  181.     URT_Mode_Select(URTX, URT_URT_mode);
  182.     //---Set DataLine Select---//
  183.     URT_DataLine_Select(URTX, URT_DataLine_2);
  184.    
  185.     //=====Set Error Control=====//
  186.     // to do...
  187.    
  188.     //=====Set Bus Status Detect Control=====//
  189.     // to do...
  190.    
  191.     //=====Set Data Control=====//
  192.     URT_RXShadowBufferThreshold_Select(URTX, URT_RXTH_1BYTE);
  193.     URT_IdlehandleMode_Select(URTX, URT_IDLEMode_No);
  194.     URT_TXGaudTime_Select(URTX, 0);
  195.    
  196.     //=====Enable URT Interrupt=====//
  197.     URT_IT_Cmd(URTX, URT_IT_RX, ENABLE);
  198.     URT_ITEA_Cmd(URTX, ENABLE);
  199.     NVIC_EnableIRQ(URT0_IRQn);

  200.     //=====Enable URT=====//
  201.     URT_Cmd(URTX, ENABLE);
  202.                
  203.         //==See MG32x02z_URT0_IRQ.c when interrupt in
  204. }

  205. int fputc(int ch,FILE *f)
  206. {
  207.         
  208.         URT_SetTXData(URTX,1,ch);
  209.         while(URT_GetITSingleFlagStatus(URTX,URT_IT_TC)==DRV_UnHappened);
  210.         URT_ClearITFlag(URTX,URT_IT_TC);
  211.         
  212.         return ch;
  213. }

  214. void UartSendByte(int ch)
  215. {
  216.         
  217.         URT_SetTXData(URTX,1,ch);
  218.         while(URT_GetITSingleFlagStatus(URTX,URT_IT_TC)==DRV_UnHappened);
  219.         URT_ClearITFlag(URTX,URT_IT_TC);
  220.         
  221. }

  222. void DMA_Init(void)
  223. {
  224.          DMA_BaseInitTypeDef DMATestPattern;

  225.     // ------------------------------------------------------------------------
  226.     // 1.Enable DMA
  227.     DMA_Cmd(ENABLE);
  228.    
  229.     // ------------------------------------------------------------------------
  230.     // 2.Enable Channel0
  231.     DMA_Channel_Cmd(DMAChannel0, ENABLE);
  232.    
  233.     // ------------------------------------------------------------------------
  234.     DMA_BaseInitStructure_Init(&DMATestPattern);
  235.    
  236.     // 3.initial & modify parameter
  237.       
  238.         // DMA channel select
  239.         DMATestPattern.DMAChx = DMAChannel0;
  240.         
  241.         // channel x source/destination auto increase address
  242.         DMATestPattern.SrcSINCSel = DISABLE;//ENABLE;
  243.         DMATestPattern.DestDINCSel = ENABLE;
  244.         
  245.         // DMA source peripheral config
  246.         DMATestPattern.SrcSymSel = DMA_SPI0_RX;
  247.         
  248.         // DMA destination peripheral config
  249.         DMATestPattern.DestSymSel = DMA_MEM_Write;
  250.         
  251.         // DMA Burst size config
  252.         DMATestPattern.BurstDataSize = DMA_BurstSize_1Byte;
  253.         
  254.         // DMA transfer data count initial number
  255.         DMATestPattern.DMATransferNUM = MYBINARYIMAGE2_LENGTH;
  256.    
  257.         // source/destination config
  258.         DMATestPattern.DMASourceAddr = &SPI0->RDAT;
  259.         DMATestPattern.DMADestinationAddr = (uint32_t *)&RcvBuf;//DMA_URT0_TX;// (uint8_t *)&RcvBuf;
  260.                                 
  261.                                 SPI_DMAReceive_Cmd(SPI0, ENABLE);
  262.                                 DMA_Channel_Cmd(DMAChannel0, ENABLE);
  263.                                 DMA_Base_Init(&DMATestPattern);
  264. }

  265. int main()
  266. {
  267.         u32 i;
  268.         PIN_InitTypeDef PINX_InitStruct;
  269.         CSC_Init();
  270.         InitSPI0();        
  271.                 Sample_URT0_Init();
  272.                 DMA_Init();
  273.         DMA_StartRequest(DMAChannel0);
  274.         i=0;
  275.     while(1)
  276.     {
  277.             i++;
  278.             if(i>=100000)
  279.             {
  280.                                 i=0;
  281.                                 
  282.                                 SPI_DMAReceive_Cmd(SPI0, ENABLE);        
  283.                                 if(RcvBuf[0]!=0)printf("RX:0x%02X\n",RcvBuf[0]);                        
  284.                                 memset(RcvBuf,0,20*sizeof(u8));
  285.                                 DMA_ClearFlag(DMA, DMA_FLAG_CH0_TCF);
  286.                                 DMA_Channel_Cmd(DMAChannel0, DISABLE);
  287.                                 DMA_SetDestinationAddress(DMAChannel0, RcvBuf);
  288.                                 DMA_Channel_Cmd(DMAChannel0, ENABLE);
  289.                                 DMA_StartRequest(DMAChannel0);

  290.             }

  291.     }
  292. }


quickman 发表于 2022-7-3 14:12 | 显示全部楼层
相当全的资料,很适合初学者                                 
beacherblack 发表于 2022-7-3 14:26 | 显示全部楼层
以后学习就方便了                                 
vivilyly 发表于 2022-7-3 14:57 | 显示全部楼层
分享的资料很高端呢。                                    
tabmone 发表于 2022-7-3 15:33 | 显示全部楼层
很详细的开发板资料                                 
lzbf 发表于 2022-7-3 15:47 | 显示全部楼层
资料还是相当全面的                                 
sheflynn 发表于 2022-7-3 16:02 | 显示全部楼层
谢谢lz分享,很有用                                 
modesty3jonah 发表于 2022-7-3 16:15 | 显示全部楼层
资料还是相当全面的                                 
juliestephen 发表于 2022-7-3 16:29 | 显示全部楼层
回复查看一下相关的内容。                                 
selongli 发表于 2022-7-3 16:43 | 显示全部楼层
很详细的开发板资料                                 
soodesyt 发表于 2022-7-3 17:39 | 显示全部楼层
以后学习就方便了                                 
cashrwood 发表于 2022-7-3 17:53 | 显示全部楼层
正准备用 来做产品,刚好用上。         
rosemoore 发表于 2022-7-3 18:07 | 显示全部楼层
这些资料太全了!!!                 
tifmill 发表于 2022-7-3 18:21 | 显示全部楼层
很详细的开发板资料                                 
yeates333 发表于 2022-7-3 19:20 | 显示全部楼层
资料还是相当全面的                                 
pklong 发表于 2022-7-3 19:31 | 显示全部楼层
以后多交流交流                  
lzmm 发表于 2022-7-3 19:51 | 显示全部楼层
感谢分享,提供的例程很实用                                 
232321122 发表于 2022-7-3 20:02 | 显示全部楼层
楼主的资料确实全面,非常感谢            
olivem55arlowe 发表于 2022-7-3 20:13 | 显示全部楼层
资料还是相当全面的                                 
htmlme 发表于 2022-7-3 20:25 | 显示全部楼层
谢谢你共享的资料                                 
您需要登录后才可以回帖 登录 | 注册

本版积分规则

26

主题

82

帖子

3

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