uart_tasks.c 25 KB

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