/* * This file is part of the libopencm3 project. * * Copyright (C) 2012 Benjamin Vernoux * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library. If not, see . */ #include #include #define UART_SRC_32K 0x00 #define UART_SRC_IRC 0x01 #define UART_SRC_ENET_RX 0x02 #define UART_SRC_ENET_TX 0x03 #define UART_SRC_GP_CLKIN 0x04 #define UART_SRC_XTAL 0x06 #define UART_SRC_PLL0USB 0x07 #define UART_SRC_PLL0AUDIO 0x08 #define UART_SRC_PLL1 0x09 #define UART_SRC_IDIVA 0x0C #define UART_SRC_IDIVB 0x0D #define UART_SRC_IDIVC 0x0E #define UART_SRC_IDIVD 0x0F #define UART_SRC_IDIVE 0x10 #define UART_CGU_AUTOBLOCK_CLOCK_BIT 11 /* clock source selection (5 bits) */ #define UART_CGU_BASE_CLK_SEL_SHIFT 24 uint32_t dummy_read; /* * UART Init function */ void uart_init(uart_num_t uart_num, uart_databit_t data_nb_bits, uart_stopbit_t data_nb_stop, uart_parity_t data_parity, uint16_t uart_divisor, uint8_t uart_divaddval, uint8_t uart_mulval) { uint32_t lcr_config; uint32_t uart_port; uart_port = uart_num; switch (uart_num) { case UART0_NUM: /* use PLL1 as clock source for UART0 */ CGU_BASE_UART0_CLK = (1< 0) { counter++; if (counter >= rx_timeout_nb_cycles) { *error = UART_TIMEOUT_ERROR; return 0; } } } uart_val = (UART_RBR(uart_port) & UART_RBR_MASKBIT); /* Clear error */ *error = UART_NO_ERROR; return uart_val; } /* This Function Wait Data TX Ready, and Write Data to UART if rx_timeout_nb_cycles = 0 Infinite wait */ void uart_write(uart_num_t uart_num, uint8_t data) { uint32_t uart_port; uart_port = uart_num; /* Wait Until FIFO not full */ while ((UART_LSR(uart_port) & UART_LSR_THRE) == 0); UART_THR(uart_port) = data; }