/******************************************************************************************** * File: serial.c * Version: 1.0.0 * Copyright: 2013 (c) Guo Wenxue * Description: The UART on board drivers/functions. * ChangeLog: 1, Release initial version on "Mon Feb 25 22:58:49 CST 2013" * *******************************************************************************************/ #include "s3c6410.h" /* * Initialise the serial port with the given baudrate. The settings * are always 8 data bits, no parity, 1 stop bit, no start bits. */ int serial_init(int baudrate) { unsigned int regv; s3c64xx_uart *const uart = s3c64xx_get_base_uart(S3C64XX_UART0); /* UART I/O port initialize: GPA0->RXD0, GPA1->TXD0) */ regv = s3c_readl(GPACON); regv = (regv & ~(0xff<<0)) | (0x22<<0); s3c_writel(regv, GPACON); /* RXD0: Pull-down, TXD0: pull up/down disable */ regv = s3c_readl(GPAPUD); regv = (regv & ~(0xf<<0)) | (0x1<<0); s3c_writel(regv, GPAPUD); /* Normal Mode, No Parity, 1 Stop Bit, 8 Bit Data */ uart->ULCON = 3; /* PCLK divide, Polling Mode */ uart->UCON = (0x2<<10) | (1<<9) | (1<<8) | (1<<2) | (1<<0); uart->UFCON = 0; /* Disable FIFO */ uart->UMCON = 0; /* Disable Auto Flow Control */ /* Baudrate, DIV=PCLK/(bps*16)-1=66000000/(115200*16)-1 = 34; */ s3c_writel(34, UBRDIV0); for (regv=0; regv<0x100; regv++) ; uart->UDIVSLOT = 0x80; return 0; } /* * Read a single byte from the serial port. Returns 1 on success, 0 * otherwise. When the function is succesfull, the character read is * written into its argument c. */ int serial_getc(void) { s3c64xx_uart *const uart = s3c64xx_get_base_uart(S3C64XX_UART0); /* wait for character to arrive */ while (!(uart->UTRSTAT & 0x1)) ; return uart->URXH & 0xff; } /* * Output a single byte to the serial port. */ void serial_putc(const char c) { s3c64xx_uart *const uart = s3c64xx_get_base_uart(S3C64XX_UART0); /* wait for room in the tx FIFO */ while (!(uart->UTRSTAT & 0x2)); uart->UTXH = c; if (c == '\n') serial_putc('\r'); /* If \n, also do \r */ } /* * Test whether a character is in the RX buffer */ int serial_tstc(void) { s3c64xx_uart *const uart = s3c64xx_get_base_uart(S3C64XX_UART0); return uart->UTRSTAT & 0x1; } void serial_puts(const char *s) { while (*s) serial_putc(*s++); }