#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_chip.h"
#include <stdint.h>
#include <stdbool.h>
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"
uint8_t received_flag=0;//
// Receive data buffer
uint8_t buffer;
uint8_t tx_buffer=0x6F;
uint8_t tx_buffer_new=0x06;
void USART0_RX_IRQHandler(void)
{
received_flag=1;
// Get the character just received
buffer = USART0->RXDATA;
switch (buffer)
{
case 'a':
tx_buffer_new=0x0F;
break;
case 'b':
tx_buffer_new=0x2F;
break;
case 'c':
tx_buffer_new=0x4F;
break;
default:
break;
}//end switch
// Clear the requesting interrupt before exiting the handler
USART_IntClear(USART0, USART_IF_RXDATAV);
}
int main(void)
{
uint32_t i;
// Chip errata
CHIP_Init();
CMU_ClockEnable(cmuClock_GPIO, true);
CMU_ClockEnable(cmuClock_USART0, true);
//EFR32fg14 LOC2 page 157 data sheet TX P0
GPIO_PinModeSet(gpioPortA,2, gpioModePushPull, 1);
//EFR32fg14 LOC2 page 157 data sheet RX P2
GPIO_PinModeSet(gpioPortA,3, gpioModeInput, 0);
GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 1);
// Default asynchronous initializer (115.2 Kbps, 8N1, no flow control)
USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;
// Configure and enable USART0
USART_InitAsync(USART0, &init);
//datasheet page 157 location2 rx portA pin3(P2) ,tx port A pin2(P0)
USART0->ROUTELOC0 = USART_ROUTELOC0_RXLOC_LOC2 | USART_ROUTELOC0_TXLOC_LOC2;
USART0->ROUTEPEN |= USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN;
// Enable NVIC USART sources
NVIC_ClearPendingIRQ(USART0_RX_IRQn);
NVIC_EnableIRQ(USART0_RX_IRQn);
NVIC_ClearPendingIRQ(USART0_TX_IRQn);
NVIC_EnableIRQ(USART0_TX_IRQn);
while (1)
{
if (received_flag==1)
{
USART_Tx(USART0,tx_buffer_new);
USART_Tx(USART1,'\n');
tx_buffer=tx_buffer_new;
received_flag=0;
}
else
{
USART_Tx(USART0,tx_buffer);
USART_Tx(USART1,'\n');
}
for(i=0;i<115;i++)
{
GPIO_PinOutSet(gpioPortA,5);
GPIO_PinOutClear(gpioPortA,5);
}
// Enable receive data valid interrupt
USART_IntEnable(USART0, USART_IEN_RXDATAV);
}
}