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

📄 io-how-to-write-a-driver.html

📁 有关ecos2。0介绍了实时嵌入式的结构以及线程调度的实现和内存的管理等
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!-- Copyright (C) 2003 Red Hat, 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.               --><HTML><HEAD><TITLE>How to Write a Driver</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos Reference Manual"HREF="ecos-ref.html"><LINKREL="UP"TITLE="I/O Package (Device Drivers)"HREF="io.html"><LINKREL="PREVIOUS"TITLE=" TTY driver"HREF="io-tty-driver.html"><LINKREL="NEXT"TITLE="Serial testing with ser_filter"HREF="io-serial-testing-with-serfilter.html"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos Reference Manual</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="io-tty-driver.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="io-serial-testing-with-serfilter.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="IO-HOW-TO-WRITE-A-DRIVER">Chapter 17. How to Write a Driver</H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="io-how-to-write-a-driver.html#IO-HOW-TO-WRITE-SERIAL-INTERFACE-DRIVER">How to Write a Serial Hardware Interface Driver</A></DT><DT><AHREF="io-serial-testing-with-serfilter.html">Serial testing with ser_filter</A></DT></DL></DIV><P>A device driver is nothing more than anamed entity that supports the basic I/O functions - read, write, getconfig, and set config. Typically a device driver also uses andmanages interrupts from the device. While the interface is generic anddevice driver independent, the actual driver implementation iscompletely up to the device driver designer. </P><P>That said, the reason for using a device driver is to provideaccess to a device from application code in as general purpose afashion as reasonable. Most driver writers are also concerned withmaking this access as simple as possible while being as efficientas possible. </P><P>Most device drivers are concerned with the movement of information,for example data bytes along a serial interface, or packets in anetwork. In order to make the most efficient use of system resources,interrupts are used. This will allow other application processingto take place while the data transfers are under way, with interruptsused to indicate when various events have occurred. For example,a serial port typically generates an interrupt after a characterhas been sent &#8220;down the wire&#8221; and the interfaceis ready for another. It makes sense to allow further applicationprocessing while the data is being sent since this can take quitea long time. The interrupt can be used to allow the driver to senda character as soon as the current one is complete, without anyactive participation by the application code. </P><P>The main building blocks for device drivers are found in theinclude file: <TTCLASS="FILENAME">&lt;cyg/io/devtab.h&gt;</TT></P><P>All device drivers in <SPANCLASS="emphasis"><ICLASS="EMPHASIS">eCos</I></SPAN> are describedby a device table entry, using the <SPANCLASS="TYPE">cyg_devtab_entry_t</SPAN> type.The entry should be created using the <TTCLASS="FUNCTION">DEVTAB_ENTRY()</TT> macro,like this:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING"><TTCLASS="FUNCTION">DEVTAB_ENTRY</TT>(l, name, dep_name, handlers, init, lookup, priv)</PRE></TD></TR></TABLE><P></P><DIVCLASS="VARIABLELIST"><P><B>Arguments</B></P><DL><DT><TTCLASS="PARAMETER"><I>l</I></TT></DT><DD><P>The "C" label for this device table entry.</P></DD><DT><TTCLASS="PARAMETER"><I>name</I></TT></DT><DD><P>The "C" string name for the device.</P></DD><DT><TTCLASS="PARAMETER"><I>dep_name</I></TT></DT><DD><P>For a layered device, the "C" string name of the    device this device is built upon.</P></DD><DT><TTCLASS="PARAMETER"><I>handlers</I></TT></DT><DD><P>A pointer to the I/O function "handlers" (see below).</P></DD><DT><TTCLASS="PARAMETER"><I>init</I></TT></DT><DD><P>A function called when eCos is initialized. This    function can query the device, setup hardware, etc.</P></DD><DT><TTCLASS="PARAMETER"><I>lookup</I></TT></DT><DD><P>A function called when <TTCLASS="FUNCTION">cyg_io_lookup()</TT> is called    for this device. </P></DD><DT><TTCLASS="PARAMETER"><I>priv</I></TT></DT><DD><P>A placeholder for any device specific data    required by the driver.</P></DD></DL></DIV><P>The interface to the driver is through the <TTCLASS="STRUCTFIELD"><I>handlers</I></TT> field.  This is a pointer toa set of functions which implement the various <TTCLASS="FUNCTION">cyg_io_XXX()</TT>routines. This table is defined by the macro:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">DEVIO_TABLE(l, write, read, get_config, set_config)</PRE></TD></TR></TABLE><P></P><DIVCLASS="VARIABLELIST"><P><B>Arguments</B></P><DL><DT><TTCLASS="PARAMETER"><I>l</I></TT></DT><DD><P>The "C" label for this table of handlers.</P></DD><DT>write</DT><DD><P>The function called as a result of    <TTCLASS="FUNCTION">cyg_io_write()</TT>.</P></DD><DT>read</DT><DD><P>The function called as a result of    <TTCLASS="FUNCTION">cyg_io_read()</TT>. </P></DD><DT>get_config</DT><DD><P>The function called as a result of    <TTCLASS="FUNCTION">cyg_io_get_config()</TT>.</P></DD><DT>set_config</DT><DD><P>The function called as a result of    <TTCLASS="FUNCTION">cyg_io_set_config()</TT>. </P></DD></DL></DIV><P>When <SPANCLASS="emphasis"><ICLASS="EMPHASIS">eCos</I></SPAN> is initialized (sometimes called&#8220;boot&#8221; time), the <TTCLASS="FUNCTION">init()</TT> function is calledfor all devices in the system. The <TTCLASS="FUNCTION">init()</TT> function isallowed to return an error in which case the device will be placed&#8220;off line&#8221; and all I/O requests to that device will beconsidered in error.</P><P>The <TTCLASS="FUNCTION">lookup()</TT> function is called wheneverthe <TTCLASS="FUNCTION">cyg_io_lookup()</TT> functionis called with this device name. The lookup function may cause the deviceto come &#8220;on line&#8221; which would then allow I/Ooperations to proceed. Future versions of the I/O systemwill allow for other states, including power saving modes,etc.</P><DIVCLASS="SECTION"><H1CLASS="SECTION"><ANAME="IO-HOW-TO-WRITE-SERIAL-INTERFACE-DRIVER">How to Write a Serial Hardware Interface Driver</H1><P>The standard serial driver supplied with<SPANCLASS="emphasis"><ICLASS="EMPHASIS">eCos</I></SPAN> is structured as a hardware independentportion and a hardware dependent interface module. To add support fora new serial port, the user should be able to use the existinghardware independent portion and just add their own interface driver which handles the details of theactual device. The user should have no need to change the hardwareindependent portion. </P><P>The interfaces used by the serial driver and serial implementationmodules are contained in the file <TTCLASS="FILENAME">&lt;cyg/io/serial.h&gt;</TT></P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>In the sections below we use the notation &lt;&lt;xx&gt;&gt; tomean a module specific value, referred to as &#8220;xx&#8221; below.</P></BLOCKQUOTE></DIV><DIVCLASS="SECTION"><H2CLASS="SECTION"><ANAME="AEN10881">DevTab Entry</H2><P>The interface module contains the devtab entry (or entriesif a single module supports more than one interface). This entryshould have the form: </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">DEVTAB_ENTRY(&lt;&lt;module_name&gt;&gt;,              &lt;&lt;device_name&gt;&gt;,             0,             &amp;serial_devio,              &lt;&lt;module_init&gt;&gt;,              &lt;&lt;module_lookup&gt;&gt;,             &amp;&lt;&lt;serial_channel&gt;&gt;            );</PRE></TD></TR></TABLE><P></P><DIVCLASS="VARIABLELIST"><P><B>Arguments</B></P><DL><DT><TTCLASS="PARAMETER"><I>module_name</I></TT></DT><DD><P>The "C" label for this devtab entry</P></DD><DT><TTCLASS="PARAMETER"><I>device_name</I></TT></DT><DD><P>The "C" string for the    device. E.g. <TTCLASS="FILENAME">/dev/serial0</TT>.</P></DD><DT><TTCLASS="PARAMETER"><I>serial_devio</I></TT></DT><DD><P>The table of I/O functions. This set is defined in    the hardware independent serial driver and should be used.</P></DD><DT><TTCLASS="PARAMETER"><I>module_init</I></TT></DT><DD><P>The module initialization function.</P></DD><DT><TTCLASS="PARAMETER"><I>module_lookup</I></TT></DT><DD><P>The device lookup function. This function    typically sets up the device for actual use, turning on    interrupts, configuring the port, etc.</P></DD><DT><TTCLASS="PARAMETER"><I>serial_channel</I></TT></DT><DD><P>This table (defined below) contains the interface    between the interface module and the serial driver proper.</P></DD></DL></DIV></DIV><DIVCLASS="SECTION"><H2CLASS="SECTION"><ANAME="AEN10918">Serial Channel Structure</H2><P>Each serial device must have a &#8220;serial channel&#8221;.This is a set of data which describes all operations on the device.It also contains buffers, etc., if the device is to be buffered.The serial channel is created by the macro: </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">SERIAL_CHANNEL_USING_INTERRUPTS(l, funs, dev_priv, baud,stop, parity, word_length,                                flags, out_buf, out_buflen, in_buf, in_buflen)</PRE></TD></TR></TABLE><P></P><DIVCLASS="VARIABLELIST"><P><B>Arguments</B></P><DL><DT><TTCLASS="PARAMETER"><I>l</I></TT></DT><DD><P>The "C" label for this structure.</P

⌨️ 快捷键说明

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