From 06d046b9634568a7fd474fab49610a7070bd0e08 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Mon, 16 May 2016 14:59:05 +0900
Subject: Improve system routines API for STM32F103

---
 example-cdc/sample.c | 21 ++++++++++++++-------
 example-cdc/sys.c    | 46 +++-------------------------------------------
 2 files changed, 17 insertions(+), 50 deletions(-)

(limited to 'example-cdc')

diff --git a/example-cdc/sample.c b/example-cdc/sample.c
index ee6d23f..9b9a4a6 100644
--- a/example-cdc/sample.c
+++ b/example-cdc/sample.c
@@ -65,24 +65,31 @@ usb_intr (void *arg)
   chopstx_intr_t interrupt;
 
   (void)arg;
-  usb_lld_init (0x80);		/* Bus powered. */
-  chopstx_claim_irq (&interrupt, INTR_REQ_USB);
-
+#if defined(OLDER_SYS_H)
   /*
+   * Historically (before sys < 3.0), NVIC priority setting for USB
+   * interrupt was done in usb_lld_sys_init.  Thus this code.
+   *
    * When USB interrupt occurs between usb_lld_init (which assumes
    * ISR) and chopstx_claim_irq (which clears pending interrupt),
    * invocation of usb_interrupt_handler won't occur.
    *
-   * We can't call usb_lld_init after chopstx_claim_irq, as
-   * usb_lld_init does its own setting for NVIC.  Calling
-   * chopstx_claim_irq after usb_lld_init overrides that.
-   *
    * Calling usb_interrupt_handler is no harm even if there were no
    * interrupts, thus, we call it unconditionally here, just in case
    * if there is a request.
    *
+   * We can't call usb_lld_init after chopstx_claim_irq, as
+   * usb_lld_init does its own setting for NVIC.  Calling
+   * chopstx_claim_irq after usb_lld_init overrides that.
+   *
    */
+  usb_lld_init (0x80);		/* Bus powered. */
+  chopstx_claim_irq (&interrupt, INTR_REQ_USB);
   usb_interrupt_handler ();
+#else
+  chopstx_claim_irq (&interrupt, INTR_REQ_USB);
+  usb_lld_init (0x80);		/* Bus powered. */
+#endif
 
   while (1)
     {
diff --git a/example-cdc/sys.c b/example-cdc/sys.c
index 7ab8ce4..9ec3b06 100644
--- a/example-cdc/sys.c
+++ b/example-cdc/sys.c
@@ -1,7 +1,7 @@
 /*
  * sys.c - system routines for the initial page for STM32F103.
  *
- * Copyright (C) 2013, 2014, 2015 Flying Stone Technology
+ * Copyright (C) 2013, 2014, 2015, 2016  Flying Stone Technology
  * Author: NIIBE Yutaka <gniibe@fsij.org>
  *
  * Copying and distribution of this file, with or without modification,
@@ -19,39 +19,6 @@
 
 #include "clk_gpio_init-stm32.c"
 
-#define CORTEX_PRIORITY_BITS    4
-#define CORTEX_PRIORITY_MASK(n)  ((n) << (8 - CORTEX_PRIORITY_BITS))
-#define USB_LP_CAN1_RX0_IRQn	 20
-#define STM32_USB_IRQ_PRIORITY   11
-
-struct NVIC {
-  volatile uint32_t ISER[8];
-  volatile uint32_t unused1[24];
-  volatile uint32_t ICER[8];
-  volatile uint32_t unused2[24];
-  volatile uint32_t ISPR[8];
-  volatile uint32_t unused3[24];
-  volatile uint32_t ICPR[8];
-  volatile uint32_t unused4[24];
-  volatile uint32_t IABR[8];
-  volatile uint32_t unused5[56];
-  volatile uint32_t IPR[60];
-};
-
-static struct NVIC *const NVICBase = ((struct NVIC *const)0xE000E100);
-#define NVIC_ISER(n)	(NVICBase->ISER[n >> 5])
-#define NVIC_ICPR(n)	(NVICBase->ICPR[n >> 5])
-#define NVIC_IPR(n)	(NVICBase->IPR[n >> 2])
-
-static void
-nvic_enable_vector (uint32_t n, uint32_t prio)
-{
-  unsigned int sh = (n & 3) << 3;
-
-  NVIC_IPR (n) = (NVIC_IPR(n) & ~(0xFF << sh)) | (prio << sh);
-  NVIC_ICPR (n) = 1 << (n & 0x1F);
-  NVIC_ISER (n) = 1 << (n & 0x1F);
-}
 
 static void
 usb_cable_config (int enable)
@@ -118,13 +85,6 @@ usb_lld_sys_init (void)
 
   usb_cable_config (1);
   RCC->APB1ENR |= RCC_APB1ENR_USBEN;
-  nvic_enable_vector (USB_LP_CAN1_RX0_IRQn,
-		      CORTEX_PRIORITY_MASK (STM32_USB_IRQ_PRIORITY));
-  /*
-   * Note that we also have other IRQ(s):
-   * 	USB_HP_CAN1_TX_IRQn (for double-buffered or isochronous)
-   * 	USBWakeUp_IRQn (suspend/resume)
-   */
   RCC->APB1RSTR = RCC_APB1RSTR_USBRST;
   RCC->APB1RSTR = 0;
 }
@@ -423,8 +383,8 @@ handler vector[] __attribute__ ((section(".vectors"))) = {
 const uint8_t sys_version[8] __attribute__((section(".sys.version"))) = {
   3*2+2,	     /* bLength */
   0x03,		     /* bDescriptorType = USB_STRING_DESCRIPTOR_TYPE */
-  /* sys version: "2.1" */
-  '2', 0, '.', 0, '1', 0,
+  /* sys version: "3.0" */
+  '3', 0, '.', 0, '0', 0,
 };
 
 const uint32_t __attribute__((section(".sys.board_id")))
-- 
cgit v1.2.3