From 061430973e82995368d27ff9081391f9475da3c7 Mon Sep 17 00:00:00 2001
From: James McKenzie <git@madingley.org>
Date: Mon, 3 Aug 2015 10:34:07 +0100
Subject: fish

---
 .gitignore                        |   1 +
 app/Makefile                      |   4 +-
 app/code.c                        |  98 +++++++++++++++++++++++++
 app/keypad.c                      | 148 +++++++++++++++++++++++++++-----------
 app/main.c                        |   8 ++-
 app/project.h                     |   3 +
 app/prototypes.h                  |   7 +-
 app/ticker.c                      |   3 +-
 app/usb.c                         |   5 ++
 boot/Makefile                     |   2 +-
 boot/usbdfu.c                     |   4 +-
 doc/TTP229B-Schematic-Diagram.pdf | Bin 0 -> 185208 bytes
 host/Makefile                     |  17 +++++
 host/hexdump.c                    |  60 ++++++++++++++++
 host/main.c                       | 101 ++++++++++++++++++++++++++
 host/project.h                    |  38 ++++++++++
 include/stm32f101cb_clock.h       |  66 +++++++++++++++++
 17 files changed, 513 insertions(+), 52 deletions(-)
 create mode 100644 app/code.c
 create mode 100644 doc/TTP229B-Schematic-Diagram.pdf
 create mode 100644 host/Makefile
 create mode 100644 host/hexdump.c
 create mode 100644 host/main.c
 create mode 100644 host/project.h
 create mode 100644 include/stm32f101cb_clock.h

diff --git a/.gitignore b/.gitignore
index e530e6a..ac55bbe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
 *.elf
 *.hex
 *.dfu
+cryptopad
diff --git a/app/Makefile b/app/Makefile
index f02196e..c875b94 100644
--- a/app/Makefile
+++ b/app/Makefile
@@ -24,7 +24,7 @@ PROG=crypto
 V=1
 default: ${PROG}.elf
 
-CSRCS=dfu.c crypto.c main.c usb.c  led.c ticker.c i2c.c lcd.c keypad.c
+CSRCS=dfu.c crypto.c main.c usb.c  led.c ticker.c i2c.c lcd.c keypad.c code.c
 
 
 BINARY = ${PROG}
@@ -34,7 +34,7 @@ include ../Makefile.include
 
 #DID=$(shell printf '\#include "id.h"\nID_PRODUCT' | ${CC} -I.. -E - | grep -v ^\# )
 
-INCLUDES +=  -I..
+INCLUDES +=  -I../include
 
 dfu:${PROG}.dfu
 	dfu-util -R -a 0 -d 1d6b:1932 -s 0x08002000:leave -D $<
diff --git a/app/code.c b/app/code.c
new file mode 100644
index 0000000..4d32f7b
--- /dev/null
+++ b/app/code.c
@@ -0,0 +1,98 @@
+#include "project.h"
+
+
+static uint8_t code[16];
+static int code_len;
+static int hide;
+static int show;
+
+
+void code_display(void)
+{
+size_t i;
+
+#if 0
+lcd_erase_line(0,16);
+
+if (code_len!=16) 
+	lcd_write("Enter code:",0,0);
+else
+	lcd_write("Code entered:",0,0);
+#endif
+
+  for (i=0;i<sizeof(code);++i) 
+	lcd_write_char(hide ? '*': (code[i] ? code[i]:' '),i,1);
+
+
+  lcd_backlight (!hide);
+}
+
+
+int
+vendor_control_request (usbd_device * usbd_dev, struct usb_setup_data *req,
+                     uint8_t ** buf, uint16_t * len,
+                     int (**complete) (usbd_device * usbd_dev,
+                                       struct usb_setup_data * req))
+{
+  (void) buf;
+  (void) len;
+  (void) usbd_dev;
+
+  (void) complete;
+
+  if ((req->bmRequestType & 0x7F) != 0x41)
+    return 0;                   /* Only accept vendor request. */
+
+  switch (req->bRequest)
+    {
+
+    case 0x34:
+        (*buf)[0] = 0x1;
+        (*buf)[1] = 0;
+        (*buf)[2] = 0;
+        (*buf)[3] = 0;
+        (*buf)[4] = 0x2;
+        (*buf)[5] = 0;          /* iString not used here */
+        *len = 6;
+        return 1;
+      }
+
+  return 0;
+
+}
+
+
+void code_tick(void)
+{
+if (!show) return;
+show--;
+	if (!show) {
+		hide++;
+		code_display();
+	}
+
+}
+
+
+
+
+void key_event (uint8_t v, int ud)
+{
+  if (!ud) return;
+
+  if (code_len==sizeof(code))  {
+	code_len=0;
+	memset(code,' ',sizeof(code));
+  }
+
+
+  show=5000;
+  hide=0;
+
+  code[code_len++]=v;
+
+
+  code_display();
+  
+
+}
diff --git a/app/keypad.c b/app/keypad.c
index daea5c7..d4a82db 100644
--- a/app/keypad.c
+++ b/app/keypad.c
@@ -7,76 +7,140 @@
 
 #define KEYPAD_DELAY do { delay_us(1); } while (0)
 
-uint16_t keypad_read(void)
-{
-uint16_t ret=0;
-uint16_t c;
 
-/*Reset the state machine in the keypad */
+#define SCAN_INTERVAL 73
+#define DEBOUNCE_INTERVAL 1
+#define DEBOUNCE_COUNT 3
 
-gpio_set (GPIO_DATA, DATA);
-KEYPAD_DELAY;
-gpio_clear (GPIO_DATA, DATA);
-KEYPAD_DELAY;
-gpio_set (GPIO_DATA, DATA);
-KEYPAD_DELAY;
+static uint32_t next_scan;
 
-gpio_clear (GPIO_CLOCK, CLOCK);
-KEYPAD_DELAY;
 
-for (c=0x8000;c;c>>=1)
+uint16_t
+keypad_raw_read (void)
 {
-gpio_set (GPIO_CLOCK, CLOCK);
-KEYPAD_DELAY;
-if (!(gpio_get (GPIO_DATA, DATA) & DATA)) ret|=c;
-gpio_clear (GPIO_CLOCK, CLOCK);
-KEYPAD_DELAY;
-}
+  uint16_t ret = 0;
+  uint16_t c;
 
-gpio_set (GPIO_CLOCK, CLOCK);
+/*Reset the state machine in the keypad */
 
-return ret;
+  gpio_set (GPIO_DATA, DATA);
+  KEYPAD_DELAY;
+  gpio_clear (GPIO_DATA, DATA);
+  KEYPAD_DELAY;
+  gpio_set (GPIO_DATA, DATA);
+  KEYPAD_DELAY;
+
+  gpio_clear (GPIO_CLOCK, CLOCK);
+  KEYPAD_DELAY;
+
+  for (c = 0x8000; c; c >>= 1)
+    {
+      gpio_set (GPIO_CLOCK, CLOCK);
+      KEYPAD_DELAY;
+      if (!(gpio_get (GPIO_DATA, DATA) & DATA))
+        ret |= c;
+      gpio_clear (GPIO_CLOCK, CLOCK);
+      KEYPAD_DELAY;
+    }
+
+  gpio_set (GPIO_CLOCK, CLOCK);
+
+  return ret;
 }
 
-static void
-keypad_scan (void)
+uint16_t
+keypad_read (void)
 {
+  int c;
+  const uint8_t lut[] = "cdef89ab45670123";
+  uint8_t ret = 0;
 
-  uint16_t v;
-  char buf[16];
 
 
-  v=keypad_read();
-
-  sprintf (buf, "%04x", v);
-  lcd_write (buf, 0, 1);
-
+/*Reset the state machine in the keypad */
 
+  gpio_set (GPIO_DATA, DATA);
+  KEYPAD_DELAY;
+  gpio_clear (GPIO_DATA, DATA);
+  KEYPAD_DELAY;
+  gpio_set (GPIO_DATA, DATA);
+  KEYPAD_DELAY;
+
+  gpio_clear (GPIO_CLOCK, CLOCK);
+  KEYPAD_DELAY;
+
+  for (c = 0; c < 16; ++c)
+    {
+      gpio_set (GPIO_CLOCK, CLOCK);
+      KEYPAD_DELAY;
+      if (!(gpio_get (GPIO_DATA, DATA) & DATA))
+        ret = lut[c];
+      gpio_clear (GPIO_CLOCK, CLOCK);
+      KEYPAD_DELAY;
+    }
+
+  gpio_set (GPIO_CLOCK, CLOCK);
+
+  return ret;
 }
 
-void
-keypad_tick (void)
-{
-  static int c;
 
-  c++;
+static void
+keypad_scan (void)
+{
+  static uint8_t last_v;
+  static uint8_t key_down;
+  static int same;
+  uint8_t v;
+
+  v = keypad_read ();
+
+  next_scan = DEBOUNCE_INTERVAL;
+
+  if (v!=last_v) {
+     last_v=v;
+     same=0;
+     return;
+  } else {
+     if (same<DEBOUNCE_COUNT) {
+     	same++;
+	return;
+     }
+  }
+
+  if (!v && !key_down)  {	
+      next_scan = SCAN_INTERVAL;
+      return;
+  }
+
+	if (key_down==v) {
+		return;
+	} else if (key_down!=v) {
+	  if (key_down)
+          key_event (key_down, 0);
+	key_down=v;
+	  if (key_down)
+          key_event (key_down, 1);
+	}
+}
 
-  if (c < 73)
-    return;
 
-  c = 0;
 
 
-  keypad_scan ();
+void
+keypad_tick (void)
+{
 
+  if (!next_scan)
+    keypad_scan ();
+  else
+    next_scan--;
 
 }
 
 
 
 
-
-
 void
 keypad_init (void)
 {
diff --git a/app/main.c b/app/main.c
index edca1e6..d1c9b27 100644
--- a/app/main.c
+++ b/app/main.c
@@ -6,9 +6,11 @@ main (void)
 {
   /*set up pll */
   //rcc_clock_setup_in_hse_8mhz_out_24mhz ();
-  rcc_clock_setup_in_hse_8mhz_out_72mhz ();
+  //rcc_clock_setup_in_hse_8mhz_out_72mhz ();
   //rcc_clock_setup_in_hsi_out_48mhz();
 
+  rcc_clock_setup_in_hse_8mhz_out_48mhz ();
+
   /*turn on clocks to periferals */
   rcc_periph_clock_enable (RCC_GPIOA);
   rcc_periph_clock_enable (RCC_GPIOB);
@@ -22,7 +24,6 @@ main (void)
   keypad_init ();
 
 
-  lcd_backlight (1);
 
 #if 0
   {
@@ -31,12 +32,13 @@ main (void)
     lcd_write (buf, 0, 1);
   }
 #endif
+  lcd_write ("Booting...", 0, 0);
 
 
-  lcd_write ("hello world", 0, 0);
 
   usb_init ();
 
+  code_display();
 
   usb_run ();
 
diff --git a/app/project.h b/app/project.h
index 3b7fc27..19bb5b2 100644
--- a/app/project.h
+++ b/app/project.h
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/flash.h>
 #include <libopencm3/stm32/gpio.h>
 #include <libopencm3/stm32/usart.h>
 #include <libopencm3/stm32/usb.h>
@@ -8,6 +9,8 @@
 #include <libopencm3/usb/usbd.h>
 #include <libopencm3/usb/hid.h>
 #include <libopencm3/cm3/cortex.h>
+#include <stm32f101cb_clock.h>
+#include <string.h>
 
 
 #include <libopencm3/cm3/scb.h>
diff --git a/app/prototypes.h b/app/prototypes.h
index 7f5498a..9a89a8c 100644
--- a/app/prototypes.h
+++ b/app/prototypes.h
@@ -52,7 +52,12 @@ extern void lcd_reset(void);
 extern void lcd_init(void);
 extern void lcd_shutdown(void);
 /* keypad.c */
-extern int i2c2_bb(int scl, int sda);
+extern uint16_t keypad_raw_read(void);
 extern uint16_t keypad_read(void);
 extern void keypad_tick(void);
 extern void keypad_init(void);
+/* code.c */
+extern void code_display(void);
+extern int vendor_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, int (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req));
+extern void code_tick(void);
+extern void key_event(uint8_t v, int ud);
diff --git a/app/ticker.c b/app/ticker.c
index c9d12ec..d054317 100644
--- a/app/ticker.c
+++ b/app/ticker.c
@@ -29,7 +29,8 @@ sys_tick_handler (void)
 
   led_tick ();
   lcd_tick ();
-  keypad_tick();
+  keypad_tick ();
+  code_tick();
 }
 
 
diff --git a/app/usb.c b/app/usb.c
index ea03f2a..2dfac76 100644
--- a/app/usb.c
+++ b/app/usb.c
@@ -143,6 +143,11 @@ usb_set_config (usbd_device * usbd_dev, uint16_t wValue)
                                   dfu_control_request);
 
 
+  usbd_register_control_callback (usbd_dev,
+                                  USB_REQ_TYPE_VENDOR | USB_REQ_TYPE_INTERFACE,
+                                  USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
+                                  vendor_control_request);
+
 }
 
 /* Buffer to be used for control requests. */
diff --git a/boot/Makefile b/boot/Makefile
index 86d4d2c..55cbe4e 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -28,5 +28,5 @@ OBJS = ${CSRCS:%.c=%.o}
 
 include ../Makefile.include
 
-CFLAGS += -I..
+CFLAGS += -I../include
 
diff --git a/boot/usbdfu.c b/boot/usbdfu.c
index 33f4c22..400fc7c 100644
--- a/boot/usbdfu.c
+++ b/boot/usbdfu.c
@@ -24,7 +24,7 @@
 #include <libopencm3/cm3/scb.h>
 #include <libopencm3/usb/usbd.h>
 #include <libopencm3/usb/dfu.h>
-
+#include <stm32f101cb_clock.h>
 
 #define APP_ADDRESS	0x08002000
 
@@ -270,7 +270,7 @@ int main(void)
 
 	dfu_flag=0;
 
-	rcc_clock_setup_in_hsi_out_48mhz();
+        rcc_clock_setup_in_hse_8mhz_out_48mhz();
 
 	rcc_periph_clock_enable(RCC_GPIOC);
 
diff --git a/doc/TTP229B-Schematic-Diagram.pdf b/doc/TTP229B-Schematic-Diagram.pdf
new file mode 100644
index 0000000..70d73f8
Binary files /dev/null and b/doc/TTP229B-Schematic-Diagram.pdf differ
diff --git a/host/Makefile b/host/Makefile
new file mode 100644
index 0000000..667d094
--- /dev/null
+++ b/host/Makefile
@@ -0,0 +1,17 @@
+INCLUDES=$(shell pkg-config --cflags libusb-1.0)
+LIBS=$(shell pkg-config --libs libusb-1.0)
+
+PROG=cryptopad
+CSRCS=main.c hexdump.c
+
+OBJS=${CSRCS:%.c=%.o}
+
+CFLAGS=${OPT} 
+CPPFLAGS=${INCLUDES} ${DEFINES}
+
+${PROG}:${OBJS}
+	${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS} ${LIBS}
+
+clean:
+	/bin/rm -f *~ *.d ${PROG} ${OBJS}
+	
diff --git a/host/hexdump.c b/host/hexdump.c
new file mode 100644
index 0000000..127faab
--- /dev/null
+++ b/host/hexdump.c
@@ -0,0 +1,60 @@
+/*
+ * hexdump.c
+ *
+ * Copyright (c) 2011 Citrix Sysmtes Inc.,
+ * All rights reserved.
+ *
+ */
+
+#include "project.h"
+
+void
+hexdump (char *prefix, void *_d, int len)
+{
+  uint8_t *d = (uint8_t *) _d;
+  int i, j, k;
+  int e;
+
+  printf ("%s %d bytes from %p\n", prefix, len, d);
+
+  if (!d || len < 0)
+    return;
+
+  e = len + 15;
+  e &= ~15;
+
+  for (i = 0; i < e; i += 16)
+    {
+      printf ("%s %05x:", prefix, i);
+      for (j = 0; j < 16; ++j)
+        {
+          k = i + j;
+
+          if (k < len)
+            printf (" %02x", d[k]);
+          else
+            printf ("   ");
+
+          if (j == 7)
+            printf (" ");
+        }
+
+      printf (" ");
+      for (j = 0; j < 16; ++j)
+        {
+          k = i + j;
+          if (k < len)
+            {
+              uint8_t c = d[k];
+              if (c < 33)
+                c = '.';
+              if (c > 126)
+                c = '.';
+              printf ("%c", c);
+            }
+          if (j == 7)
+            printf (" ");
+        }
+      printf ("\n");
+    }
+}
diff --git a/host/main.c b/host/main.c
new file mode 100644
index 0000000..a779cc1
--- /dev/null
+++ b/host/main.c
@@ -0,0 +1,101 @@
+#include "project.h"
+
+
+
+static void poke(libusb_device_handle *devh)
+{
+uint32_t timeout=4000;
+char buf[128];
+int len;
+
+len=    libusb_control_transfer( devh,
+        /* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_INTERFACE,
+        /* bRequest      */ 0x34,
+        /* wValue        */ 0x1234,
+        /* wIndex        */ 0x5678,
+        /* Data          */ buf,
+        /* wLength       */ sizeof(buf), timeout );
+
+
+
+
+if (len>=0)
+hexdump(">",buf,len);
+
+
+}
+
+
+
+static void poke_device(libusb_device *dev, struct libusb_device_descriptor *desc)
+{
+int ret;
+libusb_device_handle *devh;
+
+ret=libusb_open(dev, &devh);
+
+if (ret) {
+	warn("unable to open device: %i",ret);
+	return;
+}
+
+printf("poke\n");
+
+poke(devh);
+
+libusb_close(devh);
+
+}
+
+void probe_devices(libusb_context *ctx)
+{
+        libusb_device **list;
+        ssize_t num_devs;
+        ssize_t i;
+
+        num_devs = libusb_get_device_list(ctx, &list);
+        for (i = 0; i < num_devs; ++i) {
+                struct libusb_device_descriptor desc;
+                struct libusb_device *dev = list[i];
+
+                if (libusb_get_device_descriptor(dev, &desc))
+                        continue;
+
+
+		if (desc.idVendor!=0x1d6b) continue;
+		if (desc.idProduct!=0x1932) continue;
+
+		poke_device(dev,&desc);
+
+        }
+        libusb_free_device_list(list, 0);
+}
+
+
+
+
+
+int main(int argc,char *argv)
+{
+	int ret;
+
+        libusb_context *ctx;
+
+
+        ret = libusb_init(&ctx);
+        if (ret) 
+                errx(EX_IOERR, "unable to initialize libusb: %i", ret);
+
+
+	libusb_set_debug(ctx, 255);
+
+        probe_devices(ctx);
+
+	return 0;
+}
+
+
+	
+
+
+
diff --git a/host/project.h b/host/project.h
new file mode 100644
index 0000000..2302570
--- /dev/null
+++ b/host/project.h
@@ -0,0 +1,38 @@
+#include <libusb.h>
+#include <stdio.h>
+#include <malloc.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <libusb.h>
+
+#ifdef HAVE_ERR
+# include <err.h>
+#else
+# include <errno.h>
+# include <string.h>
+# define warnx(...) do {\
+    fprintf(stderr, __VA_ARGS__);\
+    fprintf(stderr, "\n"); } while (0)
+# define errx(eval, ...) do {\
+    warnx(__VA_ARGS__);\
+    exit(eval); } while (0)
+# define warn(...) do {\
+    fprintf(stderr, "%s: ", strerror(errno));\
+    warnx(__VA_ARGS__); } while (0)
+# define err(eval, ...) do {\
+    warn(__VA_ARGS__);\
+    exit(eval); } while (0)
+#endif /* HAVE_ERR */
+
+
+#ifdef HAVE_SYSEXITS_H
+# include <sysexits.h>
+#else
+# define EX_OK          0       /* successful termination */
+# define EX_USAGE       64      /* command line usage error */
+# define EX_SOFTWARE    70      /* internal software error */
+# define EX_IOERR       74      /* input/output error */
+#endif /* HAVE_SYSEXITS_H */
+
+
diff --git a/include/stm32f101cb_clock.h b/include/stm32f101cb_clock.h
new file mode 100644
index 0000000..0fc8a33
--- /dev/null
+++ b/include/stm32f101cb_clock.h
@@ -0,0 +1,66 @@
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/flash.h>
+
+static inline void rcc_clock_setup_in_hse_8mhz_out_48mhz(void)
+{
+	/* The part says SYSCLK < 36MHz, but to get usb we need */
+ 	/* sysclk == 48MHz or 72MHz */
+
+
+	/* Enable internal high-speed oscillator. */
+	rcc_osc_on(HSI);
+	rcc_wait_for_osc_ready(HSI);
+
+	/* Select HSI as SYSCLK source. */
+	rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
+
+	/* Enable external high-speed oscillator 8MHz. */
+	rcc_osc_on(HSE);
+	rcc_wait_for_osc_ready(HSE);
+	rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSECLK);
+
+	/*
+	 * Set prescalers for AHB, ADC, ABP1, ABP2.
+	 * Do this before touching the PLL (TODO: why?).
+	 */
+	rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_DIV2);       /* Set. 24MHz Max. 36MHz */
+	rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV4);    /* Set. 12MHz Max. 14MHz */
+	rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_NODIV);      /* Set. 24MHz Max. 36MHz */
+	rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV);      /* Set. 24MHz Max. 36MHz */
+	rcc_set_usbpre(RCC_CFGR_USBPRE_PLL_CLK_NODIV); /* Set. 48Mhz Max. 48Mhz */
+
+	/*
+	 * Sysclk runs with 36MHz -> 1 waitstates.
+	 * 0WS from 0-24MHz
+	 * 1WS from 24-48MHz
+	 * 2WS from 48-72MHz
+	 */
+	flash_set_ws(FLASH_ACR_LATENCY_1WS);
+
+	/*
+	 * Set the PLL multiplication factor to 3.
+	 * 8MHz (external) * 6 (multiplier) = 48MHz
+	 */
+	rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL6);
+
+	/* Select HSE as PLL source. */
+	rcc_set_pll_source(RCC_CFGR_PLLSRC_HSE_CLK);
+
+	/*
+	 * Don't Divide external frequency by 2 before entering PLL
+	 * (only valid/needed for HSE).
+	 */
+	rcc_set_pllxtpre(RCC_CFGR_PLLXTPRE_HSE_CLK);
+
+	/* Enable PLL oscillator and wait for it to stabilize. */
+	rcc_osc_on(PLL);
+	rcc_wait_for_osc_ready(PLL);
+
+	/* Select PLL as SYSCLK source. */
+	rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
+
+	/* Set the peripheral clock frequencies used */
+	rcc_ahb_frequency = 24000000;
+	rcc_apb1_frequency = 24000000;
+	rcc_apb2_frequency = 24000000;
+}
-- 
cgit v1.2.3