text: /*******************************************************************************
   CLOCK PLIB
  
    Company:
      Microchip Technology Inc.
  
    File Name:
      plib_clock.c
  
    Summary:
      CLOCK PLIB Implementation File.
  
    Description:
      None
  
  *******************************************************************************/
  
  /*******************************************************************************
  * Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
  *
  * Subject to your compliance with these terms, you may use Microchip software
  * and any derivatives exclusively with Microchip products. It is your
  * responsibility to comply with third party license terms applicable to your
  * use of third party software (including open source software) that may
  * accompany Microchip software.
  *
  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
  * PARTICULAR PURPOSE.
  *
  * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
  * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
  * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
  * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  *******************************************************************************/
  
  #include "plib_clock.h"
  #include "device.h"
  #include "interrupts.h"
  
  
  
  
  static void OSCCTRL_Initialize(void)
  {
      uint32_t calibValue = (uint32_t)(((*(uint64_t*)0x806020UL) >> 19 ) & 0x3fffffUL);
      OSCCTRL_REGS->OSCCTRL_CAL48M = calibValue;
  
  
      /* Selection of the Division Value */
      OSCCTRL_REGS->OSCCTRL_OSC48MDIV = (uint8_t)OSCCTRL_OSC48MDIV_DIV(0UL);
  
      while((OSCCTRL_REGS->OSCCTRL_OSC48MSYNCBUSY & OSCCTRL_OSC48MSYNCBUSY_OSC48MDIV_Msk) == OSCCTRL_OSC48MSYNCBUSY_OSC48MDIV_Msk)
      {
          /* Waiting for the synchronization */
      }
  
      while((OSCCTRL_REGS->OSCCTRL_STATUS & OSCCTRL_STATUS_OSC48MRDY_Msk) != OSCCTRL_STATUS_OSC48MRDY_Msk)
      {
          /* Waiting for the OSC48M Ready state */
      }
  }
  
  static void OSC32KCTRL_Initialize(void)
  {
      OSC32KCTRL_REGS->OSC32KCTRL_OSC32K = 0x0UL;
  
      OSC32KCTRL_REGS->OSC32KCTRL_RTCCTRL = OSC32KCTRL_RTCCTRL_RTCSEL(0UL);
  }
  
  
  
  static void GCLK0_Initialize(void)
  {
  
      GCLK_REGS->GCLK_GENCTRL[0] = GCLK_GENCTRL_DIV(1UL) | GCLK_GENCTRL_SRC(6UL) | GCLK_GENCTRL_GENEN_Msk;
  
      while((GCLK_REGS->GCLK_SYNCBUSY & GCLK_SYNCBUSY_GENCTRL0_Msk) == GCLK_SYNCBUSY_GENCTRL0_Msk)
      {
          /* wait for the Generator 0 synchronization */
      }
  }
  
  void CLOCK_Initialize (void)
  {
      /* Function to Initialize the Oscillators */
      OSCCTRL_Initialize();
  
      /* Function to Initialize the 32KHz Oscillators */
      OSC32KCTRL_Initialize();
  
      GCLK0_Initialize();
  
  
      /* Selection of the Generator and write Lock for SERCOM5_CORE */
      GCLK_REGS->GCLK_PCHCTRL[25] = GCLK_PCHCTRL_GEN(0x0UL)  | GCLK_PCHCTRL_CHEN_Msk;
  
      while ((GCLK_REGS->GCLK_PCHCTRL[25] & GCLK_PCHCTRL_CHEN_Msk) != GCLK_PCHCTRL_CHEN_Msk)
      {
          /* Wait for synchronization */
      }
  
  
  
      /* Configure the APBC Bridge Clocks */
      MCLK_REGS->MCLK_APBCMASK = 0x40U;
  
  
  }
  
  
  
  