/* 
* classRs485.h
*
* Created: 10/02/2020 20:06:45
* Author: Ledo
*/

#ifndef __CLASSRS485_H__
#define __CLASSRS485_H__

#include "asf.h"
#include "classPio.h"

#define RS4_RXBUFFER_LENGHT       32
#define RS4_TXBUFFER_LENGHT       32

//RS485 Bus Control
#define RS4_DE_PIN              PIO_PA5_IDX
#define RS4_DE_PIN_MASK         (1<<5)

extern classPio Puerto;
class classRs485
{
//variables
public:
     volatile uint8_t rs4_rxbuffer[RS4_RXBUFFER_LENGHT];
     uint8_t rs4_txbuffer[RS4_TXBUFFER_LENGHT];
     volatile uint8_t rs4_rx_index;
     uint32_t * rs4_rx_pointer;
     volatile bool rs4_datos_listos;
	 const sam_usart_opt_t usart_console_settings = {
		 	38400,
		 	US_MR_CHRL_8_BIT,
		 	US_MR_PAR_NO,
		 	US_MR_NBSTOP_1_BIT,
		 	US_MR_CHMODE_NORMAL,
		 	/* This field is only used in IrDA mode. */
		 	0
	 };

protected:
private:

//functions
public:
	classRs485();
	~classRs485();
	void configure_usart0(void);
	void Rs4_transmit(void);
protected:
private:
	classRs485( const classRs485 &c );
	classRs485& operator=( const classRs485 &c );
	void Usart0_sendBytes(uint8_t bytes);
	void Insert_check_sum(uint8_t bytes);
	bool cksum(void);

}; //classRs485

#endif //__CLASSRS485_H__
