| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- /*
- * nbus_memory.c
- *
- * Created on: Nov 4, 2023
- * Author: juraj
- */
- #include "nbus_memory.h"
- nBus_MemoryDriver *memoryImplementation;
- nBus_memoryInterface_t memory_itnerface = {nbus_memory_store_param, nbus_memory_read_param, nbus_is_param_active,
- nbus_read_header_data, nbus_memory_id, nbus_get_capacity};
- nBus_memoryHeader_t memoryHeader;
- nBus_memoryInterface_t *getnbusMemoryInterface(void)
- {
- return &memory_itnerface;
- }
- uint8_t memory_buffer[32];
- void nbus_memory_init(nBus_MemoryDriver *driver)
- {
- memoryImplementation = driver;
- for (uint32_t i = 0; i < 64; i++)
- {
- memoryHeader.params_map[i].param0 = PARAMETER_UNSET;
- memoryHeader.params_map[i].param1 = PARAMETER_UNSET;
- memoryHeader.params_map[i].param2 = PARAMETER_UNSET;
- memoryHeader.params_map[i].param3 = PARAMETER_UNSET;
- }
- }
- uint8_t ubus_memory_write4B(uint32_t data, uint16_t adr)
- {
- memory_buffer[0] = data >> 24;
- memory_buffer[1] = (data >> 16) & 0xFF;
- memory_buffer[2] = (data >> 8) & 0xFF;
- memory_buffer[3] = (data)&0xFF;
- return memoryImplementation->write_data(memory_buffer, adr, 4);
- }
- uint8_t ubus_memory_write2B(uint16_t data, uint16_t adr)
- {
- memory_buffer[0] = (data >> 8) & 0xFF;
- memory_buffer[1] = (data)&0xFF;
- return memoryImplementation->write_data(memory_buffer, adr, 2);
- }
- uint8_t ubus_memory_write1B(uint8_t data, uint16_t adr)
- {
- memory_buffer[0] = (data)&0xFF;
- return memoryImplementation->write_data(memory_buffer, adr, 1);
- }
- nBus_memoryState_t nbus_memory_store_param(uint8_t sensor_index, uint8_t param_name, uint32_t param_value)
- {
- if (param_name >= 16)
- {
- return MEMORY_STORE_FAIL;
- }
- uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
- if (addr > memoryImplementation->get_capacity())
- {
- return MEMORY_STORE_FAIL;
- }
- /*
- * Write information about validity of parameter for sensor 'sensor_index'. 0 -
- * parameter not set, 0xAA - parameter is setted
- */
- uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
- switch (param_name % 4)
- {
- case 0:
- memoryHeader.params_map[address_offset].param0 = PARAMETER_VALID;
- break;
- case 1:
- memoryHeader.params_map[address_offset].param1 = PARAMETER_VALID;
- break;
- case 2:
- memoryHeader.params_map[address_offset].param2 = PARAMETER_VALID;
- break;
- case 3:
- memoryHeader.params_map[address_offset].param3 = PARAMETER_VALID;
- break;
- }
- ubus_memory_write1B(*(uint8_t *)(&memoryHeader.params_map[address_offset]), sensor_index);
- return ubus_memory_write4B(param_value, addr) == 0 ? MEMORY_STORE_OK : MEMORY_STORE_FAIL;
- }
- uint32_t nbus_memory_read_param(uint8_t sensor_index, uint8_t param_name)
- {
- if (param_name >= 16)
- {
- return ERROR_VALUE_I32;
- }
- uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
- if (addr > memoryImplementation->get_capacity())
- {
- return ERROR_VALUE_I32;
- }
- return memoryImplementation->read_word(addr);
- }
- void nbus_read_header_data(void)
- {
- uint8_t data;
- for (uint32_t adr = 0; adr < 63; adr += 4)
- {
- data = memoryImplementation->read_byte(adr);
- memoryHeader.params_map[adr + 0] = *(nBus_MemorySensorParam_t *)(&data);
- data = memoryImplementation->read_byte(adr + 1);
- memoryHeader.params_map[adr + 1] = *(nBus_MemorySensorParam_t *)(&data);
- data = memoryImplementation->read_byte(adr + 2);
- memoryHeader.params_map[adr + 2] = *(nBus_MemorySensorParam_t *)(&data);
- data = memoryImplementation->read_byte(adr + 3);
- memoryHeader.params_map[adr + 3] = *(nBus_MemorySensorParam_t *)(&data);
- }
- }
- uint8_t nbus_memory_id(uint8_t *data)
- {
- return memoryImplementation->read_id(data);
- }
- uint32_t nbus_get_capacity(void)
- {
- return memoryImplementation->get_capacity();
- }
- uint8_t nbus_is_param_active(uint8_t sensor_index, uint8_t param_name)
- {
- uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
- uint8_t is_active;
- switch (param_name % 4)
- {
- case 0:
- is_active = memoryHeader.params_map[address_offset].param0;
- break;
- case 1:
- is_active = memoryHeader.params_map[address_offset].param1;
- break;
- case 2:
- is_active = memoryHeader.params_map[address_offset].param2;
- break;
- case 3:
- is_active = memoryHeader.params_map[address_offset].param3;
- break;
- default:
- is_active = 0;
- }
- return is_active;
- }
- // TODO pouzit upravenu funkciu z drivera: DS28EC20_writeMem
- void memory_params_format(void)
- {
- uint8_t empty_data[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- for (uint32_t adr = 0; adr < 64; adr += 32)
- {
- memoryImplementation->write_data(empty_data, adr, 32);
- }
- }
- uint8_t getParameterHeaderOffset(uint8_t sensor_index, uint8_t param_name)
- {
- return (sensor_index - 1) * 4 + param_name / 4;
- }
|