Hardware preparation: tg stk. Connect the 4-pin and 6-pin ports of the 20 PIN expansion port. 4th feet are PD0, Tx, 6th feet are PD1, Rx.
Software Process: After the UART is configured, the main function continuously triggers the DMA transmission of TX, transmitting 12 bytes of data in total, 0 ~ 9, '\ n',' \ R '. Rx adopts normal interrupt.
# Include <stdint. h>
# Include <stdbool. h>
# Include "efm32.h"
# Include "em_chip.h"
# Include "em_cmu.h"
# Include "em_gpio.h"
# Include "em_usart.h"
# Include "em_dma.h"
# Define DMA_CHANNEL_UART_TX 0 // defines the DMA transmission channel. 0 indicates that the channel is 0.
# Define SPITXSAMPLES (12) // defines the number of transmitted bytes. The maximum value is 1024 bytes.
// Define the global variables of the DMA controller. Do not change
/* DMA init structure */
DMA_Init_TypeDef dmaInit;
/* DMA callback structure */
DMA_CB_TypeDef cb [DMA_CHAN_COUNT]; // The total number of DMA_CHAN_COUN Channels
/** DMA control block, requires proper alignment .*/
# If defined (_ ICCARM __)
# Pragma data_alignment = 256
DMA_DESCRIPTOR_TypeDef dmaControlBlock [DMA_CHAN_COUNT * 2];
# Elif defined (_ CC_ARM)
DMA_DESCRIPTOR_TypeDef dmaControlBlock [DMA_CHAN_COUNT * 2] _ attribute _ (aligned (256 )));
# Elif defined (_ GNUC __)
DMA_DESCRIPTOR_TypeDef dmaControlBlock [DMA_CHAN_COUNT * 2] _ attribute _ (aligned (256 )));
# Else
# Error Undefined toolkit, need to define alignment
# Endif
Unsigned char ucUARTTxBuffer [SPITXSAMPLES]; // defines a 1 K-byte buffer for Tx Transmission Testing
Unsigned char ucComplete = 0;
// Receiving interrupt function of USART1
Void usart+rx_irqhandler (void)
{
USART_Rx (USART1); // The receiving interrupt flag is automatically cleared after reading the Rx data.
}
Void uart_initial (void)
{
Cmu_clockenable (cmuclock_hfper, true );
Cmu_clockenable (cmuclock_gpio, true );
Cmu_clockenable (cmuclock_usart1, true );
Usart_initasync_typedef uart_init = usart_initasync_default;
// Set the baud rate of usart1 to 9600
Uart_init.baudrate = 9600;
// Initialize usart1
Usart_initasync (usart1, & uart_init );
// First clear the RX interrupt flag and enable the RX interrupt
Usart1-> IFC = _ usart_ifc_mask;
Nvic_clearpendingirq (usart1_rx_irqn );
Nvic_enableirq (usart1_rx_irqn );
USART1-> IEN = USART_IEN_RXDATAV;
// USART1 GPIO port settings
GPIO_PinModeSet (gpioPortD, 0, gpioModePushPull, 1); // tx
GPIO_PinModeSet (gpioPortD, 1, gpioModeInput, 1); // rx
USART1-> ROUTE | = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC1;
}
Void UARTTxTransferComplete (unsigned int channel, bool primary, void * user)
{
UcComplete = 1;
}
Void DMA_for_UART_Tx (void)
{
DMA_CfgChannel_TypeDef chnlCfg;
Dma_includescr_typedef descrCfg;
/* Setting up call-back function */
Cb [DMA_CHANNEL_UART_TX]. cbFunc = UARTTxTransferComplete; // whether to call the callback function after the DMA is set. If no callback function is available, set it to NULL.
Cb [DMA_CHANNEL_UART_TX]. userPtr = NULL;
/* Setting up channel */
ChnlCfg. highPri = false;
ChnlCfg. enableInt = true;
// Set the conditions for sending the next byte of DMA. Here, when Tx is sent, the DMA starts to remove one byte for sending.
ChnlCfg. select = dmareq_usart+txempty;
// Initialize the callback function in the DMA register
ChnlCfg. cb = & (cb [DMA_CHANNEL_UART_TX]);
Dma_channelchannel (DMA_CHANNEL_UART_TX, & chnlCfg );
// Define the register of the DMA Logic Part
/* Setting Up Channel descriptor */
Descrcfg. dstinc = dmadataincnone; // The target address remains unchanged.
Descrcfg. srcinc = dmadatainc1; // indicates that the source address is added with 1 byte each time.
Descrcfg. size = dmadatasize1; // The unit of bytes sent each time
Descrcfg. arbrate = dmaarbitrate1; // DMA arbitration, arbitration once every 1 byte sent
Descrcfg. hprot = 0;
Dma_includescr (dma_channel_uart_tx, true, & descrcfg );
// Activate one DMA Transmission
Dma_activatebasic (dma_channel_uart_tx,
True,
False,
(Void *) & (usart1-> txdata ),
(Void *) & ucuarttxbuffer,
Spitxsamples-1 );
}
// DMA initialization, no need to change
Void dmainit (void)
{
Cmu_clockenable (cmuclock_dma, true );
/* Initializing the DMA */
Dmainit. hprot = 0;
Dmainit. controlblock = dmacontrolblock;
DMA_Init (& dmaInit );
}
// Demo used to initialize the sending Buffer
Void Uart_Tx_Data_Init (void)
{
Unsigned char ucData = '0 ';
Unsigned long ucCount = 0;
For (ucCount = 0; ucCount <SPITXSAMPLES-2; ucCount ++)
{
UcUARTTxBuffer [ucCount] = ucData;
If (ucData <'9') ucData ++;
Else ucData = '0 ';
}
UcUARTTxBuffer [ucCount] = '\ n ';
UcCount ++;
UcUARTTxBuffer [ucCount] = '\ R ';
}
/*************************************** ***********************************//**
* @ Brief Main function
**************************************** *************************************/
/*************************************** ***********************************//**
* @ Brief Main function
**************************************** *************************************/
Int main (void)
{
/* Chip errata */
CHIP_Init ();
SystemCoreClockUpdate ();
Uart_Tx_Data_Init ();
DMAInit ();
UART_Initial ();
DMA_for_UART_Tx ();
UcComplete = 1;
/* Infinite blink loop */
While (1)
{
// Whether the last DMA request has been sent
If (ucComplete)
{
Unsigned long ulDelay = 800000;
While (ulDelay --);
DMA_ActivateBasic (DMA_CHANNEL_UART_TX,
True,
False,
NULL,
NULL,
SPITXSAMPLES-1 );
UcComplete = 0;
}
}
}