diff -r 4fd621551d55 -r a52d60613cf7 src/main.cpp --- a/src/main.cpp Sat Oct 29 23:44:31 2016 +0200 +++ b/src/main.cpp Wed Jan 18 23:19:13 2017 +0100 @@ -12,50 +12,88 @@ #include "SSD1322.h" #include "hp34comm.h" +#include "QEI.h" #include "Keypad.h" +/******************************************************/ +/* L432KC */ +/******************************************************/ #if (defined STM32L432xx) // display #define DSP_MOSI PA_7 //A6 -#define DSP_MISO PA_6 //A5 +#define DSP_MISO PA_6 //A5 #define DSP_SCLK PA_5 //A4 #define DSP_CS PB_0 //D3 #define DSP_RST PB_5 //D11 #define DSP_DC PB_4 //D12 + // UART for RX (CPU->DP) and TX (DP->CPU) combined #define HP_TX PA_9 // serial1 TX #define HP_RX PA_10 // serial1 RX + +// RST pin (handle this by hand) +#define HP_RST PA_0 + // misc -#define DBGPIN PA_12 +#define DBGPIN NC + +// encoder +#define KP_ENC1 PA_8 +#define KP_ENC2 PA_11 -#elif (defined STM32F303x8) -// display -#define DSP_MOSI PA_7 //A6 -#define DSP_MISO PA_6 //A5 -#define DSP_SCLK PA_5 //A4 -#define DSP_CS PA_4 -#define DSP_RST PB_5 //D11 -#define DSP_DC PB_4 //D12 -// UART for RX (CPU->DP) and TX (DP->CPU) combined -#define HP_RX PA_10 // serial1 RX -// misc -#define DBGPIN PA_12 +// keypad +#define KP_R0 PA_1 +#define KP_R1 PA_3 +#define KP_R2 PA_4 +#define KP_R3 PA_12 +#define KP_C0 D4 +#define KP_C1 D5 +#define KP_C2 D6 +#define KP_C3 D7 +#define KP_C4 D8 + +/******************************************************/ +/* F446RE */ +/******************************************************/ #elif defined STM32F446xx +// UART +// USBTX PA_2 +// USBRX PA_3 // display #define DSP_MOSI PB_15 -#define DSP_MISO PB_14 +#define DSP_MISO NC #define DSP_SCLK PB_13 #define DSP_CS PB_12 -#define DSP_RST D11 -#define DSP_DC D12 +#define DSP_RST PB_5 +#define DSP_DC PB_4 + // UART for RX (CPU->DP) -#define HP_RX PA_1 // serial4 RX -// UART for TX (DP->CPU) -#define HP_TX PC_7 // serial6 RX +#define HP_RX PC_11 // serial3 RX +#define HP_TX PC_10 // serial3 TX + +// RST pin (handle this by hand) +#define HP_RST PC_12 + +// encoder +#define KP_ENC1 PC_4 +#define KP_ENC2 PC_5 + +// keypad +#define KP_R0 PC_0 // I-6 +#define KP_R1 PC_1 // II-5 +#define KP_R2 PC_2 // I-5 +#define KP_R3 PC_3 // II-4 + + +#define KP_C0 PB_0 // I-4 +#define KP_C1 PA_6 // I-2 +#define KP_C2 PA_7 // I-3 +#define KP_C3 PA_5 // I-1 +#define KP_C4 PD_2 // II-1 // misc -#define DBGPIN PC_0 +#define DBGPIN PC_6 #endif @@ -63,16 +101,68 @@ Serial pc(USBTX, USBRX); #endif -SSD1322 *dsp;//(10000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS, DSP_RST, DSP_DC, "SSD1322"); -HPSerial *hp; //(HP_RX, HP_TX); +SSD1322 *dsp; +HPSerial *hp; volatile uint8_t must_refresh; Thread tdsp, tloop; -Timeout blinker; +Ticker blinker; +Timeout rst_delay; +Timeout splashscreen_timer; DigitalOut led(LED1); +InterruptIn rst(HP_RST); + +QEI qenc(KP_ENC1, KP_ENC2, NC, 16); +volatile uint8_t knob; +volatile bool splashscreen; + + +typedef enum { + KEY_NONE=0, + KEY_PRESSED, + KEY_RELEASED +} key_event_t; + +typedef struct keycode { + uint8_t row; + uint8_t col; + key_event_t keyevent; +} keycode_t; + +volatile keycode_t cur_keycode; -Keypad kpad(std::vector({(int)PA_0, (int)PA_1}), - std::vector({(int)PA_8, (int)PA_11})); +PinName kp_rows[] = { + KP_R0, + KP_R1, + KP_R2, + KP_R3 +}; + +PinName kp_colums[] = { + KP_C0, KP_C1, + KP_C2, KP_C3, + KP_C4 +}; +DigitalIn kp_in[] = { + KP_R0, + KP_R1, + KP_R2, + KP_R3 +}; + +DigitalOut kp_out[] = { + KP_C0, KP_C1, + KP_C2, KP_C3, + KP_C4 +}; + +uint8_t kp_nrows = sizeof(kp_in)/sizeof(kp_in[0]); +uint8_t kp_ncols = sizeof(kp_out)/sizeof(kp_out[0]); + +void kp_cb(uint8_t row, uint8_t col); +void kr_cb(uint8_t row, uint8_t col); + +Keypad *kpad; uint8_t curchar; uint8_t cmd; uint8_t nchars; @@ -80,7 +170,6 @@ void timeout_h() { led = !led; - blinker.attach(&timeout_h, 0.5f); } typedef struct _DSP @@ -161,7 +250,7 @@ // right-side icons area { 0x00, 0x02, 0, 0, NULL, icon_alarm}, // F1.1 { 0x01, 0x02, 0, 11, NULL, icon_curve}, // F1.2 - + { 0x03, 0x04, 1, 2, "Channel"}, // F1.4 @@ -182,10 +271,10 @@ // col 4 { 0x26, 0x080, 0, 0, "EXT"}, // F3.7 { 0x25, 0x080, 0, 8, "ONCE"}, // F3.6 - + { 0xFF, 0x080, 0, 16, "MEM"}, - + // col 5 { 0x14, 0x100, 0, 0, "4W"}, // F2.5 { 0x15, 0x100, 0, 8, "OC"}, // F2.6 @@ -242,44 +331,90 @@ } if (stayup) dbgpin = 1; - } #else inline void pulse(uint8_t count=1, bool stayup=false) {} #endif +// callbacks & thread functions +void loop(); +void copy_to_lcd(void); +void test_dsp(); +void reset(void); +void reset_irq(void); +void qei_cb(int dir); +void end_splashscreen(void); -void copy_to_lcd(void); -void test_dsp(); void setup() { - blinker.attach(&timeout_h, 0.5f); - - // init the LCD - dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS, DSP_RST, DSP_DC, "SSD1322"); - //dsp.set_orientation(3); #ifdef HAVE_PC pc.baud (115200); #endif - printf("\n\nSystem Core Clock = %.3f MHZ\r\n", + + printf("\n\nSETUP\n"); + printf(" System Core Clock = %.3f MHZ\r\n", (float)SystemCoreClock/1000000); + //printf("Attaching Led 1: %d\n", LED1); + //blinker.attach(callback(timeout_h), 0.5f); + + hp = NULL; + printf("Setup HP communication pins\n"); + printf(" HP_RX=%d\n", HP_RX); + DigitalIn(HP_RX).mode(PullDown); + printf(" HP_TX=%d\n", HP_TX); + DigitalOut(HP_TX).write(1); + printf(" HP_RST=%d\n", HP_RST); + DigitalIn(HP_RST).mode(PullDown); + + printf(" setup QEI pins\n"); + printf(" ENC1=%d\n", KP_ENC1); + DigitalIn(KP_ENC1).mode(PullUp); + printf(" ENC2=%d\n", KP_ENC2); + DigitalIn(KP_ENC2).mode(PullUp); + qenc.attach(&qei_cb); + + printf(" setup Keypad\n"); + cur_keycode.keyevent = KEY_NONE; + uint8_t nrows = sizeof(kp_rows)/sizeof(kp_rows[0]); + uint8_t ncols = sizeof(kp_colums)/sizeof(kp_colums[0]); + + kpad = new Keypad(nrows, kp_in, ncols, kp_out); + printf(" attach Keypad callbacks\n"); + kpad->attach(&kp_cb, &kr_cb); + printf(" start Keypad\n"); + kpad->start(); + + printf("Setup OLED display\n"); + splashscreen = true; + // init the LCD + printf(" DSP_MOSI=%d\n", DSP_MOSI); + printf(" DSP_MISO=%d\n", DSP_MISO); + printf(" DSP_SCLK=%d\n", DSP_SCLK); + printf(" DSP_CS=%d\n", DSP_CS); + printf(" DSP_RST=%d\n", DSP_RST); + printf(" DSP_DC=%d\n", DSP_DC); + dsp = new SSD1322(20000000, DSP_MOSI, DSP_MISO, DSP_SCLK, DSP_CS, + DSP_RST, DSP_DC, "SSD1322"); + //dsp.set_orientation(3); + printf(" configure DSP\n"); // myLCD.set_font((unsigned char*) Terminal6x8); // myLCD.claim(stdout); // send stdout to the LCD display // myLCD.claim(stderr); // send stderr to the LCD display dsp->background(Black); // set background to black dsp->foreground(0xF); dsp->cls(); - + cmd = 0xFF; curchar = 0; nchars = 0; for (uint8_t i=0; ilocate(30, 10); dsp->set_font((unsigned char*)Mono19x27); dsp->printf("HP34970A"); @@ -287,21 +422,54 @@ dsp->locate(90, 40); dsp->printf("David Douard"); dsp->copy_to_lcd(); - wait(2); - dsp->cls(); + + printf("Starting LCD thread\n"); + tdsp.start(©_to_lcd); + + printf("Starting Event thread\n"); + tloop.start(&loop); + + printf("Attaching timers\n"); + splashscreen_timer.attach(callback(&end_splashscreen), 2); + rst.fall(&reset_irq); + + printf("SETUP DONE\n"); +} - hp = new HPSerial(HP_TX, HP_RX); - //hp = NULL; +void end_splashscreen(void) +{ + printf("End of splash screen\n"); + splashscreen = false; +} + +void reset_irq(void) +{ + rst_delay.attach(callback(&reset), 0.1); +} + +void reset(void) +{ + if (DigitalIn(HP_RST).read() == 0) { + if (hp == NULL) { + printf("setup HP communication handler\n"); + hp = new HPSerial(HP_TX, HP_RX); + } + + printf("!! RST !! (gstate=%d, state=%d)\n", + hp->gstate(), hp->state()); + //printf("Value is ... %X\n", hp->search()); + hp->startup(); + } } void copy_to_lcd(void) { - uint8_t mask=1; + //uint8_t mask=1; while(1) { pulse(0, true); - if (must_refresh) { + if ((splashscreen == false) && (must_refresh)) { must_refresh = 0; - //Thread::wait(20); // give a bit of time for some more cmds + //Thread::wait(20); // give a bit of time for some more cmds dsp->copy_to_lcd(); } @@ -325,11 +493,15 @@ } } -void show(uint8_t cmd, char *txt, uint8_t nchar=0) { +void show(uint8_t cmd, const char *intxt, uint8_t nchar=0) { uint8_t i, len; uint16_t bgcolor, fgcolor; char *oldv; + char *txt; + + txt = (char *)malloc(strlen(intxt)+1); + strcpy(txt, intxt); pulse(1, true); len = MAX_BUFF; @@ -359,7 +531,7 @@ if (txt[k] == 0x09) { // \t is a special char for 'unselected' display value txt[k] = 0x00; dsp->printf(txt); - + if (fgcolor == table[i].color) fgcolor /= 2; else @@ -467,7 +639,7 @@ break; } } - + free(txt); //dsp->copy_to_lcd(); pulse(1, false); } @@ -510,49 +682,76 @@ } -void loop() { // run over and over +void loop() +{ // run over and over unsigned int err[8]; for (uint8_t i=0; i<8; i++) err[i] = 0; + int p, pp; + p=0; + pp=0; while(1) { - if (hp->cmd_available()) - { - led = 1; - HPSerial::CMD cmd; - if (hp->pop(cmd)) - { - for (uint8_t i=0; i<7; i++) - if (hp->nerrors(i) > err[i]) { - printf("ERR: %d/%d/%d/%d/%d/%d/%d\n", - hp->nerrors(0), - hp->nerrors(1), - hp->nerrors(2), - hp->nerrors(3), - hp->nerrors(4), - hp->nerrors(5), - hp->nerrors(6) - ); - break; - } - for (uint8_t i=0; i<7; i++) - err[i] = hp->nerrors(i); - printf("CMD[%d] %02X", cmd.id, cmd.cmd); - - if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C)) - printf(": '%s'\n", cmd.value); - else { - printf(":"); - for (uint8_t i=0; isendkey(knob); + printf(" DONE\n"); + } + knob = 0; + } + + if (cur_keycode.keyevent != KEY_NONE) + { + printf("Keycode %dx%d: %s\n", + cur_keycode.row, cur_keycode.col, + cur_keycode.keyevent==KEY_PRESSED?"pressed":"released"); + cur_keycode.keyevent = KEY_NONE; + } + + p = qenc.getPulses(); + if (p != pp) { + printf("Pulses = %d\n", p); + pp = p; + } + if ((hp != NULL) && (hp->cmd_available())) + { + led = 1; + HPSerial::CMD cmd; + if (hp->pop(cmd)) + { + for (uint8_t i=0; i<7; i++) + if (hp->nerrors(i) > err[i]) { + printf("ERR: %d/%d/%d/%d/%d/%d/%d\n", + hp->nerrors(0), + hp->nerrors(1), + hp->nerrors(2), + hp->nerrors(3), + hp->nerrors(4), + hp->nerrors(5), + hp->nerrors(6) + ); + break; + } + for (uint8_t i=0; i<7; i++) + err[i] = hp->nerrors(i); + printf("CMD[%d] %02X", (int)cmd.id, cmd.cmd); + + if ((cmd.cmd == 0x00) || (cmd.cmd == 0x0C)) + printf(": '%s'\n", cmd.value); + else { + printf(":"); + for (uint8_t i=0; i