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 #include "rs232.h" 00032 #include "error_management.h" 00033 00034 // COM port handle 00035 static HANDLE handle_com = NULL; 00036 00037 // Delay 00038 static COMMTIMEOUTS g_cto = 00039 { 00040 RS232_MAX_WAIT_READ, 00041 0, 00042 RS232_MAX_WAIT_READ, 00043 0, 00044 0 00045 }; 00046 00047 // Configuration 00048 static DCB g_dcb = 00049 { 00050 sizeof(DCB), // DCBlength 00051 9600, // BaudRate 00052 TRUE, // fBinary 00053 FALSE, // fParity 00054 FALSE, // fOutxCtsFlow 00055 FALSE, // fOutxDsrFlow 00056 DTR_CONTROL_ENABLE, // fDtrControl 00057 FALSE, // fDsrSensitivity 00058 FALSE, // fTXContinueOnXoff 00059 FALSE, // fOutX 00060 FALSE, // fInX 00061 FALSE, // fErrorChar 00062 FALSE, // fNull 00063 RTS_CONTROL_ENABLE, // fRtsControl 00064 FALSE, // fAbortOnError 00065 0, // fDummy2 00066 0, // wReserved 00067 0x100, // XonLim 00068 0x100, // XoffLim 00069 8, // ByteSize 00070 NOPARITY, // Parity 00071 ONESTOPBIT, // StopBits 00072 0x11, // XonChar 00073 0x13, // XoffChar 00074 '?', // ErrorChar 00075 0x1A, // EofChar 00076 0x10 // EvtChar 00077 }; 00078 00079 // Open the rs232 port 00080 int rs232_open(char *_port, int baud_rate, int byte_size, int parity, int stop_bits) 00081 { 00082 // Make sure another port is not already opened 00083 ASSERT(!handle_com); 00084 00085 // Open the existing COMX file to open the port 00086 handle_com = CreateFile( 00087 _port, 00088 GENERIC_READ | GENERIC_WRITE, 00089 0, 00090 NULL, 00091 OPEN_EXISTING, 00092 FILE_ATTRIBUTE_SYSTEM, 00093 NULL); 00094 00095 // Make sure it is opened 00096 if (handle_com == INVALID_HANDLE_VALUE) 00097 return 0; 00098 00099 // buffer size for emission and reception 00100 SetupComm(handle_com, RS232_RX_SIZE, RS232_TX_SIZE); 00101 00102 // COM port configuration 00103 g_dcb.BaudRate = baud_rate; 00104 g_dcb.ByteSize = byte_size; 00105 g_dcb.Parity = parity; 00106 g_dcb.StopBits = stop_bits; 00107 if(!SetCommTimeouts(handle_com, &g_cto) || !SetCommState(handle_com, &g_dcb)) 00108 { 00109 CloseHandle(handle_com); 00110 return 0; 00111 } 00112 00113 // Flush buffers for emission and reception 00114 // DTR = 1 00115 PurgeComm(handle_com, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT); 00116 EscapeCommFunction(handle_com, SETDTR); 00117 00118 return 1; 00119 } 00120 00121 // Close the previously opened rs232 port 00122 int rs232_close() 00123 { 00124 CloseHandle(handle_com); 00125 return 1; 00126 } 00127 00128 // Read data from the rs232 port 00129 int rs232_read(void *buffer, int size, int *_read_bytes) 00130 { 00131 return ReadFile(handle_com, buffer, size, (DWORD *) _read_bytes, (LPOVERLAPPED) NULL); 00132 } 00133 00134 // Write data through the rs232 port 00135 int rs232_write(void* buffer, int size, int* _written_bytes) 00136 { 00137 return WriteFile(handle_com, buffer, size, (DWORD *) _written_bytes, (LPOVERLAPPED) NULL); 00138 }