/**
 ******************************************************************************
 * @file    radio_board_if.c
 * @author  YunHorn Technology Limited Application Team
 * @brief   This file provides an interface layer between MW and Radio Board
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2023 YunHorn Technology Limited.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */
/* Includes ------------------------------------------------------------------*/
#include "radio_board_if.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* External variables ---------------------------------------------------------*/
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Exported functions --------------------------------------------------------*/
int32_t RBI_Init(void)
{
  /* USER CODE BEGIN RBI_Init_1 */

  /* USER CODE END RBI_Init_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return BSP_RADIO_Init();
#elif defined(YUNHORN_STS_WLE5CCxx)
  /* should be calling BSP_RADIO_Init() but not supported by MX*/

  GPIO_InitTypeDef  gpio_init_structure = {0};

#if defined(YUNHORN_STS_RF_CHANNEL_SWITCH)
    /* Enable the Radio Switch Clock */
    RF_SW_CTRL1_GPIO_CLK_ENABLE();
    RF_SW_CTRL2_GPIO_CLK_ENABLE();
    /* Configure the Radio Switch pin */
    gpio_init_structure.Pin   = RF_SW_CTRL1_PIN;
    gpio_init_structure.Mode  = GPIO_MODE_OUTPUT_PP;
    gpio_init_structure.Pull  = GPIO_NOPULL;
    gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    HAL_GPIO_Init(RF_SW_CTRL1_GPIO_PORT, &gpio_init_structure);
    gpio_init_structure.Pin = RF_SW_CTRL2_PIN;
    HAL_GPIO_Init(RF_SW_CTRL2_GPIO_PORT, &gpio_init_structure);

    HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
#else
  /* Enable the Radio Switch Clock */
  RF_SW_CTRL3_GPIO_CLK_ENABLE();

  /* Configure the Radio Switch pin */
  gpio_init_structure.Pin   = RF_SW_CTRL1_PIN;
  gpio_init_structure.Mode  = GPIO_MODE_OUTPUT_PP;
  gpio_init_structure.Pull  = GPIO_NOPULL;
  gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  HAL_GPIO_Init(RF_SW_CTRL1_GPIO_PORT, &gpio_init_structure);

  gpio_init_structure.Pin = RF_SW_CTRL2_PIN;
  HAL_GPIO_Init(RF_SW_CTRL2_GPIO_PORT, &gpio_init_structure);

  gpio_init_structure.Pin = RF_SW_CTRL3_PIN;
  HAL_GPIO_Init(RF_SW_CTRL3_GPIO_PORT, &gpio_init_structure);

  HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN, GPIO_PIN_RESET);
#endif

  return 0;
#else
  /* USER CODE BEGIN RBI_Init_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_Init_2 */
#endif /* USE_BSP_DRIVER  || YUNHORN_STS_WLE5CCxx  */
  /* USER CODE BEGIN RBI_Init_3 */

  /* USER CODE END RBI_Init_3 */
}

int32_t RBI_DeInit(void)
{
  /* USER CODE BEGIN RBI_DeInit_1 */

  /* USER CODE END RBI_DeInit_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return BSP_RADIO_DeInit();
#elif defined(YUNHORN_STS_WLE5CCxx)

#if defined(YUNHORN_STS_RF_CHANNEL_SWITCH)
    RF_SW_CTRL1_GPIO_CLK_ENABLE();
    RF_SW_CTRL2_GPIO_CLK_ENABLE();
    /* Turn off switch */
    HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
    /* DeInit the Radio Switch pin */
    HAL_GPIO_DeInit(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN);
    HAL_GPIO_DeInit(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN);
#else
  RF_SW_CTRL3_GPIO_CLK_ENABLE();

  /* Turn off switch */
  HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN, GPIO_PIN_RESET);

  /* DeInit the Radio Switch pin */
  HAL_GPIO_DeInit(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN);
  HAL_GPIO_DeInit(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN);
  HAL_GPIO_DeInit(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN);
#endif

  return 0;
#else
  /* USER CODE BEGIN RBI_DeInit_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_DeInit_2 */
#endif  /* USE_BSP_DRIVER  || MX_NUCLEO_WL55JC1 */
  /* USER CODE BEGIN RBI_DeInit_3 */

  /* USER CODE END RBI_DeInit_3 */
}

int32_t RBI_ConfigRFSwitch(RBI_Switch_TypeDef Config)
{
  /* USER CODE BEGIN RBI_ConfigRFSwitch_1 */

  /* USER CODE END RBI_ConfigRFSwitch_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return BSP_RADIO_ConfigRFSwitch((BSP_RADIO_Switch_TypeDef) Config);
#elif defined(YUNHORN_STS_WLE5CCxx)
  switch (Config)
  {
    case RBI_SWITCH_OFF:
    {
#if defined(YUNHORN_STS_RF_CHANNEL_SWITCH)
        /* Turn off switch */
        HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
#else
      /* Turn off switch */
      HAL_GPIO_WritePin(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN, GPIO_PIN_RESET);
      HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
      HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
#endif
      break;
    }
    case RBI_SWITCH_RX:
    {
#if defined(YUNHORN_STS_RF_CHANNEL_SWITCH)
        /*Turns On in Rx Mode the RF Switch */
        HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_SET);
        HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
#else
      /*Turns On in Rx Mode the RF Switch */
      HAL_GPIO_WritePin(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN, GPIO_PIN_SET);
      HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_SET);
      HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_RESET);
#endif
      break;
    }
    case RBI_SWITCH_RFO_LP:
    {
#if defined(YUNHORN_STS_RF_CHANNEL_SWITCH)
        /*Turns On in Tx Low Power the RF Switch */
        HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_SET);
#else
      /*Turns On in Tx Low Power the RF Switch */
      HAL_GPIO_WritePin(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN, GPIO_PIN_SET);
      HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_SET);
      HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_SET);
#endif
      break;
    }
    case RBI_SWITCH_RFO_HP:
    {
#if defined(YUNHORN_STS_RF_CHANNEL_SWITCH)
        /*Turns On in Tx High Power the RF Switch */
        HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_SET);
#else
      /*Turns On in Tx High Power the RF Switch */
      HAL_GPIO_WritePin(RF_SW_CTRL3_GPIO_PORT, RF_SW_CTRL3_PIN, GPIO_PIN_SET);
      HAL_GPIO_WritePin(RF_SW_CTRL1_GPIO_PORT, RF_SW_CTRL1_PIN, GPIO_PIN_RESET);
      HAL_GPIO_WritePin(RF_SW_CTRL2_GPIO_PORT, RF_SW_CTRL2_PIN, GPIO_PIN_SET);
#endif
      break;
    }
    default:
      break;
  }

  return 0;
#else
  /* USER CODE BEGIN RBI_ConfigRFSwitch_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_ConfigRFSwitch_2 */
#endif /* USE_BSP_DRIVER  || YUNHORN_STS_WLE5CCxx */
  /* USER CODE BEGIN RBI_ConfigRFSwitch_3 */

  /* USER CODE END RBI_ConfigRFSwitch_3 */
}

int32_t RBI_GetTxConfig(void)
{
  /* USER CODE BEGIN RBI_GetTxConfig_1 */

  /* USER CODE END RBI_GetTxConfig_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return BSP_RADIO_GetTxConfig();

#elif defined(YUNHORN_STS_WLE5CCxx)
  return RBI_CONF_RFO;
#else
  /* USER CODE BEGIN RBI_GetTxConfig_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_GetTxConfig_2 */
#endif /* USE_BSP_DRIVER  || YUNHORN_STS_WLE5CCxx */
  /* USER CODE BEGIN RBI_GetTxConfig_3 */

  /* USER CODE END RBI_GetTxConfig_3 */
}

int32_t RBI_GetWakeUpTime(void)
{
  /* USER CODE BEGIN RBI_GetWakeUpTime_1 */

  /* USER CODE END RBI_GetWakeUpTime_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return  BSP_RADIO_GetWakeUpTime();

#elif defined(YUNHORN_STS_WLE5CCxx)
  return RF_WAKEUP_TIME;
#else
  /* USER CODE BEGIN RBI_GetWakeUpTime_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_GetWakeUpTime_2 */
#endif /* USE_BSP_DRIVER  || YUNHORN_STS_WLE5CCxx */
  /* USER CODE BEGIN RBI_GetWakeUpTime_3 */

  /* USER CODE END RBI_GetWakeUpTime_3 */
}

int32_t RBI_IsTCXO(void)
{
  /* USER CODE BEGIN RBI_IsTCXO_1 */

  /* USER CODE END RBI_IsTCXO_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return BSP_RADIO_IsTCXO();

#elif defined(YUNHORN_STS_WLE5CCxx)
  return IS_TCXO_SUPPORTED;
#else
  /* USER CODE BEGIN RBI_IsTCXO_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_IsTCXO_2 */
#endif /* USE_BSP_DRIVER   || YUNHORN_STS_WLE5CCxx */
  /* USER CODE BEGIN RBI_IsTCXO_3 */

  /* USER CODE END RBI_IsTCXO_3 */
}

int32_t RBI_IsDCDC(void)
{
  /* USER CODE BEGIN RBI_IsDCDC_1 */

  /* USER CODE END RBI_IsDCDC_1 */
#if defined(USE_BSP_DRIVER)
  /* code generated by MX does not support BSP */
  /* In order to use BSP driver, add the correspondent files in the IDE workspace */
  /* and define USE_BSP_DRIVER in the preprocessor definitions  or in platform.h */
  return BSP_RADIO_IsDCDC();

#elif defined(YUNHORN_STS_WLE5CCxx)
  return IS_DCDC_SUPPORTED;
#else
  /* USER CODE BEGIN RBI_IsDCDC_2 */
#error user to provide its board code or to call his board driver functions
  /* USER CODE END RBI_IsDCDC_2 */
#endif /* USE_BSP_DRIVER   || YUNHORN_STS_WLE5CCxx */
  /* USER CODE BEGIN RBI_IsDCDC_3 */

  /* USER CODE END RBI_IsDCDC_3 */
}

int32_t RBI_GetRFOMaxPowerConfig(RBI_RFOMaxPowerConfig_TypeDef Config)
{
  /* USER CODE BEGIN RBI_GetRFOMaxPowerConfig_1 */

  /* USER CODE END RBI_GetRFOMaxPowerConfig_1 */
#if defined(USE_BSP_DRIVER)
  /* Important note: BSP code is board dependent
   * STM32WL_Nucleo code can be found
   *       either in STM32CubeWL package under Drivers/BSP/STM32WLxx_Nucleo/
   *       or at https://github.com/STMicroelectronics/STM32CubeWL/tree/main/Drivers/BSP/STM32WLxx_Nucleo/
   * 1/ For User boards, the BSP/STM32WLxx_Nucleo/ directory can be copied and replaced in the project. The copy must then be updated depending:
   *       on board RF switch configuration (pin control, number of port etc)
   *       on TCXO configuration
   *       on DC/DC configuration
   *       on maximum output power that the board can deliver*/
  return BSP_RADIO_GetRFOMaxPowerConfig((BSP_RADIO_RFOMaxPowerConfig_TypeDef) Config);
#else
  /* 2/ Or implement RBI_RBI_GetRFOMaxPowerConfig here */
  int32_t ret = 0;
  /* USER CODE BEGIN RBI_GetRFOMaxPowerConfig_2 */
  /* #warning user to provide its board code or to call his board driver functions */
  if (Config == RBI_RFO_LP_MAXPOWER)
  {
    ret = 15; /*dBm*/
  }
  else
  {
    ret = 22; /*dBm*/
  }
  /* USER CODE END RBI_GetRFOMaxPowerConfig_2 */
  return ret;
#endif  /* USE_BSP_DRIVER  */
}

/* USER CODE BEGIN EF */

/* USER CODE END EF */

/* Private Functions Definition -----------------------------------------------*/
/* USER CODE BEGIN PrFD */

/* USER CODE END PrFD */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
