uart_tasks.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * uart_tasks.c
  3. *
  4. * Created on: Aug 14, 2024
  5. * Author: jakubski
  6. */
  7. #include "cmsis_os.h"
  8. #include "main.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "interprocess_data.h"
  12. #include "mock_tasks.h"
  13. #include "uart_tasks.h"
  14. enum SerialReceiverStates { srWaitForHeader, srCheckCrc, srRecieveData, srExecuteCmd, srFail, srFinish, srLast };
  15. extern UART_HandleTypeDef huart1;
  16. extern UART_HandleTypeDef huart2;
  17. extern UART_HandleTypeDef huart3;
  18. extern UART_HandleTypeDef huart6;
  19. extern UART_HandleTypeDef huart8;
  20. extern DMA_HandleTypeDef hdma_uart8_rx;
  21. extern CRC_HandleTypeDef hcrc;
  22. uint8_t uart1RxBuffer[UART1_RX_BUFF_SIZE] = { 0 };
  23. uint8_t uart1TxBuffer[UART1_TX_BUFF_SIZE] = { 0 };
  24. uint8_t uart1TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  25. uint8_t uart2RxBuffer[UART2_RX_BUFF_SIZE] = { 0 };
  26. uint8_t uart2TxBuffer[UART2_TX_BUFF_SIZE] = { 0 };
  27. uint8_t uart2TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  28. uint8_t uart3RxBuffer[UART3_RX_BUFF_SIZE] = { 0 };
  29. uint8_t uart3TxBuffer[UART3_TX_BUFF_SIZE] = { 0 };
  30. uint8_t uart3TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  31. uint8_t uart6RxBuffer[UART6_RX_BUFF_SIZE] = { 0 };
  32. uint8_t uart6TxBuffer[UART6_TX_BUFF_SIZE] = { 0 };
  33. uint8_t uart6TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  34. uint8_t uart8RxBuffer[UART8_RX_BUFF_SIZE] = { 0 };
  35. uint8_t uart8TxBuffer[UART8_TX_BUFF_SIZE] = { 0 };
  36. uint8_t uart8TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  37. //uint8_t boardToUartNumberMap[SLAVES_COUNT] = { 8, 3, 6, 2 };
  38. uint8_t boardToUartNumberMap[SLAVES_COUNT] = { 1, 3, 6, 2 };
  39. UartTaskData uart1TaskData = { 0 }; // Board 1
  40. UartTaskData uart3TaskData = { 0 }; // Board 2
  41. UartTaskData uart6TaskData = { 0 }; // Board 3
  42. UartTaskData uart2TaskData = { 0 }; // Board 4
  43. UartTaskData uart8TaskData = { 0 }; // Debug
  44. //UartTaskData* uartTasks[] = { &uart8TaskData, &uart3TaskData, &uart6TaskData, &uart2TaskData, NULL, };
  45. UartTaskData* uartTasks[] = { &uart1TaskData, &uart3TaskData, &uart6TaskData, &uart2TaskData, NULL, };
  46. uint8_t outputDataBuffer[OUTPUT_DATA_BUFF_SIZE];
  47. uint16_t outputDataBufferPos = 0;
  48. RESMeasurements resMeasurements[SLAVES_COUNT] = { 0 };
  49. SesnorsInfo sensorsInfo[SLAVES_COUNT] = { 0 };
  50. uint32_t slaveLastSeen[SLAVES_COUNT] = { 0 };
  51. osMutexId_t resMeasurementsMutex;
  52. osMutexId_t sensorsInfoMutex;
  53. extern RNG_HandleTypeDef hrng;
  54. void UartTasksInit (void) {
  55. uart1TaskData.uartRxBuffer = uart1RxBuffer;
  56. uart1TaskData.uartRxBufferLen = UART1_RX_BUFF_SIZE;
  57. uart1TaskData.uartTxBuffer = uart1TxBuffer;
  58. uart1TaskData.uartRxBufferLen = UART1_TX_BUFF_SIZE;
  59. uart1TaskData.frameData = uart1TaskFrameData;
  60. uart1TaskData.frameDataLen = UART1_RX_BUFF_SIZE;
  61. uart1TaskData.huart = &huart1;
  62. uart1TaskData.uartNumber = 1;
  63. uart2TaskData.uartRxBuffer = uart2RxBuffer;
  64. uart2TaskData.uartRxBufferLen = UART2_RX_BUFF_SIZE;
  65. uart2TaskData.uartTxBuffer = uart2TxBuffer;
  66. uart2TaskData.uartRxBufferLen = UART2_TX_BUFF_SIZE;
  67. uart2TaskData.frameData = uart2TaskFrameData;
  68. uart2TaskData.frameDataLen = UART2_RX_BUFF_SIZE;
  69. uart2TaskData.huart = &huart2;
  70. uart2TaskData.uartNumber = 2;
  71. uart3TaskData.uartRxBuffer = uart3RxBuffer;
  72. uart3TaskData.uartRxBufferLen = UART3_RX_BUFF_SIZE;
  73. uart3TaskData.uartTxBuffer = uart3TxBuffer;
  74. uart3TaskData.uartRxBufferLen = UART3_TX_BUFF_SIZE;
  75. uart3TaskData.frameData = uart3TaskFrameData;
  76. uart3TaskData.frameDataLen = UART3_RX_BUFF_SIZE;
  77. uart3TaskData.huart = &huart3;
  78. uart3TaskData.uartNumber = 3;
  79. uart6TaskData.uartRxBuffer = uart6RxBuffer;
  80. uart6TaskData.uartRxBufferLen = UART6_RX_BUFF_SIZE;
  81. uart6TaskData.uartTxBuffer = uart6TxBuffer;
  82. uart6TaskData.uartRxBufferLen = UART6_TX_BUFF_SIZE;
  83. uart6TaskData.frameData = uart6TaskFrameData;
  84. uart6TaskData.frameDataLen = UART6_RX_BUFF_SIZE;
  85. uart6TaskData.huart = &huart6;
  86. uart6TaskData.uartNumber = 6;
  87. uart8TaskData.uartRxBuffer = uart8RxBuffer;
  88. uart8TaskData.uartRxBufferLen = UART8_RX_BUFF_SIZE;
  89. uart8TaskData.uartTxBuffer = uart8TxBuffer;
  90. uart8TaskData.uartRxBufferLen = UART8_TX_BUFF_SIZE;
  91. uart8TaskData.frameData = uart8TaskFrameData;
  92. uart8TaskData.frameDataLen = UART8_RX_BUFF_SIZE;
  93. uart8TaskData.huart = &huart8;
  94. uart8TaskData.uartNumber = 8;
  95. UartTaskCreate (&uart1TaskData);
  96. UartTaskCreate (&uart2TaskData);
  97. UartTaskCreate (&uart3TaskData);
  98. UartTaskCreate (&uart6TaskData);
  99. UartTaskCreate (&uart8TaskData);
  100. }
  101. void UartTaskCreate (UartTaskData* uartTaskData) {
  102. osThreadAttr_t osThreadAttrRxUart = { 0 };
  103. osThreadAttr_t osThreadAttrTxUart = { 0 };
  104. uartTaskData->processRxDataMsgBuffer = xMessageBufferCreate (INPUT_DATA_BUFF_SIZE);
  105. uartTaskData->processDataCb = NULL;
  106. // osThreadAttrRxUart.name = "os_thread_uart1_rx";
  107. osThreadAttrRxUart.stack_size = configMINIMAL_STACK_SIZE * 2;
  108. osThreadAttrRxUart.priority = (osPriority_t)osPriorityHigh;
  109. uartTaskData->uartRecieveTaskHandle = osThreadNew (UartRxTask, uartTaskData, &osThreadAttrRxUart);
  110. osMessageQueueAttr_t uartTxMsgQueueAttr = { 0 };
  111. // uartTxMsgQueueAttr.name = "uart1TxMsgQueue";
  112. uartTaskData->sendCmdToSlaveQueue = osMessageQueueNew (16, sizeof (InterProcessData), &uartTxMsgQueueAttr);
  113. // osThreadAttrTxUart.name = "os_thread_uart1_tx";
  114. osThreadAttrTxUart.stack_size = configMINIMAL_STACK_SIZE * 4;
  115. osThreadAttrTxUart.priority = (osPriority_t)osPriorityNormal;
  116. uartTaskData->uartTransmitTaskHandle = osThreadNew (UartTxTask, uartTaskData, &osThreadAttrTxUart);
  117. }
  118. void HAL_UART_RxCpltCallback (UART_HandleTypeDef* huart) {
  119. // osSemaphoreRelease(uart8RxSemaphore);
  120. }
  121. void HAL_UARTEx_RxEventCallback (UART_HandleTypeDef* huart, uint16_t Size) {
  122. if (huart->Instance == USART1) {
  123. HandleUartRxCallback (&uart1TaskData, huart, Size);
  124. } else if (huart->Instance == USART2) {
  125. HandleUartRxCallback (&uart2TaskData, huart, Size);
  126. } else if (huart->Instance == USART3) {
  127. HandleUartRxCallback (&uart3TaskData, huart, Size);
  128. } else if (huart->Instance == USART6) {
  129. HandleUartRxCallback (&uart6TaskData, huart, Size);
  130. } else if (huart->Instance == UART8) {
  131. HandleUartRxCallback (&uart8TaskData, huart, Size);
  132. }
  133. }
  134. void HAL_UART_TxCpltCallback (UART_HandleTypeDef* huart) {
  135. if (huart->Instance == UART8) {
  136. }
  137. }
  138. void HandleUartRxCallback (UartTaskData* uartTaskData, UART_HandleTypeDef* huart, uint16_t Size) {
  139. BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
  140. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  141. memcpy (&(uartTaskData->frameData[uartTaskData->frameBytesCount]), uartTaskData->uartRxBuffer, Size);
  142. uartTaskData->frameBytesCount += Size;
  143. osMutexRelease (uartTaskData->rxDataBufferMutex);
  144. xTaskNotifyFromISR (uartTaskData->uartRecieveTaskHandle, Size, eSetValueWithOverwrite, &pxHigherPriorityTaskWoken);
  145. // HAL_UARTEx_ReceiveToIdle_DMA(huart, uart8RxBuffer, UART8_RX_BUFF_SIZE);
  146. // __HAL_DMA_DISABLE_IT(&hdma_uart8_rx, DMA_IT_HT);
  147. HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
  148. portEND_SWITCHING_ISR (pxHigherPriorityTaskWoken);
  149. }
  150. void UartRxTask (void* argument) {
  151. UartTaskData* uartTaskData = (UartTaskData*)argument;
  152. SerialProtocolFrameData spFrameData = { 0 };
  153. uint32_t bytesRec = 0;
  154. uint32_t crc = 0;
  155. uint16_t frameCommandRaw = 0x0000;
  156. uint16_t frameBytesCount = 0;
  157. uint16_t frameCrc = 0;
  158. uint16_t frameTotalLength = 0;
  159. uint16_t dataToSend = 0;
  160. portBASE_TYPE crcPass = pdFAIL;
  161. portBASE_TYPE proceed = pdFALSE;
  162. portBASE_TYPE frameTimeout = pdFAIL;
  163. enum SerialReceiverStates receverState = srWaitForHeader;
  164. uartTaskData->rxDataBufferMutex = osMutexNew (NULL);
  165. HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
  166. while (pdTRUE) {
  167. frameTimeout = !(xTaskNotifyWait (0, 0, &bytesRec, pdMS_TO_TICKS (FRAME_TIMEOUT_MS)));
  168. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  169. frameBytesCount = uartTaskData->frameBytesCount;
  170. osMutexRelease (uartTaskData->rxDataBufferMutex);
  171. if ((frameTimeout == pdTRUE) && (frameBytesCount > 0)) {
  172. receverState = srFail;
  173. proceed = pdTRUE;
  174. } else {
  175. if (frameTimeout == pdFALSE) {
  176. proceed = pdTRUE;
  177. printf ("Uart%d: RX bytes received: %ld\n", uartTaskData->uartNumber, bytesRec);
  178. } else {
  179. if (uartTaskData->huart->RxState == HAL_UART_STATE_READY) {
  180. HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
  181. }
  182. }
  183. }
  184. while (proceed) {
  185. switch (receverState) {
  186. case srWaitForHeader:
  187. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  188. if (uartTaskData->frameData[0] == FRAME_INDICATOR) {
  189. if (frameBytesCount > FRAME_ID_LENGTH) {
  190. spFrameData.frameHeader.frameId =
  191. CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[FRAME_HEADER_LENGTH - FRAME_RESP_STAT_LENGTH - FRAME_DATALEN_LENGTH - FRAME_ID_LENGTH - FRAME_COMMAND_LENGTH]));
  192. }
  193. if (frameBytesCount > FRAME_ID_LENGTH + FRAME_COMMAND_LENGTH) {
  194. frameCommandRaw = CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[FRAME_HEADER_LENGTH - FRAME_RESP_STAT_LENGTH - FRAME_DATALEN_LENGTH - FRAME_COMMAND_LENGTH]));
  195. spFrameData.frameHeader.frameCommand = (SerialProtocolCommands)(frameCommandRaw & 0x7FFF);
  196. spFrameData.frameHeader.isResponseFrame = (frameCommandRaw & 0x8000) != 0 ? pdTRUE : pdFALSE;
  197. }
  198. if ((frameBytesCount > FRAME_ID_LENGTH + FRAME_COMMAND_LENGTH + FRAME_RESP_STAT_LENGTH) && ((spFrameData.frameHeader.frameCommand & 0x8000) != 0)) {
  199. spFrameData.frameHeader.respStatus = (SerialProtocolRespStatus)(uartTaskData->frameData[FRAME_ID_LENGTH + FRAME_COMMAND_LENGTH + FRAME_RESP_STAT_LENGTH]);
  200. }
  201. if (frameBytesCount >= FRAME_HEADER_LENGTH) {
  202. spFrameData.frameHeader.frameDataLength = CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[FRAME_HEADER_LENGTH - FRAME_RESP_STAT_LENGTH - FRAME_DATALEN_LENGTH]));
  203. frameTotalLength = FRAME_HEADER_LENGTH + spFrameData.frameHeader.frameDataLength + FRAME_CRC_LENGTH;
  204. receverState = srRecieveData;
  205. } else {
  206. proceed = pdFALSE;
  207. }
  208. } else {
  209. if (frameBytesCount > 0) {
  210. receverState = srFail;
  211. } else {
  212. proceed = pdFALSE;
  213. }
  214. }
  215. osMutexRelease (uartTaskData->rxDataBufferMutex);
  216. break;
  217. case srRecieveData:
  218. if (frameBytesCount >= frameTotalLength) {
  219. receverState = srCheckCrc;
  220. } else {
  221. proceed = pdFALSE;
  222. }
  223. break;
  224. case srCheckCrc:
  225. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  226. frameCrc = CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[frameTotalLength - FRAME_CRC_LENGTH]));
  227. crc = HAL_CRC_Calculate (&hcrc, (uint32_t*)(uartTaskData->frameData), frameTotalLength - FRAME_CRC_LENGTH);
  228. osMutexRelease (uartTaskData->rxDataBufferMutex);
  229. crcPass = frameCrc == crc;
  230. if (crcPass) {
  231. printf ("Uart%d: Frame CRC PASS\n", uartTaskData->uartNumber);
  232. receverState = srExecuteCmd;
  233. } else {
  234. receverState = srFail;
  235. }
  236. break;
  237. case srExecuteCmd:
  238. if ((uartTaskData->processDataCb != NULL) || (uartTaskData->processRxDataMsgBuffer != NULL)) {
  239. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  240. memcpy (spFrameData.dataBuffer, &(uartTaskData->frameData[FRAME_HEADER_LENGTH]), spFrameData.frameHeader.frameDataLength);
  241. osMutexRelease (uartTaskData->rxDataBufferMutex);
  242. }
  243. if (uartTaskData->processRxDataMsgBuffer != NULL) {
  244. if(xMessageBufferSend (uartTaskData->processRxDataMsgBuffer, &spFrameData, sizeof (SerialProtocolFrameHeader) + spFrameData.frameHeader.frameDataLength, pdMS_TO_TICKS (200)) == pdFALSE)
  245. {
  246. receverState = srFail;
  247. break;
  248. }
  249. }
  250. if (uartTaskData->processDataCb != NULL) {
  251. uartTaskData->processDataCb (uartTaskData, &spFrameData);
  252. }
  253. receverState = srFinish;
  254. break;
  255. case srFail:
  256. dataToSend = 0;
  257. if ((frameTimeout == pdTRUE) && (frameBytesCount > 2)) {
  258. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spTimeout, NULL, 0);
  259. printf ("Uart%d: RX data receiver timeout!\n", uartTaskData->uartNumber);
  260. } else if (!crcPass) {
  261. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spCrcFail, NULL, 0);
  262. printf ("Uart%d: Frame CRC FAIL\n", uartTaskData->uartNumber);
  263. }
  264. else
  265. {
  266. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spInternalError, NULL, 0);
  267. }
  268. if (dataToSend > 0) {
  269. HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, dataToSend);
  270. }
  271. printf ("Uart%d: TX bytes sent: %d\n", dataToSend, uartTaskData->uartNumber);
  272. receverState = srFinish;
  273. break;
  274. case srFinish:
  275. default:
  276. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  277. uartTaskData->frameBytesCount = 0;
  278. osMutexRelease (uartTaskData->rxDataBufferMutex);
  279. spFrameData.frameHeader.frameCommand = spUnknown;
  280. frameTotalLength = 0;
  281. outputDataBufferPos = 0;
  282. receverState = srWaitForHeader;
  283. proceed = pdFALSE;
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. void ReadMeasSetFromBuffer(uint8_t* buff, uint16_t* buffPos, float* dataSet)
  290. {
  291. for(uint8_t i = 0; i < 3; i++)
  292. {
  293. ReadFloatFromBuffer(buff, buffPos, &dataSet[i]);
  294. }
  295. }
  296. void UartTxTask (void* argument) {
  297. UartTaskData* const uartTaskData = (UartTaskData*)argument;
  298. InterProcessData data = { 0 };
  299. SerialProtocolFrameData frameData = { 0 };
  300. size_t bytesInMsg;
  301. uint16_t frameId = 0;
  302. uint32_t rndVal = 0;
  303. uint16_t bytesToSend = 0;
  304. SerialProtocolCommands frameCommand = spUnknown;
  305. uint16_t inputDataBufferPos = 0;
  306. uint8_t boardNumber = 0;
  307. while (pdTRUE) {
  308. if (uartTaskData->sendCmdToSlaveQueue != NULL) {
  309. osMessageQueueGet (uartTaskData->sendCmdToSlaveQueue, &data, 0, osWaitForever);
  310. HAL_RNG_GenerateRandomNumber (&hrng, &rndVal);
  311. frameId = (uint16_t)(rndVal & 0xFFFF);
  312. frameCommand = data.spCommand;
  313. outputDataBufferPos = 0;
  314. memset (outputDataBuffer, 0x00, OUTPUT_DATA_BUFF_SIZE);
  315. switch (frameCommand) {
  316. case spSetFanSpeed:
  317. case spSetMotorXOn:
  318. case spSetMotorYOn:
  319. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.integerValues.value[0], sizeof (uint32_t));
  320. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.integerValues.value[1], sizeof (uint32_t));
  321. break;
  322. case spSetDiodeOn: WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.integerValues.value[0], sizeof (uint32_t)); break;
  323. case spSetmotorXMaxCurrent:
  324. case spSetmotorYMaxCurrent: WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.flaotValues.value[0], sizeof (float)); break;
  325. case spGetElectricalMeasurments:
  326. case spGetSensorMeasurments: break;
  327. case spClearPeakMeasurments: break;
  328. case spSetEncoderXValue: WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.flaotValues.value[0], sizeof (float)); break;
  329. case spSetEncoderYValue: WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.flaotValues.value[0], sizeof (float)); break;
  330. case spSetVoltageMeasGains:
  331. case spSetVoltageMeasOffsets:
  332. case spSetCurrentMeasGains:
  333. case spSetCurrentMeasOffsets:
  334. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.flaotValues.value[0], sizeof (float));
  335. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.flaotValues.value[1], sizeof (float));
  336. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &data.values.flaotValues.value[2], sizeof (float));
  337. break;
  338. case spResetSystem: break;
  339. default: continue; break;
  340. }
  341. bytesToSend = PrepareReqFrame (uartTaskData->uartTxBuffer, frameId, frameCommand, outputDataBuffer, outputDataBufferPos);
  342. HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, bytesToSend);
  343. bytesInMsg = xMessageBufferReceive (uartTaskData->processRxDataMsgBuffer, &frameData, INPUT_DATA_BUFF_SIZE, pdMS_TO_TICKS (1000));
  344. for (boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++) {
  345. if (boardToUartNumberMap[boardNumber] == uartTaskData->uartNumber) {
  346. break;
  347. }
  348. }
  349. if (bytesInMsg == 0) {
  350. if (frameCommand == spGetElectricalMeasurments) {
  351. osMutexAcquire (resMeasurementsMutex, osWaitForever);
  352. slaveLastSeen[boardNumber]++;
  353. osMutexRelease (resMeasurementsMutex);
  354. }
  355. printf ("Uart%d: Response timeout for frameId 0x%x\n", uartTaskData->uartNumber, frameId);
  356. } else {
  357. if ((frameId == frameData.frameHeader.frameId) && (frameData.frameHeader.respStatus == spOK)) {
  358. printf ("Uart%d: Response for frameId 0x%x OK\n", uartTaskData->uartNumber, frameId);
  359. slaveLastSeen[boardNumber] = 0;
  360. switch (frameData.frameHeader.frameCommand) {
  361. case spGetElectricalMeasurments:
  362. osMutexAcquire (resMeasurementsMutex, osWaitForever);
  363. RESMeasurements* resMeas = &resMeasurements[boardNumber];
  364. inputDataBufferPos = 0;
  365. ReadMeasSetFromBuffer (frameData.dataBuffer, &inputDataBufferPos, resMeas->voltageRMS);
  366. ReadMeasSetFromBuffer (frameData.dataBuffer, &inputDataBufferPos, resMeas->voltagePeak);
  367. ReadMeasSetFromBuffer (frameData.dataBuffer, &inputDataBufferPos, resMeas->currentRMS);
  368. ReadMeasSetFromBuffer (frameData.dataBuffer, &inputDataBufferPos, resMeas->currentPeak);
  369. ReadMeasSetFromBuffer (frameData.dataBuffer, &inputDataBufferPos, resMeas->power);
  370. osMutexRelease (resMeasurementsMutex);
  371. break;
  372. case spGetSensorMeasurments:
  373. osMutexAcquire (sensorsInfoMutex, osWaitForever);
  374. inputDataBufferPos = 0;
  375. SesnorsInfo* sensors = &sensorsInfo[boardNumber];
  376. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->pvTemperature[0]);
  377. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->pvTemperature[1]);
  378. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->fanVoltage);
  379. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->pvEncoderX);
  380. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->pvEncoderY);
  381. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->motorXStatus);
  382. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->motorYStatus);
  383. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->motorXAveCurrent);
  384. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->motorYAveCurrent);
  385. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->motorXPeakCurrent);
  386. ReadFloatFromBuffer (frameData.dataBuffer, &inputDataBufferPos, &sensors->motorYPeakCurrent);
  387. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->limitXSwitchUp);
  388. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->limitXSwitchDown);
  389. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->limitXSwitchCenter);
  390. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->limitYSwitchUp);
  391. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->limitYSwitchDown);
  392. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->limitYSwitchCenter);
  393. ReadByteFromBufer (frameData.dataBuffer, &inputDataBufferPos, &sensors->powerSupplyFailMask);
  394. osMutexRelease (sensorsInfoMutex);
  395. break;
  396. default: break;
  397. }
  398. }
  399. }
  400. } else {
  401. osDelay (pdMS_TO_TICKS (1000));
  402. }
  403. }
  404. }
  405. void MeasurmentsReqSchedulerTaskInit (void) {
  406. osThreadAttr_t osThreadAttrMeasurmentsReqSchedulerTask = { 0 };
  407. osThreadAttrMeasurmentsReqSchedulerTask.name = "os_thread_XXX";
  408. osThreadAttrMeasurmentsReqSchedulerTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  409. osThreadAttrMeasurmentsReqSchedulerTask.priority = (osPriority_t)osPriorityNormal;
  410. osThreadNew (MeasurmentsReqSchedulerTask, uartTasks, &osThreadAttrMeasurmentsReqSchedulerTask);
  411. }
  412. void MeasurmentsReqSchedulerTask (void* argument) {
  413. while (pdTRUE) {
  414. __uintptr_t* ptr = (__uintptr_t*)argument;
  415. while (*ptr != 0) {
  416. UartTaskData* uartTask = (UartTaskData*)*ptr;
  417. if (uartTask->sendCmdToSlaveQueue != NULL) {
  418. InterProcessData data = { 0 };
  419. uint8_t boardNumber = 0;
  420. for(boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++)
  421. {
  422. if(boardToUartNumberMap[boardNumber] == uartTask->uartNumber)
  423. {
  424. break;
  425. }
  426. }
  427. data.spCommand = spGetElectricalMeasurments;
  428. osMessageQueuePut (uartTask->sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  429. osMutexAcquire (resMeasurementsMutex, osWaitForever);
  430. if(slaveLastSeen[boardNumber] == 0)
  431. {
  432. data.spCommand = spGetSensorMeasurments;
  433. osMessageQueuePut (uartTask->sendCmdToSlaveQueue, &data, 0, (TickType_t)100);
  434. }
  435. osMutexRelease(resMeasurementsMutex);
  436. }
  437. ptr++;
  438. }
  439. osDelay (pdMS_TO_TICKS (MEASURMENTS_SCHEDULER_INTERVAL_MS));
  440. }
  441. }