Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > f831237475ece7d1519c6812064707d4 > files > 137

libnfc-devel-1.4.2-1.fc14.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libnfc: uart_win32.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.3 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">libnfc&#160;<span id="projectnumber">1.4.2</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="dir_fd1c9d0a8b12e3b9059fa72e8f6f49bb.html">libnfc</a>      </li>
      <li class="navelem"><a class="el" href="dir_e2634d1218a8c3cbfbedbd514dd3a3d7.html">buses</a>      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<h1>uart_win32.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*-</span>
<a name="l00002"></a>00002 <span class="comment"> * Public platform independent Near Field Communication (NFC) library</span>
<a name="l00003"></a>00003 <span class="comment"> * </span>
<a name="l00004"></a>00004 <span class="comment"> * Copyright (C) 2009, 2010, Roel Verdult, Romuald Conty</span>
<a name="l00005"></a>00005 <span class="comment"> * </span>
<a name="l00006"></a>00006 <span class="comment"> * This program is free software: you can redistribute it and/or modify it</span>
<a name="l00007"></a>00007 <span class="comment"> * under the terms of the GNU Lesser General Public License as published by the</span>
<a name="l00008"></a>00008 <span class="comment"> * Free Software Foundation, either version 3 of the License, or (at your</span>
<a name="l00009"></a>00009 <span class="comment"> * option) any later version.</span>
<a name="l00010"></a>00010 <span class="comment"> * </span>
<a name="l00011"></a>00011 <span class="comment"> * This program is distributed in the hope that it will be useful, but WITHOUT</span>
<a name="l00012"></a>00012 <span class="comment"> * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span>
<a name="l00013"></a>00013 <span class="comment"> * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for</span>
<a name="l00014"></a>00014 <span class="comment"> * more details.</span>
<a name="l00015"></a>00015 <span class="comment"> *</span>
<a name="l00016"></a>00016 <span class="comment"> * You should have received a copy of the GNU Lesser General Public License</span>
<a name="l00017"></a>00017 <span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> */</span>
<a name="l00020"></a>00020 
<a name="l00026"></a>00026 <span class="keyword">typedef</span> <span class="keyword">struct </span>{
<a name="l00027"></a>00027   HANDLE  hPort;                <span class="comment">// Serial port handle</span>
<a name="l00028"></a>00028   DCB     dcb;                  <span class="comment">// Device control settings</span>
<a name="l00029"></a>00029   COMMTIMEOUTS ct;              <span class="comment">// Serial port time-out configuration</span>
<a name="l00030"></a>00030 } serial_port_windows;
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 serial_port
<a name="l00033"></a>00033 uart_open (<span class="keyword">const</span> <span class="keywordtype">char</span> *pcPortName)
<a name="l00034"></a>00034 {
<a name="l00035"></a>00035   <span class="keywordtype">char</span>    acPortName[255];
<a name="l00036"></a>00036   serial_port_windows *sp = malloc (<span class="keyword">sizeof</span> (serial_port_windows));
<a name="l00037"></a>00037 
<a name="l00038"></a>00038   <span class="comment">// Copy the input &quot;com?&quot; to &quot;\\.\COM?&quot; format</span>
<a name="l00039"></a>00039   sprintf (acPortName, <span class="stringliteral">&quot;\\\\.\\%s&quot;</span>, pcPortName);
<a name="l00040"></a>00040   _strupr (acPortName);
<a name="l00041"></a>00041 
<a name="l00042"></a>00042   <span class="comment">// Try to open the serial port</span>
<a name="l00043"></a>00043   sp-&gt;hPort = CreateFileA (acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
<a name="l00044"></a>00044   <span class="keywordflow">if</span> (sp-&gt;hPort == INVALID_HANDLE_VALUE) {
<a name="l00045"></a>00045     uart_close (sp);
<a name="l00046"></a>00046     <span class="keywordflow">return</span> INVALID_SERIAL_PORT;
<a name="l00047"></a>00047   }
<a name="l00048"></a>00048   <span class="comment">// Prepare the device control</span>
<a name="l00049"></a>00049   memset (&amp;sp-&gt;dcb, 0, sizeof (DCB));
<a name="l00050"></a>00050   sp-&gt;dcb.DCBlength = <span class="keyword">sizeof</span> (DCB);
<a name="l00051"></a>00051   <span class="keywordflow">if</span> (!BuildCommDCBA (<span class="stringliteral">&quot;baud=9600 data=8 parity=N stop=1&quot;</span>, &amp;sp-&gt;dcb)) {
<a name="l00052"></a>00052     uart_close (sp);
<a name="l00053"></a>00053     <span class="keywordflow">return</span> INVALID_SERIAL_PORT;
<a name="l00054"></a>00054   }
<a name="l00055"></a>00055   <span class="comment">// Update the active serial port</span>
<a name="l00056"></a>00056   <span class="keywordflow">if</span> (!SetCommState (sp-&gt;hPort, &amp;sp-&gt;dcb)) {
<a name="l00057"></a>00057     uart_close (sp);
<a name="l00058"></a>00058     <span class="keywordflow">return</span> INVALID_SERIAL_PORT;
<a name="l00059"></a>00059   }
<a name="l00060"></a>00060 
<a name="l00061"></a>00061   sp-&gt;ct.ReadIntervalTimeout = 30;
<a name="l00062"></a>00062   sp-&gt;ct.ReadTotalTimeoutMultiplier = 0;
<a name="l00063"></a>00063   sp-&gt;ct.ReadTotalTimeoutConstant = 30;
<a name="l00064"></a>00064   sp-&gt;ct.WriteTotalTimeoutMultiplier = 30;
<a name="l00065"></a>00065   sp-&gt;ct.WriteTotalTimeoutConstant = 0;
<a name="l00066"></a>00066 
<a name="l00067"></a>00067   <span class="keywordflow">if</span> (!SetCommTimeouts (sp-&gt;hPort, &amp;sp-&gt;ct)) {
<a name="l00068"></a>00068     uart_close (sp);
<a name="l00069"></a>00069     <span class="keywordflow">return</span> INVALID_SERIAL_PORT;
<a name="l00070"></a>00070   }
<a name="l00071"></a>00071 
<a name="l00072"></a>00072   PurgeComm (sp-&gt;hPort, PURGE_RXABORT | PURGE_RXCLEAR);
<a name="l00073"></a>00073 
<a name="l00074"></a>00074   <span class="keywordflow">return</span> sp;
<a name="l00075"></a>00075 }
<a name="l00076"></a>00076 
<a name="l00077"></a>00077 <span class="keywordtype">void</span>
<a name="l00078"></a>00078 uart_close (<span class="keyword">const</span> serial_port sp)
<a name="l00079"></a>00079 {
<a name="l00080"></a>00080   <span class="keywordflow">if</span> (((serial_port_windows *) sp)-&gt;hPort != INVALID_HANDLE_VALUE) {
<a name="l00081"></a>00081     CloseHandle (((serial_port_windows *) sp)-&gt;hPort);
<a name="l00082"></a>00082   }
<a name="l00083"></a>00083   free (sp);
<a name="l00084"></a>00084 }
<a name="l00085"></a>00085 
<a name="l00086"></a>00086 <span class="comment">// TODO Remove PN53x related timeout</span>
<a name="l00087"></a>00087 <span class="keywordtype">void</span>
<a name="l00088"></a>00088 uart_set_speed (serial_port sp, <span class="keyword">const</span> uint32_t uiPortSpeed)
<a name="l00089"></a>00089 {
<a name="l00090"></a>00090   serial_port_windows *spw;
<a name="l00091"></a>00091 
<a name="l00092"></a>00092   DBG (<span class="stringliteral">&quot;Serial port speed requested to be set to %d bauds.&quot;</span>, uiPortSpeed);
<a name="l00093"></a>00093   <span class="comment">// Set port speed (Input and Output)</span>
<a name="l00094"></a>00094   <span class="keywordflow">switch</span> (uiPortSpeed) {
<a name="l00095"></a>00095   <span class="keywordflow">case</span> 9600:
<a name="l00096"></a>00096   <span class="keywordflow">case</span> 19200:
<a name="l00097"></a>00097   <span class="keywordflow">case</span> 38400:
<a name="l00098"></a>00098   <span class="keywordflow">case</span> 57600:
<a name="l00099"></a>00099   <span class="keywordflow">case</span> 115200:
<a name="l00100"></a>00100   <span class="keywordflow">case</span> 230400:
<a name="l00101"></a>00101   <span class="keywordflow">case</span> 460800:
<a name="l00102"></a>00102     <span class="keywordflow">break</span>;
<a name="l00103"></a>00103   <span class="keywordflow">default</span>:
<a name="l00104"></a>00104     ERR(<span class="stringliteral">&quot;Unable to set serial port speed to %d bauds. Speed value must be one of these constants: 9600 (default), 19200, 38400, 57600, 115200, 230400 or 460800.&quot;</span>, uiPortSpeed);
<a name="l00105"></a>00105     <span class="keywordflow">return</span>;
<a name="l00106"></a>00106   };
<a name="l00107"></a>00107   spw = (serial_port_windows *) sp;
<a name="l00108"></a>00108 
<a name="l00109"></a>00109   <span class="comment">// Set timeouts</span>
<a name="l00110"></a>00110   <span class="comment">//printf (&quot;UART_SPEED_T0_TIME (%d) = %d\n&quot;, uiPortSpeed, UART_SPEED_T0_TIME(uiPortSpeed));</span>
<a name="l00111"></a>00111   <span class="keywordtype">int</span> iTimeout = 200;
<a name="l00112"></a>00112   spw-&gt;ct.ReadIntervalTimeout = 2;
<a name="l00113"></a>00113   spw-&gt;ct.ReadTotalTimeoutMultiplier = 0;
<a name="l00114"></a>00114   spw-&gt;ct.ReadTotalTimeoutConstant = iTimeout;
<a name="l00115"></a>00115   spw-&gt;ct.WriteTotalTimeoutMultiplier = iTimeout;
<a name="l00116"></a>00116   spw-&gt;ct.WriteTotalTimeoutConstant = 0;
<a name="l00117"></a>00117 
<a name="l00118"></a>00118   <span class="keywordflow">if</span> (!SetCommTimeouts (spw-&gt;hPort, &amp;spw-&gt;ct)) {
<a name="l00119"></a>00119     ERR (<span class="stringliteral">&quot;Unable to apply new timeout settings.&quot;</span>);
<a name="l00120"></a>00120     <span class="keywordflow">return</span>;
<a name="l00121"></a>00121   }
<a name="l00122"></a>00122 
<a name="l00123"></a>00123   <span class="comment">// Set baud rate</span>
<a name="l00124"></a>00124   spw-&gt;dcb.BaudRate = uiPortSpeed;
<a name="l00125"></a>00125   <span class="keywordflow">if</span> (!SetCommState (spw-&gt;hPort, &amp;spw-&gt;dcb)) {
<a name="l00126"></a>00126     ERR (<span class="stringliteral">&quot;Unable to apply new speed settings.&quot;</span>);
<a name="l00127"></a>00127     <span class="keywordflow">return</span>;
<a name="l00128"></a>00128   }
<a name="l00129"></a>00129   PurgeComm (spw-&gt;hPort, PURGE_RXABORT | PURGE_RXCLEAR);
<a name="l00130"></a>00130 }
<a name="l00131"></a>00131 
<a name="l00132"></a>00132 uint32_t
<a name="l00133"></a>00133 uart_get_speed (<span class="keyword">const</span> serial_port sp)
<a name="l00134"></a>00134 {
<a name="l00135"></a>00135   <span class="keyword">const</span> serial_port_windows *spw = (serial_port_windows *) sp;
<a name="l00136"></a>00136   <span class="keywordflow">if</span> (!GetCommState (spw-&gt;hPort, (serial_port) &amp; spw-&gt;dcb))
<a name="l00137"></a>00137     <span class="keywordflow">return</span> spw-&gt;dcb.BaudRate;
<a name="l00138"></a>00138 
<a name="l00139"></a>00139   <span class="keywordflow">return</span> 0;
<a name="l00140"></a>00140 }
<a name="l00141"></a>00141 
<a name="l00142"></a>00142 <span class="keywordtype">int</span>
<a name="l00143"></a>00143 <a class="code" href="uart_8h.html#a51a4c4c363a0de96e1e9df399ae8b2f4" title="Receive data from UART and copy data to pbtRx.">uart_receive</a> (serial_port sp, byte_t * pbtRx, <span class="keywordtype">size_t</span> * pszRx)
<a name="l00144"></a>00144 {
<a name="l00145"></a>00145   <span class="keywordflow">if</span> (!ReadFile (((serial_port_windows *) sp)-&gt;hPort, pbtRx, (DWORD)(*pszRx), (LPDWORD) pszRx, NULL)) {
<a name="l00146"></a>00146     <span class="keywordflow">return</span> DEIO;
<a name="l00147"></a>00147   }
<a name="l00148"></a>00148   <span class="keywordflow">if</span> (!*pszRx)
<a name="l00149"></a>00149     <span class="keywordflow">return</span> DEIO;
<a name="l00150"></a>00150   <span class="keywordflow">return</span> 0;
<a name="l00151"></a>00151 }
<a name="l00152"></a>00152 
<a name="l00153"></a>00153 <span class="keywordtype">int</span>
<a name="l00154"></a>00154 <a class="code" href="uart_8h.html#a39c415b8d6fa14b2168f2a35cbac48d0" title="Send pbtTx content to UART.">uart_send</a> (serial_port sp, <span class="keyword">const</span> byte_t * pbtTx, <span class="keyword">const</span> <span class="keywordtype">size_t</span> szTx)
<a name="l00155"></a>00155 {
<a name="l00156"></a>00156   DWORD   dwTxLen = 0;
<a name="l00157"></a>00157   <span class="keywordflow">if</span> (!WriteFile (((serial_port_windows *) sp)-&gt;hPort, pbtTx, szTx, &amp;dwTxLen, NULL)) {
<a name="l00158"></a>00158     <span class="keywordflow">return</span> DEIO;
<a name="l00159"></a>00159   }
<a name="l00160"></a>00160   <span class="keywordflow">if</span> (!dwTxLen)
<a name="l00161"></a>00161     <span class="keywordflow">return</span> DEIO;
<a name="l00162"></a>00162   <span class="keywordflow">return</span> 0;
<a name="l00163"></a>00163 }
<a name="l00164"></a>00164 
</pre></div></div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Feb 23 2011 for libnfc by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </small></address>
</body>
</html>