void PutUARTByte (const char c) { while (!(UART_READ32 (UART_LSR(g_uart)) & UART_LSR_THRE)) { } if (c == '\n') UART_WRITE32 ((unsigned int) '\r', UART_THR(g_uart)); UART_WRITE32 ((unsigned int) c, UART_THR(g_uart)); }
int GetUARTBytes(u8 *buf, u32 size, u32 tmo_ms) { u32 LSR; int tmo_en = (tmo_ms) ? 1 : 0; ulong start_time = get_timer(0); while (size) { if (tmo_en && (get_timer(start_time) > tmo_ms)) break; /* kick watchdog to avoid cpu reset */ if (!tmo_en) platform_wdt_kick(); LSR = UART_READ32(UART_LSR(g_uart)); if (LSR & UART_LSR_DR) { *buf++ = (u8)UART_READ32(UART_RBR(g_uart)); size--; } } return (0 == size) ? 0 : -1; }
void serial_setbrg() { unsigned int byte,speed; unsigned int highspeed; unsigned int quot, divisor, remainder; //unsigned int ratefix; unsigned int uartclk; //unsigned int highclk = (UART_SRC_CLK/(speed*4)) > 10 ? (1) : 0; unsigned short data, high_speed_div, sample_count, sample_point; unsigned int tmp_div; speed = g_brg; //uartclk = UART_SRC_CLK; uartclk = (unsigned int)(mt6575_get_bus_freq()*1000/4); if (speed <= 115200 ) { highspeed = 0; quot = 16; } else { highspeed = 3; quot = 1; } if (highspeed < 3) { /*0~2*/ /* Set divisor DLL and DLH */ divisor = uartclk / (quot * speed); remainder = uartclk % (quot * speed); if (remainder >= (quot / 2) * speed) divisor += 1; UART_WRITE16(highspeed, UART_HIGHSPEED(g_uart)); byte = UART_READ32(UART_LCR(g_uart)); /* DLAB start */ UART_WRITE32((byte | UART_LCR_DLAB), UART_LCR(g_uart)); UART_WRITE32((divisor & 0x00ff), UART_DLL(g_uart)); UART_WRITE32(((divisor >> 8)&0x00ff), UART_DLH(g_uart)); UART_WRITE32(byte, UART_LCR(g_uart)); /* DLAB end */ } else {
int serial_nonblock_getc(void) { return (int)UART_READ32(UART_RBR(g_uart)); }
void serial_setbrg (U32 uartclk, U32 baudrate) { #if (CFG_FPGA_PLATFORM) #define MAX_SAMPLE_COUNT 256 U16 tmp; U32 divisor; U32 sample_data; U32 sample_count; U32 sample_point; // Setup N81,(UART_WLS_8 | UART_NONE_PARITY | UART_1_STOP) = 0x03 UART_WRITE32(0x0003, UART_LCR(g_uart)); /* * NoteXXX: Below is the sample code to set UART baud rate. * I assume that when system is reset, the UART clock rate is 26MHz * and baud rate is 115200. * use UART1_HIGHSPEED = 0x3 can get more sample count to get better UART sample rate * based on baud_rate = uart clock frequency / (sampe_count * divisor) * divisor = (DLH+DLL) */ // In order to get better UART sample rate, set UART1_HIGHSPEED = 0x3. // And we can calculate sample count for reducing effect of UART sample rate variation UART_WRITE32(0x0003, UART_HIGHSPEED(g_uart)); // calculate sample_data = sample_count*divisor // round off the result for approximating to the real baudrate sample_data = (uartclk+(baudrate/2))/baudrate; // calculate divisor divisor = (sample_data+(MAX_SAMPLE_COUNT-1))/MAX_SAMPLE_COUNT; // calculate sample count sample_count = sample_data/divisor; // calculate sample point (count from 0) sample_point = (sample_count-1)/2; // set sample count (count from 0) UART_WRITE32((sample_count-1), UART_SAMPLE_COUNT(g_uart)); // set sample point UART_WRITE32(sample_point, UART_SAMPLE_POINT(g_uart)); tmp = UART_READ32(UART_LCR(g_uart)); /* DLAB start */ UART_WRITE32((tmp | UART_LCR_DLAB), UART_LCR(g_uart)); UART_WRITE32((divisor&0xFF), UART_DLL(g_uart)); UART_WRITE32(((divisor>>8)&0xFF), UART_DLH(g_uart)); UART_WRITE32(tmp, UART_LCR(g_uart)); #else unsigned int byte; unsigned int highspeed; unsigned int quot, divisor, remainder; unsigned int ratefix; if (baudrate <= 115200 ) { highspeed = 0; quot = 16; } else { highspeed = 2; quot = 4; } /* Set divisor DLL and DLH */ divisor = uartclk / (quot * baudrate); remainder = uartclk % (quot * baudrate); if (remainder >= (quot / 2) * baudrate) divisor += 1; UART_WRITE16(highspeed, UART_HIGHSPEED(g_uart)); byte = UART_READ32(UART_LCR(g_uart)); /* DLAB start */ UART_WRITE32((byte | UART_LCR_DLAB), UART_LCR(g_uart)); UART_WRITE32((divisor & 0x00ff), UART_DLL(g_uart)); UART_WRITE32(((divisor >> 8)&0x00ff), UART_DLH(g_uart)); //UART_WRITE32(byte, UART_LCR(g_uart)); /* DLAB end */ // Setup N81,(UART_WLS_8 | UART_NONE_PARITY | UART_1_STOP) = 0x03 UART_WRITE32(0x0003, UART_LCR(g_uart)); #endif }