File : s-textio-stm32f7.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 STM32F7x, using USART6.
  33 --  Serial interface is available via the arduino port of the board:
  34 --  PIN D0 (PC7 GPIO): USART6_RX
  35 --  PIN D1 (PC6 GPIO): USART6_TX
  36 
  37 with Interfaces; use Interfaces;
  38 
  39 with Interfaces.Bit_Types;   use Interfaces.Bit_Types;
  40 with Interfaces.STM32.RCC;   use Interfaces.STM32.RCC;
  41 with Interfaces.STM32.GPIO;  use Interfaces.STM32.GPIO;
  42 with Interfaces.STM32.USART; use Interfaces.STM32.USART;
  43 with System.STM32;           use System.STM32;
  44 with System.BB.Parameters;
  45 
  46 package body System.Text_IO is
  47 
  48    Baudrate : constant := 115_200;
  49    --  Bitrate to use
  50 
  51    ----------------
  52    -- Initialize --
  53    ----------------
  54 
  55    procedure Initialize is
  56       use System.BB.Parameters;
  57 
  58       APB_Clock    : constant Positive := Positive (STM32.System_Clocks.PCLK2);
  59       Int_Divider  : constant Positive := (25 * APB_Clock) / (4 * Baudrate);
  60       Frac_Divider : constant Natural := Int_Divider rem 100;
  61 
  62    begin
  63       Initialized := True;
  64 
  65       RCC_Periph.APB2ENR.USART6EN := 1;
  66       RCC_Periph.AHB1ENR.GPIOCEN  := 1;
  67 
  68       GPIOC_Periph.MODER.Arr     (6 .. 7) := (Mode_AF,     Mode_AF);
  69       GPIOC_Periph.OSPEEDR.Arr   (6 .. 7) := (Speed_50MHz, Speed_50MHz);
  70       GPIOC_Periph.OTYPER.OT.Arr (6 .. 7) := (Push_Pull,   Push_Pull);
  71       GPIOC_Periph.PUPDR.Arr     (6 .. 7) := (Pull_Up,     Pull_Up);
  72       GPIOC_Periph.AFRL.Arr      (6 .. 7) := (AF_USART6,   AF_USART6);
  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.ISR.TC = 1);
  93 
  94    -----------------
  95    -- Is_Rx_Ready --
  96    -----------------
  97 
  98    function Is_Rx_Ready return Boolean is
  99      (USART6_Periph.ISR.RXNE = 1);
 100 
 101    ---------
 102    -- Get --
 103    ---------
 104 
 105    function Get return Character is (Character'Val (USART6_Periph.RDR.RDR));
 106 
 107    ---------
 108    -- Put --
 109    ---------
 110 
 111    procedure Put (C : Character) is
 112    begin
 113       USART6_Periph.TDR.TDR := 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;