nbus_impl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "nbus_impl.h"
  2. inline static void setErrorResponse(nBus_TypeDef *nbus, uint8_t code){
  3. nbus->function_code.error = 1;
  4. nbus->tx_buffer[4] = code;
  5. nbus->tx_length += 1;
  6. }
  7. void nbus_unicastToSensorGet(nBus_TypeDef *nbus){
  8. switch(nbus->function_code.function){
  9. case CMD_SENSOR_TYPE:
  10. {
  11. nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
  12. if (t == TYPE_UNKNOWN) {
  13. setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
  14. break;
  15. }
  16. nbus->tx_buffer[4] = t;
  17. nbus->tx_length += 1;
  18. }
  19. break;
  20. case CMD_PARAM:
  21. {
  22. if (nbus->rx_length >= 5){
  23. if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]) == 0) {
  24. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  25. break;
  26. }
  27. nbus->tx_buffer[4] = nbus->tx_buffer[3];
  28. nbus->tx_buffer[5] = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]);
  29. nbus->tx_length += 2;
  30. } else {
  31. nBus_param_t* params = nbus_interface_allParams();
  32. for(uint8_t i=0; i < nbus_interface_allParamsCount() ; i++){
  33. if(nbus->interface->hasParam(i, params[i])){
  34. nbus->tx_buffer[4+2*i] = params[i];
  35. nbus->tx_buffer[5+2*i] = nbus->interface->getParam(i, params[i]);
  36. nbus->tx_length += 2;
  37. }
  38. }
  39. }
  40. // alebo
  41. nbus->memoryInterface->getParam(nbus->sensorInfo.address, 0x05);
  42. }
  43. break;
  44. case CMD_DATA:
  45. {
  46. uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
  47. nbus->tx_length += cnt;
  48. }
  49. break;
  50. default:
  51. {
  52. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  53. }
  54. }
  55. }
  56. void nbus_unicastToSensorSet(nBus_TypeDef *nbus){
  57. switch(nbus->function_code.function){
  58. case CMD_PARAM:
  59. {
  60. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  61. if (p == PARAM_NONE) {
  62. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  63. break;
  64. }
  65. nbus->tx_buffer[4] = OK_CODE;
  66. nbus->tx_length += 1;
  67. }
  68. break;
  69. case CMD_DATA:
  70. {
  71. nbus->interface->setData(&nbus->rx_buffer[3]);
  72. nbus->tx_buffer[4] = OK_CODE;
  73. nbus->tx_length += 1;
  74. }
  75. break;
  76. default:
  77. {
  78. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  79. }
  80. }
  81. }
  82. void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
  83. switch(nbus->function_code.function){
  84. case CMD_ECHO:
  85. {
  86. for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
  87. nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
  88. }
  89. nbus->tx_length += (nbus->rx_length-4);
  90. }
  91. break;
  92. case CMD_SENSOR_CNT:
  93. {
  94. nbus->tx_buffer[4] = nbus->interface->getSensorCount();
  95. nbus->tx_length += 1;
  96. }
  97. break;
  98. case CMD_SENSOR_TYPE:
  99. {
  100. //response: sensor1_index:sensor2_type | sensor1_index:sensor2_type | ...
  101. for(uint8_t i = 0; i < nbus->interface->getSensorCount() ; i++){
  102. nbus->tx_buffer[4+2*i] = i;
  103. nbus->tx_buffer[5+2*i] = nbus->interface->getType(i);
  104. nbus->tx_length += 2;
  105. }
  106. }
  107. break;
  108. case CMD_INFO:
  109. {
  110. switch(nbus->rx_buffer[3]){
  111. case INFO_MODULE_NAME:
  112. nbus->tx_buffer[4] = MODULE_NAME[0];
  113. nbus->tx_buffer[5] = MODULE_NAME[1];
  114. nbus->tx_buffer[6] = MODULE_NAME[2];
  115. nbus->tx_buffer[7] = MODULE_NAME[3];
  116. nbus->tx_buffer[8] = MODULE_NAME[4];
  117. nbus->tx_buffer[9] = MODULE_NAME[5];
  118. nbus->tx_buffer[10] = MODULE_NAME[6];
  119. nbus->tx_buffer[11] = MODULE_NAME[7];
  120. nbus->tx_length += 8;
  121. break;
  122. case INFO_MODULE_TYPE:
  123. nbus->tx_buffer[4] = MODULE_TYPE[0];
  124. nbus->tx_buffer[5] = MODULE_TYPE[1];
  125. nbus->tx_buffer[6] = MODULE_TYPE[2];
  126. nbus->tx_length += 3;
  127. break;
  128. case INFO_MODULE_UUID:
  129. // Reference manual: Unique device ID registers
  130. uint32_t (*unique_id_3) = (uint32_t*)(0x1FF80064); // BASE address + 0x14 0ffset
  131. *(nbus->tx_buffer) = (uint32_t)unique_id_3;
  132. nbus->tx_length += 4;
  133. break;
  134. case INFO_MODULE_FW:
  135. nbus->tx_buffer[4] = VERSION_FW[0];
  136. nbus->tx_buffer[5] = '.';
  137. nbus->tx_buffer[6] = VERSION_FW[1];
  138. nbus->tx_length += 3;
  139. break;
  140. case INFO_MODULE_HW:
  141. nbus->tx_buffer[4] = VERSION_HW[0];
  142. nbus->tx_buffer[5] = '.';
  143. nbus->tx_buffer[6] = VERSION_HW[1];
  144. nbus->tx_length += 3;
  145. break;
  146. }
  147. }
  148. break;
  149. default:
  150. {
  151. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  152. }
  153. }
  154. }
  155. void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
  156. switch(nbus->function_code.function){
  157. case CMD_PARAM:
  158. {
  159. //same as nbus_unicastToSensorSet
  160. nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
  161. if (p == PARAM_NONE) {
  162. setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
  163. break;
  164. }
  165. nbus->tx_buffer[4] = OK_CODE;
  166. nbus->tx_length += 1;
  167. }
  168. break;
  169. case CMD_DATA:
  170. {
  171. nbus->interface->setData(&nbus->rx_buffer[3]);
  172. nbus->tx_buffer[4] = OK_CODE;
  173. nbus->tx_length += 1;
  174. }
  175. break;
  176. case CMD_SLEEP:
  177. {
  178. nbus->tx_buffer[4] = OK_CODE;
  179. nbus->tx_length += 1;
  180. }
  181. break;
  182. case CMD_WAKEUP:
  183. {
  184. nbus->tx_buffer[4] = OK_CODE;
  185. nbus->tx_length += 1;
  186. }
  187. break;
  188. default:
  189. {
  190. setErrorResponse(nbus, ILLEGAL_FUNCTION);
  191. }
  192. }
  193. }
  194. void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
  195. if(request_type == BROADCAST_SPECIFIC_SENSORS) {
  196. }
  197. if(request_type == BROADCAST_GLOBAL) {
  198. switch(nbus->function_code.function) {
  199. #if MODULE_MASTER
  200. case CMD_SYNC:
  201. {
  202. if(nbus->rx_length < 11){
  203. return;
  204. }
  205. RTC_TimeTypeDef sTime = {0};
  206. RTC_DateTypeDef sDate = {0};
  207. sTime.Hours = nbus->rx_buffer[7];
  208. sTime.Minutes = nbus->rx_buffer[8];
  209. sTime.Seconds = nbus->rx_buffer[9];
  210. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  211. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  212. HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
  213. sDate.WeekDay = nbus->rx_buffer[6];
  214. sDate.Date = nbus->rx_buffer[5];
  215. sDate.Month = nbus->rx_buffer[4];
  216. sDate.Year = nbus->rx_buffer[3];
  217. HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
  218. }
  219. break;
  220. #endif
  221. case CMD_START:
  222. {
  223. nbus->measure_active = MEASURE_RUNNING;
  224. }
  225. break;
  226. case CMD_STOP:
  227. {
  228. nbus->measure_active = MEASURE_STOPPED;
  229. }
  230. break;
  231. default:
  232. ; // nothing
  233. }
  234. }
  235. nbus->send_response = NO_RESPONSE;
  236. }