Update the main code: extract headers and use a CircularBuffer for key events draft

Sun, 25 Oct 2020 22:15:03 +0100

author
David Douard <david.douard@sdf3.org>
date
Sun, 25 Oct 2020 22:15:03 +0100
changeset 32
bc1d6ecbb0cc
parent 31
794401fbd5d5
child 33
167010858b8d

Update the main code: extract headers and use a CircularBuffer for key events

Also fix some forgotten s/_FP_F303RE/_FP_F030RD/

src/TARGET_HP34970_FP_F303RD/PeripheralPins.c file | annotate | diff | comparison | revisions
src/TARGET_HP34970_FP_F303RD/system_clock.c file | annotate | diff | comparison | revisions
src/def_f446re.h file | annotate | diff | comparison | revisions
src/def_hp34970_fp.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
--- a/src/TARGET_HP34970_FP_F303RD/PeripheralPins.c	Sun Oct 25 17:12:22 2020 +0100
+++ b/src/TARGET_HP34970_FP_F303RD/PeripheralPins.c	Sun Oct 25 22:15:03 2020 +0100
@@ -28,7 +28,7 @@
  *******************************************************************************
  */
 
-#if defined(TARGET_HP34970_FP_F303RE)
+#if defined(TARGET_HP34970_FP_F303RD)
 #include "PeripheralPins.h"
 #include "mbed_toolchain.h"
 #include "PeripheralPinMaps.h"
--- a/src/TARGET_HP34970_FP_F303RD/system_clock.c	Sun Oct 25 17:12:22 2020 +0100
+++ b/src/TARGET_HP34970_FP_F303RD/system_clock.c	Sun Oct 25 22:15:03 2020 +0100
@@ -29,7 +29,7 @@
   *-----------------------------------------------------------------------------
   */
 
-#if defined(TARGET_HP34970_FP_F303RE)
+#if defined(TARGET_HP34970_FP_F303RD)
 
 #include "stm32f3xx.h"
 #include "mbed_error.h"
@@ -222,4 +222,4 @@
 }
 #endif /* ((CLOCK_SOURCE) & USE_PLL_HSI) */
 
-#endif /* TARGET_HP34970_FP_F303RE  */
+#endif /* TARGET_HP34970_FP_F303RD  */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/def_f446re.h	Sun Oct 25 22:15:03 2020 +0100
@@ -0,0 +1,52 @@
+#if !defined(DEF_F446RE_H)
+
+
+#define DEF_F446RE_H
+/******************************************************/
+/*                 F446RE                             */
+/******************************************************/
+// TODO: use TARGET_NUCLEO_F446RE or similar
+// UART
+// USBTX PA_2
+// USBRX PA_3
+// display
+#define DSP_MOSI PB_15 // blue
+#define DSP_MISO PB_14 // NC
+#define DSP_SCLK PB_13 // yellow
+#define DSP_CS   PB_12 // green
+#define DSP_RST  PB_5 // green
+#define DSP_DC   PB_4 // red
+
+// UART for RX (CPU->DP)
+#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_10   //  I-1
+#define KP_C4 PD_2   // II-1
+
+// misc
+#define DBGPIN PC_6
+
+#ifdef HAVE_PC
+BufferedSerial pc(USBTX, USBRX);
+#endif
+DigitalOut led(LED1);
+#define HAS_LED
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/def_hp34970_fp.h	Sun Oct 25 22:15:03 2020 +0100
@@ -0,0 +1,43 @@
+// STM32F303xE based HP34970 replacement PCB
+
+#if !defined(DEF_HP34970_FP_H)
+
+#define DEF_HP34970_FP_H
+
+#define DSP_MOSI PA_7
+#define DSP_MISO PA_6 // NC
+#define DSP_SCLK PA_5
+#define DSP_CS   PA_2
+#define DSP_RST  PA_3
+#define DSP_DC   PA_4
+
+// UART for RX (CPU->DP)
+#define HP_RX PC_5
+#define HP_TX PC_4
+
+// RST pin (handle this by hand)
+#define HP_RST PA_15
+
+// encoder
+#define KP_ENC1 PA_0
+#define KP_ENC2 PA_1
+
+// keypad
+#define KP_R0 PB_5
+#define KP_R1 PB_6
+#define KP_R2 PB_7
+#define KP_R3 PB_8
+
+#define KP_C0 PC_2
+#define KP_C1 PC_1
+#define KP_C2 PC_0
+#define KP_C3 PB_4
+#define KP_C4 PB_0
+/*
+#if defined(HAVE_PC)
+#include <USBSerial.h>
+USBSerial pc;
+#endif
+*/
+
+#endif
--- a/src/main.cpp	Sun Oct 25 17:12:22 2020 +0100
+++ b/src/main.cpp	Sun Oct 25 22:15:03 2020 +0100
@@ -15,87 +15,15 @@
 #include "QEI.h"
 #include "Keypad.h"
 
-
-/******************************************************/
-/*                 F446RE                             */
-/******************************************************/
-#if defined STM32F446xx
-// UART
-// USBTX PA_2
-// USBRX PA_3
-// display
-#define DSP_MOSI PB_15 // blue
-#define DSP_MISO PB_14 // NC
-#define DSP_SCLK PB_13 // yellow
-#define DSP_CS   PB_12 // green
-#define DSP_RST  PB_5 // green
-#define DSP_DC   PB_4 // red
-
-// UART for RX (CPU->DP)
-#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_10   //  I-1
-#define KP_C4 PD_2   // II-1
-
-// misc
-#define DBGPIN PC_6
-
-#elif defined STM32F303xE  // HP34970 replacement PCB
-
-#define DSP_MOSI PA_7
-#define DSP_MISO PA_6 // NC
-#define DSP_SCLK PA_5
-#define DSP_CS   PA_2
-#define DSP_RST  PA_3
-#define DSP_DC   PA_4
-
-// UART for RX (CPU->DP)
-#define HP_RX PC_5
-#define HP_TX PC_4
-
-// RST pin (handle this by hand)
-#define HP_RST PA_15
-
-// encoder
-#define KP_ENC1 PA_0
-#define KP_ENC2 PA_1
-
-// keypad
-#define KP_R0 PB_5
-#define KP_R1 PB_6
-#define KP_R2 PB_7
-#define KP_R3 PB_8
-
-#define KP_C0 PC_2
-#define KP_C1 PC_1
-#define KP_C2 PC_0
-#define KP_C3 PB_4
-#define KP_C4 PB_0
-
+// Pins and device declarations
+#if defined TARGET_NUCLEO_F446RE
+#include "def_f446re.h"
+#elif defined TARGET_HP34970_FP_F303RD
+#include "def_hp34970_fp.h"
 #endif
 
+#include "platform/CircularBuffer.h"
 
-#ifdef HAVE_PC
-BufferedSerial pc(USBTX, USBRX);
-#endif
 
 SSD1322 *dsp;
 HPSerial *hp;
@@ -104,7 +32,6 @@
 Ticker blinker;
 Timeout rst_delay;
 Timeout splashscreen_timer;
-DigitalOut led(LED1);
 InterruptIn rst(HP_RST);
 
 QEI qenc(KP_ENC1, KP_ENC2, NC, 16);
@@ -124,7 +51,10 @@
   key_event_t keyevent;
 } keycode_t;
 
-volatile keycode_t cur_keycode;
+//volatile keycode_t cur_keycode;
+
+#define KEY_BUF_SIZE 10
+CircularBuffer<keycode_t, KEY_BUF_SIZE> key_buf;
 
 PinName kp_rows[] = {
   KP_R0,
@@ -165,7 +95,9 @@
 char buffer[MAX_BUFF+1];
 
 void timeout_h() {
+#if defined(HAS_LED)
   led = !led;
+#endif
 }
 
 typedef struct _DSP
@@ -336,24 +268,49 @@
 void end_splashscreen(void);
 void show(uint8_t, const char*, uint8_t);
 
-
+/*
+#if defined(HAVE_PC)
+FileHandle *mbed::mbed_override_console(int fd)
+{
+    return static_cast<FileHandle*> (&pc);
+}
+#endif
+*/
 
 void setup() {
-#ifdef HAVE_PC
-  pc.set_baud(115200);
+#if defined(HAVE_PC)
+#if   defined(TARGET_NUCLEO_F446RE)
+	pc.set_baud(115200);
+#endif
+
+/*
+#if  defined(TARGET_HP34970_FP_F303RD)
+  pc.init();
+  pc.connect();
+#endif
+*/
 #endif
 
   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);
+  /*
+#if defined(HAS_LED)
+  printf("Attaching Led 1: %d\n", LED1);
+  blinker.attach(callback(timeout_h), 0.5f);
+#endif
+  */
 
   hp = NULL;
+#if defined(TARGET_NUCLEO_F446RE)
   printf("Serial communication pins\r\n");
   printf("     USBRX=%d\r\n", USBRX);
   printf("     USBTX=%d\r\n", USBTX);
+#endif
+#if defined(TARGET_HP34970_FP_F303RD)
+  printf("Serial communication via USB\r\n");
+#endif
 
   printf("Setup HP communication pins\r\n");
   printf("     HP_RX=%d\r\n", HP_RX);
@@ -365,13 +322,13 @@
 
   printf("  setup QEI pins\r\n");
   printf("     ENC1=%d\r\n", KP_ENC1);
-  DigitalIn(KP_ENC1).mode(PullUp);
+  DigitalIn(KP_ENC1).mode(PullDown);
   printf("     ENC2=%d\r\n", KP_ENC2);
-  DigitalIn(KP_ENC2).mode(PullUp);
+  DigitalIn(KP_ENC2).mode(PullDown);
   qenc.attach(&qei_cb);
 
   printf("  setup Keypad\r\n");
-  cur_keycode.keyevent = KEY_NONE;
+  //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]);
 
@@ -411,6 +368,23 @@
   dsp->set_font((unsigned char*)Terminal6x8);
   dsp->locate(90, 40);
   dsp->printf("David Douard");
+  dsp->locate(0, 52);
+  dsp->printf("Clock = %d ", SystemCoreClock);
+
+  RCC_OscInitTypeDef cfg;
+  HAL_RCC_GetOscConfig(&cfg);
+  if (cfg.HSEState == RCC_HSE_BYPASS)
+	  dsp->printf("HSE:EXT ");
+  else if (cfg.HSEState == RCC_HSE_ON)
+	  dsp->printf("HSE:XTAL ");
+  else
+	  dsp->printf("HSE:OFF ");
+
+  if (cfg.HSIState == RCC_HSI_ON)
+	  dsp->printf("HSI:ON ");
+  else
+	  dsp->printf("HSI:OFF ");
+
   dsp->copy_to_lcd();
 
   printf("Starting LCD thread\r\n");
@@ -419,10 +393,12 @@
   printf("Starting Event thread\r\n");
   tloop.start(&loop);
 
+  /*
   dsp->clrbuff();
   show(0x00, "HH:MM:\tSS\t:mmmm", 15); // main dsp
   show(0x0C, "888", 3); // channel dsp
   show(0x0A, "\xFF\xFF\xFF\xFF", 4); // all flags
+  */
 
   printf("Attaching timers\r\n");
   splashscreen = true;
@@ -689,13 +665,25 @@
 
 void loop()
 { // run over and over
+	keycode_t key;
+
   unsigned int err[8];
   for (uint8_t i=0; i<8; i++)
     err[i] = 0;
-  int p, pp;
+  int p, pp;  // rot encoder pulse counters
   p = 0;
   pp = 0;
+
   while(1) {
+    p = qenc.getPulses();
+    if (p != pp)
+	{
+		dsp->locate(0, 0);
+		dsp->printf("Pulses = %d  ", p);
+		  dsp->copy_to_lcd();
+		pp = p;
+    }
+
     if (knob != 0)
 	{
 	  if (hp != NULL)
@@ -704,30 +692,41 @@
 		hp->sendkey(knob);
 		printf("   DONE\r\n");
 	  }
+	  else
+	  {
+		  dsp->locate(70, 0);
+		  dsp->printf("Knob = %X  ", knob);
+		  dsp->copy_to_lcd();
+	  }
 	  knob = 0;
 	}
 
-    if (cur_keycode.keyevent != KEY_NONE)
+    if (!key_buf.empty()) //cur_keycode.keyevent != KEY_NONE)
 	{
-	  printf("Keycode %dx%d: %s\r\n",
-			 cur_keycode.row, cur_keycode.col,
-			 cur_keycode.keyevent==KEY_PRESSED?"pressed":"released");
-	  cur_keycode.keyevent = KEY_NONE;
+		key_buf.pop(key);
+		printf("Keycode %dx%d: %s\r\n",
+			   key.row, key.col, key.keyevent==KEY_PRESSED?"pressed":"released");
+		if (hp != NULL) {
+		}
+		else
+		{
+			dsp->locate(140, 0);
+			dsp->printf("KC: %dx%d: %s",
+						key.row, key.col,key.keyevent==KEY_PRESSED?"PRE":"REL");
+		  dsp->copy_to_lcd();
+		}
+		// cur_keycode.keyevent = KEY_NONE;
 	}
 
-    p = qenc.getPulses();
-    if (p != pp)
-	{
-      printf("Pulses = %d\r\n", p);
-      pp = p;
-    }
 
     if ((hp != NULL) && (hp->cmd_available()))
       {
 		HPSerial::CMD cmd;
 		if (hp->pop(cmd))
     	  {
+#if defined(HAS_LED)
 			led = 1;
+#endif
     		for (uint8_t i=0; i<7; i++)
     	      if (hp->nerrors(i) > err[i]) {
     			  printf("ERR: %d/%d/%d/%d/%d/%d/%d\r\n",
@@ -755,7 +754,9 @@
     	      printf("\r\n");
     		}
     	    show(cmd.cmd, cmd.value, cmd.size);
+#if defined(HAS_LED)
 			led = 0;
+#endif
     	  }
 	  }
 	//else
@@ -773,16 +774,22 @@
 
 void kp_cb(uint8_t row, uint8_t col)
 {
-    cur_keycode.row = row;
-    cur_keycode.col = col;
-    cur_keycode.keyevent = KEY_PRESSED;
+	keycode_t key;
+    key.row = row;
+    key.col = col;
+    key.keyevent = KEY_PRESSED;
+	if(!key_buf.full())
+		key_buf.push(key);
 }
 
 void kr_cb(uint8_t row, uint8_t col)
 {
-    cur_keycode.row = row;
-    cur_keycode.col = col;
-    cur_keycode.keyevent = KEY_RELEASED;
+	keycode_t key;
+    key.row = row;
+    key.col = col;
+    key.keyevent = KEY_RELEASED;
+	if(!key_buf.full())
+		key_buf.push(key);
 }
 
 int main()

mercurial