nbus_memory.c 5.0 KB

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