Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contributing STM32 driver files from Microsoft #300

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions common/stm32/ethernet/lan8742/nx_stm32_phy_driver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/

#include "nx_stm32_phy_driver.h"
#include "nx_stm32_eth_config.h"


/* LAN8742 IO functions */
static int32_t lan8742_io_init(void);
static int32_t lan8742_io_deinit (void);

static int32_t lan8742_io_write_reg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal);
static int32_t lan8742_io_read_reg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal);

static int32_t lan8742_io_get_tick(void);

/* LAN8742 IO context object */
static lan8742_IOCtx_t LAN8742_IOCtx = { lan8742_io_init,
lan8742_io_deinit,
lan8742_io_write_reg,
lan8742_io_read_reg,
lan8742_io_get_tick
};
/* LAN8742 main object */
static lan8742_Object_t LAN8742;

/**
* @brief Initialize the PHY interface
* @param none
* @retval ETH_PHY_STATUS_OK on success, ETH_PHY_STATUS_ERROR otherwise
*/

int32_t nx_eth_phy_init(void)
{
int32_t ret = ETH_PHY_STATUS_ERROR;
/* Set PHY IO functions */

LAN8742_RegisterBusIO(&LAN8742, &LAN8742_IOCtx);
/* Initialize the LAN8742 ETH PHY */

if (LAN8742_Init(&LAN8742) == LAN8742_STATUS_OK)
{
ret = ETH_PHY_STATUS_OK;
}

return ret;
}

/**
* @brief set the Phy link state.
* @param LinkState
* @retval the link status.
*/

int32_t nx_eth_phy_set_link_state(int32_t LinkState)
{
return (LAN8742_SetLinkState(&LAN8742, LinkState));
}

/**
* @brief get the Phy link state.
* @param none
* @retval the link status.
*/

int32_t nx_eth_phy_get_link_state(void)
{
int32_t linkstate = LAN8742_GetLinkState(&LAN8742);

return linkstate;
}

/**
* @brief get the driver object handle
* @param none
* @retval pointer to the LAN8742 main object
*/

nx_eth_phy_handle_t nx_eth_phy_get_handle(void)
{
return (nx_eth_phy_handle_t)&LAN8742;
}

/**
* @brief Initialize the PHY MDIO interface
* @param None
* @retval 0 if OK, -1 if ERROR
*/

int32_t lan8742_io_init(void)
{
/* We assume that MDIO GPIO configuration is already done
in the ETH_MspInit() else it should be done here
*/

/* Configure the MDIO Clock */
HAL_ETH_SetMDIOClockRange(&eth_handle);

return ETH_PHY_STATUS_OK;
}

/**
* @brief De-Initialize the MDIO interface
* @param None
* @retval 0 if OK, -1 if ERROR
*/
int32_t lan8742_io_deinit (void)
{
return ETH_PHY_STATUS_OK;
}

/**
* @brief Read a PHY register through the MDIO interface.
* @param DevAddr: PHY port address
* @param RegAddr: PHY register address
* @param pRegVal: pointer to hold the register value
* @retval 0 if OK -1 if Error
*/
int32_t lan8742_io_read_reg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal)
{
if(HAL_ETH_ReadPHYRegister(&eth_handle, DevAddr, RegAddr, pRegVal) != HAL_OK)
{
return ETH_PHY_STATUS_ERROR;
}

return ETH_PHY_STATUS_OK;
}

int32_t lan8742_io_write_reg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal)
{
if(HAL_ETH_WritePHYRegister(&eth_handle, DevAddr, RegAddr, RegVal) != HAL_OK)
{
return ETH_PHY_STATUS_ERROR;
}

return ETH_PHY_STATUS_OK;
}

/**
* @brief Get the time in millisecons used for internal PHY driver process.
* @retval Time value
*/
int32_t lan8742_io_get_tick(void)
{
return HAL_GetTick();
}
Loading
Loading