nbus_slave_module_unicast.c 3.3 KB

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