aboutsummaryrefslogtreecommitdiff
path: root/lib/stm32/usart.c
diff options
context:
space:
mode:
authorPiotr Esden-Tempski2011-03-11 19:50:44 -0800
committerPiotr Esden-Tempski2011-03-11 19:51:48 -0800
commit1f3e4315275bfbf455e9d29ba9e6453c420ded1f (patch)
treeedee45ea4c9c07d71ea0190ba9e059cb2a4c6323 /lib/stm32/usart.c
parent3d20f37b279354e54cb5fd69e6fe078e97a6cce3 (diff)
Usart baud rate setting function now uses the new apb1 and apb2 frequency variables. And calculates the full BRR value.
Diffstat (limited to 'lib/stm32/usart.c')
-rw-r--r--lib/stm32/usart.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/stm32/usart.c b/lib/stm32/usart.c
index f644776..ead0ef7 100644
--- a/lib/stm32/usart.c
+++ b/lib/stm32/usart.c
@@ -17,14 +17,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <libopencm3/stm32/rcc.h>
+
#include <libopencm3/stm32/usart.h>
void usart_set_baudrate(u32 usart, u32 baud)
{
- u32 clock = 72000000; /* FIXME: Don't hardcode this clock! */
-
- /* TODO: Document and explain calculation. */
- USART_BRR(usart) = (u16)((clock << 4) / (baud * 16));
+ u32 clock = rcc_ppre1_frequency;
+
+ if (usart == USART1) {
+ clock = rcc_ppre2_frequency;
+ }
+
+ /* yes it is as simple as that. The reference manual is
+ * talking about factional calculation but it seems to be only
+ * marketting bable to sound awesome. It is nothing else but a
+ * simple divider to generate the correct baudrate. >_< If I
+ * am wrong feel free to correct me on that. :) (esden)
+ */
+ USART_BRR(usart) = clock/baud;
}
void usart_set_databits(u32 usart, u32 bits)