Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PLC Buffered PDC UART

This module provides configuration and utils for PLC Buffered UART service interface.

Data Structures

struct  buart_comm_data_t
 Communications Queue Info. More...
 

Macros

#define FREQ_TIMER_POLL_UART   100
 
#define SERIAL_UART_0   0
 
#define SERIAL_UART_1   1
 
#define TIMER_UART_PRIO   10
 
#define UART0_PRIO   10
 
#define UART1_PRIO   10
 
#define UART_BUFFER_SIZE   1024
 

Functions

static void _configure_TC_uart (void)
 Configure Timer Counter to generate an interrupt every 10ms. More...
 
int8_t buart_if_close (uint8_t chn)
 This function closes and disables communication in the specified UART. More...
 
int8_t buart_if_open (uint8_t chn, uint32_t bauds)
 This function opens an UART. More...
 
uint16_t buart_if_read (uint8_t chn, void *buffer, uint16_t len)
 This function receives a message. More...
 
int buart_if_rx_char (uint8_t chn)
 Get byte from UART. More...
 
uint16_t buart_if_tx_char (uint8_t chn, char data)
 Sent byte to UART. More...
 
uint16_t buart_if_write (uint8_t chn, const void *buffer, uint16_t len)
 This function transmits a message. More...
 
void TC_UART_Handler (void)
 Interrupt handler. More...
 
void UART0_Handler (void)
 Interruption handler for UART0. More...
 

Variables

static uint8_t buart_chn_open [2]
 
static buart_comm_data_t buart_comm_data_0
 
Pdc * g_p_uart_pdc0
 
pdc_packet_t g_st_uart_rx_packet0
 
pdc_packet_t g_st_uart_tx_packet0
 
static uint8_t gs_puc_uart_buf0 [UART_BUFFER_SIZE]
 
static uint32_t gs_ul_size_uart_buf0 = UART_BUFFER_SIZE
 
static uint16_t num_bytes_rx_uart0
 
uint8_t *const ptr_rx_uart_buf0 = &rx_uart_buf0[0]
 
uint8_t *const ptr_tx_uart_buf0 = &tx_uart_buf0[0]
 
static uint8_t rx_uart_buf0 [RX_UART_BUF0_SIZE]
 
static uint8_t tx_uart_buf0 [TX_UART_BUF0_SIZE]
 
uint8_t *const uartRxBuf0
 
uint8_t *const uartTxBuf0
 

#define FREQ_TIMER_POLL_UART   100

Referenced by _configure_TC_uart().

#define SERIAL_UART_0   0
#define SERIAL_UART_1   1
#define TIMER_UART_PRIO   10

Referenced by _configure_TC_uart().

#define UART0_PRIO   10

Referenced by buart_if_open().

#define UART1_PRIO   10

Referenced by buart_if_open().

#define UART_BUFFER_SIZE   1024

static void _configure_TC_uart ( void  )
static

Configure Timer Counter to generate an interrupt every 10ms.

This interrupt will be used to flush UART input and echo back.

References FREQ_TIMER_POLL_UART, ID_TC_UART, pmc_enable_periph_clk(), sysclk_get_cpu_hz(), tc_enable_interrupt(), tc_find_mck_divisor(), tc_init(), TC_UART, TC_UART_CHN, tc_write_rc(), and TIMER_UART_PRIO.

Referenced by buart_if_open().

int8_t buart_if_close ( uint8_t  chn)

This function closes and disables communication in the specified UART.

Parameters
chnCommunication channel [0, 1]
Return values
trueon success.
falseon failure.

References tc_stop(), TC_UART, TC_UART_CHN, uart_disable(), and uart_disable_interrupt().

int8_t buart_if_open ( uint8_t  chn,
uint32_t  bauds 
)

This function opens an UART.

Note
Opening of the specified UART implies initializing local variables and opening required hardware with the following configuration:
  • bauds as specified
  • 8 bits, no parity, 1 stop bit
  • enable interrupts
Parameters
chnCommunication channel [0, 1]
baudsCommunication speed in bauds
Return values
trueon success.
falseon failure.

References _configure_TC_uart(), pdc_enable_transfer(), pdc_rx_init(), pdc_tx_init(), pmc_enable_periph_clk(), ptr_rx_uart_buf0, ptr_tx_uart_buf0, buart_comm_data_t::puc_rq_buf, buart_comm_data_t::puc_tq_buf, sysclk_get_cpu_hz(), tc_start(), TC_UART, TC_UART_CHN, UART0_PRIO, UART1_PRIO, UART_BUFFER_SIZE, uart_disable_interrupt(), uart_enable_interrupt(), uart_enable_rx(), uart_enable_tx(), uart_get_pdc_base(), uart_init(), pdc_packet::ul_addr, sam_uart_opt::ul_baudrate, sam_uart_opt::ul_mck, sam_uart_opt::ul_mode, pdc_packet::ul_size, UNUSED, buart_comm_data_t::us_rq_count, buart_comm_data_t::us_rq_idx, and buart_comm_data_t::us_wq_idx.

Referenced by usi_init().

uint16_t buart_if_read ( uint8_t  chn,
void *  buffer,
uint16_t  len 
)

This function receives a message.

Note
This function receives a given number of characters from the specified UART. If so configured, the function waits until all characters are received. In this case, the watchdog timer must be reloaded to avoid a program reset.
Parameters
chnCommunication channel [0, 1]
bufferPointer to buffer for information
lenNumber of characters to receive
Return values
Numberof received characters

References buart_comm_data_t::puc_rq_buf, RX_UART_BUF0_SIZE, RX_UART_BUF1_SIZE, UNUSED, buart_comm_data_t::us_rq_count, and buart_comm_data_t::us_rq_idx.

Referenced by buart_if_rx_char(), and usi_process().

int buart_if_rx_char ( uint8_t  chn)

Get byte from UART.

Parameters
chnCommunication channel [0, 1]
Return values
Bytereceived
-1in case of no reception

References buart_if_read().

uint16_t buart_if_tx_char ( uint8_t  chn,
char  data 
)

Sent byte to UART.

Parameters
chnCommunication channel [0, 1]
dataData to sent
Return values
Numberof characters sent

References buart_if_write().

uint16_t buart_if_write ( uint8_t  chn,
const void *  buffer,
uint16_t  len 
)

This function transmits a message.

Note
This function transmits characters via the specified UART. If so configured, the function waits until all characters are inserted in the transmission queue. In this case, the watchdog timer must be reloaded to avoid a program reset.
Parameters
chnCommunication channel [0, 1]
bufferPointer to information to transmit
lenNumber of characters to transmit
Return values
Numberof characters sent

References pdc_read_tx_counter(), pdc_tx_init(), buart_comm_data_t::puc_tq_buf, TX_UART_BUF0_SIZE, TX_UART_BUF1_SIZE, pdc_packet::ul_addr, pdc_packet::ul_size, and UNUSED.

Referenced by _usi_encode_and_send(), and buart_if_tx_char().

void TC_UART_Handler ( void  )

Interrupt handler.

Record the number of bytes received, and then restart a read transfer on the UART if the transfer was stopped.

References pdc_read_rx_counter(), pdc_rx_init(), tc_get_status(), tc_stop(), TC_UART, TC_UART_CHN, UART_BUFFER_SIZE, and pdc_packet::ul_size.

uint8_t buart_chn_open[2]
static
Initial value:
= {
false,
false
}
buart_comm_data_t buart_comm_data_0
static
Pdc* g_p_uart_pdc0
pdc_packet_t g_st_uart_rx_packet0
pdc_packet_t g_st_uart_tx_packet0
uint8_t gs_puc_uart_buf0[UART_BUFFER_SIZE]
static
uint32_t gs_ul_size_uart_buf0 = UART_BUFFER_SIZE
static

Referenced by UART0_Handler().

uint16_t num_bytes_rx_uart0
static
uint8_t* const ptr_rx_uart_buf0 = &rx_uart_buf0[0]

Referenced by buart_if_open().

uint8_t* const ptr_tx_uart_buf0 = &tx_uart_buf0[0]

Referenced by buart_if_open().

uint8_t rx_uart_buf0[RX_UART_BUF0_SIZE]
static
uint8_t tx_uart_buf0[TX_UART_BUF0_SIZE]
static
uint8_t* const uartRxBuf0
uint8_t* const uartTxBuf0