// ******************** uart.c *************************************** // Risc-Dsp runtime library // // Debug console input/output for MIPS microcontrollers. // Send a byte to the UART transmitter, with interrupts disabled. // // (c) multicore.ru // // ******************************************************************* #include "uart.h" #include "mcom02.h" #include "pll.h" // // @name: uart_getchar // @description: determine uart base offset by id unsigned long uart_base(unsigned int idUart) { unsigned long ulBase = 0; switch (idUart) { case 0: ulBase = UART0_BASE; break; case 1: ulBase = UART1_BASE; break; case 2: ulBase = UART2_BASE; break; case 3: ulBase = UART3_BASE; break; } return ulBase; } // // @name: uart_config // @description: Setup uart port to transfer at given speed and frequency // // @in unsigned int khz - frequency // @in unsigned long baud - data transfer speed // void uart_config(unsigned int idUart, unsigned long baud) { unsigned long baudRatio = 0; unsigned long ulBase = uart_base(idUart); baudRatio = get_l3comm_freq(); baudRatio /= 16; baudRatio /= baud; /* Disable and program some of the baudrate */ *UART_IIR(ulBase) = 0; *UART_IER(ulBase) = 0; *UART_LCR(ulBase) = UART_LCR_DLAB; *UART_DLL(ulBase) = baudRatio & 0xff; *UART_DLH(ulBase) = baudRatio >> 8; /* Program the Mode */ *UART_LCR(ulBase) = 3; // 8 bits, No parity, 1 stop bit. *UART_MSR(ulBase) = 3; /* Enable the Device. */ // *UART_FCR(ulBase) = UART_FCR_FEWO; *UART_FCR(ulBase) = 7; } // // @name: uart_putchar // @description: Send symbol // // @in short c - // void uart_putchar(unsigned int idUart, short c) { unsigned long ulBase = uart_base(idUart); if (0 != ulBase) { while ((*UART_LSR(ulBase) & UART_LSR_THRE) == 0) ; *UART_THR(ulBase) = c; } } // // @name: uart_getchar // @description: Wait for the byte to be received and return it. // @return: unsigned short - received symbol // unsigned short uart_getchar(unsigned int idUart) { unsigned short c = 0; unsigned long ulBase = uart_base(idUart); if (0 != ulBase) { while ((*UART_LSR(ulBase) & UART_LSR_RDR) == 0) /* wait */; c = ((unsigned short)*UART_RBR(ulBase)); } return c; } // // Function: vt_puts // put string to uart port // // Parameters: // const char* str - string // int uart_puts(unsigned int uart_id, const char *str) { do { uart_putchar(uart_id, *str); } while (*str++); return 0; }