From 19e8ec39cd99e7accc4cc2656e3cc6f7a9e360c3 Mon Sep 17 00:00:00 2001 From: acki Date: Thu, 11 Dec 2025 22:59:12 +0100 Subject: [PATCH] Convert To CMake to use CLion + New comm system to send / receive array of data and not one by one --- .mxproject | 5 + .settings/language.settings.xml | 4 +- Core/Inc/actio.h | 1 + Core/Src/actio.c | 5 + Core/Src/comm.c | 250 +++++++++++++++++++++++++++----- pcb_actio.ioc | 6 +- 6 files changed, 228 insertions(+), 43 deletions(-) diff --git a/.mxproject b/.mxproject index 962243b..5ad4850 100644 --- a/.mxproject +++ b/.mxproject @@ -35,3 +35,8 @@ SourcePath#1=..\USB_Device\Target SourcePath#2=..\Core\Src SourceFiles=; +[PreviousUsedCMakes] +SourceFiles=Core\Src\main.c;USB_Device\App\usb_device.c;USB_Device\Target\usbd_conf.c;USB_Device\App\usbd_desc.c;USB_Device\App\usbd_cdc_if.c;Core\Src\stm32g4xx_it.c;Core\Src\stm32g4xx_hal_msp.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pcd.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pcd_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_ll_usb.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_rcc.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_rcc_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_flash.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_flash_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_flash_ramfunc.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_gpio.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_exti.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_dma.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_dma_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pwr.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pwr_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_cortex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_i2c.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_i2c_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_tim.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_tim_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_uart.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_uart_ex.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_core.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_ctlreq.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_ioreq.c;Middlewares\ST\STM32_USB_Device_Library\Class\CDC\Src\usbd_cdc.c;Drivers\CMSIS\Device\ST\STM32G4xx\Source\Templates\system_stm32g4xx.c;Core\Src\system_stm32g4xx.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pcd.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pcd_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_ll_usb.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_rcc.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_rcc_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_flash.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_flash_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_flash_ramfunc.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_gpio.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_exti.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_dma.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_dma_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pwr.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_pwr_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_cortex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_i2c.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_i2c_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_tim.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_tim_ex.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_uart.c;Drivers\STM32G4xx_HAL_Driver\Src\stm32g4xx_hal_uart_ex.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_core.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_ctlreq.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_ioreq.c;Middlewares\ST\STM32_USB_Device_Library\Class\CDC\Src\usbd_cdc.c;Drivers\CMSIS\Device\ST\STM32G4xx\Source\Templates\system_stm32g4xx.c;Core\Src\system_stm32g4xx.c;;;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_core.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_ctlreq.c;Middlewares\ST\STM32_USB_Device_Library\Core\Src\usbd_ioreq.c;Middlewares\ST\STM32_USB_Device_Library\Class\CDC\Src\usbd_cdc.c; +HeaderPath=Drivers\STM32G4xx_HAL_Driver\Inc;Drivers\STM32G4xx_HAL_Driver\Inc\Legacy;Middlewares\ST\STM32_USB_Device_Library\Core\Inc;Middlewares\ST\STM32_USB_Device_Library\Class\CDC\Inc;Drivers\CMSIS\Device\ST\STM32G4xx\Include;Drivers\CMSIS\Include;USB_Device\App;USB_Device\Target;Core\Inc; +CDefines=USE_HAL_DRIVER;STM32G491xx;USE_HAL_DRIVER;USE_HAL_DRIVER; + diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 04a94d2..ba65c38 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/Core/Inc/actio.h b/Core/Inc/actio.h index f7c2df4..5317b76 100644 --- a/Core/Inc/actio.h +++ b/Core/Inc/actio.h @@ -38,6 +38,7 @@ void PCA9685_ContinuousServoStop(uint8_t Channel); void PCA9685_LEDOn(uint8_t Channel); void PCA9685_LEDOff(uint8_t Channel); uint16_t getServoPos(uint16_t servoNum); +uint16_t getRelayState(uint16_t relayNum); uint16_t getAscPos(); void moveServo(uint16_t servoNum, uint16_t posNum); void moveRelay(uint16_t relayNum, uint16_t state); diff --git a/Core/Src/actio.c b/Core/Src/actio.c index d16555c..0a436cb 100644 --- a/Core/Src/actio.c +++ b/Core/Src/actio.c @@ -211,6 +211,11 @@ uint16_t getServoPos(uint16_t servoNum){ return act.positionServo[servoNum]; } +uint16_t getRelayState(uint16_t relayNum) +{ + return HAL_GPIO_ReadPin(act.relayPorts[relayNum-1], act.relayPortsNumbers[relayNum-1]); +} + uint16_t getAscPos(){ return act.positionAscenseur; } diff --git a/Core/Src/comm.c b/Core/Src/comm.c index d2785c6..4e92664 100644 --- a/Core/Src/comm.c +++ b/Core/Src/comm.c @@ -26,7 +26,7 @@ static PendingEvent pendingEvents[MAX_PENDING_EVENTS]; // ==================== Fonctions utilitaires ==================== void send_event(int idx) { - char msg[64]; + char msg[128]; sprintf(msg, "EVENT;%s;%s\n", pendingEvents[idx].evt, pendingEvents[idx].info); CDC_Transmit_FS((uint8_t*)msg, strlen(msg)); } @@ -35,7 +35,9 @@ void queue_event(const char *evt, const char *info) { for (int i = 0; i < MAX_PENDING_EVENTS; ++i) { if (!pendingEvents[i].active) { strncpy(pendingEvents[i].evt, evt, sizeof(pendingEvents[i].evt) - 1); + pendingEvents[i].evt[sizeof(pendingEvents[i].evt)-1] = '\0'; strncpy(pendingEvents[i].info, info, sizeof(pendingEvents[i].info) - 1); + pendingEvents[i].info[sizeof(pendingEvents[i].info)-1] = '\0'; pendingEvents[i].lastSentTimestamp = HAL_GetTick(); pendingEvents[i].retryCount = 0; pendingEvents[i].active = 1; @@ -59,7 +61,7 @@ void check_event_timeouts(void) { // ==================== Traitement du protocole ==================== void USBProtocol_ProcessCommand(char *cmd) { - char response[128]; + char response[256]; char *type = strtok(cmd, ";"); char *arg1 = strtok(NULL, ";"); char *arg2 = strtok(NULL, ";"); @@ -68,51 +70,223 @@ void USBProtocol_ProcessCommand(char *cmd) { if (!type || !arg1 || !arg2) return; if (strcmp(type, "GET") == 0) { - if (strncmp(arg1, "SERVO", 5) == 0 && strcmp(arg2, "POS") == 0) { - int n = atoi(arg1 + 5); - int pos = getServoPos(n); - sprintf(response, "SET;%s;%s;%d\n", arg1, arg2, pos); + if (strcmp(arg1, "SERVO") == 0 && strcmp(arg2, "POS") == 0) { + if (!arg3) { + snprintf(response, sizeof(response), "KO;SET;%s;%s\n", arg1, arg2); + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + return; + } + int n = atoi(arg3); + if (n <= 0) { + snprintf(response, sizeof(response), "SET;%s;%s;0\n", arg1, arg2); + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + return; + } + + size_t off = 0; + int success = 1; + off += snprintf(response + off, sizeof(response) - off, "SET;%s;%s;%d", arg1, arg2, n); + + for (int i = 0; i < n; ++i) { + char *servoIdStr = strtok(NULL, ";"); + if (!servoIdStr) { + success = 0; + break; + } + int servoId = atoi(servoIdStr); + int pos = getServoPos(servoId); + + /* append \";id;pos\" for each servo, check remaining buffer */ + int wrote = snprintf(response + off, sizeof(response) - off, ";%d;%d", servoId, pos); + if (wrote < 0 || (size_t)wrote >= sizeof(response) - off) { + success = 0; + break; + } + off += wrote; + } + + if (!success) { + snprintf(response, sizeof(response), "KO;SET;%s;%s\n", arg1, arg2); + } else { + /* terminate with newline */ + if (off < sizeof(response) - 1) { + response[off++] = '\n'; + response[off] = '\0'; + } else { + /* fallback if no room for newline */ + response[sizeof(response) - 1] = '\0'; + } + } + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + } else if (strcmp(arg1, "RELAY") == 0 && strcmp(arg2, "STATE") == 0) { + if (!arg3) { + snprintf(response, sizeof(response), "KO;SET;%s;%s\n", arg1, arg2); + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + return; + } + int n = atoi(arg3); + if (n <= 0) { + snprintf(response, sizeof(response), "SET;%s;%s;0\n", arg1, arg2); + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + return; + } + + size_t off = 0; + int success = 1; + off += snprintf(response + off, sizeof(response) - off, "SET;%s;%s;%d", arg1, arg2, n); + + for (int i = 0; i < n; ++i) { + char *relayIdStr = strtok(NULL, ";"); + if (!relayIdStr) { + success = 0; + break; + } + int relayId = atoi(relayIdStr); + int state = getRelayState(relayId); + + int wrote = snprintf(response + off, sizeof(response) - off, ";%d;%d", relayId, state); + if (wrote < 0 || (size_t)wrote >= sizeof(response) - off) { + success = 0; + break; + } + off += wrote; + } + + if (!success) { + snprintf(response, sizeof(response), "KO;SET;%s;%s\n", arg1, arg2); + } else { + if (off < sizeof(response) - 1) { + response[off++] = '\n'; + response[off] = '\0'; + } else { + response[sizeof(response) - 1] = '\0'; + } + } CDC_Transmit_FS((uint8_t*)response, strlen(response)); } } else if (strcmp(type, "SET") == 0 && arg3) { - int val = atoi(arg3); - bool success = 1; + int success = 1; if (strcmp(arg1, "TIR") == 0 && strcmp(arg2, "ARM") == 0) { + int val = atoi(arg3); if(val){ - armTirette(); + armTirette(); } else { - disarmTirette(); + disarmTirette(); } - } else if (strncmp(arg1, "SERVO", 5) == 0 && strncmp(arg2, "POS", 3) == 0){ - int n = atoi(arg1 + 5); - int pos = atoi(arg2 + 3); - setServoPosValue(n, pos, val); - } - sprintf(response, "%s;%s;%s;%d\n", success ? "OK" : "KO", arg1, arg2, val); - CDC_Transmit_FS((uint8_t*)response, strlen(response)); - } else if (strcmp(type, "MOV") == 0) { - bool success = 1; - if (strncmp(arg1, "SERVO", 5) == 0) { - int n = atoi(arg1 + 5); - int pos = atoi(arg2 + 3); - if(n == 5){ - if(pos == 1){ - PCA9685_ContinuousServoRun(n); - } else { - PCA9685_ContinuousServoStop(n); - } - } else { - moveServo(n, pos); - } - } else if (strncmp(arg1, "RELAY", 5) == 0) { - int n = atoi(arg1 + 5); - int state = atoi(arg2); - moveRelay(n, state); - } - sprintf(response, "%s;%s;%s\n", success ? "OK" : "KO", arg1, arg2); - CDC_Transmit_FS((uint8_t*)response, strlen(response)); + sprintf(response, "%s;%s;%s;%d\n", success ? "OK" : "KO", arg1, arg2, val); + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + } else if (strcmp(arg1, "SERVO") == 0 && strcmp(arg2, "POS") == 0){ + int n = atoi(arg3); + size_t off = 0; + off += snprintf(response + off, sizeof(response) - off, "%s;%s;%s;%d", "OK", arg1, arg2, n); + for (int i = 0; i < n; ++i) { + char *servoIdStr = strtok(NULL, ";"); + char *posStr = strtok(NULL, ";"); + char *angleStr = strtok(NULL, ";"); + if (!servoIdStr || !posStr || !angleStr) { + success = 0; + break; + } + int servoId = atoi(servoIdStr); + int pos = atoi(posStr); + int angle = atoi(angleStr); + setServoPosValue(servoId, pos, angle); + + int wrote = snprintf(response + off, sizeof(response) - off, ";%d;%d", servoId, pos); + if (wrote < 0 || (size_t)wrote >= sizeof(response) - off) { + success = 0; + break; + } + off += wrote; + } + + if (!success) { + snprintf(response, sizeof(response), "KO;%s;%s\n", arg1, arg2); + } else { + if (off < sizeof(response) - 1) { + response[off++] = '\n'; + response[off] = '\0'; + } else { + response[sizeof(response) - 1] = '\0'; + } + } + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + } + } else if (strcmp(type, "MOV") == 0) { + int success = 1; + if (strcmp(arg1, "SERVO") == 0) { + int n = atoi(arg2); + size_t off = 0; + off += snprintf(response + off, sizeof(response) - off, "%s;%s;%s;%d", "OK", arg1, arg2, n); + + for (int i = 0; i < n; ++i) { + char *servoIdStr = strtok(NULL, ";"); + char *posStr = strtok(NULL, ";"); + if (!servoIdStr || !posStr) { + success = 0; + break; + } + int servoId = atoi(servoIdStr); + int pos = atoi(posStr); + moveServo(servoId, pos); + + int wrote = snprintf(response + off, sizeof(response) - off, ";%d;%d", servoId, pos); + if (wrote < 0 || (size_t)wrote >= sizeof(response) - off) { + success = 0; + break; + } + off += wrote; + } + + if (!success) { + snprintf(response, sizeof(response), "KO;MOV;%s;%s\n", arg1, arg2); + } else { + if (off < sizeof(response) - 1) { + response[off++] = '\n'; + response[off] = '\0'; + } else { + response[sizeof(response) - 1] = '\0'; + } + } + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + } else if (strcmp(arg1, "RELAY") == 0) { + int n = atoi(arg2); + size_t off = 0; + off += snprintf(response + off, sizeof(response) - off, "%s;%s;%s;%d", "OK", arg1, arg2, n); + + for (int i = 0; i < n; ++i) { + char *relayIdStr = strtok(NULL, ";"); + char *stateStr = strtok(NULL, ";"); + if (!relayIdStr || !stateStr) { + success = 0; + break; + } + int relayId = atoi(relayIdStr); + int state = atoi(stateStr); + moveRelay(relayId, state); + + int wrote = snprintf(response + off, sizeof(response) - off, ";%d;%d", relayId, state); + if (wrote < 0 || (size_t)wrote >= sizeof(response) - off) { + success = 0; + break; + } + off += wrote; + } + + if (!success) { + snprintf(response, sizeof(response), "KO;MOV;%s;%s\n", arg1, arg2); + } else { + if (off < sizeof(response) - 1) { + response[off++] = '\n'; + response[off] = '\0'; + } else { + response[sizeof(response) - 1] = '\0'; + } + } + CDC_Transmit_FS((uint8_t*)response, strlen(response)); + } } else if (strcmp(type, "ACK") == 0) { // Réception d'un ACK d'évènement for (int i = 0; i < MAX_PENDING_EVENTS; ++i) { diff --git a/pcb_actio.ioc b/pcb_actio.ioc index 8e78bd2..6a6fcd5 100644 --- a/pcb_actio.ioc +++ b/pcb_actio.ioc @@ -129,6 +129,7 @@ PF1-OSC_OUT.Signal=GPIO_Output PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false +ProjectManager.CompilerLinker=GCC ProjectManager.CompilerOptimize=6 ProjectManager.ComputerToolchain=false ProjectManager.CoupleFile=false @@ -152,11 +153,11 @@ ProjectManager.ProjectName=pcb_actio ProjectManager.ProjectStructure= ProjectManager.RegisterCallBack= ProjectManager.StackSize=0x400 -ProjectManager.TargetToolchain=STM32CubeIDE +ProjectManager.TargetToolchain=CMake ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= -ProjectManager.UnderRoot=true +ProjectManager.UnderRoot=false ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_I2C1_Init-I2C1-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_USB_Device_Init-USB_DEVICE-false-HAL-false,6-MX_TIM1_Init-TIM1-false-HAL-true RCC.AHBFreq_Value=16000000 RCC.APB1Freq_Value=16000000 @@ -222,4 +223,3 @@ VP_TIM1_VS_no_output1.Signal=TIM1_VS_no_output1 VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS.Mode=CDC_FS VP_USB_DEVICE_VS_USB_DEVICE_CDC_FS.Signal=USB_DEVICE_VS_USB_DEVICE_CDC_FS board=custom -isbadioc=false