RBAPI(bool) com_Send(int com, unsigned char* buf, int bsize) { unsigned long nowtime; int numbytes = 0, bsize2; if (COM_duplex[com] == COM_HDUPLEX_RTS) { MPOS_Start(); set_rts(com); } while (bsize > 0) { bsize2 = (bsize <= MAXRWSIZE)? bsize : MAXRWSIZE; for (nowtime = timer_nowtime(); bsize2 > 0; buf += numbytes, bsize2 -= numbytes, bsize -= numbytes) { #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) if (WriteFile(COM_info[com].fp, buf, bsize2, (LPDWORD)&numbytes, NULL) == FALSE) { err_SetMsg(ERROR_COM_SENDFAIL, "WriteFile() fails"); goto SEND_FAIL; } #elif defined(RB_LINUX) if ((numbytes = write(COM_info[com].fp, buf, bsize2)) < 0) { err_SetMsg(ERROR_COM_SENDFAIL, "write() fails"); goto SEND_FAIL; } #else // TODO ... err_SetMsg(ERROR_COM_INVALID, "unsupported platform"); goto SEND_FAIL; #endif if ((timer_nowtime() - nowtime) > COM_TIMEOUT) { err_SetMsg(ERROR_COM_SENDFAIL, "time-out to write bytes"); goto SEND_FAIL; } } // for (nowtime... } // end while (bsize... if (COM_duplex[com] == COM_HDUPLEX_RTS) { com_FlushWFIFO(com); clear_rts(com); MPOS_End(); } return true; SEND_FAIL: if (COM_duplex[com] == COM_HDUPLEX_RTS) { clear_rts(com); MPOS_End(); } return false; }
int uart_Read(Fd_t fd, unsigned char *pBuff, int len) { /* Disable interrupt to protect reorder of bytes */ __disable_interrupt(); puartFlowctrl->pActiveBuffer = pBuff; puartFlowctrl->bActiveBufferIsJitterOne = FALSE; puartFlowctrl->ActiveBufferWriteCounter = 0; /* Copy data received in Jitter buffer to the user buffer */ while(puartFlowctrl->JitterBufferFreeBytes != UART_READ_JITTER_BUFFER_SIZE) { if(puartFlowctrl->JitterBufferReadIdx == (UART_READ_JITTER_BUFFER_SIZE - 1)) { puartFlowctrl->JitterBufferReadIdx = 0; } else { puartFlowctrl->JitterBufferReadIdx++; } puartFlowctrl->pActiveBuffer[puartFlowctrl->ActiveBufferWriteCounter++] = puartFlowctrl->JitterBuffer[puartFlowctrl->JitterBufferReadIdx]; puartFlowctrl->JitterBufferFreeBytes ++; } puartFlowctrl->bRtsSetByFlowControl = FALSE; __enable_interrupt(); clear_rts(); /* wait till all remaining bytes are received */ while(puartFlowctrl->ActiveBufferWriteCounter < len); puartFlowctrl->bActiveBufferIsJitterOne = TRUE; return len; }
Fd_t uart_Open(char *ifName, unsigned long flags) { unsigned char i; IntIsMasked = FALSE; puartFlowctrl->JitterBufferFreeBytes = UART_READ_JITTER_BUFFER_SIZE; puartFlowctrl->JitterBufferWriteIdx = 0; puartFlowctrl->pActiveBuffer = puartFlowctrl->JitterBuffer; puartFlowctrl->bActiveBufferIsJitterOne = TRUE; puartFlowctrl->JitterBufferReadIdx = 0xFF; for(i = 0; i < UART_READ_JITTER_BUFFER_SIZE; i++) { puartFlowctrl->JitterBuffer[i] = 0xCC; } /* P1.6 - WLAN enable full DS */ P1SEL &= ~BIT6; P1OUT &= ~BIT6; P1DIR |= BIT6; /* Configure Host IRQ line on P2.0 */ P2DIR &= ~BIT0; P2SEL &= ~BIT0; P2REN |= BIT0; /* Configure Pin 3.3/3.4 for RX/TX */ P3SEL |= BIT3 + BIT4; /* P4.4,5 = USCI_A0 TXD/RXD */ UCA0CTL1 |= UCSWRST; /* Put state machine in reset */ UCA0CTL0 = 0x00; UCA0CTL1 = UCSSEL__SMCLK + UCSWRST; /* Use SMCLK, keep RESET */ UCA0BR0 = 0xD9; /* 25MHz/115200= 217.01 =0xD9 (see User's Guide) */ UCA0BR1 = 0x00; /* 25MHz/9600= 2604 =0xA2C (see User's Guide) */ UCA0MCTL = UCBRS_3 + UCBRF_0; /* Modulation UCBRSx=3, UCBRFx=0 */ UCA0CTL1 &= ~UCSWRST; /* Initialize USCI state machine */ /* Enable RX Interrupt on UART */ UCA0IFG &= ~ (UCRXIFG | UCRXIFG); UCA0IE |= UCRXIE; /* Configure Pin 1.3 and 1.4 as RTS as CTS */ P1SEL &= ~(BIT3 + BIT4); P1OUT &= ~BIT4; P1DIR |= BIT4; P1DIR &= ~BIT3; P1REN |= BIT3; /* 50 ms delay */ Delay(50); /* Enable WLAN interrupt */ CC3100_InterruptEnable(); clear_rts(); return NONOS_RET_OK; }
RBAPI(bool) com_Init(int com, int duplex) { if (com_InUse(com) == true) { err_SetMsg(ERROR_COM_INUSE, "COM%d was already opened", com); return false; } #ifdef ROBOIO duplex = (duplex == COM_ADUPLEX)? COM_FDUPLEX : duplex; switch (roboio_GetRBVer()) { case RB_100b1: switch (com) { case COM_PORT1: COM_duplex[com] = (duplex != COM_HDUPLEX_TXDEN)? duplex : COM_HDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX_RTS; break; } break; case RB_100b2: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX_RTS; break; } break; case RB_100b3: switch (com) { case COM_PORT1: COM_duplex[com] = (duplex != COM_HDUPLEX_TXDEN)? duplex : COM_HDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX; break; } break; case RB_100: case RB_100RD: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = COM_HDUPLEX; break; } break; case RB_110: case RB_050: switch (com) { case COM_PORT1: COM_duplex[com] = COM_FDUPLEX; break; case COM_PORT2: COM_duplex[com] = COM_HDUPLEX_TXDEN; break; case COM_PORT3: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; case COM_PORT4: COM_duplex[com] = (duplex == COM_FDUPLEX)? duplex : COM_HDUPLEX; break; } break; default: err_SetMsg(ERROR_RBVER_UNKNOWN, "unrecognized RoBoard"); return false; } #else COM_duplex[com] = duplex; #endif if((COM_ioSection[com] = io_Init()) == -1) return false; if(uart_isenabled(com) == false) { err_SetMsg(ERROR_COM_INVALID, "COM%d isn't enabled in BIOS", com); goto COMINIT_FAIL; } COM_baseaddr[com] = uart_getbaseaddr(com); COM_oldTMode[com] = com_IsTurboMode(com); COM_oldFMode[com] = com_IsFIFO32Mode(com); #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) { #ifdef RB_MSVC_WINCE int idx = com; #else int i, idx; // find the device name of the COM port for (idx=0, i=0; i<com; i++) if (uart_isenabled(i) == true) idx++; #endif COM_info[com].fp = CreateFile( COM_portname[idx], // device name of COM port GENERIC_READ | GENERIC_WRITE, // access mode 0, // share mode 0, // security attributes OPEN_EXISTING, // opens a device only if it exists 0, // non-overlapped NULL); // NULL when opening an existing file if (COM_info[com].fp == INVALID_HANDLE_VALUE) { err_SetMsg(ERROR_COM_FAIL, "cannot open COM%d device driver", com); goto COMINIT_FAIL; } // backup the old DCB if (GetCommState(COM_info[com].fp, &(COM_info[com].oldstate)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to get DCB settings"); goto COMINIT_FAIL2; } memcpy(&(COM_info[com].newstate), &(COM_info[com].oldstate), sizeof(DCB)); // set new DCB COM_info[com].newstate.fBinary = TRUE; // binary mode COM_info[com].newstate.fOutxCtsFlow = FALSE; // no CTS output control COM_info[com].newstate.fOutxDsrFlow = FALSE; // no DSR output control COM_info[com].newstate.fDtrControl = DTR_CONTROL_DISABLE; // no DRT control COM_info[com].newstate.fDsrSensitivity = FALSE; // no sensitive to DSR COM_info[com].newstate.fOutX = FALSE; // no S/W output flow control COM_info[com].newstate.fInX = FALSE; // no S/W input flow control COM_info[com].newstate.fErrorChar = FALSE; // no replace parity-error byte COM_info[com].newstate.fNull = FALSE; // no discard NULL byte COM_info[com].newstate.fRtsControl = RTS_CONTROL_DISABLE; // no S/W input flow control COM_info[com].newstate.fAbortOnError = FALSE; // no terminate on errors if (SetCommState(COM_info[com].fp, &(COM_info[com].newstate)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to set DCB settings"); goto COMINIT_FAIL2; } // get old timeout parameters if (GetCommTimeouts(COM_info[com].fp, &(COM_info[com].oldtimeouts)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to get TIMEOUTS settings"); goto COMINIT_FAIL3; } // set timeout parameters (no waiting on read/write) COM_info[com].newtimeouts.ReadIntervalTimeout = MAXDWORD; COM_info[com].newtimeouts.ReadTotalTimeoutConstant = 0; COM_info[com].newtimeouts.ReadTotalTimeoutMultiplier = 0; COM_info[com].newtimeouts.WriteTotalTimeoutConstant = 0; COM_info[com].newtimeouts.WriteTotalTimeoutMultiplier = 0; if (SetCommTimeouts(COM_info[com].fp, &(COM_info[com].newtimeouts)) == FALSE) { err_SetMsg(ERROR_COM_FAIL, "fail to set TIMEOUT parameters"); goto COMINIT_FAIL3; } ClearCommBreak(COM_info[com].fp); ClearCommError(COM_info[com].fp, NULL, NULL); // clear all communication errors SetupComm(COM_info[com].fp, 8192, 8192); // set read/write FIFO to 8KB PurgeComm(COM_info[com].fp, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); // clear all communication buffers } #elif defined(RB_LINUX) if ((COM_info[com].fp = open(COM_portname[com], O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) { err_SetMsg(ERROR_COM_FAIL, "cannot open COM%d device driver", com); goto COMINIT_FAIL; } // backup the old termios settings if (tcgetattr(COM_info[com].fp, &(COM_info[com].oldstate)) < 0) { err_SetMsg(ERROR_COM_FAIL, "fail to get termios settings"); goto COMINIT_FAIL2; } memcpy(&(COM_info[com].newstate), &(COM_info[com].oldstate), sizeof(termios)); // set new termios settings COM_info[com].newstate.c_cflag |= CLOCAL | CREAD; COM_info[com].newstate.c_cflag &= ~CRTSCTS; // disable H/W flow control COM_info[com].newstate.c_lflag &= ~(ICANON | // raw mode ISIG | // disable SIGxxxx signals IEXTEN | // disable extended functions ECHO | ECHOE); // disable all auto-echo functions COM_info[com].newstate.c_iflag &= ~(IXON | IXOFF | IXANY); // disable S/W flow control COM_info[com].newstate.c_oflag &= ~OPOST; // raw output COM_info[com].newstate.c_cc[VTIME] = 0; // no waiting to read COM_info[com].newstate.c_cc[VMIN] = 0; if(tcsetattr(COM_info[com].fp, TCSANOW, &(COM_info[com].newstate)) < 0) { err_SetMsg(ERROR_COM_FAIL, "fail to set termios settings"); goto COMINIT_FAIL2; } // clear input/output buffers tcflush(COM_info[com].fp, TCIOFLUSH); #else // TODO ... err_SetMsg(ERROR_COM_INVALID, "unsupported platform"); goto COMINIT_FAIL; #endif if (COM_duplex[com] == COM_HDUPLEX_RTS) clear_rts(com); // set COM direction as input com_SetFormat(com, COM_BYTESIZE8, COM_STOPBIT1, COM_NOPARITY); // default data format: 8 bits, 1 stop bit, no parity com_SetBaud(com, COMBAUD_115200BPS); // default baudrate: 115200 bps com_EnableFIFO32(com); // set Vortex86DX's UART FIFO as 32 bytes return true; #if defined(RB_MSVC_WIN32) || defined(RB_MSVC_WINCE) COMINIT_FAIL3: SetCommState(COM_info[com].fp, &(COM_info[com].oldstate)); COMINIT_FAIL2: CloseHandle(COM_info[com].fp); #elif defined(RB_LINUX) COMINIT_FAIL2: close(COM_info[com].fp); #endif COMINIT_FAIL: io_Close(COM_ioSection[com]); COM_ioSection[com] = -1; return false; }