/**
 * \file
 *
 * \brief Main functions
 *
 * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */

#ifndef _MAIN_H_
#define _MAIN_H_

#include <math.h>
#include <stdio.h>
#include "usb_protocol_cdc.h"
#include "globales.h"
#include "pio.h"
#include "ioport.h"
#include "sam4e.h"
#include <string.h>
#include "w25qflash.h"
//#include "lcd.h"
//#include "gui.h"
//#define USB_RX_BUFFER_SIZE     64
//#define USB_TX_BUFFER_SIZE     64

#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)];


uint16_t adc_value;
int16_t motorTemp;
float temperatura;
ENCODER Encoder;	
FLAGS Flags;
volatile bool usb_datos_listos;
uint8_t usb_rx_buffer[USB_RX_BUFFER_SIZE];
uint8_t usb_tx_buffer[USB_TX_BUFFER_SIZE];
uint8_t usb_rx_index;

volatile INPUTS_STATE InputsState;
OUTPUTS_STATE OutsStateSet;
volatile OUTPUTS_STATE OutsStateRead;
MOTOR MotorRead;
COOLERS ReactorRead;
COOLERS IntercambiadorRead;
COOLERS SecadorRead;
UL2B ul2byte;
UL2B ul2byteH;

uint32_t page;
uint8_t subpage;
uint16_t x,y,ancho,alto;
uint32_t address;

/*! \brief Opens the communication port
 * This is called by CDC interface when USB Host enable it.
 *
 * \retval true if cdc startup is successfully done
 */
bool my_callback_cdc_enable(void);

/*! \brief Closes the communication port
 * This is called by CDC interface when USB Host disable it.
 */
void my_callback_cdc_disable(void);


void usb_data_received(uint8_t port);

void SetupPeripherals(void);
void EncoderPulse(void);
void pioHandler(uint32_t id, uint32_t index);
/**
 * \brief Set input mode for one single IOPORT pin.
 * It will configure port mode and disable pin mode (but enable peripheral).
 * \param pin IOPORT pin to configure
 * \param mode Mode masks to configure for the specified pin (\ref ioport_modes)
 * \param sense Sense for interrupt detection (\ref ioport_sense)
 */
#define ioport_set_pin_input_mode(pin, mode, sense) \
	do {\
		ioport_set_pin_dir(pin, IOPORT_DIR_INPUT);\
		ioport_set_pin_mode(pin, mode);\
		ioport_set_pin_sense_mode(pin, sense);\
	} while (0)

/**
 * \brief Set peripheral mode for one single IOPORT pin.
 * It will configure port mode and disable pin mode (but enable peripheral).
 * \param pin IOPORT pin to configure
 * \param mode Mode masks to configure for the specified pin (\ref ioport_modes)
 */
#define ioport_set_pin_peripheral_mode(pin, mode) \
	do {\
		ioport_set_pin_mode(pin, mode);\
		ioport_disable_pin(pin);\
	} while (0)	
	
	

void save_params_to_flash(void);
void read_params_from_flash_buffer(void);
void Set_defaults(void);
void Usb_cmd_decode(void);
void Usb_tx(void);
void float2Bytes(float val,uint8_t * bytes_array);
void Motor_temperature(void);
//#define RAMFUNC __attribute__ ((long_call, section (".ramfunc")))
RAMFUNC void SAMBA_reboot(void);	
extern void Pwm_init(void);	
extern void pwm_update(uint16_t period, uint16_t duty);
extern void tc0_0_init(void);
//extern void tc0_1_init(void);
//extern void tc1_0_init(void);
//extern void tc1_1_init(void);
extern void twi0_init(void);
extern void SysTick_init(void);
extern void Wdt_init (uint32_t us);
extern void Adc_init(void); 
extern void dac0_init(void);
extern int16_t read_adc_channel(uint8_t ch);
extern int16_t Adc1_read_adc_channel2(void);
#endif // _MAIN_H_
