Ejemplo n.º 1
0
void  USB2CAN_open(u32 speed,u32 flags,CAN_TIMINGS *CanTimings)
{
    CAN_ENABLE();
    USB2CAN_set_speed(speed,CanTimings);

    switch( flags & 0x0003 )
    {
    case  CAN_MODE_NORMAL: // normal mode
        CAN_LeaveTestMode();
        break;
    case  CAN_MODE_SILENT: // silent mode
        CAN_EnterTestMode( CAN_TESTR_SILENT );
        break;
    case  CAN_MODE_LOOPBACK: // loopback mode
        CAN_EnterTestMode( CAN_TESTR_LBACK );
        break;
    case  CAN_MODE_SILENT_LOOPBACK:
        CAN_EnterTestMode( CAN_TESTR_LBACK | CAN_TESTR_SILENT );
        break;
    default:
        CAN_LeaveTestMode();
        break;
    }
    // DAR
    if( flags & 0x0004 )
        CAN->CR |= CAN_CR_DAR;  // disable auto retransmitions
    else
        CAN->CR &= ~CAN_CR_DAR; // enable auto retransmitions
}
Ejemplo n.º 2
0
/*******************************************************************************
* Function Name  : CAN_Init                                                  
* Description    : Initializes the CAN peripheral according to the specified 
*                  parameters in the CAN_InitStruct.                                            
* Input          : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
*                  contains the configuration information for the CAN peripheral. 
* Output         : None                                                      
* Return         : None                                                      
*******************************************************************************/
void CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
{
  CAN_EnterInitMode(CAN_CR_CCE | CAN_InitStruct->CAN_ConfigParameters);
  CAN_SetBitrate(CAN_InitStruct->CAN_Bitrate);
  CAN_LeaveInitMode();
  CAN_LeaveTestMode();
}
Ejemplo n.º 3
0
void CAN_Com_LoopBack_IRQ(void)
{
  /* initialize the interrupt controller */
  VIC_Config(CAN_ITLine, VIC_IRQ, Priority_1);

  /* initialize the CAN at a standard bitrate, interrupts enabled */
  CAN_InitStructure.CAN_ConfigParameters=CAN_CR_IE;
  CAN_InitStructure.CAN_Bitrate=CAN_BITRATE_100K;
  CAN_Init(&CAN_InitStructure);

  /* switch into Loopback+Silent mode (self-test) */
  CAN_EnterTestMode(CAN_TESTR_LBACK | CAN_TESTR_SILENT);

  /* configure the message objects */
  CAN_InvalidateAllMsgObj();
  CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_EXT_ID);
  CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_EXT_ID, 0, CAN_LAST_EXT_ID, TRUE);

  /* enable global interrupt */
  VIC_ITCmd(CAN_ITLine, ENABLE);

  /* Send the pre-defined frame */
  CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[2]);

  /* wait until end of transmission */
  CAN_WaitEndOfTx();

  /* reception and release are done in the interrupt handler */

  /* delay to add when the reception occurs */
  delay(0x7FF);

  /* Test Received Msg */
  if((RxCanMsg.IdType == CAN_EXT_ID)&&(RxCanMsg.Id == 0x12345678)&&(RxCanMsg.Dlc == 8)
    &&(RxCanMsg.Data[0]==0x10)&&(RxCanMsg.Data[1]==0x11)&&(RxCanMsg.Data[2]==0x12)&&(RxCanMsg.Data[3]==0x13)
    &&(RxCanMsg.Data[4]==0x14)&&(RxCanMsg.Data[5]==0x15)&&(RxCanMsg.Data[6]==0x16)&&(RxCanMsg.Data[7]==0x17)){
    /*Received Msg OK*/
    LED_ON(LD3);
  } else {
    /*Received Msg KO*/
    LED_ON(LD4);
  }


  /* switch back into Normal mode */
  CAN_LeaveTestMode();

  /* disable interrupts globally */
  VIC_ITCmd(CAN_ITLine, DISABLE);
}
Ejemplo n.º 4
0
void CAN_Com_LoopBack(void)
{
  /* initialize the CAN at a standard bitrate, interrupts disabled */
  CAN_InitStructure.CAN_ConfigParameters=0x0;
  CAN_InitStructure.CAN_Bitrate=CAN_BITRATE_1M;
  CAN_Init(&CAN_InitStructure);

  /* switch into Loopback+Silent mode (self-test) */
  CAN_EnterTestMode(CAN_TESTR_LBACK | CAN_TESTR_SILENT);

  /* configure the message objects */
  CAN_InvalidateAllMsgObj();
  CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID);
  CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);

  /* Send the pre-defined answer */
  CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[1]);

  /* wait until end of transmission */
  CAN_WaitEndOfTx();

  /* wait for reception of a data frame */
  while (!CAN_ReceiveMessage(CAN_RX_MSGOBJ, FALSE, &RxCanMsg))
  {
    /*Add Timer*/
  }


  /* Test Received Msg */
  if((RxCanMsg.IdType == CAN_STD_ID)&&(RxCanMsg.Id == 0x321)&&(RxCanMsg.Dlc == 4)
    &&(RxCanMsg.Data[0]==0xAA)&&(RxCanMsg.Data[1]==0x55)&&(RxCanMsg.Data[2]==0xAA)&&(RxCanMsg.Data[3]==0x55)){
    /*Received Msg OK*/
    LED_ON(LD2);
  } else {
    /*Received Msg KO*/
    LED_ON(LD4);
  }


  /* release the message objects */
  CAN_ReleaseTxMessage(CAN_TX_MSGOBJ);
  CAN_ReleaseRxMessage(CAN_RX_MSGOBJ);

  /* switch back into Normal mode */
  CAN_LeaveTestMode();

}
Ejemplo n.º 5
0
int can_mode(can_t *obj, CanMode mode)
{
    CAN_T *can_base = (CAN_T *) NU_MODBASE(obj->can);
    
    int success = 0;
    switch (mode)
    {
        case MODE_RESET:
            CAN_LeaveTestMode(can_base);
            success = 1;
            break;
        
        case MODE_NORMAL:
            CAN_EnterTestMode(can_base, CAN_TEST_BASIC_Msk);
            success = 1;
            break;
        
        case MODE_SILENT:
            CAN_EnterTestMode(can_base, CAN_TEST_SILENT_Msk);
            success = 1;
            break;
        
        case MODE_TEST_LOCAL:
        case MODE_TEST_GLOBAL:
            CAN_EnterTestMode(can_base, CAN_TEST_LBACK_Msk);
            success = 1;
            break;
        
        case MODE_TEST_SILENT:
            CAN_EnterTestMode(can_base, CAN_TEST_SILENT_Msk | CAN_TEST_LBACK_Msk);
            success = 1;
            break;
        
        default:
            success = 0;
            break;
        
    }
    
    
    return success;
}
Ejemplo n.º 6
0
/*----------------------------------------------------------------------------*/
void Test_BasicMode_Tx()
{
    int32_t delaycount;
    STR_CANMSG_T msg1;
    delaycount=1000;
    CAN_EnterTestMode(CAN_TESTR_BASIC);	                                       
    CAN_EnableInt(CAN_CON_IE|CAN_CON_SIE);

    /* Send Message No.1 */
    msg1.FrameType= DATA_FRAME;
    msg1.IdType   = CAN_STD_ID;
	msg1.Id       = 0x001;
    msg1.DLC      = 2;
	msg1.Data[0]  = 0x00;
    msg1.Data[1]  = 0x2;
    CAN_BasicSendMsg(&msg1);
    printf("Send STD_ID:0x1,Data[0,2]\n");
    SYS_SysTickDelay(delaycount);
      
    /* Send Message No.2 */
    msg1.FrameType= DATA_FRAME;
    msg1.IdType   = CAN_STD_ID;
	msg1.Id       = 0x1AC;
    msg1.DLC      = 8;
	msg1.Data[0]  = 0x11;
    msg1.Data[1]  = 0x12;
    msg1.Data[2]  = 0x13;
    msg1.Data[3]  = 0x14;
	msg1.Data[4]  = 0x15;
    msg1.Data[5]  = 0x16;
    msg1.Data[6]  = 0x17;
    msg1.Data[7]  = 0x18;
  
    CAN_BasicSendMsg(&msg1);
    printf("Send STD_ID:0x1AC,Data[11,12,13,14,15,16,17,18]\n");
    SYS_SysTickDelay(delaycount);

    /* Send Message No.3 */
    msg1.FrameType= DATA_FRAME;
    msg1.IdType   = CAN_STD_ID;
	msg1.Id       = 0x310;
    msg1.DLC      = 8;
	msg1.Data[0]  = 0x21;
    msg1.Data[1]  = 0x22;
    msg1.Data[2]  = 0x23;
    msg1.Data[3]  = 0x24;
	msg1.Data[4]  = 0x25;
    msg1.Data[5]  = 0x26;
    msg1.Data[6]  = 0x27;
    msg1.Data[7]  = 0x28;
    CAN_BasicSendMsg(&msg1);
    printf("Send STD_ID:0x310,Data[21,22,23,24,25,26,27,28]\n");
    SYS_SysTickDelay(delaycount);

    /* Send Message No.4 */
    msg1.FrameType= DATA_FRAME;    
    msg1.IdType   = CAN_EXT_ID;
    msg1.Id       = 0x3377;
    msg1.DLC      = 8;
	msg1.Data[0]  = 0x31;
    msg1.Data[1]  = 0x32;
    msg1.Data[2]  = 0x33;
    msg1.Data[3]  = 0x34;
	msg1.Data[4]  = 0x35;
    msg1.Data[5]  = 0x36;
    msg1.Data[6]  = 0x37;
    msg1.Data[7]  = 0x38;   
    CAN_BasicSendMsg(&msg1);
    printf("Send EXT_ID:0x3377,Data[31,32,33,34,35,36,37,38]\n");
    SYS_SysTickDelay(delaycount);

    /* Send Message No.5 */
    msg1.FrameType= DATA_FRAME;    
    msg1.IdType   = CAN_EXT_ID;
    msg1.Id       = 0x7755;
    msg1.DLC      = 8;
	msg1.Data[0]  = 0x41;
    msg1.Data[1]  = 0x42;
    msg1.Data[2]  = 0x43;
    msg1.Data[3]  = 0x44;
	msg1.Data[4]  = 0x45;
    msg1.Data[5]  = 0x46;
    msg1.Data[6]  = 0x47;
    msg1.Data[7]  = 0x48;  
    CAN_BasicSendMsg(&msg1);
    printf("Send EXT_ID:0x7755,Data[41,42,43,44,45,46,47,48]\n");
    SYS_SysTickDelay(delaycount);
    CAN_LeaveTestMode();

}