USART RS485 example for SAM.
Copyright (c) 2015-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 | ACK_CHAR 0x13 |
Character to acknowledge receipt of the sync char. More... | |
#define | ALL_INTERRUPT_MASK 0xffffffff |
All interrupt mask. More... | |
#define | BUFFER_SIZE 2000 |
Size of the receive buffer and transmit buffer. More... | |
#define | PDC_BUF_SIZE BUFFER_SIZE |
Size of the buffer. 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 void | dump_info (char *p_buf, uint32_t ul_size) |
Dump buffer to uart. More... | |
static uint8_t | func_transmit (const uint8_t *p_buff, uint32_t ulsize) |
transmit data. More... | |
static uint32_t | get_tick_count (void) |
Get the tick count value. More... | |
int | main (void) |
usart_rs485 Application entry point. More... | |
void | SysTick_Handler (void) |
Handler for System Tick interrupt. More... | |
void | USART_Handler (void) |
Handler for USART 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... | |
uint32_t | g_ulcount = 0 |
count number for received data. More... | |
uint8_t * | p_revdata = &g_uc_receive_buffer[0] |
Pointer to receive buffer base. More... | |
#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 2000 |
Size of the receive buffer and transmit buffer.
Referenced by main(), and USART_Handler().
#define PDC_BUF_SIZE BUFFER_SIZE |
Size of the buffer.
#define STRING_EOL "\r" |
#define STRING_HEADER |
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().
typedef enum st_usart_state usart_state_t |
State of the USART.
enum st_usart_state |
|
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 system tick to generate an interrupt every 1ms.
References SYS_TICK_FREQ, and sysclk_get_cpu_hz().
Referenced by main().
|
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_hz(), usart_disable_interrupt(), usart_enable_rx(), usart_enable_tx(), usart_init_rs485(), and usart_set_tx_timeguard().
Referenced by main().
|
static |
Dump buffer to uart.
Referenced by main().
|
static |
transmit data.
*p_buff | data to be transmitted |
ulsize | size of all data. |
References Assert, usart_enable_interrupt(), usart_is_tx_empty(), and usart_write().
Referenced by main().
|
static |
int main | ( | void | ) |
usart_rs485 Application entry point.
Configure USART in RS485 mode. If the application starts earlier, it acts as a receiver. Otherwise, it should be a transmitter.
References ACK_CHAR, board_init(), BUFFER_SIZE, configure_console(), configure_systick(), configure_usart(), dump_info(), func_transmit(), g_state, g_uc_receive_buffer, g_uc_transmit_buffer, get_tick_count(), RECEIVED, RECEIVING, STRING_HEADER, SYNC_CHAR, sysclk_init(), TIMEOUT, TRANSMITTING, usart_disable_rx(), usart_enable_interrupt(), usart_enable_rx(), usart_enable_tx(), usart_is_rx_ready(), usart_read(), and wait().
void SysTick_Handler | ( | void | ) |
Handler for System Tick interrupt.
Process System Tick Event. Increment the ul_ms_ticks counter.
References g_ul_tick_count.
void USART_Handler | ( | void | ) |
Handler for USART interrupt.
References BUFFER_SIZE, g_state, g_ulcount, p_revdata, RECEIVED, RECEIVING, usart_disable_interrupt(), usart_get_status(), and usart_read().
|
static |
Wait for some time in ms.
References g_ul_tick_count.
volatile usart_state_t g_state = INITIALIZED |
Global usart state.
Referenced by main(), and USART_Handler().
uint8_t g_uc_receive_buffer[BUFFER_SIZE] |
Receive buffer.
Referenced by main().
uint8_t g_uc_transmit_buffer[BUFFER_SIZE] |
Transmit buffer.
Pure ASCII text.
Referenced by main().
volatile uint32_t g_ul_tick_count |
Tick Counter in unit of ms.
uint32_t g_ulcount = 0 |
count number for received data.
uint8_t* p_revdata = &g_uc_receive_buffer[0] |
Pointer to receive buffer base.