Переглянути джерело

AT32F403A: Extend SRAM to 224kB

Keir Fraser 1 рік тому
батько
коміт
1a35995046
4 змінених файлів з 45 додано та 2 видалено
  1. 3 0
      inc/mcu/stm32/f1_regs.h
  2. 38 0
      src/mcu/at32f4/floppy.c
  3. 2 0
      src/mcu/at32f4/stm32.c
  4. 2 2
      src/mcu/stm32f1/fpec.c

+ 3 - 0
inc/mcu/stm32/f1_regs.h

@@ -29,6 +29,9 @@ struct flash {
     uint32_t wrpr;     /* 20: Write protection */
 };
 
+#define FLASH_UNLOCK_KEY1 0x45670123
+#define FLASH_UNLOCK_KEY2 0xcdef89ab
+
 #define FLASH_ACR_PRFTBS     (1u<< 5)
 #define FLASH_ACR_PRFTBE     (1u<< 4)
 #define FLASH_ACR_HLFCYA     (1u<< 3)

+ 38 - 0
src/mcu/at32f4/floppy.c

@@ -53,12 +53,50 @@ void IRQ_40(void) __attribute__((alias("IRQ_INDEX_changed"))); /* EXTI15_10 */
 
 static unsigned int U_BUF_SZ;
 
+static void fpec_extend_sram(bool_t extend)
+{
+    bool_t fap_disabled = !(flash->obr & 2);
+
+    /* Unlock option bytes. */
+    flash->optkeyr = FLASH_UNLOCK_KEY1;
+    flash->optkeyr = FLASH_UNLOCK_KEY2;
+    while (!(flash->cr & FLASH_CR_OPTWRE))
+        cpu_relax();
+
+    /* Erase option bytes. */
+    flash->cr |= FLASH_CR_OPTER;
+    flash->cr |= FLASH_CR_STRT;
+    while (flash->sr & FLASH_SR_BSY)
+        continue;
+    flash->cr &= ~FLASH_CR_OPTER;
+
+    /* Program FAP and EOPB0. */
+    flash->cr |= FLASH_CR_OPTPG;
+    if (fap_disabled) {
+        *(volatile uint16_t *)0x1ffff800 = 0xa5; /* Disable protection */
+        while (flash->sr & FLASH_SR_BSY)
+            continue;
+    }
+    if (extend) {
+        *(volatile uint16_t *)0x1ffff810 = 0xfe; /* Extend SRAM */
+        while (flash->sr & FLASH_SR_BSY)
+            continue;
+    }
+}
+
 static void floppy_mcu_init(void)
 {
     const struct pin_mapping *mpin;
     const struct pin_mapping *upin;
     unsigned int avail_kb;
 
+    /* Extend AT32F403A SRAM. */
+    if ((at32f4_series == AT32F403A) && (sram_kb != 224)) {
+        fpec_init();
+        fpec_extend_sram(TRUE);
+        system_reset();
+    }
+
     avail_kb = sram_kb - ((((unsigned long)_ebss - 0x20000000) + 1023) >> 10);
     for (U_BUF_SZ = 128; U_BUF_SZ > avail_kb; U_BUF_SZ >>= 1)
         continue;

+ 2 - 0
src/mcu/at32f4/stm32.c

@@ -165,6 +165,8 @@ static void identify_mcu(void)
             apb_mhz = 108;
         }
         sram_kb = 96;
+        if (*(uint8_t *)0x1ffff810 == 0xfe) /* EOPB0 */
+            sram_kb += 128;
         break;
     case AT32F415:
         sram_kb = 32;

+ 2 - 2
src/mcu/stm32f1/fpec.c

@@ -26,8 +26,8 @@ void fpec_init(void)
 
     /* Unlock the FPEC. */
     if (flash->cr & FLASH_CR_LOCK) {
-        flash->keyr = 0x45670123;
-        flash->keyr = 0xcdef89ab;
+        flash->keyr = FLASH_UNLOCK_KEY1;
+        flash->keyr = FLASH_UNLOCK_KEY2;
     }
 
     fpec_wait_and_clear();