1 | /* |
---|
2 | * Copyright (c) 2012 Sebastian Huber. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <libchip/sersupp.h> |
---|
16 | |
---|
17 | #include <bsp.h> |
---|
18 | #include <bsp/io.h> |
---|
19 | #include <bsp/rcc.h> |
---|
20 | #include <bsp/irq.h> |
---|
21 | #include <bsp/usart.h> |
---|
22 | #include <bsp/stm32f4.h> |
---|
23 | |
---|
24 | static volatile stm32f4_usart *usart_get_regs(const console_tbl *ct) |
---|
25 | { |
---|
26 | return (stm32f4_usart *) ct->ulCtrlPort1; |
---|
27 | } |
---|
28 | |
---|
29 | #if 0 |
---|
30 | static rtems_vector_number usart_get_irq_number(const console_tbl *ct) |
---|
31 | { |
---|
32 | return ct->ulIntVector; |
---|
33 | } |
---|
34 | #endif |
---|
35 | |
---|
36 | static const stm32f4_rcc_index usart_rcc_index [] = { |
---|
37 | STM32F4_RCC_USART1, |
---|
38 | STM32F4_RCC_USART2, |
---|
39 | STM32F4_RCC_USART3, |
---|
40 | STM32F4_RCC_UART4, |
---|
41 | STM32F4_RCC_UART5, |
---|
42 | #ifdef STM32F4_FAMILY_F4XXXX |
---|
43 | STM32F4_RCC_USART6 |
---|
44 | #endif /* STM32F4_FAMILY_F4XXXX */ |
---|
45 | }; |
---|
46 | |
---|
47 | static stm32f4_rcc_index usart_get_rcc_index(const console_tbl *ct) |
---|
48 | { |
---|
49 | return usart_rcc_index [ct->ulCtrlPort2]; |
---|
50 | } |
---|
51 | |
---|
52 | static const uint8_t usart_pclk_index [] = { 1, 0, 0, 0, 0, 1 }; |
---|
53 | |
---|
54 | static const uint32_t usart_pclk_by_index [] = { |
---|
55 | STM32F4_PCLK1, |
---|
56 | STM32F4_PCLK2 |
---|
57 | }; |
---|
58 | |
---|
59 | static uint32_t usart_get_pclk(const console_tbl *ct) |
---|
60 | { |
---|
61 | return usart_pclk_by_index [usart_pclk_index [ct->ulCtrlPort2]]; |
---|
62 | } |
---|
63 | |
---|
64 | static uint32_t usart_get_baud(const console_tbl *ct) |
---|
65 | { |
---|
66 | return ct->ulClock; |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | * a = 8 * (2 - CR1[OVER8]) |
---|
71 | * |
---|
72 | * usartdiv = div_mantissa + div_fraction / a |
---|
73 | * |
---|
74 | * baud = pclk / (a * usartdiv) |
---|
75 | * |
---|
76 | * usartdiv = pclk / (a * baud) |
---|
77 | * |
---|
78 | * Calculation in integer arithmetic: |
---|
79 | * |
---|
80 | * 1. div_mantissa = pclk / (a * baud) |
---|
81 | * |
---|
82 | * 2. div_fraction = pclk / (baud - a * div_mantissa) |
---|
83 | */ |
---|
84 | static uint32_t usart_get_bbr( |
---|
85 | volatile stm32f4_usart *usart, |
---|
86 | uint32_t pclk, |
---|
87 | uint32_t baud |
---|
88 | ) |
---|
89 | { |
---|
90 | uint32_t a = 8 * (2 - ((usart->cr1 & STM32F4_USART_CR1_OVER8) != 0)); |
---|
91 | uint32_t div_mantissa_low = pclk / (a * baud); |
---|
92 | uint32_t div_fraction_low = pclk / (baud - a * div_mantissa_low); |
---|
93 | uint32_t div_mantissa_high; |
---|
94 | uint32_t div_fraction_high; |
---|
95 | uint32_t high_err; |
---|
96 | uint32_t low_err; |
---|
97 | uint32_t div_mantissa; |
---|
98 | uint32_t div_fraction; |
---|
99 | |
---|
100 | if (div_fraction_low < a - 1) { |
---|
101 | div_mantissa_high = div_fraction_low; |
---|
102 | div_fraction_high = div_fraction_low + 1; |
---|
103 | } else { |
---|
104 | div_mantissa_high = div_fraction_low + 1; |
---|
105 | div_fraction_high = 0; |
---|
106 | } |
---|
107 | |
---|
108 | high_err = pclk - baud * (a * div_mantissa_high + div_fraction_high); |
---|
109 | low_err = baud * (a * div_mantissa_low + div_fraction_low) - pclk; |
---|
110 | |
---|
111 | if (low_err < high_err) { |
---|
112 | div_mantissa = div_mantissa_low; |
---|
113 | div_fraction = div_fraction_low; |
---|
114 | } else { |
---|
115 | div_mantissa = div_mantissa_high; |
---|
116 | div_fraction = div_fraction_high; |
---|
117 | } |
---|
118 | |
---|
119 | return STM32F4_USART_BBR_DIV_MANTISSA(div_mantissa) |
---|
120 | | STM32F4_USART_BBR_DIV_FRACTION(div_fraction); |
---|
121 | } |
---|
122 | |
---|
123 | static void usart_initialize(int minor) |
---|
124 | { |
---|
125 | const console_tbl *ct = Console_Port_Tbl [minor]; |
---|
126 | volatile stm32f4_usart *usart = usart_get_regs(ct); |
---|
127 | uint32_t pclk = usart_get_pclk(ct); |
---|
128 | uint32_t baud = usart_get_baud(ct); |
---|
129 | stm32f4_rcc_index rcc_index = usart_get_rcc_index(ct); |
---|
130 | |
---|
131 | stm32f4_rcc_set_clock(rcc_index, true); |
---|
132 | |
---|
133 | usart->cr1 = 0; |
---|
134 | usart->cr2 = 0; |
---|
135 | usart->cr3 = 0; |
---|
136 | usart->bbr = usart_get_bbr(usart, pclk, baud); |
---|
137 | usart->cr1 = STM32F4_USART_CR1_UE |
---|
138 | | STM32F4_USART_CR1_TE |
---|
139 | | STM32F4_USART_CR1_RE; |
---|
140 | } |
---|
141 | |
---|
142 | static int usart_first_open(int major, int minor, void *arg) |
---|
143 | { |
---|
144 | rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg; |
---|
145 | struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1; |
---|
146 | const console_tbl *ct = Console_Port_Tbl [minor]; |
---|
147 | console_data *cd = &Console_Port_Data [minor]; |
---|
148 | |
---|
149 | cd->termios_data = tty; |
---|
150 | rtems_termios_set_initial_baud(tty, ct->ulClock); |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | static int usart_last_close(int major, int minor, void *arg) |
---|
156 | { |
---|
157 | return 0; |
---|
158 | } |
---|
159 | |
---|
160 | static int usart_read_polled(int minor) |
---|
161 | { |
---|
162 | const console_tbl *ct = Console_Port_Tbl [minor]; |
---|
163 | volatile stm32f4_usart *usart = usart_get_regs(ct); |
---|
164 | |
---|
165 | if ((usart->sr & STM32F4_USART_SR_RXNE) != 0) { |
---|
166 | return STM32F4_USART_DR_GET(usart->dr); |
---|
167 | } else { |
---|
168 | return -1; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | static void usart_write_polled(int minor, char c) |
---|
173 | { |
---|
174 | const console_tbl *ct = Console_Port_Tbl [minor]; |
---|
175 | volatile stm32f4_usart *usart = usart_get_regs(ct); |
---|
176 | |
---|
177 | while ((usart->sr & STM32F4_USART_SR_TXE) == 0) { |
---|
178 | /* Wait */ |
---|
179 | } |
---|
180 | |
---|
181 | usart->dr = STM32F4_USART_DR(c); |
---|
182 | } |
---|
183 | |
---|
184 | static ssize_t usart_write_support_polled( |
---|
185 | int minor, |
---|
186 | const char *s, |
---|
187 | size_t n |
---|
188 | ) |
---|
189 | { |
---|
190 | ssize_t i = 0; |
---|
191 | |
---|
192 | for (i = 0; i < n; ++i) { |
---|
193 | usart_write_polled(minor, s [i]); |
---|
194 | } |
---|
195 | |
---|
196 | return n; |
---|
197 | } |
---|
198 | |
---|
199 | static int usart_set_attributes(int minor, const struct termios *term) |
---|
200 | { |
---|
201 | return -1; |
---|
202 | } |
---|
203 | |
---|
204 | const console_fns stm32f4_usart_fns = { |
---|
205 | .deviceProbe = libchip_serial_default_probe, |
---|
206 | .deviceFirstOpen = usart_first_open, |
---|
207 | .deviceLastClose = usart_last_close, |
---|
208 | .deviceRead = usart_read_polled, |
---|
209 | .deviceWrite = usart_write_support_polled, |
---|
210 | .deviceInitialize = usart_initialize, |
---|
211 | .deviceWritePolled = usart_write_polled, |
---|
212 | .deviceSetAttributes = usart_set_attributes, |
---|
213 | .deviceOutputUsesInterrupts = false |
---|
214 | }; |
---|