00001 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00002 * 00003 * Redistribution and use in source and binary forms, with or without 00004 * modification, are permitted provided that the following conditions are met: 00005 * 00006 * 1. Redistributions of source code must retain the above copyright notice, this 00007 * list of conditions and the following disclaimer. 00008 * 00009 * 2. Redistributions in binary form must reproduce the above copyright notice, 00010 * this list of conditions and the following disclaimer in the documentation 00011 * and/or other materials provided with the distribution. 00012 * 00013 * 3. The name of Atmel may not be used to endorse or promote products derived 00014 * from this software without specific prior written permission. 00015 * 00016 * 4. This software may only be redistributed and used in connection with an Atmel 00017 * AVR product. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00020 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00021 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00022 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00029 * 00030 */ 00031 00032 #include "rs232.h" 00033 #include "error_management.h" 00034 00035 // COM port handle 00036 static HANDLE handle_com = NULL; 00037 00038 // Delay 00039 static COMMTIMEOUTS g_cto = 00040 { 00041 RS232_MAX_WAIT_READ, 00042 0, 00043 RS232_MAX_WAIT_READ, 00044 0, 00045 0 00046 }; 00047 00048 // Configuration 00049 static DCB g_dcb = 00050 { 00051 sizeof(DCB), // DCBlength 00052 9600, // BaudRate 00053 TRUE, // fBinary 00054 FALSE, // fParity 00055 FALSE, // fOutxCtsFlow 00056 FALSE, // fOutxDsrFlow 00057 DTR_CONTROL_ENABLE, // fDtrControl 00058 FALSE, // fDsrSensitivity 00059 FALSE, // fTXContinueOnXoff 00060 FALSE, // fOutX 00061 FALSE, // fInX 00062 FALSE, // fErrorChar 00063 FALSE, // fNull 00064 RTS_CONTROL_ENABLE, // fRtsControl 00065 FALSE, // fAbortOnError 00066 0, // fDummy2 00067 0, // wReserved 00068 0x100, // XonLim 00069 0x100, // XoffLim 00070 8, // ByteSize 00071 NOPARITY, // Parity 00072 ONESTOPBIT, // StopBits 00073 0x11, // XonChar 00074 0x13, // XoffChar 00075 '?', // ErrorChar 00076 0x1A, // EofChar 00077 0x10 // EvtChar 00078 }; 00079 00080 // Open the rs232 port 00081 int rs232_open(char *_port, int baud_rate, int byte_size, int parity, int stop_bits) 00082 { 00083 // Make sure another port is not already opened 00084 ASSERT(!handle_com); 00085 00086 // Open the existing COMX file to open the port 00087 handle_com = CreateFile( 00088 _port, 00089 GENERIC_READ | GENERIC_WRITE, 00090 0, 00091 NULL, 00092 OPEN_EXISTING, 00093 FILE_ATTRIBUTE_SYSTEM, 00094 NULL); 00095 00096 // Make sure it is opened 00097 if (handle_com == INVALID_HANDLE_VALUE) 00098 return 0; 00099 00100 // buffer size for emission and reception 00101 SetupComm(handle_com, RS232_RX_SIZE, RS232_TX_SIZE); 00102 00103 // COM port configuration 00104 g_dcb.BaudRate = baud_rate; 00105 g_dcb.ByteSize = byte_size; 00106 g_dcb.Parity = parity; 00107 g_dcb.StopBits = stop_bits; 00108 if(!SetCommTimeouts(handle_com, &g_cto) || !SetCommState(handle_com, &g_dcb)) 00109 { 00110 CloseHandle(handle_com); 00111 return 0; 00112 } 00113 00114 // Flush buffers for emission and reception 00115 // DTR = 1 00116 PurgeComm(handle_com, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT); 00117 EscapeCommFunction(handle_com, SETDTR); 00118 00119 return 1; 00120 } 00121 00122 // Close the previously opened rs232 port 00123 int rs232_close() 00124 { 00125 CloseHandle(handle_com); 00126 return 1; 00127 } 00128 00129 // Read data from the rs232 port 00130 int rs232_read(void *buffer, int size, int *_read_bytes) 00131 { 00132 return ReadFile(handle_com, buffer, size, (DWORD *) _read_bytes, (LPOVERLAPPED) NULL); 00133 } 00134 00135 // Write data through the rs232 port 00136 int rs232_write(void* buffer, int size, int* _written_bytes) 00137 { 00138 return WriteFile(handle_com, buffer, size, (DWORD *) _written_bytes, (LPOVERLAPPED) NULL); 00139 }