Esempio n. 1
0
void can_flush(void)
{
	int busy;
	time_t deadline = time_get(10); //max wait 10 ms
	do {
		busy = 0;
		busy += (CAN_TransmitStatus(CAN1, 0) == CANTXPENDING);
		busy += (CAN_TransmitStatus(CAN1, 1) == CANTXPENDING);
		busy += (CAN_TransmitStatus(CAN1, 2) == CANTXPENDING);
		if(!busy)
			break;
	} while(time_left(deadline) > 0);

	CAN_CancelTransmit(CAN1, 0);
	CAN_CancelTransmit(CAN1, 1);
	CAN_CancelTransmit(CAN1, 2);
	CAN_FIFORelease(CAN1, CAN_FIFO0);
#ifndef CONFIG_CAN_ENHANCED
	CAN_FIFORelease(CAN1, CAN_FIFO1);
#endif

#if ENABLE_CAN_INT
	circle_header = 0;
	circle_tailer = 0;
	circle_number = 0;
#endif
}
Esempio n. 2
0
void _CAN_SendMsg(uint8 *Buf,uint8 Length)
{
	static uint8 TestValue;
	u8 TransmitMailbox=0,i=0;
	CanTxMsg  TxMessage;
	CanRxMsg  RxMessage;
	TxMessage.StdId=0x00;//接收板地址
	TxMessage.RTR=CAN_RTR_DATA;//发送数据帧
	TxMessage.IDE=CAN_ID_STD;
	TxMessage.DLC=2;//发送数据的长度
	TxMessage.Data[0]=TestValue;
	TxMessage.Data[1]=0xFE;
	TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
	while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xff))
	{
	i++;
	}
	if(i==0xff)DeBug("Can Send TimeOut2",Infor_Infor);
	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
	if(RxMessage.Data[0]==TestValue%0xff)
	{
		DeBug("Can_PoolBack_Ok TestValue=%d",TestValue,Infor_Infor);
	}
	TestValue++;
}
Esempio n. 3
0
TestStatus CAN_TX(void)
{
	CanTxMsg TxMessage;
	u8 TransmitMailbox;
	u32 iCANTX = 0,iDelay=0;

	/* transmit */
	TxMessage.StdId=0x12;
	TxMessage.RTR=CAN_RTR_DATA;
	TxMessage.IDE=CAN_ID_STD; //BD_EXT.CAN_1_ID;
	TxMessage.DLC=8;
	TxMessage.Data[0]=0xCA;
	TxMessage.Data[1]=0xAE;
	TxMessage.Data[2]=0xAA;
	TxMessage.Data[3]=0xBB;
	TxMessage.Data[4]=0xCC;
	TxMessage.Data[5]=0xDD;
	TxMessage.Data[6]=0xEE;
	TxMessage.Data[7]=0xFF;
	
	TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
	iCANTX = 0;
	while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK) && (iCANTX!= 0xFF))
	{
	  iCANTX++;
	  for(iDelay=0;iDelay<500;iDelay++);
	}
	if(iCANTX==0xFF)
	  return FAILED;
	else
	  return PASSED;
	  
}
Esempio n. 4
0
File: CAN1.c Progetto: haosir7/----
void CAN1_Send(uint32_t ID,u8 *Data)       
{  
	u8 TransmitMailbox=0,i=0;  
	CanTxMsg TxMessage;  
	
	TxMessage.StdId = ID;            
	TxMessage.ExtId = 0x0000;
	TxMessage.IDE = CAN_ID_STD; 
	TxMessage.RTR = CAN_RTR_DATA;
	TxMessage.DLC = 8; 
	for(i=0;i<8;i++)
	{
		TxMessage.Data[i] = *Data++; 
	}
	
	TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
	
	while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xFF))
	{
		i++;
	}
	i=0;
	while((CAN_MessagePending(CAN1,CAN_FIFO0)<1)&&(i!=0xFF))
	{
		i++;
	}
}
Esempio n. 5
0
//发送CAN1命令
uint8_t DST_CAN_1::Send_CAN1_CMD(int16_t current_201,int16_t current_202,int16_t current_203)
{
	uint8_t mbox;					//发送邮箱
	uint16_t i= 0;
	CanTxMsg tx_message;	//CAN Tx message structure definition
	
	tx_message.StdId = 0x200;					//标准ID
	tx_message.IDE = CAN_Id_Standard;	//标准帧
	tx_message.RTR = CAN_RTR_Data;		//数据帧
	tx_message.DLC = 0x08;						//要发送的数据长度
	
	/*********************数据*********************/
	tx_message.Data[0] = (unsigned char)(current_201 >> 8);
	tx_message.Data[1] = (unsigned char)current_201;
	tx_message.Data[2] = (unsigned char)(current_202 >> 8);
	tx_message.Data[3] = (unsigned char)current_202;
	tx_message.Data[4] = (unsigned char)(current_203 >> 8);
	tx_message.Data[5] = (unsigned char)current_203;
	tx_message.Data[6] = 0x00;
	tx_message.Data[7] = 0x00;
	
	mbox = CAN_Transmit(CAN1,&tx_message);
  while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++;	//等待发送结束
  if(i>=0XFFF)return _DEFEATED;	//发送失败
	return _SUCCEED;	//发送成功
}
Esempio n. 6
0
/**
 * If a transmission is ongoing, the next message can will be delayed until that transmission
 * is done.
 */
int comm_is_tx_busy(void) {
	int ok = 1;

#if COMM_USE_CAN
	if (CAN_TransmitStatus(CAN1, 0) == CAN_TxStatus_Pending ||
			CAN_TransmitStatus(CAN1, 1) == CAN_TxStatus_Pending ||
			CAN_TransmitStatus(CAN1, 2) == CAN_TxStatus_Pending) {
		ok = 0;
	}
#else
	int i;
	for (i = 0;i < USARTINTERFACE_TX_BUFFER_NUM;i++) {
		if (!usartinterfaceIsTxBufferFree(i)) {
			ok = 0;
			break;
		}
	}
#endif

	return !ok;
}
int CAN_device_tx_msg(uint8_t channel, CAN_msg * msg, unsigned int timeoutMs)
{
        CanTxMsg TxMessage;

        /* Transmit Structure preparation */
        if (msg->isExtendedAddress) {
                TxMessage.ExtId = msg->addressValue;
                TxMessage.IDE = CAN_ID_EXT;
        } else {
                TxMessage.StdId = msg->addressValue;
                TxMessage.IDE = CAN_ID_STD;
        }

        TxMessage.RTR = CAN_RTR_DATA;
        TxMessage.DLC = msg->dataLength;
        memcpy(TxMessage.Data, msg->data, msg->dataLength);

        CAN_TypeDef* chan = channel == 0 ? CAN1 : CAN2;
        const uint8_t mailbox = CAN_Transmit(chan, &TxMessage);

        /*
         * Then they don't want to wait.  Ok.  Let caller know if they
         * got a mailbox then.  If not, message was unable to be sent.
         */
        if (0 == timeoutMs)
                return mailbox != CAN_TxStatus_NoMailBox;

        /* Using ticks avoids a race-condition */
        size_t ticks = getCurrentTicks();
        const size_t trigger = ticks + msToTicks(timeoutMs);
        uint8_t status = CAN_TxStatus_Failed;

        while(ticks <= trigger) {
                status = CAN_TransmitStatus(chan, mailbox);
                if (CAN_TxStatus_Pending != status)
                        break;

                /*
                 * Not using yield here as it will cause lower priority tasks
                 * to starve.  Yield only allows tasks of equal or greater
                 * priority to run.
                 */
                delayTicks(1);

                ticks = getCurrentTicks();
        }

        if (CAN_TxStatus_Pending == status)
                CAN_CancelTransmit(chan, mailbox);

        return status == CAN_TxStatus_Ok;
}
Esempio n. 8
0
/**
 * @brief  Transmit
 * @param  None
 * @retval The number of unprocessed messages (uint8_t).
 */
void CAN_transmitByte(uint16_t StdId, uint8_t data){
	/* Toggle status LED */
	GPIOE->ODR ^= CAN_TX_LED << 8;

	/* Configure the message to be transmitted. */
	TxMsg.StdId = StdId;
	TxMsg.Data[0] = data;
	TxMsg.DLC = 1;

	/* Put message in Tx Mailbox and store the mailbox number. */
	TransmitMailbox = CAN_Transmit(CAN1, &TxMsg);

	/* Wait on Transmit */
	while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CAN_TxStatus_Ok));

}
Esempio n. 9
0
u8 CAN_SendMsg(CanMessage_t * msg) 
{
  CanTxMsg TxMessage;
  u8 TransmitMailbox, i;

  if (0 == extId)
  {
    TxMessage.StdId = msg->id;
    TxMessage.ExtId = 0;
    TxMessage.IDE = CAN_ID_STD;
  }
  else
  {
    TxMessage.StdId = 0;
    TxMessage.ExtId = msg->id;
    TxMessage.IDE = CAN_ID_EXT;
  }

  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.DLC = msg->len;

  memmove(TxMessage.Data, msg->payload, 8);

  TransmitMailbox=CAN_Transmit(&TxMessage);
  if (CAN_NO_MB == TransmitMailbox) {
    return 0;
  }

  i = 0;
  while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && (i != 0xFF))
  {
    i++;
    delay(1000);
  }

  if (i != 0xff)
  {
#ifdef DEBUG_LOG_CAN_FRAME
    DebugCanFrame(1, extId, msg);
#endif
    return 255;
  }
  else
  {
    return 0; //can transmit fails
  }
}
Esempio n. 10
0
/**
  * @brief  发送一帧CAN数据
  * @param  CANx CAN通道号
  * @param  TxMessage CAN消息指针
  * @retval None
  */
u8 CAN1_SendOneFrame(CanTxMsg *TxMessage)
{
	u8 TransmitMailbox;   
	u32	TimeOut = 0;
	
	TransmitMailbox = CAN_Transmit(CAN1, TxMessage);
	while (CAN_TransmitStatus(CAN1, TransmitMailbox) != CAN_TxStatus_Ok)
	{
		TimeOut++;
		if (TimeOut > 10000000)
		{
			return 1;
		}
	}
	
	return 0;
}
Esempio n. 11
0
void SendCanMessage(uint32_t id, uint16_t data)
{
    CanTxMsg TxMessage;
    uint8_t mailbox;

    TxMessage.StdId = id;
    TxMessage.ExtId = id;
    TxMessage.RTR=CAN_RTR_DATA;
    TxMessage.IDE=CAN_ID_STD;
    TxMessage.DLC = 4;

    *((uint16_t*)(&TxMessage.Data[0])) = data;

    mailbox = CAN_Transmit(CAN1,&TxMessage);
    while (CAN_TransmitStatus(CAN1, mailbox) != CAN_TxStatus_Ok) {
        ;
    };

}
Esempio n. 12
0
void can_tx(uint16_t addr,uint8_t dlc,uint8_t* data)
{
  CanTxMsg TxMessage;
  static volatile uint8_t status,i;
  uint8_t TransmitMailbox = 0;

  /* transmit */
  TxMessage.StdId=addr;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.IDE=CAN_ID_STD;
  TxMessage.DLC=dlc;
  memcpy(TxMessage.Data,data,dlc);

  TransmitMailbox=CAN_Transmit(CAN1, &TxMessage);
  
  for(;;)
  {
    status=CAN_TransmitStatus(CAN1, TransmitMailbox);
    if(status!=CANTXPENDING)
    {break;}
  }
}
Esempio n. 13
0
/*************************************************************
Func:  CAN 发送消息
Date:  2014-8-26
Note:  
*************************************************************/
void CAN_SendMsg(void *Msg,uint8 Length)
{
	static uint8 TestValue='0';
	uint16 TransmitMailbox=0,i=0;
	CanTxMsg  TxMessage;
	TxMessage.ExtId=0x11<<13;//接收板地址
	TxMessage.RTR=CAN_RTR_DATA;//发送数据帧
	TxMessage.IDE=CAN_ID_EXT;
	TxMessage.DLC=Length+1;//发送数据的长度
	BufferCoppy(Msg,&TxMessage.Data[0],Length);
	TxMessage.Data[Length]=TestValue;
	TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
	while((CAN_TransmitStatus(CAN1,TransmitMailbox)!=CANTXOK)&&(i!=0xff)) 
	{
	i++;
		Tos_TaskDelay(1);
	}
	if(i==0xff)DeBug("Can Send TimeOut1",Infor_Infor);
	else DeBug("Can Send Succeed1",Infor_Infor);
	if(TestValue<'9')TestValue++;
	else TestValue='0';
}
Esempio n. 14
0
//can发送一组数据(固定格式:ID为0X12,标准帧,数据帧)
//len:数据长度(最大为8)
//msg:数据指针,最大为8个字节.
//返回值:0,成功;
//       其他,失败;
u8 Can_Send_Msg(u8* msg, u8 len)
{
    u8 mbox;
    u16 i = 0;
    CanTxMsg TxMessage;
    TxMessage.StdId = 0x12;    // 标准标识符为0
    //    TxMessage.ExtId = 0x12;    // 设置扩展标示符(29位)
    //IDE位为标识符选择位(0,使用标准标识符;1,使用扩展标识符)
    TxMessage.IDE = 0;         // 使用扩展标识符
    //RTR位用于标识是否是远程帧(0,数据帧;1,远程帧)
    TxMessage.RTR = 0;         // 消息类型为数据帧,一帧8位
    //DLC段为数据长度表示段,高位在前,DLC段有效值为0~8,但是接收方接收到9~15的时候并不认为是错误
    TxMessage.DLC = len;       // 发送两帧信息
    for (i = 0; i < len; i++)
        TxMessage.Data[i] = msg[i];// 第一帧信息
    mbox = CAN_Transmit(CAN1, &TxMessage);
    i = 0;
    while ((CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed) && (i < 0XFFF))i++; //等待发送结束
    if (i >= 0XFFF)return 1;
    return 0;

}
Esempio n. 15
0
/*
 * 函数名:CAN_Polling
 * 描述  :配置 CAN 的工作模式为 回环模式
 * 输入  :无
 * 输出  : -PASSED   成功
 *         -FAILED   失败
 * 调用  :内部调用
 */
TestStatus CAN_Polling(void)
{
	CAN_InitTypeDef        CAN_InitStructure;
	CAN_FilterInitTypeDef  CAN_FilterInitStructure;
	CanTxMsg TxMessage;
	CanRxMsg RxMessage;
	uint32_t i = 0;
	uint8_t TransmitMailbox = 0;
	
	/* CAN register init */
	CAN_DeInit(CAN1);
	CAN_StructInit(&CAN_InitStructure);
	
	/* CAN cell init */
	CAN_InitStructure.CAN_TTCM=DISABLE;						 // 时间触发通信禁止
	CAN_InitStructure.CAN_ABOM=DISABLE;						 // 离线退出是在中断置位清0后退出
	CAN_InitStructure.CAN_AWUM=DISABLE;						 // 自动唤醒模式:清零sleep
	CAN_InitStructure.CAN_NART=DISABLE;						 // 自动重新传送豹纹,知道发送成功
	CAN_InitStructure.CAN_RFLM=DISABLE;						 // FIFO没有锁定,新报文覆盖旧报文  
	CAN_InitStructure.CAN_TXFP=DISABLE;						 // 发送报文优先级确定:标志符
	CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;	 // 回环模式
	CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;				 // 1tq、BS1、BS2的值跟波特率有关
	CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
	CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
	CAN_InitStructure.CAN_Prescaler=5;             // 分频系数为5
	CAN_Init(CAN1, &CAN_InitStructure);            // 初始化CAN1 
	
	/* 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);
	
	/* transmit */
	TxMessage.StdId=0x11;				// 设定标准标识符(11位,扩展的为29位)
	TxMessage.RTR=CAN_RTR_DATA;	// 传输消息的帧类型为数据帧(还有远程帧)
	TxMessage.IDE=CAN_ID_STD;		// 消息标志符实验标准标识符
	TxMessage.DLC=2;					  // 发送两帧,一帧8位
	TxMessage.Data[0]=0xCA;			// 第一帧数据
	TxMessage.Data[1]=0xFE;			// 第二帧数据
	
	TransmitMailbox=CAN_Transmit(CAN1, &TxMessage);
	i = 0;
	// 用于检查消息传输是否正常
	while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF))
	{
		i++;
	}
	
	i = 0;
	// 检查返回的挂号的信息数目
	while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF))
	{
		i++;
	}
	
	/* receive */
	RxMessage.StdId=0x00;
	RxMessage.IDE=CAN_ID_STD;
	RxMessage.DLC=0;
	RxMessage.Data[0]=0x00;
	RxMessage.Data[1]=0x00;
	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
	
	
	if (RxMessage.StdId!=0x11)
	{
		return FAILED;  
	}
	
	if (RxMessage.IDE!=CAN_ID_STD)
	{
		return FAILED;
	}
	
	if (RxMessage.DLC!=2)
	{
		return FAILED;  
	}
	
	/* 判断发送的信息和接收的信息是否相等 */
	if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
	{
		return FAILED;
	}
	
	//printf("receive data:0X%X,0X%X",RxMessage.Data[0], RxMessage.Data[1]);  
	
	return PASSED; /* Test Passed */
}
Esempio n. 16
0
//送信
int main(void)
{
	char str[70] = {0};
	int a;

	init();

	can_tx_flame.StdId 		= 0x500;//ID 11bit 0〜0x7FF
	can_tx_flame.ExtId		= 0x0;	//拡張フレームID 29bit 0〜0x1FFFFFFF
	can_tx_flame.IDE 		= 0;	//拡張フレームIDを使う場合1
	can_tx_flame.RTR		= 0;	//データフレーム:0 リモートフレーム:1
	can_tx_flame.DLC		= 1;	//送信するデータフィールドのバイト数 0~8
	can_tx_flame.Data[0]	= start;	//送信するデータフィールド


	CAN_Transmit_List_Stack(0, &can_tx_flame);//0x00AというIDのフレームをリストに追加
	/*
	can_tx_flame.StdId 		= 0x00B;
	CAN_Transmit_List_Stack(0, &can_tx_flame);//0x00BというIDのフレームをリストに追加
	can_tx_flame.StdId 		= 0x00C;
	CAN_Transmit_List_Stack(0, &can_tx_flame);//0x00CというIDのフレームをリストに追加
	can_tx_flame.StdId 		= 0x00D;
	CAN_Transmit_List_Stack(0, &can_tx_flame);//0x00DというIDのフレームをリストに追加
	*/
	//以降割込みによって0x00Aから0x00Dのフレームが繰り返し送信される


	while(ticker < 500);

	GPIOD->BSRRL = GPIO_Pin_12;

	CAN_Transmit(CAN1, &can_tx_flame);//送信

    while(1)
    {

    	while(ticker > 300){
    		ticker = 0;

    		if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == SET){
    			GPIOD->BSRRH = GPIO_Pin_13;
#ifndef USE_INTERRUPT_CAN_TX
    			CAN_Transmit(CAN1, &can_tx_flame);//送信

    			while(CANTXOK != CAN_TransmitStatus(CAN1,0));
#endif
    		}else{
    			GPIOD->BSRRL = GPIO_Pin_13;
    		}

    		sprintf(str,"StId:0x%X \n\rIDE:%d \n\rRTR:%d \n\rDLC:%X \n\rData[0]:0x%X \n\rRerror:%d \n\rTerror:%d \n\rStart:%X\r\n"
    											,can_tx_flame.StdId
    											,can_tx_flame.IDE
    											,can_tx_flame.RTR
    											,can_tx_flame.DLC
    											,can_tx_flame.Data[0]
    											,CAN_GetReceiveErrorCounter(CAN1)
    											,CAN_GetLSBTransmitErrorCounter(CAN1)
    											,psbutton.start.value);

    		transmit_uart3_s(str);

    	}
    }
}
Esempio n. 17
0
/**
  * @brief  Configures the CAN, transmit and receive by polling
  * @param  None
  * @retval PASSED if the reception is well done, FAILED in other case
  */
TestStatus CAN_Polling(void)
{
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  CanTxMsg TxMessage;
  CanRxMsg RxMessage;
  uint32_t i = 0;
  uint8_t TransmitMailbox = 0;

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

  CAN_StructInit(&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_InitStructure.CAN_RFLM = DISABLE;
  CAN_InitStructure.CAN_TXFP = DISABLE;
  CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;

  /* CAN Baudrate = 125kbps (CAN clocked at 36 MHz) */
  CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler = 16;
  CAN_Init(CAN, &CAN_InitStructure);

  /* 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);

  /* transmit */
  TxMessage.StdId = 0x11;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 2;
  TxMessage.Data[0] = 0xCA;
  TxMessage.Data[1] = 0xFE;

  TransmitMailbox = CAN_Transmit(CAN, &TxMessage);
  i = 0;
  while((CAN_TransmitStatus(CAN, TransmitMailbox)  !=  CANTXOK) && (i  !=  0xFFFF))
  {
    i++;
  }

  i = 0;
  while((CAN_MessagePending(CAN, CAN_FIFO0) < 1) && (i  !=  0xFFFF))
  {
    i++;
  }

  /* receive */
  RxMessage.StdId = 0x00;
  RxMessage.IDE = CAN_ID_STD;
  RxMessage.DLC = 0;
  RxMessage.Data[0] = 0x00;
  RxMessage.Data[1] = 0x00;
  CAN_Receive(CAN, CAN_FIFO0, &RxMessage);

  if (RxMessage.StdId != 0x11)
  {
    return FAILED;  
  }

  if (RxMessage.IDE != CAN_ID_STD)
  {
    return FAILED;
  }

  if (RxMessage.DLC != 2)
  {
    return FAILED;  
  }

  if ((RxMessage.Data[0]<<8|RxMessage.Data[1]) != 0xCAFE)
  {
    return FAILED;
  }
  
  return PASSED; /* Test Passed */
}
Esempio n. 18
0
void send_can1(CanTxMsg *data){
	CAN_Transmit(CAN1, data);
	while(CANTXOK != CAN_TransmitStatus(CAN1,0));
}
Esempio n. 19
0
TestStatus CAN_Polling(void)
{
	CanTxMsg TxMessage;
	CanRxMsg RxMessage;
	uint32_t i = 0;
	uint8_t TransmitMailbox = 0;
	/* transmit */
	TxMessage.StdId=0x11;				// 设定标准标识符(11位,扩展的为29位)
	TxMessage.RTR=CAN_RTR_DATA;	// 传输消息的帧类型为数据帧(还有远程帧)
	TxMessage.IDE=CAN_ID_STD;		// 消息标志符实验标准标识符
	TxMessage.DLC=2;					  // 发送两帧,一帧8位
	TxMessage.Data[0]=0xCA;			// 第一帧数据
	TxMessage.Data[1]=0xFE;			// 第二帧数据
	
	TransmitMailbox=CAN_Transmit(CAN1, &TxMessage);
	i = 0;
	// 用于检查消息传输是否正常
	while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF))
	{
		i++;
	}
	
	i = 0;
	// 检查返回的挂号的信息数目
	while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF))
	{
		i++;
	}
	
	/* receive */
	RxMessage.StdId=0x00;
	RxMessage.IDE=CAN_ID_STD;
	RxMessage.DLC=0;
	RxMessage.Data[0]=0x00;
	RxMessage.Data[1]=0x00;
	CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
	
	
	if (RxMessage.StdId!=0x11)
	{
		return FAILED;  
	}
	
	if (RxMessage.IDE!=CAN_ID_STD)
	{
		return FAILED;
	}
	
	if (RxMessage.DLC!=2)
	{
		return FAILED;  
	}
	
	/* 判断发送的信息和接收的信息是否相等 */
	if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
	{
		return FAILED;
	}
	
	//printf("receive data:0X%X,0X%X",RxMessage.Data[0], RxMessage.Data[1]);  
	
	return PASSED; /* Test Passed */
}
/*******************************************************************************
* Function Name  : SysTick_Handler
* Description    : This function handles SysTick Handler.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SysTick_Handler(void)
{ if(bDeviceState==CONFIGURED){  

      /********************************************************************************
      ********************* Message received from CAN BUS (ROV) ***********************
      *********************************************************************************/  
      if(CANpacket_receive==1 && Bridge.CAN_State != CAN_MSGINQUEUE && Bridge.CAN_State != CAN_PROC_MSGINQUEUE)
      { 
        if(CAN_SaveMsg(&Bridge.RxMessage1,&Bridge.COM_Lastmessage.CDC_ToPC_Buffer))
        {
          Bridge.CAN_State=ChangeState_level_Busier(Bridge.CAN_State);
          STM_EVAL_LEDToggle(LED9);
        }
        else 
        {
          Bridge.CAN_State=ChangeState_level_Freer(Bridge.CAN_State);
          STM_EVAL_LEDToggle(LED10);
        }
         /*Send CAN message if queue isn't empty */      
        if (Bridge.CAN_State == CAN_MSGINQUEUE || Bridge.CAN_State == CAN_PROC_MSGINQUEUE)
        {
           COM_SendMsg(&Bridge.COM_Lastmessage.CDC_ToPC_Buffer);
           Bridge.COM_Acknowledgement.State=ACK_WAITING;
           Bridge.COM_Acknowledgement.COM_ACK=NO_ACK;
        }
       CANpacket_receive=0; 
      }
      
      /********************************************************************************
      ********************* Message received from USB port (pc) ***********************
      *********************************************************************************/
        if( Bridge.COM_Status==COM_ReadyToProcess)
        { 
        
          if(Bridge.COM_State == COM_PROCESSING)
          {
            COM_SaveMsg(&Bridge.Reception_Buffer,&Bridge.COM_Lastmessage.CDC_FromPC_Buffer);
            Bridge.COM_State=ChangeState_level_Busier(Bridge.COM_State);
            Bridge.COM_Status=COM_WaitingforHeader;
          }
          /*Send serial message if queue isn't empty */
          if ((Bridge.COM_State == COM_MSGINQUEUE || Bridge.COM_State == COM_PROC_MSGINQUEUE)  ) 
            {  CANTX_Counter=0;           
              // make the function to send CAN messages through serial          
              CAN_SendMsg(&Bridge.COM_Lastmessage.CDC_FromPC_Buffer);
              CAN_mbox=CAN_Transmit(CAN1,&Bridge.TxMessage1);
              while(((Bridge.CAN_Err.ErrorData=CAN_TransmitStatus(CAN1, CAN_mbox))  !=  CAN_TxStatus_Ok) && (CANTX_Counter !=  0xFFFF))
              {
                CANTX_Counter++;
              }
              if(CANTX_Counter!=0xFFFF)
              {
                if (Bridge.COM_State == COM_PROC_MSGINQUEUE)
                    COM_SaveMsg(&Bridge.Reception_Buffer,&Bridge.COM_Lastmessage.CDC_FromPC_Buffer);
                  Bridge.COM_State=ChangeState_level_Freer(Bridge.COM_State);
              }
               else
                  Bridge.CAN_Err.ErrorId=CAN_SENDERROR;
            }               
      }    
      
     
  
      /********************************************************************************
      ********************* Error Message on the USB port (pc) ***********************
      *********************************************************************************/  
    if (Bridge.COM_Err.ErrorId)
    {     
      STM_EVAL_LEDToggle(LED5);
      //COM_SendSpecial(Bridge.COM_Err.ErrorId); to be replaced by a table to send
      Bridge.COM_Err.ErrorId=0;    
    }
    
      /********************************************************************************
      ********************* Error Message (ROV) ***********************
      *********************************************************************************/
    if (Bridge.CAN_Err.ErrorId)
    { 
      STM_EVAL_LEDToggle(LED8);
      //COM_SendSpecial(Bridge.CAN_Err.ErrorId);
      Bridge.CAN_Err.ErrorId=0;
    }
    
      /********************************************************************************
      ********************* Acknowledgement Message (PC) ***********************
      *********************************************************************************/
     if (Bridge.COM_Acknowledgement.State==ACK_RECEIVED)
    {  
      if(Bridge.COM_Acknowledgement.COM_ACK==ACK)
      {
         Bridge.COM_Acknowledgement.State=ACK_IDLE;
         Bridge.CAN_State=ChangeState_level_Freer(Bridge.CAN_State);
      }
       else
       {
         Bridge.COM_Acknowledgement.Nb_Tries++;
         COM_SendMsg(&Bridge.COM_Lastmessage.CDC_ToPC_Buffer);
         Bridge.COM_Acknowledgement.State=ACK_WAITING;
       }
    }   
     else if(Bridge.COM_Acknowledgement.State==ACK_WAITING)
     {
       Bridge.COM_Acknowledgement.Nb_Check++;
       STM_EVAL_LEDToggle(LED6);
       COM_SendMsg(&Bridge.COM_Lastmessage.CDC_ToPC_Buffer);
     }
  }
  STM_EVAL_LEDOff(LED7); 
}