src/main.cpp

changeset 32
bc1d6ecbb0cc
parent 28
424d792fea4f
child 34
94ab7ff42a1b
equal deleted inserted replaced
31:794401fbd5d5 32:bc1d6ecbb0cc
13 #include "hp34comm.h" 13 #include "hp34comm.h"
14 14
15 #include "QEI.h" 15 #include "QEI.h"
16 #include "Keypad.h" 16 #include "Keypad.h"
17 17
18 18 // Pins and device declarations
19 /******************************************************/ 19 #if defined TARGET_NUCLEO_F446RE
20 /* F446RE */ 20 #include "def_f446re.h"
21 /******************************************************/ 21 #elif defined TARGET_HP34970_FP_F303RD
22 #if defined STM32F446xx 22 #include "def_hp34970_fp.h"
23 // UART 23 #endif
24 // USBTX PA_2 24
25 // USBRX PA_3 25 #include "platform/CircularBuffer.h"
26 // display 26
27 #define DSP_MOSI PB_15 // blue
28 #define DSP_MISO PB_14 // NC
29 #define DSP_SCLK PB_13 // yellow
30 #define DSP_CS PB_12 // green
31 #define DSP_RST PB_5 // green
32 #define DSP_DC PB_4 // red
33
34 // UART for RX (CPU->DP)
35 #define HP_RX PC_11 // serial3 RX
36 #define HP_TX PC_10 // serial3 TX
37
38 // RST pin (handle this by hand)
39 #define HP_RST PC_12
40
41 // encoder
42 #define KP_ENC1 PC_4
43 #define KP_ENC2 PC_5
44
45 // keypad
46 #define KP_R0 PC_0 // I-6
47 #define KP_R1 PC_1 // II-5
48 #define KP_R2 PC_2 // I-5
49 #define KP_R3 PC_3 // II-4
50
51
52 #define KP_C0 PB_0 // I-4
53 #define KP_C1 PA_6 // I-2
54 #define KP_C2 PA_7 // I-3
55 #define KP_C3 PA_10 // I-1
56 #define KP_C4 PD_2 // II-1
57
58 // misc
59 #define DBGPIN PC_6
60
61 #elif defined STM32F303xE // HP34970 replacement PCB
62
63 #define DSP_MOSI PA_7
64 #define DSP_MISO PA_6 // NC
65 #define DSP_SCLK PA_5
66 #define DSP_CS PA_2
67 #define DSP_RST PA_3
68 #define DSP_DC PA_4
69
70 // UART for RX (CPU->DP)
71 #define HP_RX PC_5
72 #define HP_TX PC_4
73
74 // RST pin (handle this by hand)
75 #define HP_RST PA_15
76
77 // encoder
78 #define KP_ENC1 PA_0
79 #define KP_ENC2 PA_1
80
81 // keypad
82 #define KP_R0 PB_5
83 #define KP_R1 PB_6
84 #define KP_R2 PB_7
85 #define KP_R3 PB_8
86
87 #define KP_C0 PC_2
88 #define KP_C1 PC_1
89 #define KP_C2 PC_0
90 #define KP_C3 PB_4
91 #define KP_C4 PB_0
92
93 #endif
94
95
96 #ifdef HAVE_PC
97 BufferedSerial pc(USBTX, USBRX);
98 #endif
99 27
100 SSD1322 *dsp; 28 SSD1322 *dsp;
101 HPSerial *hp; 29 HPSerial *hp;
102 volatile uint8_t must_refresh; 30 volatile uint8_t must_refresh;
103 Thread tdsp, tloop; 31 Thread tdsp, tloop;
104 Ticker blinker; 32 Ticker blinker;
105 Timeout rst_delay; 33 Timeout rst_delay;
106 Timeout splashscreen_timer; 34 Timeout splashscreen_timer;
107 DigitalOut led(LED1);
108 InterruptIn rst(HP_RST); 35 InterruptIn rst(HP_RST);
109 36
110 QEI qenc(KP_ENC1, KP_ENC2, NC, 16); 37 QEI qenc(KP_ENC1, KP_ENC2, NC, 16);
111 volatile uint8_t knob; 38 volatile uint8_t knob;
112 volatile bool splashscreen; 39 volatile bool splashscreen;
122 uint8_t row; 49 uint8_t row;
123 uint8_t col; 50 uint8_t col;
124 key_event_t keyevent; 51 key_event_t keyevent;
125 } keycode_t; 52 } keycode_t;
126 53
127 volatile keycode_t cur_keycode; 54 //volatile keycode_t cur_keycode;
55
56 #define KEY_BUF_SIZE 10
57 CircularBuffer<keycode_t, KEY_BUF_SIZE> key_buf;
128 58
129 PinName kp_rows[] = { 59 PinName kp_rows[] = {
130 KP_R0, 60 KP_R0,
131 KP_R1, 61 KP_R1,
132 KP_R2, 62 KP_R2,
163 //uint8_t curcmd; 93 //uint8_t curcmd;
164 uint8_t nchars; 94 uint8_t nchars;
165 char buffer[MAX_BUFF+1]; 95 char buffer[MAX_BUFF+1];
166 96
167 void timeout_h() { 97 void timeout_h() {
98 #if defined(HAS_LED)
168 led = !led; 99 led = !led;
100 #endif
169 } 101 }
170 102
171 typedef struct _DSP 103 typedef struct _DSP
172 { 104 {
173 uint8_t cmd; 105 uint8_t cmd;
334 void reset_irq(void); 266 void reset_irq(void);
335 void qei_cb(int dir); 267 void qei_cb(int dir);
336 void end_splashscreen(void); 268 void end_splashscreen(void);
337 void show(uint8_t, const char*, uint8_t); 269 void show(uint8_t, const char*, uint8_t);
338 270
339 271 /*
272 #if defined(HAVE_PC)
273 FileHandle *mbed::mbed_override_console(int fd)
274 {
275 return static_cast<FileHandle*> (&pc);
276 }
277 #endif
278 */
340 279
341 void setup() { 280 void setup() {
342 #ifdef HAVE_PC 281 #if defined(HAVE_PC)
343 pc.set_baud(115200); 282 #if defined(TARGET_NUCLEO_F446RE)
283 pc.set_baud(115200);
284 #endif
285
286 /*
287 #if defined(TARGET_HP34970_FP_F303RD)
288 pc.init();
289 pc.connect();
290 #endif
291 */
344 #endif 292 #endif
345 293
346 printf("\n\nSETUP\n"); 294 printf("\n\nSETUP\n");
347 printf(" System Core Clock = %.3f MHZ\r\n", 295 printf(" System Core Clock = %.3f MHZ\r\n",
348 (float)SystemCoreClock/1000000); 296 (float)SystemCoreClock/1000000);
349 297
350 //printf("Attaching Led 1: %d\n", LED1); 298 /*
351 //blinker.attach(callback(timeout_h), 0.5f); 299 #if defined(HAS_LED)
300 printf("Attaching Led 1: %d\n", LED1);
301 blinker.attach(callback(timeout_h), 0.5f);
302 #endif
303 */
352 304
353 hp = NULL; 305 hp = NULL;
306 #if defined(TARGET_NUCLEO_F446RE)
354 printf("Serial communication pins\r\n"); 307 printf("Serial communication pins\r\n");
355 printf(" USBRX=%d\r\n", USBRX); 308 printf(" USBRX=%d\r\n", USBRX);
356 printf(" USBTX=%d\r\n", USBTX); 309 printf(" USBTX=%d\r\n", USBTX);
310 #endif
311 #if defined(TARGET_HP34970_FP_F303RD)
312 printf("Serial communication via USB\r\n");
313 #endif
357 314
358 printf("Setup HP communication pins\r\n"); 315 printf("Setup HP communication pins\r\n");
359 printf(" HP_RX=%d\r\n", HP_RX); 316 printf(" HP_RX=%d\r\n", HP_RX);
360 DigitalIn(HP_RX).mode(PullDown); 317 DigitalIn(HP_RX).mode(PullDown);
361 printf(" HP_TX=%d\r\n", HP_TX); 318 printf(" HP_TX=%d\r\n", HP_TX);
363 printf(" HP_RST=%d\r\n", HP_RST); 320 printf(" HP_RST=%d\r\n", HP_RST);
364 DigitalIn(HP_RST).mode(PullDown); 321 DigitalIn(HP_RST).mode(PullDown);
365 322
366 printf(" setup QEI pins\r\n"); 323 printf(" setup QEI pins\r\n");
367 printf(" ENC1=%d\r\n", KP_ENC1); 324 printf(" ENC1=%d\r\n", KP_ENC1);
368 DigitalIn(KP_ENC1).mode(PullUp); 325 DigitalIn(KP_ENC1).mode(PullDown);
369 printf(" ENC2=%d\r\n", KP_ENC2); 326 printf(" ENC2=%d\r\n", KP_ENC2);
370 DigitalIn(KP_ENC2).mode(PullUp); 327 DigitalIn(KP_ENC2).mode(PullDown);
371 qenc.attach(&qei_cb); 328 qenc.attach(&qei_cb);
372 329
373 printf(" setup Keypad\r\n"); 330 printf(" setup Keypad\r\n");
374 cur_keycode.keyevent = KEY_NONE; 331 //cur_keycode.keyevent = KEY_NONE;
375 uint8_t nrows = sizeof(kp_rows)/sizeof(kp_rows[0]); 332 uint8_t nrows = sizeof(kp_rows)/sizeof(kp_rows[0]);
376 uint8_t ncols = sizeof(kp_colums)/sizeof(kp_colums[0]); 333 uint8_t ncols = sizeof(kp_colums)/sizeof(kp_colums[0]);
377 334
378 kpad = new Keypad(nrows, kp_in, ncols, kp_out); 335 kpad = new Keypad(nrows, kp_in, ncols, kp_out);
379 printf(" attach Keypad callbacks\r\n"); 336 printf(" attach Keypad callbacks\r\n");
409 dsp->set_font((unsigned char*)Mono19x27); 366 dsp->set_font((unsigned char*)Mono19x27);
410 dsp->printf("HP34970A"); 367 dsp->printf("HP34970A");
411 dsp->set_font((unsigned char*)Terminal6x8); 368 dsp->set_font((unsigned char*)Terminal6x8);
412 dsp->locate(90, 40); 369 dsp->locate(90, 40);
413 dsp->printf("David Douard"); 370 dsp->printf("David Douard");
371 dsp->locate(0, 52);
372 dsp->printf("Clock = %d ", SystemCoreClock);
373
374 RCC_OscInitTypeDef cfg;
375 HAL_RCC_GetOscConfig(&cfg);
376 if (cfg.HSEState == RCC_HSE_BYPASS)
377 dsp->printf("HSE:EXT ");
378 else if (cfg.HSEState == RCC_HSE_ON)
379 dsp->printf("HSE:XTAL ");
380 else
381 dsp->printf("HSE:OFF ");
382
383 if (cfg.HSIState == RCC_HSI_ON)
384 dsp->printf("HSI:ON ");
385 else
386 dsp->printf("HSI:OFF ");
387
414 dsp->copy_to_lcd(); 388 dsp->copy_to_lcd();
415 389
416 printf("Starting LCD thread\r\n"); 390 printf("Starting LCD thread\r\n");
417 tdsp.start(&copy_to_lcd); 391 tdsp.start(&copy_to_lcd);
418 392
419 printf("Starting Event thread\r\n"); 393 printf("Starting Event thread\r\n");
420 tloop.start(&loop); 394 tloop.start(&loop);
421 395
396 /*
422 dsp->clrbuff(); 397 dsp->clrbuff();
423 show(0x00, "HH:MM:\tSS\t:mmmm", 15); // main dsp 398 show(0x00, "HH:MM:\tSS\t:mmmm", 15); // main dsp
424 show(0x0C, "888", 3); // channel dsp 399 show(0x0C, "888", 3); // channel dsp
425 show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags 400 show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags
401 */
426 402
427 printf("Attaching timers\r\n"); 403 printf("Attaching timers\r\n");
428 splashscreen = true; 404 splashscreen = true;
429 splashscreen_timer.attach(callback(&end_splashscreen), 2); 405 splashscreen_timer.attach(callback(&end_splashscreen), 2);
430 rst.fall(&reset_irq); 406 rst.fall(&reset_irq);
687 } 663 }
688 664
689 665
690 void loop() 666 void loop()
691 { // run over and over 667 { // run over and over
668 keycode_t key;
669
692 unsigned int err[8]; 670 unsigned int err[8];
693 for (uint8_t i=0; i<8; i++) 671 for (uint8_t i=0; i<8; i++)
694 err[i] = 0; 672 err[i] = 0;
695 int p, pp; 673 int p, pp; // rot encoder pulse counters
696 p = 0; 674 p = 0;
697 pp = 0; 675 pp = 0;
676
698 while(1) { 677 while(1) {
678 p = qenc.getPulses();
679 if (p != pp)
680 {
681 dsp->locate(0, 0);
682 dsp->printf("Pulses = %d ", p);
683 dsp->copy_to_lcd();
684 pp = p;
685 }
686
699 if (knob != 0) 687 if (knob != 0)
700 { 688 {
701 if (hp != NULL) 689 if (hp != NULL)
702 { 690 {
703 printf("Sending keycode %X\r\n", knob); 691 printf("Sending keycode %X\r\n", knob);
704 hp->sendkey(knob); 692 hp->sendkey(knob);
705 printf(" DONE\r\n"); 693 printf(" DONE\r\n");
706 } 694 }
695 else
696 {
697 dsp->locate(70, 0);
698 dsp->printf("Knob = %X ", knob);
699 dsp->copy_to_lcd();
700 }
707 knob = 0; 701 knob = 0;
708 } 702 }
709 703
710 if (cur_keycode.keyevent != KEY_NONE) 704 if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE)
711 { 705 {
712 printf("Keycode %dx%d: %s\r\n", 706 key_buf.pop(key);
713 cur_keycode.row, cur_keycode.col, 707 printf("Keycode %dx%d: %s\r\n",
714 cur_keycode.keyevent==KEY_PRESSED?"pressed":"released"); 708 key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released");
715 cur_keycode.keyevent = KEY_NONE; 709 if (hp != NULL) {
710 }
711 else
712 {
713 dsp->locate(140, 0);
714 dsp->printf("KC: %dx%d: %s",
715 key.row, key.col,key.keyevent==KEY_PRESSED?"PRE":"REL");
716 dsp->copy_to_lcd();
717 }
718 // cur_keycode.keyevent = KEY_NONE;
716 } 719 }
717 720
718 p = qenc.getPulses();
719 if (p != pp)
720 {
721 printf("Pulses = %d\r\n", p);
722 pp = p;
723 }
724 721
725 if ((hp != NULL) && (hp->cmd_available())) 722 if ((hp != NULL) && (hp->cmd_available()))
726 { 723 {
727 HPSerial::CMD cmd; 724 HPSerial::CMD cmd;
728 if (hp->pop(cmd)) 725 if (hp->pop(cmd))
729 { 726 {
727 #if defined(HAS_LED)
730 led = 1; 728 led = 1;
729 #endif
731 for (uint8_t i=0; i<7; i++) 730 for (uint8_t i=0; i<7; i++)
732 if (hp->nerrors(i) > err[i]) { 731 if (hp->nerrors(i) > err[i]) {
733 printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n", 732 printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n",
734 hp->nerrors(0), 733 hp->nerrors(0),
735 hp->nerrors(1), 734 hp->nerrors(1),
753 for (uint8_t i=0; i<cmd.size; i++) 752 for (uint8_t i=0; i<cmd.size; i++)
754 printf("%02x ", cmd.value[i]); 753 printf("%02x ", cmd.value[i]);
755 printf("\r\n"); 754 printf("\r\n");
756 } 755 }
757 show(cmd.cmd, cmd.value, cmd.size); 756 show(cmd.cmd, cmd.value, cmd.size);
757 #if defined(HAS_LED)
758 led = 0; 758 led = 0;
759 #endif
759 } 760 }
760 } 761 }
761 //else 762 //else
762 ThisThread::sleep_for(1); 763 ThisThread::sleep_for(1);
763 } 764 }
771 knob = 0x83; 772 knob = 0x83;
772 } 773 }
773 774
774 void kp_cb(uint8_t row, uint8_t col) 775 void kp_cb(uint8_t row, uint8_t col)
775 { 776 {
776 cur_keycode.row = row; 777 keycode_t key;
777 cur_keycode.col = col; 778 key.row = row;
778 cur_keycode.keyevent = KEY_PRESSED; 779 key.col = col;
780 key.keyevent = KEY_PRESSED;
781 if(!key_buf.full())
782 key_buf.push(key);
779 } 783 }
780 784
781 void kr_cb(uint8_t row, uint8_t col) 785 void kr_cb(uint8_t row, uint8_t col)
782 { 786 {
783 cur_keycode.row = row; 787 keycode_t key;
784 cur_keycode.col = col; 788 key.row = row;
785 cur_keycode.keyevent = KEY_RELEASED; 789 key.col = col;
790 key.keyevent = KEY_RELEASED;
791 if(!key_buf.full())
792 key_buf.push(key);
786 } 793 }
787 794
788 int main() 795 int main()
789 { 796 {
790 setup(); 797 setup();

mercurial