summaryrefslogtreecommitdiff
path: root/Command.c
blob: 9363a81e93da0e7255a64fb11dfd93cd858fd024 (plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
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
528
529
530
531
532
533
534
535
536
537
538
539
540
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
568
569
570
571
572
573
574
575
576
577
578
579
580
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
607
608
609
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
637
638
639
640
//////////////////////////////////////////////////////////////////////////////
//
// 	Filename:	Command.c
//	Version:		
//	Data:		
//
//	Author:		Liu, Zemin
//	Company:	JYE Tech
//
//-----------------------------------------------------------------------------
//
// 	Target: 		STM32F103C8 
// 	Tool chain: 	CodeSourcery G++
//
//-----------------------------------------------------------------------------
//	Required files:
//
//-----------------------------------------------------------------------------
//	Notes:
//
//
//-----------------------------------------------------------------------------
//	Revision History:
//
///////////////////////////////////////////////////////////////////////////////
#include "stm32f10x.h"
#include "stm32f10x_conf.h"

#include "Common.h"
#include "Board.h"
#include "Command.h"
#include	"libdso138.h"
#include	"Screen.h"
#include 	"Eeprom.h"

// ===========================================================
// 	File Scope Global variables
// ===========================================================
//
const 	KeyScanCode KScanCodeTab[9] = {
//	scan code	key code		key param
	{0x7FFF, 	KC_SW1,		'1'},		// 1
	{0xBFFF, 	KC_SW2,		'2'},		// 2
	{0xDFFF, 	KC_SW3,		'3'},		// 3
	{0xEFFF, 	KC_SW4,		'4'},		// 4
	{0x7FFE, 	KC_SW1H,	'5'},		// 5
	{0xBFFE, 	KC_SW2H,	'6'},		// 6
	{0xDFFE, 	KC_SW3H,	'7'},		// 7
	{0xEFFE, 	KC_SW4H,	'8'},		// 8
	{0, 			0,			0}		
	};

KEYPAD	Keypad;
U8	VSenPrev, CplPrev;
U16	Flags;


// ===========================================================
//	Function Definitions
// ===========================================================
//
void	AppInit()
{
 U16 tmp0;

// =============================
// Note: DSO_Init() must be executed for proper capture engine running
 
 DSO_Init();

// =============================

 // Check EEPROM for valid settings
 EE_ReadVariable(Addr_SettingStatus, &tmp0);
 if(tmp0 == SettingStatus_Initialized) {
	// Load saved settings
	EE_ReadVariable(VirtAddVarTab[Addr_Vpos], &tmp0);
	SetVPos(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_Vsen], &tmp0);
	SetVSen(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_Cpl], &tmp0);
	SetCpl(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_TimeBase], &tmp0);
	SetTimeBase(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_TrigMode], &tmp0);
	SetTrigMode(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_TrigEdge], &tmp0);
	SetTrigEdge(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_TrigLvl], &tmp0);
	SetTrigLvl(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_RecLen], &tmp0);
	SetRecLen(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_HPos], &tmp0);
	SetHPos(tmp0);
	EE_ReadVariable(VirtAddVarTab[Addr_VPosOfs], &tmp0);
	SetVPosOfs(tmp0);
 	}
 else {
	// Load default settings and initialize EEPROM
 	SetVPos(0);
	EE_WriteVariable(VirtAddVarTab[Addr_Vpos], 0);
 	SetVSen(VS_05V);
	EE_WriteVariable(VirtAddVarTab[Addr_Vsen], VS_05V);
 	SetCpl(CP_DC);
	EE_WriteVariable(VirtAddVarTab[Addr_Cpl], CP_DC);
 	SetTimeBase(TB_1ms);
	EE_WriteVariable(VirtAddVarTab[Addr_TimeBase], TB_1ms);
 	SetTrigMode(TM_Auto);
	EE_WriteVariable(VirtAddVarTab[Addr_TrigMode], TM_Auto);
 	SetTrigEdge(TE_Falling);
	EE_WriteVariable(VirtAddVarTab[Addr_TrigEdge], TE_Falling);
 	SetTrigLvl(0);
	EE_WriteVariable(VirtAddVarTab[Addr_TrigLvl], 0);
 	SetRecLen(SampleBufSizeMax);
	EE_WriteVariable(VirtAddVarTab[Addr_RecLen], SampleBufSizeMax);
 	SetHPos(GetRecLen()/2 - WDsize/2);
	EE_WriteVariable(VirtAddVarTab[Addr_HPos], GetRecLen()/2 - WDsize/2);
	SetVPosOfs(0);
	EE_WriteVariable(VirtAddVarTab[Addr_VPosOfs], 0);

	// Mark down EEPROM has been initialized
	EE_WriteVariable(VirtAddVarTab[Addr_SettingStatus], SettingStatus_Initialized);

 	}

 OutputTLvl();

 // Misc initialization
 TimerKeyScan = 1;

 Keypad.KDebounceVal = KD_val;
 
}

void	KeyProc(void)
{
 switch(Keypad.KeyCode) {
	case KC_SW1:
		DoKeyOk();
	default:	
		break;
		
	case KC_SW2:
		DoKeyInc();
		break;

	case KC_SW3:
		DoKeyDec();
		break;

	case KC_SW4:
		DoKeySel();
		break;

	case KC_SW1H:
		DoKeyOkH();
		break;

	case KC_SW2H:
		DoKeyIncH();
		break;

	case KC_SW3H:
		DoKeyDecH();
		break;

	case KC_SW4H:
		DoKeySelH();
		break;

 	}
}


void KeyScan(void)
{
 U16	tmp1;
 U8	tmp2, tmp3;

 Keypad.KScanBuf = NoKey;
 // Read buttons
 tmp1 = (PB_Port & PB_Bits) | ~PB_Bits;
 if(tmp1 != NoKey) {
	Keypad.KScanBuf = tmp1;
 	}

 
 // -- Debouncing
 if((Keypad.KScanBuf == NoKey) || (Keypad.KScanBuf != Keypad.KScanCode)) {
 	Keypad.KScanCode = Keypad.KScanBuf;
	Keypad.KCount = 0;
	Keypad.KHCount = 0;
	Keypad.KTimeChk = KH_val;
 	}
 else {
	Keypad.KCount++;
	if(Keypad.KCount > Keypad.KDebounceVal) {
		if(Keypad.KCount == Keypad.KDebounceVal + 3) {
			Keypad.KCount = Keypad.KDebounceVal;
			if(++Keypad.KHCount == Keypad.KTimeChk) {
				// Key hold detected
				Keypad.KScanCode &= 0xFFFE;
				KeyConvert((KeyScanCode *)KScanCodeTab, Keypad.KScanCode);
				// Change KTimeChk for key repeat
				Keypad.KTimeChk += KR_Time;
				}
			}
		}
	else if(Keypad.KCount == Keypad.KDebounceVal) {
		// Key push detected
		KeyConvert((KeyScanCode *)KScanCodeTab, Keypad.KScanCode);
		}
	}

#define	Threshold_High		0x0900
#define	Threshold_Low		0x0300

 // Read switch SEN1
 tmp1 = ADC_Poll(ADC2, 2);
 tmp2 = 0;
 if(tmp1 > Threshold_High) {
 	tmp2 = 2;
 	}
 else if(tmp1 > Threshold_Low) {
 	tmp2 = 1;
 	}

 // Read switch SEN2
 tmp1 = ADC_Poll(ADC2, 1);
 tmp3 = 0;
 if(tmp1 > Threshold_High) {
 	tmp3 = 2;
 	}
 else if(tmp1 > Threshold_Low) {
 	tmp3 = 1;
 	}

 // Determine VSen setting
 tmp2 = 3 * tmp2 + tmp3 + VSenMin;
 if(tmp2 != VSenPrev) {
 	SetVSen(tmp2);
	VSenPrev = tmp2;
 	UpdateDisp(Disp_Param);
 	}
 
 // Read switch Cpl
 tmp1 = ADC_Poll(ADC2, 3);
 tmp2 = 0;
 if(tmp1 > Threshold_High) {
 	tmp2 = 2;
 	}
 else if(tmp1 > Threshold_Low) {
 	tmp2 = 1;
 	}
 tmp2 = 2 - tmp2;
 
 // Determine Cpl setting
 if(tmp2 != CplPrev) {
	SetCpl(tmp2);
	CplPrev = tmp2;
 	UpdateDisp(Disp_Param);
 	}
 }

void	KeyConvert(KeyScanCode *KSCTab, U16 KSCode)
{
 U16 tmp1;
 
 while((tmp1 = *(U16 *)(KSCTab + 0))) {
	if(tmp1 == KSCode) {
		// -- Match found
		Keypad.KeyCode = *(U8 *)((U8 *)KSCTab + 2);
		Keypad.KeyCodeBuf = Keypad.KeyCode;
		Keypad.KeyParam = *(U8 *)((U8 *)KSCTab + 3);
		return;
		}
	else {
		// -- Proceed to next entry
		KSCTab = (KeyScanCode *)((U8 *)KSCTab + sizeof(KeyScanCode));
		}
	
 	}
}

void	DoKeyOk(void)
{
 U16 tmp;
 
 tmp = GetDsoStatus();
 // Toggle HOLD state
 BitXor(tmp, DSO_Hold);

 if(BitTest(tmp, DSO_Hold)) {
 	// Set HOLD 
 	SetHold();
 	// Stop capture
 	StopCapture();
 	}
 else {
 	// Clear HOLD
 	ClrHold();
 	// Start capture at exit of HOLD
	StartCapture();
 	}
 
 UpdateDisp(Disp_Param);
}

void	DoKeyInc(void)
{
 S8	tmp0;
 S16	tmp1;
 
 switch(GetFocus()) {
	case FC_Timebase:
 		tmp0 = GetTimebase();
 		tmp0++;
 		tmp0 = SetTimeBase(tmp0);
		EE_WriteVariable(VirtAddVarTab[Addr_TimeBase], tmp0);
		if(tmp0 >= TB_20ms) {
			// Restart capture
			StartCapture();
			}
		else {
			// Change sampling rate only
			UpdateTimebase();
			}
		// Make key debounce time shorter for these TB's 
		if((tmp0 < TB_20ms) && (tmp0 > TB_1s)) {
			Keypad.KDebounceVal = KD_val1;
			}
		else {
			Keypad.KDebounceVal = KD_val;
			}
	default:	
		break;
		
	case FC_TrigMode:
 		tmp0 = GetTrigMode();
 		tmp0++;
 		tmp0 = SetTrigMode(tmp0);
		EE_WriteVariable(VirtAddVarTab[Addr_TrigMode], tmp0);
		// Restart capture. 
		StartCapture();
		break;

	case FC_TrigEdge:
 		tmp0 = GetTrigEdge();
 		tmp0++;
 		tmp0 = SetTrigEdge(tmp0);
		EE_WriteVariable(VirtAddVarTab[Addr_TrigEdge], tmp0);
		break;
		
	case FC_VPos:
 		tmp1 = GetVPos();
 		tmp1++;
 		tmp1 = SetVPos(tmp1);
		EE_WriteVariable(VirtAddVarTab[Addr_Vpos], tmp1);
 		UpdateDisp(Disp_Trace);
		break;

	case FC_TrigLvl:
 		tmp1 = GetTrigLvl();
 		tmp1++;
 		tmp1 = SetTrigLvl(tmp1);
		EE_WriteVariable(VirtAddVarTab[Addr_TrigLvl], tmp1);
		OutputTLvl();
		break;
		
	case FC_HPos:
		// Move waveform right
 		tmp1 = GetHPos();
 		tmp1--;
 		tmp1 = SetHPos(tmp1);
		EE_WriteVariable(VirtAddVarTab[Addr_HPos], tmp1);
 		UpdateDisp(Disp_Trace);
		break;
		
 	}
 
 UpdateDisp(Disp_Param);
}

void	DoKeyDec(void)
{
 S8	tmp0;
 S16	tmp1;
 
 switch(GetFocus()) {
	case FC_Timebase:
 		tmp0 = GetTimebase();
 		tmp0--;
 		tmp0 = SetTimeBase(tmp0);
		EE_WriteVariable(VirtAddVarTab[Addr_TimeBase], tmp0);
		if(tmp0 >= TB_50ms) {
			// Restart capture
//			UartPutc('7', USART1);
			StartCapture();
//			UartPutc('1', USART1);
			}
		else {
			// Change sampling rate only
			UpdateTimebase();
			
			}
/*		
		// Make key debounce time shorter for these TB's 
		if((tmp0 < TB_20ms) && (tmp0 > TB_1s)) {
			Delay(50000);	// To avoid same keypress repeated
			Keypad.KDebounceVal = KD_val1;
			}
		else {
			Keypad.KDebounceVal = KD_val;
			}
*/		
	default:	
		break;
		
	case FC_TrigMode:
 		tmp0 = GetTrigMode();
 		tmp0--;
 		tmp0 = SetTrigMode(tmp0);
		EE_WriteVariable(VirtAddVarTab[Addr_TrigMode], tmp0);
		// Restart capture. 
		StartCapture();
		break;

	case FC_TrigEdge:
 		tmp0 = GetTrigEdge();
 		tmp0--;
 		tmp0 = SetTrigEdge(tmp0);
		EE_WriteVariable(VirtAddVarTab[Addr_TrigEdge], tmp0);
		break;
		
	case FC_VPos:
 		tmp1 = GetVPos();
 		tmp1--;
 		tmp1 = SetVPos(tmp1);
		EE_WriteVariable(VirtAddVarTab[Addr_Vpos], tmp1);
 		UpdateDisp(Disp_Trace);
		break;

	case FC_TrigLvl:
 		tmp1 = GetTrigLvl();
 		tmp1--;
 		tmp1 = SetTrigLvl(tmp1);
		EE_WriteVariable(VirtAddVarTab[Addr_TrigLvl], tmp1);
		OutputTLvl();
		break;
		
	case FC_HPos:
		// Move waveform left
 		tmp1 = GetHPos();
 		tmp1++;
 		tmp1 = SetHPos(tmp1);
		EE_WriteVariable(VirtAddVarTab[Addr_HPos], tmp1);
 		UpdateDisp(Disp_Trace);
		break;
		
 	}
 
 UpdateDisp(Disp_Param);
}

void	DoKeySel(void)
{
 U8 tmp;

 tmp = GetFocus();
 tmp++;
 if(tmp >= FC_Max) {
 	tmp = 0;
 	}
 SetFocus(tmp);

 UpdateDisp(Disp_Param);
}

void	DoKeyOkH(void)
{
 S16 tmp1;
 
 if(GetFocus() == FC_VPos) {
	// Do VPos alignment
	tmp1 = (S16)(GetAverage() - WWindowMidValue);
	SetVPosOfs(tmp1);
	EE_WriteVariable(VirtAddVarTab[Addr_VPosOfs], tmp1);
 	}
}

void	DoKeyIncH(void)
{
 S16	tmp1;
 
 switch(GetFocus()) {
	case FC_VPos:
 		tmp1 = GetVPos();
 		tmp1 += 10;
 		SetVPos(tmp1);
		break;

	case FC_TrigLvl:
 		tmp1 = GetTrigLvl();
 		tmp1 += 10;
 		SetTrigLvl(tmp1);
		OutputTLvl();
		break;
		
	case FC_HPos:
		// Move waveform right
 		tmp1 = GetHPos();
 		tmp1 -= 20;
 		SetHPos(tmp1);		
		break;
 	}

 UpdateDisp(Disp_Param);
}

void	DoKeyDecH(void)
{
 S16	tmp1;
 
 switch(GetFocus()) {
	case FC_VPos:
 		tmp1 = GetVPos();
 		tmp1 -= 10;
 		SetVPos(tmp1);
		break;

	case FC_TrigLvl:
 		tmp1 = GetTrigLvl();
 		tmp1 -= 10;
 		SetTrigLvl(tmp1);
		OutputTLvl();
		break;
		
	case FC_HPos:
		// Move waveform left
 		tmp1 = GetHPos();
 		tmp1 += 20;
 		SetHPos(tmp1);
		break;
 	}

 UpdateDisp(Disp_Param);
}

void	DoKeySelH(void)
{

}

void	LedBlink(void)
{
 U16	tmp;
 
 // Turn on LED
 Port_BitClr(LED_Base, (1 << LED_Bit));
 tmp = 20;
 while(tmp) {
 	Delay(65000);
 	tmp--;
 	}
 
 // Turn off LED
 Port_BitSet(LED_Base, (1 << LED_Bit));
 tmp = 20;
 while(tmp) {
 	Delay(65000);
 	tmp--;
 	}

 // Turn on LED
 Port_BitClr(LED_Base, (1 << LED_Bit));
 tmp = 20;
 while(tmp) {
 	Delay(65000);
 	tmp--;
 	}
 
 // Turn off LED
 Port_BitSet(LED_Base, (1 << LED_Bit));
}

void	TestMode(void)
{
 U16 tmp;
 
 // Change system clock to HSI
 RCC->CFGR &= ~RCC_CFGR_SW;
 RCC->CFGR |= RCC_CFGR_SW_HSI;

 // Disable JTAG and SWD
 AFIO->MAPR &= ~AFIO_MAPR_SWJ_CFG;
 AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_2;

 // Remap PD0 & PD1 to make them available as GPIO
 AFIO->MAPR |= AFIO_MAPR_PD01_REMAP;

 // Set all ports to output mode
 GPIOA->CRL = 0x33333333;
 GPIOA->CRH = 0x33333333;

 GPIOB->CRL = 0x33333333;
 GPIOB->CRH = 0x33333333;

 GPIOC->CRL = 0x33333333;
 GPIOC->CRH = 0x33333333;
 
 GPIOD->CRL = 0x33333333;
 GPIOD->CRH = 0x33333333;

 // Blink LEDs
 while(1) {
	GPIOA->ODR = 0x5555;
	GPIOB->ODR = 0x5555;
	GPIOC->ODR = 0x5555;
	GPIOD->ODR = 0x5555;

	tmp = 5;
	while(tmp) {
		Delay(60000);
		tmp--;
		}

	GPIOA->ODR = 0xAAAA;
	GPIOB->ODR = 0xAAAA;
	GPIOC->ODR = 0xAAAA;
	GPIOD->ODR = 0xAAAA;

	tmp = 5;
	while(tmp) {
		Delay(60000);
		tmp--;
		}
 	}
 
}