USART Serial example for SAM.
Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.
#include <string.h>
#include "asf.h"
#include "stdio_serial.h"
#include "conf_board.h"
#include "conf_clock.h"
#include "conf_example.h"
Macros | |
#define | ALL_INTERRUPT_MASK 0xffffffff |
All interrupt mask. More... | |
#define | BUFFER_SIZE 100 |
Size of the receive buffer used by the PDC, in bytes. More... | |
#define | BYTE_TRANSFER 0 |
USART FIFO transfer type definition. More... | |
#define | MAX_BUF_NUM 1 |
Max buffer number. More... | |
#define | PDC_TRANSFER 1 |
USART PDC transfer type definition. More... | |
#define | STRING_EOL "\r" |
#define | STRING_HEADER |
#define | TC_FREQ 1 |
Timer counter frequency in Hz. More... | |
Functions | |
static void | configure_console (void) |
Configure UART for debug message output. More... | |
static void | configure_tc (void) |
Configure Timer Counter 0 (TC0) to generate an interrupt every 200ms. More... | |
static void | configure_usart (void) |
Configure USART in normal (serial rs232) mode, asynchronous, 8 bits, 1 stop bit, no parity, 115200 bauds and enable its transmitter and receiver. More... | |
static void | display_main_menu (void) |
Display main menu. More... | |
int | main (void) |
Application entry point for usart_serial example. More... | |
void | TC0_Handler (void) |
Interrupt handler for TC0. More... | |
static void | usart_clear (void) |
Reset the TX & RX, and clear the PDC counter. More... | |
void | USART_Handler (void) |
Interrupt handler for USART. More... | |
Variables | |
Pdc * | g_p_pdc |
Pointer to PDC register base. More... | |
pdc_packet_t | g_st_nextpacket |
pdc_packet_t | g_st_packet |
PDC data packet. More... | |
static uint8_t | g_uc_transend_flag = 0 |
Flag of one transfer end. More... | |
static uint8_t | gs_puc_buffer [2][BUFFER_SIZE] |
Receive buffer. More... | |
static uint8_t | gs_puc_nextbuffer [2][BUFFER_SIZE] |
Next Receive buffer. More... | |
static uint8_t | gs_uc_buf_num = 0 |
Buffer number in use. More... | |
static uint8_t | gs_uc_trans_mode = PDC_TRANSFER |
Current transfer mode. More... | |
static uint32_t | gs_ul_read_buffer = 0 |
Byte mode read buffer. More... | |
static uint32_t | gs_ul_size_buffer = BUFFER_SIZE |
Current bytes in buffer. More... | |
static uint32_t | gs_ul_size_nextbuffer = BUFFER_SIZE |
Current bytes in next buffer. More... | |
#define ALL_INTERRUPT_MASK 0xffffffff |
All interrupt mask.
Referenced by configure_usart().
#define BUFFER_SIZE 100 |
Size of the receive buffer used by the PDC, in bytes.
Referenced by main(), TC0_Handler(), and USART_Handler().
#define BYTE_TRANSFER 0 |
USART FIFO transfer type definition.
Referenced by main().
#define MAX_BUF_NUM 1 |
Max buffer number.
Referenced by USART_Handler().
#define PDC_TRANSFER 1 |
USART PDC transfer type definition.
Referenced by main(), TC0_Handler(), and USART_Handler().
#define STRING_EOL "\r" |
#define STRING_HEADER |
Referenced by main().
#define TC_FREQ 1 |
Timer counter frequency in Hz.
Referenced by configure_tc().
|
static |
Configure UART for debug message output.
References uart_rs232_options::baudrate, stdio_serial_init(), and sysclk_enable_peripheral_clock().
Referenced by main().
|
static |
Configure Timer Counter 0 (TC0) to generate an interrupt every 200ms.
This interrupt will be used to flush USART input and echo back.
References pmc_enable_periph_clk(), sysclk_get_cpu_hz(), tc_enable_interrupt(), tc_find_mck_divisor(), TC_FREQ, tc_init(), and tc_write_rc().
Referenced by main().
|
static |
Configure USART in normal (serial rs232) mode, asynchronous, 8 bits, 1 stop bit, no parity, 115200 bauds and enable its transmitter and receiver.
References ALL_INTERRUPT_MASK, SAM4L, sysclk_enable_peripheral_clock(), sysclk_get_peripheral_hz(), usart_disable_interrupt(), usart_enable_rx(), usart_enable_tx(), and usart_init_rs232().
Referenced by main().
|
static |
Display main menu.
Referenced by main().
int main | ( | void | ) |
Application entry point for usart_serial example.
References board_init(), BUFFER_SIZE, BYTE_TRANSFER, configure_console(), configure_tc(), configure_usart(), display_main_menu(), g_p_pdc, gs_puc_buffer, gs_puc_nextbuffer, gs_uc_buf_num, gs_uc_trans_mode, gs_ul_size_buffer, gs_ul_size_nextbuffer, pdc_disable_transfer(), pdc_enable_transfer(), pdc_rx_init(), PDC_TRANSFER, STRING_HEADER, sysclk_init(), tc_start(), uart_read(), pdc_packet::ul_addr, pdc_packet::ul_size, usart_clear(), usart_disable_interrupt(), usart_enable_interrupt(), and usart_get_pdc_base().
void TC0_Handler | ( | void | ) |
Interrupt handler for TC0.
Record the number of bytes received, and then restart a read transfer on the USART if the transfer was stopped.
References BUFFER_SIZE, g_p_pdc, g_uc_transend_flag, gs_uc_trans_mode, gs_ul_size_buffer, gs_ul_size_nextbuffer, pdc_read_rx_counter(), pdc_read_rx_next_counter(), pdc_rx_clear_cnt(), PDC_TRANSFER, and tc_get_status().
|
static |
Reset the TX & RX, and clear the PDC counter.
References g_p_pdc, pdc_rx_init(), pdc_packet::ul_addr, pdc_packet::ul_size, usart_enable_rx(), usart_enable_tx(), usart_reset_rx(), and usart_reset_tx().
Referenced by main().
void USART_Handler | ( | void | ) |
Interrupt handler for USART.
Echo the bytes received and start the next receive.
References BUFFER_SIZE, g_p_pdc, g_uc_transend_flag, gs_puc_buffer, gs_puc_nextbuffer, gs_uc_buf_num, gs_uc_trans_mode, gs_ul_read_buffer, gs_ul_size_buffer, gs_ul_size_nextbuffer, MAX_BUF_NUM, pdc_rx_init(), PDC_TRANSFER, pdc_tx_init(), tc_start(), tc_stop(), pdc_packet::ul_addr, pdc_packet::ul_size, usart_get_status(), usart_getchar(), and usart_write().
Pdc* g_p_pdc |
Pointer to PDC register base.
pdc_packet_t g_st_nextpacket |
pdc_packet_t g_st_packet |
PDC data packet.
|
static |
Flag of one transfer end.
Referenced by TC0_Handler(), and USART_Handler().
|
static |
Receive buffer.
Referenced by main(), and USART_Handler().
|
static |
Next Receive buffer.
Referenced by main(), and USART_Handler().
|
static |
Buffer number in use.
Referenced by main(), and USART_Handler().
|
static |
Current transfer mode.
Referenced by main(), TC0_Handler(), and USART_Handler().
|
static |
Byte mode read buffer.
Referenced by USART_Handler().
|
static |
Current bytes in buffer.
Referenced by main(), TC0_Handler(), and USART_Handler().
|
static |
Current bytes in next buffer.
Referenced by main(), TC0_Handler(), and USART_Handler().