/*
 * main.h
 * SAM4E ASF Cpp Project with all the drivers to supports SAMTFT4.0_Touch Board
 * from www.ledoelectronics.com
 * Device USB CDC Stack (ext library)
 * CAN Bus
 * TWI, SPI, Usart
 * NT35510 TFT Cpp library
 * Resistive Touch Pad (AD7843)
 * Etcs.
 * Created: 08/02/2020 10:22:11
 *  Author: Ledo
 */ 


#ifndef MAIN_H_
#define MAIN_H_
#include "sam.h"
#include "tipos.h"
#include "classTC.h"
#include "classPio.h"
#include "classCan.h"
#include "classSpi.h"
#include "classTwi.h"
#include "classTFT.h"
#include "classGraphics.h"
#include "classW25Qflash.h"
#include "classM41T00.h"
#include "classCan.h"
#include "classCanApplication.h"
#include "classAD7843.h"
#include "classKeyboard.h"
#include "classRs485.h"

#define DATA_PAGE_ADDRESS (IFLASH_ADDR + IFLASH_SIZE - IFLASH_PAGE_SIZE)
/* Flash page buffer for alignment */
static uint32_t ul_page_buffer[IFLASH_PAGE_SIZE / sizeof(uint32_t)];

classTC Timers;
classPio Puerto;
classCan Can0;
classSpi Spi0;
classTwi I2C;
classTFT Display;
classGraphics Gui;
classW25Qflash W25q;
classM41T00 M41t00;
classCan myCan;
classCanApplication myCanAppl;
classAD7843 Touch;
classKeyboard Keys;
classRs485 RS4;

FLAGS Flags;
PARAMETROS params;
uint32_t page;
uint8_t subpage;
uint16_t x,y,ancho,alto;
uint32_t address;
volatile uint8_t CAN_counter;
volatile uint8_t sysLED_counter;

void Board_init(void);
void usb_msg_decode(void);
void usb_tx_to_pc();
void dateTime_show(void);
void read_params_from_flash_buffer(void);
void Set_defaults(void);
void save_params_to_flash(void);

RAMFUNC void SAMBA_reboot (void);

/*****************************************************************************
* USB Library Functions
* Usb_init(). Configure CPU Clock and Usb CDC Stack
* Usb_stop(). Disables USB Stack
* * Usb_rx_data_pointer(void). Return pointer to receive byte array
* Usb_data_received(). Return true when data arraivals
* Usb_tx_buffer_fill(i,byte). tx array filling
* chksum_insert();Add checksum to tx data array
* Usb_send_report(). Usb send data to PC
******************************************************************************/
extern "C" void Usb_init(void);
extern "C" void Usb_stop(void);
extern "C" unsigned char * Usb_rx_data_pointer(void);//return rx_data pointer
extern "C" bool Usb_data_received(void);//True when usb data arraival
extern "C" void Usb_tx_buffer_fill(unsigned char index,unsigned char byte);
extern "C" void chksum_insert(void);
extern "C" void Usb_send_report(void);

#endif /* MAIN_H_ */