nbus_slave_module_unicast.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if (nbus->rx_length >= 5) // get concrete param
  17. {
  18. if (nbus->interface->hasParam(0, (nBus_param_t)nbus->rx_buffer[3]) == 0) // handle non-existing param
  19. {
  20. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  21. break;
  22. }
  23. nbus->tx_buffer[4] = nbus->rx_buffer[3]; // param id
  24. int32_t param_value = nbus->interface->getParam(0, (nBus_param_t)nbus->rx_buffer[3]);
  25. nbus->tx_buffer[5] = (uint8_t)(param_value & 0xFF);
  26. nbus->tx_buffer[6] = (uint8_t)((param_value >> 8) & 0xFF);
  27. nbus->tx_buffer[7] = (uint8_t)((param_value >> 16) & 0xFF);
  28. nbus->tx_buffer[8] = (uint8_t)((param_value >> 24) & 0xFF);
  29. nbus->tx_length += 5;
  30. }
  31. else // get all params
  32. {
  33. nBus_param_t *params = nbus_interface_allParams();
  34. for (uint8_t i = 0; i < nbus_interface_allParamsCount(); i++)
  35. {
  36. if (nbus->interface->hasParam(0, params[i]))
  37. {
  38. int32_t param_value = nbus->interface->getParam(0, params[i]);
  39. nbus->tx_buffer[4 + 5 * i] = params[i]; // param id
  40. // nbus->tx_buffer[5+2*i] =
  41. // nbus->interface->getParam(0, params[i]);
  42. nbus->tx_buffer[5 + 5 * i] = (uint8_t)(param_value & 0xFF);
  43. nbus->tx_buffer[6 + 5 * i] = (uint8_t)((param_value >> 8) & 0xFF);
  44. nbus->tx_buffer[7 + 5 * i] = (uint8_t)((param_value >> 16) & 0xFF);
  45. nbus->tx_buffer[8 + 5 * i] = (uint8_t)((param_value >> 24) & 0xFF);
  46. nbus->tx_length += 5;
  47. }
  48. }
  49. }
  50. }
  51. break;
  52. case CMD_SENSOR_CNT: {
  53. nbus->tx_buffer[4] = nbus->interface->getSensorCount();
  54. nbus->tx_length += 1;
  55. }
  56. break;
  57. case CMD_DATA: {
  58. if (nbus->measure_active == MEASURE_RUNNING)
  59. {
  60. // response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
  61. uint8_t cnt = nbus->interface->getData(0, &nbus->tx_buffer[4]);
  62. if (cnt == 0)
  63. {
  64. setErrorResponse(nbus, DEVICE_BUSY);
  65. }
  66. nbus->tx_length += cnt;
  67. }
  68. else
  69. {
  70. setErrorResponse(nbus, DEVICE_NOT_READY);
  71. }
  72. }
  73. break;
  74. case CMD_SENSOR_TYPE: {
  75. // response: sensor1_index:sensor1_type | sensor2_index:sensor2_type | ...
  76. for (uint8_t i = 0; i < nbus->interface->getSensorCount(); i++)
  77. {
  78. nbus->tx_buffer[4 + 2 * i] = i;
  79. nbus->tx_buffer[5 + 2 * i] = nbus->interface->getType(i);
  80. nbus->tx_length += 2;
  81. }
  82. }
  83. break;
  84. case CMD_INFO: {
  85. switch (nbus->rx_buffer[3])
  86. {
  87. case INFO_GENERAL: {
  88. // name
  89. nbus->tx_buffer[4] = MODULE_NAME[0];
  90. nbus->tx_buffer[5] = MODULE_NAME[1];
  91. nbus->tx_buffer[6] = MODULE_NAME[2];
  92. nbus->tx_buffer[7] = MODULE_NAME[3];
  93. nbus->tx_buffer[8] = MODULE_NAME[4];
  94. nbus->tx_buffer[9] = MODULE_NAME[5];
  95. nbus->tx_buffer[10] = MODULE_NAME[6];
  96. nbus->tx_buffer[11] = MODULE_NAME[7];
  97. nbus->tx_length += 8;
  98. // type
  99. nbus->tx_buffer[12] = MODULE_TYPE[0];
  100. nbus->tx_buffer[13] = MODULE_TYPE[1];
  101. nbus->tx_buffer[14] = MODULE_TYPE[2];
  102. nbus->tx_length += 3;
  103. // Reference manual: Unique device ID registers
  104. #if defined(STM32)
  105. uint32_t(*unique_id_3) = (uint32_t *)(0x1FF80064); // BASE address + 0x14 0ffset
  106. #elif defined(ESP32) || defined(NRF)
  107. uint32_t unique_id_3 = 0x12345678;
  108. #endif
  109. nbus->tx_buffer[15] = ((uint8_t *)(&unique_id_3))[0];
  110. nbus->tx_buffer[16] = ((uint8_t *)(&unique_id_3))[1];
  111. nbus->tx_buffer[17] = ((uint8_t *)(&unique_id_3))[2];
  112. nbus->tx_buffer[18] = ((uint8_t *)(&unique_id_3))[3];
  113. nbus->tx_length += 4;
  114. // fw version
  115. nbus->tx_buffer[19] = VERSION_FW[0];
  116. nbus->tx_buffer[20] = '.';
  117. nbus->tx_buffer[21] = VERSION_FW[1];
  118. nbus->tx_length += 3;
  119. // hw version
  120. nbus->tx_buffer[22] = VERSION_HW[0];
  121. nbus->tx_buffer[23] = '.';
  122. nbus->tx_buffer[24] = VERSION_HW[1];
  123. nbus->tx_length += 3;
  124. // module memory
  125. nbus->memoryInterface->getId(&nbus->tx_buffer[25]);
  126. nbus->tx_length += 8;
  127. break;
  128. }
  129. case INFO_FORMAT: {
  130. uint8_t sensor_cnt = nbus->interface->getSensorCount();
  131. for (int8_t i = 0; i < sensor_cnt; ++i)
  132. {
  133. nBus_sensorFormat_t format = nbus->interface->getSensorFormat(i + 1);
  134. uint8_t *format_ptr = (uint8_t *)&format;
  135. nbus->tx_buffer[4 * i + 4] = i + 1;
  136. nbus->tx_buffer[4 * i + 5] = format_ptr[0];
  137. nbus->tx_buffer[4 * i + 6] = format_ptr[1];
  138. nbus->tx_buffer[4 * i + 7] = format_ptr[2];
  139. }
  140. nbus->tx_length += 4 * sensor_cnt;
  141. break;
  142. }
  143. }
  144. }
  145. break;
  146. default: {
  147. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  148. }
  149. }
  150. }
  151. void nbus_slave_unicastToModuleSet(nBus_TypeDef *nbus)
  152. {
  153. switch (nbus->function_code.function)
  154. {
  155. case CMD_START: {
  156. nbus->measure_active = MEASURE_RUNNING;
  157. nbus->interface->start();
  158. nbus->hw_platform->led_on();
  159. }
  160. break;
  161. case CMD_STOP: {
  162. nbus->measure_active = MEASURE_STOPPED;
  163. nbus->interface->stop();
  164. nbus->hw_platform->led_off();
  165. }
  166. break;
  167. case CMD_PARAM: {
  168. // same as nbus_unicastToSensorSet
  169. int32_t param_value =
  170. nbus->rx_buffer[4] | nbus->rx_buffer[5] << 8 | nbus->rx_buffer[6] << 16 << nbus->rx_buffer[7] << 23;
  171. nBus_param_t p = nbus->interface->setParam(0, (nBus_param_t)nbus->rx_buffer[3], param_value);
  172. if (p == PARAM_NONE)
  173. {
  174. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  175. break;
  176. }
  177. nbus->tx_buffer[4] = OK_CODE;
  178. nbus->tx_length += 1;
  179. }
  180. break;
  181. case CMD_DATA: {
  182. nbus->tx_buffer[4] = nbus->interface->setData(&nbus->rx_buffer[3]);
  183. if (nbus->tx_buffer[4] != OK_CODE)
  184. {
  185. nbus->function_code.error = 1;
  186. }
  187. nbus->tx_length += 1;
  188. }
  189. break;
  190. case CMD_SLEEP: {
  191. nbus->tx_buffer[4] = OK_CODE;
  192. nbus->tx_length += 1;
  193. }
  194. break;
  195. case CMD_WAKEUP: {
  196. nbus->tx_buffer[4] = OK_CODE;
  197. nbus->tx_length += 1;
  198. }
  199. break;
  200. case CMD_CALIBRATE: {
  201. nbus->hw_platform->led_on();
  202. nbus->tx_buffer[4] = nbus->interface->calibrate(0, 0, NULL);
  203. nbus->tx_length += 1;
  204. nbus->hw_platform->led_off();
  205. }
  206. break;
  207. case CMD_RESET: {
  208. // POZOR! cas fomatovania: 0.3s (pri 1W pamati)
  209. memory_params_format();
  210. nbus->tx_buffer[4] = OK_CODE;
  211. nbus->tx_length += 1;
  212. }
  213. break;
  214. /*
  215. case CMD_STORE: {
  216. if (nbus->interface->store != NULL) {
  217. nbus->interface->store();
  218. nbus->tx_buffer[4] = OK_CODE;
  219. } else {
  220. nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
  221. }
  222. nbus->tx_length += 1;
  223. } break;
  224. */
  225. default: {
  226. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  227. }
  228. }
  229. }
  230. #endif