/*
 * canbus.c
 *
 * Created: 09/08/2018 10:13:05
 *  Author: Ledo
 */ 

#include "asf.h"
#include <can.h>

#define CAN_COMM_RXMB_ID    0
#define CAN_COMM_TXMB_ID    1//0//1537//1
#define CAN_TRANSFER_ID     1537//1537//7
#define CAN_TX_PRIO         15
#define CAN_MSG_DUMMY_DATA  0x2f002101ffu


/** CAN frame max data length */
#define MAX_CAN_FRAME_DATA_LEN      8

/** Transfer mailbox structure */
can_mb_conf_t tx_mailbox, rx_mailbox;

/** Receive status */
volatile uint32_t g_ul_recv_status = 0;

//Prototipos
void reset_mailbox_conf(can_mb_conf_t *p_mailbox);
void decode_can_msg(can_mb_conf_t *p_mailbox);

void Can_init(void)
{
	uint32_t ul_sysclk, cnt = 0;
	uint8_t uc_key;

	/* Enable CAN0 clock. */
	pmc_enable_periph_clk(ID_CAN0);

	ul_sysclk = sysclk_get_cpu_hz();
	can_init(CAN0, ul_sysclk, CAN_BPS_125K);

	/* Disable all CAN0 interrupts. */
	can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
	can_reset_all_mailbox(CAN0);
	/* Init CAN0 Mailbox 0 to Reception Mailbox. */
	reset_mailbox_conf(&rx_mailbox);
	rx_mailbox.ul_mb_idx = CAN_COMM_RXMB_ID;
	rx_mailbox.uc_obj_type = CAN_MB_RX_MODE;
	rx_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
	rx_mailbox.ul_id = CAN_MID_MIDvA(CAN_TRANSFER_ID);
	can_mailbox_init(CAN0, &rx_mailbox);

	/* Init CAN0 Mailbox 1 to Transmit Mailbox. */
	reset_mailbox_conf(&tx_mailbox);
	tx_mailbox.ul_mb_idx = CAN_COMM_TXMB_ID;
	tx_mailbox.uc_obj_type = CAN_MB_TX_MODE;
	tx_mailbox.uc_tx_prio = CAN_TX_PRIO;
	tx_mailbox.uc_id_ver = 0;
	tx_mailbox.ul_id_msk = 0;
	can_mailbox_init(CAN0, &tx_mailbox);

	/* Write transmit information into mailbox. */
	tx_mailbox.ul_id = CAN_MID_MIDvA(0);//(CAN_TRANSFER_ID);
	tx_mailbox.ul_datal = 0x0001u;
	tx_mailbox.uc_length = 2;

	/* Enable CAN0 mailbox interrupt. */
	can_enable_interrupt(CAN0, 1 << CAN_COMM_RXMB_ID);
	/* Configure and enable interrupt of CAN0. */
	NVIC_EnableIRQ(CAN0_IRQn);
	
	
}

/**
 * \brief Default interrupt handler for CAN 1.
 */
void CAN0_Handler(void)
{
	uint32_t ul_status;

	ul_status = can_mailbox_get_status(CAN0, 0);
	if (ul_status & CAN_MSR_MRDY) {
		rx_mailbox.ul_status = ul_status;
		can_mailbox_read(CAN0, &rx_mailbox);
		//puts("Got CAN message, press 'r' to display\r");
		g_ul_recv_status = 1;
	}
}

/**
 * \brief Decode CAN messages.
 *
 *  \param p_mailbox Pointer to CAN Mailbox structure.
 */
void decode_can_msg(can_mb_conf_t *p_mailbox)
{
	if (p_mailbox->ul_datal != CAN_MSG_DUMMY_DATA) 
	{
		;//puts("Wrong message header\r");
	} 
	else 
	{
		;//printf("%d\r\n", (int)p_mailbox->ul_datah);
	}
}

/**
 * \brief Reset mailbox configure structure.
 *
 *  \param p_mailbox Pointer to mailbox configure structure.
 */
void reset_mailbox_conf(can_mb_conf_t *p_mailbox)
{
	p_mailbox->ul_mb_idx = 0;
	p_mailbox->uc_obj_type = 0;
	p_mailbox->uc_id_ver = 0;
	p_mailbox->uc_length = 0;
	p_mailbox->uc_tx_prio = 0;
	p_mailbox->ul_status = 0;
	p_mailbox->ul_id_msk = 0;
	p_mailbox->ul_id = 0;
	p_mailbox->ul_fid = 0;
	p_mailbox->ul_datal = 0;
	p_mailbox->ul_datah = 0;
}

