/*
 * rs485.h
 *
 * Created: 24/09/2016 19:15:12
 *  Author: Ledo
 */ 


#ifndef RS485_H_
#define RS485_H_

#include <asf.h>
#include <avr32/io.h>
#include <stdbool.h>
#include "gpio.h"
#include "sysclk.h"
#include "delay.h"
#include "globales.h"

#  define USART                 (&AVR32_USART0)
#  define USART_RX_PIN          AVR32_USART0_RXD_0_1_PIN
#  define USART_RX_FUNCTION     AVR32_USART0_RXD_0_1_FUNCTION
#  define USART_TX_PIN          AVR32_USART0_TXD_0_1_PIN
#  define USART_TX_FUNCTION     AVR32_USART0_TXD_0_1_FUNCTION
#  define USART_CLOCK_MASK      AVR32_USART0_CLK_PBA
#  define PDCA_CLOCK_HSB        AVR32_PDCA_CLK_HSB
#  define PDCA_CLOCK_PB         AVR32_PDCA_CLK_PBC

#define RS485_TX_BUFFLENGH      36
#define RS485_RX_BUFFLENGH      32

uint8_t rs4_rx_buffer[RS485_RX_BUFFLENGH];
uint8_t rs4_tx_buffer[RS485_TX_BUFFLENGH];
uint8_t rx_index;
static const gpio_map_t USART_GPIO_MAP =
{
	{USART_RX_PIN, USART_RX_FUNCTION},
	{USART_TX_PIN, USART_TX_FUNCTION}
};

// USART options.
static const usart_options_t USART_OPTIONS =
{
	.baudrate     = 38400,
	.charlength   = 8,
	.paritytype   = USART_NO_PARITY,
	.stopbits     = USART_1_STOPBIT,
	.channelmode  = USART_NORMAL_CHMODE
};


__attribute__((__interrupt__))
static void usart_irq(void);
void rs485_init(void);
void send_to_lcd(char cmd);

#endif /* RS485_H_ */