You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
10 KiB
C++

/*
* FRAMDATABASE.cpp
*
* Author: Aleksey Gerasimenko
* gerasimenko.aleksey.n@gmail.com
*/
#include "FRAM/FRAMDATABASE.h"
namespace FRAM
{
FRAMDATABASE::FRAMDATABASE():
m_mode(),
m_state(),
m_fram_data_buffer(),
m_buffer_pointer((uint16_t*)&m_fram_data_buffer),
m_fram_object(),
m_fram_size(FRAM_FRAM_SIZE),
m_fram_object_index(0),
m_fram_object_address(0),
m_fram_object_last_address(0),
m_fram_rw_index(0),
m_header(),
m_footer(),
m_verify(false)
{
m_header.class_id = HEADER_CLASS_ID;
m_header.part_id = HEADER_PART_ID;
m_header.software_version = HEADER_SOFTWARE_VERSION;
m_header.size_of_fram_data = sizeof(m_fram_data_buffer);
m_footer.foot = FOOTER_FOOT;
}
//
FRAM::FRAMDATABASE::mode_t FRAMDATABASE::get_mode()
{
return m_mode;
//
}//
//
bool FRAMDATABASE::compare_mode(FRAM::FRAMDATABASE::mode_t mode)
{
return mode == m_mode;
//
}//
//
FRAM::FRAMDATABASE::state_t FRAMDATABASE::get_state()
{
return m_state;
//
}//
//
bool FRAMDATABASE::compare_state(FRAM::FRAMDATABASE::state_t state)
{
return state == m_state;
//
}//
//
bool FRAMDATABASE::is_free()
{
return m_state == FRAM::FRAMDATABASE::FREE;
}//
//
bool FRAMDATABASE::is_busy()
{
return m_state == FRAM::FRAMDATABASE::BUSY;
}//
//
bool FRAMDATABASE::is_read()
{
return m_mode == FRAM::FRAMDATABASE::READ;
}//
//
bool FRAMDATABASE::is_burn()
{
return m_mode == FRAM::FRAMDATABASE::BURN;
}//
//
bool FRAMDATABASE::is_erase()
{
return m_mode == FRAM::FRAMDATABASE::ERASE;
}//
//
bool FRAMDATABASE::is_restore()
{
return m_mode == FRAM::FRAMDATABASE::RESTORE;
}//
//
bool FRAMDATABASE::is_verify()
{
return m_mode == FRAM::FRAMDATABASE::VERIFY;
}//
//
void FRAMDATABASE::set_read()
{
m_state = FRAM::FRAMDATABASE::BUSY;
m_mode = FRAM::FRAMDATABASE::READ;
m_fram_rw_index = 0;
//
}//
//
void FRAMDATABASE::set_burn()
{
m_state = FRAM::FRAMDATABASE::BUSY;
m_mode = FRAM::FRAMDATABASE::BURN;
m_fram_rw_index = 0;
//
}//
//
void FRAMDATABASE::set_erase()
{
m_state = FRAM::FRAMDATABASE::BUSY;
m_mode = FRAM::FRAMDATABASE::ERASE;
m_fram_rw_index = 0;
//
}//
//
void FRAMDATABASE::set_verify()
{
m_state = FRAM::FRAMDATABASE::BUSY;
m_mode = FRAM::FRAMDATABASE::VERIFY;
m_fram_rw_index = 0;
//
}//
//
void FRAMDATABASE::set_restore()
{
m_state = FRAM::FRAMDATABASE::BUSY;
m_mode = FRAM::FRAMDATABASE::RESTORE;
m_fram_rw_index = 0;
//
}//
//
void FRAMDATABASE::set_break()
{
m_state = FRAM::FRAMDATABASE::FREE;
m_mode = FRAM::FRAMDATABASE::WAIT;
m_fram_rw_index = 0;
//
}//
//
FRAM::FRAMHeader FRAMDATABASE::get_header()
{
return m_header;
//
}//
//
FRAM::FRAMFooter FRAMDATABASE::get_footer()
{
return m_footer;
//
}//
//
void FRAMDATABASE::extract_system_configuration(SYSCTRL::SystemControlConfiguration *sys_config)
{
m_fram_data_buffer.extract_system_configuration(*sys_config);
//
}//
//
void FRAMDATABASE::implement_dafault_configuration()
{
for(uint16_t i = 0; i < m_fram_object_index; i++)
{
FRAM::FRAMDataObjects& o = m_fram_object[i];
o.restore_default_value();
//
}//for
//
}//
//
void FRAMDATABASE::upload_configuration(SYSCTRL::SystemControlConfiguration *sys_config)
{
register_configuration_parameters(sys_config);
//implement_dafault_configuration();
read_from_fram_to_buffer();
m_verify = m_fram_data_buffer.verify_header_and_footer(m_header, m_footer);
//m_verify = false;
if(m_verify)
{
extract_system_configuration(sys_config);
}
else
{
erase_fram_buffer();
implement_dafault_configuration();
m_fram_data_buffer.update(m_header, *sys_config, m_footer);
write_to_fram_from_buffer();
//
}//if else
}//
//
void FRAMDATABASE::update_buffer(const SYSCTRL::SystemControlConfiguration *sys_config)
{
m_fram_data_buffer.update(m_header, *sys_config, m_footer);
//
}//
//
void FRAMDATABASE::erase_fram_index()
{
if(m_mode == FRAM::FRAMDATABASE::ERASE)
{
if(m_fram_rw_index < sizeof(m_fram_data_buffer))
{
writeint(0xFFFF, m_fram_rw_index << 1);
m_fram_rw_index++;
}
else
{
set_break();
//
}//if else
}//if
//
}//
//
void FRAMDATABASE::verify_fram_index()
{
if(m_mode == FRAM::FRAMDATABASE::VERIFY)
{
if(m_fram_rw_index < sizeof(m_fram_data_buffer))
{
m_fram_rw_index++;
}
else
{
set_break();
//
}//if else
}//if
//
}//
//
void FRAMDATABASE::write_fram_index()
{
if(m_mode == FRAM::FRAMDATABASE::BURN)
{
if(m_fram_rw_index < sizeof(m_fram_data_buffer))
{
writeint((*(m_buffer_pointer + m_fram_rw_index)), m_fram_rw_index << 1);
m_fram_rw_index++;
}
else
{
set_break();
//
}//if else
}//if
//
}//
//
void FRAMDATABASE::read_fram_index()
{
if(m_mode == FRAM::FRAMDATABASE::READ)
{
if(m_fram_rw_index < sizeof(m_fram_data_buffer))
{
*(m_buffer_pointer + m_fram_rw_index) = readint(m_fram_rw_index << 1);
m_fram_rw_index++;
}
else
{
set_break();
//
}//if else
}//if
//
}//
//
void FRAMDATABASE::restore_fram_index()
{
if(m_mode == FRAM::FRAMDATABASE::RESTORE)
{
if(m_fram_rw_index < sizeof(m_fram_data_buffer))
{
writeint((*(m_buffer_pointer + m_fram_rw_index)), m_fram_rw_index << 1);
m_fram_rw_index++;
}
else
{
set_break();
//
}//if else
}//if
//
}//
//
void FRAMDATABASE::read_from_fram_to_buffer()
{
set_read();
for(m_fram_rw_index = 0; m_fram_rw_index < sizeof(m_fram_data_buffer); m_fram_rw_index++)
{
*(m_buffer_pointer + m_fram_rw_index) = readint(m_fram_rw_index << 1);
//
}//for
//
m_mode = FRAM::FRAMDATABASE::READ;
m_state = FRAMDATABASE::BUSY;
set_break();
//
}//
//
void FRAMDATABASE::write_to_fram_from_buffer()
{
set_burn();
for(m_fram_rw_index = 0; m_fram_rw_index < sizeof(m_fram_data_buffer); m_fram_rw_index++)
{
writeint((*(m_buffer_pointer + m_fram_rw_index)), m_fram_rw_index << 1);
//
}//for
//
set_break();
//
}//
//
void FRAMDATABASE::erase_fram_buffer()
{
set_verify();
for(m_fram_rw_index = 0; m_fram_rw_index < sizeof(m_fram_data_buffer); m_fram_rw_index++)
{
writeint(0xFFFF, m_fram_rw_index << 1);
*(m_buffer_pointer + m_fram_rw_index) = readint(m_fram_rw_index << 1);
//
}//for
//
set_break();
//
}//
//
void FRAMDATABASE::add_float(uint8_t readonly, float* pParam, float default_value)
{
if((m_fram_object_index < NUMBER_FRAM_PARAMETERS)&&
(((m_fram_object_address + (sizeof(float)<<1)) < m_fram_size)))
{
FRAM::FRAMDataObjects& object = m_fram_object[m_fram_object_index];
object.add_register_float(m_fram_object_address, readonly, pParam, default_value);
m_fram_object_index++;
m_fram_object_address += (sizeof(float) << 1);
m_fram_object_last_address = m_fram_object_address;
//
}//if
//
}//
//
void FRAMDATABASE::add_int16(uint8_t readonly, int16_t* pParam, int16_t default_value)
{
if((m_fram_object_index < NUMBER_FRAM_PARAMETERS)&&
(((m_fram_object_address + (sizeof(uint16_t)<<1)) < m_fram_size)))
{
FRAM::FRAMDataObjects& object = m_fram_object[m_fram_object_index];
object.add_register_int16(m_fram_object_address, readonly, pParam, default_value);
m_fram_object_index++;
m_fram_object_address += sizeof(int16_t) << 1;
m_fram_object_last_address = m_fram_object_address;
//
}//if
//
}//
//
void FRAMDATABASE::add_int32(uint8_t readonly, int32_t* pParam, int32_t default_value)
{
if((m_fram_object_index < NUMBER_FRAM_PARAMETERS)&&
(((m_fram_object_address + (sizeof(int32_t)<<1)) < m_fram_size)))
{
FRAM::FRAMDataObjects& object = m_fram_object[m_fram_object_index];
object.add_register_int32(m_fram_object_address, readonly, pParam, default_value);
m_fram_object_index++;
m_fram_object_address += (sizeof(int32_t) << 1);
m_fram_object_last_address = m_fram_object_address;
//
}//if
}//
//
void FRAMDATABASE::add_uint16(uint8_t readonly, uint16_t* pParam, uint16_t default_value)
{
if((m_fram_object_index < NUMBER_FRAM_PARAMETERS)&&
(((m_fram_object_address + (sizeof(uint16_t)<<1)) < m_fram_size)))
{
FRAM::FRAMDataObjects& object = m_fram_object[m_fram_object_index];
object.add_register_uint16(m_fram_object_address, readonly, pParam, default_value);
m_fram_object_index++;
m_fram_object_address += sizeof(uint16_t) << 1;
m_fram_object_last_address = m_fram_object_address;
//
}//if
//
}//
//
void FRAMDATABASE::add_uint32(uint8_t readonly, uint32_t* pParam, uint32_t default_value)
{
if((m_fram_object_index < NUMBER_FRAM_PARAMETERS)&&
(((m_fram_object_address + (sizeof(uint32_t)<<1)) < m_fram_size)))
{
FRAM::FRAMDataObjects& object = m_fram_object[m_fram_object_index];
object.add_register_uint32(m_fram_object_address, readonly, pParam, default_value);
m_fram_object_index++;
m_fram_object_address += (sizeof(uint32_t) << 1);
m_fram_object_last_address = m_fram_object_address;
//
}//if
}//
//
void FRAMDATABASE::add_bool(uint8_t readonly, bool* pParam, bool default_value)
{
if((m_fram_object_index < NUMBER_FRAM_PARAMETERS)&&
(((m_fram_object_address + (sizeof(uint16_t)<<1)) < m_fram_size)))
{
FRAM::FRAMDataObjects& object = m_fram_object[m_fram_object_index];
object.add_register_bool(m_fram_object_address, readonly, pParam, default_value);
m_fram_object_index++;
m_fram_object_address += sizeof(int16_t) << 1;
m_fram_object_last_address = m_fram_object_address;
//
}//if
//
}//
//
} /* namespace FRAM */