Browse Source

AT32F4: Support both AT32F403 and AT32F415

Keir Fraser 3 years ago
parent
commit
4b82a229b1

+ 2 - 2
Makefile

@@ -65,8 +65,8 @@ dist:
 	$(PYTHON) ./scripts/mk_update.py cat $(PROJ)-$(VER)/$(PROJ)-$(VER).upd \
 		$(PROJ)-$(VER)/$(PROJ)-$(VER).upd $(PROJ)-$(VER).upd
 	$(MAKE) clean
-	$(MAKE) mcu=at32f415 all
-	cp -a $(PROJ)-$(VER).hex $(PROJ)-$(VER)/$(PROJ)-AT32F415-$(VER).hex
+	$(MAKE) mcu=at32f4 all
+	cp -a $(PROJ)-$(VER).hex $(PROJ)-$(VER)/$(PROJ)-AT32F4-$(VER).hex
 	$(PYTHON) ./scripts/mk_update.py cat $(PROJ)-$(VER)/$(PROJ)-$(VER).upd \
 		$(PROJ)-$(VER)/$(PROJ)-$(VER).upd $(PROJ)-$(VER).upd
 	$(MAKE) clean

+ 3 - 3
Rules.mk

@@ -30,9 +30,9 @@ stm32f1=y
 else ifeq ($(mcu),stm32f7)
 FLAGS += -mcpu=cortex-m7 -DSTM32F7=7 -DMCU=7
 stm32f7=y
-else ifeq ($(mcu),at32f415)
-FLAGS += -mcpu=cortex-m4 -DAT32F415=4 -DMCU=4
-at32f415=y
+else ifeq ($(mcu),at32f4)
+FLAGS += -mcpu=cortex-m4 -DAT32F4=4 -DMCU=4
+at32f4=y
 endif
 
 ifneq ($(debug),y)

+ 1 - 1
bootloader/Bootloader.ld.S

@@ -4,7 +4,7 @@
 #define FLASH_BASE 0x08000000
 #define FLASH_LEN  8K
 
-#elif MCU == STM32F7 || MCU == AT32F415
+#elif MCU == STM32F7 || MCU == AT32F4
 
 #define FLASH_BASE 0x08000000
 #define FLASH_LEN  16K

+ 1 - 1
bootloader/mcu/Makefile

@@ -1,3 +1,3 @@
 SUBDIRS-$(stm32f1) += stm32f1
 SUBDIRS-$(stm32f7) += stm32f7
-SUBDIRS-$(at32f415) += at32f415
+SUBDIRS-$(at32f4) += at32f4

+ 4 - 0
bootloader/mcu/at32f4/Makefile

@@ -0,0 +1,4 @@
+RPATH = $(ROOT)/src/mcu/at32f4
+
+OBJS += stm32.o
+OBJS += fpec.o

+ 0 - 4
bootloader/mcu/at32f415/Makefile

@@ -1,4 +0,0 @@
-RPATH = $(ROOT)/src/mcu/at32f415
-
-OBJS += stm32.o
-OBJS += fpec.o

+ 3 - 2
bootloader/usb/Makefile

@@ -10,7 +10,8 @@ OBJS-$(stm32f1) += hw_f1.o
 OBJS-$(stm32f7) += hw_dwc_otg.o
 OBJS-$(stm32f7) += hw_f7.o
 
-OBJS-$(at32f415) += hw_dwc_otg.o
-OBJS-$(at32f415) += hw_at32f415.o
+OBJS-$(at32f4) += hw_dwc_otg.o
+OBJS-$(at32f4) += hw_usbd.o
+OBJS-$(at32f4) += hw_at32f4.o
 
 $(OBJS) $(OBJS-y): CFLAGS += -include $(ROOT)/src/usb/defs.h

+ 3 - 3
inc/decls.h

@@ -24,9 +24,9 @@
 #elif MCU == STM32F7
 #include "mcu/stm32/f7_regs.h"
 #include "mcu/stm32/f7.h"
-#elif MCU == AT32F415
-#include "mcu/at32/f415_regs.h"
-#include "mcu/at32/f415.h"
+#elif MCU == AT32F4
+#include "mcu/at32/f4_regs.h"
+#include "mcu/at32/f4.h"
 #endif
 #include "intrinsics.h"
 

+ 12 - 0
inc/mcu/at32/f4.h

@@ -0,0 +1,12 @@
+
+#include "../stm32/f1.h"
+
+#undef FLASH_PAGE_SIZE
+extern unsigned int FLASH_PAGE_SIZE;
+
+#define AT32F403  0x02
+#define AT32F413  0x04
+#define AT32F415  0x05
+#define AT32F403A 0x07
+#define AT32F407  0x08
+extern unsigned int at32f4_series;

+ 0 - 5
inc/mcu/at32/f415.h

@@ -1,5 +0,0 @@
-
-#include "../stm32/f1.h"
-
-#undef FLASH_PAGE_SIZE
-extern unsigned int FLASH_PAGE_SIZE;

+ 0 - 0
inc/mcu/at32/f415_regs.h → inc/mcu/at32/f4_regs.h


+ 2 - 2
scripts/mk_update.py

@@ -32,11 +32,11 @@ from greaseweazle import version
 
 name_to_hw_model = { 'stm32f1': 1,
                      'stm32f7': 7,
-                     'at32f415': 4 }
+                     'at32f4': 4 }
 
 hw_model_to_name = { 1: 'STM32F103',
                      7: 'STM32F730',
-                     4: 'AT32F415' }
+                     4: 'AT32F4xx' }
 
 def mk_cat_entry(dat, hw_model, sig):
     max_kb = { 1: { b'BL':  8, b'GW': 56 },

+ 1 - 1
scripts/openocd/f1.cfg

@@ -1,5 +1,5 @@
 # Uncomment the following for APM32F103
-#set CPUTAPID 0x2ba01477
+set CPUTAPID 0x2ba01477
 
 source [find interface/stlink.cfg]
 

+ 2 - 2
scripts/stm32.ld.S

@@ -8,7 +8,7 @@ MEMORY
 #elif MCU == STM32F7
   RAM (rwx)       : ORIGIN = 0x20000000, LENGTH = 64K
   EXT_RAM (rwx)   : ORIGIN = 0x20010000, LENGTH = 192K
-#elif MCU == AT32F415
+#elif MCU == AT32F4
   RAM (rwx)       : ORIGIN = 0x20000000, LENGTH = 32K
 #endif
 }
@@ -28,7 +28,7 @@ SECTIONS
     _etext = .;
   } >FLASH
 
-#if MCU == STM32F1 || MCU == AT32F415
+#if MCU == STM32F1 || MCU == AT32F4
   .flags : {
     _reset_flag = .;
     . = . + 4;

+ 1 - 1
src/Greaseweazle.ld.S

@@ -4,7 +4,7 @@
 #define FLASH_BASE 0x08002000
 #define FLASH_LEN  56K
 
-#elif MCU == STM32F7 || MCU == AT32F415
+#elif MCU == STM32F7 || MCU == AT32F4
 
 #define FLASH_BASE 0x08004000
 #define FLASH_LEN  48K

+ 1 - 1
src/Makefile

@@ -11,7 +11,7 @@ OBJS += util.o
 OBJS += floppy.o
 
 OBJS-$(stm32f7)  += testmode.o
-OBJS-$(at32f415) += testmode.o
+OBJS-$(at32f4) += testmode.o
 
 OBJS-$(debug) += console.o
 

+ 2 - 2
src/board.c

@@ -77,8 +77,8 @@ static void gpio_pull_up_pins(GPIO gpio, uint16_t mask)
 #include "mcu/stm32f1/board.c"
 #elif MCU == STM32F7
 #include "mcu/stm32f7/board.c"
-#elif MCU == AT32F415
-#include "mcu/at32f415/board.c"
+#elif MCU == AT32F4
+#include "mcu/at32f4/board.c"
 #endif
 
 void board_init(void)

+ 4 - 4
src/console.c

@@ -17,7 +17,7 @@
 #define PCLK SYSCLK
 #elif MCU == STM32F7
 #define PCLK (APB2_MHZ * 1000000)
-#elif MCU == AT32F415
+#elif MCU == AT32F4
 #define PCLK SYSCLK
 #endif
 
@@ -25,7 +25,7 @@
 
 static void ser_putc(uint8_t c)
 {
-#if MCU == STM32F1 || MCU == AT32F415
+#if MCU == STM32F1 || MCU == AT32F4
     while (!(usart1->sr & USART_SR_TXE))
         cpu_relax();
     usart1->dr = c;
@@ -85,7 +85,7 @@ void console_init(void)
     peripheral_clock_delay();
 
     /* Enable TX pin (PA9) for USART output, RX pin (PA10) as input. */
-#if MCU == STM32F1 || MCU == AT32F415
+#if MCU == STM32F1 || MCU == AT32F4
     gpio_configure_pin(gpioa, 9, AFO_pushpull(_10MHz));
     gpio_configure_pin(gpioa, 10, GPI_pull_up);
 #elif MCU == STM32F7
@@ -104,7 +104,7 @@ void console_init(void)
  * any serial input to cause a crash dump of the stuck context. */
 void console_crash_on_input(void)
 {
-#if MCU == STM32F1 || MCU == AT32F415
+#if MCU == STM32F1 || MCU == AT32F4
     (void)usart1->dr; /* clear UART_SR_RXNE */
 #elif MCU == STM32F7
     usart1->rqr = USART_RQR_RXFRQ; /* clear ISR_RXNE */

+ 2 - 2
src/floppy.c

@@ -49,8 +49,8 @@ static const struct gw_delay factory_delay_params = {
 #include "mcu/stm32f1/floppy.c"
 #elif MCU == STM32F7
 #include "mcu/stm32f7/floppy.c"
-#elif MCU == AT32F415
-#include "mcu/at32f415/floppy.c"
+#elif MCU == AT32F4
+#include "mcu/at32f4/floppy.c"
 #endif
 
 static struct index {

+ 1 - 1
src/fw_update.c

@@ -13,7 +13,7 @@
 /*  8kB-64kB (56kB total) */
 #define FIRMWARE_START 0x08002000
 #define FIRMWARE_END   0x08010000
-#elif MCU == STM32F7 || MCU == AT32F415
+#elif MCU == STM32F7 || MCU == AT32F4
 /* 16kB-64KB (48kB total) */
 #define FIRMWARE_START 0x08004000
 #define FIRMWARE_END   0x08010000

+ 1 - 1
src/mcu/Makefile

@@ -1,3 +1,3 @@
 SUBDIRS-$(stm32f1) += stm32f1
 SUBDIRS-$(stm32f7) += stm32f7
-SUBDIRS-$(at32f415) += at32f415
+SUBDIRS-$(at32f4) += at32f4

+ 0 - 0
src/mcu/at32f415/Makefile → src/mcu/at32f4/Makefile


+ 1 - 1
src/mcu/at32f415/board.c → src/mcu/at32f4/board.c

@@ -1,5 +1,5 @@
 /*
- * at32f415/board.c
+ * at32f4/board.c
  * 
  * Board-specific setup and management.
  * 

+ 2 - 2
src/mcu/at32f415/floppy.c → src/mcu/at32f4/floppy.c

@@ -1,7 +1,7 @@
 /*
- * at32f415/floppy.c
+ * at32f4/floppy.c
  * 
- * Floppy interface control: AT32F415CBT7
+ * Floppy interface control: AT32F4xx
  * 
  * Written & released by Keir Fraser <keir.xen@gmail.com>
  * 

+ 0 - 0
src/mcu/at32f415/fpec.c → src/mcu/at32f4/fpec.c


+ 7 - 3
src/mcu/at32f415/stm32.c → src/mcu/at32f4/stm32.c

@@ -10,11 +10,14 @@
  */
 
 unsigned int FLASH_PAGE_SIZE = 2048;
+unsigned int at32f4_series;
 
 static void clock_init(void)
 {
-    /* Flash controller: reads require 2 wait states at 72MHz. */
-    flash->acr = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY(2);
+    if (at32f4_series == AT32F415) {
+        /* Flash controller: reads require 2 wait states at 72MHz. */
+        flash->acr = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY(2);
+    }
 
     /* Start up the external oscillator. */
     rcc->cr |= RCC_CR_HSEON;
@@ -64,15 +67,16 @@ static void identify_mcu(void)
     unsigned int flash_kb = *(uint16_t *)0x1ffff7e0;
     if (flash_kb <= 128)
         FLASH_PAGE_SIZE = 1024;
+    at32f4_series = *(uint8_t *)0x1ffff7f3; /* UID[95:88] */
 }
 
 void stm32_init(void)
 {
+    identify_mcu();
     cortex_init();
     clock_init();
     peripheral_init();
     cpu_sync();
-    identify_mcu();
 }
 
 void gpio_configure_pin(GPIO gpio, unsigned int pin, unsigned int mode)

+ 0 - 0
src/mcu/at32f415/testmode.c → src/mcu/at32f4/testmode.c


+ 2 - 2
src/timer.c

@@ -21,7 +21,7 @@ void IRQ_50(void) __attribute__((alias("IRQ_timer")));
 #define tim tim5 /* 32-bit timer */
 #define tim_bits 32
 #define TIM_CR1_MCUBITS 0
-#elif MCU == AT32F415
+#elif MCU == AT32F4
 void IRQ_50(void) __attribute__((alias("IRQ_timer")));
 #define TIMER_IRQ 50
 #define tim tim5 /* 32-bit timer */
@@ -122,7 +122,7 @@ void timer_cancel(struct timer *timer)
 
 void timers_init(void)
 {
-#if MCU == STM32F7 || MCU == AT32F415
+#if MCU == STM32F7 || MCU == AT32F4
     rcc->apb1enr |= RCC_APB1ENR_TIM5EN;
     peripheral_clock_delay();
 #endif

+ 3 - 2
src/usb/Makefile

@@ -8,7 +8,8 @@ OBJS-$(stm32f1) += hw_f1.o
 OBJS-$(stm32f7) += hw_dwc_otg.o
 OBJS-$(stm32f7) += hw_f7.o
 
-OBJS-$(at32f415) += hw_dwc_otg.o
-OBJS-$(at32f415) += hw_at32f415.o
+OBJS-$(at32f4) += hw_dwc_otg.o
+OBJS-$(at32f4) += hw_usbd.o
+OBJS-$(at32f4) += hw_at32f4.o
 
 $(OBJS) $(OBJS-y): CFLAGS += -include defs.h

+ 111 - 0
src/usb/hw_at32f4.c

@@ -0,0 +1,111 @@
+/*
+ * hw_at32f4.c
+ * 
+ * AT32F4xx-specific handling for USB controller.
+ * 
+ * Written & released by Keir Fraser <keir.xen@gmail.com>
+ * 
+ * This is free and unencumbered software released into the public domain.
+ * See the file COPYING for more details, or visit <http://unlicense.org>.
+ */
+
+#include "hw_dwc_otg.h"
+
+static const struct usb_driver *drv;
+
+void hw_usb_init(void)
+{
+    switch (at32f4_series) {
+
+    case AT32F403:
+        drv = &usbd;
+        break;
+
+    case AT32F415:
+        drv = &dwc_otg;
+
+        conf_iface = IFACE_FS;
+
+        rcc->ahbenr |= RCC_AHBENR_OTGFSEN;
+
+        /* PHY selection must be followed by a core reset. */
+        core_reset();
+        /* Activate FS transceiver. */
+        otg->gccfg = OTG_GCCFG_PWRDWN | OTG_GCCFG_VBUSBSEN;
+        break;
+
+    }
+
+    drv->init();
+}
+
+void hw_usb_deinit(void)
+{
+    drv->deinit();
+
+    switch (at32f4_series) {
+    case AT32F415:
+        rcc->ahbenr &= ~RCC_AHBENR_OTGFSEN;
+        break;
+    }
+}
+
+bool_t hw_has_highspeed(void)
+{
+    return drv->has_highspeed();
+}
+
+bool_t usb_is_highspeed(void)
+{
+    return drv->is_highspeed();
+}
+
+int ep_rx_ready(uint8_t epnr)
+{
+    return drv->ep_rx_ready(epnr);
+}
+
+bool_t ep_tx_ready(uint8_t epnr)
+{
+    return drv->ep_tx_ready(epnr);
+}
+ 
+void usb_read(uint8_t epnr, void *buf, uint32_t len)
+{
+    drv->read(epnr, buf, len);
+}
+
+void usb_write(uint8_t epnr, const void *buf, uint32_t len)
+{
+    drv->write(epnr, buf, len);
+}
+ 
+void usb_stall(uint8_t epnr)
+{
+    drv->stall(epnr);
+}
+
+void usb_configure_ep(uint8_t epnr, uint8_t type, uint32_t size)
+{
+    drv->configure_ep(epnr, type, size);
+}
+
+void usb_setaddr(uint8_t addr)
+{
+    drv->setaddr(addr);
+}
+
+void usb_process(void)
+{
+    drv->process();
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "Linux"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */

+ 0 - 93
src/usb/hw_at32f415.c

@@ -1,93 +0,0 @@
-/*
- * hw_at32f415.c
- * 
- * AT32F415-specific handling for DWC-OTG USB 2.0 controller.
- * 
- * Written & released by Keir Fraser <keir.xen@gmail.com>
- * 
- * This is free and unencumbered software released into the public domain.
- * See the file COPYING for more details, or visit <http://unlicense.org>.
- */
-
-#include "hw_dwc_otg.h"
-
-void hw_usb_init(void)
-{
-    conf_iface = IFACE_FS;
-
-    rcc->ahbenr |= RCC_AHBENR_OTGFSEN;
-
-    /* PHY selection must be followed by a core reset. */
-    core_reset();
-    /* Activate FS transceiver. */
-    otg->gccfg = OTG_GCCFG_PWRDWN | OTG_GCCFG_VBUSBSEN;
-
-    dwc_otg.init();
-}
-
-void hw_usb_deinit(void)
-{
-    dwc_otg.deinit();
-
-    rcc->ahbenr &= ~RCC_AHBENR_OTGFSEN;
-}
-
-bool_t hw_has_highspeed(void)
-{
-    return dwc_otg.has_highspeed();
-}
-
-bool_t usb_is_highspeed(void)
-{
-    return dwc_otg.is_highspeed();
-}
-
-int ep_rx_ready(uint8_t epnr)
-{
-    return dwc_otg.ep_rx_ready(epnr);
-}
-
-bool_t ep_tx_ready(uint8_t epnr)
-{
-    return dwc_otg.ep_tx_ready(epnr);
-}
- 
-void usb_read(uint8_t epnr, void *buf, uint32_t len)
-{
-    dwc_otg.read(epnr, buf, len);
-}
-
-void usb_write(uint8_t epnr, const void *buf, uint32_t len)
-{
-    dwc_otg.write(epnr, buf, len);
-}
- 
-void usb_stall(uint8_t epnr)
-{
-    dwc_otg.stall(epnr);
-}
-
-void usb_configure_ep(uint8_t epnr, uint8_t type, uint32_t size)
-{
-    dwc_otg.configure_ep(epnr, type, size);
-}
-
-void usb_setaddr(uint8_t addr)
-{
-    dwc_otg.setaddr(addr);
-}
-
-void usb_process(void)
-{
-    dwc_otg.process();
-}
-
-/*
- * Local variables:
- * mode: C
- * c-file-style: "Linux"
- * c-basic-offset: 4
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- */

+ 1 - 1
src/usb/hw_dwc_otg.h

@@ -19,7 +19,7 @@
 #if MCU == STM32F7
 #define conf_port PORT_HS
 #define MAX_MPS USB_HS_MPS
-#elif MCU == AT32F415
+#elif MCU == AT32F4
 #define conf_port PORT_FS
 #define MAX_MPS USB_FS_MPS
 #endif