3 This file is part of the OpenARMWare.
4 Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "hw_uart_regs.h"
24 #include "uart_defines.h"
25 #include "circularbytebuffer.h"
29 uint32_t uart_base[] = { UART0_BASE, UART1_BASE, UART2_BASE };
32 uint32_t gpio_base[] =
33 { GPIOA_BASE, GPIOB_BASE, GPIOC_BASE, GPIOD_BASE,
34 GPIOE_BASE, GPIOF_BASE, GPIOG_BASE, GPIOH_BASE,
39 uint8_t uart_tx_gpio[] = { GPIOA, GPIOD, GPIOG };
41 uint8_t uart_rx_gpio[] = { GPIOA, GPIOD, GPIOG };
43 uint8_t uart_tx_pin[] = { 1, 1, 1 };
45 uint8_t uart_rx_pin[] = { 0, 0, 0 };
47 uint8_t uart_tx_pctl[] = {1, 5, 1};
49 uint8_t uart_rx_pctl[] = {1, 5, 1};
51 uint8_t uart_isr_vector[] = {5, 6, 33};
54 uint32_t uart_rx_buffersize[] = {128, 128, 128};
56 uint32_t uart_tx_buffersize[] = {256, 256, 256};
59 circularbytebuffer_t uart_rx_buffer[3];
61 circularbytebuffer_t uart_tx_buffer[3];
65 void uart_tx_isr(uint8_t uartno);
67 void uart_rx_isr(uint8_t uartno);
71 if(HW_REG(UART0_BASE+UARTMIS_OFFSET)&_BV(UART_TXMIS)){
72 // HW_REG(uart_base[0]+UARTDR_OFFSET) = 'X';
75 if(HW_REG(UART0_BASE+UARTMIS_OFFSET)&_BV(UART_RXMIS)){
81 void uart_tx_isr(uint8_t uartno){
83 tmp = circularbytebuffer_cnt(&(uart_tx_buffer[uartno]));
84 while(tmp-- && (!(HW_REG(uart_base[uartno]+UARTFR_OFFSET)&_BV(UART_TXFF)))){
85 HW_REG(uart_base[uartno]+UARTDR_OFFSET)
86 = (uint32_t)circularbytebuffer_get_fifo(&(uart_tx_buffer[uartno]));
88 HW_REG(uart_base[uartno]+UARTICR_OFFSET) |= _BV(UART_TXIC);
92 void uart_rx_isr(uint8_t uartno){
94 while(!HW_REG(uart_base[uartno]+UARTFR_OFFSET)&_BV(UART_RXFE)){
95 c = HW_REG(uart_base[uartno]+UARTDR_OFFSET);
96 circularbytebuffer_append(c, &(uart_rx_buffer[uartno]));
98 HW_REG(uart_base[uartno]+UARTICR_OFFSET) |= _BV(UART_RXIC);
101 void calc_baud_values(uint32_t baudrate, uint16_t* intdivider, uint8_t* fracdivider, uint8_t* highspeed){
104 uart_freq = sysclk_get_freq();
105 *highspeed = (baudrate*16>uart_freq)?1:0;
106 tmp = (uint64_t)uart_freq*128/((*highspeed?8:16)*baudrate);
109 *fracdivider = (uint8_t)(tmp&0x3f);
110 *intdivider = tmp>>6;
113 uint8_t uart_init(uint8_t uartno, uint32_t baudrate, uint8_t databits, uint8_t paraty, uint8_t stopbits){
119 return UART_ERROR_WRONG_UART;
121 if(databits>UART_DATABITS_8){
122 return UART_ERROR_WRONG_DATABITS;
124 if(paraty>UART_PARATY_SPACE){
125 return UART_ERROR_WRONG_PARATY;
127 if(stopbits>UART_STOPBITS_TWO){
128 return UART_ERROR_WRONG_STOPBITS;
130 if(0==circularbytebuffer_init(uart_rx_buffersize[uartno], &(uart_rx_buffer[uartno]))){
131 return UART_ERROR_RX_BUFFER_INIT;
133 if(0==circularbytebuffer_init(uart_tx_buffersize[uartno], &(uart_tx_buffer[uartno]))){
134 return UART_ERROR_TX_BUFFER_INIT;
136 /* enable clock for uart */
137 HW_REG(SYSCTL_BASE+RCGC1_OFFSET) |= _BV(uartno);
138 /* enable clock for gpio*/
139 HW_REG(SYSCTL_BASE+RCGC2_OFFSET) |= _BV(uart_rx_gpio[uartno]) | _BV(uart_tx_gpio[uartno]);
140 HW_REG(SYSCTL_BASE+RCGC2_OFFSET) |= 1;
142 HW_REG(gpio_base[uart_rx_gpio[uartno]] + GPIO_ODR_OFFSET) &= ~_BV(uart_rx_pin[uartno]); /* open drain */
143 HW_REG(gpio_base[uart_tx_gpio[uartno]] + GPIO_ODR_OFFSET) &= ~_BV(uart_tx_pin[uartno]); /* open drain */
144 HW_REG(gpio_base[uart_rx_gpio[uartno]] + GPIO_PUR_OFFSET) &= ~_BV(uart_rx_pin[uartno]); /* pull-up */
145 HW_REG(gpio_base[uart_tx_gpio[uartno]] + GPIO_PUR_OFFSET) &= ~_BV(uart_tx_pin[uartno]); /* pull-up */
146 HW_REG(gpio_base[uart_rx_gpio[uartno]] + GPIO_PDR_OFFSET) &= ~_BV(uart_rx_pin[uartno]); /* pull-down*/
147 HW_REG(gpio_base[uart_tx_gpio[uartno]] + GPIO_PDR_OFFSET) &= ~_BV(uart_tx_pin[uartno]); /* pull-down*/
148 HW_REG(gpio_base[uart_rx_gpio[uartno]] + GPIO_DEN_OFFSET) |= _BV(uart_rx_pin[uartno]); /* digital enable */
149 HW_REG(gpio_base[uart_tx_gpio[uartno]] + GPIO_DEN_OFFSET) |= _BV(uart_tx_pin[uartno]); /* digital enable */
151 /* switch to alternate function for rx */
152 HW_REG(gpio_base[uart_rx_gpio[uartno]]+GPIO_AFSEL_OFFSET) |= _BV(uart_rx_pin[uartno]);
153 /* switch to alternate function for tx */
154 HW_REG(gpio_base[uart_tx_gpio[uartno]]+GPIO_AFSEL_OFFSET) |= _BV(uart_tx_pin[uartno]);
155 /* switch multiplexer to uart for rx */
156 HW_REG(gpio_base[uart_rx_gpio[uartno]]+GPIO_PCTL_OFFSET) &= ~(0x0f<<(uart_rx_pin[uartno]*4));
157 HW_REG(gpio_base[uart_rx_gpio[uartno]]+GPIO_PCTL_OFFSET) |= ((uart_rx_pctl[uartno])<<(uart_rx_pin[uartno]*4));
158 /* switch multiplexer to uart for tx */
159 HW_REG(gpio_base[uart_tx_gpio[uartno]]+GPIO_PCTL_OFFSET) &= ~(0x0f<<(uart_tx_pin[uartno]*4));
160 HW_REG(gpio_base[uart_tx_gpio[uartno]]+GPIO_PCTL_OFFSET) |= ((uart_tx_pctl[uartno])<<(uart_tx_pin[uartno]*4));
161 /* set pins to be 2mA */
162 HW_REG(gpio_base[uart_rx_gpio[uartno]]+GPIO_DR2R_OFFSET) |= _BV(uart_rx_pin[uartno]);
163 HW_REG(gpio_base[uart_tx_gpio[uartno]]+GPIO_DR2R_OFFSET) |= _BV(uart_tx_pin[uartno]);
164 /* configure rx pin as input */
165 HW_REG(gpio_base[uart_rx_gpio[uartno]]+GPIO_DIR_OFFSET) &= ~_BV(uart_rx_pin[uartno]);
166 /* configure tx pin as output */
167 HW_REG(gpio_base[uart_tx_gpio[uartno]]+GPIO_DIR_OFFSET) |= _BV(uart_tx_pin[uartno]);
170 HW_REG(uart_base[uartno]+UARTCTL_OFFSET) &= ~_BV(UART_UARTEN);
171 /* set baudrate parameters */
173 calc_baud_values(baudrate,
174 (uint16_t*)&HW_REG(uart_base[uartno]+UARTIBRD_OFFSET),
175 (uint8_t*)&HW_REG(uart_base[uartno]+UARTFBRD_OFFSET),
177 /* wait until uart is no longer busy */
178 while(HW_REG(uart_base[uartno]+UARTFR_OFFSET)&_BV(UART_BUSY))
181 HW_REG(uart_base[uartno]+UARTLCRH_OFFSET) &= ~_BV(UART_FEN);
182 /* set line parameters (bits, paraty, stopbits*/
183 tmp = HW_REG(uart_base[uartno]+UARTLCRH_OFFSET);
185 tmp |= (paraty==UART_PARATY_MARK||paraty==UART_PARATY_SPACE)?_BV(7):0; /* set flag for mark or space paraty*/
187 tmp |= _BV(UART_FEN); /* enable FIFOs */
188 tmp |= (stopbits==UART_STOPBITS_TWO)?_BV(3):0;
189 tmp |= (paraty==UART_PARATY_EVEN || paraty==UART_PARATY_MARK)?_BV(2):0;
190 tmp |= (paraty!=UART_PARATY_NONE)?_BV(1):0;
191 HW_REG(uart_base[uartno]+UARTLCRH_OFFSET) = tmp;
192 /* set the highspeed bit accordingly */
194 HW_REG(uart_base[uartno]+UARTCTL_OFFSET) |= _BV(UART_HSE);
196 HW_REG(uart_base[uartno]+UARTCTL_OFFSET) &= ~_BV(UART_HSE);
198 /* uart interrupt enable */
199 HW_REG(uart_base[uartno]+UARTIM_OFFSET) |= _BV(UART_TXIM) | _BV(UART_RXIM);
200 HW_REG(ISR_ENABLE_VECTOR+uart_isr_vector[uartno]/32) |=
201 _BV(uart_isr_vector[uartno]%32);
203 HW_REG(uart_base[uartno]+UARTCTL_OFFSET) |= _BV(UART_EOT);
204 HW_REG(uart_base[uartno]+UARTFR_OFFSET) = 0;
205 HW_REG(uart_base[uartno]+UARTCTL_OFFSET) |= _BV(UART_RXE) | _BV(UART_TXE);
206 HW_REG(uart_base[uartno]+UARTCTL_OFFSET) |= _BV(UART_UARTEN);
208 return UART_ERROR_OK;
212 void uart_putc(uint8_t uartno, uint8_t byte){
216 /* wait while buffer is full */
217 while(circularbytebuffer_cnt(&(uart_tx_buffer[uartno]))==uart_tx_buffersize[uartno]){
219 if(circularbytebuffer_cnt(&(uart_tx_buffer[uartno]))>0){
220 circularbytebuffer_append(byte, &(uart_tx_buffer[uartno]));
223 /* if we have a full uart, append to buffer */
224 if(HW_REG(uart_base[uartno]+UARTFR_OFFSET)&_BV(UART_TXFF)){
225 circularbytebuffer_append(byte, &(uart_tx_buffer[uartno]));
228 HW_REG(uart_base[uartno]+UARTDR_OFFSET) = (uint32_t)byte;
231 uint16_t uart_getc(uint8_t uartno){
235 if(circularbytebuffer_cnt(&(uart_rx_buffer[uartno]))){
236 return circularbytebuffer_get_fifo(&(uart_rx_buffer[uartno]));
238 /* wait while the FIFO is empty */
239 while(HW_REG(uart_base[uartno]+UARTFR_OFFSET)&_BV(UART_RXFE))
241 return (uint16_t)HW_REG(uart_base[uartno]+UARTDR_OFFSET);
244 uint32_t uart_dataavail(uint8_t uartno){
248 /* wait while the FIFO is empty */
249 if(circularbytebuffer_cnt(&(uart_rx_buffer[uartno]))){
252 return(HW_REG(uart_base[uartno]+UARTFR_OFFSET)&_BV(UART_RXFE))?0:1;