Microchip® Advanced Software Framework

usart_rs485_example_pdca.c File Reference

USART RS485 example with PDCA for SAM.

Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.

#include <string.h>
#include <asf.h>
#include "conf_board.h"
#include "conf_clock.h"
#include "conf_example.h"

Macros

#define ACK_CHAR   0x13
 Character to acknowledge receipt of the sync char. More...
 
#define ALL_INTERRUPT_MASK   0xffffffff
 All interrupt mask. More...
 
#define BUFFER_SIZE   1280
 Size of the receive buffer and transmit buffer. More...
 
#define PDCA_PID_USART0_RX   0
 PDCA channel peripheral setting. More...
 
#define PDCA_PID_USART0_TX   18
 
#define PDCA_RX_CHANNEL   0
 PDCA receive channel setting. More...
 
#define PDCA_TX_CHANNEL   1
 PDCA transfer channel setting. More...
 
#define STRING_EOL   "\r"
 
#define STRING_HEADER
 
#define SYNC_CHAR   0x11
 Character to synchronize with the other end. More...
 
#define SYS_TICK_FREQ   1000
 System tick frequency in Hz. More...
 
#define TIMEOUT   (1000)
 Acknowledge time out. More...
 

Typedefs

typedef enum st_usart_state usart_state_t
 State of the USART. More...
 

Enumerations

enum  st_usart_state {
  INITIALIZED,
  TRANSMITTING,
  RECEIVING,
  RECEIVED,
  TRANSMITTED
}
 State of the USART. More...
 

Functions

static void configure_console (void)
 Configure UART for debug message output. More...
 
static void configure_systick (void)
 Configure system tick to generate an interrupt every 1ms. More...
 
static void configure_usart (void)
 USART RS485 mode configuration. More...
 
static uint32_t get_tick_count (void)
 Get the tick count value. More...
 
int main (void)
 usart_rs485 Application entry point. More...
 
static void PDCA_RX_Handler (enum pdca_channel_status status)
 Interrupt handler for PDCA interrupt. More...
 
static void PDCA_TX_Handler (enum pdca_channel_status status)
 Interrupt handler for PDCA interrupt. More...
 
void SysTick_Handler (void)
 Handler for System Tick interrupt. More...
 
static void wait (volatile uint32_t ul_ms)
 Wait for some time in ms. More...
 

Variables

volatile usart_state_t g_state = INITIALIZED
 Global usart state. More...
 
uint8_t g_uc_receive_buffer [BUFFER_SIZE]
 Receive buffer. More...
 
uint8_t g_uc_transmit_buffer [BUFFER_SIZE]
 Transmit buffer. More...
 
volatile uint32_t g_ul_tick_count
 Tick Counter in unit of ms. More...
 
pdca_channel_config_t PDCA_RX_OPTIONS
 PDC channel options. More...
 
pdca_channel_config_t PDCA_TX_OPTIONS
 

#define ACK_CHAR   0x13

Character to acknowledge receipt of the sync char.

Referenced by main().

#define ALL_INTERRUPT_MASK   0xffffffff

All interrupt mask.

Referenced by configure_usart().

#define BUFFER_SIZE   1280

Size of the receive buffer and transmit buffer.

Referenced by main().

#define PDCA_PID_USART0_RX   0

PDCA channel peripheral setting.

#define PDCA_PID_USART0_TX   18
#define PDCA_RX_CHANNEL   0

PDCA receive channel setting.

Referenced by main(), and PDCA_RX_Handler().

#define PDCA_TX_CHANNEL   1

PDCA transfer channel setting.

Referenced by main(), and PDCA_TX_Handler().

#define STRING_EOL   "\r"
#define STRING_HEADER
Value:
"-- USART RS485 Example --\r\n" \
"-- "BOARD_NAME" --\r\n" \
"-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
#define BOARD_NAME
Definition: inc/app_init.h:140
#define STRING_EOL
Definition: usart_rs485_example_pdca.c:150

Referenced by main().

#define SYNC_CHAR   0x11

Character to synchronize with the other end.

Referenced by main().

#define SYS_TICK_FREQ   1000

System tick frequency in Hz.

Referenced by configure_systick().

#define TIMEOUT   (1000)

Acknowledge time out.

Referenced by main().

State of the USART.

State of the USART.

Enumerator
INITIALIZED 
TRANSMITTING 
RECEIVING 
RECEIVED 
TRANSMITTED 

static void configure_console ( void  )
static

Configure UART for debug message output.

References uart_rs232_options::baudrate, and stdio_serial_init().

Referenced by main().

static void configure_systick ( void  )
static

Configure system tick to generate an interrupt every 1ms.

References SYS_TICK_FREQ, and sysclk_get_cpu_hz().

Referenced by main().

static void configure_usart ( void  )
static

USART RS485 mode configuration.

Configure USART in RS485 mode, asynchronous, 8 bits, 1 stop bit, no parity, 256000 bauds and enable its transmitter and receiver.

References ALL_INTERRUPT_MASK, sysclk_enable_peripheral_clock(), sysclk_get_peripheral_bus_hz(), usart_disable_interrupt(), usart_enable_rx(), usart_enable_tx(), and usart_init_rs485().

Referenced by main().

static uint32_t get_tick_count ( void  )
static

Get the tick count value.

References g_ul_tick_count.

Referenced by main().

static void PDCA_RX_Handler ( enum pdca_channel_status  status)
static

Interrupt handler for PDCA interrupt.

References g_state, PDCA_CH_TRANSFER_COMPLETED, pdca_channel_disable_interrupt(), PDCA_RX_CHANNEL, and RECEIVED.

Referenced by main().

static void PDCA_TX_Handler ( enum pdca_channel_status  status)
static

Interrupt handler for PDCA interrupt.

References g_state, PDCA_CH_TRANSFER_COMPLETED, pdca_channel_disable_interrupt(), PDCA_TX_CHANNEL, and TRANSMITTED.

Referenced by main().

void SysTick_Handler ( void  )

Handler for System Tick interrupt.

Process System Tick Event. Increment the ul_ms_ticks counter.

References g_ul_tick_count.

static void wait ( volatile uint32_t  ul_ms)
static

Wait for some time in ms.

References g_ul_tick_count.

Referenced by main(), run_osc32_test(), run_osc_test(), and run_pll_dfll_test().

volatile usart_state_t g_state = INITIALIZED

Global usart state.

Referenced by main(), PDCA_RX_Handler(), and PDCA_TX_Handler().

uint8_t g_uc_receive_buffer[BUFFER_SIZE]

Receive buffer.

Referenced by main().

uint8_t g_uc_transmit_buffer[BUFFER_SIZE]
Initial value:
= "DESCRIPTION of this example: \r\n \
**************************************************************************\r\n\
* This application gives an example of how to use USART in RS485 mode.\r\n\
* RS-485 is a standard defining the electrical characteristics of drivers \r\n\
* and receivers for use in balanced digital multipoint systems. The standard \r\n\
* is published by the ANSI TIA/EIA. \r\n\
* \r\n\
* This example connects two boards through RS485 interface. One board acts \r\n\
* as the transmitter and the other one as the receiver. It is determined by \r\n\
* the sequence the two applications started. The earlier started board will \r\n\
* act automatically as the receiver due to no acknowledgement received. The \r\n\
* receiver will wait until sync character is received. Then it sends the \r\n\
* acknowledgement and waits for the full frame sent by the transmitter. \r\n\
* At the end of reception, it prints out message through UART interface to \r\n\
* assert that the whole process succeeds.\r\n\
**************************************************************************\r\n\
END of DESCRIPTION \r\n\
"

Transmit buffer.

Pure ASCII text.

Referenced by main().

volatile uint32_t g_ul_tick_count

Tick Counter in unit of ms.

Referenced by get_tick_count(), SysTick_Handler(), and wait().

pdca_channel_config_t PDCA_RX_OPTIONS
Initial value:
= {
.addr = (void *)g_uc_receive_buffer,
.size = BUFFER_SIZE,
.r_addr = NULL,
.r_size = 0,
.transfer_size = PDCA_MR_SIZE_BYTE
}
#define PDCA_PID_USART0_RX
PDCA channel peripheral setting.
Definition: usart_rs485_example_pdca.c:132
#define NULL
Definition: src/include/lwip/def.h:47
#define BUFFER_SIZE
Size of the receive buffer and transmit buffer.
Definition: usart_rs485_example_pdca.c:125
uint8_t g_uc_receive_buffer[BUFFER_SIZE]
Receive buffer.
Definition: usart_rs485_example_pdca.c:191

PDC channel options.

pdca_channel_config_t PDCA_TX_OPTIONS
Initial value:
= {
.addr = (void *)g_uc_transmit_buffer,
.size = BUFFER_SIZE,
.r_addr = NULL,
.r_size = 0,
.transfer_size = PDCA_MR_SIZE_BYTE
}
#define NULL
Definition: src/include/lwip/def.h:47
#define PDCA_PID_USART0_TX
Definition: usart_rs485_example_pdca.c:133
#define BUFFER_SIZE
Size of the receive buffer and transmit buffer.
Definition: usart_rs485_example_pdca.c:125
uint8_t g_uc_transmit_buffer[BUFFER_SIZE]
Transmit buffer.
Definition: usart_rs485_example_pdca.c:171