Saura 3 luni în urmă
părinte
comite
c0f53c27c6

+ 1 - 0
Core/Inc/stm32f3xx_it.h

@@ -56,6 +56,7 @@ void DebugMon_Handler(void);
 void PendSV_Handler(void);
 void SysTick_Handler(void);
 void DMA1_Channel6_IRQHandler(void);
+void USART2_IRQHandler(void);
 /* USER CODE BEGIN EFP */
 
 /* USER CODE END EFP */

+ 0 - 4
Core/Src/at45db041.c

@@ -267,10 +267,6 @@ void at45db_write(uint16_t page, uint8_t *data, uint16_t size)
     FLASH_CS_ENABLE();
     HAL_SPI_Transmit(&hspi2, cmd, 4, HAL_MAX_DELAY);
     FLASH_CS_DISABLE();
-
-    // 3. Ждём готовности
-    while (!(at45db_get_status() & 0x80))
-        HAL_Delay(1);
 }
 
 

+ 45 - 171
Core/Src/main.c

@@ -1,20 +1,20 @@
 /* USER CODE BEGIN Header */
 /**
-  ******************************************************************************
-  * @file           : main.c
-  * @brief          : Main program body
-  ******************************************************************************
-  * @attention
-  *
-  * Copyright (c) 2025 STMicroelectronics.
-  * All rights reserved.
-  *
-  * This software is licensed under terms that can be found in the LICENSE file
-  * in the root directory of this software component.
-  * If no LICENSE file comes with this software, it is provided AS-IS.
-  *
-  ******************************************************************************
-  */
+ ******************************************************************************
+ * @file           : main.c
+ * @brief          : Main program body
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2025 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
 /* USER CODE END Header */
 /* Includes ------------------------------------------------------------------*/
 #include "main.h"
@@ -68,102 +68,41 @@ static void MX_TIM1_Init(void);
 /* USER CODE BEGIN 0 */
 
 #include "at45db041.h"
-
+#include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 
-typedef struct {
-    uint8_t counter;
-    uint8_t is_test;
-} TestData;
+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;
+  HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
+  return len;
 }
 
-// ---
-#define PAGE_SIZE 256
-#define RX_BUF_SIZE 1
+#define RX_DATA_SIZE 4096
+#define RC_DATA_SIZE 256
 
-uint8_t rx_buf[RX_BUF_SIZE];
-uint8_t file_data[256];
-uint32_t file_len = 0;
+typedef struct Packet {
+    uint8_t header[2];
+    uint8_t data[RC_DATA_SIZE];
+} Packet;
 
-void start_uart_rx(void)
-{
-    HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rx_buf, RX_BUF_SIZE);
-}
+static uint8_t rx_buffer[258];
+static uint8_t rx_buffer_idx;
 
-/* Этот колб�?к вызывает�?�? при IDLE */
-void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
-{
-    if (huart->Instance == USART2)
-    {
-        // Копируем данные в общий буфер
-        memcpy(&file_data[file_len], rx_buf, Size);
-        file_len += Size;
+static uint8_t tmp_buffer[258];
 
-        printf("Received chunk: %u bytes (total: %lu)\r\n", Size, file_len);
+void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) {
+    // memcpy(rx_buffer + (RC_DATA_SIZE * rc_idx), rc_buffer, RC_DATA_SIZE);
 
-        start_uart_rx();
-    }
-}
+    // rc_idx += 1;
+    // if (rc_idx >= RX_DATA_SIZE) {
+    //    rc_idx = 0;
+    // }
 
-void uart_receive_audio(void)
-{
-    printf("=== UART Flash Loader Ready ===\r\n");
-    printf("Send your audio file now...\r\n");
-
-    start_uart_rx();
-
-    while (1)
-    {
-//        	buffer[total_bytes] = tmp_byte;
-//        	total_bytes += 1;
-//        	if (total_bytes >= 256) {
-//        		total_bytes = 0;
-//        		for (int i = 0; i < total_bytes; i += 1) {
-//        			printf("%u", buffer[i]);
-//        		}
-//        	}
-
-
-
-//        if (HAL_UART_Receive(&huart2, &byte, 1, 10) == HAL_OK)
-//        {
-//
-//        }
-//        else
-//        {
-//            // е�?ли 2 �?екунды тишины — значит передача закончила�?ь
-//            if (HAL_GetTick() - last_rx_time > 2000)
-//            {
-//                if (index > 0)
-//                {
-//                    // запи�?ываем неполную �?траницу
-////                    at45db_write(page, page_buffer, index);
-////                    at45db_wait_ready();
-//                    total_bytes += index;
-//                }
-//
-//                printf("Upload complete. Total %lu bytes written.\r\n", total_bytes);
-//
-//			  {
-//				printf("Data read:\r\n");
-//				for (int i = 0; i < page_buffer / 256; i++)
-//				{
-//					printf("%02X ", page_buffer[i]);
-//				}
-//				printf("\r\n");
-//			  }
-//
-//                break;
-//            }
-//        }
-    }
+    HAL_UART_Receive_DMA(&huart2, rx_buffer, sizeof(rx_buffer));
 }
 
-
 /* USER CODE END 0 */
 
 /**
@@ -200,82 +139,17 @@ int main(void)
   MX_TIM1_Init();
   /* USER CODE BEGIN 2 */
 
-  at45db_init();
-
-//  {
-//	uint8_t buffer[1024] = {0};
-//
-//	printf("Data read:\r\n");
-//	for (int i = 0; i < sizeof(buffer); i++)
-//	{
-//		at45db_read(i, buffer, sizeof(buffer));
-//		at45db_wait_ready();
-//		printf("%02X ", buffer[i]);
-//	}
-//	printf("\r\n");
-//  }
-
-  uart_receive_audio();
-
-//  // подождём, пока чип готов
-//  at45db_wait_ready();
-//
-//  // �?тираем �?траницу (необ�?зательно, е�?ли 0xFF)
-////  at45db_page_erase(0);
-//  at45db_wait_ready();
-//
-//  TestData test_data = {
-//		  .counter = 4,
-//		  .is_test = 5,
-//  };
-//
-//  at45db_write(0, (uint8_t*)&test_data, sizeof(test_data));
-//  at45db_wait_ready();
-//
-//  at45db_read(0, (uint8_t*)&test_data, sizeof(test_data));
-//  printf("Read back: %d, %d\r\n", test_data.counter, test_data.is_test);
-//  at45db_wait_ready();
-//
-//  // читаем первые 16 байт дл�? контрол�?
-//  uint8_t buffer[16] = {0};
-//  at45db_read(0, buffer, sizeof(buffer));
-//  printf("Data read:\r\n");
-//  for (int i = 0; i < sizeof(buffer); i++)
-//      printf("%02X ", buffer[i]);
-//  printf("\r\n");
+  HAL_UART_Receive_DMA(&huart2, rx_buffer, sizeof(rx_buffer));
 
+//  at45db_init();
+//  uart_receive_loop();
 
   /* USER CODE END 2 */
 
   /* Infinite loop */
   /* USER CODE BEGIN WHILE */
-  while (1)
-  {
-//	  printf("=== AT45DB041E Test ===\r\n");
-//
-//	  uint8_t is_ready = at45db_is_ready();
-//
-//	  if (is_ready)
-//	  {
-//		  printf("Device READY\r\n");
-//	  }
-//	  else
-//	  {
-//		printf("Device BUSY\r\n");
-//		continue;
-//	  }
-//
-//	  dev_id_t id = at45db_read_man_id();
-//	  if (id == DEV_ID_OK)
-//	      printf("Manufacturer ID OK\r\n");
-//	  else
-//	      printf("Manufacturer ID ERROR\r\n");
-//
-//	  uint8_t status = at45db_get_status();
-//	  printf("Status: 0x%02X\r\n", status);
-//	  printf("Page size: %u bytes\r\n", at45db_get_page_size());
-
-	  HAL_Delay(1);
+  while (1) {
+    HAL_Delay(10);
 
     /* USER CODE END WHILE */
 
@@ -550,8 +424,7 @@ void Error_Handler(void)
   /* USER CODE BEGIN Error_Handler_Debug */
   /* User can add his own implementation to report the HAL error return state */
   __disable_irq();
-  while (1)
-  {
+  while (1) {
   }
   /* USER CODE END Error_Handler_Debug */
 }
@@ -567,8 +440,9 @@ void Error_Handler(void)
 void assert_failed(uint8_t *file, uint32_t line)
 {
   /* USER CODE BEGIN 6 */
-  /* User can add his own implementation to report the file name and line number,
-     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
+  /* User can add his own implementation to report the file name and line
+     number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file,
+     line) */
   /* USER CODE END 6 */
 }
 #endif /* USE_FULL_ASSERT */

+ 7 - 1
Core/Src/stm32f3xx_hal_msp.c

@@ -257,7 +257,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
     hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
     hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
     hdma_usart2_rx.Init.Mode = DMA_NORMAL;
-    hdma_usart2_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
+    hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
     if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
     {
       Error_Handler();
@@ -265,6 +265,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
 
     __HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);
 
+    /* USART2 interrupt Init */
+    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
+    HAL_NVIC_EnableIRQ(USART2_IRQn);
   /* USER CODE BEGIN USART2_MspInit 1 */
 
   /* USER CODE END USART2_MspInit 1 */
@@ -296,6 +299,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
 
     /* USART2 DMA DeInit */
     HAL_DMA_DeInit(huart->hdmarx);
+
+    /* USART2 interrupt DeInit */
+    HAL_NVIC_DisableIRQ(USART2_IRQn);
   /* USER CODE BEGIN USART2_MspDeInit 1 */
 
   /* USER CODE END USART2_MspDeInit 1 */

+ 15 - 0
Core/Src/stm32f3xx_it.c

@@ -56,6 +56,7 @@
 
 /* External variables --------------------------------------------------------*/
 extern DMA_HandleTypeDef hdma_usart2_rx;
+extern UART_HandleTypeDef huart2;
 /* USER CODE BEGIN EV */
 
 /* USER CODE END EV */
@@ -212,6 +213,20 @@ void DMA1_Channel6_IRQHandler(void)
   /* USER CODE END DMA1_Channel6_IRQn 1 */
 }
 
+/**
+  * @brief This function handles USART2 global interrupt.
+  */
+void USART2_IRQHandler(void)
+{
+  /* USER CODE BEGIN USART2_IRQn 0 */
+
+  /* USER CODE END USART2_IRQn 0 */
+  HAL_UART_IRQHandler(&huart2);
+  /* USER CODE BEGIN USART2_IRQn 1 */
+
+  /* USER CODE END USART2_IRQn 1 */
+}
+
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */

+ 4 - 2
nucleo-stm32f302r8.ioc

@@ -11,7 +11,7 @@ Dma.USART2_RX.0.MemInc=DMA_MINC_ENABLE
 Dma.USART2_RX.0.Mode=DMA_NORMAL
 Dma.USART2_RX.0.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
 Dma.USART2_RX.0.PeriphInc=DMA_PINC_DISABLE
-Dma.USART2_RX.0.Priority=DMA_PRIORITY_VERY_HIGH
+Dma.USART2_RX.0.Priority=DMA_PRIORITY_LOW
 Dma.USART2_RX.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
 File.Version=6
 GPIO.groupedBy=Group By Peripherals
@@ -62,6 +62,7 @@ NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 NVIC.PriorityGroup=NVIC_PRIORITYGROUP_0
 NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
 NVIC.SysTick_IRQn=true\:0\:0\:true\:false\:true\:true\:true\:false
+NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
 NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:true\:false\:false
 PA13.GPIOParameters=GPIO_Label
 PA13.GPIO_Label=TMS
@@ -202,7 +203,8 @@ SPI2.VirtualType=VM_MASTER
 TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 TIM1.IPParameters=Channel-PWM Generation1 CH1
 USART2.BaudRate=115200
-USART2.IPParameters=VirtualMode-Asynchronous,BaudRate
+USART2.IPParameters=VirtualMode-Asynchronous,BaudRate,Mode
+USART2.Mode=MODE_TX_RX
 USART2.VirtualMode-Asynchronous=VM_ASYNC
 VP_SYS_VS_Systick.Mode=SysTick
 VP_SYS_VS_Systick.Signal=SYS_VS_Systick