File : s-textio-stm32f469.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . T E X T _ I O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- --
19 -- --
20 -- --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- Minimal version of Text_IO body for use on STM32F4xxx, using USART1
33
34 with Interfaces; use Interfaces;
35
36 with Interfaces.Bit_Types; use Interfaces.Bit_Types;
37 with Interfaces.STM32.RCC; use Interfaces.STM32.RCC;
38 with Interfaces.STM32.GPIO; use Interfaces.STM32.GPIO;
39 with Interfaces.STM32.USART; use Interfaces.STM32.USART;
40 with System.STM32; use System.STM32;
41 with System.BB.Parameters;
42
43 package body System.Text_IO is
44
45 Baudrate : constant := 115_200;
46 -- Bitrate to use
47
48 ----------------
49 -- Initialize --
50 ----------------
51
52 procedure Initialize is
53 use System.BB.Parameters;
54
55 APB_Clock : constant Positive := Positive (STM32.System_Clocks.PCLK2);
56 Int_Divider : constant Positive := (25 * APB_Clock) / (4 * Baudrate);
57 Frac_Divider : constant Natural := Int_Divider rem 100;
58 Pins : constant array (1 .. 2) of Natural := (9, 14);
59
60 begin
61 Initialized := True;
62
63 RCC_Periph.APB2ENR.USART6EN := 1;
64 RCC_Periph.AHB1ENR.GPIOGEN := 1;
65
66 for Pin of Pins loop
67 GPIOG_Periph.MODER.Arr (Pin) := Mode_AF;
68 GPIOG_Periph.OSPEEDR.Arr (Pin) := Speed_50MHz;
69 GPIOG_Periph.OTYPER.OT.Arr (Pin) := Push_Pull;
70 GPIOG_Periph.PUPDR.Arr (Pin) := Pull_Up;
71 GPIOG_Periph.AFRL.Arr (Pin) := AF_USART6;
72 end loop;
73
74 USART6_Periph.BRR :=
75 (DIV_Fraction => UInt4 (((Frac_Divider * 16 + 50) / 100) mod 16),
76 DIV_Mantissa => UInt12 (Int_Divider / 100),
77 others => <>);
78 USART6_Periph.CR1 :=
79 (UE => 1,
80 RE => 1,
81 TE => 1,
82 others => <>);
83 USART6_Periph.CR2 := (others => <>);
84 USART6_Periph.CR3 := (others => <>);
85 end Initialize;
86
87 -----------------
88 -- Is_Tx_Ready --
89 -----------------
90
91 function Is_Tx_Ready return Boolean is
92 (USART6_Periph.SR.TC = 1);
93
94 -----------------
95 -- Is_Rx_Ready --
96 -----------------
97
98 function Is_Rx_Ready return Boolean is
99 (USART6_Periph.SR.RXNE = 1);
100
101 ---------
102 -- Get --
103 ---------
104
105 function Get return Character is (Character'Val (USART6_Periph.DR.DR));
106
107 ---------
108 -- Put --
109 ---------
110
111 procedure Put (C : Character) is
112 begin
113 USART6_Periph.DR.DR := Character'Pos (C);
114 end Put;
115
116 ----------------------------
117 -- Use_Cr_Lf_For_New_Line --
118 ----------------------------
119
120 function Use_Cr_Lf_For_New_Line return Boolean is (True);
121
122 end System.Text_IO;