⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can.sgml

📁 开放源码实时操作系统源码.
💻 SGML
📖 第 1 页 / 共 5 页
字号:
</function>). 
In non-blocking mode with timeouts, if there is no space in buffer for the 
CAN message, the driver waits a certain amount of time (the timeout time) 
for space in the buffer. If there is still no space in buffer after 
expiration of the timeout time, <varname>-EINTR</varname> is returned and 
the caller must try again.
</PARA>
    
<PARA>
If a message was sucessfully sent, the function returns <varname>ENOERR</varname>.
</PARA>
</SECTION>
    
<SECTION>  
<TITLE>CAN Messages</TITLE>
    
<PARA>
The CAN driver uses <structname>cyg_can_message</structname> structures to 
pass messages between the application and the CAN driver. The type 
cyg_can_message provides a device independent type of CAN message. 
Before calling the write function this message should be setup properly.
</PARA>

<PROGRAMLISTING>
typedef struct can_message
{
    cyg_uint32         id;
    cyg_uint8          data[8];
    cyg_can_id_type    ext;
    cyg_can_frame_type rtr;
    cyg_uint8          dlc;
} cyg_can_message;
</PROGRAMLISTING>

<PARA>
The structure contains the following fields:
</PARA>
 
<variablelist>
   <varlistentry>
       <term><type>cyg_uint32</type> <varname>id</varname></term>
           <listitem><para>
Message ID. This is the ID to be transmitted with the message, or the 
ID received. If the <structfield>ext</structfield> field is set, then 
this will contain a 29 bit ID, otherwise it will contain an 11 bit ID.
          </para></listitem>
   </varlistentry>
   <varlistentry>
       <term><type>cyg_uint32</type> <varname>data</varname></term>
           <listitem><para>
Message data. Only the first <structfield>dlc</structfield> bytes of 
data are valid. If the <structfield>rtr</structfield> field is set, 
then the contents of this field are ignored.
          </para></listitem>
   </varlistentry>
   <varlistentry>
       <term><type>cyg_can_id_type</type> <varname>ext</varname></term>
           <listitem><para>
Extended ID. If this field is <varname>CYGNUM_CAN_ID_EXT</varname> then the 
<structname>id</structname> field contains a 29 bit extended ID. If it 
contains <varname>CYGNUM_CAN_ID_STD</varname> then the ID is 11 bits.
          </para></listitem>
   </varlistentry>
   <varlistentry>
       <term><type>cyg_can_frame_type</type> <varname>rtr</varname></term>
           <listitem><para>
Remote Transmission Request. If this field contains 
<varname>CYGNUM_CAN_FRAME_RTR</varname> then the RTR bit on the message 
will be set and the <structfield>data</structfield> field will be ignored. 
If the field contains <varname>CYGNUM_CAN_FRAME_DATA</varname> then a 
normal data frame will be send.
          </para></listitem>
   </varlistentry>
   <varlistentry>
       <term><type>cyg_uint8</type> <varname>dlc</varname></term>
           <listitem><para>
The length of the data carried in the message. This can range from 
zero to 8. In a message with the <structfield>rtr</structfield> field set, 
this indicates the size of data being requested.
          </para></listitem>
   </varlistentry>
</variablelist>

<PARA>
Example code for sending one single CAN message:
</PARA>

<PROGRAMLISTING>
cyg_can_message tx_msg;
cyg_uint32      len;
Cyg_ErrNo       ret;

tx_msg.id  = 0x100;
tx_msg.ext = CYGNUM_CAN_ID_EXT;
tx_msg.rtr = CYGNUM_CAN_FRAME_DATA;
tx_msg.dlc = 1;
tx_msg.data[0] = 0xF1;

len = sizeof(tx_msg);
ret = cyg_io_write(hDrvCAN, &amp;tx_msg, &amp;len);
</PROGRAMLISTING>       
</SECTION><!-- can-cyg_can_message -->

<SECTION>
<TITLE>cyg_io_read</TITLE> 
 
<PROGRAMLISTING>
cyg_io_read(handle, buf, len)
</PROGRAMLISTING>

<PARA>
To receive a message the application calls <function>cyg_can_recv()</function>.
This function receives one single event from a device. The desired size 
of data to receive is contained in <parameter>*len</parameter> and the
actual size obtained will be returned in the same place. A pointer to 
a <type>cyg_can_event</type> is contained in <parameter>*buf</parameter>. 
No manipulation of the data is performed before being transferred. 
Again, this buffering is completely configurable. On return, 
<parameter>*len</parameter> contains <function>sizeof(cyg_can_event)</function>.
</PARA>
   
<PARA>    
It is possible to configure the read call to be blocking (default) or 
non-blocking. Non-blocking mode requires both the configuration option 
<varname>CYGOPT_IO_CAN_SUPPORT_NONBLOCKING</varname> to be enabled, 
and the specific device to be set to non-blocking mode for reads 
(see <function>cyg_io_set_config()</function>). In blocking mode, 
the call will not return until one single CAN event has been read. 
In non-blocking mode, if there is no CAN event in buffer, the call 
returns immediately with <varname>-EAGAIN</varname> and the caller must 
try again.
</PARA>
    
<PARA>
It is possible to configure the write call to be non-blocking with timeout. 
None-blocking mode with timeout requires the configuration option 
<varname>CYGOPT_IO_CAN_SUPPORT_NONBLOCKING</varname> and 
<varname>CYGOPT_IO_CAN_SUPPORT_TIMEOUTS</varname> to be enabled, 
requires the eCos kernel package to be included and the specific device 
to be set to non-blocking mode for reads (see 
<function>cyg_io_set_config()</function>). In non-blocking mode with timeouts, 
if there is no CAN event in receive buffer, the driver waits a certain amount 
of time (the timeout time) for a CAN event to arrive. If there is still no 
CAN event in buffer after expiration of the timeout time, <varname>-EINTR</varname> 
is returned and the caller must try again.
</PARA>
    
<PARA>
If a event was sucessfully received, the function returns <varname>ENOERR</varname>.    
</PARA>
</SECTION><!-- cyg_io_read -->


<SECTION>  
<TITLE>CAN Events</TITLE>

<PARA>
The CAN driver uses <structname>cyg_can_event</structname> structures to
pass events from hardware device driver to the generic CAN driver.
A <structname>cyg_can_event</structname> provides a generic device 
independent type for handling CAN events that may occur.
</PARA> 

<PROGRAMLISTING>
typedef struct cyg_can_event_st
{
    cyg_uint32      timestamp;
    cyg_can_message msg;
    cyg_uint16      flags;
} cyg_can_event;
</PROGRAMLISTING>

<PARA>
The structure contains the following fields:
</PARA>
 
<variablelist>
   <varlistentry>
       <term><type>cyg_uint32</type> <varname>timestamp</varname></term>
           <listitem><para>
If the hardware CAN device driver supports timestamps then this field may 
contain a timestamp value for an event that occured.
          </para></listitem>
   </varlistentry>
   <varlistentry>
       <term><type>cyg_can_message</type> <varname>msg</varname></term>
           <listitem><para>
CAN message. The msg field contains a CAN message if an RX or TX event
occured.  If another type of event occured,
the <structfield>data</structfield> field of
the <structfield>msg</structfield> may contain additional event
specific data.
          </para></listitem>
   </varlistentry>
   <varlistentry>
       <term><type>cyg_uint16</type> <varname>flags</varname></term>
           <listitem><para>
Event flags. The <varname>flags</varname> field contains 16 bits that 
indicate which kind of events occured. 
          </para></listitem>
   </varlistentry>
</variablelist>
    
<PARA>
The following events are supported and after receiving an event the 
application should check the flag field against these values:
</PARA>
    
<PROGRAMLISTING>
typedef enum
{
  CYGNUM_CAN_EVENT_RX               = 0x0001, // message received
  CYGNUM_CAN_EVENT_TX               = 0x0002, // message transmitted
  CYGNUM_CAN_EVENT_WARNING_RX       = 0x0004, // (TEC) reached warning level (>96)
  CYGNUM_CAN_EVENT_WARNING_TX       = 0x0008, // (REC) reached warning level (>96)
  CYGNUM_CAN_EVENT_ERR_PASSIVE      = 0x0010, // CAN "error passive" occured
  CYGNUM_CAN_EVENT_BUS_OFF          = 0x0020, // CAN "bus off" error occured
  CYGNUM_CAN_EVENT_OVERRUN_RX       = 0x0040, // overrun in RX queue or hardware 
  CYGNUM_CAN_EVENT_OVERRUN_TX       = 0x0080, // overrun in TX queue occured
  CYGNUM_CAN_EVENT_CAN_ERR          = 0x0100, // a CAN bit or frame error occured
  CYGNUM_CAN_EVENT_LEAVING_STANDBY  = 0x0200, // CAN hardware leaves standby
  CYGNUM_CAN_EVENT_ENTERING_STANDBY = 0x0400, // CAN hardware enters standby
  CYGNUM_CAN_EVENT_ARBITRATION_LOST = 0x0800, // arbitration lost
  CYGNUM_CAN_EVENT_FILTER_ERR       = 0x1000, // CAN message filter / acceptance filter error
  CYGNUM_CAN_EVENT_PHY_FAULT        = 0x2000, // General failure of physical layer 
  CYGNUM_CAN_EVENT_PHY_H            = 0x4000, // Fault on CAN-H (Low Speed CAN)
  CYGNUM_CAN_EVENT_PHY_L            = 0x8000, // Fault on CAN-L (Low Speed CAN)
} cyg_can_event_flags;
</PROGRAMLISTING>

<PARA>
Often the flags field will contain only one single set flag. But it is 
possible that a number of flags is set and so the flag field should always 
be checked by a receiver. I.e. if the <varname>CYGNUM_CAN_EVENT_RX</varname> 
is set then also the <varname>CYGNUM_CAN_EVENT_OVERRUN_RX</varname> 
may be set if the received message caused an RX overrun.
</PARA>
    
<PARA>
The internal receive buffers of the CAN device driver are circular buffers. 
That means that even if the buffers are completely filled new messages 
will be received. In this case the newest message will always overwrite 
the oldest message in receive buffer. If this happens the 
<varname>CYGNUM_CAN_EVENT_OVERRUN_RX</varname> flag will be set for this 
new message that caused overwriting of the old one. The 
<varname>CYGNUM_CAN_EVENT_OVERRUN_RX</varname> flag will be set also if 
a overrun occures in hardware message buffers of the CAN device.
</PARA>
    
<PARA>
Example code for receiving one single CAN event:
</PARA>

<PROGRAMLISTING>
cyg_can_event rx_event;
cyg_uint32    len;
Cyg_ErrNo     ret;

len = sizeof(rx_event);
ret = cyg_io_read(hDrvCAN, &amp;rx_event, &amp;len);

if (ENOERR == ret)
{
    if (rx_event.flags &amp; CYGNUM_CAN_EVENT_RX)
    {
        // handle RX event
    }
    
    if (rx_event.flags &amp; ~CYGNUM_CAN_EVENT_RX)
    {
        // handle other events
    }
}
else if (-EINTR == ret)
{
    // handle timeout
}
</PROGRAMLISTING>       
</SECTION><!-- can-cyg_can_event -->

  
<SECTION>
<TITLE>cyg_io_get_config</TITLE> 
  
<PROGRAMLISTING>
cyg_io_get_config(handle, key, buf, len)
</PROGRAMLISTING>

<PARA>
This function is used to obtain run-time configuration about a device. 
The type of information retrieved is specified by the <parameter>key</parameter>. 
The data will be returned in the given buffer. The value of 
<parameter>*len</parameter> should contain the amount of data requested, 
which must be at least as large as the size appropriate to the selected 
<parameter>key</parameter>. The actual size of data retrieved is placed 
in <parameter>*len</parameter>. The appropriate key values are all listed 
in the file <filename>&lt;cyg/io/config_keys.h&gt;</filename>.    
</PARA>   
    
<PARA>
The following config keys are currently supported:
</PARA>  
    
<PROGRAMLISTING>
CYG_IO_GET_CONFIG_READ_BLOCKING
CYG_IO_GET_CONFIG_WRITE_BLOCKING
CYG_IO_GET_CONFIG_CAN_INFO
CYG_IO_GET_CONFIG_CAN_BUFFER_INFO
CYG_IO_GET_CONFIG_CAN_MSGBUF_INFO
CYG_IO_GET_CONFIG_CAN_TIMEOUT
CYG_IO_GET_CONFIG_CAN_HDI
CYG_IO_GET_CONFIG_CAN_STATE
</PROGRAMLISTING>
</SECTION><!-- can-cyg-io-get-config -->
  
<SECTION>
<TITLE>cyg_io_set_config</TITLE> 
  
<PROGRAMLISTING>
cyg_io_set_config(handle, key, buf, len)
</PROGRAMLISTING>

<PARA>
This function is used to manipulate or change the run-time configuration 
of a device. The type of information is specified by the <parameter>key</parameter>. 
The data will be obtained from the given buffer. The value of 
<parameter>*len</parameter> should contain the amount of data provided, 
which must match the size appropriate to the selected <parameter>key</parameter>. 
The appropriate key values are all listed in the file 
<filename>&lt;cyg/io/config_keys.h&gt;</filename>.  
</PARA>   
    
<PARA>
The following config keys are currently supported:
</PARA>  
    
<PROGRAMLISTING>
CYG_IO_SET_CONFIG_READ_BLOCKING
CYG_IO_SET_CONFIG_WRITE_BLOCKING
CYG_IO_SET_CONFIG_CAN_INFO
CYG_IO_SET_CONFIG_CAN_OUTPUT_DRAIN
CYG_IO_SET_CONFIG_CAN_OUTPUT_FLUSH
CYG_IO_SET_CONFIG_CAN_INPUT_FLUSH
CYG_IO_SET_CONFIG_CAN_TIMEOUT
CYG_IO_SET_CONFIG_CAN_MSGBUF
CYG_IO_SET_CONFIG_CAN_MODE
CYG_IO_SET_CONFIG_CAN_ABORT
CYG_IO_SET_CONFIG_CAN_CALLBACK

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -