Ejemplo n.º 1
0
void can_init(can_t* can_filters)
{

  gpio_clock_init(GPIOA);
  can_clock_init(CAN1);

  GPIO_InitTypeDef GPIO_InitStructure =
    {
      .GPIO_Pin   = CAN_RX_PIN,
      .GPIO_Speed = GPIO_Speed_50MHz,
      .GPIO_Mode  = GPIO_Mode_IN_FLOATING,
    };
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = CAN_TX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  CAN_InitTypeDef CAN_InitStructure;
  CAN_StructInit(&CAN_InitStructure);
  CAN_InitStructure.CAN_Prescaler = 1;
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
  CAN_InitStructure.CAN_SJW = CAN_SJW_4tq;
  CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_4tq;
  CAN_InitStructure.CAN_ABOM = ENABLE;
  CAN_InitStructure.CAN_RFLM = ENABLE;
  CAN_Init(CAN1, &CAN_InitStructure);

  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
  CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE);

  NVIC_InitTypeDef NVIC_InitStructure =
    {
      .NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn,
      .NVIC_IRQChannelPreemptionPriority = 7,
      .NVIC_IRQChannelSubPriority = 0,
      .NVIC_IRQChannelCmd = ENABLE,
    };
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX1_IRQn;
  NVIC_Init(&NVIC_InitStructure);

  for (int i = 0; i < can_filters->n_filters; i++)
    CAN_FilterInit(can_filters->CAN_filterInit + i);

  xCAN_receive_queue0 = xQueueCreate(10, sizeof(CanRxMsg));
  xCAN_receive_queue1 = xQueueCreate(10, sizeof(CanRxMsg));

}

void USB_LP_CAN1_RX0_IRQHandler()
{
  CanRxMsg RxMessage;
  portBASE_TYPE resch = pdFALSE;
  CAN_Receive(CAN1, CAN_FIFO0,&RxMessage);
  xQueueSendFromISR(xCAN_receive_queue0, &RxMessage, &resch);
  portEND_SWITCHING_ISR(resch);
}
Ejemplo n.º 2
0
void Can1_Init(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
      // 首先打开电源及时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  
   
    NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
   
    NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    // 然后配置pin
		Can_GPIO_Config();
    CAN_config_init();
    CAN_ITConfig(CAN1, CAN_IT_FMP0 | CAN_IT_FF0 | CAN_IT_FOV0, ENABLE);  // fifo0中断
    CAN_ITConfig(CAN1, CAN_IT_FMP1 | CAN_IT_FF1 | CAN_IT_FOV1, ENABLE);  // fifo1中断
    //CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);                // 发送中断
}
Ejemplo n.º 3
0
EXPORT void Can_Hw_DisableControllerInterrupts(uint8 controller)
{
	  imask_t state;
	  Can_UnitType *canUnit;
	  CAN_HW_t *canHw;

	  canUnit = CAN_GET_PRIVATE_DATA(controller);

	  VALIDATE_NO_RV( (canUnit->state!=CANIF_CS_UNINIT), 0x4, CAN_E_UNINIT );

	  Irq_Save(state);
	  if(canUnit->lock_cnt > 0 )
	  {
	    // Interrupts already disabled
	    canUnit->lock_cnt++;
	    Irq_Restore(state);
	    return;
	  }
	  canUnit->lock_cnt++;
	  Irq_Restore(state);

	  /* Don't try to be intelligent, turn everything off */
	  canHw = GetController(controller);

	  /* Turn off the tx interrupt mailboxes */
	   CAN_ITConfig(canHw, CAN_IT_TME, DISABLE);

	   /* Turn off the bus off/tx warning/rx warning and error and rx  */
	   CAN_ITConfig(canHw, CAN_IT_FMP0 | CAN_IT_BOF | CAN_IT_ERR | CAN_IT_WKU, DISABLE);
}
Ejemplo n.º 4
0
extern hal_result_t hal_can_enable(hal_can_t id)
{
	hal_result_t res;
	//hal_can_internals_t *intitem = s_hal_can_internals[HAL_can_id2index(id)];
    hal_can_internal_item_t* intitem = s_hal_can_theinternals.items[HAL_can_id2index(id)];
    CAN_TypeDef* CANx = HAL_can_port2peripheral(id);

    if(hal_false == s_hal_can_initted_is(id))
    {
        return(hal_res_NOK_generic);
    }

    if(NULL == intitem)
    {
        return(hal_res_NOK_generic);
    }

    //res = s_hal_can_hw_registers_init(id); // hardware setup
    
    hl_can_enable((hl_can_t)id);

    if(res != hal_res_OK)
    {
    	return(hal_res_NOK_generic);
    }


    // disable scheduling
    hal_base_osal_scheduling_suspend();

    intitem->enabled = 1;

    // configure interrupts on rx (CAN_IT_FMP0 -> FIFO 0 message pending) and tx (CAN_IT_TME -> transmit mailbox empty)
    // it is the same using only one call of CAN_ITConfig() or two ...
    CAN_ITConfig(HAL_can_port2peripheral(id), (CAN_IT_FMP0 | CAN_IT_TME), ENABLE);
    //CAN_ITConfig(HAL_can_port2peripheral(id), CAN_IT_FMP0, ENABLE);
    //CAN_ITConfig(HAL_can_port2peripheral(id), CAN_IT_TME, ENABLE);
    CAN_ITConfig( HAL_can_port2peripheral(id), CAN_IT_ERROR,  ENABLE); // VALE
    
    // nvic 
    s_hal_can_isr_rx_enable(id);
    // dont enable the nvic for the tx
    //s_hal_can_tx_enable(id);
    s_hal_can_isr_tx_disable(id);
    intitem->txisrisenabled = 0;
    
    s_hal_can_isr_err_enable(id);// VALE
    
    // it is in can driver by K&%L. it was not in hal-1. it seems not be important. it does not make can2 work ..
    CANx->MCR &= ~(1 << 0);             /* normal operating mode, reset INRQ   */
    while (CANx->MSR & (1 << 0));


    // enable scheduling 
    hal_base_osal_scheduling_restart();

	return(hal_res_OK);
}
Ejemplo n.º 5
0
void Can_IT_Config(void)
{
#if  USE_CAN1_R0
	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
#endif
#if  USE_CAN1_R1
	CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE);
#endif
#if  USE_CAN2_R0
	CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
#endif
#if  USE_CAN2_R1
	CAN_ITConfig(CAN2, CAN_IT_FMP1, ENABLE);
#endif
}
Ejemplo n.º 6
0
/*
 ***************************************************************
 * 名称: can1_init 
 * 功能: can1初始化
 * 输入: 1.baud_num
 * 输出: NULL 
 * 返回: NULL 
 * 描述: 无 
 ***************************************************************
 */  
void can1_init(u32 baud_num)
{
	CAN_InitTypeDef CAN_InitStructure;
	
	/* CAN register init */
	CAN_NVIC_Configuration();
	CAN_GPIO_Configuration();
	CAN_Address_GPIO_Config();

	CAN_StructInit(&CAN_InitStructure);

	/* CAN cell init */
	CAN_InitStructure.CAN_TTCM = DISABLE;	// 非时间触发通信模式 
	CAN_InitStructure.CAN_ABOM = DISABLE;	// 软件自动离线管理	
	CAN_InitStructure.CAN_AWUM = DISABLE;	// 睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
	CAN_InitStructure.CAN_NART = DISABLE;	// 禁止报文自动传送  
	CAN_InitStructure.CAN_RFLM = DISABLE;	// 报文不锁定,新的覆盖旧的 
	CAN_InitStructure.CAN_TXFP = ENABLE;	// 优先级由报文标识符决定
	CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
	CAN_InitStructure.CAN_SJW = BaudTable[baud_num].SJW;
	CAN_InitStructure.CAN_BS1 = BaudTable[baud_num].BS1;
	CAN_InitStructure.CAN_BS2 = BaudTable[baud_num].BS2;
	CAN_InitStructure.CAN_Prescaler = BaudTable[baud_num].PreScale;

	CAN_Init(CAN1, &CAN_InitStructure);
	
	//设置CAN接收过滤器
 	CAN_ConfigFilter(0, 0x7ff);	// 广播地址,接受广播命令
	CAN_ConfigFilter(1, Read_CAN_Address()&0x7f);	// 本节点真实地址

	//使能接收中断
	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
Ejemplo n.º 7
0
/**
 * @brief  This function handles CAN1 TX irq and sends more data if available
 */
static void PIOS_CAN_TxGeneric(void)
{
	CAN_ClearITPendingBit(can_dev->cfg->regs, CAN_IT_TME);

	bool valid = PIOS_CAN_validate(can_dev);
	PIOS_Assert(valid);

	bool tx_need_yield = false;
	
	if (can_dev->tx_out_cb) {

		// Prepare CAN message structure
		CanTxMsg msg;
		msg.StdId = CAN_COM_ID;
		msg.ExtId = 0;
		msg.IDE = CAN_ID_STD;
		msg.RTR = CAN_RTR_DATA;
		msg.DLC = (can_dev->tx_out_cb)(can_dev->tx_out_context, msg.Data, MAX_SEND_LEN, NULL, &tx_need_yield);

		// Send message and get mailbox number
		if (msg.DLC > 0) {
			CAN_Transmit(can_dev->cfg->regs, &msg);
		} else {
			CAN_ITConfig(can_dev->cfg->regs, CAN_IT_TME, DISABLE);
		}

		// TODO: deal with failure to send and keep the message to retransmit
	}
	
#if defined(PIOS_INCLUDE_FREERTOS)
	portEND_SWITCHING_ISR(tx_need_yield ? pdTRUE : pdFALSE);
#endif /* defined(PIOS_INCLUDE_FREERTOS) */
}
Ejemplo n.º 8
0
int RecvFrame(u8 type,u8 *pRecvFrame)
{
	u8 os_err;
	int iLength=0;//系统时间 有问题
	
	while(1){
		
		switch(type){
		case OBD_TYPE_ISO:
			iLength=Normal_RecvFrame(pRecvFrame,50);
			break;
		case OBD_TYPE_KWP:
			iLength=KWP_RecvFrame(pRecvFrame);
			break;
		case OBD_TYPE_VPW:
			iLength=VPW_RecvFrame(pRecvFrame);
			break;
		case OBD_TYPE_PWM:
			iLength=PWM_RecvFrame(pRecvFrame);
			break;
		case OBD_TYPE_CAN1:
		case OBD_TYPE_CAN2:
			CAN_ITConfig(CAN1,CAN_IT_FMP0 | CAN_IT_FOV0, ENABLE);
			OSSemPend(sem_obd_prot, 1*OS_TICKS_PER_SEC, &os_err);//timeout: 1 seconds
			if(os_err)break;
			iLength=CAN_RecvFrame(pRecvFrame);
			break;
		}
		if(iLength==0)continue;
		else break;
	}
	my_icar.event = 0;
	return iLength;
}
static void CAN_device_init_1(int baud)
{
    CAN_DeInit(CAN1);

    /* CAN GPIOs configuration ************************************************* */
    /* Enable GPIO clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

    /* Connect CAN pins to Alternate Function */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_9);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_9);
    initGPIO(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);

    /* CAN configuration ******************************************************* */
    /* Enable CAN clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

    initCAN(CAN1, baud);

    //set default filter
    CAN_device_set_filter(0, 0, 0, 0, 0);

    /* Enable FIFO 0 message pending Interrupt */
    CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

    initCANInterrupts(CAN1, USB_LP_CAN1_RX0_IRQn);
}
Ejemplo n.º 10
0
void 			CAN2_TX_IRQHandler(void)
{
CanTxMsg	tx;
					if(_buffer_pull(__can->tx,&tx,sizeof(CanTxMsg)))
						CAN_Transmit(CAN2,&tx);
					else
						CAN_ITConfig(CAN2, CAN_IT_TME, DISABLE);						
}
Ejemplo n.º 11
0
Archivo: main.c Proyecto: kitaev/can
void CAN_Config(void) {
    CAN_InitTypeDef CAN_InitStruct;
    CAN_FilterInitTypeDef CAN_FilterInitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_9);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_9);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);


    // can peripherial
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

    CAN_StructInit(&CAN_InitStruct);
    CAN_InitStruct.CAN_ABOM = DISABLE;
    CAN_InitStruct.CAN_AWUM = DISABLE;
    CAN_InitStruct.CAN_NART = DISABLE;
    CAN_InitStruct.CAN_RFLM = DISABLE;
    CAN_InitStruct.CAN_TTCM = DISABLE;
    CAN_InitStruct.CAN_TXFP = DISABLE;
    CAN_InitStruct.CAN_Prescaler = 18; // 125000 Baud rate assuimg CAN speed 36MHz
    CAN_InitStruct.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStruct.CAN_BS1 = CAN_BS1_13tq;
    CAN_InitStruct.CAN_BS2 = CAN_BS2_2tq;
    CAN_InitStruct.CAN_Mode = CAN_Mode_Normal;
    CAN_Init(CAN1, &CAN_InitStruct);

    // can filter
    CAN_FilterInitStruct.CAN_FilterNumber = 0;
    CAN_FilterInitStruct.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStruct.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStruct.CAN_FilterActivation = ENABLE;
    CAN_FilterInitStruct.CAN_FilterFIFOAssignment = CAN_FIFO0;
    CAN_FilterInitStruct.CAN_FilterIdHigh = 0;
    CAN_FilterInitStruct.CAN_FilterIdLow = 0;
    CAN_FilterInitStruct.CAN_FilterMaskIdHigh = 0;
    CAN_FilterInitStruct.CAN_FilterMaskIdLow = 0;
    CAN_FilterInit(&CAN_FilterInitStruct);

    // can interrupt
    CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

    NVIC_InitStruct.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_Init(&NVIC_InitStruct);
}
Ejemplo n.º 12
0
void DST_CAN_1::CAN_Configuration_Init(void)
{
	GPIO_InitTypeDef				GPIO_InitStructure;	
	CAN_InitTypeDef			 		CAN_InitStructure;
 	CAN_FilterInitTypeDef 	CAN_FilterInitStructure;
	NVIC_InitTypeDef 				NVIC_InitStructure;

 //使能相关时钟
	RCC_AHB1PeriphClockCmd(RCC_CAN_1, ENABLE);//使能PORTA时钟	                   											 

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟	

	//初始化GPIO
	GPIO_InitStructure.GPIO_Pin = CAN_1_TX | CAN_1_RX;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
	GPIO_Init(GPIO_CAN_1, &GPIO_InitStructure);//初始化CAN_1_TX,CAN_1_RX

	//引脚复用映射配置
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource1,GPIO_AF_CAN1); //GPIOD1复用为CAN1_TX
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource0,GPIO_AF_CAN1); //GPIOD0复用为CAN1_RX

	//CAN单元设置
	CAN_InitStructure.CAN_TTCM=DISABLE;	//非时间触发通信模式   
	CAN_InitStructure.CAN_ABOM=DISABLE;	//软件自动离线管理	  
	CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
	CAN_InitStructure.CAN_NART=ENABLE;	//禁止报文自动传送 
	CAN_InitStructure.CAN_RFLM=DISABLE;	//报文不锁定,新的覆盖旧的  
	CAN_InitStructure.CAN_TXFP=DISABLE;	//优先级由报文标识符决定 
	CAN_InitStructure.CAN_Mode= CAN_Mode_Normal;	 //模式设置 
	CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;	//重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
	CAN_InitStructure.CAN_BS1=CAN_BS1_9tq; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
	CAN_InitStructure.CAN_BS2=CAN_BS2_4tq;//Tbs2范围CAN_BS2_1tq ~	CAN_BS2_8tq
	CAN_InitStructure.CAN_Prescaler=3;  //分频系数(Fdiv)为brp+1	
	CAN_Init(CAN1, &CAN_InitStructure);   // 初始化CAN1 
	
	//配置过滤器
	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;////32位ID
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32位MASK
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//过滤器0关联到FIFO0
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器0
	CAN_FilterInit(&CAN_FilterInitStructure);//滤波器初始化
	
	CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//★FIFO0★消息挂号中断允许.		    

	NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     // 主优先级为1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;            // 次优先级为0
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}
Ejemplo n.º 13
0
EXPORT void Can_Hw_EnableControllerInterrupts( uint8 controller )
{
	 imask_t state;
	  Can_UnitType *canUnit;
	  CAN_HW_t *canHw;
	  const Can_ControllerConfigType *canHwConfig;

	  canUnit = CAN_GET_PRIVATE_DATA(controller);

	  VALIDATE_NO_RV( (canUnit->state!=CANIF_CS_UNINIT), 0x5, CAN_E_UNINIT );

	  Irq_Save(state);
	  if( canUnit->lock_cnt > 1 )
	  {
	    // IRQ should still be disabled so just decrement counter
	    canUnit->lock_cnt--;
	    Irq_Restore(state);
	    return;
	  } else if (canUnit->lock_cnt == 1)
	  {
	    canUnit->lock_cnt = 0;
	  }
	  Irq_Restore(state);

	  canHw = GetController(controller);

	  canHwConfig = CAN_GET_CONTROLLER_CONFIG(Can_Global.channelMap[controller]);

	   if( canHwConfig->CanRxProcessing == CAN_PROCESS_TYPE_INTERRUPT ) {
	     /* Turn on the rx interrupt */
	 	CAN_ITConfig(canHw, CAN_IT_FMP0, ENABLE);
	   }
	   if( canHwConfig->CanTxProcessing == CAN_PROCESS_TYPE_INTERRUPT ) {
	 	/* Turn on the tx interrupt mailboxes */
	   	CAN_ITConfig(canHw, CAN_IT_TME, ENABLE);
	   }

	   // BusOff here represents all errors and warnings
	   if( canHwConfig->CanBusOffProcessing == CAN_PROCESS_TYPE_INTERRUPT ) {
	 	/* Turn on the bus off/tx warning/rx warning and error and rx  */
	 	CAN_ITConfig(canHw, CAN_IT_BOF | CAN_IT_ERR | CAN_IT_WKU, ENABLE);
	   }

	   return;
}
Ejemplo n.º 14
0
static void PIOS_CAN_RxStart(uintptr_t can_id, uint16_t rx_bytes_avail)
{
	struct pios_can_dev *can_dev = (struct pios_can_dev *)can_id;
	
	bool valid = PIOS_CAN_validate(can_dev);
	PIOS_Assert(valid);
	
	CAN_ITConfig(can_dev->cfg->regs, CAN_IT_FMP1, ENABLE);
}
Ejemplo n.º 15
0
void InitCAN(void)
{
	// Setup CAN Pins
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	// Alternate functions
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
	
	
	// Setup CAN-modules
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

	/* CAN register init */
	CAN_DeInit(CAN1);

	/* CAN cell init */
	CAN_InitTypeDef CAN_InitStructure;
	CAN_InitStructure.CAN_TTCM = DISABLE;
	CAN_InitStructure.CAN_ABOM = DISABLE;
	CAN_InitStructure.CAN_AWUM = DISABLE;
	CAN_InitStructure.CAN_NART = DISABLE;
	CAN_InitStructure.CAN_RFLM = DISABLE;
	CAN_InitStructure.CAN_TXFP = DISABLE;
	CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
	CAN_InitStructure.CAN_SJW = CAN_SJW_3tq;

	/* CAN Baudrate = 500 KBps (CAN clocked at 42 MHz) */
	CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;
	CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
	CAN_InitStructure.CAN_Prescaler = (42000000 / 7) / 500000;
	CAN_Init(CAN1, &CAN_InitStructure);

	/* CAN filter init */
	CAN_FilterInitTypeDef CAN_FilterInitStructure;
	CAN_FilterInitStructure.CAN_FilterNumber = 0;
	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 = 0;
	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
	CAN_FilterInit(&CAN_FilterInitStructure);

	/* Enable FIFO 0 message pending Interrupt */
	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
Ejemplo n.º 16
0
void TSVN_CAN_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	CAN_InitTypeDef CAN_InitStructure;
	CAN_FilterInitTypeDef CAN_FilterInitStructure;
	NVIC_InitTypeDef  NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);  
  GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_PD01 , ENABLE);
	
	CAN_DeInit(CAN1);
	
	CAN_InitStructure.CAN_TTCM = DISABLE;
  CAN_InitStructure.CAN_ABOM = DISABLE;
  CAN_InitStructure.CAN_AWUM = DISABLE;
  CAN_InitStructure.CAN_NART = DISABLE;
  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_3tq;  //3
  CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;	//5
  CAN_InitStructure.CAN_Prescaler = 4;
	
	CAN_FilterInitStructure.CAN_FilterNumber = 0;
	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 = 0;
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);
	CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);  
	CAN_Init(CAN1, &CAN_InitStructure);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
	NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
}
Ejemplo n.º 17
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
  */

  /* Configures LED 1..4 */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDOff(LED1);
  DFLASH

  /* NVIC configuration */
  NVIC_Config();
  RCC_Configuration();
  GPIO_Configuration();

  USART_InitStructure.USART_BaudRate = 19200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
  Debug("\nZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz!!!!!!!!!!!!!!!!!!!!");

  DFLASH

  /* CAN configuration */
  CAN_Config();
  Delay();
  CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);

  /* Infinite loop */
  short int x,led;

  while(1) {
    x++;
    if (x == 0) {
      if (led) {
          STM_EVAL_LEDOn(LED1);
          Delay();
      }
      else {
          STM_EVAL_LEDOff(LED1);
          TxMessage.Data[0]++;
          CAN_Transmit(CANx, &TxMessage);
          Delay();
      }
      led ^= 1;
    }
  }//END While
}//END main
Ejemplo n.º 18
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* NVIC configuration */
  NVIC_Config();

  /* Configures LED 1..4 */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  
  /* Configure Push button key */
  STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); 
   
  /* CAN configuration */
  CAN_Config();
  
  CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);

  /* turn off all leds*/
  STM_EVAL_LEDOff(LED1);
  STM_EVAL_LEDOff(LED2);
  STM_EVAL_LEDOff(LED3);
  STM_EVAL_LEDOff(LED4);
 
  /* Infinite loop */
  while(1)
  {
    while(STM_EVAL_PBGetState(BUTTON_KEY) == KEY_PRESSED)
    {
      if(KeyNumber == 0x4) 
      {
        KeyNumber = 0x00;
      }
      else
      {
        LED_Display(++KeyNumber);
        TxMessage.Data[0] = KeyNumber;
        CAN_Transmit(CANx, &TxMessage);
        Delay();
        
        while(STM_EVAL_PBGetState(BUTTON_KEY) != KEY_NOT_PRESSED)
        {
        }
      }
    }
  }
}
Ejemplo n.º 19
0
 /**
  * @file   CAN_Configuration
  * @brief  Configures the CAN
  * @param  无
  * @retval 无
  */
void CAN_Configuration(void)
{
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;

  GPIO_Configuration();
  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE; /* 时间触发禁止, 时间触发:CAN硬件的内部定时器被激活,并且被用于产生时间戳 */
  CAN_InitStructure.CAN_ABOM = DISABLE; /* 自动离线禁止,自动离线:一旦硬件监控到128次11个隐性位,就自动退出离线状态。在这里要软件设定后才能退出 */
  CAN_InitStructure.CAN_AWUM = DISABLE; /* 自动唤醒禁止,有报文来的时候自动退出休眠	*/
  CAN_InitStructure.CAN_NART = DISABLE; /* 报文重传, 如果错误一直传到成功止,否则只传一次 */
  CAN_InitStructure.CAN_RFLM = DISABLE; /* 接收FIFO锁定, 1--锁定后接收到新的报文摘不要,0--接收到新的报文则覆盖前一报文	*/
  CAN_InitStructure.CAN_TXFP = ENABLE;  /* 发送优先级  0---由标识符决定  1---由发送请求顺序决定	*/
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; /* 模式	*/
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;      /* 重新同步跳宽,只有can硬件处于初始化模式时才能访问这个寄存器 */
  CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq;      /* 时间段1 */
  CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;      /* 时间段2 */
  CAN_InitStructure.CAN_Prescaler = 45;         /* 波特率预分频数 */  
 
  /* 波特率计算方法 */
  /* CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1)  此处计算为  CANbps=36000000/(45*(4+3+1))=100kHz */   														  //此处Tseg1+1 = CAN_BS1_8tp
  /* 配置大方向: Tseg1>=Tseg2  Tseg2>=tq; Tseg2>=2TSJW */
    
  if (CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED) 		
  {
    /* 初始化时先设置CAN_MCR的初始化位 */  														
    /* 然后查看硬件是否真的设置了CAN_MSR的初始化位来确认是否进入了初始化模式  */		 													                  
  }	
  /* 配置CAN过滤器 */
  /* 32位对应的id */
  /* stdid[10:0],extid[17:0],ide,rtr	*/
  /* 16位对应的id */
  /* stdid[10:0],ide,rtr,extid[17:15] */
  /* 一般使用屏蔽模式	*/
  /* 要注意的是fifo接收存满了中断,还有就是fifo的概念,即取的一直是最早那一个数据, 要释放才能取下一个数据 */
  /* 常使用的中断有 */
  /* 1,有信息中断,即fifo挂号中断 */
  /* 2,fifo满中断	*/
  /* 3,fifo满之后又有信息来则中断,即fifo溢出中断	*/
  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=0;  /* 能够通过该过滤器的报文存到fifo0中 */
  CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
  CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);   /* 挂号中断, 进入中断后读fifo的报文函数释放报文清中断标志 */
  CAN_FilterInit(&CAN_FilterInitStructure);
}
Ejemplo n.º 20
0
static void PIOS_CAN_TxStart(uintptr_t can_id, uint16_t tx_bytes_avail)
{
	struct pios_can_dev *can_dev = (struct pios_can_dev *)can_id;
	
	bool valid = PIOS_CAN_validate(can_dev);
	PIOS_Assert(valid);

 	CAN_ITConfig(can_dev->cfg->regs, CAN_IT_TME, ENABLE);
	
	USB_HP_CAN1_TX_IRQHandler();
}
Ejemplo n.º 21
0
void CAN2_Configuration(void)
{
    CAN_InitTypeDef        can;
    CAN_FilterInitTypeDef  can_filter;
    GPIO_InitTypeDef       gpio;
    NVIC_InitTypeDef       nvic;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); 

    gpio.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 ;
    gpio.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(GPIOB, &gpio);

    nvic.NVIC_IRQChannel = CAN2_RX0_IRQn;
    nvic.NVIC_IRQChannelPreemptionPriority = 1;
    nvic.NVIC_IRQChannelSubPriority = 0;
    nvic.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvic);

    CAN_DeInit(CAN2);
    CAN_StructInit(&can);

    can.CAN_TTCM = DISABLE;
    can.CAN_ABOM = DISABLE;    
    can.CAN_AWUM = DISABLE;    
    can.CAN_NART = DISABLE;    
    can.CAN_RFLM = DISABLE;    
    can.CAN_TXFP = ENABLE;     
    can.CAN_Mode = CAN_Mode_Normal; 
    can.CAN_SJW  = CAN_SJW_1tq;
    can.CAN_BS1 = CAN_BS1_9tq;
    can.CAN_BS2 = CAN_BS2_4tq;
    can.CAN_Prescaler = 3;   //CAN BaudRate 42/(1+9+4)/3=1Mbps
    CAN_Init(CAN2, &can);
    
    can_filter.CAN_FilterNumber=14;
    can_filter.CAN_FilterMode=CAN_FilterMode_IdMask;
    can_filter.CAN_FilterScale=CAN_FilterScale_32bit;
    can_filter.CAN_FilterIdHigh=0x0000;
    can_filter.CAN_FilterIdLow=0x0000;
    can_filter.CAN_FilterMaskIdHigh=0x0000;
    can_filter.CAN_FilterMaskIdLow=0x0000;
    can_filter.CAN_FilterFIFOAssignment=0;//the message which pass the filter save in fifo0
    can_filter.CAN_FilterActivation=ENABLE;
    CAN_FilterInit(&can_filter);
    
    CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);
}
Ejemplo n.º 22
0
EXPORT Can_ReturnType Can_Hw_Write( Can_HwHandleType/* Can_HTHType */ hth, const Can_PduType *pduInfo ) {
	  Can_ReturnType rv = CAN_OK;
	  CAN_HW_t *canHw;
	  const Can_HardwareObjectType *hohObj;
	  const Can_ControllerConfigType *canHwConfig;
	  uint32 controller;
	  imask_t state;

	  hohObj = Can_FindHoh(hth, &controller);
	  if (hohObj == NULL)
	    return CAN_NOT_OK;

	  Can_UnitType *canUnit = CAN_GET_PRIVATE_DATA(controller);

	  canHw = GetController(controller);
	  Irq_Save(state);

	  CanTxMsg TxMessage;

	  TxMessage.RTR=CAN_RTR_DATA;
	  TxMessage.DLC=pduInfo->length;

	  memcpy(TxMessage.Data, pduInfo->sdu, pduInfo->length);

	  if (hohObj->CanIdType == CAN_ID_TYPE_EXTENDED) {
		TxMessage.IDE=CAN_ID_EXT;
		TxMessage.ExtId=pduInfo->id;
	  } else {
		TxMessage.IDE=CAN_ID_STD;
		TxMessage.StdId=pduInfo->id;
	  }

	  // check for any free box
	  if(CAN_Transmit(canHw,&TxMessage) != CAN_NO_MB) {
	    canHwConfig = CAN_GET_CONTROLLER_CONFIG(Can_Global.channelMap[controller]);

	    if( canHwConfig->CanTxProcessing == CAN_PROCESS_TYPE_INTERRUPT ) {
	  	  /* Turn on the tx interrupt mailboxes */
	    	CAN_ITConfig(canHw,CAN_IT_TME, ENABLE);
	    }

		// Increment statistics
		canUnit->stats.txSuccessCnt++;

	    // Store pdu handle in unit to be used by TxConfirmation
	    canUnit->swPduHandle = pduInfo->swPduHandle;
	  } else {
	    rv = CAN_BUSY;
	  }
	  Irq_Restore(state);

	  return rv;
}
Ejemplo n.º 23
0
/*******************************************************************************
* Function Name	: 
* Description		: 
* Output				:
* Return				:
*******************************************************************************/
void			_CAN::TX_ISR(_CAN *p) {
CanTxMsg	buf={0,0,CAN_ID_STD,CAN_RTR_DATA,0,0,0,0,0,0,0,0,0};
					if(p) {																					// klic za prijavo instance
						me=p;
						me->tx=_buffer_init(100*sizeof(CanTxMsg));
					} else {																				// klic iz ISR, instanca in buffer morata bit ze formirana
						if(_buffer_pull(me->tx,&buf,sizeof(CanTxMsg)))
							CAN_Transmit(__CAN__,&buf);									// oddaj, ce je kaj na bufferju
						else
							CAN_ITConfig(__CAN__, CAN_IT_TME, DISABLE);	// sicer zapri interrupt
					}					
}
Ejemplo n.º 24
0
extern hal_result_t hal_can_disable(hal_can_t id) 
{
    //hal_can_internals_t *intitem = s_hal_can_internals[HAL_can_id2index(id)];
    hal_can_internal_item_t* intitem = s_hal_can_theinternals.items[HAL_can_id2index(id)];

    if(hal_false == s_hal_can_initted_is(id))
    {
        return(hal_res_NOK_generic);
    }

    if(NULL == intitem)
    {
        return(hal_res_NOK_generic);
    }

    // disable scheduling
    hal_base_osal_scheduling_suspend();

    intitem->enabled = 0;

    // deconfigure interrupts on rx (CAN_IT_FMP0 -> FIFO 0 message pending) and tx (CAN_IT_TME -> transmit mailbox empty)
    // it is the same using only one call of CAN_ITConfig() or two ...
    CAN_ITConfig(HAL_can_port2peripheral(id), (CAN_IT_FMP0 | CAN_IT_TME), DISABLE);
    CAN_ITConfig(HAL_can_port2peripheral(id), CAN_IT_TME, DISABLE);
    CAN_ITConfig(HAL_can_port2peripheral(id), CAN_IT_FMP0, DISABLE);
    CAN_ITConfig( HAL_can_port2peripheral(id), CAN_IT_ERROR,  DISABLE); // VALE
    // nvic 
    s_hal_can_isr_rx_disable(id);
    // dont disable the nvic for the tx. it was never enabled
    s_hal_can_isr_tx_disable(id);
    intitem->txisrisenabled = 0;
    
    s_hal_can_isr_err_disable(id); // VALE
    
    // enable scheduling 
    hal_base_osal_scheduling_restart();

    return(hal_res_OK);
}
Ejemplo n.º 25
0
/*******************************************************************************
* Function Name	: 
* Description		: 
* Output				:
* Return				:
*******************************************************************************/
void			_CAN::Send(CanTxMsg *msg) {
					if(_buffer_count(tx) > 0 || (__CAN__->TSR & CAN_TSR_TME) == 0) {
						_buffer_push(tx,&msg,sizeof(msg));
					} else
						CAN_Transmit(__CAN__,msg);
					CAN_ITConfig(__CAN__, CAN_IT_TME, ENABLE);		
//________ debug print__________________________________________________________________
					if(_BIT(_LM::debug, DBG_CAN_TX)) {
//						_io *temp=_stdio(lm->io);
						printf("\r\n:%04d>%02X ",__time__ % 10000,msg->StdId);
						for(int i=0;i<msg->DLC;++i)
							printf(" %02X",msg->Data[i]);
						printf("\r\n:");
//						_stdio(temp);
					}
}
Ejemplo n.º 26
0
void CAN_Initialise()
{
	CAN_InitTypeDef CAN_InitStruct;
    CAN_FilterInitTypeDef  CAN_FilterInitStructure;
	
	void GPIO_Initialise();
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE);
	
	/* CAN register init */
	CAN_DeInit(CAN2);
	
	/* CAN cell init */
	CAN_InitStruct.CAN_TTCM = DISABLE;
	CAN_InitStruct.CAN_ABOM = DISABLE;
	CAN_InitStruct.CAN_AWUM = DISABLE;
	CAN_InitStruct.CAN_NART = DISABLE;
	CAN_InitStruct.CAN_RFLM = DISABLE;
	CAN_InitStruct.CAN_TXFP = DISABLE;
	CAN_InitStruct.CAN_Mode = CAN_Mode_Normal;
	CAN_InitStruct.CAN_SJW = CAN_SJW_3tq;

    //Bus length 3-5m.
    
	/* CAN Baudrate = 500 KBps (CAN clocked at 42 MHz) */
	CAN_InitStruct.CAN_BS1 = CAN_BS1_4tq;
	CAN_InitStruct.CAN_BS2 = CAN_BS2_2tq;
	CAN_InitStruct.CAN_Prescaler = (42000000 / 7) / 500000; //12
	CAN_Init(CAN2, &CAN_InitStruct);

	/* CAN filter init */
	CAN_FilterInitStructure.CAN_FilterNumber = 0;
	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 = 0;
	CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
	CAN_FilterInit(&CAN_FilterInitStructure);

	/* Enable FIFO 0 message pending Interrupt */
	CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);

}
Ejemplo n.º 27
0
void _CAN1_Init()
{
	
	CAN_InitTypeDef CAN_InitStructure;
	CAN_FilterInitTypeDef  CAN_FilterInitStructure; 
	NVIC_InitTypeDef NVIC_InitStructure;	

	_CAN1_GPIO_Init();
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
	CAN_InitStructure.CAN_TTCM=DISABLE;
	CAN_InitStructure.CAN_ABOM=DISABLE;
	CAN_InitStructure.CAN_AWUM=DISABLE;
	CAN_InitStructure.CAN_NART=DISABLE;
	CAN_InitStructure.CAN_RFLM=DISABLE;
	CAN_InitStructure.CAN_TXFP=DISABLE;
	//CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
	CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
	CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;/*重新同步跳宽*/
	CAN_InitStructure.CAN_BS1=CAN_BS1_4tq;/*时间段1*/
	CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;/*时间段2*/
	CAN_InitStructure.CAN_Prescaler=45;/*波特率预分频数*/
	CAN_Init(CAN1,&CAN_InitStructure);
	
	//允许所有报文通过
	CAN_FilterInitStructure.CAN_FilterNumber=0; //设置过滤器组0,范围为0~13
	CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定过滤器被设置为标识符屏蔽模式
	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//给出过滤器位宽为32位
	CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;//为过滤器分配缓存 此处到//fifo 因为 CAN_FilterInitStructure.CAN_FilterNumber=0;
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
	CAN_FilterInit(&CAN_FilterInitStructure);
	CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);
	
	NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
}
Ejemplo n.º 28
0
/*
 * 函数名:CAN_Filter_Config
 * 描述  :CAN的过滤器 配置
 * 输入  :无
 * 输出  : 无
 * 调用  :内部调用
 */
static void CAN_Filter_Config(void)
{
   CAN_FilterInitTypeDef  CAN_FilterInitStructure;

	/*CAN过滤器初始化*/
	CAN_FilterInitStructure.CAN_FilterNumber=0;						//过滤器组0
    CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;	//工作在标识符屏蔽位模式
	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;	//过滤器位宽为单个32位。
	/* 使能报文标示符过滤器按照标示符的内容进行比对过滤,扩展ID不是如下的就抛弃掉,是的话,会存入FIFO0。 */

    CAN_FilterInitStructure.CAN_FilterIdHigh= (((u32)0x1314<<3)&0xFFFF0000)>>16;				//要过滤的ID高位 
    CAN_FilterInitStructure.CAN_FilterIdLow= (((u32)0x1314<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF; //要过滤的ID低位 
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh= 0xFFFF;			//过滤器高16位每位必须匹配
    CAN_FilterInitStructure.CAN_FilterMaskIdLow= 0xFFFF;			//过滤器低16位每位必须匹配
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0 ;				//过滤器被关联到FIFO0
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;			//使能过滤器
	CAN_FilterInit(&CAN_FilterInitStructure);
	/*CAN通信中断使能*/
	CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
Ejemplo n.º 29
0
void CAN1_RX0_IRQHandler() {

  /* Advance buffer head. */
  uint16_t tempRX_Head = ((&CAN1_Buf)->RX_Head + 1) & (CAN_BUFSIZE-1);

  /* Check for overflow. */
  uint16_t tempRX_Tail = (&CAN1_Buf)->RX_Tail;
  CanRxMsg data;
  CAN_Receive(CAN1, CAN_FIFO0, &data);

  if (tempRX_Head == tempRX_Tail) {
      /* Disable the CAN1 Receive interrupt */
      CAN_ITConfig(CAN1, CAN_IT_FMP0, DISABLE);
  }else{
      (&CAN1_Buf)->RX[(&CAN1_Buf)->RX_Head] = data;
      (&CAN1_Buf)->RX_Head = tempRX_Head;
  }

  __DSB();      // prevent erroneous recall of this handler due to delayed memory write
}
Ejemplo n.º 30
0
/**
  * @brief  Main program
  * @param  None
  * @retval : None
  */
int main(void)
{
 uint32_t i=0;
  
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* NVIC configuration ------------------------------------------------------*/
  NVIC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  /* CAN configuration */
  CAN_Config();

  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);

  /* turn off all leds*/
  LED_Display(5);

  /* Infinite loop*/
  while(1)
  {
    while(Key_Status()== Key_Pressed)
     {
	if(Key_Pressed_Number==0x4) 
        {
	   Key_Pressed_Number = 0x00;
        }
	else
        {
       	  LED_Display(++Key_Pressed_Number);
          TxMessage.Data[0] = Key_Pressed_Number;
          CAN_Transmit(CAN1, &TxMessage);
	  for(i=0;i<0xFFFF;i++);
          while(Key_Status()!= Key_NoPressed);
        }
     }
  }
}