📄 io-can-api-details.html
字号:
<!-- Copyright (C) 2009 Free Software Foundation, Inc. -->
<!-- This material may be distributed only subject to the terms -->
<!-- and conditions set forth in the Open Publication License, v1.0 -->
<!-- or later (the latest version is presently available at -->
<!-- http://www.opencontent.org/openpub/). -->
<!-- Distribution of the work or derivative of the work in any -->
<!-- standard (paper) book form is prohibited unless prior -->
<!-- permission is obtained from the copyright holder. -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>API Details</TITLE
><meta name="MSSmartTagsPreventParsing" content="TRUE">
<META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="eCos Reference Manual"
HREF="ecos-ref.html"><LINK
REL="UP"
TITLE="CAN driver details"
HREF="io-can-driver-details.html"><LINK
REL="PREVIOUS"
TITLE="CAN driver details"
HREF="io-can-driver-details.html"><LINK
REL="NEXT"
TITLE="Runtime Configuration"
HREF="io-can-runtime-cfg.html"></HEAD
><BODY
CLASS="SECTION"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>eCos Reference Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="io-can-driver-details.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 32. CAN driver details</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="io-can-runtime-cfg.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECTION"
><H1
CLASS="SECTION"
><A
NAME="IO-CAN-API-DETAILS"
>API Details</A
></H1
><DIV
CLASS="SECTION"
><H2
CLASS="SECTION"
><A
NAME="AEN9070"
>cyg_io_write</A
></H2
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cyg_io_write(handle, buf, len)</PRE
></TD
></TR
></TABLE
><P
>To transmit a message an application must fill a <SPAN
CLASS="TYPE"
>cyg_can_message</SPAN
>
buffer and call <CODE
CLASS="FUNCTION"
>cyg_io_write()</CODE
>.
This function sends one single CAN message (not a buffer of CAN messages)
to a device. The size of data to send is contained in <CODE
CLASS="PARAMETER"
>*len</CODE
>
and the actual size sent will be returned in the same place. A pointer to a
<SPAN
CLASS="TYPE"
>cyg_can_message</SPAN
> is contained in <CODE
CLASS="PARAMETER"
>*buf</CODE
>.
The driver maintains a buffer to hold the data. The size of the intermediate
buffer is configurable within the interface module. The data is not modified
at all while it is being buffered. On return, <CODE
CLASS="PARAMETER"
>*len</CODE
>
contains the amount of characters actually consumed - that means
<CODE
CLASS="PARAMETER"
>*len</CODE
> always contains
<CODE
CLASS="FUNCTION"
>sizeof(cyg_can_message)</CODE
>.</P
><P
>It is possible to configure the write call to be blocking (default) or
non-blocking. Non-blocking mode requires both the configuration option
<CODE
CLASS="VARNAME"
>CYGOPT_IO_CAN_SUPPORT_NONBLOCKING</CODE
> to be enabled, and
the specific device to be set to non-blocking mode for writes
(see <CODE
CLASS="FUNCTION"
>cyg_io_set_config()</CODE
>). In blocking mode, the
call will not return until there is space in the buffer and the content
of the CAN message has been consumed. In non-blocking mode, if there is
no space in buffer for the CAN message, <CODE
CLASS="VARNAME"
>-EAGAIN</CODE
> is
returned and the caller must try again.</P
><P
>It is possible to configure the write call to be non-blocking with timeout.
None-blocking mode with timeout requires the configuration option
<CODE
CLASS="VARNAME"
>CYGOPT_IO_CAN_SUPPORT_NONBLOCKING</CODE
> and
<CODE
CLASS="VARNAME"
>CYGOPT_IO_CAN_SUPPORT_TIMEOUTS</CODE
> to be enabled, requires
the eCos kernel package to be included and the specific device to be set
to non-blocking mode for writes (see <CODE
CLASS="FUNCTION"
>cyg_io_set_config()</CODE
>).
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, <CODE
CLASS="VARNAME"
>-EINTR</CODE
> is returned and
the caller must try again.</P
><P
>If a message was sucessfully sent, the function returns <CODE
CLASS="VARNAME"
>ENOERR</CODE
>.</P
></DIV
><DIV
CLASS="SECTION"
><H2
CLASS="SECTION"
><A
NAME="AEN9093"
>CAN Messages</A
></H2
><P
>The CAN driver uses <CODE
CLASS="STRUCTNAME"
>cyg_can_message</CODE
> 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.</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="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;</PRE
></TD
></TR
></TABLE
><P
>The structure contains the following fields:</P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><SPAN
CLASS="TYPE"
>cyg_uint32</SPAN
> <CODE
CLASS="VARNAME"
>id</CODE
></DT
><DD
><P
>Message ID. This is the ID to be transmitted with the message, or the
ID received. If the <CODE
CLASS="STRUCTFIELD"
>ext</CODE
> field is set, then
this will contain a 29 bit ID, otherwise it will contain an 11 bit ID.
</P
></DD
><DT
><SPAN
CLASS="TYPE"
>cyg_uint32</SPAN
> <CODE
CLASS="VARNAME"
>data</CODE
></DT
><DD
><P
>Message data. Only the first <CODE
CLASS="STRUCTFIELD"
>dlc</CODE
> bytes of
data are valid. If the <CODE
CLASS="STRUCTFIELD"
>rtr</CODE
> field is set,
then the contents of this field are ignored.
</P
></DD
><DT
><SPAN
CLASS="TYPE"
>cyg_can_id_type</SPAN
> <CODE
CLASS="VARNAME"
>ext</CODE
></DT
><DD
><P
>Extended ID. If this field is <CODE
CLASS="VARNAME"
>CYGNUM_CAN_ID_EXT</CODE
> then the
<CODE
CLASS="STRUCTNAME"
>id</CODE
> field contains a 29 bit extended ID. If it
contains <CODE
CLASS="VARNAME"
>CYGNUM_CAN_ID_STD</CODE
> then the ID is 11 bits.
</P
></DD
><DT
><SPAN
CLASS="TYPE"
>cyg_can_frame_type</SPAN
> <CODE
CLASS="VARNAME"
>rtr</CODE
></DT
><DD
><P
>Remote Transmission Request. If this field contains
<CODE
CLASS="VARNAME"
>CYGNUM_CAN_FRAME_RTR</CODE
> then the RTR bit on the message
will be set and the <CODE
CLASS="STRUCTFIELD"
>data</CODE
> field will be ignored.
If the field contains <CODE
CLASS="VARNAME"
>CYGNUM_CAN_FRAME_DATA</CODE
> then a
normal data frame will be send.
</P
></DD
><DT
><SPAN
CLASS="TYPE"
>cyg_uint8</SPAN
> <CODE
CLASS="VARNAME"
>dlc</CODE
></DT
><DD
><P
>The length of the data carried in the message. This can range from
zero to 8. In a message with the <CODE
CLASS="STRUCTFIELD"
>rtr</CODE
> field set,
this indicates the size of data being requested.
</P
></DD
></DL
></DIV
><P
>Example code for sending one single CAN message:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="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, &tx_msg, &len);</PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECTION"
><H2
CLASS="SECTION"
><A
NAME="AEN9142"
>cyg_io_read</A
></H2
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>cyg_io_read(handle, buf, len)</PRE
></TD
></TR
></TABLE
><P
>To receive a message the application calls <CODE
CLASS="FUNCTION"
>cyg_can_recv()</CODE
>.
This function receives one single event from a device. The desired size
of data to receive is contained in <CODE
CLASS="PARAMETER"
>*len</CODE
> and the
actual size obtained will be returned in the same place. A pointer to
a <SPAN
CLASS="TYPE"
>cyg_can_event</SPAN
> is contained in <CODE
CLASS="PARAMETER"
>*buf</CODE
>.
No manipulation of the data is performed before being transferred.
Again, this buffering is completely configurable. On return,
<CODE
CLASS="PARAMETER"
>*len</CODE
> contains <CODE
CLASS="FUNCTION"
>sizeof(cyg_can_event)</CODE
>.</P
><P
>
It is possible to configure the read call to be blocking (default) or
non-blocking. Non-blocking mode requires both the configuration option
<CODE
CLASS="VARNAME"
>CYGOPT_IO_CAN_SUPPORT_NONBLOCKING</CODE
> to be enabled,
and the specific device to be set to non-blocking mode for reads
(see <CODE
CLASS="FUNCTION"
>cyg_io_set_config()</CODE
>). In blocking mode,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -