/***********************************************************************************************************************
 * This file was generated by the MCUXpresso Config Tools. Any manual edits made to this file
 * will be overwritten if the respective MCUXpresso Config Tools is used to update this file.
 **********************************************************************************************************************/

/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
!!GlobalInfo
product: Peripherals v15.0
processor: MCXN947
package_id: MCXN947VDF
mcu_data: ksdk2_0
processor_version: 25.06.10
board: FRDM-MCXN947
functionalGroups:
- name: BOARD_InitPeripherals
  UUID: 065b1d8a-64bc-4e15-83fe-6ddaf7bb48ff
  called_from_default_init: true
  selectedCore: cm33_core0
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/

/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
component:
- type: 'system'
- type_id: 'system'
- global_system_definitions:
  - user_definitions: ''
  - user_includes: ''
  - global_init: ''
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/

/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
component:
- type: 'uart_cmsis_common'
- type_id: 'uart_cmsis_common'
- global_USART_CMSIS_common:
  - quick_selection: 'default'
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/

/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
component:
- type: 'gpio_adapter_common'
- type_id: 'gpio_adapter_common'
- global_gpio_adapter_common:
  - quick_selection: 'default'
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */

/***********************************************************************************************************************
 * Included files
 **********************************************************************************************************************/
#include "peripherals.h"

/***********************************************************************************************************************
 * BOARD_InitPeripherals functional group
 **********************************************************************************************************************/
/***********************************************************************************************************************
 * NVIC initialization code
 **********************************************************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
instance:
- name: 'NVIC'
- type: 'nvic'
- mode: 'general'
- custom_name_enabled: 'false'
- type_id: 'nvic'
- functional_group: 'BOARD_InitPeripherals'
- peripheral: 'NVIC'
- config_sets:
  - nvic:
    - interrupt_table:
      - 0: []
      - 1: []
    - interrupts: []
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */

/* Empty initialization function (commented out)
static void NVIC_init(void) {
} */

/***********************************************************************************************************************
 * ADC0 initialization code
 **********************************************************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
instance:
- name: 'ADC0'
- type: 'lpadc'
- mode: 'LPADC'
- custom_name_enabled: 'false'
- type_id: 'lpadc_2.8.1'
- functional_group: 'BOARD_InitPeripherals'
- peripheral: 'ADC0'
- config_sets:
  - fsl_lpadc:
    - lpadcConfig:
      - clockSource: 'AsynchronousFunctionClock'
      - clockSourceFreq: 'ClocksTool_DefaultInit'
      - enableInDozeMode: 'true'
      - conversionAverageMode: 'kLPADC_ConversionAverage1'
      - offsetCalibration: 'no'
      - autoCalibrate: 'false'
      - enableAnalogPreliminary: 'false'
      - powerUpDelay: '0x80'
      - referenceVoltageSource: 'kLPADC_ReferenceVoltageAlt3'
      - powerLevelMode: 'kLPADC_PowerLevelAlt1'
      - triggerPriorityPolicy: 'kLPADC_ConvPreemptImmediatelyNotAutoResumed'
      - enableConvPause: 'false'
      - convPauseDelay: '0'
      - FIFO0Watermark: '0'
      - FIFO1Watermark: '0'
      - FIFO0WatermarkDMA: 'false'
      - FIFO1WatermarkDMA: 'false'
    - lpadcConvCommandConfig:
      - 0:
        - user_commandId: ''
        - commandId: '1'
        - chainedNextCommandNumber: '0'
        - sampleChannelMode: 'kLPADC_SampleChannelSingleEndSideA'
        - channelNumber: 'A.0'
        - enableChannelB_b: 'false'
        - channelBNumber: 'B.0'
        - enableAutoChannelIncrement: 'false'
        - loopCount: '0'
        - hardwareAverageMode: 'kLPADC_HardwareAverageCount1'
        - sampleTimeMode: 'kLPADC_SampleTimeADCK3'
        - hardwareCompareMode: 'kLPADC_HardwareCompareDisabled'
        - hardwareCompareValueHigh: '0'
        - hardwareCompareValueLow: '0'
        - conversionResoultuionMode: 'kLPADC_ConversionResolutionStandard'
        - enableWaitTrigger: 'false'
    - lpadcConvTriggerConfig:
      - 0:
        - user_triggerId: ''
        - triggerId: '0'
        - targetCommandId: '1'
        - delayPower: '0'
        - priority: 'false'
        - channelAFIFOSelect: '0'
        - channelBFIFOSelect: '0'
        - enableHardwareTrigger: 'false'
    - IRQ_cfg:
      - interrupt_type: 'kLPADC_FIFO0WatermarkInterruptEnable'
      - enable_irq: 'true'
      - adc_interrupt:
        - IRQn: 'ADC0_IRQn'
        - enable_interrrupt: 'enabled'
        - enable_priority: 'true'
        - priority: '5'
        - enable_custom_name: 'false'
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */
const lpadc_config_t ADC0_config = {
  .enableInDozeMode = true,
  .conversionAverageMode = kLPADC_ConversionAverage1,
  .enableAnalogPreliminary = false,
  .powerUpDelay = 0x80UL,
  .referenceVoltageSource = kLPADC_ReferenceVoltageAlt3,
  .powerLevelMode = kLPADC_PowerLevelAlt1,
  .triggerPriorityPolicy = kLPADC_ConvPreemptImmediatelyNotAutoResumed,
  .enableConvPause = false,
  .convPauseDelay = 0UL,
  .FIFO0Watermark = 0UL,
  .FIFO1Watermark = 0UL
};
lpadc_conv_command_config_t ADC0_commandsConfig[1] = {
  {
    .sampleChannelMode = kLPADC_SampleChannelSingleEndSideA,
    .channelNumber = 0U,
    .channelBNumber = 0U,
    .chainedNextCommandNumber = 0,
    .enableChannelB = false,
    .enableAutoChannelIncrement = false,
    .loopCount = 0UL,
    .hardwareAverageMode = kLPADC_HardwareAverageCount1,
    .sampleTimeMode = kLPADC_SampleTimeADCK3,
    .hardwareCompareMode = kLPADC_HardwareCompareDisabled,
    .hardwareCompareValueHigh = 0UL,
    .hardwareCompareValueLow = 0UL,
    .conversionResolutionMode = kLPADC_ConversionResolutionStandard,
    .enableWaitTrigger = false
  }
};
lpadc_conv_trigger_config_t ADC0_triggersConfig[1] = {
  {
    .targetCommandId = 1,
    .delayPower = 0UL,
    .channelAFIFOSelect = 0,
    .channelBFIFOSelect = 0,
    .priority = 1,
    .enableHardwareTrigger = false
  }
};

static void ADC0_init(void) {
  /* Initialize LPADC converter */
  LPADC_Init(ADC0_PERIPHERAL, &ADC0_config);
  /* Configure conversion command 1. */
  LPADC_SetConvCommandConfig(ADC0_PERIPHERAL, 1, &ADC0_commandsConfig[0]);
  /* Configure trigger 0. */
  LPADC_SetConvTriggerConfig(ADC0_PERIPHERAL, 0, &ADC0_triggersConfig[0]);
  /* Interrupt vector ADC0_IRQn priority settings in the NVIC. */
  NVIC_SetPriority(ADC0_IRQN, ADC0_IRQ_PRIORITY);
  /* Enable interrupts from LPADC */
  LPADC_EnableInterrupts(ADC0_PERIPHERAL, (kLPADC_FIFO0WatermarkInterruptEnable));
  /* Enable interrupt ADC0_IRQN request in the NVIC */
  EnableIRQ(ADC0_IRQN);
}

/***********************************************************************************************************************
 * VREF0 initialization code
 **********************************************************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
instance:
- name: 'VREF0'
- type: 'vref_1'
- mode: 'general'
- custom_name_enabled: 'false'
- type_id: 'vref_1_2.4.0'
- functional_group: 'BOARD_InitPeripherals'
- peripheral: 'VREF0'
- config_sets:
  - fsl_vref:
    - vref_config:
      - bufferMode: 'kVREF_ModeHighPowerBuffer'
      - enableLowPowerBuff: 'true'
      - enableInternalVoltageRegulator: 'true'
      - enableChopOscillator: 'true'
      - enableHCBandgap: 'true'
      - enableCurvatureCompensation: 'true'
      - initTrim: 'false'
    - quick_selection: 'default'
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */
const vref_config_t VREF0_config = {
  .bufferMode = kVREF_ModeHighPowerBuffer,
  .enableLowPowerBuff = true,
  .enableInternalVoltageRegulator = true,
  .enableChopOscillator = true,
  .enableHCBandgap = true,
  .enableCurvatureCompensation = true
};

static void VREF0_init(void) {
  /* Power up analog module in SPC */
  SPC_EnableActiveModeAnalogModules(SPC0, kSPC_controlVref);
  /* VREF0 peripheral initialization */
  VREF_Init(VREF0_PERIPHERAL, &VREF0_config);
}

/***********************************************************************************************************************
 * LP_FLEXCOMM2 initialization code
 **********************************************************************************************************************/
/* clang-format off */
/* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
instance:
- name: 'LP_FLEXCOMM2'
- type: 'lpflexcomm_lpi2c'
- mode: 'interrupt'
- custom_name_enabled: 'false'
- type_id: 'lpflexcomm_lpi2c_2.1.1'
- functional_group: 'BOARD_InitPeripherals'
- peripheral: 'LP_FLEXCOMM2'
- config_sets:
  - generalCfg:
    - lpi2c_mode: 'master'
    - clock_configuration:
      - clockSource: 'LPFLEXCOMMFunctionClock'
      - clockSourceFreq: 'ClocksTool_DefaultInit'
    - master_config:
      - enableMaster: 'true'
      - enableDoze: 'true'
      - debugEnable: 'false'
      - ignoreAck: 'false'
      - pinConfig: 'kLPI2C_2PinOpenDrain'
      - baudRate_Hz: '100000'
      - realBaudRateCount: []
      - busIdleTimeout_ns: '0'
      - pinLowTimeout_ns: '0'
      - sdaGlitchFilterWidth_ns: '0'
      - sclGlitchFilterWidth_ns: '0'
      - hostRequest:
        - enable: 'false'
        - source: 'kLPI2C_HostRequestExternalPin'
        - polarity: 'kLPI2C_HostRequestPinActiveHigh'
      - enable_dma_master: 'false'
      - lpi2c_dma_master_struct_t:
        - DMATxEnable: 'true'
        - DMARxEnable: 'true'
    - quick_selection: 'qs_default'
  - interrupt:
    - master_flags: 'kLPI2C_MasterTxReadyFlag kLPI2C_MasterRxReadyFlag kLPI2C_MasterStopDetectFlag kLPI2C_MasterNackDetectFlag'
    - enable_irq: 'true'
    - interrupt:
      - IRQn: 'LP_FLEXCOMM2_IRQn'
      - enable_interrrupt: 'noInit'
      - enable_priority: 'true'
      - priority: '4'
      - enable_custom_name: 'false'
 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS **********/
/* clang-format on */
const lpi2c_master_config_t LP_FLEXCOMM2_masterConfig = {
  .enableMaster = true,
  .enableDoze = true,
  .debugEnable = false,
  .ignoreAck = false,
  .pinConfig = kLPI2C_2PinOpenDrain,
  .baudRate_Hz = 100000UL,
  .busIdleTimeout_ns = 0UL,
  .pinLowTimeout_ns = 0UL,
  .sdaGlitchFilterWidth_ns = 0U,
  .sclGlitchFilterWidth_ns = 0U,
  .hostRequest = {
    .enable = false,
    .source = kLPI2C_HostRequestExternalPin,
    .polarity = kLPI2C_HostRequestPinActiveHigh
  }
};

static void LP_FLEXCOMM2_init(void) {
  /* LP_FLEXCOMM2 LPI2C leader initialization */
  LPI2C_MasterInit(LP_FLEXCOMM2_PERIPHERAL, &LP_FLEXCOMM2_masterConfig, LP_FLEXCOMM2_CLOCK_SOURCE);
  /* LP_FLEXCOMM2 LPI2C leader interrupt sources */
  LPI2C_MasterEnableInterrupts(LP_FLEXCOMM2_PERIPHERAL, (uint32_t)(kLPI2C_MasterTxReadyFlag | kLPI2C_MasterRxReadyFlag | kLPI2C_MasterStopDetectFlag | kLPI2C_MasterNackDetectFlag));
  /* Interrupt vector LP_FLEXCOMM2_IRQn priority settings in the NVIC. */
  NVIC_SetPriority(LP_FLEXCOMM2_IRQN, LP_FLEXCOMM2_IRQ_PRIORITY);
  /* Interrupt LP_FLEXCOMM2_IRQN request in the NVIC is not initialized (disabled by default). */
  /* It can be enabled later by EnableIRQ(LP_FLEXCOMM2_IRQN); function call. */
}

/***********************************************************************************************************************
 * Initialization functions
 **********************************************************************************************************************/
void BOARD_InitPeripherals(void)
{
  /* Initialize components */
  ADC0_init();
  VREF0_init();
  LP_FLEXCOMM2_init();
}

/***********************************************************************************************************************
 * BOARD_InitBootPeripherals function
 **********************************************************************************************************************/
void BOARD_InitBootPeripherals(void)
{
  BOARD_InitPeripherals();
}
