nbus_impl_module_unicast.c 2.9 KB

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