text: /*
   * Copyright 2018 NXP
   * All rights reserved.
   *
   *
   * SPDX-License-Identifier: BSD-3-Clause
   */
  
  #include <stdint.h>
  #include "fsl_common.h"
  #include "fsl_debug_console.h"
  #include "board.h"
  #include "fsl_clock.h"
  #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
  #include "fsl_i2c.h"
  #endif /* SDK_I2C_BASED_COMPONENT_USED */
  #if defined BOARD_USE_CODEC
  #include "fsl_wm8904.h"
  #include "fsl_i3c.h"
  #endif
  
  /*******************************************************************************
   * Variables
   ******************************************************************************/
  #if defined BOARD_USE_CODEC
  codec_config_t boardCodecConfig = {.I2C_SendFunc    = BOARD_Codec_I2C_Send,
                                     .I2C_ReceiveFunc = BOARD_Codec_I2C_Receive,
                                     .op.Init         = WM8904_Init,
                                     .op.Deinit       = WM8904_Deinit,
                                     .op.SetFormat    = WM8904_SetAudioFormat};
  #endif
  /*******************************************************************************
   * Code
   ******************************************************************************/
  /* Initialize debug console. */
  void BOARD_InitDebugConsole(void)
  {
      uint32_t uartClkSrcFreq;
  
      RESET_ClearPeripheralReset(BOARD_DEBUG_UART_RST);
      /* attach FRG0 clock to FLEXCOMM0 (debug console) */
      CLOCK_SetFRGClock(BOARD_DEBUG_UART_FRG_CLK);
      CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
  
      uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
  
      DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
  }
  
  #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
  void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
  {
      i2c_master_config_t i2cConfig = {0};
  
      I2C_MasterGetDefaultConfig(&i2cConfig);
      I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
  }
  
  status_t BOARD_I2C_Send(I2C_Type *base,
                          uint8_t deviceAddress,
                          uint32_t subAddress,
                          uint8_t subaddressSize,
                          uint8_t *txBuff,
                          uint8_t txBuffSize)
  {
      i2c_master_transfer_t masterXfer;
  
      /* Prepare transfer structure. */
      masterXfer.slaveAddress   = deviceAddress;
      masterXfer.direction      = kI2C_Write;
      masterXfer.subaddress     = subAddress;
      masterXfer.subaddressSize = subaddressSize;
      masterXfer.data           = txBuff;
      masterXfer.dataSize       = txBuffSize;
      masterXfer.flags          = kI2C_TransferDefaultFlag;
  
      return I2C_MasterTransferBlocking(base, &masterXfer);
  }
  
  status_t BOARD_I2C_Receive(I2C_Type *base,
                             uint8_t deviceAddress,
                             uint32_t subAddress,
                             uint8_t subaddressSize,
                             uint8_t *rxBuff,
                             uint8_t rxBuffSize)
  {
      i2c_master_transfer_t masterXfer;
  
      /* Prepare transfer structure. */
      masterXfer.slaveAddress   = deviceAddress;
      masterXfer.subaddress     = subAddress;
      masterXfer.subaddressSize = subaddressSize;
      masterXfer.data           = rxBuff;
      masterXfer.dataSize       = rxBuffSize;
      masterXfer.direction      = kI2C_Read;
      masterXfer.flags          = kI2C_TransferDefaultFlag;
  
      return I2C_MasterTransferBlocking(base, &masterXfer);
  }
  #endif
  
  #if defined BOARD_USE_CODEC
  void BOARD_I3C_Init(I3C_Type *base, uint32_t clkSrc_Hz)
  {
      i3c_master_config_t i3cConfig;
  
      I3C_MasterGetDefaultConfig(&i3cConfig);
      I3C_MasterInit(base, &i3cConfig, clkSrc_Hz);
  }
  
  status_t BOARD_I3C_Send(I3C_Type *base,
                          uint8_t deviceAddress,
                          uint32_t subAddress,
                          uint8_t subaddressSize,
                          uint8_t *txBuff,
                          uint8_t txBuffSize)
  {
      i3c_master_transfer_t masterXfer;
  
      /* Prepare transfer structure. */
      masterXfer.slaveAddress   = deviceAddress;
      masterXfer.direction      = kI3C_Write;
      masterXfer.subaddress     = subAddress;
      masterXfer.subaddressSize = subaddressSize;
      masterXfer.data           = txBuff;
      masterXfer.dataSize       = txBuffSize;
      masterXfer.flags          = kI3C_TransferDefaultFlag;
  
      return I3C_MasterTransferBlocking(base, kI3C_TypeI2C, &masterXfer);
  }
  
  status_t BOARD_I3C_Receive(I3C_Type *base,
                             uint8_t deviceAddress,
                             uint32_t subAddress,
                             uint8_t subaddressSize,
                             uint8_t *rxBuff,
                             uint8_t rxBuffSize)
  {
      i3c_master_transfer_t masterXfer;
  
      /* Prepare transfer structure. */
      masterXfer.slaveAddress   = deviceAddress;
      masterXfer.subaddress     = subAddress;
      masterXfer.subaddressSize = subaddressSize;
      masterXfer.data           = rxBuff;
      masterXfer.dataSize       = rxBuffSize;
      masterXfer.direction      = kI3C_Read;
      masterXfer.flags          = kI3C_TransferDefaultFlag;
  
      return I3C_MasterTransferBlocking(base, kI3C_TypeI2C, &masterXfer);
  }
  
  void BOARD_Codec_I2C_Init(void)
  {
  #if BOARD_I3C_CODEC
      BOARD_I3C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
  #else
      BOARD_I2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
  #endif
  }
  
  status_t BOARD_Codec_I2C_Send(
      uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
  {
  #if BOARD_I3C_CODEC
      return BOARD_I3C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
  #else
      return BOARD_I2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
  #endif
                            txBuffSize);
  }
  
  status_t BOARD_Codec_I2C_Receive(
      uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  {
  #if BOARD_I3C_CODEC
      return BOARD_I3C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
  #else
      return BOARD_I2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
  #endif
  }
  #endif
  
  #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
  void BOARD_PMIC_I2C_Init(void)
  {
      BOARD_I2C_Init(BOARD_PMIC_I2C_BASEADDR, BOARD_PMIC_I2C_CLOCK_FREQ);
  }
  
  status_t BOARD_PMIC_I2C_Send(
      uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
  {
      return BOARD_I2C_Send(BOARD_PMIC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
                            txBuffSize);
  }
  
  status_t BOARD_PMIC_I2C_Receive(
      uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  {
      return BOARD_I2C_Receive(BOARD_PMIC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
  }
  
  void BOARD_Accel_I2C_Init(void)
  {
      BOARD_I2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
  }
  
  status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
  {
      uint8_t data = (uint8_t)txBuff;
  
      return BOARD_I2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
  }
  
  status_t BOARD_Accel_I2C_Receive(
      uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  {
      return BOARD_I2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
  }
  
  #endif /* SDK_I2C_BASED_COMPONENT_USED */
  