NULL check so it doesnt display (null) on the comment line
[kugel-rb.git] / flash / uart_boot / uart_win.c
blob6e82e9580c7b884d7a73f6d408b1f6fa45d6363b
1 // UART wrapper implementation for the Win32 platform
2 // make a new version of this file for different systems, e.g. Linux
4 #include <windows.h>
5 #include "scalar_types.h" // (U)INT8/16/32
6 #include "Uart.h"
8 // COMx for windows, returns NULL on error
9 tUartHandle UartOpen(char* szPortName)
11 HANDLE serial_handle;
12 DCB dcb;
13 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 };
15 memset(&dcb,0,sizeof(dcb));
17 /* -------------------------------------------------------------------- */
18 // set DCB to configure the serial port
19 dcb.DCBlength = sizeof(dcb);
21 dcb.fOutxCtsFlow = 0;
22 dcb.fOutxDsrFlow = 0;
23 dcb.fDtrControl = DTR_CONTROL_ENABLE; // enable for power
24 dcb.fDsrSensitivity = 0;
25 dcb.fRtsControl = RTS_CONTROL_ENABLE; // enable for power
26 dcb.fOutX = 0;
27 dcb.fInX = 0;
29 /* ----------------- misc parameters ----- */
30 dcb.fErrorChar = 0;
31 dcb.fBinary = 1;
32 dcb.fNull = 0;
33 dcb.fAbortOnError = 0;
34 dcb.wReserved = 0;
35 dcb.XonLim = 2;
36 dcb.XoffLim = 4;
37 dcb.XonChar = 0x13;
38 dcb.XoffChar = 0x19;
39 dcb.EvtChar = 0;
41 /* ----------------- defaults ----- */
42 dcb.BaudRate = 4800;
43 dcb.Parity = NOPARITY;
44 dcb.fParity = 0;
45 dcb.StopBits = ONESTOPBIT;
46 dcb.ByteSize = 8;
49 /* -------------------------------------------------------------------- */
50 // opening serial port
51 serial_handle = CreateFile(szPortName, GENERIC_READ | GENERIC_WRITE,
52 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
54 if (serial_handle == INVALID_HANDLE_VALUE)
56 //printf("Cannot open port \n");
57 return NULL;
60 SetCommMask(serial_handle, 0);
61 SetCommTimeouts(serial_handle, &cto);
63 if(!SetCommState(serial_handle, &dcb))
65 //printf("Error setting up COM params\n");
66 CloseHandle(serial_handle);
67 return NULL;
70 return serial_handle;
73 // returns true on success, false on error
74 bool UartConfig(tUartHandle handle, long lBaudRate, tParity nParity, tStopBits nStopBits, int nByteSize)
76 DCB dcb;
78 if (!GetCommState (handle, &dcb))
80 return false;
83 dcb.BaudRate = lBaudRate;
84 dcb.Parity = nParity;
85 dcb.StopBits = nStopBits;
86 dcb.ByteSize = nByteSize;
88 if(!SetCommState(handle, &dcb))
90 //DWORD dwErr = GetLastError();
91 //printf("Error %d setting up COM params for baudrate byte\n", dwErr);
92 return false;
95 return true;
98 // returns how much data was actually transmitted
99 long UartWrite(tUartHandle handle, unsigned char* pData, long lSize)
101 BOOL success;
102 DWORD result_nbr;
104 success = WriteFile(handle, pData, lSize, &result_nbr, NULL);
106 if(!success)
108 return 0;
111 return result_nbr;
114 // returns how much data was actually received
115 long UartRead(tUartHandle handle, unsigned char* pBuffer, long lSize)
117 BOOL success;
118 DWORD read_nbr;
120 success = ReadFile(handle, pBuffer, lSize, &read_nbr, NULL);
121 if(!success)
123 return 0;
126 return read_nbr;
130 void UartClose(tUartHandle handle)
132 if (handle != NULL)
134 CloseHandle(handle);
137 return;