This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
405 lines (341 loc) · 10.8 KB
/
uart.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// $Id: uart.c,v 1.26 2004-05-13 14:45:46 peter Exp $
#include <msp430x14x.h>
#include <string.h>
//â¨¯ë ¯¥à¥¬¥ëå
#include "type_def.h"
#ifdef STEND
#include "uart_s.h"
#endif // STEND
// ¬®¤ã«ì ¤«ï à ¡®âë á ¯ ª¥â ¬¨
#include "uart_p.h"
//¤ ë¥ ¯ ª¥â®¢
extern u8 packets[MAXQUE*MAXPACKETLEN];
// á«ã¦¥¡ë¥ ¤ ë¥ ¤«ï ¯ ª¥â®¢
extern struct que queue[MAXQUE];
// "inline" äãªæ¨¨ ¤«ï 149 ¯à®æ¥áá®à
#include "m149.h"
#include "global.h"
/* ¬®¤ã«ì à ¡®âë á ¯®á«¥¤®¢ ⥫ìë¬ ¯®à⮬ msp430f149 */
extern u16 temp_hold;
extern u16 timer_sum_sleep;
extern u16 timer_sum_int;
extern u16 timer_hold;
extern u16 timer_sum;
extern u16 sleep;
extern u16 timer_sum_display;
extern u16 timer_sum_serial;
extern u16 timer_sum_adc;
extern u16 timer_sum_stat;
extern u16 why_job;
extern u16 stat_buf[STAT_FIFO_RCV_LEN*SIZE_STAT];
extern unsigned int stat_rcv_fifo_start; /* stat receive buffer start index */
extern volatile unsigned int stat_rcv_fifo_end; /* stat receive buffer end index */
extern u16 stat1_buf[STAT1_FIFO_RCV_LEN*SIZE_STAT1];
extern unsigned int stat1_rcv_fifo_start; /* stat receive buffer start index */
extern volatile unsigned int stat1_rcv_fifo_end; /* stat receive buffer end index */
#ifdef DEBUG_SERIAL
u16 packet_in_fifo;
u16 packet_in_fifo_max;
u16 fifo_trn_depth;
u16 packet_fifo_full;
u16 fifo_trn_depth_max;
u16 error_uart_depth;
u16 error_send_serial;
u16 length_sended_2_fifo;
u16 length_sended_2_fifo_max;
u16 length_sended_2_fifo_min;
u16 error_packets_crc;
u16 jitter_adc;
u16 jitter_adc_max;
u16 jitter_pusk;
#endif //DEBUG_SERIAL
u16 current_rec_packet;
u16 rec_length;
u16 received_packed;
extern unsigned int results0[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
extern unsigned int results1[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
extern unsigned int results2[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
extern unsigned int results3[ADC_FIFO_RCV_LEN*SIZE_OF_ADC_DUMP];
//extern unsigned int results4[ADC_FIFO_RCV_LEN];
//extern unsigned int results5[ADC_FIFO_RCV_LEN];
//extern unsigned int results6[ADC_FIFO_RCV_LEN];
//extern unsigned int results7[ADC_FIFO_RCV_LEN];
//extern unsigned int results8[ADC_FIFO_RCV_LEN];
extern unsigned int results[ADC_FIFO_RCV_LEN];
extern u16 dac[NUM_CHANEL];
extern u16 chanel;
/*
Š®«¨ç¥á⢮ ‡ 票¥
¡ ©â
_____
1 ¯®à浪®¢ë©
®¬¥à
¯ ª¥â
1 ⨯
¯ ª¥â
1 ¤«¨ ¯ ª¥â (¡¥§ ¬ àª¥à ª®æ ¯ ª¥â )
2 CRC
1 x7E - ¬ àª¥à ¯ ª¥â
*/
#define CHANNEL_TO_SET 6 //§ 票¥ ®¬¥à ª « €– ¢ 8-®¬ ¯ ª¥â¥
#define NUM_OF_CHANNEL_DAC 7 //§ 票¥ ®¬¥à ª « –€ ¢ 8-®¬ ¯ ª¥â¥
#define VALVE_OF_CHANNEL_DAC 9 //§ 票¥ ¤«ï –€ ¢ 8-®¬ ¯ ª¥â¥
#define DATA3PACKET 24 //ᬥ饨¥ (á ª®æ ) ¯®«®¦¥¨ï ¢ ¯ ª¥â¥ à §¬¥é¥¨ï ¤ ëå
#define DATA5PACKET (SIZE_STAT*2+6)
#define DATA6PACKET (SIZE_STAT1*2+6)
#define DATA7PACKET (32+6)
#define ESCAPE 0x7D
#define EOFPACKET 0x7E //ª®¤ ¯à¨§ ª ª®æ ª ¤à
volatile unsigned int asp_trn_fifo_start; /* serial transmit buffer start index */
unsigned int asp_trn_fifo_end; /* serial transmit flash buffer end index */
u16 asp_trn_fifo_buf[SERIAL_FIFO_TRN_LEN]; /* storage for serial transmit buffer */
//u8 test[4]={'0','1','2','3'};
u16 last_sended_packet;
#ifdef STEND
//¯à¥àë¢ ¨¥ serial port 0
interrupt[UART0TX_VECTOR] void usart0_tx (void)
{
#else
#ifdef CABLE
//¯à¥àë¢ ¨¥ serial port 1
interrupt[UART1TX_VECTOR] void usart1_tx (void)
{
#else //CABLE
error directives
#endif
#endif
#ifdef CABLE
int temp_led;
#endif
HOLD_TIME_IRQ()
#ifdef CABLE
temp_led=P1OUT;
#endif
#ifdef CABLE
TXBUF1 = asp_trn_fifo_buf[asp_trn_fifo_start++ & (SERIAL_FIFO_TRN_LEN-1)];
#endif //CABLE
#ifdef STEND
TXBUF0 = asp_trn_fifo_buf[asp_trn_fifo_start++ & (SERIAL_FIFO_TRN_LEN-1)];
#endif //STEND
#ifdef DEBUG_SERIAL
if (fifo_trn_depth_max<fifo_trn_depth) fifo_trn_depth_max=fifo_trn_depth;
fifo_trn_depth--;
#endif
if (asp_trn_fifo_start==asp_trn_fifo_end){ // ¥á«¨ ¤ ëå ¡®«ìè¥ ¥â
#ifdef CABLE
IE2 &= ~UTXIE1; // â® § ¯à¥é ¥¬ ¯à¥àë¢. ¯¥à¥¤ ç¨
#endif //CABLE
#ifdef STEND
IE1 &= ~UTXIE0; // â® § ¯à¥é ¥¬ ¯à¥àë¢. ¯¥à¥¤ ç¨
#endif //STEND
#ifdef DEBUG_SERIAL
if (fifo_trn_depth) {error_uart_depth++;fifo_trn_depth=0;}
#endif
//ä¨ä®èª ®¡ã«¨« áì - å¢ â¨â ᯠâì, ¤® ¯¥à¥¤ ¢ âì ¤ ë¥
_BIC_SR_IRQ(CPUOFF); // Clear LPM0, SET BREAKPOINT HERE
}
#ifdef CABLE
P1OUT = temp_led; // return led state
#endif
//¤«ï ®â« ¤ª¨
//®¡ï§ â¥«ì® “€’œ ’Ž’ €’ˆ‘‹ˆ!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
//_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bits from 0(SR)
//!!!!!!!!!!!!!!!!!!!!!!!
SUM_TIME_IRQ_NOSLEEP();
}
u16 esc_rec;
#ifdef STEND
//¯à¥àë¢ ¨¥ serial port 0
interrupt[UART0RX_VECTOR] void usart0_rx (void)
{
#else
#ifdef CABLE
//¯à¥àë¢ ¨¥ serial port 1
interrupt[UART1RX_VECTOR] void usart1_rx (void)
{
#else
error defines stend or cable
#endif //cable
#endif //stend
u16 received_sym;
u16 shift_fifo;
HOLD_TIME_IRQ()
#ifdef CABLE
received_sym=RXBUF1;
#endif //cable
#ifdef STEND
received_sym=RXBUF0;
#endif //stend
if (current_rec_packet>=MAXQUE){
current_rec_packet=hold_packet();
if (current_rec_packet>=MAXQUE) return; // ᢮¡®¤ë© ¯ ª¥â ¥ ¤ «¨
rec_length=0;
}
shift_fifo=current_rec_packet*MAXPACKETLEN+rec_length;
switch(received_sym){
case ESCAPE:
esc_rec=1;
SUM_TIME_IRQ_NOSLEEP();
break;
case EOFPACKET:
queue[current_rec_packet].busy=PACKREC;
queue[current_rec_packet].len=rec_length;
current_rec_packet=MAXQUE;
received_packed++;
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bits from 0(SR)
SUM_TIME_IRQ();
break;
default:
if (esc_rec){
esc_rec=0;
packets[shift_fifo++]=received_sym^0x40;
rec_length++;
if (rec_length>=MAXPACKETLEN){ //¯¥à¥¯®«¥¨¥ ¯à¨¥¬ (¥â EOF)
rec_length=0;
}
}
else{
packets[shift_fifo++]=received_sym;
rec_length++;
if (rec_length>=MAXPACKETLEN){ //¯¥à¥¯®«¥¨¥ ¯à¨¥¬ (¥â EOF)
rec_length=0;
}
}
SUM_TIME_IRQ_NOSLEEP();
break;
}
}
/* ¢ fifo ¯¥à¥¤ çã ¯¥à¥áë« ¥â ¤ ë¥ ¯ ª¥â , á ¯¥à¥ª®¤¨à®¢ ¨¥¬
ESC ¯®á«¥¤®¢ ⥫ì®áâ¨
‚®§¢à é ¥â ª®«¨ç¥á⢮ ¡ ©â, ª®â®àë¥ ¥é¥ ¥ 㤠«®áì ¯¥à¥¤ âì */
u16 send_serial_massiv(u8* data,u16 len){
static u16 escape_sym;
u16 counter;
u16 counter1;
u16 t_end;
u16 t_start;
#ifdef DEBUG_SERIAL
if (len==0){
error_send_serial++;
return 0;
}
#endif
// do {
t_end= asp_trn_fifo_end&(SERIAL_FIFO_TRN_LEN-1);
t_start=asp_trn_fifo_start&(SERIAL_FIFO_TRN_LEN-1);
if (t_end>=t_start) //®ç¥à¥¤ì ¥é¥ ¥ ¯¥à¥è« ç¥à¥§ ªà © ¨ 墮áâ ¢¯¥à¥¤¨
{
counter=SERIAL_FIFO_TRN_LEN-t_end;
if (t_start==0) counter--;
}
else
counter=t_start-t_end-1;
// if (counter==0) break;
if (counter==0) return len;
if (counter>len) counter=len;
counter1=counter;
while(counter){
if (escape_sym){
escape_sym=0;
asp_trn_fifo_buf[t_end++]=(*data++)^0x40;
}
else
if (*data==EOFPACKET||*data==ESCAPE){
escape_sym=1;
asp_trn_fifo_buf[t_end++]=ESCAPE;
len++;
}
else{
asp_trn_fifo_buf[t_end++]=*data++;
}
counter--;
}
len-=counter1;
#ifdef DEBUG_SERIAL
_DINT();
#endif
asp_trn_fifo_end+=counter1;
#ifdef DEBUG_SERIAL
fifo_trn_depth+=counter1;
_EINT(); // Enable interrupts
#endif
disable_int_no_interrupt();
//¥á«¨ ä¨ä®èª ¥ ¯ãáâ ï ¨ ¯à¥àë¢ ¨ï § ¯à¥é¥ë, â® à §à¥è ¥¬ ¨å
if ( ((asp_trn_fifo_end&(SERIAL_FIFO_TRN_LEN-1))!=(asp_trn_fifo_start&(SERIAL_FIFO_TRN_LEN-1)))
//&&
)
#ifdef CABLE
IE2 |= UTXIE1; // ¤ ë¥ ¢ ä¨ä®èª¥ ¥áâì - à §à¥è ¥¬ ¯à¥àë¢ ¨ï ¯¥à¥¤ ç¨
#endif //CABLE
#ifdef STEND
IE1 |= UTXIE0; // ¤ ë¥ ¢ ä¨ä®èª¥ ¥áâì - à §à¥è ¥¬ ¯à¥àë¢ ¨ï ¯¥à¥¤ ç¨
#endif //STEND
enable_int_no_interrupt();
// }while(len);
return len;
}
u8 write_asp_trn_fifo(u8 data_wr){
if ( ( (asp_trn_fifo_end+1)&(SERIAL_FIFO_TRN_LEN-1))== (asp_trn_fifo_start&(SERIAL_FIFO_TRN_LEN-1)) )
return 0;
disable_int_no_interrupt();
asp_trn_fifo_buf[asp_trn_fifo_end++ & (SERIAL_FIFO_TRN_LEN-1)]=data_wr;
#ifdef DEBUG_SERIAL
fifo_trn_depth++;
#endif
#ifdef CABLE
IE2 |= UTXIE1; // ¤ ë¥ ¢ ä¨ä®èª¥ ¥áâì - à §à¥è ¥¬ ¯à¥àë¢ ¨ï ¯¥à¥¤ ç¨
#endif //CABLE
#ifdef STEND
IE1 |= UTXIE0; // ¤ ë¥ ¢ ä¨ä®èª¥ ¥áâì - à §à¥è ¥¬ ¯à¥àë¢ ¨ï ¯¥à¥¤ ç¨
#endif //STEND
enable_int_no_interrupt();
return 1;
}
void work_with_serial(void){
int x;
for (x=0;x<MAXQUE;x++){
if (last_sended_packet<MAXQUE) break;
switch(queue[x].busy){
case NOTSENDED:
last_sended_packet=x;
break;
}//hctiws
}//rof
if (last_sended_packet<MAXQUE){
x=last_sended_packet;
//¯®áë« ¥¬ (᪮«ìª® ¢ ä¨ä®èªã ¯®¬¥áâ¨âáï) ¯ ª¥â
#ifdef DEBUG_SERIAL
length_sended_2_fifo=queue[x].len;
#endif //DEBUG_SERIAL
queue[x].len=send_serial_massiv(&packets[(x+1)*MAXPACKETLEN-queue[x].len],queue[x].len);
#ifdef DEBUG_SERIAL
length_sended_2_fifo-=queue[x].len;
if (length_sended_2_fifo_max<length_sended_2_fifo)
length_sended_2_fifo_max=length_sended_2_fifo;
if (length_sended_2_fifo_min>length_sended_2_fifo)
length_sended_2_fifo_min=length_sended_2_fifo;
//§¤¥áì ¥®¡å®¤¨¬® ¯®¬¥áâ¨âì í⨠¤ ë¥ ¢ fifo áâ â¨á⨪¨
#endif //DEBUG_SERIAL
//¥á«¨ ¢á¥ ®â¯à ¢¨«¨
if (queue[x].len==0){
//¢ëáë« ¥¬ ¬ àª¥à ª®æ ¯ ª¥â
while (write_asp_trn_fifo(EOFPACKET)==0) ;
//¯®¬¥ç ¥¬ ¯ ª¥â ®â¬¥ç¥ë¬ ¨«¨ ®¦¨¤ î饬 ¯®¤â¢¥à¦¤¥¨ï
//®â« ¤ª queue[x].busy=WAIT_ACK;
//¤«ï ®â« ¤ª¨
queue[x].busy=FREEPLACE;
#ifdef DEBUG_SERIAL
packet_in_fifo--;
#endif //DEBUG_SERIAL
//¤«ï ®â« ¤ª¨
//¨é¥¬ á«¥¤ãî騩 ¯ ª¥â ¢ ®ç¥à¥¤¨ ®â¯à ¢ªã
while((++last_sended_packet)<MAXQUE){
switch(queue[last_sended_packet].busy){
// ©¤¥ ¥®â¯à ¢«¥ë© ¯ ª¥â
case NOTSENDED:
return;
}
}//elihw ¤®è«¨ ¤® ª®æ ®ç¥à¥¤¨ - ¯ ª¥â®¢ ®â¯à ¢ªã ¥â
}//fi - ⥪ã騩 ¯ ª¥â ®â¯à ¢«¥ ¥é¥ ¥ ¤® ª®æ
}//fi - ¯®ª ¥â ¯ ª¥â®¢ ¤«ï ®â¯à ¢ª¨
}