-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksz8895.c
116 lines (92 loc) · 4.08 KB
/
ksz8895.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
////////////////////////////////////////////////////////////////////////////////
/// @file
/// @brief KSZ8895 Ethernet switch.
////////////////////////////////////////////////////////////////////////////////
// *****************************************************************************
// ************************** System Include Files *****************************
// *****************************************************************************
// *****************************************************************************
// ************************** User Include Files *******************************
// *****************************************************************************
#include "tcpip/tcpip.h"
#include "ksz8895.h"
#include "softspi.h"
// *****************************************************************************
// ************************** Macros *******************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Defines ******************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Definitions **************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Function Prototypes ******************************
// *****************************************************************************
// *****************************************************************************
// ************************** Global Functions *********************************
// *****************************************************************************
int KSZ8895Init( void )
{
uint8_t readData;
// Read chip family ID to verify SPI bus functionality. ---------
do {
// Read contents of register 0x00.
while (KSZ8895ReadReg(0x00, &readData) != 0);
}
while (readData != 0x95);
// Write/Read/Verify the start switch bit. ----------------------
do {
// Write "Start Switch" bit to register 0x01.
while (KSZ8895WriteReg(0x01, 0x01) != 0);
// Read contents of register 0x01.
while (KSZ8895ReadReg(0x01, &readData) != 0);
}
while ((readData & 0x01) != 0x01); // Verify "Start Switch" bit is set.
return 0;
}
//==============================================================================
int KSZ8895ReadReg(uint8_t address, uint8_t *data)
{
uint32_t rxData;
SOFTSPI_TRANSFER readCmd =
{
.dev = SOFTSPI_KSZ8895,
.txDat = 0x00030000 | (((uint32_t)address) << 8),
.rxBuf = &rxData,
.numOfBits = 24,
.freq = 50000,
};
if (SoftSPIXfer(&readCmd) != 0)
{
return -1;
}
while (!readCmd.xferDone); // Wait for transfer to complete.
*data = rxData & 0x000000FF;
return 0;
}
//==============================================================================
int KSZ8895WriteReg(uint8_t address, uint8_t data)
{
SOFTSPI_TRANSFER xfer =
{
.dev = SOFTSPI_KSZ8895,
.txDat = 0x00020000 | (((uint32_t)address) << 8) | data,
.rxBuf = 0,
.numOfBits = 24,
.freq = 50000,
.xferDone = 0
};
return SoftSPIXfer(&xfer);
}
//==============================================================================
void KSZ8895Reset( void )
{
LATDCLR = _LATD_LATD5_MASK; // Low
DelayMs(100); // Allow time for capacitive load to switch.
LATDSET = _LATD_LATD5_MASK; // High
DelayMs(300); // Allow time for switch to power up.
}
// *****************************************************************************
// ************************** Static Functions *********************************
// *****************************************************************************