src/main.cpp

changeset 19
a52d60613cf7
parent 18
4fd621551d55
child 21
9ffa9d727d80
equal deleted inserted replaced
18:4fd621551d55 19:a52d60613cf7
10 #include "Arial12x12.h" 10 #include "Arial12x12.h"
11 11
12 #include "SSD1322.h" 12 #include "SSD1322.h"
13 #include "hp34comm.h" 13 #include "hp34comm.h"
14 14
15 #include "QEI.h"
15 #include "Keypad.h" 16 #include "Keypad.h"
16 17
18 /******************************************************/
19 /* L432KC */
20 /******************************************************/
17 #if (defined STM32L432xx) 21 #if (defined STM32L432xx)
18 // display 22 // display
19 #define DSP_MOSI PA_7 //A6 23 #define DSP_MOSI PA_7 //A6
20 #define DSP_MISO PA_6 //A5 24 #define DSP_MISO PA_6 //A5
21 #define DSP_SCLK PA_5 //A4 25 #define DSP_SCLK PA_5 //A4
22 #define DSP_CS PB_0 //D3 26 #define DSP_CS PB_0 //D3
23 #define DSP_RST PB_5 //D11 27 #define DSP_RST PB_5 //D11
24 #define DSP_DC PB_4 //D12 28 #define DSP_DC PB_4 //D12
29
25 // UART for RX (CPU->DP) and TX (DP->CPU) combined 30 // UART for RX (CPU->DP) and TX (DP->CPU) combined
26 #define HP_TX PA_9 // serial1 TX 31 #define HP_TX PA_9 // serial1 TX
27 #define HP_RX PA_10 // serial1 RX 32 #define HP_RX PA_10 // serial1 RX
33
34 // RST pin (handle this by hand)
35 #define HP_RST PA_0
36
28 // misc 37 // misc
29 #define DBGPIN PA_12 38 #define DBGPIN NC
30 39
31 #elif (defined STM32F303x8) 40 // encoder
32 // display 41 #define KP_ENC1 PA_8
33 #define DSP_MOSI PA_7 //A6 42 #define KP_ENC2 PA_11
34 #define DSP_MISO PA_6 //A5 43
35 #define DSP_SCLK PA_5 //A4 44 // keypad
36 #define DSP_CS PA_4 45 #define KP_R0 PA_1
37 #define DSP_RST PB_5 //D11 46 #define KP_R1 PA_3
38 #define DSP_DC PB_4 //D12 47 #define KP_R2 PA_4
39 // UART for RX (CPU->DP) and TX (DP->CPU) combined 48 #define KP_R3 PA_12
40 #define HP_RX PA_10 // serial1 RX 49
41 // misc 50 #define KP_C0 D4
42 #define DBGPIN PA_12 51 #define KP_C1 D5
43 52 #define KP_C2 D6
53 #define KP_C3 D7
54 #define KP_C4 D8
55
56 /******************************************************/
57 /* F446RE */
58 /******************************************************/
44 #elif defined STM32F446xx 59 #elif defined STM32F446xx
60 // UART
61 // USBTX PA_2
62 // USBRX PA_3
45 // display 63 // display
46 #define DSP_MOSI PB_15 64 #define DSP_MOSI PB_15
47 #define DSP_MISO PB_14 65 #define DSP_MISO NC
48 #define DSP_SCLK PB_13 66 #define DSP_SCLK PB_13
49 #define DSP_CS PB_12 67 #define DSP_CS PB_12
50 #define DSP_RST D11 68 #define DSP_RST PB_5
51 #define DSP_DC D12 69 #define DSP_DC PB_4
70
52 // UART for RX (CPU->DP) 71 // UART for RX (CPU->DP)
53 #define HP_RX PA_1 // serial4 RX 72 #define HP_RX PC_11 // serial3 RX
54 // UART for TX (DP->CPU) 73 #define HP_TX PC_10 // serial3 TX
55 #define HP_TX PC_7 // serial6 RX 74
75 // RST pin (handle this by hand)
76 #define HP_RST PC_12
77
78 // encoder
79 #define KP_ENC1 PC_4
80 #define KP_ENC2 PC_5
81
82 // keypad
83 #define KP_R0 PC_0 // I-6
84 #define KP_R1 PC_1 // II-5
85 #define KP_R2 PC_2 // I-5
86 #define KP_R3 PC_3 // II-4
87
88
89 #define KP_C0 PB_0 // I-4
90 #define KP_C1 PA_6 // I-2
91 #define KP_C2 PA_7 // I-3
92 #define KP_C3 PA_5 // I-1
93 #define KP_C4 PD_2 // II-1
56 94
57 // misc 95 // misc
58 #define DBGPIN PC_0 96 #define DBGPIN PC_6
59 97
60 #endif 98 #endif
61 99
62 #ifdef HAVE_PC 100 #ifdef HAVE_PC
63 Serial pc(USBTX, USBRX); 101 Serial pc(USBTX, USBRX);
64 #endif 102 #endif
65 103
66 SSD1322 *dsp;//(10000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS, DSP_RST, DSP_DC, "SSD1322"); 104 SSD1322 *dsp;
67 HPSerial *hp; //(HP_RX, HP_TX); 105 HPSerial *hp;
68 volatile uint8_t must_refresh; 106 volatile uint8_t must_refresh;
69 Thread tdsp, tloop; 107 Thread tdsp, tloop;
70 Timeout blinker; 108 Ticker blinker;
109 Timeout rst_delay;
110 Timeout splashscreen_timer;
71 DigitalOut led(LED1); 111 DigitalOut led(LED1);
72 112 InterruptIn rst(HP_RST);
73 Keypad kpad(std::vector<int>({(int)PA_0, (int)PA_1}), 113
74 std::vector<int>({(int)PA_8, (int)PA_11})); 114 QEI qenc(KP_ENC1, KP_ENC2, NC, 16);
75 115 volatile uint8_t knob;
116 volatile bool splashscreen;
117
118
119 typedef enum {
120 KEY_NONE=0,
121 KEY_PRESSED,
122 KEY_RELEASED
123 } key_event_t;
124
125 typedef struct keycode {
126 uint8_t row;
127 uint8_t col;
128 key_event_t keyevent;
129 } keycode_t;
130
131 volatile keycode_t cur_keycode;
132
133 PinName kp_rows[] = {
134 KP_R0,
135 KP_R1,
136 KP_R2,
137 KP_R3
138 };
139
140 PinName kp_colums[] = {
141 KP_C0, KP_C1,
142 KP_C2, KP_C3,
143 KP_C4
144 };
145
146 DigitalIn kp_in[] = {
147 KP_R0,
148 KP_R1,
149 KP_R2,
150 KP_R3
151 };
152
153 DigitalOut kp_out[] = {
154 KP_C0, KP_C1,
155 KP_C2, KP_C3,
156 KP_C4
157 };
158
159 uint8_t kp_nrows = sizeof(kp_in)/sizeof(kp_in[0]);
160 uint8_t kp_ncols = sizeof(kp_out)/sizeof(kp_out[0]);
161
162 void kp_cb(uint8_t row, uint8_t col);
163 void kr_cb(uint8_t row, uint8_t col);
164
165 Keypad *kpad;
76 uint8_t curchar; 166 uint8_t curchar;
77 uint8_t cmd; 167 uint8_t cmd;
78 uint8_t nchars; 168 uint8_t nchars;
79 char buffer[MAX_BUFF+1]; 169 char buffer[MAX_BUFF+1];
80 170
81 void timeout_h() { 171 void timeout_h() {
82 led = !led; 172 led = !led;
83 blinker.attach(&timeout_h, 0.5f);
84 } 173 }
85 174
86 typedef struct _DSP 175 typedef struct _DSP
87 { 176 {
88 uint8_t cmd; 177 uint8_t cmd;
159 { 248 {
160 // flag, zone, x0, y0, msg, icon 249 // flag, zone, x0, y0, msg, icon
161 // right-side icons area 250 // right-side icons area
162 { 0x00, 0x02, 0, 0, NULL, icon_alarm}, // F1.1 251 { 0x00, 0x02, 0, 0, NULL, icon_alarm}, // F1.1
163 { 0x01, 0x02, 0, 11, NULL, icon_curve}, // F1.2 252 { 0x01, 0x02, 0, 11, NULL, icon_curve}, // F1.2
164 253
165 254
166 { 0x03, 0x04, 1, 2, "Channel"}, // F1.4 255 { 0x03, 0x04, 1, 2, "Channel"}, // F1.4
167 256
168 // col 1 257 // col 1
169 { 0x34, 0x10, 0, 0, "MON"}, // F4.5 258 { 0x34, 0x10, 0, 0, "MON"}, // F4.5
180 { 0x40, 0x040, 0, 8, "RMT"}, // F4.1 269 { 0x40, 0x040, 0, 8, "RMT"}, // F4.1
181 270
182 // col 4 271 // col 4
183 { 0x26, 0x080, 0, 0, "EXT"}, // F3.7 272 { 0x26, 0x080, 0, 0, "EXT"}, // F3.7
184 { 0x25, 0x080, 0, 8, "ONCE"}, // F3.6 273 { 0x25, 0x080, 0, 8, "ONCE"}, // F3.6
185 274
186 { 0xFF, 0x080, 0, 16, "MEM"}, 275 { 0xFF, 0x080, 0, 16, "MEM"},
187 276
188 277
189 // col 5 278 // col 5
190 { 0x14, 0x100, 0, 0, "4W"}, // F2.5 279 { 0x14, 0x100, 0, 0, "4W"}, // F2.5
191 { 0x15, 0x100, 0, 8, "OC"}, // F2.6 280 { 0x15, 0x100, 0, 8, "OC"}, // F2.6
192 { 0xFF, 0x100, 0, 16, "LST"}, // F3.8? Nope 281 { 0xFF, 0x100, 0, 16, "LST"}, // F3.8? Nope
193 { 0xFF, 0x100, 0, 16, "MIN"}, 282 { 0xFF, 0x100, 0, 16, "MIN"},
240 dbgpin = 0; 329 dbgpin = 0;
241 wait_us(2); 330 wait_us(2);
242 } 331 }
243 if (stayup) 332 if (stayup)
244 dbgpin = 1; 333 dbgpin = 1;
245
246 } 334 }
247 #else 335 #else
248 inline void pulse(uint8_t count=1, bool stayup=false) 336 inline void pulse(uint8_t count=1, bool stayup=false)
249 {} 337 {}
250 #endif 338 #endif
251 339
252 340 // callbacks & thread functions
253 341 void loop();
254 void copy_to_lcd(void); 342 void copy_to_lcd(void);
255 void test_dsp(); 343 void test_dsp();
344 void reset(void);
345 void reset_irq(void);
346 void qei_cb(int dir);
347 void end_splashscreen(void);
348
349
256 350
257 void setup() { 351 void setup() {
258 blinker.attach(&timeout_h, 0.5f);
259
260 // init the LCD
261 dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS, DSP_RST, DSP_DC, "SSD1322");
262 //dsp.set_orientation(3);
263 #ifdef HAVE_PC 352 #ifdef HAVE_PC
264 pc.baud (115200); 353 pc.baud (115200);
265 #endif 354 #endif
266 printf("\n\nSystem Core Clock = %.3f MHZ\r\n", 355
356 printf("\n\nSETUP\n");
357 printf(" System Core Clock = %.3f MHZ\r\n",
267 (float)SystemCoreClock/1000000); 358 (float)SystemCoreClock/1000000);
268 359
360 //printf("Attaching Led 1: %d\n", LED1);
361 //blinker.attach(callback(timeout_h), 0.5f);
362
363 hp = NULL;
364 printf("Setup HP communication pins\n");
365 printf(" HP_RX=%d\n", HP_RX);
366 DigitalIn(HP_RX).mode(PullDown);
367 printf(" HP_TX=%d\n", HP_TX);
368 DigitalOut(HP_TX).write(1);
369 printf(" HP_RST=%d\n", HP_RST);
370 DigitalIn(HP_RST).mode(PullDown);
371
372 printf(" setup QEI pins\n");
373 printf(" ENC1=%d\n", KP_ENC1);
374 DigitalIn(KP_ENC1).mode(PullUp);
375 printf(" ENC2=%d\n", KP_ENC2);
376 DigitalIn(KP_ENC2).mode(PullUp);
377 qenc.attach(&qei_cb);
378
379 printf(" setup Keypad\n");
380 cur_keycode.keyevent = KEY_NONE;
381 uint8_t nrows = sizeof(kp_rows)/sizeof(kp_rows[0]);
382 uint8_t ncols = sizeof(kp_colums)/sizeof(kp_colums[0]);
383
384 kpad = new Keypad(nrows, kp_in, ncols, kp_out);
385 printf(" attach Keypad callbacks\n");
386 kpad->attach(&kp_cb, &kr_cb);
387 printf(" start Keypad\n");
388 kpad->start();
389
390 printf("Setup OLED display\n");
391 splashscreen = true;
392 // init the LCD
393 printf(" DSP_MOSI=%d\n", DSP_MOSI);
394 printf(" DSP_MISO=%d\n", DSP_MISO);
395 printf(" DSP_SCLK=%d\n", DSP_SCLK);
396 printf(" DSP_CS=%d\n", DSP_CS);
397 printf(" DSP_RST=%d\n", DSP_RST);
398 printf(" DSP_DC=%d\n", DSP_DC);
399 dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS,
400 DSP_RST, DSP_DC, "SSD1322");
401 //dsp.set_orientation(3);
402 printf(" configure DSP\n");
269 // myLCD.set_font((unsigned char*) Terminal6x8); 403 // myLCD.set_font((unsigned char*) Terminal6x8);
270 // myLCD.claim(stdout); // send stdout to the LCD display 404 // myLCD.claim(stdout); // send stdout to the LCD display
271 // myLCD.claim(stderr); // send stderr to the LCD display 405 // myLCD.claim(stderr); // send stderr to the LCD display
272 dsp->background(Black); // set background to black 406 dsp->background(Black); // set background to black
273 dsp->foreground(0xF); 407 dsp->foreground(0xF);
274 dsp->cls(); 408 dsp->cls();
275 409
276 cmd = 0xFF; 410 cmd = 0xFF;
277 curchar = 0; 411 curchar = 0;
278 nchars = 0; 412 nchars = 0;
279 413
280 for (uint8_t i=0; i<sizeof(table)/sizeof(table[0]); ++i) 414 for (uint8_t i=0; i<sizeof(table)/sizeof(table[0]); ++i)
281 memset(table[i].buffer, 0, MAX_BUFF+1); 415 memset(table[i].buffer, 0, MAX_BUFF+1);
282 416
417 printf(" display splash screen\n");
283 dsp->locate(30, 10); 418 dsp->locate(30, 10);
284 dsp->set_font((unsigned char*)Mono19x27); 419 dsp->set_font((unsigned char*)Mono19x27);
285 dsp->printf("HP34970A"); 420 dsp->printf("HP34970A");
286 dsp->set_font((unsigned char*)Terminal6x8); 421 dsp->set_font((unsigned char*)Terminal6x8);
287 dsp->locate(90, 40); 422 dsp->locate(90, 40);
288 dsp->printf("David Douard"); 423 dsp->printf("David Douard");
289 dsp->copy_to_lcd(); 424 dsp->copy_to_lcd();
290 wait(2); 425
291 dsp->cls(); 426 printf("Starting LCD thread\n");
292 427 tdsp.start(&copy_to_lcd);
293 hp = new HPSerial(HP_TX, HP_RX); 428
294 //hp = NULL; 429 printf("Starting Event thread\n");
430 tloop.start(&loop);
431
432 printf("Attaching timers\n");
433 splashscreen_timer.attach(callback(&end_splashscreen), 2);
434 rst.fall(&reset_irq);
435
436 printf("SETUP DONE\n");
437 }
438
439 void end_splashscreen(void)
440 {
441 printf("End of splash screen\n");
442 splashscreen = false;
443 }
444
445 void reset_irq(void)
446 {
447 rst_delay.attach(callback(&reset), 0.1);
448 }
449
450 void reset(void)
451 {
452 if (DigitalIn(HP_RST).read() == 0) {
453 if (hp == NULL) {
454 printf("setup HP communication handler\n");
455 hp = new HPSerial(HP_TX, HP_RX);
456 }
457
458 printf("!! RST !! (gstate=%d, state=%d)\n",
459 hp->gstate(), hp->state());
460 //printf("Value is ... %X\n", hp->search());
461 hp->startup();
462 }
295 } 463 }
296 464
297 void copy_to_lcd(void) { 465 void copy_to_lcd(void) {
298 uint8_t mask=1; 466 //uint8_t mask=1;
299 467
300 while(1) { 468 while(1) {
301 pulse(0, true); 469 pulse(0, true);
302 if (must_refresh) { 470 if ((splashscreen == false) && (must_refresh)) {
303 must_refresh = 0; 471 must_refresh = 0;
304 //Thread::wait(20); // give a bit of time for some more cmds 472 //Thread::wait(20); // give a bit of time for some more cmds
305 dsp->copy_to_lcd(); 473 dsp->copy_to_lcd();
306 } 474 }
307 475
308 /* 476 /*
309 if (must_refresh & mask) { 477 if (must_refresh & mask) {
323 pulse(0, false); 491 pulse(0, false);
324 Thread::wait(30); 492 Thread::wait(30);
325 } 493 }
326 } 494 }
327 495
328 void show(uint8_t cmd, char *txt, uint8_t nchar=0) { 496 void show(uint8_t cmd, const char *intxt, uint8_t nchar=0) {
329 uint8_t i, len; 497 uint8_t i, len;
330 uint16_t bgcolor, fgcolor; 498 uint16_t bgcolor, fgcolor;
331 char *oldv; 499 char *oldv;
332 500 char *txt;
501
502
503 txt = (char *)malloc(strlen(intxt)+1);
504 strcpy(txt, intxt);
333 pulse(1, true); 505 pulse(1, true);
334 506
335 len = MAX_BUFF; 507 len = MAX_BUFF;
336 508
337 for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) { 509 for (i=0; i<sizeof(table)/sizeof(table[0]); ++i) {
357 break; 529 break;
358 } 530 }
359 if (txt[k] == 0x09) { // \t is a special char for 'unselected' display value 531 if (txt[k] == 0x09) { // \t is a special char for 'unselected' display value
360 txt[k] = 0x00; 532 txt[k] = 0x00;
361 dsp->printf(txt); 533 dsp->printf(txt);
362 534
363 if (fgcolor == table[i].color) 535 if (fgcolor == table[i].color)
364 fgcolor /= 2; 536 fgcolor /= 2;
365 else 537 else
366 fgcolor = table[i].color; 538 fgcolor = table[i].color;
367 dsp->foreground(fgcolor); 539 dsp->foreground(fgcolor);
465 for(uint8_t j=0; j<table[i].maxsize; j++) 637 for(uint8_t j=0; j<table[i].maxsize; j++)
466 oldv[j] = txt[j]; 638 oldv[j] = txt[j];
467 break; 639 break;
468 } 640 }
469 } 641 }
470 642 free(txt);
471 //dsp->copy_to_lcd(); 643 //dsp->copy_to_lcd();
472 pulse(1, false); 644 pulse(1, false);
473 } 645 }
474 646
475 void test_dsp() 647 void test_dsp()
508 wait(2); 680 wait(2);
509 dsp->cls(); 681 dsp->cls();
510 } 682 }
511 683
512 684
513 void loop() { // run over and over 685 void loop()
686 { // run over and over
514 unsigned int err[8]; 687 unsigned int err[8];
515 for (uint8_t i=0; i<8; i++) 688 for (uint8_t i=0; i<8; i++)
516 err[i] = 0; 689 err[i] = 0;
690 int p, pp;
691 p=0;
692 pp=0;
517 while(1) { 693 while(1) {
518 if (hp->cmd_available()) 694 if (knob != 0)
519 { 695 {
520 led = 1; 696 if (hp != NULL) {
521 HPSerial::CMD cmd; 697 printf("Sending keycode %X\n", knob);
522 if (hp->pop(cmd)) 698 hp->sendkey(knob);
523 { 699 printf(" DONE\n");
524 for (uint8_t i=0; i<7; i++) 700 }
525 if (hp->nerrors(i) > err[i]) { 701 knob = 0;
526 printf("ERR: %d/%d/%d/%d/%d/%d/%d\n", 702 }
527 hp->nerrors(0), 703
528 hp->nerrors(1), 704 if (cur_keycode.keyevent != KEY_NONE)
529 hp->nerrors(2), 705 {
530 hp->nerrors(3), 706 printf("Keycode %dx%d: %s\n",
531 hp->nerrors(4), 707 cur_keycode.row, cur_keycode.col,
532 hp->nerrors(5), 708 cur_keycode.keyevent==KEY_PRESSED?"pressed":"released");
533 hp->nerrors(6) 709 cur_keycode.keyevent = KEY_NONE;
534 ); 710 }
535 break; 711
536 } 712 p = qenc.getPulses();
537 for (uint8_t i=0; i<7; i++) 713 if (p != pp) {
538 err[i] = hp->nerrors(i); 714 printf("Pulses = %d\n", p);
539 printf("CMD[%d] %02X", cmd.id, cmd.cmd); 715 pp = p;
540 716 }
541 if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C)) 717 if ((hp != NULL) && (hp->cmd_available()))
542 printf(": '%s'\n", cmd.value); 718 {
543 else { 719 led = 1;
544 printf(":"); 720 HPSerial::CMD cmd;
545 for (uint8_t i=0; i<cmd.size; i++) 721 if (hp->pop(cmd))
546 printf("%02x ", cmd.value[i]); 722 {
547 printf("\n"); 723 for (uint8_t i=0; i<7; i++)
548 } 724 if (hp->nerrors(i) > err[i]) {
549 show(cmd.cmd, cmd.value, cmd.size); 725 printf("ERR: %d/%d/%d/%d/%d/%d/%d\n",
550 } 726 hp->nerrors(0),
727 hp->nerrors(1),
728 hp->nerrors(2),
729 hp->nerrors(3),
730 hp->nerrors(4),
731 hp->nerrors(5),
732 hp->nerrors(6)
733 );
734 break;
735 }
736 for (uint8_t i=0; i<7; i++)
737 err[i] = hp->nerrors(i);
738 printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd);
739
740 if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C))
741 printf(": '%s'\n", cmd.value);
742 else {
743 printf(":");
744 for (uint8_t i=0; i<cmd.size; i++)
745 printf("%02x ", cmd.value[i]);
746 printf("\n");
747 }
748 show(cmd.cmd, cmd.value, cmd.size);
749 }
551 else 750 else
552 printf("\n"); 751 printf("\n");
553 752
554 show(cmd.cmd, cmd.value, cmd.size); 753 show(cmd.cmd, cmd.value, cmd.size);
555 754
556 led = 0; 755 led = 0;
557 } 756 }
558 else 757 else
559 Thread::wait(1); 758 Thread::wait(1);
560 } 759 }
561 } 760 }
562 761
762 void qei_cb(int dir)
763 {
764 if(dir == 1) // turn right
765 knob = 0x80;
766 else // turn left
767 knob = 0x83;
768 }
769
770 void kp_cb(uint8_t row, uint8_t col)
771 {
772 cur_keycode.row = row;
773 cur_keycode.col = col;
774 cur_keycode.keyevent = KEY_PRESSED;
775 }
776
777 void kr_cb(uint8_t row, uint8_t col)
778 {
779 cur_keycode.row = row;
780 cur_keycode.col = col;
781 cur_keycode.keyevent = KEY_RELEASED;
782 }
783
563 int main() 784 int main()
564 { 785 {
565 setup(); 786 setup();
566 787 printf("Main loop (noop)\n");
567 tdsp.start(copy_to_lcd); 788 while(1)
568 789 wait(.1);
569 test_dsp(); 790 }
570
571 tloop.start(loop);
572 while(1);
573 //loop();
574 }

mercurial