#include "log_params.h"
#include "log_to_memory.h"
#include "MemoryFunctions.h"

//#pragma DATA_SECTION(log_params, ".fast_vars");
TYPE_LOG_PARAMS log_params = TYPE_LOG_PARAMS_DEFAULT;

void defineVarsErrSlowLogs(void)
{
    //volatile static
    unsigned long l1;

    //fastlog
    log_params.volume_of_fast_log = VOLUME_OF_FAST_LOG;
	log_params.start_address_log = START_ADDRESS_LOG;
	l1 = (unsigned long)(log_params.BlockSizeErr*VOLUME_OF_FAST_LOG);
//	l1 = (unsigned long)(VOLUME_OF_FAST_LOG) * l1;
	log_params.end_address_log = (unsigned long)log_params.start_address_log + (unsigned long)l1 - 1;
	// выравниваем конец блока если вышел за границы
	while (log_params.end_address_log >= START_ADDRESS_LOG_SLOW)
	{
	    log_params.end_address_log -= log_params.BlockSizeErr;
	    log_params.volume_of_fast_log--;
	}

	l1 = (unsigned long)(log_params.volume_of_fast_log/3);
	l1 = (unsigned long)(log_params.BlockSizeErr*l1);
	log_params.end_address_log_level_1 = (unsigned long)log_params.start_address_log + (unsigned long)l1 - 1;
	log_params.end_address_log_level_2 = log_params.end_address_log_level_1 + (unsigned long)l1 - 1;


	//slow log
    log_params.volume_of_slow_log = VOLUME_OF_SLOW_LOG;
	log_params.start_address_log_slow = ((unsigned long)log_params.end_address_log + 0x1);
	l1 = (unsigned long)VOLUME_OF_SLOW_LOG * (unsigned long)log_params.BlockSizeSlow;
	log_params.end_address_log_slow = ((unsigned long)log_params.start_address_log_slow + l1 - 1);
    // выравниваем конец блока если вышел за границы
    while (log_params.end_address_log_slow >= END_ADDRESS_LOGS)
    {
        log_params.end_address_log_slow -= log_params.BlockSizeSlow;
        log_params.volume_of_slow_log--;
    }
    l1 = (unsigned long)(log_params.volume_of_slow_log/3);
    l1 = (unsigned long)(log_params.BlockSizeSlow*l1);
    log_params.end_address_log_slow_level_1 = (unsigned long)log_params.start_address_log_slow + (unsigned long)l1 - 1;
    log_params.end_address_log_slow_level_2 = log_params.end_address_log_slow_level_1 + (unsigned long)l1 - 1;

//	log_params.start_address_save_log_memory = START_ADDRESS_SAVE_ON_ALARM;
//	log_params.end_address_save_log_memory = END_ADDRESS_LOGS;

//	log_params.end_address_log_slow_level_2 = log_params.end_address_save_log_memory - 0x2000;
//	log_params.end_address_log_slow_level_3 = log_params.end_address_save_log_memory - 0x4000;


    // индексы в начало
    log_params.addres_mem = log_params.start_address_log;
    log_params.addres_mem_slow = log_params.start_address_log_slow;

}

void initLogSize(unsigned int c_fast, unsigned int c_slow)
{
    log_params.size_slow_done = 0;
    log_params.size_fast_done = 0;
	log_params.init = 0;

	if (c_fast>SIZE_LOGS_ARRAY)
	    c_fast = SIZE_LOGS_ARRAY;

    if (c_slow>SIZE_LOGS_ARRAY)
        c_slow = SIZE_LOGS_ARRAY;

	logsdata.block_size_fast = c_fast;
    logsdata.block_size_slow = c_slow;

	getFastLogs(1);
	getSlowLogs(1);
	
//	log_params.BlockSizeErr = logpar.block_size_counter_fast;//block_size_counter_fast;
//	log_params.BlockSizeSlow = logpar.block_size_counter_slow;

	defineVarsErrSlowLogs();

	log_params.init = 1;

	clear_mem_all();


//    clear_mem(FAST_LOG);
//    clear_mem(SLOW_LOG);

	//	set_start_mem(FAST_LOG);
	//	getFastLogs();
	//	log_params.BlockSizeErr = block_size_counter_fast;
	//	set_start_mem(SLOW_LOG);
	//	getLogs();
	//	log_params.BlockSizeSlow = block_size_counter_slow;
}

//void initErrLog()
//{
//
//	static unsigned long SizeLogErr = 0;
//	static unsigned long SizeLogSlow = 0;
//	unsigned int sizeHiword = 0;
//	unsigned int sizeLoword = 0;
//	unsigned int addrHiword = 0;
//	unsigned int addrLoword = 0;
//
//	set_start_mem(FAST_LOG);
////	set_start_mem(ALARM_SAVE_MEMORY);
//	set_start_mem(INIT_LOG);
////	set_start_mem(ERR_LOG);
//
//	clear_mem(INIT_LOG);
//	clear_mem(FAST_LOG);
////	clear_mem(ALARM_SAVE_MEMORY);
////	clear_mem(ERR_LOG);
//
////	SizeLog = log_params.end_address_log - log_params.start_address_log;				 //  END_ADDRESS_LOG - START_ADDRESS_LOG; //ERR_BLOCK_SIZE*600;
//	SizeLogErr = log_params.end_address_err_log - log_params.start_address_err_log;	//  END_ADDRESS_ERR_LOG - START_ADDRESS_ERR_LOG;//(long)ERR_BLOCK_SIZE*(long)3000;
//	SizeLogSlow = log_params.end_address_save_log_memory - log_params.start_address_save_log_memory; //  END_ADDRESS_LOG_SLOW - START_ADDRESS_LOG_SLOW;
//
//	sizeHiword = SizeLogErr >> 16;
//	sizeLoword = SizeLogErr;
//
//	WriteMemory(ADDR_SIZE_ERR_LOW, sizeLoword);
//	WriteMemory(ADDR_SIZE_ERR_HIGH, sizeHiword);
//
//	sizeHiword = SizeLogSlow >> 16;
//	sizeLoword = SizeLogSlow;
//	WriteMemory(ADDR_SIZE_SLOW_LOW, sizeLoword);
//	WriteMemory(ADDR_SIZE_SLOW_HIGH, sizeHiword); //!!!
//
//	WriteMemory(ADDR_ERR_BLOCK_SIZE, log_params.BlockSizeErr);
//	WriteMemory(ADDR_SLOW_BLOCK_SIZE, log_params.BlockSizeSlow);
//
//	addrHiword = log_params.start_address_err_log >> 16;
//	addrLoword = log_params.start_address_err_log;
//	WriteMemory(ADDR_START_ADDR_ERR_LOG_LOW, addrLoword);
//	WriteMemory(ADDR_START_ADDR_ERR_LOG_HIGH, addrHiword);
//
//	addrHiword = log_params.start_address_save_log_memory >> 16;
//	addrLoword = log_params.start_address_save_log_memory;
//	WriteMemory(ADDR_START_ADDR_SLOW_LOG_LOW, addrLoword);
//	WriteMemory(ADDR_START_ADDR_SLOW_LOG_HIGH, addrHiword);
//}


