1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
35
36
47
48
49
50
51
54
55
62
63
64
68
69
70
71
72
73
74
75
76
77
78
87
88
89
90
91
92
93
94
99
100
101
102
103
104
121
122
126
127
128
166
167
168
169
170
171
172
173
176
177
178
179
180
181
182
187
204
205
212
213
214
215
216
217
227
231
232
233
234
235
242
243
244
245
246
247
257
261
262
263
264
265
272
273
274
275
276
277
287
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
317
318
319
323
324
329
330
331
335
336
355
356
357
361
362
363
367
368
372
376
380
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
414
418
422
427
428
432
433
441
442
443
444
445
452
453
454
455
456
457
467
471
472
473
474
475
482
483
484
485
486
487
497
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
537
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
577
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
640
644
645
646
647
648
651
652
655
656
659
660
/* ... */
#include "dp83848.h"
/* ... */
/* ... */
/* ... */
Includes
/* ... */
#define DP83848_SW_RESET_TO ((uint32_t)500U)
#define DP83848_INIT_TO ((uint32_t)2000U)
#define DP83848_MAX_DEV_ADDR ((uint32_t)31U)
/* ... */
Private define
/* ... */
/* ... */
int32_t DP83848_RegisterBusIO(dp83848_Object_t *pObj, dp83848_IOCtx_t *ioctx)
{
if(!pObj || !ioctx->ReadReg || !ioctx->WriteReg || !ioctx->GetTick)
{
return DP83848_STATUS_ERROR;
}if (!pObj || !ioctx->ReadReg || !ioctx->WriteReg || !ioctx->GetTick) { ... }
pObj->IO.Init = ioctx->Init;
pObj->IO.DeInit = ioctx->DeInit;
pObj->IO.ReadReg = ioctx->ReadReg;
pObj->IO.WriteReg = ioctx->WriteReg;
pObj->IO.GetTick = ioctx->GetTick;
return DP83848_STATUS_OK;
}{ ... }
/* ... */
int32_t DP83848_Init(dp83848_Object_t *pObj)
{
uint32_t tickstart = 0, regvalue = 0, addr = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->Is_Initialized == 0)
{
if(pObj->IO.Init != 0)
{
pObj->IO.Init();
}if (pObj->IO.Init != 0) { ... }
pObj->DevAddr = DP83848_MAX_DEV_ADDR + 1;
for(addr = 0; addr <= DP83848_MAX_DEV_ADDR; addr ++)
{
if(pObj->IO.ReadReg(addr, DP83848_SMR, ®value) < 0)
{
status = DP83848_STATUS_READ_ERROR;
/* ... */
continue;
}if (pObj->IO.ReadReg(addr, DP83848_SMR, ®value) < 0) { ... }
if((regvalue & DP83848_SMR_PHY_ADDR) == addr)
{
pObj->DevAddr = addr;
status = DP83848_STATUS_OK;
break;
}if ((regvalue & DP83848_SMR_PHY_ADDR) == addr) { ... }
}for (addr = 0; addr <= DP83848_MAX_DEV_ADDR; addr ++) { ... }
if(pObj->DevAddr > DP83848_MAX_DEV_ADDR)
{
status = DP83848_STATUS_ADDRESS_ERROR;
}if (pObj->DevAddr > DP83848_MAX_DEV_ADDR) { ... }
if(status == DP83848_STATUS_OK)
{
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, DP83848_BCR_SOFT_RESET) >= 0)
{
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, ®value) >= 0)
{
tickstart = pObj->IO.GetTick();
while(regvalue & DP83848_BCR_SOFT_RESET)
{
if((pObj->IO.GetTick() - tickstart) <= DP83848_SW_RESET_TO)
{
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, ®value) < 0)
{
status = DP83848_STATUS_READ_ERROR;
break;
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, ®value) < 0) { ... }
}if ((pObj->IO.GetTick() - tickstart) <= DP83848_SW_RESET_TO) { ... }
else
{
status = DP83848_STATUS_RESET_TIMEOUT;
break;
}else { ... }
}while (regvalue & DP83848_BCR_SOFT_RESET) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, ®value) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, DP83848_BCR_SOFT_RESET) >= 0) { ... }
else
{
status = DP83848_STATUS_WRITE_ERROR;
}else { ... }
}if (status == DP83848_STATUS_OK) { ... }
}if (pObj->Is_Initialized == 0) { ... }
if(status == DP83848_STATUS_OK)
{
tickstart = pObj->IO.GetTick();
while((pObj->IO.GetTick() - tickstart) <= DP83848_INIT_TO)
{
}while ((pObj->IO.GetTick() - tickstart) <= DP83848_INIT_TO) { ... }
pObj->Is_Initialized = 1;
}if (status == DP83848_STATUS_OK) { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_DeInit(dp83848_Object_t *pObj)
{
if(pObj->Is_Initialized)
{
if(pObj->IO.DeInit != 0)
{
if(pObj->IO.DeInit() < 0)
{
return DP83848_STATUS_ERROR;
}if (pObj->IO.DeInit() < 0) { ... }
}if (pObj->IO.DeInit != 0) { ... }
pObj->Is_Initialized = 0;
}if (pObj->Is_Initialized) { ... }
return DP83848_STATUS_OK;
}{ ... }
/* ... */
int32_t DP83848_DisablePowerDownMode(dp83848_Object_t *pObj)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0)
{
readval &= ~DP83848_BCR_POWER_DOWN;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_EnablePowerDownMode(dp83848_Object_t *pObj)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0)
{
readval |= DP83848_BCR_POWER_DOWN;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_StartAutoNego(dp83848_Object_t *pObj)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0)
{
readval |= DP83848_BCR_AUTONEGO_EN;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_GetLinkState(dp83848_Object_t *pObj)
{
uint32_t readval = 0;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BSR, &readval) < 0)
{
return DP83848_STATUS_READ_ERROR;
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BSR, &readval) < 0) { ... }
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BSR, &readval) < 0)
{
return DP83848_STATUS_READ_ERROR;
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BSR, &readval) < 0) { ... }
if((readval & DP83848_BSR_LINK_STATUS) == 0)
{
return DP83848_STATUS_LINK_DOWN;
}if ((readval & DP83848_BSR_LINK_STATUS) == 0) { ... }
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) < 0)
{
return DP83848_STATUS_READ_ERROR;
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) < 0) { ... }
if((readval & DP83848_BCR_AUTONEGO_EN) != DP83848_BCR_AUTONEGO_EN)
{
if(((readval & DP83848_BCR_SPEED_SELECT) == DP83848_BCR_SPEED_SELECT) && ((readval & DP83848_BCR_DUPLEX_MODE) == DP83848_BCR_DUPLEX_MODE))
{
return DP83848_STATUS_100MBITS_FULLDUPLEX;
}if (((readval & DP83848_BCR_SPEED_SELECT) == DP83848_BCR_SPEED_SELECT) && ((readval & DP83848_BCR_DUPLEX_MODE) == DP83848_BCR_DUPLEX_MODE)) { ... }
else if ((readval & DP83848_BCR_SPEED_SELECT) == DP83848_BCR_SPEED_SELECT)
{
return DP83848_STATUS_100MBITS_HALFDUPLEX;
}else if ((readval & DP83848_BCR_SPEED_SELECT) == DP83848_BCR_SPEED_SELECT) { ... }
else if ((readval & DP83848_BCR_DUPLEX_MODE) == DP83848_BCR_DUPLEX_MODE)
{
return DP83848_STATUS_10MBITS_FULLDUPLEX;
}else if ((readval & DP83848_BCR_DUPLEX_MODE) == DP83848_BCR_DUPLEX_MODE) { ... }
else
{
return DP83848_STATUS_10MBITS_HALFDUPLEX;
}else { ... }
}if ((readval & DP83848_BCR_AUTONEGO_EN) != DP83848_BCR_AUTONEGO_EN) { ... }
else
{
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_PHYSCSR, &readval) < 0)
{
return DP83848_STATUS_READ_ERROR;
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_PHYSCSR, &readval) < 0) { ... }
if((readval & DP83848_PHYSCSR_AUTONEGO_DONE) == 0)
{
return DP83848_STATUS_AUTONEGO_NOTDONE;
}if ((readval & DP83848_PHYSCSR_AUTONEGO_DONE) == 0) { ... }
if((readval & DP83848_PHYSCSR_HCDSPEEDMASK) == DP83848_PHYSCSR_100BTX_FD)
{
return DP83848_STATUS_100MBITS_FULLDUPLEX;
}if ((readval & DP83848_PHYSCSR_HCDSPEEDMASK) == DP83848_PHYSCSR_100BTX_FD) { ... }
else if ((readval & DP83848_PHYSCSR_HCDSPEEDMASK) == DP83848_PHYSCSR_100BTX_HD)
{
return DP83848_STATUS_100MBITS_HALFDUPLEX;
}else if ((readval & DP83848_PHYSCSR_HCDSPEEDMASK) == DP83848_PHYSCSR_100BTX_HD) { ... }
else if ((readval & DP83848_PHYSCSR_HCDSPEEDMASK) == DP83848_PHYSCSR_10BT_FD)
{
return DP83848_STATUS_10MBITS_FULLDUPLEX;
}else if ((readval & DP83848_PHYSCSR_HCDSPEEDMASK) == DP83848_PHYSCSR_10BT_FD) { ... }
else
{
return DP83848_STATUS_10MBITS_HALFDUPLEX;
}else { ... }
}else { ... }
}{ ... }
/* ... */
int32_t DP83848_SetLinkState(dp83848_Object_t *pObj, uint32_t LinkState)
{
uint32_t bcrvalue = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &bcrvalue) >= 0)
{
bcrvalue &= ~(DP83848_BCR_AUTONEGO_EN | DP83848_BCR_SPEED_SELECT | DP83848_BCR_DUPLEX_MODE);
if(LinkState == DP83848_STATUS_100MBITS_FULLDUPLEX)
{
bcrvalue |= (DP83848_BCR_SPEED_SELECT | DP83848_BCR_DUPLEX_MODE);
}if (LinkState == DP83848_STATUS_100MBITS_FULLDUPLEX) { ... }
else if (LinkState == DP83848_STATUS_100MBITS_HALFDUPLEX)
{
bcrvalue |= DP83848_BCR_SPEED_SELECT;
}else if (LinkState == DP83848_STATUS_100MBITS_HALFDUPLEX) { ... }
else if (LinkState == DP83848_STATUS_10MBITS_FULLDUPLEX)
{
bcrvalue |= DP83848_BCR_DUPLEX_MODE;
}else if (LinkState == DP83848_STATUS_10MBITS_FULLDUPLEX) { ... }
else
{
status = DP83848_STATUS_ERROR;
}else { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &bcrvalue) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
if(status == DP83848_STATUS_OK)
{
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, bcrvalue) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, bcrvalue) < 0) { ... }
}if (status == DP83848_STATUS_OK) { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_EnableLoopbackMode(dp83848_Object_t *pObj)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0)
{
readval |= DP83848_BCR_LOOPBACK;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_DisableLoopbackMode(dp83848_Object_t *pObj)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0)
{
readval &= ~DP83848_BCR_LOOPBACK;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_BCR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_BCR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_EnableIT(dp83848_Object_t *pObj, uint32_t Interrupt)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_IMR, &readval) >= 0)
{
readval |= Interrupt;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_IMR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_IMR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_IMR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_DisableIT(dp83848_Object_t *pObj, uint32_t Interrupt)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_IMR, &readval) >= 0)
{
readval &= ~Interrupt;
if(pObj->IO.WriteReg(pObj->DevAddr, DP83848_IMR, readval) < 0)
{
status = DP83848_STATUS_WRITE_ERROR;
}if (pObj->IO.WriteReg(pObj->DevAddr, DP83848_IMR, readval) < 0) { ... }
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_IMR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_ClearIT(dp83848_Object_t *pObj, uint32_t Interrupt)
{
uint32_t readval = 0;
int32_t status = DP83848_STATUS_OK;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_ISFR, &readval) < 0)
{
status = DP83848_STATUS_READ_ERROR;
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_ISFR, &readval) < 0) { ... }
return status;
}{ ... }
/* ... */
int32_t DP83848_GetITStatus(dp83848_Object_t *pObj, uint32_t Interrupt)
{
uint32_t readval = 0;
int32_t status = 0;
if(pObj->IO.ReadReg(pObj->DevAddr, DP83848_ISFR, &readval) >= 0)
{
status = ((readval & Interrupt) == Interrupt);
}if (pObj->IO.ReadReg(pObj->DevAddr, DP83848_ISFR, &readval) >= 0) { ... }
else
{
status = DP83848_STATUS_READ_ERROR;
}else { ... }
return status;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */