text: /*******************************************************************************
    System Initialization File
  
    File Name:
      initialization.c
  
    Summary:
      This file contains source code necessary to initialize the system.
  
    Description:
      This file contains source code necessary to initialize the system.  It
      implements the "SYS_Initialize" function, defines the configuration bits,
      and allocates any necessary global system resources,
   *******************************************************************************/
  
  // DOM-IGNORE-BEGIN
  /*******************************************************************************
  * 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.
   *******************************************************************************/
  // DOM-IGNORE-END
  
  // *****************************************************************************
  // *****************************************************************************
  // Section: Included Files
  // *****************************************************************************
  // *****************************************************************************
  #include "configuration.h"
  #include "definitions.h"
  #include "device.h"
  
  
  
  // ****************************************************************************
  // ****************************************************************************
  // Section: Configuration Bits
  // ****************************************************************************
  // ****************************************************************************
  #pragma config BOD33_DIS = SET
  #pragma config BOD33USERLEVEL = 0x1c
  #pragma config BOD33_ACTION = RESET
  #pragma config BOD33_HYST = 0x2
  #pragma config NVMCTRL_BOOTPROT = 0
  #pragma config NVMCTRL_SEESBLK = 0x0
  #pragma config NVMCTRL_SEEPSZ = 0x0
  #pragma config RAMECC_ECCDIS = SET
  #pragma config WDT_ENABLE = CLEAR
  #pragma config WDT_ALWAYSON = CLEAR
  #pragma config WDT_PER = CYC8192
  #pragma config WDT_WINDOW = CYC8192
  #pragma config WDT_EWOFFSET = CYC8192
  #pragma config WDT_WEN = CLEAR
  #pragma config NVMCTRL_REGION_LOCKS = 0xffffffff
  
  
  
  
  // *****************************************************************************
  // *****************************************************************************
  // Section: Driver Initialization Data
  // *****************************************************************************
  // *****************************************************************************
  // <editor-fold defaultstate="collapsed" desc="DRV_I2C Instance 0 Initialization Data">
  
  /* I2C Client Objects Pool */
  static DRV_I2C_CLIENT_OBJ drvI2C0ClientObjPool[DRV_I2C_CLIENTS_NUMBER_IDX0];
  
  /* I2C Transfer Objects Pool */
  static DRV_I2C_TRANSFER_OBJ drvI2C0TransferObj[DRV_I2C_QUEUE_SIZE_IDX0];
  
  /* I2C PLib Interface Initialization */
  const DRV_I2C_PLIB_INTERFACE drvI2C0PLibAPI = {
  
      /* I2C PLib Transfer Read Add function */
      .read = (DRV_I2C_PLIB_READ)SERCOM6_I2C_Read,
  
      /* I2C PLib Transfer Write Add function */
      .write = (DRV_I2C_PLIB_WRITE)SERCOM6_I2C_Write,
  
  
      /* I2C PLib Transfer Write Read Add function */
      .writeRead = (DRV_I2C_PLIB_WRITE_READ)SERCOM6_I2C_WriteRead,
  
      /* I2C PLib Transfer Status function */
      .errorGet = (DRV_I2C_PLIB_ERROR_GET)SERCOM6_I2C_ErrorGet,
  
      /* I2C PLib Transfer Setup function */
      .transferSetup = (DRV_I2C_PLIB_TRANSFER_SETUP)SERCOM6_I2C_TransferSetup,
  
      /* I2C PLib Callback Register */
      .callbackRegister = (DRV_I2C_PLIB_CALLBACK_REGISTER)SERCOM6_I2C_CallbackRegister,
  };
  
  
  const DRV_I2C_INTERRUPT_SOURCES drvI2C0InterruptSources =
  {
      /* Peripheral has more than one interrupt vector */
      .isSingleIntSrc                        = false,
  
      /* Peripheral interrupt lines */
      .intSources.multi.i2cInt0          = SERCOM6_0_IRQn,
      .intSources.multi.i2cInt1          = SERCOM6_1_IRQn,
      .intSources.multi.i2cInt2          = SERCOM6_2_IRQn,
      .intSources.multi.i2cInt3          = SERCOM6_OTHER_IRQn,
  };
  
  /* I2C Driver Initialization Data */
  const DRV_I2C_INIT drvI2C0InitData =
  {
      /* I2C PLib API */
      .i2cPlib = &drvI2C0PLibAPI,
  
      /* I2C Number of clients */
      .numClients = DRV_I2C_CLIENTS_NUMBER_IDX0,
  
      /* I2C Client Objects Pool */
      .clientObjPool = (uintptr_t)&drvI2C0ClientObjPool[0],
  
      /* I2C TWI Queue Size */
      .transferObjPoolSize = DRV_I2C_QUEUE_SIZE_IDX0,
  
      /* I2C Transfer Objects */
      .transferObjPool = (uintptr_t)&drvI2C0TransferObj[0],
  
      /* I2C interrupt sources */
      .interruptSources = &drvI2C0InterruptSources,
  
      /* I2C Clock Speed */
      .clockSpeed = DRV_I2C_CLOCK_SPEED_IDX0,
  };
  
  // </editor-fold>
  
  // <editor-fold defaultstate="collapsed" desc="DRV_I2S Instance 0 Initialization Data">
  
  /* I2S PLIB Interface Initialization */
  DRV_I2S_PLIB_INTERFACE drvI2S0PlibAPI =
  {
      .I2S_LRCLK_Get = (DRV_I2S_LRCLK_GET)I2S_LRCLK_Get,  
  };
  
  /* I2S Driver Initialization Data */
  DRV_I2S_INIT drvI2S0InitData =
  {
      /* I2S PLIB API */
      .i2sPlib = &drvI2S0PlibAPI,
  
      /* I2S IRQ */
      .interruptI2S = DRV_I2S_INT_SRC_IDX0,
  
      /* I2S Number of clients */
      .numClients = DRV_I2S_CLIENTS_NUMBER_IDX0,
  
      /* I2S Queue Size */
      .queueSize = DRV_I2S_QUEUE_SIZE_IDX0,  
  
      .dmaChannelTransmit = DRV_I2S_XMIT_DMA_CH_IDX0,
      .dmaChannelReceive  = DRV_I2S_RCV_DMA_CH_IDX0,
      .i2sTransmitAddress = (void *)&(I2S_REGS->I2S_TXDATA),
      .i2sReceiveAddress = (void *)&(I2S_REGS->I2S_RXDATA),
      .interruptDMA = DMAC_0_IRQn,
  
      .dmaDataLength = DRV_I2S_DATA_LENGTH_IDX0,
  };
  
  // </editor-fold>
  
  /*** CODEC Driver Initialization Data ***/
  const DRV_WM8904_INIT drvwm8904Codec0InitData =
  {
      .masterMode = DRV_WM8904_MASTER_MODE,
      .samplingRate = DRV_WM8904_AUDIO_SAMPLING_RATE,
      .volume = DRV_WM8904_VOLUME,
      .audioDataFormat = DRV_WM8904_AUDIO_DATA_FORMAT_MACRO,
      .enableMicInput = DRV_WM8904_ENABLE_MIC_INPUT,
      .enableMicBias = DRV_WM8904_ENABLE_MIC_BIAS,
      .micGain = DRV_WM8904_MIC_GAIN
  };
  
  
  
  // *****************************************************************************
  // *****************************************************************************
  // Section: System Data
  // *****************************************************************************
  // *****************************************************************************
  /* Structure to hold the object handles for the modules in the system. */
  SYSTEM_OBJECTS sysObj;
  
  // *****************************************************************************
  // *****************************************************************************
  // Section: Library/Stack Initialization Data
  // *****************************************************************************
  // *****************************************************************************
  
  
  // *****************************************************************************
  // *****************************************************************************
  // Section: System Initialization
  // *****************************************************************************
  // *****************************************************************************
  // <editor-fold defaultstate="collapsed" desc="SYS_TIME Initialization Data">
  
  const SYS_TIME_PLIB_INTERFACE sysTimePlibAPI = {
      .timerCallbackSet = (SYS_TIME_PLIB_CALLBACK_REGISTER)TC0_TimerCallbackRegister,
      .timerStart = (SYS_TIME_PLIB_START)TC0_TimerStart,
      .timerStop = (SYS_TIME_PLIB_STOP)TC0_TimerStop,
      .timerFrequencyGet = (SYS_TIME_PLIB_FREQUENCY_GET)TC0_TimerFrequencyGet,
      .timerPeriodSet = (SYS_TIME_PLIB_PERIOD_SET)TC0_Timer16bitPeriodSet,
      .timerCompareSet = (SYS_TIME_PLIB_COMPARE_SET)TC0_Timer16bitCompareSet,
      .timerCounterGet = (SYS_TIME_PLIB_COUNTER_GET)TC0_Timer16bitCounterGet,
  };
  
  const SYS_TIME_INIT sysTimeInitData =
  {
      .timePlib = &sysTimePlibAPI,
      .hwTimerIntNum = TC0_IRQn,
  };
  
  // </editor-fold>
  
  
  
  /*******************************************************************************
    Function:
      void SYS_Initialize ( void *data )
  
    Summary:
      Initializes the board, services, drivers, application and other modules.
  
    Remarks:
   */
  
  void SYS_Initialize ( void* data )
  {
      NVMCTRL_Initialize( );
  
    
      PORT_Initialize();
  
      CLOCK_Initialize();
  
  
  
  
      SERCOM6_I2C_Initialize();
  
      DMAC_Initialize();
  
  	BSP_Initialize();
      TC0_TimerInitialize();
  
      I2S_Initialize();
  
      /* Initialize I2C0 Driver Instance */
      sysObj.drvI2C0 = DRV_I2C_Initialize(DRV_I2C_INDEX_0, (SYS_MODULE_INIT *)&drvI2C0InitData);
      /* Initialize I2S0 Driver Instance */
      sysObj.drvI2S0 = DRV_I2S_Initialize(DRV_I2S_INDEX_0, (SYS_MODULE_INIT *)&drvI2S0InitData);
  
      sysObj.drvwm8904Codec0 = DRV_WM8904_Initialize(DRV_WM8904_INDEX_0, (SYS_MODULE_INIT *)&drvwm8904Codec0InitData);
  
  
      sysObj.sysTime = SYS_TIME_Initialize(SYS_TIME_INDEX_0, (SYS_MODULE_INIT *)&sysTimeInitData);
  
  
      APP_Initialize();
  
  
      NVIC_Initialize();
  
  }
  
  
  /*******************************************************************************
   End of File
  */
  