-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoNeg.h
More file actions
202 lines (172 loc) · 4.44 KB
/
Copy pathAutoNeg.h
File metadata and controls
202 lines (172 loc) · 4.44 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef AutoNeg_h
#define AutoNeg_h
#include "Arduino.h"
namespace AutoNeg {
// Code word size (extended pages not supported)
static const unsigned char PAGE_SIZE = 16;
// Code word special bits
static const unsigned char RF = 13;
static const unsigned char ACK = 14;
static const unsigned char NP = 15;
/** @brief Timer abstraction with IEEE 802.3 state diagram semantics
*
* Timer is a thin wrapper around micros(), providing an ability to
* statically define a timer's timeout, start a timer, and test if
* a timer is done, i.e., if it's timeout has arrived.
*/
class Timer {
public:
Timer(unsigned long period_in_us) : period_(period_in_us) {}
void start()
{
start(micros());
}
void start(unsigned long epoch)
{
timeout_ = epoch + period_;
}
bool done() const
{
return micros() >= timeout_;
}
private:
unsigned long period_;
unsigned long timeout_;
};
/** @brief IEEE 802.3 Clause 28 Auto-Negotiation receiver
*
* Implements the Receive state diagram from Figure 28-17.
*/
class Receiver {
private:
enum class State {
IDLE = 1,
LINK_PULSE_DETECT,
LINK_PULSE_COUNT,
FLP_PASS,
FLP_CHECK,
FLP_CAPTURE,
FLP_CLOCK,
FLP_DATA_0,
FLP_DATA_1,
};
public:
Receiver();
void start() {
enter(State::IDLE);
}
void tick();
void received_pulse();
void reset();
private:
void enter(State state);
static const char FLP_CNT_DONE = 6;
static const char RX_BIT_CNT_CHECK = 10;
State state_;
volatile char linkpulse_ = 0;
volatile unsigned long linkpulse_time_;
char flp_cnt_;
bool flp_receive_idle_;
bool ability_match_;
char ability_match_counter_;
bool acknowledge_match_;
char acknowledge_match_counter_;
bool consistency_match_;
char rx_bit_cnt_;
char rx_link_code_word_[AutoNeg::PAGE_SIZE];
char ability_match_word_[AutoNeg::PAGE_SIZE];
Timer flp_test_min_timer_{25};
Timer flp_test_max_timer_{165};
Timer nlp_test_min_timer_{7000};
Timer nlp_test_max_timer_{150000};
Timer data_detect_min_timer_{47};
Timer data_detect_max_timer_{120}; // from experimentation
friend class Arbitrator;
};
/** @brief IEEE 802.3 Clause 28 Auto-Negotiation transmitter
*
* Implements the Transmit state diagram from Figure 28-16.
*/
class Transmitter {
private:
enum class State {
IDLE,
TRANSMIT_REMAINING_ACKNOWLEDGE,
TRANSMIT_ABILITY,
TRANSMIT_CLOCK_BIT,
TRANSMIT_DATA_BIT,
TRANSMIT_COUNT_ACK,
};
public:
Transmitter();
void start() {
enter(State::IDLE);
}
void tick();
private:
void enter(State state);
void send_pulse() const;
static const char REMAINING_ACK_CNT_DONE = 6;
static const char TX_BIT_CNT_DONE = PAGE_SIZE + 1;
State state_;
char remaining_ack_cnt_;
bool ack_finished_ = false;
bool complete_ack_ = false;
bool transmit_ability_ = false;
bool transmit_ack_ = false;
char tx_bit_cnt_;
char tx_link_code_word_[PAGE_SIZE];
Timer transmit_link_burst_timer_{14000};
Timer interval_timer_{48}; // from experimentation
friend class Arbitrator;
};
/** @brief IEEE 802.3 Clause 28 Auto-Negotiation arbitrator
*
* Implements (most of) the Arbitration state diagram from Figure 28-18.
*
* Next Page and Extended Page support not (yet) implemented.
*/
class Arbitrator {
public:
Arbitrator();
void start();
void tick();
void received_pulse() {
receiver_.received_pulse();
}
bool complete() const {
return mr_autoneg_complete_;
}
const char *get_ability_match_word() const {
return receiver_.ability_match_word_;
}
private:
enum class State {
AUTO_NEGOTIATION_ENABLE,
TRANSMIT_DISABLE,
ABILITY_DETECT,
ACKNOWLEDGE_DETECT,
COMPLETE_ACKNOWLEDGE,
NEXT_PAGE_WAIT,
AUTO_NEGOTIATION_COMPLETE,
};
void enter(State state);
State state_;
char mr_adv_ability_[PAGE_SIZE] = { 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0 };
bool mr_autoneg_complete_;
bool mr_next_page_loaded_;
bool mr_lp_autoneg_able_;
bool mr_lp_np_able_;
bool mr_np_able_;
char mr_np_tx_[PAGE_SIZE];
bool mr_page_rx_;
bool base_page_;
bool desire_np_;
bool np_rx_;
bool toggle_rx_;
bool toggle_tx_;
Receiver receiver_;
Transmitter transmitter_;
};
};
#endif