nbus_slave_module_unicast.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <nbus_slave.h>
  2. #if MODULE_SLAVE == 1
  3. void nbus_slave_unicastToModuleGet(nBus_TypeDef *nbus)
  4. {
  5. switch (nbus->function_code.function)
  6. {
  7. case CMD_ECHO: {
  8. for (uint8_t i = 3; i < nbus->rx_length - 1; i++)
  9. {
  10. nbus->tx_buffer[i + 1] = nbus->rx_buffer[i];
  11. }
  12. nbus->tx_length += (nbus->rx_length - 4);
  13. }
  14. break;
  15. case CMD_PARAM: {
  16. // same as nbus_unicastToSensorSet
  17. int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]);
  18. if (param_value == PARAM_VALUE_NONE)
  19. {
  20. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  21. break;
  22. }
  23. nbus->tx_buffer[4] = (uint8_t)(param_value & 0xFF);
  24. nbus->tx_buffer[5] = (uint8_t)((param_value >> 8) & 0xFF);
  25. nbus->tx_buffer[6] = (uint8_t)((param_value >> 16) & 0xFF);
  26. nbus->tx_buffer[7] = (uint8_t)((param_value >> 24) & 0xFF);
  27. nbus->tx_length += 4;
  28. }
  29. break;
  30. case CMD_SENSOR_CNT: {
  31. nbus->tx_buffer[4] = nbus->interface->getSensorCount();
  32. nbus->tx_length += 1;
  33. }
  34. break;
  35. case CMD_DATA: {
  36. if (nbus->measure_active == MEASURE_RUNNING)
  37. {
  38. // response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
  39. uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
  40. if (cnt == 0)
  41. {
  42. setErrorResponse(nbus, DEVICE_BUSY);
  43. }
  44. nbus->tx_length += cnt;
  45. }
  46. else
  47. {
  48. setErrorResponse(nbus, DEVICE_NOT_READY);
  49. }
  50. }
  51. break;
  52. case CMD_SENSOR_TYPE: {
  53. // response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
  54. for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++)
  55. {
  56. nbus->tx_buffer[4 + 2 * i] = i;
  57. nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
  58. nbus->tx_length += 2;
  59. }
  60. }
  61. break;
  62. case CMD_INFO: {
  63. switch (nbus->rx_buffer[3])
  64. {
  65. case INFO_MODULE_NAME:
  66. nbus->tx_buffer[4] = MODULE_NAME[0];
  67. nbus->tx_buffer[5] = MODULE_NAME[1];
  68. nbus->tx_buffer[6] = MODULE_NAME[2];
  69. nbus->tx_buffer[7] = MODULE_NAME[3];
  70. nbus->tx_buffer[8] = MODULE_NAME[4];
  71. nbus->tx_buffer[9] = MODULE_NAME[5];
  72. nbus->tx_buffer[10] = MODULE_NAME[6];
  73. nbus->tx_buffer[11] = MODULE_NAME[7];
  74. nbus->tx_length += 8;
  75. break;
  76. case INFO_MODULE_TYPE:
  77. nbus->tx_buffer[4] = MODULE_TYPE[0];
  78. nbus->tx_buffer[5] = MODULE_TYPE[1];
  79. nbus->tx_buffer[6] = MODULE_TYPE[2];
  80. nbus->tx_length += 3;
  81. break;
  82. case INFO_MODULE_UUID:
  83. // Reference manual: Unique device ID registers
  84. uint32_t(*unique_id_3) = (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
  85. *(nbus->tx_buffer) = (uint32_t)unique_id_3;
  86. nbus->tx_length += 4;
  87. break;
  88. case INFO_MODULE_FW:
  89. nbus->tx_buffer[4] = VERSION_FW[0];
  90. nbus->tx_buffer[5] = '.';
  91. nbus->tx_buffer[6] = VERSION_FW[1];
  92. nbus->tx_length += 3;
  93. break;
  94. case INFO_MODULE_HW:
  95. nbus->tx_buffer[4] = VERSION_HW[0];
  96. nbus->tx_buffer[5] = '.';
  97. nbus->tx_buffer[6] = VERSION_HW[1];
  98. nbus->tx_length += 3;
  99. break;
  100. case INFO_MODULE_MEMORY_ID: {
  101. uint8_t n = nbus->memoryInterface->getId(&nbus->tx_buffer[4]);
  102. nbus->tx_length += n;
  103. }
  104. }
  105. }
  106. break;
  107. default: {
  108. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  109. }
  110. }
  111. }
  112. void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
  113. {
  114. switch (nbus->function_code.function)
  115. {
  116. case CMD_PARAM: {
  117. // same as nbus_unicastToSensorSet
  118. int32_t param_value =
  119. nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  120. nBus_param_t p = nbus->interface->setParam(0, (nBus_param_t)nbus->rx_buffer[3], param_value);
  121. if (p == PARAM_NONE)
  122. {
  123. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  124. break;
  125. }
  126. nbus->tx_buffer[4] = OK_CODE;
  127. nbus->tx_length += 1;
  128. }
  129. break;
  130. case CMD_DATA: {
  131. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  132. if (nbus->tx_buffer[4] != OK_CODE)
  133. {
  134. nbus->function_code.error = 1;
  135. }
  136. nbus->tx_length += 1;
  137. }
  138. break;
  139. case CMD_SLEEP: {
  140. nbus->tx_buffer[4] = OK_CODE;
  141. nbus->tx_length += 1;
  142. }
  143. break;
  144. case CMD_WAKEUP: {
  145. nbus->tx_buffer[4] = OK_CODE;
  146. nbus->tx_length += 1;
  147. }
  148. case CMD_RESET: {
  149. // POZOR! cas fomatovania: 0.3s (pri 1W pamati)
  150. memory_params_format();
  151. nbus->tx_buffer[4] = OK_CODE;
  152. nbus->tx_length += 1;
  153. }
  154. break;
  155. /*
  156. case CMD_STORE: {
  157. if (nbus->interface->store != NULL) {
  158. nbus->interface->store();
  159. nbus->tx_buffer[4] = OK_CODE;
  160. } else {
  161. nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
  162. }
  163. nbus->tx_length += 1;
  164. } break;
  165. */
  166. default: {
  167. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  168. }
  169. }
  170. }
  171. #endif