Based on the basic polling, set Rx to interrupt reception.
Hardware environment: tg stk, transient connection of PD0 and PD1, Tx and Rx of usart1, without using the internal loopback function. Corresponds to the 4th and 6th ports of the tg stk 20 pin expansion port.
Software environment: in the main loop, data is continuously sent. In the Rx interrupt, the received data is determined. If it is a special word, the LED control pin is reversed.
The Code is as follows:
# Include <stdint. h>
# Include <stdbool. h>
# Include "efm32.h"
# Include "efm32_chip.h"
# Include "efm32_cmu.h"
# Include "efm32_gpio.h"
# Include "efm32_usart.h"
Void usart+rx_irqhandler (void)
{
If (USART_Rx (USART1) = 0x5)
{
GPIO_PinOutToggle (gpioPortD, 7 );
}
}
Void SPI_Initial (void)
{
CMU_ClockEnable (cmuClock_HFPER, true );
Cmu_clockenable (cmuclock_gpio, true );
Cmu_clockenable (cmuclock_usart1, true );
Usart_initsync_typedef spi_init = usart_initsync_default;
Spi_init.baudrate = 200000;
Usart_initsync (usart1, & spi_init );
Usart1-> IFC = _ usart_ifc_mask;
Nvic_clearpendingirq (usart1_rx_irqn );
Nvic_enableirq (usart1_rx_irqn );
Usart1-> ien = usart_ien_rxdatav;
Gpio_pinmodeset (gpioportd, 0, gpiomodepushpull, 1); // TX
Gpio_pinmodeset (gpioportd, 1, gpiomodeinput, 1); // RX
GPIO_PinModeSet (gpioPortD, 2, gpioModePushPull, 1); // clk
GPIO_PinModeSet (gpioPortD, 3, gpioModePushPull, 1); // cs
USART1-> ROUTE | = USART_ROUTE_CSPEN | USART_ROUTE_CLKPEN | USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_LOCATION_LOC1;
}
/*************************************** ***********************************//**
* @ Brief Main function
**************************************** *************************************/
/*************************************** ***********************************//**
* @ Brief Main function
**************************************** *************************************/
Int main (void)
{
/* Chip errata */
Chip_init ();
Systemcoreclockupdate ();
Spi_initial ();
Cmu_clockenable (cmuclock_gpio, true );
Gpio_pinmodeset (gpioportd, 7, gpiomodepushpull, 0 );
Unsigned char ucdata = 0;
/* Infinite blink loop */
While (1)
{
Unsigned long uldelay = 100000;
While (uldelay --);
If (ucdata <10) ucdata ++;
Else ucdata = 0;
Usart_tx (usart1, ucdata );
}
}
The effect is as follows: