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