/**
  ******************************************************************************
  * @file    Hyperterminal\main.c
  * @brief   This file contains the main function for interface Hyperterminal with STM8S UART3.
  * @author  STMicroelectronics - MCD Application Team
  * @version V1.0.0
  * @date    12-January-2018
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
#include "hyperterminal.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
u32 LSIMeasurment(void); // function used for beeper calibration

/**
  * @brief Validation firmware main entry point.
  * @par Parameters:
  * None
  * @retval
  * None
  */
void main(void)
{
  /*High speed internal clock prescaler: 1*/
  CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);

  /* Enable USART3, BEEP, TIM2 and TIM3 peripheral clocks*/
  CLK_PeripheralClockConfig(CLK_Peripheral_USART3, ENABLE);
  CLK_PeripheralClockConfig(CLK_Peripheral_BEEP, ENABLE);
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE);

  /* Enable LSI Clock */
  CLK_LSICmd(ENABLE);
  while(CLK_GetFlagStatus(CLK_FLAG_LSIRDY) != SET);
  CLK_BEEPClockConfig(CLK_BEEPCLKSource_LSI);

  /* Time base configuration */
  TIM3_DeInit();
  TIM3_TimeBaseInit(TIM3_Prescaler_1, TIM3_CounterMode_Up, 0xFFFF);
  TIM3_ITConfig(TIM3_IT_Update, ENABLE);

  /* Enable general interrupts */
  enableInterrupts();

  /* Set GPIO for LED LD1 (PB5)  */
  GPIO_Init(GPIOB, GPIO_Pin_5 , GPIO_Mode_Out_PP_High_Fast);

  /* UART3 configuration     -------------------------------------*/
  /* UART3 configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - Odd parity
        - Receive and transmit enabled
        - UART3 Clock disabled
  */

  GPIO_ExternalPullUpConfig(GPIOG, GPIO_Pin_0, ENABLE);
  GPIO_ExternalPullUpConfig(GPIOG, GPIO_Pin_1, ENABLE);

  USART_DeInit(USART3);
  USART_Init(USART3, (u32)9600, USART_WordLength_9b, USART_StopBits_1, USART_Parity_Odd, (USART_Mode_TypeDef)(USART_Mode_Tx | USART_Mode_Rx));
  USART_Cmd(USART3,ENABLE);

  /* BEEP calibration */
  BEEP_LSICalibrationConfig(LSIMeasurment());

  /*Main loop*/
  while (1)
  {
    Main_Menu();
  }
}

/**
  * @brief LSI Measurment.
  * @par Parameters:
  * None
  * @retval
  * u32 LSI Value
  */
u32 LSIMeasurment(void)
{
  u32 lsi_freq_hz = 0x0;
  u32 fmaster = 0x0;
  u16 ICValue1 = 0x0;
  u16 ICValue2 = 0x0;

  /* Get master frequency */
  fmaster = CLK_GetClockFreq();

  /* Enable the LSI measurement: LSI clock connected to timer Input Capture 1 */
  BEEP->CSR1 |= BEEP_CSR1_MSR;

  /* Measure the LSI frequency with TIMER Input Capture 1 */

  /* Capture only every 8 events!!! */
  /* Enable capture of TI1 */
  TIM2_ICInit(TIM2_Channel_1, TIM2_ICPolarity_Rising, TIM2_ICSelection_DirectTI, TIM2_ICPSC_DIV8, 0);

  /* Enable TIM2 */
  TIM2_Cmd(ENABLE);

  /* wait a capture on cc1 */
  while ((TIM2->SR1 & TIM2_FLAG_CC1) != TIM2_FLAG_CC1);

  /* Get CCR1 value*/
  ICValue1 = TIM2_GetCapture1();
  TIM2_ClearFlag(TIM2_FLAG_CC1);

  /* wait a capture on cc1 */
  while ((TIM2->SR1 & TIM2_FLAG_CC1) != TIM2_FLAG_CC1);

  /* Get CCR1 value*/
  ICValue2 = TIM2_GetCapture1();
  TIM2_ClearFlag(TIM2_FLAG_CC1);

  /* Disable IC1 input capture */
  TIM2->CCER1 &= (u8)(~TIM_CCER1_CC1E);

  /* Disable timer3 */
  TIM2_Cmd(DISABLE);

  /* Compute LSI clock frequency */
  lsi_freq_hz = (8 * fmaster) / (ICValue2 - ICValue1);

  /* Disable the LSI measurement: LSI clock disconnected from timer Input Capture 1 */
  BEEP->CSR1 &= (u8)(~BEEP_CSR1_MSR);

 return (lsi_freq_hz);
}

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