uart_tasks.c 25 KB

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