nbus_memory.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * nbus_memory.c
  3. *
  4. * Created on: Nov 4, 2023
  5. * Author: juraj
  6. */
  7. #include "nbus_memory.h"
  8. nBus_MemoryDriver *memoryImplementation;
  9. nBus_memoryInterface_t memory_itnerface = {
  10. nbus_memory_store_param, nbus_memory_read_param, nbus_is_param_active,
  11. nbus_read_header_data, nbus_memory_id, nbus_get_capacity};
  12. nBus_memoryHeader_t memoryHeader;
  13. nBus_memoryInterface_t *getnbusMemoryInterface(void) {
  14. return &memory_itnerface;
  15. }
  16. uint8_t memory_buffer[32];
  17. void nbus_memory_init(nBus_MemoryDriver *driver) {
  18. memoryImplementation = driver;
  19. for (uint32_t i = 0; i < 64; i++) {
  20. memoryHeader.params_map[i].param0 = PARAMETER_UNSET;
  21. memoryHeader.params_map[i].param1 = PARAMETER_UNSET;
  22. memoryHeader.params_map[i].param2 = PARAMETER_UNSET;
  23. memoryHeader.params_map[i].param3 = PARAMETER_UNSET;
  24. }
  25. }
  26. uint8_t ubus_memory_write4B(uint32_t data, uint16_t adr) {
  27. memory_buffer[0] = data>>24;
  28. memory_buffer[1] = (data>>16) & 0xFF;
  29. memory_buffer[2] = (data>>8) & 0xFF;
  30. memory_buffer[3] = (data) & 0xFF;
  31. return memoryImplementation->write_data(memory_buffer, adr, 4);
  32. }
  33. uint8_t ubus_memory_write2B(uint16_t data, uint16_t adr) {
  34. memory_buffer[0] = (data>>8) & 0xFF;
  35. memory_buffer[1] = (data) & 0xFF;
  36. return memoryImplementation->write_data(memory_buffer, adr, 2);
  37. }
  38. uint8_t ubus_memory_write1B(uint8_t data, uint16_t adr) {
  39. memory_buffer[0] = (data) & 0xFF;
  40. return memoryImplementation->write_data(memory_buffer, adr, 1);
  41. }
  42. nBus_memoryState_t nbus_memory_store_param(uint8_t sensor_index, uint8_t param_name,
  43. uint32_t param_value) {
  44. if (param_name >= 16) {
  45. return MEMORY_STORE_FAIL;
  46. }
  47. uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
  48. if (addr > memoryImplementation->get_capacity()) {
  49. return MEMORY_STORE_FAIL;
  50. }
  51. /*
  52. * Write information about validity of parameter for sensor 'sensor_index'. 0 -
  53. * parameter not set, 0xAA - parameter is setted
  54. */
  55. uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
  56. switch (param_name % 4) {
  57. case 0:
  58. memoryHeader.params_map[address_offset].param0 = PARAMETER_VALID;
  59. break;
  60. case 1:
  61. memoryHeader.params_map[address_offset].param1 = PARAMETER_VALID;
  62. break;
  63. case 2:
  64. memoryHeader.params_map[address_offset].param2 = PARAMETER_VALID;
  65. break;
  66. case 3:
  67. memoryHeader.params_map[address_offset].param3 = PARAMETER_VALID;
  68. break;
  69. }
  70. ubus_memory_write1B(
  71. *(uint8_t *)(&memoryHeader.params_map[address_offset]), sensor_index);
  72. return ubus_memory_write4B(param_value, addr) == 0
  73. ? MEMORY_STORE_OK
  74. : MEMORY_STORE_FAIL;
  75. }
  76. uint32_t nbus_memory_read_param(uint8_t sensor_index, uint8_t param_name) {
  77. if (param_name >= 16) {
  78. return ERROR_VALUE_I32;
  79. }
  80. uint16_t addr = GET_PARAM_ADDRESS(sensor_index, param_name);
  81. if (addr > memoryImplementation->get_capacity()) {
  82. return ERROR_VALUE_I32;
  83. }
  84. return memoryImplementation->read_word(addr);
  85. }
  86. void nbus_read_header_data(void) {
  87. uint8_t data;
  88. for (uint32_t adr = 0; adr < 63; adr += 4) {
  89. data = memoryImplementation->read_byte(adr);
  90. memoryHeader.params_map[adr + 0] = *(nBus_MemorySensorParam_t *)(&data);
  91. data = memoryImplementation->read_byte(adr + 1);
  92. memoryHeader.params_map[adr + 1] = *(nBus_MemorySensorParam_t *)(&data);
  93. data = memoryImplementation->read_byte(adr + 2);
  94. memoryHeader.params_map[adr + 2] = *(nBus_MemorySensorParam_t *)(&data);
  95. data = memoryImplementation->read_byte(adr + 3);
  96. memoryHeader.params_map[adr + 3] = *(nBus_MemorySensorParam_t *)(&data);
  97. }
  98. }
  99. uint8_t nbus_memory_id(uint8_t *data) {
  100. return memoryImplementation->read_id(data);
  101. }
  102. uint32_t nbus_get_capacity(void) {
  103. return memoryImplementation->get_capacity();
  104. }
  105. uint8_t nbus_is_param_active(uint8_t sensor_index, uint8_t param_name) {
  106. uint8_t address_offset = getParameterHeaderOffset(sensor_index, param_name);
  107. uint8_t is_active;
  108. switch (param_name % 4) {
  109. case 0:
  110. is_active = memoryHeader.params_map[address_offset].param0;
  111. break;
  112. case 1:
  113. is_active = memoryHeader.params_map[address_offset].param1;
  114. break;
  115. case 2:
  116. is_active = memoryHeader.params_map[address_offset].param2;
  117. break;
  118. case 3:
  119. is_active = memoryHeader.params_map[address_offset].param3;
  120. break;
  121. default:
  122. is_active = 0;
  123. }
  124. return is_active;
  125. }
  126. // TODO pouzit upravenu funkciu z drivera: DS28EC20_writeMem
  127. void memory_params_format(void) {
  128. uint8_t empty_data[32] = {
  129. 0,0,0,0,0,0,0,0,
  130. 0,0,0,0,0,0,0,0,
  131. 0,0,0,0,0,0,0,0,
  132. 0,0,0,0,0,0,0,0};
  133. for (uint32_t adr = 0; adr < 64; adr += 32) {
  134. memoryImplementation->write_data(empty_data, adr, 32);
  135. }
  136. }
  137. uint8_t getParameterHeaderOffset(uint8_t sensor_index, uint8_t param_name) {
  138. return (sensor_index - 1) * 4 + param_name / 4;
  139. }