/***********************************************************************************************************
 * Copyright (c)  2021 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app_can_rx.c
 * Description : app can rx source file
 * Author(s)   : yanhaihua
 * version     : V1.0
 * Modify date : 2021-09-24
 ***********************************************************************************************************/
#include "app_can_rx.h"
#include "can.h"
#include "app_can_protocol.h"
#include "gpio.h"

#define  CAN_RX_INT_MODE

uint8_t can_irq_flag = 0;
/***********************************************************************************************************
 * Function     : can_irq_pro
 * Description  : can irq pro
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void can_irq_pro(void)
{
	can_irq_flag = 1;
}

/***********************************************************************************************************
 * Function     : user_app_pro
 * Description  : user app pro , 添加用户代码
 * Input		: can_rx_msg
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void user_app_pro(s_can_rx_msg can_rx_msg)
{
	/* 以下代码为打印测试，用户可添加应用代码 */
	if(can_rx_msg.rtr)																						/* 如果是遥控帧 */
	{
		print2Debug("received remote frame!! \r\n");
		if(can_rx_msg.ide)																					/* 扩展帧 */
		{
			print2Debug("ext_id = 0x%x\r\n", can_rx_msg.ext_id);
		}
		else																								/* 标准帧 */
		{
			print2Debug("std_id = 0x%x\r\n", can_rx_msg.std_id);
		}
		print2Debug("ide = 0x%x\r\n", can_rx_msg.ide);
		print2Debug("rtr = 0x%x\r\n", can_rx_msg.rtr);
	}
	else																									/* 如果是数据帧 */
	{
		print2Debug("received data frame!! \r\n");
		
		if(can_rx_msg.ide)																					/* 扩展帧 */
		{
			print2Debug("ext_id = 0x%x\r\n", can_rx_msg.ext_id);
		}
		else																								/* 标准帧 */
		{
			print2Debug("std_id = 0x%x\r\n", can_rx_msg.std_id);
		}
		print2Debug("ide = 0x%x\r\n", can_rx_msg.ide);
		print2Debug("rtr = 0x%x\r\n", can_rx_msg.rtr);
		print2Debug("len = 0x%x\r\n", can_rx_msg.len);
		print2Debug("recv data : ");
		for(uint8_t i=0;i<can_rx_msg.len;i++)
		{
			print2Debug("%02x   ", can_rx_msg.data[i]);
		}
		print2Debug("\r\n\r\n");
	}
}

/***********************************************************************************************************
 * Function		: app_can_rx_test
 * Description	: app_can_rx_test
 * Input		: none
 * Output		: none
 * Return		: none
 ***********************************************************************************************************/
void app_can_rx_test(void)
{
	print2Debug("start test CAN rx function!! \n");
	
	gpio_set_sel(PB3,0x04);																					/* PB3复用为CAN_RX */
	gpio_set_sel(PB4,0x06);																					/* PB4复用为CAN_TX */
	gpio_set_ie(PB3,ENABLE);																				/* 使能TX,RX管脚 */
	gpio_set_ie(PB4,ENABLE);																				/* 使能TX,RX管脚 */
	can_init(CAN_RATE_125K);												
	
#ifdef 	CAN_RX_INT_MODE
	can_irq_init(ENABLE,can_irq_pro);
#endif
	
	
	s_can_filter_msg can_filter_msg;
	can_filter_msg.std_id1 = 0x592;																			/* 过滤器ID1 */
	can_filter_msg.ext_id1 = 0x11E8E477;																	/* 扩展过滤器ID1 */
	can_filter_msg.rtr = 0;
	can_filter_msg.std_id2 = 0;
	
	uint32_t can_filter_value = app_can_filter_value(can_filter_msg,CAN_DOUBLE_FILTER, CAN0_STD_FORMAT);	/* 获取过滤器寄存器值 */	
	can_filter_config(ENABLE,CAN_DOUBLE_FILTER,CAN0_STD_FORMAT, can_filter_value);							/* 配置过滤寄存器 */
	
	while(1)
	{
#ifdef 	CAN_RX_INT_MODE
		if(can_irq_flag)																				/* 接收完成中断 */
		{
			s_can_rx_msg can_rx_msg;
			uint32_t can_rx_data[4];
			can_read_data(can_rx_data, &can_rx_msg.ide, &can_rx_msg.rtr, &can_rx_msg.len); 
			app_can_data_deocde_data(&can_rx_msg,can_rx_data);
			user_app_pro(can_rx_msg);
			can_irq_flag = 0;	
		}
#else
		if(can_get_sr_reg()&0x80)																		/* fifo 至少一条消息 */
		{
			while(can_get_rmc_reg())																	/* 处理完fifo中所有帧 */
			{
				s_can_rx_msg can_rx_msg;
				uint32_t can_rx_data[4];
				can_read_data(can_rx_data, &can_rx_msg.ide, &can_rx_msg.rtr, &can_rx_msg.len); 
				app_can_data_deocde_data(&can_rx_msg,can_rx_data);
				user_app_pro(can_rx_msg);
			}
		}
#endif
	}
}
