📄 synth-new-target.html
字号:
<!-- 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>Writing New Devices - target</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="eCos Synthetic Target"HREF="hal-synth-arch.html"><LINKREL="PREVIOUS"TITLE="System Calls"HREF="synth-syscalls.html"><LINKREL="NEXT"TITLE="Writing New Devices - host"HREF="synth-new-host.html"></HEAD><BODYCLASS="REFENTRY"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="synth-syscalls.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="synth-new-host.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="SYNTH-NEW-TARGET">Writing New Devices - target</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN18180"></A><H2>Name</H2>Writing New Devices -- extending the synthetic target, target-side</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN18183"><H2>Synopsis</H2><DIVCLASS="FUNCSYNOPSIS"><ANAME="AEN18184"><P></P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="FUNCSYNOPSISINFO">#include <cyg/hal/hal_io.h> </PRE></TD></TR></TABLE><P><CODE><CODECLASS="FUNCDEF">int synth_auxiliary_instantiate</CODE>(const char* package, const char* version, const char* device, const char* instance, const char* data);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void synth_auxiliary_xchgmsg</CODE>(int device_id, int request, int arg1, int arg2, const unsigned char* txdata, int txlen, int* reply, unsigned char* rxdata, int* rxlen, int max_rxlen);</CODE></P><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="SYNTH-NEW-TARGET-DESCRIPTION"></A><H2>Description</H2><P>In some ways writing a device driver for the synthetic target is verysimilar to writing one for a real target. Obviously it has to providethe standard interface for that class of device, so for example anethernet device has to provide <TTCLASS="FUNCTION">can_send</TT>,<TTCLASS="FUNCTION">send</TT>, <TTCLASS="FUNCTION">recv</TT> and similarfunctions. Many devices will involve interrupts, so the drivercontains ISR and DSR functions and will call<TTCLASS="FUNCTION">cyg_drv_interrupt_create</TT>,<TTCLASS="FUNCTION">cyg_drv_interrupt_acknowledge</TT>, and relatedfunctions. </P><P>In other ways writing a device driver for the synthetic target is verydifferent. Usually the driver will not have any direct access to theunderlying hardware. In fact for some devices the I/O may not involvereal hardware, instead everything is emulated by widgets on thegraphical display. Therefore the driver cannot just peek and pokedevice registers, instead it must interact with host-side code byexchanging message. The synthetic target HAL provides a function<TTCLASS="FUNCTION">synth_auxiliary_xchgmsg</TT> for this purpose. </P><P>Initialization of a synthetic target device driver is also verydifferent. On real targets the device hardware already exists when thedriver's initialization routine runs. On the synthetic target it isfirst necessary to instantiate the device inside the I/O auxiliary, bya call to <TTCLASS="FUNCTION">synth_auxiliary_instantiate</TT>. Thatfunction performs a special message exchange with the I/O auxiliary,causing it to load a Tcl script for the desired type of device and runan instantiation procedure within that script. </P><P>Use of the I/O auxiliary is optional: if the user does not specify<TTCLASS="OPTION">--io</TT> on the command line then the auxiliary will notbe started and hence most I/O operations will not be possible. Devicedrivers should allow for this possibility, for example by justdiscarding any data that gets written. The HAL exports a flag<TTCLASS="VARNAME">synth_auxiliary_running</TT> which should be checked. </P></DIV><DIVCLASS="REFSECT1"><ANAME="SYNTH-NEW-TARGET-INSTANTIATE"></A><H2>Instantiating a Device</H2><P>Device instantiation should happen during the C++ prioritized staticconstructor phase of system initialization, before control switches to<TTCLASS="FUNCTION">cyg_user_start</TT> and general application code. Thisensures that there is a clearly defined point at which the I/Oauxiliary knows that all required devices have been loaded. It canthen perform various consistency checks and clean-ups, run the user's<TTCLASS="FILENAME">mainrc.tcl</TT> script, and make the main windowvisible. </P><P>For standard devices generic eCos I/O code will call the deviceinitialization routines at the right time, iterating through the<TTCLASS="VARNAME">DEVTAB</TT> table in a static constructor. The sameholds for network devices and file systems. For more custom devicescode like the following can be used: </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">#include <cyg/infra/cyg_type.h>class mydev_init { public: mydev_init() { … }};static mydev_init mydev_init_object CYGBLD_ATTRIB_INIT_PRI(CYG_INIT_IO);</PRE></TD></TR></TABLE><P>Some care has to be taken because the object<TTCLASS="VARNAME">mydev_init_object</TT> will typically not be referencedby other code, and hence may get eliminated at link-time. If the codeis part of an eCos package then problems can be avoided by putting therelevant file in <TTCLASS="FILENAME">libextras.a</TT>: </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cdl_package CYGPKG_DEVS_MINE { … compile -library=libextras.a init.cxx}</PRE></TD></TR></TABLE><P>For devices inside application code the same can be achieved bylinking the relevant module as a <TTCLASS="FILENAME">.o</TT> file ratherthan putting it in a <TTCLASS="FILENAME">.a</TT> library. </P><P>In the device initialization routine the main operation is a call to<TTCLASS="FUNCTION">synth_auxiliary_instantiate</TT>. This takes fivearguments, all of which should be strings: </P><P></P><DIVCLASS="VARIABLELIST"><DL><DT><TTCLASS="VARNAME">package</TT></DT><DD><P>For device drivers which are eCos packages this should be a directorypath relative to the eCos repository, for example<TTCLASS="LITERAL">devs/eth/synth/ecosynth</TT>. This will allow the I/Oauxiliary to find the various host-side support files for this packagewithin the install tree. If the device is application-specific and notpart of an eCos package then a NULL pointer can be used, causing theI/O auxiliary to search for the support files in the current directoryand then in <TTCLASS="FILENAME">~/.ecos/synth</TT>instead. </P></DD><DT><TTCLASS="VARNAME">version</TT></DT><DD><P>For eCos packages this argument should be the version of the packagethat is being used, for example <TTCLASS="LITERAL">current</TT>. A simpleway to get this version is to use the<TTCLASS="FUNCTION">SYNTH_MAKESTRING</TT> macro on the package name.If the device is application-specific then a NULL pointer should beused. </P></DD><DT><TTCLASS="VARNAME">device</TT></DT><DD><P>This argument specifies the type of device being instantiated, forexample <TTCLASS="LITERAL">ethernet</TT>. More specifically the I/Oauxiliary will append a <TTCLASS="FILENAME">.tcl</TT> suffix, givingthe name of a Tcl script that will handle all I/O requests for thedevice. If the application requires several instances of a typeof device then the script will only be loaded once, but the scriptwill contain an instantiation procedure that will be called for eachdevice instance. </P></DD><DT><TTCLASS="VARNAME">instance</TT></DT><DD><P>If it is possible to have multiple instances of a device then thisargument identifies the particular instance, for example<TTCLASS="LITERAL">eth0</TT> or <TTCLASS="LITERAL">eth1</TT>. Otherwise a NULLpointer can be used. </P></DD><DT><TTCLASS="VARNAME">data</TT></DT><DD><P>This argument can be used to pass additional initialization data fromeCos to the host-side support. This is useful for devices where eCosconfigury must control certain aspects of the device, rather thanhost-side configury such as the target definition file, because eCoshas compile-time dependencies on some or all of the relevant options.An example might be an emulated frame buffer where eCos has beenstatically configured for a particular screen size, orientation anddepth. There is no fixed format for this string, it will beinterpreted only by the device-specific host-side Tcl script. Howeverthe string length should be limited to a couple of hundred bytes toavoid possible buffer overflow problems.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -