示例#1
0
文件: Can_Hw.c 项目: SushMJ/gainos-tk
EXPORT Std_ReturnType Can_Hw_InitController(uint8 controller,const Can_ControllerConfigType* config)
{
	  CAN_HW_t *canHw;
	  uint8_t tq;
	  uint8_t tqSync;
	  uint8_t tq1;
	  uint8_t tq2;
	  uint32_t clock;
	  Can_UnitType *canUnit;
	  uint8 cId = controller;
	  const Can_ControllerConfigType *canHwConfig;
	  const Can_HardwareObjectType *hohObj;


	  canUnit = CAN_GET_PRIVATE_DATA(controller);

	  canHw = GetController(cId);
	  canHwConfig = CAN_GET_CONTROLLER_CONFIG(Can_Global.channelMap[cId]);

	  // Start this baby up
	  CAN_DeInit(canHw);

	  /* CAN filter init. We set up two filters - one for the master (CAN1) and
	   * one for the slave (CAN2)
	   *
	   * CAN_SlaveStartBank(n) denotes which filter is the first of the slave.
	   *
	   * The filter registers reside in CAN1 and is shared to CAN2, so we only need
	   * to set up this once.
	   */

	  // We let all frames in and do the filtering in software.
	  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
	  CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
	  CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
	  CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
	  CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	  CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
	  CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	  CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
	  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;

	  // Init filter 0 (CAN1/master)
	  CAN_FilterInitStructure.CAN_FilterNumber=0;
	  CAN_FilterInit(&CAN_FilterInitStructure);

	  // Init filter 1 (CAN2/slave)
	  CAN_FilterInitStructure.CAN_FilterNumber=1;
	  CAN_FilterInit(&CAN_FilterInitStructure);

	  // Set which filter to use for CAN2.
	  CAN_SlaveStartBank(1);

	  // acceptance filters
	   hohObj = canHwConfig->Can_Hoh;
	   --hohObj;
	   do {
		 ++hohObj;
	     if (hohObj->CanObjectType == CAN_OBJECT_TYPE_RECEIVE)
	     {
	    	 // TODO Hw filtering
	     }
	   }while( !hohObj->Can_EOL );

	  // Clock calucation
	  // -------------------------------------------------------------------
	  //
	  // * 1 TQ = Sclk period( also called SCK )
	  // * Ftq = Fcanclk / ( PRESDIV + 1 ) = Sclk
	  //   ( Fcanclk can come from crystal or from the peripheral dividers )
	  //
	  // -->
	  // TQ = 1/Ftq = (PRESDIV+1)/Fcanclk --> PRESDIV = (TQ * Fcanclk - 1 )
	  // TQ is between 8 and 25
	  clock = McuE_GetSystemClock()/2;

	  tqSync = config->CanControllerPropSeg + 1;
	  tq1 = config->CanControllerSeg1 + 1;
	  tq2 = config->CanControllerSeg2 + 1;
	  tq = tqSync + tq1 + tq2;

	  CAN_InitTypeDef        CAN_InitStructure;
	  CAN_StructInit(&CAN_InitStructure);

	  /* CAN cell init */
	  CAN_InitStructure.CAN_TTCM=DISABLE;
	  CAN_InitStructure.CAN_ABOM=ENABLE;
	  CAN_InitStructure.CAN_AWUM=ENABLE;
	  CAN_InitStructure.CAN_NART=DISABLE;
	  CAN_InitStructure.CAN_RFLM=DISABLE;
	  CAN_InitStructure.CAN_TXFP=DISABLE;
	  if(config->Can_Loopback){
		  CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
	  }else{
		  CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
	  }

	  CAN_InitStructure.CAN_SJW=config->CanControllerPropSeg;
	  CAN_InitStructure.CAN_BS1=config->CanControllerSeg1;
	  CAN_InitStructure.CAN_BS2=config->CanControllerSeg2;
	  CAN_InitStructure.CAN_Prescaler= clock/(config->CanControllerBaudRate*1000*tq);

	  if(CANINITOK != CAN_Init(canHw,&CAN_InitStructure))
	  {
		return E_NOT_OK;
	  }

	  canUnit->state = CANIF_CS_STOPPED;
	  Can_EnableControllerInterrupts(cId);

	  return E_OK;
}
示例#2
0
void BWM_5S_CanBus_Config(void)
{
    CAN_InitTypeDef CAN_InitStructure;
    CAN_FilterInitTypeDef CAN_FilterInitStructure;
    /* CAN register init */
    // CAN_DeInit();
    /* CAN1 and CAN2 register init */
    CAN_DeInit(CAN1);
    CAN_DeInit(CAN2);


    /* Struct init*/
    CAN_StructInit(&CAN_InitStructure);
	CAN_FilterInit(&CAN_FilterInitStructure); //初始化CAN_FilterInitStructrue结构体变量

    /* CAN cell init */
    CAN_InitStructure.CAN_TTCM = DISABLE; //禁止时间触发通信模式
    CAN_InitStructure.CAN_ABOM = DISABLE;
    CAN_InitStructure.CAN_AWUM = DISABLE;
    CAN_InitStructure.CAN_NART = DISABLE;
    //CAN报文只被发送1次,不管发送的结果如何(成功、出错或仲裁丢失)
    CAN_InitStructure.CAN_RFLM = DISABLE;
    CAN_InitStructure.CAN_TXFP = DISABLE;
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq; //1--16
    CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; //1--8
    CAN_InitStructure.CAN_Prescaler = 4;
    ////(pclk1/((1+8+7)*16)) = 32Mhz/16/20 = 100Kbits
    //CAN_Init(&CAN_InitStructure);

    /*Initializes the CAN1	and CAN2 */
    CAN_Init(CAN1, &CAN_InitStructure);


    /* CAN cell init */
    CAN_InitStructure.CAN_TTCM = DISABLE; //禁止时间触发通信模式
    CAN_InitStructure.CAN_ABOM = DISABLE;
    CAN_InitStructure.CAN_AWUM = DISABLE;
    CAN_InitStructure.CAN_NART = DISABLE;
    //CAN报文只被发送1次,不管发送的结果如何(成功、出错或仲裁丢失)
    CAN_InitStructure.CAN_RFLM = DISABLE;
    CAN_InitStructure.CAN_TXFP = DISABLE;
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq; //1--16
    CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; //1--8
    CAN_InitStructure.CAN_Prescaler = 4;//20
    ////(pclk1/((1+8+7)*16)) = 32Mhz/16/20 = 100Kbits
    CAN_Init(CAN2, &CAN_InitStructure);


	CAN_FilterInitStructure.CAN_FilterNumber=0;     /* 过滤器0 */
  	CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;  /* 屏敝模式 */
  	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /* 32位 */
	CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;  /* 以下四个都为0, 表明不过滤任何id */
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; //选择FIFO0
	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; //使能过滤器
	CAN_FilterInit(&CAN_FilterInitStructure); //进入初始化函数
	CAN_SlaveStartBank(0);//5,Miller.Tao


}
示例#3
0
void Can_FILTER_Config(void)
{
	int8_t i=0;
	int8_t can1_filter_bank = 0,can2_filter_bank = 15;
	int8_t can1_fifo0_num = 0,can1_fifo1_num = 0;
	int8_t can2_fifo0_num = 0,can2_fifo1_num = 0;
	uint16_t temp1[14*4]={0};
	uint16_t temp2[14*4]={0};
	uint16_t temp3[14*4]={0};
	uint16_t temp4[14*4]={0};
	
	CAN_FilterInitTypeDef  CAN_FilterInitStructure;
	
#if USE_CAN2_R0 || USE_CAN2_R1
	CAN_SlaveStartBank(15);
#endif
	
	for(i=0;i<Can_Data_Num;i++)
	{
		if(Can_Database[i].Data_type==READ_ONLY)
		{
			if(Can_Database[i].Channel==1)
			{
				if(Can_Database[i].Fifo_num==CAN_Filter_FIFO0)
				{
					temp1[can1_fifo0_num] = (uint16_t)(Can_Database[i].Data_ID)<<5;
					can1_fifo0_num++;
				}else
				{
					temp2[can1_fifo1_num] = (uint16_t)(Can_Database[i].Data_ID)<<5;
					can1_fifo1_num++;
				}
				
			}
			else if(Can_Database[i].Channel==2)
			{
				if(Can_Database[i].Fifo_num==CAN_Filter_FIFO0)
				{
					temp3[can2_fifo0_num] = (uint16_t)(Can_Database[i].Data_ID)<<5;
					can2_fifo0_num++;
				}else
				{
					temp4[can2_fifo1_num] = (uint16_t)(Can_Database[i].Data_ID)<<5;
					can2_fifo1_num++;
				}
			}
		}
	}
	
	for(i=0;i<(3 - (can1_fifo0_num-1)%4);i++)
	{
		temp1[can1_fifo0_num+i] = temp1[can1_fifo0_num-1];
	}
	for(i=0;i<(3 - (can1_fifo1_num-1)%4);i++)
	{
		temp2[can1_fifo1_num+i] = temp2[can1_fifo1_num-1];
	}
	for(i=0;i<(3 - (can2_fifo0_num-1)%4);i++)
	{
		temp3[can2_fifo0_num+i] = temp3[can2_fifo0_num-1];
	}
	for(i=0;i<(3 - (can2_fifo1_num-1)%4);i++)
	{
		temp4[can2_fifo1_num+i] = temp4[can2_fifo1_num-1];
	}
	
	CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
	CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
	
    if(can1_fifo0_num>0)
	{
		for(i=0;i<=((can1_fifo0_num-1) / 4);i++)
		{
			CAN_FilterInitStructure.CAN_FilterNumber = can1_filter_bank;
			can1_filter_bank++;
			CAN_FilterInitStructure.CAN_FilterIdHigh	=	temp1[4*i+0];
			CAN_FilterInitStructure.CAN_FilterIdLow		=	temp1[4*i+1];
			CAN_FilterInitStructure.CAN_FilterMaskIdHigh=	temp1[4*i+2];
			CAN_FilterInitStructure.CAN_FilterMaskIdLow	=	temp1[4*i+3];
			CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
			CAN_FilterInit(&CAN_FilterInitStructure);
		}
	}
	
	if(can1_fifo1_num>0)
	{
		for(i=0;i<=((can1_fifo1_num-1) / 4);i++)
		{
			CAN_FilterInitStructure.CAN_FilterNumber = can1_filter_bank;
			can1_filter_bank++;
			CAN_FilterInitStructure.CAN_FilterIdHigh	=	temp2[4*i+0];
			CAN_FilterInitStructure.CAN_FilterIdLow		=	temp2[4*i+1];
			CAN_FilterInitStructure.CAN_FilterMaskIdHigh=	temp2[4*i+2];
			CAN_FilterInitStructure.CAN_FilterMaskIdLow	=	temp2[4*i+3];
			CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1;
			CAN_FilterInit(&CAN_FilterInitStructure);
		}
	}
	
	if(can2_fifo0_num>0)
	{
		for(i=0;i<=((can2_fifo0_num-1) / 4);i++)
		{
			CAN_FilterInitStructure.CAN_FilterNumber = can2_filter_bank;
			can2_filter_bank++;
			CAN_FilterInitStructure.CAN_FilterIdHigh	=	temp3[4*i+0];
			CAN_FilterInitStructure.CAN_FilterIdLow		=	temp3[4*i+1];
			CAN_FilterInitStructure.CAN_FilterMaskIdHigh=	temp3[4*i+2];
			CAN_FilterInitStructure.CAN_FilterMaskIdLow	=	temp3[4*i+3];
			CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
			CAN_FilterInit(&CAN_FilterInitStructure);
		}
	}
	
	if(can2_fifo1_num>0)
	{
		for(i=0;i<=((can2_fifo1_num-1) / 4);i++)
		{
			CAN_FilterInitStructure.CAN_FilterNumber = can2_filter_bank;
			can2_filter_bank++;
			CAN_FilterInitStructure.CAN_FilterIdHigh	=	temp4[4*i+0];
			CAN_FilterInitStructure.CAN_FilterIdLow		=	temp4[4*i+1];
			CAN_FilterInitStructure.CAN_FilterMaskIdHigh=	temp4[4*i+2];
			CAN_FilterInitStructure.CAN_FilterMaskIdLow	=	temp4[4*i+3];
			CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1;
			CAN_FilterInit(&CAN_FilterInitStructure);
		}
	}
}