#include "bsp_can.h"
#include <stdio.h>
#include "systick.h"

can_rx_message_type can1_receive_message_fifo0;
can_tx_message_type can1_transmit_message;
volatile Enum_can_mode can1_receive_fifo0_flag;

uint8_t bit12[8];
uint8_t bit13[8];
uint8_t bit14[8];
uint8_t bit15[8];
uint8_t bit16[8];
uint8_t bit17[8];
uint8_t bit18[8];
uint8_t bit19[8];
uint8_t bit20[8];

void TJA1042_STB_Config(void) {
    gpio_init_type gpio_init_struct;

    /* 启用 GPIOA 时钟 */
    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

    
    gpio_default_para_init(&gpio_init_struct);
    gpio_init_struct.gpio_pins = GPIO_PINS_12;       // 选择 PA0
    gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;   // 输出模式
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; // 推挽输出
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;     // 无上下拉
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE; // 驱动能力
    gpio_init(GPIOB, &gpio_init_struct);
		gpio_bits_reset(GPIOB, GPIO_PINS_12);
}

static void can1_gpio_config(void)
{
  gpio_init_type gpio_init_struct;
  /* enable the gpio clock */
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

  gpio_default_para_init(&gpio_init_struct);

  /* configure the can tx, rx pin */
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pins = GPIO_PINS_11 | GPIO_PINS_12;	
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOA, &gpio_init_struct);

  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE12, GPIO_MUX_9);
  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE11, GPIO_MUX_9);
	TJA1042_STB_Config();
	delay_ms(100);
}

/**
  *  @brief  can configiguration.
  *  @param  none
  *  @retval none
  */
static void can1_configuration(void)
{
  can_base_type can_base_struct;
  can_baudrate_type can_baudrate_struct;
  can_filter_init_type can_filter_init_struct;
  /* enable the can clock */
  crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);

  /* can base init */
  can_default_para_init(&can_base_struct);
  can_base_struct.mode_selection = CAN_MODE_COMMUNICATE;
  can_base_struct.ttc_enable = FALSE;
  can_base_struct.aebo_enable = FALSE;
  can_base_struct.aed_enable = FALSE;
  can_base_struct.prsf_enable = FALSE;
  can_base_struct.mdrsel_selection = CAN_DISCARDING_FIRST_RECEIVED;
  can_base_struct.mmssr_selection = CAN_SENDING_BY_REQUEST;
	
  can_base_init(CAN1, &can_base_struct);

  /* can baudrate, set boudrate = pclk/(baudrate_div *(1 + bts1_size + bts2_size)) */
  can_baudrate_struct.baudrate_div = 30;
  can_baudrate_struct.rsaw_size = CAN_RSAW_1TQ;
  can_baudrate_struct.bts1_size = CAN_BTS1_6TQ;
  can_baudrate_struct.bts2_size = CAN_BTS2_1TQ;
  can_baudrate_set(CAN1, &can_baudrate_struct);


  /*can_filter_0_config--------------------------------------------------------------*/
  can_filter_init_struct.filter_activate_enable = TRUE;
  can_filter_init_struct.filter_number = 0;
  can_filter_init_struct.filter_fifo = CAN_FILTER_FIFO0;
  can_filter_init_struct.filter_bit = CAN_FILTER_16BIT;  
  can_filter_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;  
  /*Standard identifier + Mask Mode + Data/Remote frame: id/mask 11bit --------------*/
  can_filter_init_struct.filter_id_high = 0x0000;
  can_filter_init_struct.filter_id_low = 0x0000;
  can_filter_init_struct.filter_mask_high = 0x0000;
  can_filter_init_struct.filter_mask_low = 0x0000;

  can_filter_init(CAN1, &can_filter_init_struct);

  /* can interrupt config */
  //nvic_irq_enable(CAN1_SE_IRQn, 0x00, 0x00);
  nvic_irq_enable(CAN1_RX0_IRQn, 2, 0);
  can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);

//  /* error interrupt enable */
//  can_interrupt_enable(CAN1, CAN_ETRIEN_INT, TRUE);
//  can_interrupt_enable(CAN1, CAN_EOIEN_INT, TRUE);
}

void can1_init(void)
{
		can1_gpio_config();
		can1_configuration();
		delay_ms(200);
}



void send_can1_data(uint32_t id, uint8_t *msg, uint8_t msg_len)
{
//		over_time = 600;
		uint16_t over_time = 600;
		if(msg_len > 8)
		{
			msg_len = 8;
		}
		
		can1_transmit_message.standard_id = id;
		can1_transmit_message.extended_id = 0;
		can1_transmit_message.id_type = CAN_ID_STANDARD;
		can1_transmit_message.frame_type = CAN_TFT_DATA;
		can1_transmit_message.dlc = msg_len;
		
		for(uint8_t i = 0; i < msg_len; i++)
			can1_transmit_message.data[i] = msg[i];
    
		
    /* transmit message */
    while((can_message_transmit(CAN1, &can1_transmit_message) == CAN_TX_STATUS_NO_EMPTY) && (over_time))
		{
				over_time--;
		}
}


void send_can1_buffer(uint32_t id, uint8_t *buffer, uint16_t buffer_len)
{
		uint8_t temp1 = buffer_len/8;
		uint8_t temp2 = buffer_len%8;

		
		if(temp1 == 0)
		{
			send_can1_data(id, buffer, buffer_len);
		}
		else
		{
			for(uint8_t i = 0; i < temp1; i++)
			{
				send_can1_data(id, buffer + 8*i, 8);
			}
			if(temp2 != 0)
			{
				send_can1_data(id, buffer+(buffer_len - temp2), temp2);
			}
		}
}

void CAN1_RX0_IRQHandler(void)
{
    if (can_flag_get(CAN1, CAN_RF0MN_FLAG) == SET)
    {
        can_flag_clear(CAN1, CAN_RF0MN_FLAG);
        can_message_receive(CAN1, CAN_RX_FIFO0, &can1_receive_message_fifo0);
        
        // 直接使用 can1_receive_message_fifo0
//        switch(can1_receive_message_fifo0.standard_id)
//        {
//						case 0x014:
//                // 处理ID 0x020
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit20);
//                break;
//            case 0x013:
//                // 处理ID 0x002
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit19);
//                break;
//            case 0x012:
//                // 处理ID 0x003
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit18);
//                break;
//						case 0x11:
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit17);
//								break;
//						case 0x10:
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit16);
//								break;
//						case 0x00F:
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit15);
//								break;
//						case 0x00E:
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit14);
//								break;
//						case 0x00D:
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit13);
//								break;
//						case 0x00C:
//								can1_transmit_data_pressure(can1_receive_message_fifo0.standard_id,bit12);
//								break;
//        }
    }
}
void can_transmit_all_id(){
	

	can1_transmit_data_pressure(0x014,bit20);
	

	can1_transmit_data_pressure(0x013,bit19);
	

	can1_transmit_data_pressure(0x012,bit18);
	

	can1_transmit_data_pressure(0x011,bit17);
	

	can1_transmit_data_pressure(0x010,bit16);
	

	can1_transmit_data_pressure(0x00F,bit15);
	

	can1_transmit_data_pressure(0x00E,bit14);
	

	can1_transmit_data_pressure(0x00D,bit13);
	

	can1_transmit_data_pressure(0x00C,bit12);
	

	
								
       
}
/* can transmit data */

void can1_transmit_data_pressure(uint32_t id,uint8_t pressure[8])
{
	uint16_t over_time = 600;
	uint8_t transmit_status;
	can_tx_message_type tx_message_struct;
	tx_message_struct.standard_id = id;
	tx_message_struct.extended_id = 0;
	tx_message_struct.id_type = CAN_ID_STANDARD;
	tx_message_struct.frame_type = CAN_TFT_DATA;
	tx_message_struct.dlc = 8;
	// 复制数据
    for(int i = 0; i < 8; i++) {
        tx_message_struct.data[i] = pressure[i];
    }
    
    // 尝试发送消息
    transmit_status = can_message_transmit(CAN1, &tx_message_struct);
    
    // 等待发送完成或超时
    while((transmit_status == CAN_TX_STATUS_NO_EMPTY) && (over_time > 0))
    {
        // 可能需要添加小延迟，避免过于频繁的检查
        delay_us(10); // 根据你的系统调整
        
        // 重新检查发送状态
       transmit_status = can_message_transmit(CAN1, &tx_message_struct);
       over_time--;
    }

}
void copyToBytes(uint8_t externalArray[12][12]) {
	for(int i=0;i<8;i++){
			bit20[i]=externalArray[7][i];
		}
		for(int i=0;i<7;i++){
			bit12[i]=externalArray[6-i][0];
		}
		for(int i=0;i<7;i++){
			bit13[i]=externalArray[6-i][7];
		}
		for(int i=0;i<4;i++){
			bit14[i]=externalArray[6][2+i];
			bit14[4 + i]=externalArray[5][2+i];
		}
		for(int i=0;i<2;i++){
			bit15[i] = externalArray[6-i][1];
			bit15[2+i] = externalArray[6-i][6];
		}
		for(int i=0;i<4;i++){
			bit15[4+i] = externalArray[4][2+i];
		}
		bit16[0] = externalArray[4][1];
		bit16[1] = externalArray[4][6];
		for(int i=0;i<6;i++){
			bit16[2+i] = externalArray[3][i+1];
		}
		for(int i =0;i<6;i++){
			bit17[i] = externalArray[2][i+1];
			bit18[i] = externalArray[1][i+1];
			bit19[i] = externalArray[0][i+1];
		}
}

