CHG: adjustments to the USB reading part.

This commit is contained in:
iceman1001 2017-10-05 15:55:08 +02:00
parent a4b4a1a9a2
commit b4a03581c2
4 changed files with 56 additions and 21 deletions

View file

@ -1738,7 +1738,7 @@ typedef struct _AT91S_UDP {
#define AT91C_UDP_EPEDS (0x1 << 15) // (UDP) Endpoint Enable Disable
#define AT91C_UDP_RXBYTECNT (0x7FF << 16) // (UDP) Number Of Bytes Available in the FIFO
// -------- UDP_TXVC : (UDP Offset: 0x74) Transceiver Control Register --------
#define AT91C_UDP_TXVDIS (0x1 << 8) // (UDP)
#define AT91C_UDP_TXVDIS (0x1 << 8) // (UDP) Transceiver Disable
// *****************************************************************************
// REGISTER ADDRESS DEFINITION FOR AT91SAM7S512

View file

@ -81,7 +81,10 @@
#define LED_D_INV() INVBIT(GPIO_LED_D)
#define RELAY_ON() HIGH(GPIO_RELAY)
#define RELAY_OFF() LOW(GPIO_RELAY)
#define BUTTON_PRESS() !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_BUTTON)
#define BUTTON_PRESS() !((AT91C_BASE_PIOA->PIO_PDSR & GPIO_BUTTON) == GPIO_BUTTON)
//NVDD goes LOW when USB is attached.
#define USB_ATTACHED() !((AT91C_BASE_PIOA->PIO_PDSR & GPIO_NVDD_ON) == GPIO_NVDD_ON)
#define VERSION_INFORMATION_MAGIC 0x56334d50
struct version_information {

View file

@ -202,6 +202,7 @@ typedef struct{
#define CMD_MIFARE_CHKKEYS 0x0623
#define CMD_MIFARE_SETMOD 0x0624
#define CMD_MIFARE_CHKKEYS_FAST 0x0625
#define CMD_MIFARE_SNIFFER 0x0630
//ultralightC

View file

@ -61,6 +61,9 @@ serial_port uart_open(const char* pcPortName) {
char acPortName[255];
serial_port_windows* sp = malloc(sizeof(serial_port_windows));
if (sp == 0)
return INVALID_SERIAL_PORT;
// Copy the input "com?" to "\\.\COM?" format
sprintf(acPortName,"\\\\.\\%s", pcPortName);
upcase(acPortName);
@ -86,6 +89,7 @@ serial_port uart_open(const char* pcPortName) {
return INVALID_SERIAL_PORT;
}
// all zero's configure: no timeout for read/write used.
// took settings from libnfc/buses/uart.c
sp->ct.ReadIntervalTimeout = 0;//1;
sp->ct.ReadTotalTimeoutMultiplier = 0;//1;
sp->ct.ReadTotalTimeoutConstant = 30;
@ -107,38 +111,65 @@ serial_port uart_open(const char* pcPortName) {
}
void uart_close(const serial_port sp) {
if (!sp) return;
if (sp == INVALID_SERIAL_PORT) return;
if (sp == CLAIMED_SERIAL_PORT) return;
if (((serial_port_windows*)sp)->hPort != NULL )
if (!sp) return;
if (sp == INVALID_SERIAL_PORT) return;
if (sp == CLAIMED_SERIAL_PORT) return;
if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE )
CloseHandle(((serial_port_windows*)sp)->hPort);
free(sp);
}
bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
return (*pszRxLen != 0);
//ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
//return (*pszRxLen != 0);
DWORD dwBytesToGet = (DWORD)pszMaxRxLen;
DWORD dwBytesReceived = 0;
DWORD dwTotalBytesReceived = 0;
BOOL res;
do {
res = ReadFile(((serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived, dwBytesToGet, &dwBytesReceived, NULL);
dwTotalBytesReceived += dwBytesReceived;
if (!res)
return false;
else if (dwBytesReceived == 0)
return false;
if (((DWORD)pszMaxRxLen) > dwTotalBytesReceived)
dwBytesToGet -= dwBytesReceived;
} while (((DWORD)pszMaxRxLen) > dwTotalBytesReceived);
return (dwTotalBytesReceived == (DWORD) pszMaxRxLen);
}
bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) {
DWORD dwTxLen = 0;
return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
return (dwTxLen != 0);
DWORD dwTxLen = 0;
if ( !WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL) ) {
return false;
}
return (dwTxLen != 0);
}
bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
serial_port_windows* spw;
spw = (serial_port_windows*)sp;
spw->dcb.BaudRate = uiPortSpeed;
return SetCommState(spw->hPort, &spw->dcb);
serial_port_windows* spw;
spw = (serial_port_windows*)sp;
spw->dcb.BaudRate = uiPortSpeed;
bool result = SetCommState(spw->hPort, &spw->dcb);
PurgeComm(spw->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
return result;
}
uint32_t uart_get_speed(const serial_port sp) {
const serial_port_windows* spw = (serial_port_windows*)sp;
if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
return spw->dcb.BaudRate;
}
return 0;
const serial_port_windows* spw = (serial_port_windows*)sp;
if (!GetCommState(spw->hPort, (serial_port) & spw->dcb))
return spw->dcb.BaudRate;
return 0;
}
#endif
#endif