uart_tasks.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include "adc_buffers.h"
  14. #include "interprocess_data.h"
  15. #include "meas_tasks.h"
  16. #include "measurements.h"
  17. #include "mock_tasks.h"
  18. #include "peripherial.h"
  19. #include "uart_tasks.h"
  20. #include "position_task.h"
  21. enum SerialReceiverStates { srWaitForHeader, srCheckCrc, srRecieveData, srExecuteCmd, srFail, srFinish, srLast };
  22. extern UART_HandleTypeDef huart1;
  23. extern UART_HandleTypeDef huart8;
  24. extern DMA_HandleTypeDef hdma_uart8_rx;
  25. extern CRC_HandleTypeDef hcrc;
  26. extern DAC_HandleTypeDef hdac1;
  27. extern osTimerId_t debugLedTimerHandle;
  28. extern osTimerId_t fanTimerHandle;
  29. extern osTimerId_t motorXTimerHandle;
  30. extern osTimerId_t motorYTimerHandle;
  31. extern TIM_HandleTypeDef htim1;
  32. extern TIM_HandleTypeDef htim3;
  33. extern TIM_OC_InitTypeDef fanTimerConfigOC;
  34. extern TIM_OC_InitTypeDef motorXYTimerConfigOC;
  35. extern COMP_HandleTypeDef hcomp1;
  36. uint8_t uart1RxBuffer[UART1_RX_BUFF_SIZE] = { 0 };
  37. uint8_t uart1TxBuffer[UART1_TX_BUFF_SIZE] = { 0 };
  38. uint8_t uart1TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  39. uint8_t uart8RxBuffer[UART8_RX_BUFF_SIZE] = { 0 };
  40. uint8_t uart8TxBuffer[UART8_TX_BUFF_SIZE] = { 0 };
  41. uint8_t uart8TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
  42. UartTaskData uart1TaskData = { 0 }; // Board 1
  43. UartTaskData uart8TaskData = { 0 }; // Debug
  44. uint8_t outputDataBuffer[OUTPUT_DATA_BUFF_SIZE];
  45. uint16_t outputDataBufferPos = 0;
  46. extern RNG_HandleTypeDef hrng;
  47. void UartTasksInit (void) {
  48. uart1TaskData.uartRxBuffer = uart1RxBuffer;
  49. uart1TaskData.uartRxBufferLen = UART1_RX_BUFF_SIZE;
  50. uart1TaskData.uartTxBuffer = uart1TxBuffer;
  51. uart1TaskData.uartRxBufferLen = UART1_TX_BUFF_SIZE;
  52. uart1TaskData.frameData = uart1TaskFrameData;
  53. uart1TaskData.frameDataLen = UART1_RX_BUFF_SIZE;
  54. uart1TaskData.huart = &huart1;
  55. uart1TaskData.uartNumber = 1;
  56. uart1TaskData.processDataCb = Uart1ReceivedDataProcessCallback;
  57. uart1TaskData.processRxDataMsgBuffer = NULL;
  58. uart8TaskData.uartRxBuffer = uart8RxBuffer;
  59. uart8TaskData.uartRxBufferLen = UART8_RX_BUFF_SIZE;
  60. uart8TaskData.uartTxBuffer = uart8TxBuffer;
  61. uart8TaskData.uartRxBufferLen = UART8_TX_BUFF_SIZE;
  62. uart8TaskData.frameData = uart8TaskFrameData;
  63. uart8TaskData.frameDataLen = UART8_RX_BUFF_SIZE;
  64. uart8TaskData.huart = &huart8;
  65. uart8TaskData.uartNumber = 8;
  66. uart8TaskData.processDataCb = Uart8ReceivedDataProcessCallback;
  67. uart8TaskData.processRxDataMsgBuffer = NULL;
  68. #ifdef USE_UART8_INSTEAD_UART1
  69. UartTaskCreate (&uart8TaskData);
  70. #else
  71. UartTaskCreate (&uart1TaskData);
  72. #endif
  73. }
  74. void UartTaskCreate (UartTaskData* uartTaskData) {
  75. osThreadAttr_t osThreadAttrRxUart = { 0 };
  76. osThreadAttrRxUart.stack_size = configMINIMAL_STACK_SIZE * 2;
  77. osThreadAttrRxUart.priority = (osPriority_t)osPriorityHigh;
  78. uartTaskData->uartRecieveTaskHandle = osThreadNew (UartRxTask, uartTaskData, &osThreadAttrRxUart);
  79. }
  80. void HAL_UART_RxCpltCallback (UART_HandleTypeDef* huart) {
  81. }
  82. void HAL_UARTEx_RxEventCallback (UART_HandleTypeDef* huart, uint16_t Size) {
  83. if (huart->Instance == USART1) {
  84. HandleUartRxCallback (&uart1TaskData, huart, Size);
  85. } else if (huart->Instance == UART8) {
  86. HandleUartRxCallback (&uart8TaskData, huart, Size);
  87. }
  88. }
  89. void HAL_UART_TxCpltCallback (UART_HandleTypeDef* huart) {
  90. if (huart->Instance == UART8) {
  91. }
  92. }
  93. void HandleUartRxCallback (UartTaskData* uartTaskData, UART_HandleTypeDef* huart, uint16_t Size) {
  94. BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
  95. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  96. memcpy (&(uartTaskData->frameData[uartTaskData->frameBytesCount]), uartTaskData->uartRxBuffer, Size);
  97. uartTaskData->frameBytesCount += Size;
  98. osMutexRelease (uartTaskData->rxDataBufferMutex);
  99. xTaskNotifyFromISR (uartTaskData->uartRecieveTaskHandle, Size, eSetValueWithOverwrite, &pxHigherPriorityTaskWoken);
  100. HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
  101. portEND_SWITCHING_ISR (pxHigherPriorityTaskWoken);
  102. }
  103. void UartRxTask (void* argument) {
  104. UartTaskData* uartTaskData = (UartTaskData*)argument;
  105. SerialProtocolFrameData spFrameData = { 0 };
  106. uint32_t bytesRec = 0;
  107. uint32_t crc = 0;
  108. uint16_t frameCommandRaw = 0x0000;
  109. uint16_t frameBytesCount = 0;
  110. uint16_t frameCrc = 0;
  111. uint16_t frameTotalLength = 0;
  112. uint16_t dataToSend = 0;
  113. portBASE_TYPE crcPass = pdFAIL;
  114. portBASE_TYPE proceed = pdFALSE;
  115. portBASE_TYPE frameTimeout = pdFAIL;
  116. enum SerialReceiverStates receverState = srWaitForHeader;
  117. uartTaskData->rxDataBufferMutex = osMutexNew (NULL);
  118. HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
  119. while (pdTRUE) {
  120. frameTimeout = !(xTaskNotifyWait (0, 0, &bytesRec, pdMS_TO_TICKS (FRAME_TIMEOUT_MS)));
  121. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  122. frameBytesCount = uartTaskData->frameBytesCount;
  123. osMutexRelease (uartTaskData->rxDataBufferMutex);
  124. if ((frameTimeout == pdTRUE) && (frameBytesCount > 0)) {
  125. receverState = srFail;
  126. proceed = pdTRUE;
  127. } else {
  128. if (frameTimeout == pdFALSE) {
  129. proceed = pdTRUE;
  130. #ifdef SERIAL_PROTOCOL_DBG
  131. printf ("Uart%d: RX bytes received: %ld\n", uartTaskData->uartNumber, bytesRec);
  132. #endif
  133. } else {
  134. if (uartTaskData->huart->RxState == HAL_UART_STATE_READY) {
  135. HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
  136. }
  137. }
  138. }
  139. while (proceed) {
  140. switch (receverState) {
  141. case srWaitForHeader:
  142. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  143. if (uartTaskData->frameData[0] == FRAME_INDICATOR) {
  144. if (frameBytesCount > FRAME_ID_LENGTH) {
  145. spFrameData.frameHeader.frameId =
  146. CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[FRAME_HEADER_LENGTH - FRAME_RESP_STAT_LENGTH - FRAME_DATALEN_LENGTH - FRAME_ID_LENGTH - FRAME_COMMAND_LENGTH]));
  147. }
  148. if (frameBytesCount > FRAME_ID_LENGTH + FRAME_COMMAND_LENGTH) {
  149. frameCommandRaw = CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[FRAME_HEADER_LENGTH - FRAME_RESP_STAT_LENGTH - FRAME_DATALEN_LENGTH - FRAME_COMMAND_LENGTH]));
  150. spFrameData.frameHeader.frameCommand = (SerialProtocolCommands)(frameCommandRaw & 0x7FFF);
  151. spFrameData.frameHeader.isResponseFrame = (frameCommandRaw & 0x8000) != 0 ? pdTRUE : pdFALSE;
  152. }
  153. if ((frameBytesCount > FRAME_ID_LENGTH + FRAME_COMMAND_LENGTH + FRAME_RESP_STAT_LENGTH) && ((spFrameData.frameHeader.frameCommand & 0x8000) != 0)) {
  154. spFrameData.frameHeader.respStatus = (SerialProtocolRespStatus)(uartTaskData->frameData[FRAME_ID_LENGTH + FRAME_COMMAND_LENGTH + FRAME_RESP_STAT_LENGTH]);
  155. }
  156. if (frameBytesCount >= FRAME_HEADER_LENGTH) {
  157. spFrameData.frameHeader.frameDataLength = CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[FRAME_HEADER_LENGTH - FRAME_RESP_STAT_LENGTH - FRAME_DATALEN_LENGTH]));
  158. frameTotalLength = FRAME_HEADER_LENGTH + spFrameData.frameHeader.frameDataLength + FRAME_CRC_LENGTH;
  159. receverState = srRecieveData;
  160. } else {
  161. proceed = pdFALSE;
  162. }
  163. } else {
  164. if (frameBytesCount > 0) {
  165. receverState = srFail;
  166. } else {
  167. proceed = pdFALSE;
  168. }
  169. }
  170. osMutexRelease (uartTaskData->rxDataBufferMutex);
  171. break;
  172. case srRecieveData:
  173. if (frameBytesCount >= frameTotalLength) {
  174. receverState = srCheckCrc;
  175. } else {
  176. proceed = pdFALSE;
  177. }
  178. break;
  179. case srCheckCrc:
  180. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  181. frameCrc = CONVERT_BYTES_TO_SHORT_WORD (&(uartTaskData->frameData[frameTotalLength - FRAME_CRC_LENGTH]));
  182. crc = HAL_CRC_Calculate (&hcrc, (uint32_t*)(uartTaskData->frameData), frameTotalLength - FRAME_CRC_LENGTH);
  183. osMutexRelease (uartTaskData->rxDataBufferMutex);
  184. crcPass = frameCrc == crc;
  185. if (crcPass) {
  186. #ifdef SERIAL_PROTOCOL_DBG
  187. printf ("Uart%d: Frame CRC PASS\n", uartTaskData->uartNumber);
  188. #endif
  189. receverState = srExecuteCmd;
  190. } else {
  191. receverState = srFail;
  192. }
  193. break;
  194. case srExecuteCmd:
  195. if ((uartTaskData->processDataCb != NULL) || (uartTaskData->processRxDataMsgBuffer != NULL)) {
  196. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  197. memcpy (spFrameData.dataBuffer, &(uartTaskData->frameData[FRAME_HEADER_LENGTH]), spFrameData.frameHeader.frameDataLength);
  198. osMutexRelease (uartTaskData->rxDataBufferMutex);
  199. }
  200. if (uartTaskData->processRxDataMsgBuffer != NULL) {
  201. if (xMessageBufferSend (uartTaskData->processRxDataMsgBuffer, &spFrameData, sizeof (SerialProtocolFrameHeader) + spFrameData.frameHeader.frameDataLength, pdMS_TO_TICKS (200)) == pdFALSE) {
  202. receverState = srFail;
  203. break;
  204. }
  205. }
  206. if (uartTaskData->processDataCb != NULL) {
  207. uartTaskData->processDataCb (uartTaskData, &spFrameData);
  208. }
  209. receverState = srFinish;
  210. break;
  211. case srFail:
  212. dataToSend = 0;
  213. if ((frameTimeout == pdTRUE) && (frameBytesCount > 2)) {
  214. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spTimeout, NULL, 0);
  215. #ifdef SERIAL_PROTOCOL_DBG
  216. printf ("Uart%d: RX data receiver timeout!\n", uartTaskData->uartNumber);
  217. #endif
  218. } else if (!crcPass) {
  219. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spCrcFail, NULL, 0);
  220. #ifdef SERIAL_PROTOCOL_DBG
  221. printf ("Uart%d: Frame CRC FAIL\n", uartTaskData->uartNumber);
  222. #endif
  223. } else {
  224. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spInternalError, NULL, 0);
  225. }
  226. if (dataToSend > 0) {
  227. HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, dataToSend);
  228. }
  229. #ifdef SERIAL_PROTOCOL_DBG
  230. printf ("Uart%d: TX bytes sent: %d\n", dataToSend, uartTaskData->uartNumber);
  231. #endif
  232. receverState = srFinish;
  233. break;
  234. case srFinish:
  235. default:
  236. osMutexAcquire (uartTaskData->rxDataBufferMutex, osWaitForever);
  237. uartTaskData->frameBytesCount = 0;
  238. osMutexRelease (uartTaskData->rxDataBufferMutex);
  239. spFrameData.frameHeader.frameCommand = spUnknown;
  240. frameTotalLength = 0;
  241. outputDataBufferPos = 0;
  242. receverState = srWaitForHeader;
  243. proceed = pdFALSE;
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. void Uart8ReceivedDataProcessCallback (void* arg, SerialProtocolFrameData* spFrameData) {
  250. Uart1ReceivedDataProcessCallback (arg, spFrameData);
  251. }
  252. void Uart1ReceivedDataProcessCallback (void* arg, SerialProtocolFrameData* spFrameData) {
  253. UartTaskData* uartTaskData = (UartTaskData*)arg;
  254. uint16_t dataToSend = 0;
  255. outputDataBufferPos = 0;
  256. uint16_t inputDataBufferPos = 0;
  257. SerialProtocolRespStatus respStatus = spUnknownCommand;
  258. switch (spFrameData->frameHeader.frameCommand) {
  259. case spGetElectricalMeasurments:
  260. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  261. for (int i = 0; i < 3; i++) {
  262. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &resMeasurements.voltageRMS[i], sizeof (float));
  263. }
  264. for (int i = 0; i < 3; i++) {
  265. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &resMeasurements.voltagePeak[i], sizeof (float));
  266. }
  267. for (int i = 0; i < 3; i++) {
  268. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &resMeasurements.currentRMS[i], sizeof (float));
  269. }
  270. for (int i = 0; i < 3; i++) {
  271. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &resMeasurements.currentPeak[i], sizeof (float));
  272. }
  273. for (int i = 0; i < 3; i++) {
  274. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &resMeasurements.power[i], sizeof (float));
  275. }
  276. osMutexRelease (resMeasurementsMutex);
  277. respStatus = spOK;
  278. } else {
  279. respStatus = spInternalError;
  280. }
  281. break;
  282. case spGetSensorMeasurments:
  283. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  284. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.pvTemperature[0], sizeof (float));
  285. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.pvTemperature[1], sizeof (float));
  286. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.fanVoltage, sizeof (float));
  287. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.pvEncoderXraw, sizeof (int32_t));
  288. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.pvEncoderYraw, sizeof (int32_t));
  289. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.motorXStatus, sizeof (uint8_t));
  290. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.motorYStatus, sizeof (uint8_t));
  291. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.motorXAveCurrent, sizeof (float));
  292. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.motorYAveCurrent, sizeof (float));
  293. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.motorXPeakCurrent, sizeof (float));
  294. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.motorYPeakCurrent, sizeof (float));
  295. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.limitXSwitchUp, sizeof (uint8_t));
  296. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.limitXSwitchDown, sizeof (uint8_t));
  297. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.limitXSwitchCenter, sizeof (uint8_t));
  298. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.limitYSwitchUp, sizeof (uint8_t));
  299. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.limitYSwitchDown, sizeof (uint8_t));
  300. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.limitYSwitchCenter, sizeof (uint8_t));
  301. uint8_t comparatorOutput = HAL_COMP_GetOutputLevel (&hcomp1) == COMP_OUTPUT_LEVEL_HIGH ? 1 : 0;
  302. sensorsInfo.powerSupplyFailMask = ~((comparatorOutput << 1) | HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_3)) & 0x01;
  303. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.powerSupplyFailMask, sizeof (uint8_t));
  304. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.currentXPosition, sizeof (float));
  305. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.currentYPosition, sizeof (float));
  306. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionXOffset, sizeof (float));
  307. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionYOffset, sizeof (float));
  308. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionXWeak, sizeof (uint8_t));
  309. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionYWeak, sizeof (uint8_t));
  310. osMutexRelease (sensorsInfoMutex);
  311. respStatus = spOK;
  312. } else {
  313. respStatus = spInternalError;
  314. }
  315. break;
  316. case spSetFanSpeed:
  317. osTimerStop (fanTimerHandle);
  318. int32_t fanTimerPeriod = 0;
  319. uint32_t pulse = 0;
  320. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, &pulse);
  321. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&fanTimerPeriod);
  322. fanTimerConfigOC.Pulse = pulse * 10;
  323. if (HAL_TIM_PWM_ConfigChannel (&htim1, &fanTimerConfigOC, TIM_CHANNEL_2) != HAL_OK) {
  324. Error_Handler ();
  325. }
  326. if (fanTimerPeriod > 0) {
  327. osTimerStart (fanTimerHandle, fanTimerPeriod * 1000);
  328. HAL_TIM_PWM_Start (&htim1, TIM_CHANNEL_2);
  329. } else if (fanTimerPeriod == 0) {
  330. osTimerStop (fanTimerHandle);
  331. HAL_TIM_PWM_Stop (&htim1, TIM_CHANNEL_2);
  332. } else if (fanTimerPeriod == -1) {
  333. osTimerStop (fanTimerHandle);
  334. HAL_TIM_PWM_Start (&htim1, TIM_CHANNEL_2);
  335. }
  336. respStatus = spOK;
  337. break;
  338. case spSetMotorXOn:
  339. int32_t motorXPWMPulse = 0;
  340. int32_t motorXTimerPeriod = 0;
  341. uint32_t motorXStatus = 0;
  342. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXPWMPulse);
  343. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXTimerPeriod);
  344. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  345. motorXStatus =
  346. MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, motorXPWMPulse, motorXTimerPeriod, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
  347. sensorsInfo.motorXStatus = motorXStatus;
  348. if (motorXStatus == 1) {
  349. sensorsInfo.motorXPeakCurrent = 0.0;
  350. }
  351. osMutexRelease (sensorsInfoMutex);
  352. respStatus = spOK;
  353. } else {
  354. respStatus = spInternalError;
  355. }
  356. break;
  357. case spSetMotorYOn:
  358. int32_t motorYPWMPulse = 0;
  359. int32_t motorYTimerPeriod = 0;
  360. uint32_t motorYStatus = 0;
  361. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYPWMPulse);
  362. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYTimerPeriod);
  363. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  364. motorYStatus =
  365. MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, motorYPWMPulse, motorYTimerPeriod, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
  366. sensorsInfo.motorYStatus = motorYStatus;
  367. if (motorYStatus == 1) {
  368. sensorsInfo.motorYPeakCurrent = 0.0;
  369. }
  370. osMutexRelease (sensorsInfoMutex);
  371. respStatus = spOK;
  372. } else {
  373. respStatus = spInternalError;
  374. }
  375. break;
  376. case spSetDiodeOn:
  377. osTimerStop (debugLedTimerHandle);
  378. int32_t dbgLedTimerPeriod = 0;
  379. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&dbgLedTimerPeriod);
  380. if (dbgLedTimerPeriod > 0) {
  381. osTimerStart (debugLedTimerHandle, dbgLedTimerPeriod * 1000);
  382. DbgLEDOn (DBG_LED1);
  383. } else if (dbgLedTimerPeriod == 0) {
  384. osTimerStop (debugLedTimerHandle);
  385. DbgLEDOff (DBG_LED1);
  386. } else if (dbgLedTimerPeriod == -1) {
  387. osTimerStop (debugLedTimerHandle);
  388. DbgLEDOn (DBG_LED1);
  389. }
  390. respStatus = spOK;
  391. break;
  392. case spSetmotorXMaxCurrent:
  393. float motorXMaxCurrent = 0;
  394. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXMaxCurrent);
  395. uint32_t dacDataCh1 = (uint32_t)(4095 * motorXMaxCurrent / (EXT_VREF_mV * 0.001));
  396. if (dacDataCh1 > 4095) {
  397. dacDataCh1 = 4095;
  398. }
  399. HAL_DAC_SetValue (&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dacDataCh1);
  400. HAL_DAC_Start (&hdac1, DAC_CHANNEL_1);
  401. respStatus = spOK;
  402. break;
  403. case spSetmotorYMaxCurrent:
  404. float motorYMaxCurrent = 0;
  405. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYMaxCurrent);
  406. uint32_t dacDataCh2 = (uint32_t)(4095 * motorYMaxCurrent / (EXT_VREF_mV * 0.001));
  407. if (dacDataCh2 > 4095) {
  408. dacDataCh2 = 4095;
  409. }
  410. HAL_DAC_SetValue (&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dacDataCh2);
  411. HAL_DAC_Start (&hdac1, DAC_CHANNEL_2);
  412. respStatus = spOK;
  413. break;
  414. case spClearPeakMeasurments:
  415. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  416. for (int i = 0; i < 3; i++) {
  417. resMeasurements.voltagePeak[i] = resMeasurements.voltageRMS[i];
  418. resMeasurements.currentPeak[i] = resMeasurements.currentRMS[i];
  419. }
  420. osMutexRelease (resMeasurementsMutex);
  421. respStatus = spOK;
  422. } else {
  423. respStatus = spInternalError;
  424. }
  425. break;
  426. case spSetEncoderXValue:
  427. float enocoderXValue = 0;
  428. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&enocoderXValue);
  429. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  430. sensorsInfo.positionXOffset = enocoderXValue - sensorsInfo.currentXPosition;
  431. osMutexRelease (sensorsInfoMutex);
  432. respStatus = spOK;
  433. } else {
  434. respStatus = spInternalError;
  435. }
  436. break;
  437. case spSetEncoderYValue:
  438. float enocoderYValue = 0;
  439. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&enocoderYValue);
  440. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  441. sensorsInfo.positionYOffset = enocoderYValue - sensorsInfo.currentYPosition;
  442. osMutexRelease (sensorsInfoMutex);
  443. respStatus = spOK;
  444. } else {
  445. respStatus = spInternalError;
  446. }
  447. break;
  448. case spSetVoltageMeasGains:
  449. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  450. for (uint8_t i = 0; i < 3; i++) {
  451. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&U_MeasCorrectionData[i].gain);
  452. }
  453. osMutexRelease (resMeasurementsMutex);
  454. respStatus = spOK;
  455. } else {
  456. respStatus = spInternalError;
  457. }
  458. break;
  459. case spSetVoltageMeasOffsets:
  460. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  461. for (uint8_t i = 0; i < 3; i++) {
  462. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&U_MeasCorrectionData[i].offset);
  463. }
  464. osMutexRelease (resMeasurementsMutex);
  465. respStatus = spOK;
  466. } else {
  467. respStatus = spInternalError;
  468. }
  469. break;
  470. case spSetCurrentMeasGains:
  471. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  472. for (uint8_t i = 0; i < 3; i++) {
  473. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&I_MeasCorrectionData[i].gain);
  474. }
  475. osMutexRelease (resMeasurementsMutex);
  476. respStatus = spOK;
  477. } else {
  478. respStatus = spInternalError;
  479. }
  480. break;
  481. case spSetCurrentMeasOffsets:
  482. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  483. for (uint8_t i = 0; i < 3; i++) {
  484. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&I_MeasCorrectionData[i].offset);
  485. }
  486. osMutexRelease (resMeasurementsMutex);
  487. respStatus = spOK;
  488. } else {
  489. respStatus = spInternalError;
  490. }
  491. break;
  492. case spResetSystem:
  493. __disable_irq();
  494. NVIC_SystemReset();
  495. break;
  496. case spSetPositonX:
  497. PositionControlTaskData posXData __attribute__ ((aligned (32))) = { 0 };
  498. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  499. sensorsInfo.positionXWeak = 1;
  500. osMutexRelease (sensorsInfoMutex);
  501. }
  502. if (positionXControlTaskInitArg.positionSettingQueue != NULL)
  503. {
  504. ReadFloatFromBuffer(spFrameData->dataBuffer, &inputDataBufferPos, &posXData.positionSettingValue);
  505. osMessageQueuePut(positionXControlTaskInitArg.positionSettingQueue, &posXData, 0, 0);
  506. }
  507. break;
  508. case spSetPositonY:
  509. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  510. sensorsInfo.positionYWeak = 1;
  511. osMutexRelease (sensorsInfoMutex);
  512. }
  513. PositionControlTaskData posYData __attribute__ ((aligned (32))) = { 0 };
  514. if (positionYControlTaskInitArg.positionSettingQueue != NULL)
  515. {
  516. ReadFloatFromBuffer(spFrameData->dataBuffer, &inputDataBufferPos, &posYData.positionSettingValue);
  517. osMessageQueuePut(positionYControlTaskInitArg.positionSettingQueue, &posYData, 0, 0);
  518. }
  519. break;
  520. default: respStatus = spUnknownCommand; break;
  521. }
  522. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData->frameHeader.frameId, spFrameData->frameHeader.frameCommand, respStatus, outputDataBuffer, outputDataBufferPos);
  523. if (dataToSend > 0) {
  524. HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, dataToSend);
  525. }
  526. #ifdef SERIAL_PROTOCOL_DBG
  527. printf ("Uart%d: TX bytes sent: %d\n", uartTaskData->uartNumber, dataToSend);
  528. #endif
  529. }
  530. void UartTxTask (void* argument) {
  531. while (pdTRUE) {
  532. osDelay (pdMS_TO_TICKS (1000));
  533. }
  534. }