Choco OS  V.0.16.9.0
Join to the chocolate world
oc_eth_phy.c
Go to the documentation of this file.
1 
27 #include <oc_eth.h>
28 #include <oc_eth_phy.h>
29 #include <oc_module.h>
30 #include <oc_ktime.h>
31 
32 #ifdef oC_ETH_LLD_AVAILABLE
33 
39 #define _________________________________________LOCAL_PROT_SECTION_________________________________________________________________________
40 
41 static inline oC_ErrorCode_t WriteRegister ( oC_ETH_PhyAddress_t PhyAddress , oC_ETH_PhyRegister_t Register , uint32_t Value );
42 static inline oC_ErrorCode_t ReadRegister ( oC_ETH_PhyAddress_t PhyAddress , oC_ETH_PhyRegister_t Register , uint32_t * outValue );
43 
44 #undef _________________________________________LOCAL_PROT_SECTION_________________________________________________________________________
45 
46 
52 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________
53 
54 //==========================================================================================================================================
69 //==========================================================================================================================================
70 oC_ErrorCode_t oC_ETH_WritePhyRegister( oC_ETH_PhyAddress_t PhyAddress , oC_ETH_PhyRegister_t Register , uint32_t Value )
71 {
72  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
73 
74  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_ETH))
75  {
76  errorCode = WriteRegister(PhyAddress,Register,Value);
77  }
78 
79  return errorCode;
80 }
81 
82 //==========================================================================================================================================
96 //==========================================================================================================================================
97 oC_ErrorCode_t oC_ETH_ReadPhyRegister( oC_ETH_PhyAddress_t PhyAddress , oC_ETH_PhyRegister_t Register , uint32_t * outValue )
98 {
99  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
100 
101  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_ETH))
102  {
103  errorCode = ReadRegister(PhyAddress,Register,outValue);
104  }
105 
106  return errorCode;
107 }
108 
109 //==========================================================================================================================================
120 //==========================================================================================================================================
121 oC_ErrorCode_t oC_ETH_PhyReset( oC_ETH_PhyAddress_t PhyAddress )
122 {
123  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
124 
125  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_ETH))
126  {
127  oC_ETH_PhyRegister_BCR_t BCR = { .Value = 0 };
128 
129  BCR.SoftReset = 1;
130 
131  if(ErrorCode( WriteRegister( PhyAddress , oC_ETH_PhyRegister_BCR , BCR.Value ) ))
132  {
133  /* The reset process takes 0.5 s */
134  sleep(ms(500));
135 
136  errorCode = oC_ErrorCode_None;
137  }
138  }
139 
140  return errorCode;
141 }
142 
143 //==========================================================================================================================================
154 //==========================================================================================================================================
155 oC_ErrorCode_t oC_ETH_ReadLinkStatus( oC_ETH_PhyAddress_t PhyAddress , oC_Net_LinkStatus_t * outLinkStatus )
156 {
157  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
158 
159  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_ETH))
160  {
161  oC_ETH_PhyRegister_BSR_t BSR = { .Value = 0 };
162 
163  if(
164  ErrorCondition( isram(outLinkStatus) , oC_ErrorCode_OutputAddressNotInRAM )
165  && ErrorCode ( ReadRegister( PhyAddress , oC_ETH_PhyRegister_BSR , &BSR.Value ) )
166  )
167  {
168  *outLinkStatus = BSR.LinkStatus == 1 ? oC_Net_LinkStatus_Up : oC_Net_LinkStatus_Down;
169  errorCode = oC_ErrorCode_None;
170  }
171  }
172 
173  return errorCode;
174 }
175 
176 //==========================================================================================================================================
188 //==========================================================================================================================================
189 oC_ErrorCode_t oC_ETH_SetAutoNegotiation( oC_ETH_PhyAddress_t PhyAddress , bool Enabled , oC_Time_t Timeout )
190 {
191  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
192 
193  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_ETH))
194  {
195  oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
196  oC_ETH_PhyRegister_BCR_t BCR = { .Value = 0 };
197  oC_ETH_PhyRegister_BSR_t BSR = { .Value = 0 };
198 
199 
200  if(
201  ErrorCode( ReadRegister ( PhyAddress , oC_ETH_PhyRegister_BCR , &BCR.Value ) )
202  )
203  {
204  BCR.AutoNegotiation = 1;
205 
206  if(
207  ErrorCode( WriteRegister( PhyAddress , oC_ETH_PhyRegister_BCR , BCR.Value ) )
208  )
209  {
210  errorCode = oC_ErrorCode_None;
211 
212  while(
213  Timeout > 0
214  && ErrorCode ( ReadRegister(PhyAddress,oC_ETH_PhyRegister_BSR,&BSR.Value ))
215  && BSR.AutoNegotiateComplete == 0
216  && ErrorCondition ( oC_KTime_GetTimestamp() < endTimestamp , oC_ErrorCode_Timeout )
217  );
218  }
219  }
220  }
221 
222  return errorCode;
223 }
224 
225 //==========================================================================================================================================
236 //==========================================================================================================================================
237 oC_ErrorCode_t oC_ETH_SetPhyLoopback( oC_ETH_PhyAddress_t PhyAddress , bool Enabled )
238 {
239  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
240 
241  if(oC_Module_TurnOnVerification(&errorCode , oC_Module_ETH))
242  {
243  oC_ETH_PhyRegister_BCR_t BCR = { .Value = 0 };
244 
245  if( ErrorCode( ReadRegister ( PhyAddress , oC_ETH_PhyRegister_BCR , &BCR.Value )) )
246  {
247  if(Enabled)
248  {
249  BCR.Loopback = 1;
250  BCR.AutoNegotiation = 0;
251  BCR.SpeedSelect = 1;
252  BCR.DuplexMode = 1;
253  }
254  else
255  {
256  BCR.Loopback = 0;
257  BCR.AutoNegotiation = 1;
258  BCR.SpeedSelect = 0;
259  BCR.DuplexMode = 0;
260  }
261 
262  if( ErrorCode( WriteRegister( PhyAddress , oC_ETH_PhyRegister_BCR , BCR.Value )) )
263  {
264  errorCode = oC_ErrorCode_None;
265  }
266  }
267  }
268 
269  return errorCode;
270 }
271 
272 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________
273 
279 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
280 
281 //==========================================================================================================================================
285 //==========================================================================================================================================
286 static inline oC_ErrorCode_t WriteRegister( oC_ETH_PhyAddress_t PhyAddress , oC_ETH_PhyRegister_t Register , uint32_t Value )
287 {
288  return oC_ETH_LLD_WritePhyRegister(PhyAddress,(oC_ETH_LLD_PHY_RegisterAddress_t)Register,Value);
289 }
290 
291 //==========================================================================================================================================
295 //==========================================================================================================================================
296 static inline oC_ErrorCode_t ReadRegister( oC_ETH_PhyAddress_t PhyAddress , oC_ETH_PhyRegister_t Register , uint32_t * outValue )
297 {
298  return oC_ETH_LLD_ReadPhyRegister(PhyAddress,(oC_ETH_LLD_PHY_RegisterAddress_t)Register,outValue);
299 }
300 
301 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
302 
303 #endif
uint32_t SoftReset
Soft-Reset: 1 = Software reset.
Definition: oc_eth_phy.h:90
stores value of the BSR register
Definition: oc_eth_phy.h:109
oC_ErrorCode_t oC_ETH_ReadPhyRegister(oC_ETH_PhyAddress_t PhyAddress, oC_ETH_PhyRegister_t Register, uint32_t *outValue)
writes PHY register
Definition: oc_eth_phy.c:97
oC_ETH_PhyRegister_BasicStatusRegister
Definition: oc_eth_phy.h:64
oC_ErrorCode_t oC_ETH_SetAutoNegotiation(oC_ETH_PhyAddress_t PhyAddress, bool Enabled, oC_Time_t Timeout)
enables/disables AutoNegotiation
Definition: oc_eth_phy.c:189
static oC_ErrorCode_t ReadRegister(oC_ETH_PhyAddress_t PhyAddress, oC_ETH_PhyRegister_t Register, uint32_t *outValue)
reads PHY register
Definition: oc_eth_phy.c:296
uint32_t Value
Stores value of the register.
Definition: oc_eth_phy.h:136
uint32_t AutoNegotiation
Auto-Negotiation Enable: 0 = disable auto-negotiate process, 1 = enable auto negotiate process...
Definition: oc_eth_phy.h:87
oC_ErrorCode_t oC_ETH_PhyReset(oC_ETH_PhyAddress_t PhyAddress)
resets PHY
Definition: oc_eth_phy.c:121
Header with definitions for PHY handling.
oC_ErrorCode_t oC_ETH_WritePhyRegister(oC_ETH_PhyAddress_t PhyAddress, oC_ETH_PhyRegister_t Register, uint32_t Value)
writes PHY register
Definition: oc_eth_phy.c:70
#define ms(time)
Number of ms.
Definition: oc_cfg.h:128
The file with interface for the module library.
oC_ETH_PhyRegister_t
stores PHY register address
Definition: oc_eth_phy.h:58
static oC_ErrorCode_t WriteRegister(oC_ETH_PhyAddress_t PhyAddress, oC_ETH_PhyRegister_t Register, uint32_t Value)
writes PHY register
Definition: oc_eth_phy.c:286
oC_ETH_PhyRegister_BasicControlRegister
Definition: oc_eth_phy.h:63
uint32_t AutoNegotiateComplete
Auto-Negotiate Complete: 0 = auto negotiate process not completed, 1 = auto negotiate process complet...
Definition: oc_eth_phy.h:119
oC_ErrorCode_t oC_ETH_SetPhyLoopback(oC_ETH_PhyAddress_t PhyAddress, bool Enabled)
enables/disables PHY loopback mode
Definition: oc_eth_phy.c:237
uint32_t Value
Stores value of the register.
Definition: oc_eth_phy.h:98
oC_ErrorCode_t oC_ETH_ReadLinkStatus(oC_ETH_PhyAddress_t PhyAddress, oC_Net_LinkStatus_t *outLinkStatus)
reads link status from the PHY
Definition: oc_eth_phy.c:155
uint32_t SpeedSelect
Speed select: 0 = Slower, 1 = Faster.
Definition: oc_eth_phy.h:88
Network interface is DOWN (cable is not connected)
Definition: oc_net.h:363
uint32_t LinkStatus
Link Status: 0 = link is down, 1 = link is up.
Definition: oc_eth_phy.h:116
stores value of the BCR register
Definition: oc_eth_phy.h:77
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
Definition: oc_module.h:138
uint32_t DuplexMode
Duplex mode: 0 = Half duplex, 1 = Full duplex.
Definition: oc_eth_phy.h:83
Network interface is UP (cable is connected)
Definition: oc_net.h:362
oC_Net_LinkStatus_t
stores network interface link status
Definition: oc_net.h:360
oC_ETH_LLD_PHY_Address_t oC_ETH_PhyAddress_t
stores address of a PHY
Definition: oc_eth.h:97
uint32_t Loopback
Loopback mode: 0 = Normal operation, 1 = loopback mode.
Definition: oc_eth_phy.h:89
The file with interface for ETH driver.
The file with interface of kernel time module.