UART API
for EFM32 Happy Gecko ARM Cortex M0+
for EFM32 Happy Gecko ARM Cortex M0+
#include
"em_system.h"
#include
"em_chip.h"
#include
"em_cmu.h"
#include
"em_gpio.h"
#include
"em_usart.h"
#define COM_PORT gpioPortC
#define TX_PIN
0
#define RX_PIN
1
const
char
test_string[] =
"\n\EFM32 Happy Gecko!\n\r"
;
char
rx_char;
int
main () {
uint8_t i;
CHIP_Init();
CMU_OscillatorEnable(cmuOsc_HFXO,
true
,
true
);
// enable HF XTAL osc and wait for it to stabilize
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
// select HF XTAL osc as system clock source (24MHz)
CMU_ClockEnable(cmuClock_GPIO,
true
);
// enable GPIO peripheral clock
CMU_ClockEnable(cmuClock_USART1,
true
);
// enable USART1 peripheral clock
GPIO_PinModeSet(COM_PORT, TX_PIN, gpioModePushPull,
1
);
// set TX pin to push-pull output, initialize high (otherwise glitches can occur)
GPIO_PinModeSet(COM_PORT, RX_PIN, gpioModeInput,
0
);
// set RX pin as input (no filter)
USART_InitAsync_TypeDef uartInit =
{
.enable = usartDisable,
// wait to enable transmitter and receiver
.refFreq =
0
,
// setting refFreq to 0 will invoke the CMU_ClockFreqGet() function and measure the HFPER clock
.baudrate =
115200
,
// desired baud rate
.oversampling = usartOVS16,
// set oversampling to x16
.databits = usartDatabits8,
// 8 data bits
.parity = usartNoParity,
// no parity bits
.stopbits = usartStopbits1,
// 1 stop bit
.mvdis =
false
,
// use majority voting
.prsRxEnable =
false
,
// not using PRS input
.prsRxCh = usartPrsRxCh0,
// doesn't matter what channel we select
};
USART_InitAsync(USART1, &uartInit);
// apply configuration to USART1
USART1->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | _USART_ROUTE_LOCATION_LOC0;
// clear buffers, enable transmitter and receiver pins
USART_IntClear(USART1, _USART_IF_MASK);
// clear all USART interrupt flags
NVIC_ClearPendingIRQ(USART1_RX_IRQn);
// clear pending RX interrupt flag in NVIC
NVIC_ClearPendingIRQ(USART1_TX_IRQn);
// clear pending TX interrupt flag in NVIC
USART_Enable(USART1, usartEnable);
// enable transmitter and receiver
for
(i=
0
; i<sizeof(test_string); i++) {
while
(!(USART1->STATUS & (
1
<<
6
)));
// wait for TX buffer to empty
USART1->TXDATA = test_string[i];
// send character
}
while
(
1
) {
if
(USART1->STATUS & (
1
<<
7
)) {
// if RX buffer contains valid data
rx_char = USART1->RXDATA;
// store the data
}
if
(rx_char) {
// if we have a valid character
while
(!(USART1->STATUS & (
1
<<
6
)));
// wait for TX buffer to empty
USART1->TXDATA = rx_char;
// echo received char
rx_char =
0
;
// clear temp variable
}
}
}
Комментариев нет:
Отправить комментарий