/**
  ******************************************************************************
  * @file    Project/main.c
  * @author  "Volodymyr M. Shylov" <shilow@ukr.net>
  * @version v.0.1
  * @date    24-11-2023
  * @brief   Main program body. VA-meter from DC/DC XL4005
   ******************************************************************************
  * @attention
  *
  * "THE BEER-WARE LICENSE" (Revision 42):
  * <shilow@ukr.net> wrote this file.  As long as you retain this notice you
  * can do whatever you want with this stuff. If we meet some day, and you think
  * this stuff is worth it, you can buy me a beer in return.   Shilov V.N.
  *
  ******************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "board.h"
#include "i2c.h"
#include "led.h"
#include "delay.h"
#include "htu21.h"

/* Private define ------------------------------------------------------------*/
#define TIM4_PERIOD  (uint8_t)124

/* Private typedef -----------------------------------------------------------*/
/* Private constants ---------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO uint16_t ConversionBuffer[ADC_SMPLS];
__IO uint8_t BufferIndex = 0;
static int16_t Temperature;
static uint16_t Humidity;

/* Private function prototypes -----------------------------------------------*/
static void boardInit(void);
static void showT(void);
static void showH(void);

void main(void)
{
  /* Board Configuration */
  boardInit();
  Delay(1000);
  LedDigits[3] = led_O;
  LedDigits[7] = led_H;

  /* I2C Configuration */
  i2c_master_init();

  /* Infinite loop */
  while (1) {
    htu_GetTemperature(&Temperature);
    showT();
    Delay(410);
    htu_GetHumidity(&Humidity);
    showH();
    Delay(440);
  }

}

/* Private functions ---------------------------------------------------------*/
static void showT(void) {
  int16_t t = (Temperature + 5) / 10;;
  uint8_t a;

  LedDigits[0] = t / 100;
  a = t % 100;
  LedDigits[1] = a / 10;
  LedDigits[2] = a % 10;
}

static void showH(void) {
 uint8_t a;

 LedDigits[4] = Humidity / 100;
 a = Humidity % 100;
 LedDigits[5] = a / 10;
 LedDigits[6] = a % 10;
}

static void boardInit(void) {
  /* Насколько я понял, для частот выше 16МГц нужно вводить задержку при работе с FLASH:
    - Before using the HSE clock make sure that the "Flash_Wait_States" is set to 1.
    - To do so :
     - with STVD (menu: Debug Instrument -> MCU configuration -> Options)
     - with EWSTM8 (menu: ST-LINK -> Option bytes ->Flash_Wait_States: 1)
  */

  /* Initialization of the clock to 16MHz */
  CLK->CKDIVR = 0x00;

  /* Disable clock of unused peripherial */
  CLK->PCKENR1 = (uint8_t)(~(CLK_PCKENR1_UART1 | CLK_PCKENR1_SPI));
  CLK->PCKENR2 = (uint8_t)(~(CLK_PCKENR2_CAN | CLK_PCKENR2_AWU)); // | CLK_PCKENR2_ADC

  /* SWIM clock */
  CLK->SWIMCCR = 0x00;

  /* Configure GPIO used for LED to Push-Pull, High, Fast*/
  LED_SEG1_PORT->ODR |= (uint8_t)LED_SEG1_PINS;
  LED_SEG1_PORT->DDR |= (uint8_t)LED_SEG1_PINS;
  LED_SEG1_PORT->CR1 |= (uint8_t)LED_SEG1_PINS;
  LED_SEG1_PORT->CR2 |= (uint8_t)LED_SEG1_PINS;

  LED_SEG2_PORT->ODR |= (uint8_t)LED_SEG2_PINS;
  LED_SEG2_PORT->DDR |= (uint8_t)LED_SEG2_PINS;
  LED_SEG2_PORT->CR1 |= (uint8_t)LED_SEG2_PINS;
  LED_SEG2_PORT->CR2 |= (uint8_t)LED_SEG2_PINS;

  LED_SEG3_PORT->ODR |= (uint8_t)LED_SEG3_PINS;
  LED_SEG3_PORT->DDR |= (uint8_t)LED_SEG3_PINS;
  LED_SEG3_PORT->CR1 |= (uint8_t)LED_SEG3_PINS;
  LED_SEG3_PORT->CR2 |= (uint8_t)LED_SEG3_PINS;

  /* `SPI` pins to Push-Pull, High, Fast */
  SPI_PORT->ODR |= (uint8_t)(SPI_SCK|SPI_DATA);
  SPI_PORT->DDR |= (uint8_t)(SPI_SCK|SPI_DATA);
  SPI_PORT->CR1 |= (uint8_t)(SPI_SCK|SPI_DATA);
  SPI_PORT->CR2 |= (uint8_t)(SPI_SCK|SPI_DATA);

  /** Configure ADC */
  /* De-Init ADC peripheral*/
  //ADC1_DeInit();

  /* Init ADC1 peripheral */
  //ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC_CHNLU, ADC1_PRESSEL_FCPU_D12, \
            ADC1_EXTTRIG_TIM, ENABLE, ADC1_ALIGN_RIGHT, ADC_SCHTU, DISABLE);

  /* Enable EOC interrupt */
  //ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE);

  /*Start Conversion */
  ////ADC1_StartConversion();

  /** Configure TIM1 */
  //TIM1_DeInit();

  /* Time Base configuration */
  /*
  Timer period - 3,125 ms
  TIM1_Period = 50
  TIM1_Prescaler = 1000
  TIM1_CounterMode = TIM1_COUNTERMODE_UP
  TIM1_RepetitionCounter = 0
  */
  //TIM1_TimeBaseInit(999, TIM1_COUNTERMODE_UP, 49, 0);

  /* Trigrer configuration */
  //TIM1_SelectOutputTrigger(TIM1_TRGOSOURCE_UPDATE);

  /* Update Interrupt Enable */
  ////TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);

  /* Enable TIM1 */
  //TIM1_Cmd(ENABLE);

  /**
  TIM4 configuration:
   - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
     clock used is 16 MHz / 128 = 125 000 Hz
   - With 125 000 Hz we can generate time base:
     max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
     min time base is 0.016 ms if TIM4_PERIOD = 1   --> (  1 + 1) / 125000 = 0.016 ms
   - In this example we need to generate a time base equal to 1 ms
     so TIM4_PERIOD = (0.001 * 125000 - 1) = 124
   */

  /* Time base configuration. Set the Prescaler value */
  TIM4->PSCR = (uint8_t)(TIM4_PRESCALER_128);
  /* Set the Autoreload value */
  TIM4->ARR = (uint8_t)(TIM4_PERIOD);
  /* Clear TIM4 update flag */
  TIM4->SR1 = (uint8_t)(~TIM4_FLAG_UPDATE);
  /* Enable update interrupt */
  TIM4->IER |= (uint8_t)TIM4_IT_UPDATE;

  /* enable interrupts */
  enableInterrupts();

  /* Enable TIM4 */
  TIM4->CR1 |= (uint8_t)TIM4_CR1_CEN;
}

#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/************************ (C) COPYRIGHT Shilov V.N. *****END OF FILE****/
