CANoe CAPL LIN 睡眠唤醒

目录

LIN NM

start state:

action:

自定义睡眠唤醒

linSendWakeup

lingotosleep

配置自定义睡眠

​编辑


LIN NM

在CANoe中,软件已经集成好了LIN的网络管理失眠唤醒模块LIN NM

在这个模块中适用于所有LIN的网络管理

在这个模块中,定义了开始start state,和action

start state:

定义了两种上电状态,在oe工程启动时候的初始化状态

1.awake:一般选择awake

2.asleep:

action:

定义了网络行为

1.sleep:立即进入睡眠

2.wakeup:立即唤醒

注:如果使用此模块的时候,wakeup:唤醒帧默认为1ms左右的显性电平;sleep:默认使用睡眠:0x3C:00 FF FF FF FF FF FF FF

自定义睡眠唤醒

如果我们不能满足于系统给定的睡眠唤醒行为,这时候我们可以自定义睡眠唤醒行为

此时,我们需要用到CAPL中的睡眠唤醒函数

上图是所有的睡眠唤醒函数

linSendWakeup

lingotosleep

有三种函数允许我们调用,同时等效于linwake和linsetwakeuptimings这两个函数

on key 'b'
{ 
  if(1== a)
  {
    writeex(-3,1,"睡眠");
    linGotoSleep();
    a = 2;
  }
    
  else if(2 == a)
  {
    writeex(-3,1,"发送唤醒指令");
    linSendWakeup();
    a = 3;
  }
   else if(3 == a)
  {
    writeex(-3,1,"睡眠");
    linGotoSleep();
    a = 4;
  }
   else if(4 == a)
  {
    writeex(-3,1,"发送唤醒指令");
    linSendWakeup(150,3,2000);
    a = 5;
  }
}

配置自定义睡眠

如果我们使用使用睡眠不是0xFF的时候;这时候我们需要自己配置睡眠指令

on key 'b'
{ 
  if(1== a)
  {
    writeex(-3,1,"睡眠");
    linGotoSleep();
    a = 2;
  }
    
  else if(2 == a)
  {
    writeex(-3,1,"发送唤醒指令");
    linSendWakeup();
    a = 3;
  }
   else if(3 == a)
  {
    writeex(-3,1,"睡眠");
    master_msg.byte(0) = 0;
    master_msg.byte(1) = 0x1;
    master_msg.byte(2) = 0x2;
    master_msg.byte(3) = 0x3;
    master_msg.byte(4) = 0x4;
    master_msg.byte(5) = 0x5;
    master_msg.byte(6) = 0x6;
    master_msg.byte(7) = 0x7;
    master_msg.rtr = 0;
    output(master_msg);
    master_msg.rtr = 1;
    output(master_msg);
    a = 4;
  }
   else if(4 == a)
  {
    writeex(-3,1,"发送唤醒指令");
    linSendWakeup(150,3,2000);
    a = 5;
  }
}

内部唤醒

如果我们想用其他id唤醒网络的话,我们可以使用内部唤醒函数进行网络唤醒

testcase NewTestCase()
{
  byte i;
  linFrame 0x3 msg_wk;
  linFrame 0x3  tst_Frame;
  linChangeDlc(msg_wk.id, 5);
  tst_Frame.MsgChannel = 1;
  //tst_Frame.ID = 5;
  tst_Frame.DLC = 5;
  
  for (i=0; i < 8; ++i)
  {
    tst_Frame.byte(i) = 0xFF;
  }
  tst_Frame.RTR=0;
  output(tst_Frame);
  linSetRespCounter(msg_wk.id,-1);
  
  if(0 == linBusIsAwake())
  {
    write("11");
    
//    Util_ResetResponseData(msg_wk.id,5);
//    linSetRespCounter(msg_wk.id,-1);
    linChangeWakeupSettings(0, msg_wk.id);
    linSilentWakeup();
//    //Util_SendHeader(msg_wk.id);    
   
  }  
}
void Util_SendHeader (byte frameId)
{
  linFrame 0x0  tst_Frame;

  tst_Frame.MsgChannel = 1;
  tst_Frame.ID = frameId;
  tst_Frame.RTR=1;
  output(tst_Frame);
}
void Util_ResetResponseData(byte frameId, int length)
{
  long i;
  linFrame 0x0  tst_Frame;
  linChangeDlc(frameId, length);
  tst_Frame.MsgChannel = 1;
  tst_Frame.ID = frameId;
  tst_Frame.DLC = length;
  
  for (i=0; i < length; ++i)
  {
    tst_Frame.byte(i) = 0xFF;
  }
  tst_Frame.RTR=0;
  output(tst_Frame);
}

void MainTest ()
{
  NewTestCase();
}
 

testcase NewTestCase()
{
  linFrame 0x3 msg_wk;

  if(0 == linBusIsAwake())
  {
    write("11");
    
   // linETFSetDirtyFlag(0x10,1);
    linChangeDlc(msg_wk.id,5);
    msg_wk.byte(0) = 0xFF;
    msg_wk.byte(1) = 0xFF;
    msg_wk.byte(2) = 0xFF;
    msg_wk.byte(3) = 0xFF;
    msg_wk.byte(4) = 0xFF;
    msg_wk.byte(5) = 0xFF;
    msg_wk.byte(6) = 0xFF;
    msg_wk.byte(7) = 0xFF;
    msg_wk.rtr = 0;
    msg_wk.msgChannel = 1;
    msg_wk.id  = 3;
    msg_wk.dlc = 5;
    output(msg_wk);
    linSetRespCounter(msg_wk.id,-1);
    linChangeWakeupSettings(0, msg_wk.id);
    linSilentWakeup();

  }
  

  
  
  
}

void MainTest ()
{
  NewTestCase();
}

 

当总线需要外部触发源的时候可以使用linSendBitStream()

{

...

Util_BitStream_Initialize();
  Util_BitStream_AddDominantBits(Util_SyncBreakLength);   // sync break
  Util_BitStream_AddRecessiveBits(Util_SyncDelLength);    // sync delimiter
  Util_BitStream_AddLinByte(0x55);                        // sync byte field
   Util_BitStream_AddLinByte(linGetProtectedId(0X37));
  Util_BitStream_Transmit();
  testWaitForTimeout(20);(内部有6-20ms的延时,必须等待发送完成才可以发送其他报文)

...

}

void Util_BitStream_Initialize()
{
  dword i;

  Util_PositionInBitStream = 0;
  for (i = 0; i < elcount(Util_BitStream); ++i) 
  {
    Util_BitStream[i] = 0xff;
  }
  for (i = 0; i < Util_MaxVariableBits; ++i) 
  {
    Util_VarBitStreamBitLengths[i] = 0;
  }
}

dword Util_BitStream_AddDominantBits (dword bitsNum)
{
  dword i;

  for (i = bitsNum; i > 0;) // set dominant bits
  {
    if (i >= 8 && (Util_PositionInBitStream % 8) == 0)
    {
      Util_BitStream[Util_PositionInBitStream >> 3] = 0;  // set whole byte value
      i -= 8;
      Util_PositionInBitStream += 8;
    }
    else
    {
      Util_BitStream[Util_PositionInBitStream >> 3] &= ~(1 << (Util_PositionInBitStream & 0x7)); // set single bit
      --i;
      ++Util_PositionInBitStream;
    }
  }
  return bitsNum;
}

dword Util_BitStream_AddRecessiveBits (dword bitsNum)
{
  Util_PositionInBitStream += bitsNum; 

  return bitsNum;
}

dword Util_BitStream_AddLinByte (byte byteValue)
{
  dword i;

  Util_BitStream[Util_PositionInBitStream >> 3] &= ~(1 << (Util_PositionInBitStream & 0x7)); // set dominant start bit
  ++Util_PositionInBitStream;
  for (i = 0; i < 8; ++i)
  {
    if ((byteValue & (1 << i)) == 0) // dominant bits
    {
      Util_BitStream[Util_PositionInBitStream >> 3] &= ~(1 << (Util_PositionInBitStream & 0x7));
    }
    ++Util_PositionInBitStream;
  }
  ++Util_PositionInBitStream; // set recessive stop bit

  return 10;
}

void Util_BitStream_Transmit()
{
  Util_BitStream_Transmit(_True);
}

/************************************************************************************************** 
  Description   : This function transmits a sequence of previously defined bits 
                  without timeout prevention
  ------------------------------------------------------------------------------------------------ 
  Parameter     : activateTimeoutPrevention: state of timeout prevention
  ------------------------------------------------------------------------------------------------ 
  Return code   : None
**************************************************************************************************/
void Util_BitStream_Transmit(enum _Bool activateTimeoutPrevention)
{
  linSendBitStream(Util_BitStream, Util_PositionInBitStream, activateTimeoutPrevention);
}

其他函数具体参考help手册就行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanyikangkang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值