Saura 3 месяцев назад
Родитель
Сommit
8bde27402f
4 измененных файлов с 185 добавлено и 100 удалено
  1. 3 3
      Core/Inc/at45db041.h
  2. 40 69
      Core/Src/at45db041.c
  3. 141 27
      Core/Src/main.c
  4. 1 1
      nucleo-stm32f302r8.ioc

+ 3 - 3
Core/Inc/at45db041.h

@@ -110,11 +110,11 @@ void at45db_page_size_conf(uint16_t mode);
 uint16_t at45db_wake_up_from_deep_sleep(at45db_t info);
 void at45db_ultra_deep_sleep(at45db_t info);
 uint16_t at45db_wake_up_from_ultra_deep_sleep(at45db_t info);
-void at45db_read_data(uint32_t address, uint8_t *data, uint16_t size);
+void at45db_read(uint16_t page, uint8_t *data, uint16_t size);
 int at45db_is_ready(void);
 uint16_t at45db_fault_check(void);
-void at45db_program(uint32_t addr, uint8_t *buffer, uint16_t size);
+void at45db_write(uint16_t page, uint8_t *data, uint16_t size);
 void at45db_read_continuous(uint32_t addr, uint8_t *buffer, uint32_t size);
-int is_device_busy(void);
+void at45db_wait_ready(void);
 
 #endif /* AT45DB041_H_ */

+ 40 - 69
Core/Src/at45db041.c

@@ -65,10 +65,10 @@ void at45db_init()
 #endif
 
 #ifdef DATAFLASH_PAGE_SIZE
-    AT45DB->block_size = 2112;
-    AT45DB->blocks     = 256;
-    AT45DB->flash_mbit = 4;
-    AT45DB->page_size  = 264;
+    AT45DB.block_size = 2112;
+    AT45DB.blocks     = 256;
+    AT45DB.flash_mbit = 4;
+    AT45DB.page_size  = 264;
 #endif
 }
 
@@ -196,39 +196,24 @@ uint16_t at45db_wake_up_from_ultra_deep_sleep(at45db_t info)
     return 1;
 }
 
-/**
- * @brief This functions performs a contineus array read inside the main memory of the external flash.
- * It starts from the specified address and continues to the next address if the size is long enough in order to take
- * information from the next addresses.
- * @param address : This is the starting address of the main memory you want to start reading from.
- * @param data    : The buffer that will contain the return data from the main memory of the flash.
- * @param size    : The size of data that you want to read.
- * @retval None.
- */
-void at45db_read_data(uint32_t address, uint8_t *data, uint16_t size)
+void at45db_read(uint16_t page, uint8_t *data, uint16_t size)
 {
-    uint8_t address_bytes[5];
+    uint8_t cmd[5];
+    uint32_t addr = (uint32_t)page << 8;
 
-    /*Set the address bytes*/
-    address_bytes[0] = CONTINUOUS_ARRAY_READ_H_MODE2;
-    address_bytes[1] = ((address >> 16) & 0xFF);
-    address_bytes[2] = ((address >> 8) & 0xFF);
-    address_bytes[3] = (address & 0xFF);
-    address_bytes[4] = 0;
+    cmd[0] = 0x0B; // high-speed read
+    cmd[1] = (addr >> 16) & 0xFF;
+    cmd[2] = (addr >> 8) & 0xFF;
+    cmd[3] = addr & 0xFF;
+    cmd[4] = 0x00; // dummy
 
-    /*Select the device*/
     FLASH_CS_ENABLE();
-
-    /*Transmit the address bytes*/
-    HAL_SPI_Transmit(&hspi2, address_bytes, 4, HAL_MAX_DELAY);
-
-    /*Receive the data from the external flash memory*/
+    HAL_SPI_Transmit(&hspi2, cmd, 5, HAL_MAX_DELAY);
     HAL_SPI_Receive(&hspi2, data, size, HAL_MAX_DELAY);
-
-    /*Release the slave device*/
     FLASH_CS_DISABLE();
 }
 
+
 /**
  * @brief Enter flash into ultra deep sleep.
  * @param info : Structure information about the external flash operation.
@@ -260,54 +245,35 @@ void at45db_ultra_deep_sleep(at45db_t info)
 }
 
 
-/**
- * @brief This function performs a programming operation into the main memory of the external flash
- * using the internal buffer 1 with built in erase operation.
- * @param addr  : The starting address of the main memory.
- * @param buffer: The buffer that holds the data which will be stored inside the main memory.
- * @param size  : The size of the data.
- * @retval None.
- */
-void at45db_program(uint32_t addr, uint8_t *buffer, uint16_t size)
+void at45db_write(uint16_t page, uint8_t *data, uint16_t size)
 {
-    /*Load data into Buffer 1*/
-    uint8_t loadCommand[4];
-    loadCommand[0] = 0x84; // Buffer 1 Write opcode
-    loadCommand[1] = 0x00;			// Dummy byte
-    loadCommand[2] = 0x00;			// Dummy byte
-    loadCommand[3] = 0x00;			// Buffer starting address
-
-    /*Select the external flash memory*/
-    FLASH_CS_ENABLE();
+    uint8_t cmd[4];
+    uint32_t addr = (uint32_t)page << 8;
 
-    /*Send the proper command & data to the internal buffer 1*/
-    HAL_SPI_Transmit(&hspi2, loadCommand, 4, HAL_MAX_DELAY);
-    HAL_SPI_Receive(&hspi2, (uint8_t *)buffer, size, HAL_MAX_DELAY);
 
-    /*Deselect the flash memory*/
+    // 1. Запись во внутренний буфер
+    cmd[0] = 0x84; cmd[1] = 0; cmd[2] = 0; cmd[3] = 0;
+    FLASH_CS_ENABLE();
+    HAL_SPI_Transmit(&hspi2, cmd, 4, HAL_MAX_DELAY);
+    HAL_SPI_Transmit(&hspi2, data, size, HAL_MAX_DELAY);
     FLASH_CS_DISABLE();
 
-    /*Program Buffer 1 to Main Memory Page with Built-In Erase*/
-    uint8_t programCommand[4];
-    programCommand[0] = 0x83;	// Buffer 1 to Main Memory Page Program with Built-In Erase opcode
-    programCommand[1] = (addr >> 16) & 0xFF;	// Address byte 1
-    programCommand[2] = (addr >> 8) & 0xFF;		// Address byte 2
-    programCommand[3] = addr & 0xFF;			// Address byte 3
+    // 2. Перенос буфера в нужную страницу
+    cmd[0] = 0x83;
+    cmd[1] = (addr >> 16) & 0xFF;
+    cmd[2] = (addr >> 8) & 0xFF;
+    cmd[3] = addr & 0xFF;
 
-    /*Select the external flash memory*/
     FLASH_CS_ENABLE();
-
-    /*Program the main memory. Send contents of buffer 1 to main memory*/
-    HAL_SPI_Transmit(&hspi2, programCommand, 4, HAL_MAX_DELAY);
-
-    /*Deselect the flash memory*/
+    HAL_SPI_Transmit(&hspi2, cmd, 4, HAL_MAX_DELAY);
     FLASH_CS_DISABLE();
 
+    // 3. Ждём готовности
+    while (!(at45db_get_status() & 0x80))
+        HAL_Delay(1);
+}
 
-    int is_ready = at45db_is_ready();
 
-    while (!is_ready) {}
-}
 
 void at45db_read_continuous(uint32_t addr, uint8_t *buffer, uint32_t size)
 {
@@ -387,9 +353,7 @@ void at45db_chip_erase(void)
 
     /*Check for fault operation*/
     res = at45db_fault_check();
-
-	if (res == 1)
-	{
+	if (res == 1) {
         printf("There was a fault in erase operation...\n\r");
     }
 	else if (res == 0) {
@@ -495,3 +459,10 @@ void at45db_deep_sleep(at45db_t *info)
 //    SET_BIT(GPIO_SPIx->MODER, (1U<<(SPIx_GPIO_MISO_PIN*2)));
 
 }
+
+void at45db_wait_ready(void)
+{
+    while (!(at45db_get_status() & 0x80)) {
+        HAL_Delay(1);
+    }
+}

+ 141 - 27
Core/Src/main.c

@@ -69,11 +69,82 @@ static void MX_TIM1_Init(void);
 
 #include <stdio.h>
 
+typedef struct {
+    uint8_t counter;
+    uint8_t is_test;
+} TestData;
+
 int _write(int file, char *ptr, int len) {
     HAL_UART_Transmit(&huart2, (uint8_t*)ptr, len, HAL_MAX_DELAY);
     return len;
 }
 
+// ---
+#define PAGE_SIZE 256
+
+void uart_receive_audio(void)
+{
+    uint8_t page_buffer[2048];
+    uint16_t index = 0;
+    uint16_t page = 0;
+    uint32_t total_bytes = 0;
+
+    printf("=== UART Flash Loader Ready ===\r\n");
+    printf("Send your audio file now...\r\n");
+
+    uint8_t byte;
+    uint32_t last_rx_time = HAL_GetTick();
+
+    while (1)
+    {
+        if (HAL_UART_Receive(&huart2, &byte, 1, 10) == HAL_OK)
+        {
+            page_buffer[index++] = byte;
+            last_rx_time = HAL_GetTick();
+            total_bytes++;
+
+            // когда набрали 256 байт — записываем страницу
+            if (index >= 256)
+            {
+//                at45db_write(page, page_buffer, 256);
+//                at45db_wait_ready();
+                printf("Written page %u (%lu bytes)\r\n", page, total_bytes);
+
+                index = 0;
+                page++;
+            }
+        }
+        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;
+            }
+        }
+    }
+}
+
+
 /* USER CODE END 0 */
 
 /**
@@ -111,37 +182,80 @@ int main(void)
 
   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");
+
+
   /* 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(1000);
+//	  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);
 
     /* USER CODE END WHILE */
 
@@ -218,7 +332,7 @@ static void MX_SPI2_Init(void)
   hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
   hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
   hspi2.Init.NSS = SPI_NSS_SOFT;
-  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
+  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
   hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
   hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
@@ -331,7 +445,7 @@ static void MX_USART2_UART_Init(void)
 
   /* USER CODE END USART2_Init 1 */
   huart2.Instance = USART2;
-  huart2.Init.BaudRate = 9600;
+  huart2.Init.BaudRate = 115200;
   huart2.Init.WordLength = UART_WORDLENGTH_8B;
   huart2.Init.StopBits = UART_STOPBITS_1;
   huart2.Init.Parity = UART_PARITY_NONE;

+ 1 - 1
nucleo-stm32f302r8.ioc

@@ -188,7 +188,7 @@ SPI2.NSSPMode=SPI_NSS_PULSE_DISABLE
 SPI2.VirtualType=VM_MASTER
 TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 TIM1.IPParameters=Channel-PWM Generation1 CH1
-USART2.BaudRate=9600
+USART2.BaudRate=115200
 USART2.IPParameters=VirtualMode-Asynchronous,BaudRate
 USART2.VirtualMode-Asynchronous=VM_ASYNC
 VP_SYS_VS_Systick.Mode=SysTick