#include "stm32l0xx_hal.h"
#include "stm32l0xx_nucleo.h"
#include "usart.h"
#include "io.h"
#include "lt8920_trs.h"
#include "lt8920_test.h"
#include "xa_shield.h"
#include <stdio.h>

void clock_hsi_init(void);
static void Error_Handler(void);

//#define SENDER
//#define RECEIVER
//#define SCANNER

int main()
{
    HAL_Init();
    
    clock_hsi_init();
    
    SystemCoreClockUpdate();
    
    // При установленном шилде PA_5 настраивается как вход
    //BSP_LED_Init(LED2);
    
    BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
    
    usart_init();
    printf("Controller starting...\r\n");
    
    //io_init();
    xa_shield();
    
    BSP_LED_On(LED2);
    
    
// -------------------------------------------------------------------------- //
// RF module
    lt_init();

#if defined (SCANNER)    
    lt_scanner_init();
#elif defined (SENDER) || defined (RECEIVER)
    lt_common_init();
#endif

    while (1)
    {
        xa_shield_test();
        
        
        //lt_scanner_test();
        //lt_spi_test();
#if defined (SENDER)      
        lt_sender_test();
#elif defined (RECEIVER)
        lt_receiver_test();
#elif defined (SCANNER)
        lt_scanner_test();
#endif        
    }
}

//
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if (GPIO_Pin == KEY_BUTTON_PIN)
    {
        //BSP_LED_Toggle(LED2);
        //lt_sender_test();
    }
}


void clock_hsi_init(void)
{
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    RCC_OscInitTypeDef RCC_OscInitStruct;
    
    __HAL_RCC_PWR_CLK_ENABLE();
    
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  
/*    
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
    {
        Error_Handler();
    }
*/    
    
    /* -2- Enable HSI Oscillator, select it as PLL source and finally activate the PLL */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
    RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
    RCC_OscInitStruct.HSICalibrationValue = 0x10;
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }
    
    /* -3- Select the PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
    RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;  
    if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
    {
        Error_Handler();
    }
    
    /* -4- Optional: Disable HSE Oscillator (if the HSE is no more needed by the application) */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
}

//
static void Error_Handler(void)
{
    BSP_LED_On(LED2);
  
    while(1) {}
}