上回我们说到了MG32F02A的UART0进行串口输出。
由于CPU的资源是非常宝贵的,所以我们希望能够不利用CPU资源就进行数据从UART0输出,这时候就需要使用我们的DMA功能了,虽然从代码上看CPU的事情还是有一些,但是如果作为接收方,那么DMA就会非常方便,当然这都是后话了。
如下程序中为UART0使用DMA方式进行输出。在该程序中,你会发现笙泉M0的DMA使用起来比较方便,不需要选择方向,直接设置DMA起始方和DMA接收方即可,而且笙泉的DMA通道比较简单,只有3个通道,且通道0是全功能通道,所以,只要设置为通道0,你的DMA就一定能用,不需要到处查手册。另外一点需要注意,从内存发送到外设时每发送完一次都会自动关闭外设的DMA使能以避免DMA疯狂传输,所以需要重新启动一次。
- #include "MG32x02z_DRV.H"
- #include
- #define URTX URT0
- #define URTX URT0
- #define MYBINARYIMAGE2_LENGTH 20
- uint8_t SendBuf[MYBINARYIMAGE2_LENGTH]={1,2,3,4,5,6,7,8,9};//__attribute__((at(0x20001000)));
- typedef uint8_t u8;
- typedef uint16_t u16;
- typedef uint32_t u32;
- typedef uint64_t u64;
- void CSC_Init (void)
- {
- CSC_PLL_TyprDef CSC_PLL_CFG;
- UnProtectModuleReg(MEMprotect); // Setting flash wait state
- MEM_SetFlashWaitState(MEM_FWAIT_ONE); // 50MHz> Sysclk >=25MHz
- ProtectModuleReg(MEMprotect);
- UnProtectModuleReg(CSCprotect);
- CSC_CK_APB_Divider_Select(APB_DIV_1); // Modify CK_APB divider APB=CK_MAIN/1
- CSC_CK_AHB_Divider_Select(AHB_DIV_1); // Modify CK_AHB divider AHB=APB/1
- /* CK_HS selection */
- CSC_IHRCO_Select(IHRCO_12MHz); // IHRCO Sel 12MHz
- CSC_IHRCO_Cmd(ENABLE);
- while(CSC_GetSingleFlagStatus(CSC_IHRCOF) == DRV_Normal);
- CSC_ClearFlag(CSC_IHRCOF);
- CSC_CK_HS_Select(HS_CK_IHRCO); // CK_HS select IHRCO
- /* PLL */
- /**********************************************************/
- CSC_PLL_CFG.InputDivider=PLLI_DIV_2; // 12M/2=6M
- CSC_PLL_CFG.Multiplication=PLLIx16; // 6M*16=96M
- CSC_PLL_CFG.OutputDivider=PLLO_DIV_2; // PLLO=96M/2=48M
- CSC_PLL_Config(&CSC_PLL_CFG);
- CSC_PLL_Cmd(ENABLE);
- while(CSC_GetSingleFlagStatus(CSC_PLLF) == DRV_Normal);
- CSC_ClearFlag(CSC_PLLF);
- /**********************************************************/
- /* CK_MAIN */
- CSC_CK_MAIN_Select(MAIN_CK_HS);
- /* Configure ICKO function */
- /* Configure peripheral clock */
- CSC_PeriphProcessClockSource_Config(CSC_UART0_CKS, CK_APB);
- CSC_PeriphOnModeClock_Config(CSC_ON_UART0,ENABLE);
- CSC_PeriphOnModeClock_Config(CSC_ON_PortB,ENABLE);
- CSC_PeriphOnModeClock_Config(CSC_ON_DMA,ENABLE);
- ProtectModuleReg(CSCprotect);
- }
- int fputc(int ch,FILE *f)
- {
- URT_SetTXData(URTX,1,ch);
- while(URT_GetITSingleFlagStatus(URTX,URT_IT_TC)==DRV_UnHappened);
- URT_ClearITFlag(URTX,URT_IT_TC);
- return ch;
- }
- void UartSendByte(int ch)
- {
- URT_SetTXData(URTX,1,ch);
- while(URT_GetITSingleFlagStatus(URTX,URT_IT_TC)==DRV_UnHappened);
- URT_ClearITFlag(URTX,URT_IT_TC);
- }
- void URT0_Init(void)
- {
- URT_BRG_TypeDef URT_BRG;
- URT_Data_TypeDef DataDef;
- PIN_InitTypeDef PINX_InitStruct;
- //==Set GPIO init
- //PB8 PPO TX ,PB9 ODO RX
- PINX_InitStruct.PINX_Mode = PINX_Mode_PushPull_O; // Pin select Push Pull mode
- PINX_InitStruct.PINX_PUResistant = PINX_PUResistant_Enable; // Enable pull up resistor
- PINX_InitStruct.PINX_Speed = PINX_Speed_Low;
- PINX_InitStruct.PINX_OUTDrive = PINX_OUTDrive_Level0; // Pin output driver full strength.
- PINX_InitStruct.PINX_FilterDivider = PINX_FilterDivider_Bypass; // Pin input deglitch filter clock divider bypass
- PINX_InitStruct.PINX_Inverse = PINX_Inverse_Disable; // Pin input data not inverse
- PINX_InitStruct.PINX_Alternate_Function = PB8_AF_URT0_TX; // Pin AFS = URT0_TX
- GPIO_PinMode_Config(PINB(8),&PINX_InitStruct); // TXD at PB8
- PINX_InitStruct.PINX_Mode = PINX_Mode_OpenDrain_O; // Pin select Open Drain mode
- PINX_InitStruct.PINX_Alternate_Function = PB9_AF_URT0_RX; // Pin AFS = URT0_RX
- GPIO_PinMode_Config(PINB(9),&PINX_InitStruct); // RXD at PB9
- //=====Set Clock=====//
- //---Set BaudRate---//
- URT_BRG.URT_InteranlClockSource = URT_BDClock_PROC;
- URT_BRG.URT_BaudRateMode = URT_BDMode_Separated;
- URT_BRG.URT_PrescalerCounterReload = 0; //Set PSR
- URT_BRG.URT_BaudRateCounterReload = 3; //Set RLR
- URT_BaudRateGenerator_Config(URTX, &URT_BRG); //BR115200 = f(CK_URTx)/(PSR+1)/(RLR+1)/(OS_NUM+1)
- URT_BaudRateGenerator_Cmd(URTX, ENABLE); //Enable BaudRateGenerator
- //---TX/RX Clock---//
- URT_TXClockSource_Select(URTX, URT_TXClock_Internal); //URT_TX use BaudRateGenerator
- URT_RXClockSource_Select(URTX, URT_RXClock_Internal); //URT_RX use BaudRateGenerator
- URT_TXOverSamplingSampleNumber_Select(URTX, 25); //Set TX OS_NUM
- URT_RXOverSamplingSampleNumber_Select(URTX, 25); //Set RX OS_NUM
- URT_RXOverSamplingMode_Select(URTX, URT_RXSMP_3TIME);
- URT_TX_Cmd(URTX, ENABLE); //Enable TX
- URT_RX_Cmd(URTX, ENABLE); //Enable RX
- //=====Set Mode=====//
- //---Set Data character config---//
- DataDef.URT_TX_DataLength = URT_DataLength_8;
- DataDef.URT_RX_DataLength = URT_DataLength_8;
- DataDef.URT_TX_DataOrder = URT_DataTyped_LSB;
- DataDef.URT_RX_DataOrder = URT_DataTyped_LSB;
- DataDef.URT_TX_Parity = URT_Parity_No;
- DataDef.URT_RX_Parity = URT_Parity_No;
- DataDef.URT_TX_StopBits = URT_StopBits_1_0;
- DataDef.URT_RX_StopBits = URT_StopBits_1_0;
- DataDef.URT_TX_DataInverse = DISABLE;
- DataDef.URT_RX_DataInverse = DISABLE;
- URT_DataCharacter_Config(URTX, &DataDef);
- //---Set Mode Select---//
- URT_Mode_Select(URTX, URT_URT_mode);
- //---Set DataLine Select---//
- URT_DataLine_Select(URTX, URT_DataLine_2);
- //=====Set Data Control=====//
- URT_RXShadowBufferThreshold_Select(URTX, URT_RXTH_1BYTE);
- URT_IdlehandleMode_Select(URTX, URT_IDLEMode_No);
- URT_TXGaudTime_Select(URTX, 0);
- //=====Enable URT Interrupt=====//
- //URT_IT_Cmd(URTX, URT_IT_RX, ENABLE);
- //URT_ITEA_Cmd(URTX, ENABLE);
- //NVIC_EnableIRQ(URT0_IRQn);
- //=====Enable URT=====//
- URT_Cmd(URTX, ENABLE);
- //==See MG32x02z_URT0_IRQ.c when interrupt in
- }
- void DMA_Init(void)
- {
- DMA_BaseInitTypeDef DMATestPattern;
- // ------------------------------------------------------------------------
- // 1.Enable DMA
- DMA_Cmd(ENABLE);
- // ------------------------------------------------------------------------
- // 2.Enable Channel0
- DMA_Channel_Cmd(DMAChannel0, ENABLE);
- // ------------------------------------------------------------------------
- DMA_BaseInitStructure_Init(&DMATestPattern);
- // 3.initial & modify parameter
- // DMA channel select
- DMATestPattern.DMAChx = DMAChannel0;
- // channel x source/destination auto increase address
- DMATestPattern.SrcSINCSel = ENABLE; //内存需要地址自增
- DMATestPattern.DestDINCSel = DISABLE; //外设不需要地址自增
- // DMA source peripheral config
- DMATestPattern.SrcSymSel = DMA_MEM_Read;
- // DMA destination peripheral config
- DMATestPattern.DestSymSel = DMA_URT0_TX;
- // DMA Burst size config
- DMATestPattern.BurstDataSize = DMA_BurstSize_1Byte;
- // DMA transfer data count initial number
- DMATestPattern.DMATransferNUM = MYBINARYIMAGE2_LENGTH;
- // source/destination config
- DMATestPattern.DMASourceAddr = (uint8_t *)&SendBuf;
- DMATestPattern.DMADestinationAddr = &URT0->TDAT;
- URT_TXDMA_Cmd(URT0, ENABLE);
- DMA_Base_Init(&DMATestPattern);
- }
- int main()
- {
- int i;
- CSC_Init();
- URT0_Init();
- printf("Hello!n");
- DMA_Init();
- while(1)
- {
- i++;
- if(i>=500000)
- {
- i=0;
- strcpy(SendBuf,"i am source");
- DMA_ClearFlag(DMA, DMA_FLAG_CH0_TCF);
- DMA_StartRequest(DMAChannel0);
- while (DMA_GetSingleFlagStatus(DMA, DMA_FLAG_CH0_TCF) == DRV_UnHappened);
- DMA_ClearFlag(DMA, DMA_FLAG_CH0_TCF);
- URT_TXDMA_Cmd(URT0, ENABLE); //每次发送完需要重新使能
- }
- }
- }
就这样,使能UART0的DMA功能后只要往我设定好的SendBuf中丢数据,数据就会自动搬运到UART的DAT寄存器中并发送出去,以此减小CPU消耗。
|