diff options
Diffstat (limited to 'flash/uart_boot/uart_win.c')
-rw-r--r-- | flash/uart_boot/uart_win.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/flash/uart_boot/uart_win.c b/flash/uart_boot/uart_win.c new file mode 100644 index 0000000000..243017ac88 --- /dev/null +++ b/flash/uart_boot/uart_win.c | |||
@@ -0,0 +1,138 @@ | |||
1 | // UART wrapper implementation for the Win32 platform | ||
2 | // make a new version of this file for different systems, e.g. Linux | ||
3 | |||
4 | #include <windows.h> | ||
5 | #include "scalar_types.h" // (U)INT8/16/32 | ||
6 | #include "Uart.h" | ||
7 | |||
8 | // COMx for windows, returns NULL on error | ||
9 | tUartHandle UartOpen(char* szPortName) | ||
10 | { | ||
11 | HANDLE serial_handle; | ||
12 | DCB dcb; | ||
13 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 }; | ||
14 | |||
15 | memset(&dcb,0,sizeof(dcb)); | ||
16 | |||
17 | /* -------------------------------------------------------------------- */ | ||
18 | // set DCB to configure the serial port | ||
19 | dcb.DCBlength = sizeof(dcb); | ||
20 | |||
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; | ||
28 | |||
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; | ||
40 | |||
41 | /* ----------------- defaults ----- */ | ||
42 | dcb.BaudRate = 4800; | ||
43 | dcb.Parity = NOPARITY; | ||
44 | dcb.fParity = 0; | ||
45 | dcb.StopBits = ONESTOPBIT; | ||
46 | dcb.ByteSize = 8; | ||
47 | |||
48 | |||
49 | /* -------------------------------------------------------------------- */ | ||
50 | // opening serial port | ||
51 | serial_handle = CreateFile(szPortName, GENERIC_READ | GENERIC_WRITE, | ||
52 | 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); | ||
53 | |||
54 | if (serial_handle == INVALID_HANDLE_VALUE) | ||
55 | { | ||
56 | //printf("Cannot open port \n"); | ||
57 | return NULL; | ||
58 | } | ||
59 | |||
60 | SetCommMask(serial_handle, 0); | ||
61 | SetCommTimeouts(serial_handle, &cto); | ||
62 | |||
63 | if(!SetCommState(serial_handle, &dcb)) | ||
64 | { | ||
65 | //printf("Error setting up COM params\n"); | ||
66 | CloseHandle(serial_handle); | ||
67 | return NULL; | ||
68 | } | ||
69 | |||
70 | return serial_handle; | ||
71 | } | ||
72 | |||
73 | // returns true on success, false on error | ||
74 | bool UartConfig(tUartHandle handle, long lBaudRate, tParity nParity, tStopBits nStopBits, int nByteSize) | ||
75 | { | ||
76 | DCB dcb; | ||
77 | |||
78 | if (!GetCommState (handle, &dcb)) | ||
79 | { | ||
80 | return false; | ||
81 | } | ||
82 | |||
83 | dcb.BaudRate = lBaudRate; | ||
84 | dcb.Parity = nParity; | ||
85 | dcb.StopBits = nStopBits; | ||
86 | dcb.ByteSize = nByteSize; | ||
87 | |||
88 | if(!SetCommState(handle, &dcb)) | ||
89 | { | ||
90 | //DWORD dwErr = GetLastError(); | ||
91 | //printf("Error %d setting up COM params for baudrate byte\n", dwErr); | ||
92 | return false; | ||
93 | } | ||
94 | |||
95 | return true; | ||
96 | } | ||
97 | |||
98 | // returns how much data was actually transmitted | ||
99 | long UartWrite(tUartHandle handle, unsigned char* pData, long lSize) | ||
100 | { | ||
101 | BOOL success; | ||
102 | DWORD result_nbr; | ||
103 | |||
104 | success = WriteFile(handle, pData, lSize, &result_nbr, NULL); | ||
105 | |||
106 | if(!success) | ||
107 | { | ||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | return result_nbr; | ||
112 | } | ||
113 | |||
114 | // returns how much data was actually received | ||
115 | long UartRead(tUartHandle handle, unsigned char* pBuffer, long lSize) | ||
116 | { | ||
117 | BOOL success; | ||
118 | DWORD read_nbr; | ||
119 | |||
120 | success = ReadFile(handle, pBuffer, lSize, &read_nbr, NULL); | ||
121 | if(!success) | ||
122 | { | ||
123 | return 0; | ||
124 | } | ||
125 | |||
126 | return read_nbr; | ||
127 | } | ||
128 | |||
129 | |||
130 | void UartClose(tUartHandle handle) | ||
131 | { | ||
132 | if (handle != NULL) | ||
133 | { | ||
134 | CloseHandle(handle); | ||
135 | } | ||
136 | |||
137 | return; | ||
138 | } | ||