Explorar el Código

STM32F7: Debug console on USART1

Keir Fraser hace 5 años
padre
commit
484257ea2f
Se han modificado 6 ficheros con 65 adiciones y 11 borrados
  1. 3 0
      inc/stm32/f1.h
  2. 6 0
      inc/stm32/f7.h
  3. 6 0
      inc/stm32/f7_regs.h
  4. 26 1
      src/console.c
  5. 14 10
      src/main.c
  6. 10 0
      src/stm32f7.c

+ 3 - 0
inc/stm32/f1.h

@@ -56,6 +56,9 @@ static USB_BUF usb_buf = (uint32_t *)USB_BUF_BASE;
 #define SYSCLK_MHZ 72
 #define FLASH_PAGE_SIZE 1024
 
+/* No delay required after enabling a peripheral clock, before accessing it. */
+#define peripheral_clock_delay() ((void)0)
+
 /*
  * Local variables:
  * mode: C

+ 6 - 0
inc/stm32/f7.h

@@ -66,12 +66,18 @@ static USB_OTG usb_otg_fs = (struct usb_otg *)USB_OTG_FS_BASE;
 static USB_OTG usb_otg_hs = (struct usb_otg *)USB_OTG_HS_BASE;
 
 #define SYSCLK_MHZ 216
+#define AHB_MHZ (SYSCLK_MHZ / 1)  /* 216MHz */
+#define APB1_MHZ (SYSCLK_MHZ / 4) /* 54MHz */
+#define APB2_MHZ (SYSCLK_MHZ / 2) /* 108MHz */
+
 #define FLASH_PAGE_SIZE 16384
 
 /* Delay after enabling peripheral clock, before accessing peripheral 
  * (Ref STMicro RM0431, Section 5.2.12) */
 void peripheral_clock_delay(void);
 
+void gpio_set_af(GPIO gpio, unsigned int pin, unsigned int af);
+
 /*
  * Local variables:
  * mode: C

+ 6 - 0
inc/stm32/f7_regs.h

@@ -504,6 +504,12 @@ struct usart {
 #define USART_CR3_IREN       (1u<< 1)
 #define USART_CR3_EIE        (1u<< 0)
 
+#define USART_RQR_TXFRQ      (1u<< 4)
+#define USART_RQR_RXFRQ      (1u<< 3)
+#define USART_RQR_MMRQ       (1u<< 2)
+#define USART_RQR_SBKRQ      (1u<< 1)
+#define USART_RQR_ABRRQ      (1u<< 0)
+
 #define USART_ISR_TCBGT      (1u<<25)
 #define USART_ISR_TEACK      (1u<<21)
 #define USART_ISR_RWU        (1u<<19)

+ 26 - 1
src/console.c

@@ -13,13 +13,25 @@
 #define BAUD 3000000 /* 3Mbaud */
 #endif
 
+#if STM32F == 1
+#define PCLK SYSCLK
+#elif STM32F == 7
+#define PCLK (APB2_MHZ * 1000000)
+#endif
+
 #define USART1_IRQ 37
 
 static void ser_putc(uint8_t c)
 {
+#if STM32F == 1
     while (!(usart1->sr & USART_SR_TXE))
         cpu_relax();
     usart1->dr = c;
+#elif STM32F == 7
+    while (!(usart1->isr & USART_ISR_TXE))
+        cpu_relax();
+    usart1->tdr = c;
+#endif
 }
 
 int vprintk(const char *format, va_list ap)
@@ -68,13 +80,21 @@ void console_init(void)
 {
     /* Turn on the clocks. */
     rcc->apb2enr |= RCC_APB2ENR_USART1EN;
+    peripheral_clock_delay();
 
     /* Enable TX pin (PA9) for USART output, RX pin (PA10) as input. */
+#if STM32F == 1
     gpio_configure_pin(gpioa, 9, AFO_pushpull(_10MHz));
     gpio_configure_pin(gpioa, 10, GPI_pull_up);
+#elif STM32F == 7
+    gpio_set_af(gpioa, 9, 7);
+    gpio_set_af(gpioa, 10, 7);
+    gpio_configure_pin(gpioa, 9, AFO_pushpull(_10MHz));
+    gpio_configure_pin(gpioa, 10, AFO_pushpull(_10MHz));
+#endif
 
     /* BAUD, 8n1. */
-    usart1->brr = SYSCLK / BAUD;
+    usart1->brr = PCLK / BAUD;
     usart1->cr1 = (USART_CR1_UE | USART_CR1_TE | USART_CR1_RE);
 }
 
@@ -82,7 +102,12 @@ void console_init(void)
  * any serial input to cause a crash dump of the stuck context. */
 void console_crash_on_input(void)
 {
+#if STM32F == 1
     (void)usart1->dr; /* clear UART_SR_RXNE */
+#elif STM32F == 7
+    usart1->rqr = USART_RQR_RXFRQ; /* clear ISR_RXNE */
+    usart1->icr = USART_ICR_ORECF; /* clear ISR_ORE */
+#endif
     usart1->cr1 |= USART_CR1_RXNEIE;
     IRQx_set_prio(USART1_IRQ, RESET_IRQ_PRI);
     IRQx_enable(USART1_IRQ);

+ 14 - 10
src/main.c

@@ -34,21 +34,25 @@ int main(void)
     time_init();
     console_init();
     console_crash_on_input();
-
-    gpio_configure_pin(gpioa, 15, GPO_pushpull(_2MHz, HIGH));
-    for (;;) {
-        delay_ms(20);
-        gpio_write_pin(gpioa, 15, LOW);
-        delay_ms(20);
-        gpio_write_pin(gpioa, 15, HIGH);
-    }
-
-    board_init();
+//    board_init();
 
     printk("\n** Greaseweazle v%u.%u\n", fw_major, fw_minor);
     printk("** Keir Fraser <keir.xen@gmail.com>\n");
     printk("** https://github.com/keirf/Greaseweazle\n\n");
 
+
+    {
+        int i;
+        gpio_configure_pin(gpioa, 15, GPO_pushpull(_2MHz, HIGH));
+        for (i = 0;; i++) {
+            printk("Hello %d\n", i);
+            delay_ms(200);
+            gpio_write_pin(gpioa, 15, LOW);
+            delay_ms(200);
+            gpio_write_pin(gpioa, 15, HIGH);
+        }
+    }
+
     floppy_init();
     usb_init();
 

+ 10 - 0
src/stm32f7.c

@@ -107,6 +107,16 @@ void gpio_configure_pin(GPIO gpio, unsigned int pin, unsigned int mode)
     gpio->pupdr = (gpio->pupdr & ~(3<<(pin<<1))) | ((mode&3)<<(pin<<1));
 }
 
+void gpio_set_af(GPIO gpio, unsigned int pin, unsigned int af)
+{
+    if (pin < 8) {
+        gpio->afrl = (gpio->afrl & ~(15<<(pin<<2))) | (af<<(pin<<2));
+    } else {
+        pin -= 8;
+        gpio->afrh = (gpio->afrh & ~(15<<(pin<<2))) | (af<<(pin<<2));
+    }
+}
+
 /*
  * Local variables:
  * mode: C