#include "at32f403a_407.h"
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#include "common_config.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
extern "C" {
#include "usb_eth.h"
#include "mux.h"
#include "misc.h"
#include "spi_common.h"
#include "user_fatfs.h"
#include "spi_flash.h"
#include "usb_eth.h"
#include "extended_sram.h"
#include "modbus.h"
#include "common_gpio.h"
#include "io.h"
#include "input.h"
#include "output.h"
#include "sys_api.h"
#include "settings_api.h"
#include "update.h"
#include "uptime.h"
#include "rtc.h"
#include "mb.h"
#include "io_utils.h"
#include "buttons.h"
#include "adc_transport.h"
#include "shift_reg.h"
#include "analog_input.h"
#include "dac_transport.h"
#include "log.h"
#include "swap.h"
#include "utility.h"
#include "soft_test.h"
#include "soft_wdt.h"
#include "usb_clock.h"
}
#include "terminal_sbs.h"
#include "terminal_usartbridge.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>


void init_task(void *argument);


//
int main(void)
{
    __disable_irq();
    nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x08021000);
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
	__enable_irq();
  
    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
    
    extend_SRAM();
  
    system_clock_config();

    delay_init();

                    
    xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
    
    
#if defined (MAI_12)    
    xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
#endif

#if defined (MAO_8)    
    xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
#endif    
    
        
    vTaskStartScheduler();
     
    while (1) {}
}


void init_task(void *argument)
{
// -------------------------------------------------------------------------- //    
// Software watchdog
  
    gpio_wdt_init();
    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
    
// -------------------------------------------------------------------------
// CLI
    
    sbsTerminal.configure();
    terminalUsartBridge.configure();    
    
// -------------------------------------------------------------------------- //    
// Настройки. Загрузка и проверка.

    // Мьютекс для работы с настройками
    init_settings(); 
    
    // Системные настройки и копия
    sys_settings_load(&sys_settings);
    memcpy(&temp_sys_settings, &sys_settings, sizeof(sys_settings_t));
    
    // Основные настройки
    settings_load(&settings);
    
    // Настройки параметров порта Modbus
    settings_set_modbus_params(settings.com_settings.mb_port);
    
// -------------------------------------------------------------------------- //    
// Безопасный режим, входы, выходы
    
    save_mode_init();
    
    save_mode_set(true);   
  
    io_port_init();
    
    //in_exint_init();    

// -------------------------------------------------------------------------- //
// Кнопки [SET, RESET]
    
    button_init();
    xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
    
// -------------------------------------------------------------------------- //
// Uptime    
    
    uptime_init();
    
// -------------------------------------------------------------------------- //
// RTC    
    
    TM_RTC_Init();
    rtc_subtim_init();
    
// -------------------------------------------------------------------------- //    
// Мультиплексор
    
    mux_gpio_init();
    xTaskCreate(mux_task, "mux_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
   
// -------------------------------------------------------------------------- //            
// Базовая инициализация входов/выходов
// TODO потом брать значения из настроек

    io_init();

    xTaskCreate(input_task, "input_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
    
// -------------------------------------------------------------------------- //        
// Modbus    
    
    mb_init();
    
// -------------------------------------------------------------------------- //        
// Сброс счетчика попыток загрузок
    
    update_reset_boot_try();    
    
// -------------------------------------------------------------------------- //    
// Ревизия платы
    
    gpio_get_rev();
    
// -------------------------------------------------------------------------- //    
// SPI flash, журнал событий, архив

    common_spi_init();
    spi_flash_init();
    
    log_init(false);
    
// -------------------------------------------------------------------------- //
// Статистика FreeRTOS
    
    utl_stat_timer_init();
    
// -------------------------------------------------------------------------- //
// EVENT. Обновление FW.
    
    uint16_t update_status = bpr_data_read(BACKUP_UPDATE_STATUS);
    
    if (update_status == IAP_UPDATE_OK)
        log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_OK, 0, atof(FW_VERSION));
    else if (update_status == IAP_UPDATE_ERROR)
        log_add_entry(LOG_UPDATE_FW, LOG_EVENT_STATE_ERR, 0, atof(FW_VERSION));
        
    bpr_data_write(BACKUP_UPDATE_STATUS, IAP_UPDATE_NO);

// -------------------------------------------------------------------------- //
// EVENT. Включение питания/перезагрузка.    
    
    log_add_entry(LOG_SYSTEM_BOOT, (log_event_state_t)0, 0, 0);
   
// -------------------------------------------------------------------------- //    
// USB (RNDIS class)
#ifdef USB_RNDIS    
    usb_clock48m_select(USB_CLK_HEXT);
    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
    usb_init();
    usb_eth_init();
#endif 
    
// Удаляем стартовую задачу 
    vTaskDelete(NULL);
    
// -------------------------------------------------------------------------- //        
// Тесты

    //usb_eth_init();
    //sys_clear();
    //mux_led_test_init();
    //pwm_test();
    
    // -------------------------------------------------------------------------- //    
// Сдвиговые регистры и мультиплексоры    
    //sh_init();
    
// Тесты аналоговых входов    
    //ai_connect_test();
    //ai_mode_test();
    //ai_connect_channel(V_ISO);  
    //ai_connect_channel(AN_INP_1);  
    //ai_connect_channel(AN_INP_7);  
    
// -------------------------------------------------------------------------- //    
// DAC    
/*    
    dac_gpio_init();
    dac_test(CH_DAC_1, 30000);  
*/    
/*    
    dac_test(CH_DAC_2, 10000);  
    dac_test(CH_DAC_3, 10000);
    dac_test(CH_DAC_4, 10000);  
*/    
/*    
    dac_test(CH_DAC_1, 10000);  
    dac_test(CH_DAC_2, 10000);  
    dac_test(CH_DAC_3, 10000);
    dac_test(CH_DAC_4, 10000);  
*/  
    
#if 0
    // Настройки по умолчанию 
    settings_set_all_default();
    settings_save(&settings);
#endif    
    
// -------------------------------------------------------------------------- //        
// Отдельный таск для предварительных тестов
    
    // Разное
    //xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
    
    // Тесты GPIO
    //xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
}