-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_timing_module.cpp
More file actions
320 lines (268 loc) · 8.26 KB
/
Copy pathbit_timing_module.cpp
File metadata and controls
320 lines (268 loc) · 8.26 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "mbed.h"
//Parâmetros configuráveis
#define SJW 2
#define SYNC 1
#define TSEG1 8
#define TSEG2 7
#define BITRATE 62.5 // Para um Tq de 10ms
//#define BITRATE 1// Bit com 1 segundo (debug)
//#define BITRATE 0.0625//Tq de 1 segundo (debug)
#define EOFINTERFRAME 10
#define ON 1
#define OFF 0
//Serial para debug
Serial pc(USBTX,USBRX);
//Leds de sinal
DigitalOut tq_led(p19);
DigitalOut softsync_led(LED1);
DigitalOut hardsync_led(LED2);
DigitalOut st_led2(LED3);
DigitalOut st_led1(LED4);
//Saídas de sinal de indicação
DigitalOut tq_sig(p11);
DigitalOut softsync_sig(p12);
DigitalOut hardsync_sig(p13);
DigitalOut st_sig1(p14);
DigitalOut st_sig2(p15);
//Controles
DigitalIn REQ(p5);
DigitalOut SAMPLE(p6);
DigitalOut WRITE(p7);
DigitalIn TX(p8);
DigitalOut RX(p9);
//Interface com transciever
DigitalOut tx_can(p22);
DigitalIn rx_can(p21);
//Timeout
Ticker tic;
//------------------------------------------------------------------------------
//Declaração dos ENUMs
//------------------------------------------------------------------------------
enum{
SYNC_STATE,
SEG1_STATE,
SEG2_STATE
}STATE;
//------------------------------------------------------------------------------
//Variáveis
//------------------------------------------------------------------------------
int tq_counter = 0;
int s1_counter=0;
int s2_counter=0;
int state = 0;
int next_state = SYNC_STATE;
float tq_period = 0;
float bit_period=0;
int last_bit_read = 1;
int bit_read = 1;
int recessive_bit_counter = 0;
int low_transition_detected = 0;
int hardsync=0;
int softsync=0;
int sjw=0;
int sample=0;
int sigwrite=0;
//------------------------------------------------------------------------------
//Função para configurar a velocidade de comunicação
//------------------------------------------------------------------------------
void configBitRate(float baud){
bit_period = (float)1/(float)baud;
tq_period = (bit_period/(float)(SYNC+TSEG1+TSEG2));
//printf("tq_period: %f...\r\n",tq_period);
}
//------------------------------------------------------------------------------
//Função para o estado sync
//------------------------------------------------------------------------------
int syncState(){
tq_counter=1; // incremanta contador de tq.
tq_led = !tq_led;//Toggle do led de tq.
tq_sig = !tq_sig;//Toogle do sinal de tq no pino.
st_led2 = OFF;
st_led1 = OFF;// leds de indicação de estado interno = 00(0).
st_sig2 = 0;
st_sig1 = 0;// sinais de indicação de estado interno = 00(0).
sigwrite=0;
softsync_led=0;
hardsync_led=0;
softsync=0;
hardsync=0;
bit_read=rx_can.read();//Leitura do pino rx da interface can
if( last_bit_read == 1 ){
if( bit_read == 0 ) { //detectou uma transição high to low
if(recessive_bit_counter > EOFINTERFRAME){//Verifica se foi hard ou soft sync
hardsync_led=1;
hardsync=1;
WRITE=1;
sigwrite=1;
}
else{
softsync_led=1;
softsync=1;
}
}
}
return SEG1_STATE;
}
//------------------------------------------------------------------------------
//Função para o estado TSEG1
//------------------------------------------------------------------------------
int t1State(){
s1_counter++;
tq_counter=s1_counter;// incremanta contador de tq.
tq_led = !tq_led;//Toggle do led de tq.
tq_sig = !tq_sig;//Toogle do sinal de tq no pino.
st_led2 = 0;
st_led1 = 1;// leds de indicação de estado interno = 01(1).
st_sig2 = 0;
st_sig1 = 1;// sinais de indicação de estado interno = 01(1).
sigwrite=0;
softsync_led=0;
hardsync_led=0;
softsync=0;
hardsync=0;
bit_read=rx_can.read();//Leitura do pino rx da interface can
if( last_bit_read == 1 ){
if( bit_read == 0 ){ //detectou uma transição high to low
if( recessive_bit_counter > EOFINTERFRAME ){//Verifica se foi hard ou soft sync
hardsync_led=1;
hardsync=1;
sigwrite=1;
WRITE=1;
s1_counter=0;
last_bit_read=0;
recessive_bit_counter=0;
}
else{
softsync_led=1;
softsync=1;
sjw=SJW;
}
}
}
if( s1_counter < TSEG1+sjw ){//fica nesse estado até que s1_counter chegue em TSEG1+sjw
return SEG1_STATE;
}
else{//Senão salta para o próximo estado
s1_counter=0;
sjw=0;
return SEG2_STATE;
}
}
//------------------------------------------------------------------------------
//Função para o estado TSEG2
//------------------------------------------------------------------------------
int t2State(){
s2_counter++;
tq_counter=s2_counter;// incremanta contador de tq.
tq_led = !tq_led;//Toggle do led de tq.
tq_sig = !tq_sig;//Toogle do sinal de tq no pino.
st_led2=1;
st_led1=0;//leds de indicação de estado interno = 10(2).
st_sig2 = 1;
st_sig1 = 0;// sinais de indicação de estado interno = 10(2).
sample = 0;
softsync_led=0;
hardsync_led=0;
softsync=0;
hardsync=0;
WRITE=0;
sigwrite=0;
bit_read=rx_can.read();//Leitura do pino rx da interface can
if( last_bit_read == 1 ){
if( bit_read == 0 ){ //detectou uma transição high to low
if(recessive_bit_counter > EOFINTERFRAME){//Verifica se foi hard ou soft sync
hardsync_led=1;
hardsync=1;
WRITE=1;
sigwrite=1;
last_bit_read=0;
recessive_bit_counter=0;
}
else{
softsync_led=1;
softsync=1;
sjw=(-1)*SJW;
}
}
}
if( s2_counter == 1 ){
SAMPLE=1;
sample=1;
RX=rx_can;
last_bit_read = bit_read;
if(bit_read==1){
recessive_bit_counter++;
}
else{
recessive_bit_counter=0;
}
}
else{
SAMPLE=0;
}
if( hardsync==1 ){
sjw=0;
s2_counter = 0;
return SEG1_STATE;
}
if( s2_counter < (TSEG2+sjw) ){
return SEG2_STATE;
}
else{
sjw=0;
s2_counter=0;
return SYNC_STATE;
}
}
//------------------------------------------------------------------------------
//Função que imprime os sinais internos na serial
//------------------------------------------------------------------------------
void serial_debug(){
pc.printf("tq_counter:%2d ",tq_counter);
pc.printf("state:%2d ",state);
pc.printf("next_state:%2d ",next_state);
pc.printf("bit_read:%2d ",bit_read);
pc.printf("last_bit_read:%2d ",last_bit_read);
pc.printf("hardsync:%2d ",hardsync);
pc.printf("softsync:%2d ",softsync);
pc.printf("recessive_bit_counter:%2d ",recessive_bit_counter);
pc.printf("sample:%2d ",sample);
pc.printf("write:%2d ",sigwrite);
pc.printf("\r\n");
}
//------------------------------------------------------------------------------
//Função que imprime os sinais internos na serial
//------------------------------------------------------------------------------
void atTic(){
switch(next_state){
case SYNC_STATE:
state=0;
next_state = syncState();
break;
case SEG1_STATE:
state=1;
next_state = t1State();
break;
case SEG2_STATE:
state=2;
next_state = t2State();
break;
}
//serial_debug();
}
//------------------------------------------------------------------------------
//Função principal
//------------------------------------------------------------------------------
int main() {
pc.baud(921600);
configBitRate(BITRATE);
tic.attach(&atTic,tq_period);
//tic.attach(&atTic,1);
tx_can=1;
while(1) {
REQ.read();
if(REQ ==1){
tx_can=TX;
}
}
}