uart_tasks.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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.pvEncoderX, sizeof (float));
  288. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.pvEncoderY, sizeof (float));
  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.positionXWeak, sizeof (uint8_t));
  307. WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionYWeak, sizeof (uint8_t));
  308. osMutexRelease (sensorsInfoMutex);
  309. respStatus = spOK;
  310. } else {
  311. respStatus = spInternalError;
  312. }
  313. break;
  314. case spSetFanSpeed:
  315. osTimerStop (fanTimerHandle);
  316. int32_t fanTimerPeriod = 0;
  317. uint32_t pulse = 0;
  318. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, &pulse);
  319. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&fanTimerPeriod);
  320. fanTimerConfigOC.Pulse = pulse * 10;
  321. if (HAL_TIM_PWM_ConfigChannel (&htim1, &fanTimerConfigOC, TIM_CHANNEL_2) != HAL_OK) {
  322. Error_Handler ();
  323. }
  324. if (fanTimerPeriod > 0) {
  325. osTimerStart (fanTimerHandle, fanTimerPeriod * 1000);
  326. HAL_TIM_PWM_Start (&htim1, TIM_CHANNEL_2);
  327. } else if (fanTimerPeriod == 0) {
  328. osTimerStop (fanTimerHandle);
  329. HAL_TIM_PWM_Stop (&htim1, TIM_CHANNEL_2);
  330. } else if (fanTimerPeriod == -1) {
  331. osTimerStop (fanTimerHandle);
  332. HAL_TIM_PWM_Start (&htim1, TIM_CHANNEL_2);
  333. }
  334. respStatus = spOK;
  335. break;
  336. case spSetMotorXOn:
  337. int32_t motorXPWMPulse = 0;
  338. int32_t motorXTimerPeriod = 0;
  339. uint32_t motorXStatus = 0;
  340. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXPWMPulse);
  341. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXTimerPeriod);
  342. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  343. motorXStatus =
  344. MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, motorXPWMPulse, motorXTimerPeriod, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
  345. sensorsInfo.motorXStatus = motorXStatus;
  346. if (motorXStatus == 1) {
  347. sensorsInfo.motorXPeakCurrent = 0.0;
  348. }
  349. osMutexRelease (sensorsInfoMutex);
  350. respStatus = spOK;
  351. } else {
  352. respStatus = spInternalError;
  353. }
  354. break;
  355. case spSetMotorYOn:
  356. int32_t motorYPWMPulse = 0;
  357. int32_t motorYTimerPeriod = 0;
  358. uint32_t motorYStatus = 0;
  359. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYPWMPulse);
  360. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYTimerPeriod);
  361. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  362. motorYStatus =
  363. MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, motorYPWMPulse, motorYTimerPeriod, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
  364. sensorsInfo.motorYStatus = motorYStatus;
  365. if (motorYStatus == 1) {
  366. sensorsInfo.motorYPeakCurrent = 0.0;
  367. }
  368. osMutexRelease (sensorsInfoMutex);
  369. respStatus = spOK;
  370. } else {
  371. respStatus = spInternalError;
  372. }
  373. break;
  374. case spSetDiodeOn:
  375. osTimerStop (debugLedTimerHandle);
  376. int32_t dbgLedTimerPeriod = 0;
  377. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&dbgLedTimerPeriod);
  378. if (dbgLedTimerPeriod > 0) {
  379. osTimerStart (debugLedTimerHandle, dbgLedTimerPeriod * 1000);
  380. DbgLEDOn (DBG_LED1);
  381. } else if (dbgLedTimerPeriod == 0) {
  382. osTimerStop (debugLedTimerHandle);
  383. DbgLEDOff (DBG_LED1);
  384. } else if (dbgLedTimerPeriod == -1) {
  385. osTimerStop (debugLedTimerHandle);
  386. DbgLEDOn (DBG_LED1);
  387. }
  388. respStatus = spOK;
  389. break;
  390. case spSetmotorXMaxCurrent:
  391. float motorXMaxCurrent = 0;
  392. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXMaxCurrent);
  393. uint32_t dacDataCh1 = (uint32_t)(4095 * motorXMaxCurrent / (EXT_VREF_mV * 0.001));
  394. HAL_DAC_SetValue (&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dacDataCh1);
  395. HAL_DAC_Start (&hdac1, DAC_CHANNEL_1);
  396. respStatus = spOK;
  397. break;
  398. case spSetmotorYMaxCurrent:
  399. float motorYMaxCurrent = 0;
  400. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYMaxCurrent);
  401. uint32_t dacDataCh2 = (uint32_t)(4095 * motorYMaxCurrent / (EXT_VREF_mV * 0.001));
  402. HAL_DAC_SetValue (&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dacDataCh2);
  403. HAL_DAC_Start (&hdac1, DAC_CHANNEL_2);
  404. respStatus = spOK;
  405. break;
  406. case spClearPeakMeasurments:
  407. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  408. for (int i = 0; i < 3; i++) {
  409. resMeasurements.voltagePeak[i] = resMeasurements.voltageRMS[i];
  410. resMeasurements.currentPeak[i] = resMeasurements.currentRMS[i];
  411. }
  412. osMutexRelease (resMeasurementsMutex);
  413. respStatus = spOK;
  414. } else {
  415. respStatus = spInternalError;
  416. }
  417. break;
  418. case spSetEncoderXValue:
  419. float enocoderXValue = 0;
  420. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&enocoderXValue);
  421. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  422. sensorsInfo.pvEncoderX = enocoderXValue;
  423. sensorsInfo.currentXPosition = 100 * enocoderXValue / MAX_X_AXE_ANGLE;
  424. osMutexRelease (sensorsInfoMutex);
  425. respStatus = spOK;
  426. } else {
  427. respStatus = spInternalError;
  428. }
  429. break;
  430. case spSetEncoderYValue:
  431. float enocoderYValue = 0;
  432. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&enocoderYValue);
  433. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  434. sensorsInfo.pvEncoderY = enocoderYValue;
  435. sensorsInfo.currentYPosition = 100 * enocoderYValue / MAX_X_AXE_ANGLE;
  436. osMutexRelease (sensorsInfoMutex);
  437. respStatus = spOK;
  438. } else {
  439. respStatus = spInternalError;
  440. }
  441. break;
  442. case spSetVoltageMeasGains:
  443. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  444. for (uint8_t i = 0; i < 3; i++) {
  445. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&U_MeasCorrectionData[i].gain);
  446. }
  447. osMutexRelease (resMeasurementsMutex);
  448. respStatus = spOK;
  449. } else {
  450. respStatus = spInternalError;
  451. }
  452. break;
  453. case spSetVoltageMeasOffsets:
  454. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  455. for (uint8_t i = 0; i < 3; i++) {
  456. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&U_MeasCorrectionData[i].offset);
  457. }
  458. osMutexRelease (resMeasurementsMutex);
  459. respStatus = spOK;
  460. } else {
  461. respStatus = spInternalError;
  462. }
  463. break;
  464. case spSetCurrentMeasGains:
  465. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  466. for (uint8_t i = 0; i < 3; i++) {
  467. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&I_MeasCorrectionData[i].gain);
  468. }
  469. osMutexRelease (resMeasurementsMutex);
  470. respStatus = spOK;
  471. } else {
  472. respStatus = spInternalError;
  473. }
  474. break;
  475. case spSetCurrentMeasOffsets:
  476. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  477. for (uint8_t i = 0; i < 3; i++) {
  478. ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&I_MeasCorrectionData[i].offset);
  479. }
  480. osMutexRelease (resMeasurementsMutex);
  481. respStatus = spOK;
  482. } else {
  483. respStatus = spInternalError;
  484. }
  485. break;
  486. case spResetSystem:
  487. __disable_irq();
  488. NVIC_SystemReset();
  489. break;
  490. case spSetPositonX:
  491. PositionControlTaskData posXData __attribute__ ((aligned (32))) = { 0 };
  492. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  493. sensorsInfo.positionXWeak = 1;
  494. osMutexRelease (sensorsInfoMutex);
  495. }
  496. if (positionXControlTaskInitArg.positionSettingQueue != NULL)
  497. {
  498. ReadFloatFromBuffer(spFrameData->dataBuffer, &inputDataBufferPos, &posXData.positionSettingValue);
  499. osMessageQueuePut(positionXControlTaskInitArg.positionSettingQueue, &posXData, 0, 0);
  500. }
  501. break;
  502. case spSetPositonY:
  503. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  504. sensorsInfo.positionYWeak = 1;
  505. osMutexRelease (sensorsInfoMutex);
  506. }
  507. PositionControlTaskData posYData __attribute__ ((aligned (32))) = { 0 };
  508. if (positionYControlTaskInitArg.positionSettingQueue != NULL)
  509. {
  510. ReadFloatFromBuffer(spFrameData->dataBuffer, &inputDataBufferPos, &posYData.positionSettingValue);
  511. osMessageQueuePut(positionYControlTaskInitArg.positionSettingQueue, &posYData, 0, 0);
  512. }
  513. break;
  514. default: respStatus = spUnknownCommand; break;
  515. }
  516. dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData->frameHeader.frameId, spFrameData->frameHeader.frameCommand, respStatus, outputDataBuffer, outputDataBufferPos);
  517. if (dataToSend > 0) {
  518. HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, dataToSend);
  519. }
  520. #ifdef SERIAL_PROTOCOL_DBG
  521. printf ("Uart%d: TX bytes sent: %d\n", uartTaskData->uartNumber, dataToSend);
  522. #endif
  523. }
  524. void UartTxTask (void* argument) {
  525. while (pdTRUE) {
  526. osDelay (pdMS_TO_TICKS (1000));
  527. }
  528. }