You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2020/05/18 09:35:54 UTC

[mynewt-core] 02/07: tinyusb: Add hardware initialization for NRF5x

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 532734cc8181ff036f8938ff8d8e94998288bc02
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Thu Apr 30 14:01:56 2020 +0200

    tinyusb: Add hardware initialization for NRF5x
    
    This provides code for hardware initialization.
    Part of this initialization is present in tinyusb bsps which
    are not used by mynewt since it has own bsps.
---
 hw/usb/tinyusb/nrf5x/include/tusb_hw.h | 25 +++++++++
 hw/usb/tinyusb/nrf5x/pkg.yml           | 33 ++++++++++++
 hw/usb/tinyusb/nrf5x/src/nrf5x.c       | 98 ++++++++++++++++++++++++++++++++++
 3 files changed, 156 insertions(+)

diff --git a/hw/usb/tinyusb/nrf5x/include/tusb_hw.h b/hw/usb/tinyusb/nrf5x/include/tusb_hw.h
new file mode 100755
index 0000000..c3a280f
--- /dev/null
+++ b/hw/usb/tinyusb/nrf5x/include/tusb_hw.h
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __TUSB_HW_H__
+#define __TUSB_HW_H__
+
+#define CFG_TUSB_MCU OPT_MCU_NRF5X
+
+#endif
diff --git a/hw/usb/tinyusb/nrf5x/pkg.yml b/hw/usb/tinyusb/nrf5x/pkg.yml
new file mode 100644
index 0000000..a8c08af
--- /dev/null
+++ b/hw/usb/tinyusb/nrf5x/pkg.yml
@@ -0,0 +1,33 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: hw/usb/tinyusb/nrf5x
+pkg.description: Hardware initialization for Tinyusb
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - usb
+    - tinyusb
+
+pkg.apis:
+    - TINYUSB_HW_INIT
+
+pkg.deps:
+    - "@apache-mynewt-core/kernel/os"
+    - "@tinyusb/tinyusb"
diff --git a/hw/usb/tinyusb/nrf5x/src/nrf5x.c b/hw/usb/tinyusb/nrf5x/src/nrf5x.c
new file mode 100755
index 0000000..03d0545
--- /dev/null
+++ b/hw/usb/tinyusb/nrf5x/src/nrf5x.c
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <os/mynewt.h>
+#include <mcu/mcu.h>
+
+#include <device/usbd.h>
+#include <tusb.h>
+#include <nrfx/hal/nrf_power.h>
+
+/* same value as NRFX_POWER_USB_EVT_* in nrfx_power */
+enum {
+    USB_EVT_DETECTED = 0,
+    USB_EVT_REMOVED = 1,
+    USB_EVT_READY = 2
+};
+
+/**
+ * tinyusb function that handles power event (0: detected, 1: remove, 2: ready)
+ * We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
+ */
+extern void tusb_hal_nrf_power_event(uint32_t event);
+
+static void
+USBD_IRQHandler(void)
+{
+    tud_int_handler(0);
+}
+
+/* Power ISR to detect USB VBUS state */
+static void
+POWER_CLOCK_IRQHandler(void)
+{
+    uint32_t enabled = nrf_power_int_enable_get(NRF_POWER);
+
+    if ((0 != (enabled & NRF_POWER_INT_USBDETECTED_MASK)) &&
+        nrf_power_event_get_and_clear(NRF_POWER,NRF_POWER_EVENT_USBDETECTED)) {
+        tusb_hal_nrf_power_event(USB_EVT_DETECTED);
+    }
+
+    if ((0 != (enabled & NRF_POWER_INT_USBREMOVED_MASK)) &&
+        nrf_power_event_get_and_clear(NRF_POWER,NRF_POWER_EVENT_USBREMOVED)) {
+        tusb_hal_nrf_power_event(USB_EVT_REMOVED);
+    }
+
+    if ((0 != (enabled & NRF_POWER_INT_USBPWRRDY_MASK)) &&
+        nrf_power_event_get_and_clear(NRF_POWER, NRF_POWER_EVENT_USBPWRRDY)) {
+        tusb_hal_nrf_power_event(USB_EVT_READY);
+    }
+}
+
+void
+tinyusb_hardware_init(void)
+{
+    /* Setup USB IRQ */
+    NVIC_SetVector(USBD_IRQn, (uint32_t)USBD_IRQHandler);
+    NVIC_SetPriority(USBD_IRQn, 2);
+
+    /* Setup Power IRQ to detect USB VBUS state ( detected, ready, removed) */
+    NVIC_SetVector(POWER_CLOCK_IRQn, (uint32_t)POWER_CLOCK_IRQHandler);
+    NVIC_SetPriority(POWER_CLOCK_IRQn, 7);
+    nrf_power_int_enable(NRF_POWER,
+                         NRF_POWER_INT_USBDETECTED_MASK |
+                         NRF_POWER_INT_USBREMOVED_MASK  |
+                         NRF_POWER_INT_USBPWRRDY_MASK);
+
+    NVIC_EnableIRQ(POWER_CLOCK_IRQn);
+
+    /*
+     * USB power may already be ready at this time -> no event generated
+     * We need to invoke the handler based on the status initially
+     */
+    uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
+
+    if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk) {
+        tusb_hal_nrf_power_event(USB_EVT_DETECTED);
+    }
+
+    if (usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk) {
+        tusb_hal_nrf_power_event(USB_EVT_READY);
+    }
+}