nbus_slave_module_unicast.c 2.9 KB

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