/* Name: sw-uart.c * Project: AVR USB driver for CDC interface on Low-Speed USB * Author: Osamu Tamura * Creation Date: 2006-06-22 * Tabsize: 4 * Copyright: (c) 2006 by Recursion Co., Ltd. * License: Proprietary, free under certain conditions. See Documentation. * * 2006-07-10 software-UART interrupt handling time reduced. */ /* General Description: */ #include #include #include /* needed by usbdrv.h */ #include "oddebug.h" #include "usbdrv.h" #include "uart.h" /* UART buffer */ uchar urptr, uwptr, irptr, iwptr; uchar rx_buf[RX_SIZE], tx_buf[TX_SIZE]; void uartInit(uint baudrate) { PRR = (1<>6)+(baudrate>>1)) / baudrate - 1; OCR1C = 0; RX_DELAY = -((OCR1A+1)>>2); /* 1.25 sample bit */ if( RX_DELAY<=OCR1A ) RX_DELAY = OCR1A + 1; /* for 1200 bps */ TCCR0A = 2; /* CTC */ RX_READY = 1; TIMSK = (1< rs232c : transmit */ if( TCCR0B==0 && uwptr!=irptr ) { uchar data; data = bit_reverse( tx_buf[irptr] ); irptr = (irptr+1) & TX_MASK; TCNT0 = 0; USISR = 0x4b; /* interrupt at D4 */ EEARL = 0; /* usi_phase */ #ifdef UART_INVERT OCR0B = ~((data<<4) | 0x0f); #else OCR0B = ((data<<4) | 0x0f); /* D4-7, stop bit */ #endif data >>= 1; cli(); #ifdef UART_INVERT USIDR = ~data; #else USIDR = data; /* startbit, D0-3 */ #endif TCCR0B = 3; /* start timer0: 1/64 clk */ sei(); /* host => device : accept */ if( usbAllRequestsAreDisabled() && uartTxBytesFree()>=HW_CDC_BULK_OUT_SIZE ) { usbEnableAllRequests(); } } }