diff --git a/apps/m0_lowload/sdkconfig.default b/apps/m0_lowload/sdkconfig.default
index e02d4b4..b6d6fa0 100644
--- a/apps/m0_lowload/sdkconfig.default
+++ b/apps/m0_lowload/sdkconfig.default
@@ -1,3 +1,3 @@
CONFIG_COMPONENT_MAILBOX=y
CONFIG_COMPONENT_MAILBOX_IRQFWD_SDH=y
-CONFIG_COMPONENT_MAILBOX_IRQFWD_GPIO=y
\ No newline at end of file
+CONFIG_COMPONENT_MAILBOX_IRQFWD_GPIO=y
diff --git a/components/mailbox/CMakeLists.txt b/components/mailbox/CMakeLists.txt
index 353f0c7..bfca28b 100644
--- a/components/mailbox/CMakeLists.txt
+++ b/components/mailbox/CMakeLists.txt
@@ -1,4 +1,4 @@
sdk_generate_library(oblfr_mailbox)
sdk_add_include_directories(include)
-sdk_library_add_sources(${CMAKE_CURRENT_SOURCE_DIR}/src/oblfr_mailbox.c)
+sdk_library_add_sources(${CMAKE_CURRENT_SOURCE_DIR}/src/oblfr_mailbox.c ${CMAKE_CURRENT_SOURCE_DIR}/src/oblfr_usb_peripheral.c )
diff --git a/components/mailbox/Kconfig b/components/mailbox/Kconfig
index f1c41ce..c792d39 100644
--- a/components/mailbox/Kconfig
+++ b/components/mailbox/Kconfig
@@ -27,6 +27,14 @@ config COMPONENT_MAILBOX_IRQFWD_USB
help
Curently disabled as USB is not working
+config COMPONENT_MAILBOX_USB_HOST
+ depends on COMPONENT_MAILBOX_IRQFWD_USB
+ bool "Set USB to HOST mode"
+ default n
+ help
+ By default USB operates in peripheral mode. Set this to use the
+ USB adapter as a host device.
+
config COMPONENT_MAILBOX_IRQFWD_GPIO
bool "Enable IRQ forwarding for GPIO"
default y
diff --git a/components/mailbox/include/oblfr_usb_peripheral.h b/components/mailbox/include/oblfr_usb_peripheral.h
new file mode 100644
index 0000000..6218280
--- /dev/null
+++ b/components/mailbox/include/oblfr_usb_peripheral.h
@@ -0,0 +1,86 @@
+// Portions Copyright () 2023 OpenBuffalo
+/**
+ ******************************************************************************
+ * @file usb_v2_reg.h
+ * @version V1.0
+ * @date 2022-08-15
+ * @brief This file is the description of.IP register
+ ******************************************************************************
+ * @attention
+ *
+ *
© COPYRIGHT(c) 2020 Bouffalo Lab
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Bouffalo Lab nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+#ifndef __OBLFR_USBPHY_H__
+
+#include
+#include "oblfr_common.h"
+
+oblfr_err_t setup_usb_peripheral(void);
+
+// SUBSYSTEMS
+#define BLFB_USB_BASE ((uint32_t)0x20072000)
+#define BFLB_PDS_BASE ((uint32_t)0x2000e000)
+
+// PDS REGISTERS
+#define PDS_USB_CTL_OFFSET (0x500) /* usb_ctl */
+#define PDS_USB_PHY_CTRL_OFFSET (0x504) /* usb_phy_ctrl */
+
+// PDS REGISTER VALUES
+
+/* 0x500 : usb_ctl */
+#define PDS_REG_USB_SW_RST_N (1 << 0U)
+#define PDS_REG_USB_EXT_SUSP_N (1 << 1U)
+#define PDS_REG_USB_WAKEUP (1 << 2U)
+#define PDS_REG_USB_L1_WAKEUP (1 << 3U)
+#define PDS_REG_USB_DRVBUS_POL (1 << 4U)
+#define PDS_REG_USB_IDDIG (1 << 5U)
+
+/* 0x504 : usb_phy_ctrl */
+#define PDS_REG_USB_PHY_PONRST (1 << 0U)
+#define PDS_REG_USB_PHY_OSCOUTEN (1 << 1U)
+#define PDS_REG_USB_PHY_XTLSEL_SHIFT (2U)
+#define PDS_REG_USB_PHY_XTLSEL_MASK (0x3 << PDS_REG_USB_PHY_XTLSEL_SHIFT)
+#define PDS_REG_USB_PHY_XTLSEL_MASK (0x3 << PDS_REG_USB_PHY_XTLSEL_SHIFT)
+#define PDS_REG_USB_PHY_OUTCLKSEL (1 << 4U)
+#define PDS_REG_USB_PHY_PLLALIV (1 << 5U)
+#define PDS_REG_PU_USB20_PSW (1 << 6U)
+
+
+// USB REGS
+
+#define USB_GLB_INT_OFFSET (0xC4) /* GLB_INT */
+
+
+/* 0xC4 : GLB_INT */
+#define USB_MDEV_INT (1 << 0U)
+#define USB_MOTG_INT (1 << 1U)
+#define USB_MHC_INT (1 << 2U)
+#define USB_POLARITY (1 << 3U)
+
+
+#endif
diff --git a/components/mailbox/src/oblfr_mailbox.c b/components/mailbox/src/oblfr_mailbox.c
index ec758fb..a260681 100644
--- a/components/mailbox/src/oblfr_mailbox.c
+++ b/components/mailbox/src/oblfr_mailbox.c
@@ -14,6 +14,7 @@
#endif
#include "oblfr_mailbox.h"
+#include "oblfr_usb_peripheral.h"
#define DBG_TAG "MBOX"
#include "log.h"
@@ -415,6 +416,13 @@ oblfr_err_t oblfr_mailbox_init()
}
#endif
+#ifdef CONFIG_COMPONENT_MAILBOX_IRQFWD_USB
+ if (setup_usb_peripheral() != SUCCESS) {
+ LOG_E("Failed to setup USB peripheral\r\n");
+ return OBLFR_ERR_ERROR;
+ }
+#endif
+
return OBLFR_OK;
}
@@ -443,4 +451,3 @@ oblfr_err_t oblfr_mailbox_dump()
oblfr_mailbox_signal_unlock(false);
return OBLFR_OK;
}
-
diff --git a/components/mailbox/src/oblfr_usb_peripheral.c b/components/mailbox/src/oblfr_usb_peripheral.c
new file mode 100644
index 0000000..2dc719b
--- /dev/null
+++ b/components/mailbox/src/oblfr_usb_peripheral.c
@@ -0,0 +1,115 @@
+//
+// This is the bare bones code to power up the USB subsystem so that
+// it can be used once the system is booted.
+//
+// All code was taken from the bl_mcu_sdk with adaptations, primarily
+// from the file:
+//
+// drivers/lhal/src/bflb_usb_v2.c
+//
+
+// Portions Copyright (c) 2023 OpenBuffalo
+
+/**
+ * @brief
+ *
+ * Copyright (c) 2021 Bouffalolab team
+ *
+ * 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
+#include
+#include
+#include
+#include "oblfr_usb_peripheral.h"
+#define DBG_TAG "USB"
+#include "log.h"
+
+// Enable USB A mode instead of B
+#define USB_HOST
+
+oblfr_err_t setup_usb_peripheral(void)
+{
+ uint32_t regval;
+
+ /* USB_PHY_CTRL[3:2] reg_usb_phy_xtlsel=0 */
+ /* 2000e504 = 0x40; #100; USB_PHY_CTRL[6] reg_pu_usb20_psw=1 (VCC33A) */
+ /* 2000e504 = 0x41; #500; USB_PHY_CTRL[0] reg_usb_phy_ponrst=1 */
+ /* 2000e500 = 0x20; #100; USB_CTL[0] reg_usb_sw_rst_n=0 */
+ /* 2000e500 = 0x22; #500; USB_CTL[1] reg_usb_ext_susp_n=1 */
+ /* 2000e500 = 0x23; #100; USB_CTL[0] reg_usb_sw_rst_n=1 */
+ /* #1.2ms; wait UCLK */
+ /* wait(soc616_b0.usb_uclk); */
+
+
+ LOG_I("Initializing USB...\r\n");
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_PHY_CTRL_OFFSET);
+ regval &= ~PDS_REG_USB_PHY_XTLSEL_MASK;
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_PHY_CTRL_OFFSET);
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_PHY_CTRL_OFFSET);
+ regval |= PDS_REG_PU_USB20_PSW;
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_PHY_CTRL_OFFSET);
+
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_PHY_CTRL_OFFSET);
+ regval |= PDS_REG_USB_PHY_PONRST;
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_PHY_CTRL_OFFSET);
+
+ /* greater than 5T */
+ bflb_mtimer_delay_us(1);
+
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+ regval &= ~PDS_REG_USB_SW_RST_N;
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+
+ /* greater than 5T */
+ bflb_mtimer_delay_us(1);
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+ regval |= PDS_REG_USB_EXT_SUSP_N;
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+
+ /* wait UCLK 1.2ms */
+ bflb_mtimer_delay_ms(3);
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+ regval |= PDS_REG_USB_SW_RST_N;
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+
+ bflb_mtimer_delay_ms(2);
+
+ regval = getreg32(BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+
+#ifdef CONFIG_COMPONENT_MAILBOX_USB_HOST
+ LOG_I("USB Initialized in HOST mode\r\n");
+ regval &= ~PDS_REG_USB_IDDIG;
+#else
+ LOG_I("USB Initialized in DEVICE mode\r\n");
+ regval |= PDS_REG_USB_IDDIG;
+#endif
+
+ putreg32(regval, BFLB_PDS_BASE + PDS_USB_CTL_OFFSET);
+
+ bflb_mtimer_delay_ms(10);
+
+ return OBLFR_OK;
+}
+