summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJames McKenzie <git@madingley.org>2015-08-03 10:34:07 +0100
committerJames McKenzie <git@madingley.org>2015-08-03 10:34:07 +0100
commit061430973e82995368d27ff9081391f9475da3c7 (patch)
tree62ac12e6cff789050ca17f00ac70d85de75e3efd /app
parent23e5d273ef3e11c8ad463c632daa5a52684bc5bb (diff)
downloadcandlestick-061430973e82995368d27ff9081391f9475da3c7.tar.gz
candlestick-061430973e82995368d27ff9081391f9475da3c7.tar.bz2
candlestick-061430973e82995368d27ff9081391f9475da3c7.zip
fish
Diffstat (limited to 'app')
-rw-r--r--app/Makefile4
-rw-r--r--app/code.c98
-rw-r--r--app/keypad.c148
-rw-r--r--app/main.c8
-rw-r--r--app/project.h3
-rw-r--r--app/prototypes.h7
-rw-r--r--app/ticker.c3
-rw-r--r--app/usb.c5
8 files changed, 227 insertions, 49 deletions
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. */