diff options
author | Jörg Hohensohn <hohensoh@rockbox.org> | 2003-11-30 11:37:43 +0000 |
---|---|---|
committer | Jörg Hohensohn <hohensoh@rockbox.org> | 2003-11-30 11:37:43 +0000 |
commit | 6a4e4c87c24455e18bbd77565cb3e993ee350618 (patch) | |
tree | 6f2ceac4da97aa63ff8deef939bd3cc2d56d3466 /flash/uart_boot/uart_win.c | |
parent | 014c4fa1f8d56850cfd504a90221c293e4a158f6 (diff) |
source code for all my flash stuff, now finally in cvs
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4083 a1c6a512-1295-4272-9138-f99709370657
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 @@ +// UART wrapper implementation for the Win32 platform +// make a new version of this file for different systems, e.g. Linux + +#include <windows.h> +#include "scalar_types.h" // (U)INT8/16/32 +#include "Uart.h" + +// COMx for windows, returns NULL on error +tUartHandle UartOpen(char* szPortName) +{ + HANDLE serial_handle; + DCB dcb; + COMMTIMEOUTS cto = { 0, 0, 0, 0, 0 }; + + memset(&dcb,0,sizeof(dcb)); + + /* -------------------------------------------------------------------- */ + // set DCB to configure the serial port + dcb.DCBlength = sizeof(dcb); + + dcb.fOutxCtsFlow = 0; + dcb.fOutxDsrFlow = 0; + dcb.fDtrControl = DTR_CONTROL_ENABLE; // enable for power + dcb.fDsrSensitivity = 0; + dcb.fRtsControl = RTS_CONTROL_ENABLE; // enable for power + dcb.fOutX = 0; + dcb.fInX = 0; + + /* ----------------- misc parameters ----- */ + dcb.fErrorChar = 0; + dcb.fBinary = 1; + dcb.fNull = 0; + dcb.fAbortOnError = 0; + dcb.wReserved = 0; + dcb.XonLim = 2; + dcb.XoffLim = 4; + dcb.XonChar = 0x13; + dcb.XoffChar = 0x19; + dcb.EvtChar = 0; + + /* ----------------- defaults ----- */ + dcb.BaudRate = 4800; + dcb.Parity = NOPARITY; + dcb.fParity = 0; + dcb.StopBits = ONESTOPBIT; + dcb.ByteSize = 8; + + + /* -------------------------------------------------------------------- */ + // opening serial port + serial_handle = CreateFile(szPortName, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); + + if (serial_handle == INVALID_HANDLE_VALUE) + { + //printf("Cannot open port \n"); + return NULL; + } + + SetCommMask(serial_handle, 0); + SetCommTimeouts(serial_handle, &cto); + + if(!SetCommState(serial_handle, &dcb)) + { + //printf("Error setting up COM params\n"); + CloseHandle(serial_handle); + return NULL; + } + + return serial_handle; +} + +// returns true on success, false on error +bool UartConfig(tUartHandle handle, long lBaudRate, tParity nParity, tStopBits nStopBits, int nByteSize) +{ + DCB dcb; + + if (!GetCommState (handle, &dcb)) + { + return false; + } + + dcb.BaudRate = lBaudRate; + dcb.Parity = nParity; + dcb.StopBits = nStopBits; + dcb.ByteSize = nByteSize; + + if(!SetCommState(handle, &dcb)) + { + //DWORD dwErr = GetLastError(); + //printf("Error %d setting up COM params for baudrate byte\n", dwErr); + return false; + } + + return true; +} + +// returns how much data was actually transmitted +long UartWrite(tUartHandle handle, unsigned char* pData, long lSize) +{ + BOOL success; + DWORD result_nbr; + + success = WriteFile(handle, pData, lSize, &result_nbr, NULL); + + if(!success) + { + return 0; + } + + return result_nbr; +} + +// returns how much data was actually received +long UartRead(tUartHandle handle, unsigned char* pBuffer, long lSize) +{ + BOOL success; + DWORD read_nbr; + + success = ReadFile(handle, pBuffer, lSize, &read_nbr, NULL); + if(!success) + { + return 0; + } + + return read_nbr; +} + + +void UartClose(tUartHandle handle) +{ + if (handle != NULL) + { + CloseHandle(handle); + } + + return; +} |