Skip to content

Commit

Permalink
refactor(hal/usb): Add new USB PHY related HAL API
Browse files Browse the repository at this point in the history
This commit adds/updates the USB PHY related HAL APIs. The following changes
are made:

- Updated 'usb_wrap_hal.h' API
- Added 'usb_serial_jtag_hal.h' API
  • Loading branch information
Dazza0 committed May 13, 2024
1 parent 4f996fc commit d41515f
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 137 deletions.
4 changes: 4 additions & 0 deletions components/hal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ if(NOT BOOTLOADER_BUILD)
list(APPEND srcs "ds_hal.c")
endif()

if(CONFIG_SOC_USB_SERIAL_JTAG_SUPPORTED)
list(APPEND srcs "usb_serial_jtag_hal.c")
endif()

if(CONFIG_SOC_USB_OTG_SUPPORTED)
list(APPEND srcs
"usb_dwc_hal.c"
Expand Down
51 changes: 51 additions & 0 deletions components/hal/include/hal/usb_serial_jtag_hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "soc/soc_caps.h"
#if SOC_USB_SERIAL_JTAG_SUPPORTED
#include "hal/usb_serial_jtag_ll.h"
#endif
#include "hal/usb_serial_jtag_types.h"

#ifdef __cplusplus
extern "C" {
#endif

#if SOC_USB_SERIAL_JTAG_SUPPORTED

/**
* @brief HAL context type of USJ driver
*/
typedef struct {
usb_serial_jtag_dev_t *dev;
} usb_serial_jtag_hal_context_t;

/**
* @brief Initialize the USJ HAL driver
*
* @param hal USJ HAL context
*/
void usb_serial_jtag_hal_init(usb_serial_jtag_hal_context_t *hal);

/* ---------------------------- USB PHY Control ---------------------------- */

#if USB_SERIAL_JTAG_LL_EXT_PHY_SUPPORTED
/**
* @brief Configure whether USJ is routed to internal/external FSLS PHY
*
* @param hal USJ HAL context
* @param external True if external, False if internal
*/
void usb_serial_jtag_hal_phy_set_external(usb_serial_jtag_hal_context_t *hal, bool external);
#endif // USB_SERIAL_JTAG_LL_EXT_PHY_SUPPORTED

#endif // SOC_USB_SERIAL_JTAG_SUPPORTED

#ifdef __cplusplus
}
#endif
79 changes: 44 additions & 35 deletions components/hal/include/hal/usb_wrap_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,89 @@
#pragma once

#include <stdbool.h>
#include "usb_dwc_types.h"
#include "usb_phy_types.h"
#include "soc/soc_caps.h"
#if SOC_USB_OTG_SUPPORTED
#include "soc/usb_wrap_struct.h"
#include "hal/usb_wrap_ll.h"
#endif
#if SOC_USB_SERIAL_JTAG_SUPPORTED
#include "soc/usb_serial_jtag_struct.h"
#endif
#include "hal/usb_wrap_types.h"

#ifdef __cplusplus
extern "C" {
#endif

#if SOC_USB_OTG_SUPPORTED

/**
* Context that should be maintained by both the driver and the HAL
* @brief HAL context type of USB WRAP driver
*/
typedef struct {
usb_wrap_dev_t *wrap_dev; /**< Pointer to base address of USB Wrapper registers */
#if SOC_USB_SERIAL_JTAG_SUPPORTED
usb_serial_jtag_dev_t *jtag_dev; /**< Pointer to base address of USB Serial JTAG registers */
#endif
} usb_fsls_phy_hal_context_t;
usb_wrap_dev_t *dev;
} usb_wrap_hal_context_t;

/**
* @brief Init the USB PHY hal. This function should be called first before other hal layer function is called
* @brief Initialize the USB WRAP HAL driver
*
* @param hal Context of the HAL layer
* @param hal USB WRAP HAL context
*/
void usb_fsls_phy_hal_init(usb_fsls_phy_hal_context_t *hal);
void usb_wrap_hal_init(usb_wrap_hal_context_t *hal);

/* ---------------------------- USB PHY Control ---------------------------- */

#if USB_WRAP_LL_EXT_PHY_SUPPORTED
/**
* @brief Configure internal/external PHY for USB_OTG
* @brief Configure whether USB WRAP is routed to internal/external FSLS PHY
*
* @param hal Context of the HAL layer
* @param phy_target USB PHY target
* @param hal USB WRAP HAL context
* @param external True if external, False if internal
*/
void usb_fsls_phy_hal_otg_conf(usb_fsls_phy_hal_context_t *hal, usb_phy_target_t phy_target);
void usb_wrap_hal_phy_set_external(usb_wrap_hal_context_t *hal, bool external);
#endif // USB_WRAP_LL_EXT_PHY_SUPPORTED

#if SOC_USB_SERIAL_JTAG_SUPPORTED
/**
* @brief Configure internal/external PHY for USB_Serial_JTAG
* @brief Enables and sets override of pull up/down resistors
*
* @param hal Context of the HAL layer
* @param phy_target USB PHY target
* @param hal USB WRAP HAL context
* @param vals Override values
*/
void usb_fsls_phy_hal_jtag_conf(usb_fsls_phy_hal_context_t *hal, usb_phy_target_t phy_target);
#endif
static inline void usb_wrap_hal_phy_enable_pull_override(usb_wrap_hal_context_t *hal, const usb_wrap_pull_override_vals_t *vals)
{
usb_wrap_ll_phy_enable_pull_override(hal->dev, vals);
}

/**
* @brief Configure pullup/pulldown loads for the D+/D- as a host
* @brief Disables pull up/down resistor override
*
* @param hal Context of the HAL layer
* @param hal USB WRAP HAL context
*/
void usb_fsls_phy_hal_int_load_conf_host(usb_fsls_phy_hal_context_t *hal);
static inline void usb_wrap_hal_phy_disable_pull_override(usb_wrap_hal_context_t *hal)
{
usb_wrap_ll_phy_disable_pull_override(hal->dev);
}

/**
* @brief Configure pullup/pulldown loads for the D+/D- as a device
* @brief Enables/disables the USB FSLS PHY's test mode
*
* @param hal Context of the HAL layer
* @param speed USB speed
* @param hal USB WRAP HAL context
* @param enable Whether to enable test mode
*/
void usb_fsls_phy_hal_int_load_conf_dev(usb_fsls_phy_hal_context_t *hal, usb_phy_speed_t speed);
static inline void usb_wrap_hal_phy_enable_test_mode(usb_wrap_hal_context_t *hal, bool enable)
{
usb_wrap_ll_phy_enable_test_mode(hal->dev, enable);
}

/**
* @brief Enable/Disable test mode for internal PHY to mimic host-device disconnection
* @brief Set the USB FSLS PHY's signal test values
*
* @param hal Context of the HAL layer
* @param disconn Whether to disconnect
* @param hal USB WRAP HAL context
* @param vals Test values
*/
void usb_fsls_phy_hal_int_mimick_disconn(usb_fsls_phy_hal_context_t *hal, bool disconn);
static inline void usb_wrap_hal_phy_test_mode_set_signals(usb_wrap_hal_context_t *hal, const usb_wrap_test_mode_vals_t *vals)
{
usb_wrap_ll_phy_test_mode_set_signals(hal->dev, vals);
}

#endif // SOC_USB_OTG_SUPPORTED

#ifdef __cplusplus
}
Expand Down
28 changes: 28 additions & 0 deletions components/hal/usb_serial_jtag_hal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "hal/usb_serial_jtag_ll.h"
#include "hal/usb_serial_jtag_hal.h"

void usb_serial_jtag_hal_init(usb_serial_jtag_hal_context_t *hal)
{
hal->dev = &USB_SERIAL_JTAG;
#if !USB_SERIAL_JTAG_LL_EXT_PHY_SUPPORTED
usb_serial_jtag_ll_phy_set_defaults();
#endif
}

#if USB_SERIAL_JTAG_LL_EXT_PHY_SUPPORTED
void usb_serial_jtag_hal_phy_set_external(usb_serial_jtag_hal_context_t *hal, bool external)
{
if (external) {
usb_serial_jtag_ll_phy_enable_external(true);
} else {
usb_serial_jtag_ll_phy_enable_external(false);
usb_serial_jtag_ll_phy_enable_pad(true);
}
}
#endif // USB_SERIAL_JTAG_LL_EXT_PHY_SUPPORTED
94 changes: 9 additions & 85 deletions components/hal/usb_wrap_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,25 @@
*/

#include "soc/soc_caps.h"
#if SOC_USB_SERIAL_JTAG_SUPPORTED
#include "hal/usb_serial_jtag_ll.h"
#endif
#include "hal/usb_wrap_ll.h"
#include "hal/usb_wrap_hal.h"

void usb_fsls_phy_hal_init(usb_fsls_phy_hal_context_t *hal)
void usb_wrap_hal_init(usb_wrap_hal_context_t *hal)
{
hal->wrap_dev = &USB_WRAP;
#if SOC_USB_SERIAL_JTAG_SUPPORTED
hal->jtag_dev = &USB_SERIAL_JTAG;
#endif
hal->dev = &USB_WRAP;
#if !USB_WRAP_LL_EXT_PHY_SUPPORTED
usb_wrap_ll_phy_set_defaults(hal->wrap_dev);
usb_wrap_ll_phy_set_defaults(hal->dev);
#endif
}

#if USB_WRAP_LL_EXT_PHY_SUPPORTED
void usb_fsls_phy_hal_otg_conf(usb_fsls_phy_hal_context_t *hal, usb_phy_target_t phy_target)
void usb_wrap_hal_phy_set_external(usb_wrap_hal_context_t *hal, bool external)
{
if (phy_target == USB_PHY_TARGET_EXT) {
usb_wrap_ll_phy_enable_external(hal->wrap_dev, true);
} else if (phy_target == USB_PHY_TARGET_INT) {
usb_wrap_ll_phy_enable_external(hal->wrap_dev, false);
usb_wrap_ll_phy_enable_pad(hal->wrap_dev, true);
}
}
#endif // USB_WRAP_LL_EXT_PHY_SUPPORTED

#if SOC_USB_SERIAL_JTAG_SUPPORTED
void usb_fsls_phy_hal_jtag_conf(usb_fsls_phy_hal_context_t *hal, usb_phy_target_t phy_target)
{
if (phy_target == USB_PHY_TARGET_EXT) {
usb_serial_jtag_ll_phy_enable_external(true); // USJ uses external PHY
} else if (phy_target == USB_PHY_TARGET_INT) {
usb_serial_jtag_ll_phy_enable_external(false); // USJ uses internal PHY
usb_serial_jtag_ll_phy_enable_pad(true); // Enable USB PHY pads
}
}
#endif

void usb_fsls_phy_hal_int_load_conf_host(usb_fsls_phy_hal_context_t *hal)
{
// HOST - upstream: dp_pd = 1, dm_pd = 1
usb_wrap_pull_override_vals_t vals = {
.dp_pu = false,
.dm_pu = false,
.dp_pd = true,
.dm_pd = true,
};
usb_wrap_ll_phy_enable_pull_override(hal->wrap_dev, &vals);
}

void usb_fsls_phy_hal_int_load_conf_dev(usb_fsls_phy_hal_context_t *hal, usb_phy_speed_t speed)
{
// DEVICE - downstream
if (speed == USB_PHY_SPEED_LOW) {
// LS: dm_pu = 1
usb_wrap_pull_override_vals_t vals = {
.dp_pu = false,
.dm_pu = true,
.dp_pd = false,
.dm_pd = false,
};
usb_wrap_ll_phy_enable_pull_override(hal->wrap_dev, &vals);
} else {
// FS: dp_pu = 1
usb_wrap_pull_override_vals_t vals = {
.dp_pu = true,
.dm_pu = false,
.dm_pd = false,
.dp_pd = false,
};
usb_wrap_ll_phy_enable_pull_override(hal->wrap_dev, &vals);
}
}

void usb_fsls_phy_hal_int_mimick_disconn(usb_fsls_phy_hal_context_t *hal, bool disconn)
{
if (disconn) {
/*
We mimic a disconnect by enabling the internal PHY's test mode, then forcing the output_enable to HIGH. This will:
A HIGH output_enable will cause the received VP and VM to be zero, thus mimicking a disconnection.
*/
usb_wrap_test_mode_vals_t vals = {
.tx_enable_n = true,
.tx_dp = false,
.tx_dm = false,
.rx_dp = false,
.rx_dm = false,
.rx_rcv = false,
};
usb_wrap_ll_phy_test_mode_set_signals(hal->wrap_dev, &vals);
usb_wrap_ll_phy_enable_test_mode(hal->wrap_dev, true);
if (external) {
usb_wrap_ll_phy_enable_external(hal->dev, true);
} else {
usb_wrap_ll_phy_enable_test_mode(hal->wrap_dev, false);
usb_wrap_ll_phy_enable_external(hal->dev, false);
usb_wrap_ll_phy_enable_pad(hal->dev, true);
}
}
#endif // USB_WRAP_LL_EXT_PHY_SUPPORTED
Loading

0 comments on commit d41515f

Please sign in to comment.