Saura 3 months ago
parent
commit
3c9aea87af
3 changed files with 86 additions and 17 deletions
  1. 73 14
      Core/Src/main.c
  2. 11 1
      nucleo-stm32f302r8 Debug.launch
  3. 2 2
      nucleo-stm32f302r8.ioc

+ 73 - 14
Core/Src/main.c

@@ -73,12 +73,20 @@ static void MX_TIM1_Init(void);
 #include <string.h>
 
 extern UART_HandleTypeDef huart2;
+//
+//int _write(int file, char *ptr, int len) {
+//  HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
+//  return len;
+//}
 
-int _write(int file, char *ptr, int len) {
-  HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
-  return len;
+int __io_putchar(int ch)
+{
+ // Write character to ITM ch.0
+ ITM_SendChar(ch);
+ return(ch);
 }
 
+
 #define RX_DATA_SIZE 4096
 #define RC_DATA_SIZE 256
 
@@ -87,22 +95,73 @@ typedef struct Packet {
     uint8_t data[RC_DATA_SIZE];
 } Packet;
 
-static uint8_t rx_buffer[258];
-static uint8_t rx_buffer_idx;
+#define CHUNK_SIZE 256
+
+typedef enum {
+    STATE_WAITING = 0,
+    STATE_DOWNLOADING
+} State;
+
+static State state = STATE_WAITING;
+
+static uint32_t expected_file_size = 0;
+static uint32_t received_size = 0;
+
+static uint8_t rx_size_buf[2];   // буфер для длины
+static uint8_t rx_file_buf[256]; // буфер для данных
+
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
+{
+    if (huart->Instance != USART2) return;
+
+    switch (state)
+    {
+    case STATE_WAITING: {
+        expected_file_size = (uint16_t)rx_size_buf[0]
+                           | ((uint16_t)rx_size_buf[1] << 8);
+
+        printf("File size: %u bytes\n", expected_file_size);
+
+        received_size = 0;
+        state = STATE_DOWNLOADING;
+
+        // вычисляем первый блок
+        uint16_t chunk = (expected_file_size - received_size > CHUNK_SIZE)
+                       ? CHUNK_SIZE
+                       : (expected_file_size - received_size);
 
-static uint8_t tmp_buffer[258];
+        HAL_UART_Receive_DMA(&huart2, rx_file_buf, chunk);
+        break;
+    }
 
-void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) {
-    // memcpy(rx_buffer + (RC_DATA_SIZE * rc_idx), rc_buffer, RC_DATA_SIZE);
+    case STATE_DOWNLOADING: {
+        received_size += huart->RxXferSize;
 
-    // rc_idx += 1;
-    // if (rc_idx >= RX_DATA_SIZE) {
-    //    rc_idx = 0;
-    // }
+        printf("Received: %u / %u\n", received_size, expected_file_size);
 
-    HAL_UART_Receive_DMA(&huart2, rx_buffer, sizeof(rx_buffer));
+        if (received_size >= expected_file_size) {
+            state = STATE_WAITING;
+            expected_file_size = 0;
+            received_size = 0;
+            memset(rx_size_buf, 0, sizeof(rx_size_buf));
+            memset(rx_file_buf, 0, sizeof(rx_file_buf));
+
+            HAL_UART_Receive_DMA(&huart2, rx_size_buf, 2);
+
+            printf("Download finished!\n");
+        } else {
+            // вычисляем следующий блок
+            uint16_t remaining = expected_file_size - received_size;
+            uint16_t chunk = (remaining > CHUNK_SIZE) ? CHUNK_SIZE : remaining;
+
+            HAL_UART_Receive_DMA(&huart2, rx_file_buf, chunk);
+        }
+        break;
+    }
+    }
 }
 
+
 /* USER CODE END 0 */
 
 /**
@@ -139,7 +198,7 @@ int main(void)
   MX_TIM1_Init();
   /* USER CODE BEGIN 2 */
 
-  HAL_UART_Receive_DMA(&huart2, rx_buffer, sizeof(rx_buffer));
+  HAL_UART_Receive_DMA(&huart2, rx_size_buf, 2);
 
 //  at45db_init();
 //  uart_receive_loop();

+ 11 - 1
nucleo-stm32f302r8 Debug.launch

@@ -7,7 +7,7 @@
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.debug_auth_key_path" value=""/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.debug_auth_permission" value="debug_non_secure_L3"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.enable_live_expr" value="true"/>
-    <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.enable_swv" value="false"/>
+    <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.enable_swv" value="true"/>
     <intAttribute key="com.st.stm32cube.ide.mcu.debug.launch.formatVersion" value="2"/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.ip_address_local" value="localhost"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.limit_swo_clock.enabled" value="false"/>
@@ -20,6 +20,7 @@
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.startuptab.exception.unaligned" value="false"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.startuptab.haltonexception" value="true"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.swd_mode" value="true"/>
+    <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.swv_config_enabled" value="true"/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.swv_port" value="61235"/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.launch.swv_trace_hclk" value="64000000"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.launch.useRemoteTarget" value="true"/>
@@ -41,6 +42,15 @@
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlink.watchdog_config" value="none"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.debug.stlinkenable_rtos" value="false"/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.debug.stlinkrestart_configurations" value="{&quot;fVersion&quot;:1,&quot;fItems&quot;:[{&quot;fDisplayName&quot;:&quot;Reset&quot;,&quot;fIsSuppressible&quot;:false,&quot;fResetAttribute&quot;:&quot;Software system reset&quot;,&quot;fResetStrategies&quot;:[{&quot;fDisplayName&quot;:&quot;Software system reset&quot;,&quot;fLaunchAttribute&quot;:&quot;system_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset\r\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;Hardware reset&quot;,&quot;fLaunchAttribute&quot;:&quot;hardware_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset hardware\r\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;Core reset&quot;,&quot;fLaunchAttribute&quot;:&quot;core_reset&quot;,&quot;fGdbCommands&quot;:[&quot;monitor reset core\r\n&quot;],&quot;fCmdOptions&quot;:[&quot;-g&quot;]},{&quot;fDisplayName&quot;:&quot;None&quot;,&quot;fLaunchAttribute&quot;:&quot;no_reset&quot;,&quot;fGdbCommands&quot;:[],&quot;fCmdOptions&quot;:[&quot;-g&quot;]}],&quot;fGdbCommandGroup&quot;:{&quot;name&quot;:&quot;Additional commands&quot;,&quot;commands&quot;:[]},&quot;fStartApplication&quot;:true}]}"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.datatrace_0" value="Enabled=false:Address=0x0:Access=Read/Write:Size=Word:Function=Data Value"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.datatrace_1" value="Enabled=false:Address=0x0:Access=Read/Write:Size=Word:Function=Data Value"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.datatrace_2" value="Enabled=false:Address=0x0:Access=Read/Write:Size=Word:Function=Data Value"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.datatrace_3" value="Enabled=false:Address=0x0:Access=Read/Write:Size=Word:Function=Data Value"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.itmports" value="1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.itmports_priv" value="0:0:0:0"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.pc_sample" value="0:16384"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.timestamps" value="1:1"/>
+    <stringAttribute key="com.st.stm32cube.ide.mcu.debug.swv.trace_events" value="Cpi=0:Exc=0:Sleep=0:Lsu=0:Fold=0:Exetrc=0"/>
     <booleanAttribute key="com.st.stm32cube.ide.mcu.rtosproxy.enableRtosProxy" value="false"/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.rtosproxy.rtosProxyCustomProperties" value=""/>
     <stringAttribute key="com.st.stm32cube.ide.mcu.rtosproxy.rtosProxyDriver" value="threadx"/>

+ 2 - 2
nucleo-stm32f302r8.ioc

@@ -67,12 +67,12 @@ NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:true\:false\:false
 PA13.GPIOParameters=GPIO_Label
 PA13.GPIO_Label=TMS
 PA13.Locked=true
-PA13.Mode=Serial_Wire
+PA13.Mode=Trace_Asynchronous_SW
 PA13.Signal=SYS_JTMS-SWDIO
 PA14.GPIOParameters=GPIO_Label
 PA14.GPIO_Label=TCK
 PA14.Locked=true
-PA14.Mode=Serial_Wire
+PA14.Mode=Trace_Asynchronous_SW
 PA14.Signal=SYS_JTCK-SWCLK
 PA2.Mode=Asynchronous
 PA2.Signal=USART2_TX