File : s-stm32-f4x9x.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --          Copyright (C) 2012-2016, Free Software Foundation, Inc.         --
   6 --                                                                          --
   7 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
   8 -- terms of the  GNU General Public License as published  by the Free Soft- --
   9 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  10 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  11 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  12 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  13 --                                                                          --
  14 --                                                                          --
  15 --                                                                          --
  16 --                                                                          --
  17 --                                                                          --
  18 -- You should have received a copy of the GNU General Public License and    --
  19 -- a copy of the GCC Runtime Library Exception along with this program;     --
  20 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  21 -- <http://www.gnu.org/licenses/>.                                          --
  22 --                                                                          --
  23 -- GNAT was originally developed  by the GNAT team at  New York University. --
  24 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  25 --                                                                          --
  26 ------------------------------------------------------------------------------
  27 
  28 with Ada.Unchecked_Conversion;
  29 
  30 with System.BB.Parameters;
  31 
  32 with Interfaces;            use Interfaces;
  33 with Interfaces.Bit_Types;  use Interfaces.Bit_Types;
  34 with Interfaces.STM32.RCC;  use Interfaces.STM32.RCC;
  35 
  36 package body System.STM32 is
  37 
  38    package Param renames System.BB.Parameters;
  39 
  40    HPRE_Presc_Table : constant array (AHB_Prescaler_Enum) of Word :=
  41      (2, 4, 8, 16, 64, 128, 256, 512);
  42 
  43    PPRE_Presc_Table : constant array (APB_Prescaler_Enum) of Word :=
  44      (2, 4, 8, 16);
  45 
  46    -------------------
  47    -- System_Clocks --
  48    -------------------
  49 
  50    function System_Clocks return RCC_System_Clocks
  51    is
  52       Source       : constant SYSCLK_Source :=
  53                       SYSCLK_Source'Val (RCC_Periph.CFGR.SWS);
  54       Result       : RCC_System_Clocks;
  55 
  56    begin
  57       case Source is
  58 
  59          --  HSI as source
  60 
  61          when SYSCLK_SRC_HSI =>
  62             Result.SYSCLK := Param.HSI_Clock;
  63 
  64          --  HSE as source
  65 
  66          when SYSCLK_SRC_HSE =>
  67             Result.SYSCLK := Param.HSE_Clock;
  68 
  69          --  PLL as source
  70 
  71          when SYSCLK_SRC_PLL =>
  72             declare
  73                Pllm   : constant Word := Word (RCC_Periph.PLLCFGR.PLLM);
  74                Plln   : constant Word := Word (RCC_Periph.PLLCFGR.PLLN);
  75                Pllp   : constant Word := Word (RCC_Periph.PLLCFGR.PLLP);
  76                Pllvco : Word;
  77 
  78             begin
  79                case PLL_Source'Val (RCC_Periph.PLLCFGR.PLLSRC) is
  80                   when PLL_SRC_HSE =>
  81                      Pllvco := (Param.HSE_Clock / Pllm) * Plln;
  82                   when PLL_SRC_HSI =>
  83                      Pllvco := (Param.HSI_Clock / Pllm) * Plln;
  84                end case;
  85 
  86                Result.SYSCLK := Pllvco / ((Pllp + 1) * 2);
  87             end;
  88       end case;
  89 
  90       declare
  91          function To_AHBP is new Ada.Unchecked_Conversion
  92            (CFGR_HPRE_Field, AHB_Prescaler);
  93          function To_APBP is new Ada.Unchecked_Conversion
  94            (CFGR_PPRE_Element, APB_Prescaler);
  95 
  96          HPRE      : constant AHB_Prescaler := To_AHBP (RCC_Periph.CFGR.HPRE);
  97          HPRE_Div  : constant Word := (if HPRE.Enabled
  98                                        then HPRE_Presc_Table (HPRE.Value)
  99                                        else 1);
 100          PPRE1     : constant APB_Prescaler :=
 101                       To_APBP (RCC_Periph.CFGR.PPRE.Arr (1));
 102          PPRE1_Div : constant Word := (if PPRE1.Enabled
 103                                        then PPRE_Presc_Table (PPRE1.Value)
 104                                        else 1);
 105          PPRE2     : constant APB_Prescaler :=
 106                       To_APBP (RCC_Periph.CFGR.PPRE.Arr (2));
 107          PPRE2_Div : constant Word := (if PPRE2.Enabled
 108                                        then PPRE_Presc_Table (PPRE2.Value)
 109                                        else 1);
 110 
 111       begin
 112          Result.HCLK  := Result.SYSCLK / HPRE_Div;
 113          Result.PCLK1 := Result.HCLK / PPRE1_Div;
 114          Result.PCLK2 := Result.HCLK / PPRE2_Div;
 115 
 116          if not PPRE1.Enabled then
 117             Result.TIMCLK1 := Result.PCLK1;
 118          else
 119             Result.TIMCLK1 := Result.PCLK1 * 2;
 120          end if;
 121 
 122          if not PPRE2.Enabled then
 123             Result.TIMCLK2 := Result.PCLK2;
 124          else
 125             Result.TIMCLK2 := Result.PCLK2 * 2;
 126          end if;
 127       end;
 128 
 129       return Result;
 130    end System_Clocks;
 131 
 132 end System.STM32;