intx uart_read( TiUartAdapter * uart, char * buf, intx size, uint8 opt ) { #ifdef CONFIG_UART_INTERRUPT_DRIVEN intx copied=0; int8 count =0; hal_enter_critical(); copied = min( uart->rxlen, size ); if (copied > 0) { memmove( (void *)buf, (void *)&(uart->rxbuf[0]), copied ); uart->rxlen -= copied; if (uart->rxlen > 0) memmove( (void *)&(uart->rxbuf[0]), (void *)&(uart->rxbuf[copied]), uart->rxlen ); } hal_leave_critical(); return copied; #endif #ifndef CONFIG_UART_INTERRUPT_DRIVEN intx ret=0; if (size > 0) { ret = uart_getchar( uart, buf ); } return ret; #endif }
//This writes up to nbytes bytes to the FIFO //If the head runs in to the tail, not all bytes are written //The number of bytes written is returned int fifo_write(fifo_t * f, const void * buf, int nbytes) { int i; const char * p; p = buf; hal_enter_critical(); for (i = 0; i < nbytes; i++) { //first check to see if there is space in the buffer if ( (f->head + 1 == f->tail) || ( (f->head + 1 == f->size) && (f->tail == 0) ) ) return i; //no more room else { f->buf[f->head] = *p++; f->head++; //increment the head if ( f->head == f->size ) //check for wrap-around f->head = 0; } } hal_exit_critical(); return nbytes; }
void uart_close( TiUartAdapter * uart ) { // todo: // you should disable interrutps here // #ifdef CONFIG_UART_INTERRUPT_DRIVEN uart->rxlen = 0; uart->txlen = 0; switch (uart->id) { case 0: //hal_detachhandler( INTNUM_USART1_RX ); //hal_detachhandler( INTNUM_USART1_UDRE ); break; case 1: //hal_detachhandler( INTNUM_USART1_RX ); //hal_detachhandler( INTNUM_USART1_UDRE ); }; #endif } /******************************************************************************* * this function is hardware related * you should change the register in this function * * attention: this function will return immediately. it will not wait for the * incoming data. if there's no arrival data pending in the USART's register, * this function will simply return -1. * * @return * 0 success, *ch is char just read from UART * -1 failed. The value of -1 means the uart object doesn't exists. ******************************************************************************/ intx uart_getchar( TiUartAdapter * uart, char * pc ) { #ifdef CONFIG_UART_INTERRUPT_DRIVEN intx ret = -1; hal_enter_critical(); if (uart->rxlen > 0) { *pc = uart->rxbuf[0]; uart->rxlen --; memmove( (void *)&(uart->rxbuf[0]), (void *)&(uart->rxbuf[1]), uart->rxlen ); ret = 0; } hal_leave_critical(); return ret; #endif #ifndef CONFIG_UART_INTERRUPT_DRIVEN int8 ret=0; char c; BYTE status; switch (uart->id) { case 0: // Turning on reception status = U0CSR; U0CSR |= UART_ENABLE_RECEIVE; while (!URX0IF ); c = U0DBUF; URX0IF = FALSE; // Restoring old state U0CSR = status; *pc = c; ret = 0; break; case 1: break; case 2: break; default: ret = -1; } return ret; #endif }