Skip to content

Commit e8005f6

Browse files
author
Cruz Monrreal
authored
Merge pull request #7206 from mikaleppanen/k64f_async_powerup
K64f non-blocking powerup
2 parents 7b7dfc6 + b7627e8 commit e8005f6

File tree

5 files changed

+153
-107
lines changed

5 files changed

+153
-107
lines changed

features/netsocket/emac-drivers/TARGET_Freescale_EMAC/kinetis_emac.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ bool Kinetis_EMAC::low_level_init_successful()
186186
phy_speed_t phy_speed;
187187
phy_duplex_t phy_duplex;
188188
uint32_t phyAddr = 0;
189-
bool link = false;
190189
enet_config_t config;
191190

192191
// Allocate RX descriptors
@@ -231,16 +230,16 @@ bool Kinetis_EMAC::low_level_init_successful()
231230

232231
ENET_GetDefaultConfig(&config);
233232

234-
PHY_Init(ENET, 0, sysClock);
235-
PHY_GetLinkStatus(ENET, phyAddr, &link);
236-
if (link) {
237-
/* Get link information from PHY */
238-
PHY_GetLinkSpeedDuplex(ENET, phyAddr, &phy_speed, &phy_duplex);
239-
/* Change the MII speed and duplex for actual link status. */
240-
config.miiSpeed = (enet_mii_speed_t)phy_speed;
241-
config.miiDuplex = (enet_mii_duplex_t)phy_duplex;
242-
config.interrupt = kENET_RxFrameInterrupt | kENET_TxFrameInterrupt;
233+
if (PHY_Init(ENET, phyAddr, sysClock) != kStatus_Success) {
234+
return false;
243235
}
236+
237+
/* Get link information from PHY */
238+
PHY_GetLinkSpeedDuplex(ENET, phyAddr, &phy_speed, &phy_duplex);
239+
/* Change the MII speed and duplex for actual link status. */
240+
config.miiSpeed = (enet_mii_speed_t)phy_speed;
241+
config.miiDuplex = (enet_mii_duplex_t)phy_duplex;
242+
config.interrupt = kENET_RxFrameInterrupt | kENET_TxFrameInterrupt;
244243
config.rxMaxFrameLen = ENET_ETH_MAX_FLEN;
245244
config.macSpecialConfig = kENET_ControlFlowControlEnable;
246245
config.txAccelerConfig = 0;
@@ -262,7 +261,6 @@ bool Kinetis_EMAC::low_level_init_successful()
262261
return true;
263262
}
264263

265-
266264
/** \brief Allocates a emac_mem_buf_t and returns the data from the incoming packet.
267265
*
268266
* \param[in] idx index of packet to be read
@@ -413,8 +411,8 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
413411
buf = copy_buf;
414412
}
415413

416-
/* Check if a descriptor is available for the transfer. */
417-
if (xTXDCountSem.wait(0) == 0) {
414+
/* Check if a descriptor is available for the transfer (wait 10ms before dropping the buffer) */
415+
if (xTXDCountSem.wait(10) == 0) {
418416
memory_manager->free(buf);
419417
return false;
420418
}
@@ -452,41 +450,42 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
452450
*******************************************************************************/
453451

454452
#define STATE_UNKNOWN (-1)
455-
456-
int phy_link_status(void) {
457-
bool connection_status;
458-
uint32_t phyAddr = 0;
459-
460-
PHY_GetLinkStatus(ENET, phyAddr, &connection_status);
461-
return (int)connection_status;
462-
}
453+
#define STATE_LINK_DOWN (0)
454+
#define STATE_LINK_UP (1)
463455

464456
void Kinetis_EMAC::phy_task()
465457
{
466-
static PHY_STATE prev_state = {STATE_UNKNOWN, (phy_speed_t)STATE_UNKNOWN, (phy_duplex_t)STATE_UNKNOWN};
467-
468458
uint32_t phyAddr = 0;
469459

470460
// Get current status
471461
PHY_STATE crt_state;
472462
bool connection_status;
473463
PHY_GetLinkStatus(ENET, phyAddr, &connection_status);
474-
crt_state.connected = connection_status;
475-
// Get the actual PHY link speed
476-
PHY_GetLinkSpeedDuplex(ENET, phyAddr, &crt_state.speed, &crt_state.duplex);
464+
465+
if (connection_status) {
466+
crt_state.connected = STATE_LINK_UP;
467+
} else {
468+
crt_state.connected = STATE_LINK_DOWN;
469+
}
470+
471+
if (crt_state.connected == STATE_LINK_UP) {
472+
if (prev_state.connected != STATE_LINK_UP) {
473+
PHY_AutoNegotiation(ENET, phyAddr);
474+
}
475+
476+
PHY_GetLinkSpeedDuplex(ENET, phyAddr, &crt_state.speed, &crt_state.duplex);
477+
478+
if (prev_state.connected != STATE_LINK_UP || crt_state.speed != prev_state.speed) {
479+
/* Poke the registers*/
480+
ENET_SetMII(ENET, (enet_mii_speed_t)crt_state.speed, (enet_mii_duplex_t)crt_state.duplex);
481+
}
482+
}
477483

478484
// Compare with previous state
479485
if (crt_state.connected != prev_state.connected && emac_link_state_cb) {
480486
emac_link_state_cb(crt_state.connected);
481487
}
482488

483-
if (crt_state.speed != prev_state.speed) {
484-
uint32_t rcr = ENET->RCR;
485-
rcr &= ~ENET_RCR_RMII_10T_MASK;
486-
rcr |= ENET_RCR_RMII_10T(!crt_state.speed);
487-
ENET->RCR = rcr;
488-
}
489-
490489
prev_state = crt_state;
491490
}
492491

@@ -504,19 +503,20 @@ bool Kinetis_EMAC::power_up()
504503
rx_isr();
505504

506505
/* PHY monitoring task */
507-
prev_state.connected = STATE_UNKNOWN;
506+
prev_state.connected = STATE_LINK_DOWN;
508507
prev_state.speed = (phy_speed_t)STATE_UNKNOWN;
509508
prev_state.duplex = (phy_duplex_t)STATE_UNKNOWN;
510509

511-
phy_task_handle = mbed::mbed_event_queue()->call_every(PHY_TASK_PERIOD_MS, mbed::callback(this, &Kinetis_EMAC::phy_task));
510+
mbed::mbed_event_queue()->call(mbed::callback(this, &Kinetis_EMAC::phy_task));
512511

513512
/* Allow the PHY task to detect the initial link state and set up the proper flags */
514513
osDelay(10);
515514

515+
phy_task_handle = mbed::mbed_event_queue()->call_every(PHY_TASK_PERIOD_MS, mbed::callback(this, &Kinetis_EMAC::phy_task));
516+
516517
return true;
517518
}
518519

519-
520520
uint32_t Kinetis_EMAC::get_mtu_size() const
521521
{
522522
return KINETIS_ETH_MTU_SIZE;

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/fsl_phy.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ extern clock_ip_name_t s_enetClock[FSL_FEATURE_SOC_ENET_COUNT];
6464

6565
status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
6666
{
67-
uint32_t bssReg;
6867
uint32_t counter = PHY_TIMEOUT_COUNT;
6968
uint32_t idReg = 0;
7069
status_t result = kStatus_Success;
@@ -89,36 +88,42 @@ status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
8988
}
9089

9190
/* Reset PHY. */
92-
counter = PHY_TIMEOUT_COUNT;
9391
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
92+
93+
return result;
94+
}
95+
96+
status_t PHY_AutoNegotiation(ENET_Type *base, uint32_t phyAddr)
97+
{
98+
status_t result = kStatus_Success;
99+
uint32_t bssReg;
100+
uint32_t counter = PHY_TIMEOUT_COUNT;
101+
102+
/* Set the negotiation. */
103+
result = PHY_Write(base, phyAddr, PHY_AUTONEG_ADVERTISE_REG,
104+
(PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
105+
PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
94106
if (result == kStatus_Success)
95107
{
96-
/* Set the negotiation. */
97-
result = PHY_Write(base, phyAddr, PHY_AUTONEG_ADVERTISE_REG,
98-
(PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
99-
PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
108+
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG,
109+
(PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
100110
if (result == kStatus_Success)
101111
{
102-
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG,
103-
(PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
104-
if (result == kStatus_Success)
112+
/* Check auto negotiation complete. */
113+
while (counter --)
105114
{
106-
/* Check auto negotiation complete. */
107-
while (counter --)
115+
result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &bssReg);
116+
if ( result == kStatus_Success)
108117
{
109-
result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &bssReg);
110-
if ( result == kStatus_Success)
118+
if ((bssReg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0)
111119
{
112-
if ((bssReg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0)
113-
{
114-
break;
115-
}
120+
break;
116121
}
122+
}
117123

118-
if (!counter)
119-
{
120-
return kStatus_PHY_AutoNegotiateFail;
121-
}
124+
if (!counter)
125+
{
126+
return kStatus_PHY_AutoNegotiateFail;
122127
}
123128
}
124129
}

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/fsl_phy.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,19 @@ extern "C" {
136136
* @param srcClock_Hz The module clock frequency - system clock for MII management interface - SMI.
137137
* @retval kStatus_Success PHY initialize success
138138
* @retval kStatus_PHY_SMIVisitTimeout PHY SMI visit time out
139-
* @retval kStatus_PHY_AutoNegotiateFail PHY auto negotiate fail
140139
*/
141140
status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz);
142141

142+
/*!
143+
* @brief Initiates auto negotiation.
144+
*
145+
* @param base ENET peripheral base address.
146+
* @param phyAddr The PHY address.
147+
* @retval kStatus_Success PHY auto negotiation success
148+
* @retval kStatus_PHY_AutoNegotiateFail PHY auto negotiate fail
149+
*/
150+
status_t PHY_AutoNegotiation(ENET_Type *base, uint32_t phyAddr);
151+
143152
/*!
144153
* @brief PHY Write function. This function write data over the SMI to
145154
* the specified PHY register. This function is called by all PHY interfaces.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/fsl_phy.c

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
/*
2-
* Copyright (c) 2015, Freescale Semiconductor, Inc.
3-
* All rights reserved.
4-
*
5-
* Redistribution and use in source and binary forms, with or without modification,
6-
* are permitted provided that the following conditions are met:
7-
*
8-
* o Redistributions of source code must retain the above copyright notice, this list
9-
* of conditions and the following disclaimer.
10-
*
11-
* o Redistributions in binary form must reproduce the above copyright notice, this
12-
* list of conditions and the following disclaimer in the documentation and/or
13-
* other materials provided with the distribution.
14-
*
15-
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16-
* contributors may be used to endorse or promote products derived from this
17-
* software without specific prior written permission.
18-
*
19-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21-
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23-
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24-
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25-
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26-
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28-
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29-
*/
2+
* Copyright (c) 2015, Freescale Semiconductor, Inc.
3+
* Copyright 2016-2017 NXP
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* o Redistributions of source code must retain the above copyright notice, this list
9+
* of conditions and the following disclaimer.
10+
*
11+
* o Redistributions in binary form must reproduce the above copyright notice, this
12+
* list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* o Neither the name of the copyright holder nor the names of its
16+
* contributors may be used to endorse or promote products derived from this
17+
* software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
3030

3131
#include "fsl_phy.h"
3232

@@ -53,54 +53,77 @@ extern uint32_t ENET_GetInstance(ENET_Type *base);
5353
* Variables
5454
******************************************************************************/
5555

56+
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
5657
/*! @brief Pointers to enet clocks for each instance. */
5758
extern clock_ip_name_t s_enetClock[FSL_FEATURE_SOC_ENET_COUNT];
59+
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
5860

5961
/*******************************************************************************
6062
* Code
6163
******************************************************************************/
6264

6365
status_t PHY_Init(ENET_Type *base, uint32_t phyAddr, uint32_t srcClock_Hz)
6466
{
65-
uint32_t bssReg;
6667
uint32_t counter = PHY_TIMEOUT_COUNT;
68+
uint32_t idReg = 0;
6769
status_t result = kStatus_Success;
6870
uint32_t instance = ENET_GetInstance(base);
6971

72+
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
7073
/* Set SMI first. */
7174
CLOCK_EnableClock(s_enetClock[instance]);
75+
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
7276
ENET_SetSMI(base, srcClock_Hz, false);
7377

78+
/* Initialization after PHY stars to work. */
79+
while ((idReg != PHY_CONTROL_ID1) && (counter != 0))
80+
{
81+
PHY_Read(base, phyAddr, PHY_ID1_REG, &idReg);
82+
counter --;
83+
}
84+
85+
if (!counter)
86+
{
87+
return kStatus_Fail;
88+
}
89+
7490
/* Reset PHY. */
7591
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, PHY_BCTL_RESET_MASK);
92+
93+
return result;
94+
}
95+
96+
status_t PHY_AutoNegotiation(ENET_Type *base, uint32_t phyAddr)
97+
{
98+
status_t result = kStatus_Success;
99+
uint32_t bssReg;
100+
uint32_t counter = PHY_TIMEOUT_COUNT;
101+
102+
/* Set the negotiation. */
103+
result = PHY_Write(base, phyAddr, PHY_AUTONEG_ADVERTISE_REG,
104+
(PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
105+
PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
76106
if (result == kStatus_Success)
77107
{
78-
/* Set the negotiation. */
79-
result = PHY_Write(base, phyAddr, PHY_AUTONEG_ADVERTISE_REG,
80-
(PHY_100BASETX_FULLDUPLEX_MASK | PHY_100BASETX_HALFDUPLEX_MASK |
81-
PHY_10BASETX_FULLDUPLEX_MASK | PHY_10BASETX_HALFDUPLEX_MASK | 0x1U));
108+
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG,
109+
(PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
82110
if (result == kStatus_Success)
83111
{
84-
result = PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG,
85-
(PHY_BCTL_AUTONEG_MASK | PHY_BCTL_RESTART_AUTONEG_MASK));
86-
if (result == kStatus_Success)
112+
/* Check auto negotiation complete. */
113+
while (counter --)
87114
{
88-
/* Check auto negotiation complete. */
89-
while (counter --)
115+
result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &bssReg);
116+
if ( result == kStatus_Success)
90117
{
91-
result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &bssReg);
92-
if ( result == kStatus_Success)
118+
if ((bssReg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0)
93119
{
94-
if ((bssReg & PHY_BSTATUS_AUTONEGCOMP_MASK) != 0)
95-
{
96-
break;
97-
}
120+
break;
98121
}
122+
}
99123

100-
if (!counter)
101-
{
102-
return kStatus_PHY_AutoNegotiateFail;
103-
}
124+
if (!counter)
125+
{
126+
return kStatus_PHY_AutoNegotiateFail;
104127
}
105128
}
106129
}

0 commit comments

Comments
 (0)