nbus_slave_module_unicast.c 4.5 KB

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