nbus_slave_module_unicast.c 4.1 KB

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