From 98cba32ca9d62f6e4b7e735d177dba699b8d0698 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 6 Jun 2015 12:38:36 +0100 Subject: inititalcommit --- app/.Makefile.swo | Bin 0 -> 12288 bytes app/.Makefile.swp | Bin 0 -> 4096 bytes app/.main.c.swp | Bin 0 -> 4096 bytes app/.usart.c.swn | Bin 0 -> 4096 bytes app/.usart.c.swo | Bin 0 -> 16384 bytes app/Makefile | 61 ++++++++++ app/atkbd.c | 325 +++++++++++++++++++++++++++++++++++++++++++++++++++ app/consumer.c | 93 +++++++++++++++ app/dfu.c | 80 +++++++++++++ app/gdb.script | 2 + app/keyboard.c | 117 +++++++++++++++++++ app/pins.h | 49 ++++++++ app/project.h | 26 +++++ app/prototypes.h | 57 +++++++++ app/ring.c | 75 ++++++++++++ app/ring.h | 14 +++ app/stdio.c | 83 +++++++++++++ app/ticker.c | 76 ++++++++++++ app/tims_keyboard.c | 44 +++++++ app/tims_keyboard.ld | 43 +++++++ app/usart.c | 100 ++++++++++++++++ app/usb.c | 133 +++++++++++++++++++++ 22 files changed, 1378 insertions(+) create mode 100644 app/.Makefile.swo create mode 100644 app/.Makefile.swp create mode 100644 app/.main.c.swp create mode 100644 app/.usart.c.swn create mode 100644 app/.usart.c.swo create mode 100644 app/Makefile create mode 100644 app/atkbd.c create mode 100644 app/consumer.c create mode 100644 app/dfu.c create mode 100644 app/gdb.script create mode 100644 app/keyboard.c create mode 100644 app/pins.h create mode 100644 app/project.h create mode 100644 app/prototypes.h create mode 100644 app/ring.c create mode 100644 app/ring.h create mode 100644 app/stdio.c create mode 100644 app/ticker.c create mode 100644 app/tims_keyboard.c create mode 100644 app/tims_keyboard.ld create mode 100644 app/usart.c create mode 100644 app/usb.c (limited to 'app') diff --git a/app/.Makefile.swo b/app/.Makefile.swo new file mode 100644 index 0000000..c28d39f Binary files /dev/null and b/app/.Makefile.swo differ diff --git a/app/.Makefile.swp b/app/.Makefile.swp new file mode 100644 index 0000000..db8a3c3 Binary files /dev/null and b/app/.Makefile.swp differ diff --git a/app/.main.c.swp b/app/.main.c.swp new file mode 100644 index 0000000..633f747 Binary files /dev/null and b/app/.main.c.swp differ diff --git a/app/.usart.c.swn b/app/.usart.c.swn new file mode 100644 index 0000000..88c369d Binary files /dev/null and b/app/.usart.c.swn differ diff --git a/app/.usart.c.swo b/app/.usart.c.swo new file mode 100644 index 0000000..e5985d3 Binary files /dev/null and b/app/.usart.c.swo differ diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..39d9779 --- /dev/null +++ b/app/Makefile @@ -0,0 +1,61 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +CPROTO=cproto +PROG=tims_keyboard + + +V=1 +default: ${PROG}.elf + +CSRCS=tims_keyboard.c dfu.c keyboard.c usb.c consumer.c atkbd.c ring.c usart.c stdio.c ticker.c + + +BINARY = ${PROG} +OBJS = ${CSRCS:%.c=%.o} + +include ../Makefile.include + +CFLAGS+=-Wno-redundant-decls -Wno-unused-parameter + +DID=$(shell printf '\#include "id.h"\nID_PRODUCT' | ${CC} -I.. -E - | grep -v ^\# ) + +INCLUDES += -I.. + +dfu:${PROG}.dfu + dfu-util -R -a 0 -d 1d6b:${DID} -s 0x08002000:leave -D $< + +program: ${PROG}.elf + echo halt | nc -t localhost 4444 + echo flash write_image erase ${PWD}/$< 0x2000 | nc -t localhost 4444 + echo reset run | nc -t localhost 4444 + +protos: + echo -n > prototypes.h + ${CPROTO} $(INCLUDES) $(DEFINES) -e -v ${CSRCS} > prototypes.h.tmp + mv -f prototypes.h.tmp prototypes.h + +ds: + $(Q)$(OOCD) -f $(OOCD_INTERFACE) \ + -f $(OOCD_BOARD) + +debug: ${PROG}.elf + ${PREFIX}-gdb -x gdb.script ${PROG}.elf + + diff --git a/app/atkbd.c b/app/atkbd.c new file mode 100644 index 0000000..0dfc9c8 --- /dev/null +++ b/app/atkbd.c @@ -0,0 +1,325 @@ +#include "project.h" + +#define KBCLK (1UL << 0) +#define KBCLK_PORT GPIOA +#define KBCLK_IRQ NVIC_EXTI0_IRQ + +#define KBDAT (1UL << 1) +#define KBDAT_PORT GPIOA + +/*Cope with lost sync */ +/*If we've had no bits for this long, it's definately the start of a new word, 72MHz units */ +#define RESYNC_GAP (7200000) /* 100ms */ + +#define ATKBD_TIMEOUT 1000 /*how long to wait for the keyboard to respond to a command in ms */ + + +typedef enum { + STATE_START=0, + STATE_BIT0, + STATE_BIT1, + STATE_BIT2, + STATE_BIT3, + STATE_BIT4, + STATE_BIT5, + STATE_BIT6, + STATE_BIT7, + STATE_PARITY, + STATE_STOP, +} atkbd_state; + + +#define ATKBD_CMD_SETLEDS 0xed +#define ATKBD_CMD_GSCANSET 0xf0 +#define ATKBD_CMD_SSCANSET 0xf0 +#define ATKBD_CMD_GETID 0xf2 +#define ATKBD_CMD_SETREP 0xf3 +#define ATKBD_CMD_ENABLE 0xf4 +#define ATKBD_CMD_RESET_DIS 0xf5 +#define ATKBD_CMD_RESET_DEF 0xf6 +#define ATKBD_CMD_SETALL_MB 0xf8 +#define ATKBD_CMD_SETALL_MBR 0xfa +#define ATKBD_CMD_RESET_BAT 0xff +#define ATKBD_CMD_RESEND 0xfe +#define ATKBD_CMD_EX_ENABLE 0xea +#define ATKBD_CMD_EX_SETLEDS 0xeb +#define ATKBD_CMD_OK_GETID 0xe8 + +#define ATKBD_RET_ECHO 0xee +#define ATKBD_RET_ACK 0xfa +#define ATKBD_RET_NAK 0xfe +#define ATKBD_RET_BAT 0xaa +#define ATKBD_RET_EMUL0 0xe0 +#define ATKBD_RET_EMUL1 0xe1 +#define ATKBD_RET_RELEASE 0xf0 +#define ATKBD_RET_HANJA 0xf1 +#define ATKBD_RET_HANGEUL 0xf2 +#define ATKBD_RET_ERR 0xff + +#define ATKBD_KEY_UNKNOWN 0 +#define ATKBD_KEY_NULL 255 + + +static int atkbd_ack; +static int atkbd_nack; +static int atkbd_bat; +static int atkbd_echo; + + + +static uint32_t cycle_diff(uint32_t a,uint32_t b) +{ +return b-a; +} + + +static void atkbd_dispatch(int emul,int key,int updown) +{ + +printf("KEY> %x %x %x\r\n",emul,key,updown); + + +} + +static void atkbd_data_dispatch(uint8_t byte) +{ +static int release; +static int emul; + +printf("ATKBD < 0x%02x\r\n",byte); + +switch (byte) { +case ATKBD_RET_ACK: + atkbd_ack++; + break; +case ATKBD_RET_NAK: + atkbd_nack++; + break; +case ATKBD_RET_BAT: + atkbd_bat++; + break; +case ATKBD_RET_ECHO: + atkbd_echo++; + break; +case ATKBD_RET_ERR: +case ATKBD_KEY_UNKNOWN: +case ATKBD_RET_HANJA: /*Don't handle japanese or korean for the moment*/ +case ATKBD_RET_HANGEUL: /*Don't handle japanese or korean for the moment*/ + /*All these need no action */ + break; +case ATKBD_RET_EMUL0: + emul=1; + break; +case ATKBD_RET_EMUL1: + emul=2; + break; +case ATKBD_RET_RELEASE: + release=1; + break; +default: + atkbd_dispatch(emul,byte,release); + emul=0; + release=0; +} + +} + + +void exti0_isr(void) +{ + static uint32_t last_interrupt=0; + static atkbd_state state=STATE_START; + static uint8_t atkbd_byte; + static int parity; + + uint32_t now,diff; + int d; + + d=!!GET(KBDAT); + + exti_reset_request(KBCLK); + + now=dwt_read_cycle_counter(); + diff=cycle_diff(last_interrupt,now); + last_interrupt=now; + + + if (diff>RESYNC_GAP) state=STATE_START; + + + switch (state) { + case STATE_START: + atkbd_byte=0; + parity=0; + if (!d) state++; + break; + case STATE_BIT0: + case STATE_BIT1: + case STATE_BIT2: + case STATE_BIT3: + case STATE_BIT4: + case STATE_BIT5: + case STATE_BIT6: + case STATE_BIT7: + atkbd_byte|=d<<(state-STATE_BIT0); + /* fall through*/ + case STATE_PARITY: + parity ^= d; + state++; + break; + case STATE_STOP: + if (d && parity) atkbd_data_dispatch(atkbd_byte); + state=STATE_START; + break; + } +} + + + +void atkbd_set(int clk,int dat) +{ +if (clk) { + MAP_INPUT_PU(KBCLK); +} else { + CLEAR(KBCLK); + MAP_OUTPUT_PP(KBCLK); +} + + +if (dat) { + MAP_INPUT_PU(KBDAT); +} else { + CLEAR(KBDAT); + MAP_OUTPUT_PP(KBDAT); +} + +} + + +int atkbd_send(uint8_t d) +{ +uint32_t then=ticks; +int parity=1; +uint32_t c; + + +nvic_disable_irq(KBCLK_IRQ); + +#define PS2_CLOCK_SEIZE_DELAY 200 +#define PS2_BIT_DELAY 10 + +atkbd_set(0,1); +delay_us(PS2_CLOCK_SEIZE_DELAY); +atkbd_set(0,0); +delay_us(PS2_BIT_DELAY); +atkbd_set(1,0); +delay_us(PS2_BIT_DELAY); + +for (c=1;c<0x100;c<<=1) { +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +atkbd_set(1,c&d); +parity^=!!(c&d); +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +} +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +atkbd_set(1,parity); +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +atkbd_set(1,1); +while (GET(KBDAT)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + + + +while (!GET(KBDAT)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +exti_reset_request(KBCLK); +nvic_enable_irq(KBCLK_IRQ); + +printf("ATKBD > 0x%02x\r\n",d); + +return 0; + +err: +atkbd_set(1,1); +while (!GET(KBDAT)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +exti_reset_request(KBCLK); +nvic_enable_irq(KBCLK_IRQ); + +printf("ATKBD >! 0x%02x\r\n",d); +return -1; +} + +static int atkbd_reset(void) +{ +uint32_t then=ticks; +atkbd_bat=0; +atkbd_send(ATKBD_CMD_RESET_BAT); +while (!atkbd_bat) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +then=ticks; +atkbd_ack=0; +atkbd_send(ATKBD_CMD_SETALL_MBR); +while (!atkbd_ack) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + + +return 0; +} + +int atkbd_request_echo(void) +{ +uint32_t then=ticks; +atkbd_ack=0; +atkbd_send(ATKBD_CMD_ECHO); +while (!atkbd_echo) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +return 0; +} + + +int atkbd_set_leds(uint8_t leds) +{ +uint32_t then=ticks; +atkbd_ack=0; +atkbd_send(ATKBD_CMD_SETLEDS); +while (!atkbd_ack) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +then=ticks; +atkbd_ack=0; +atkbd_send(leds); +while (!atkbd_ack) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +return 0; +} + + +void atkbd_init(void) +{ +atkbd_set(1,1); +delay_ms(200); + +nvic_enable_irq(NVIC_EXTI0_IRQ); + + +exti_select_source(KBCLK, KBCLK_PORT); +exti_set_trigger(KBCLK, EXTI_TRIGGER_FALLING); +exti_enable_request(KBCLK); + +exti_reset_request(KBCLK); +nvic_enable_irq(KBCLK_IRQ); + + +atkbd_reset(); +atkbd_request_echo(); +atkbd_set_leds(0x07); + + +} + + diff --git a/app/consumer.c b/app/consumer.c new file mode 100644 index 0000000..044c15b --- /dev/null +++ b/app/consumer.c @@ -0,0 +1,93 @@ +#include "project.h" + + +static const uint8_t consumer_report_descriptor[] = { + 0x05, 0x0c, /* Usage Page (Consumer Devices) */ + 0x09, 0x01, /* Usage (Consumer Control) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x15, 0x00, /* Logical minimum (0) */ + 0x25, 0x01, /* Logical maximum (1) */ + 0x75, 0x01, /* Report size (1) */ + 0x95, 0x10, /* Report count (16) */ + + 0x09, 0xb5, /* USAGE (Scan Next Track) */ + 0x09, 0xb6, /* USAGE (Scan Prev Track) */ + 0x09, 0xb7, /* USAGE (Stop) */ + 0x09, 0xcd, /* USAGE (Play/Pause) */ + + 0x09, 0xe2, /* USAGE (Mute) */ + 0x09, 0xe9, /* USAGE (Volume up) */ + 0x09, 0xea, /* USAGE (Volume down) */ + 0x0a, 0x21, 0x02, /* USAGE (www search) */ + + 0x0a, 0x23, 0x02, /* USAGE (www home) */ + 0x0a, 0x24, 0x02, /* USAGE (www back) */ + 0x0a, 0x25, 0x02, /* USAGE (www forward) */ + 0x0a, 0x26, 0x02, /* USAGE (www stop) */ + + 0x0a, 0x27, 0x02, /* USAGE (www refresh) */ + 0x0a, 0x2a, 0x02, /* USAGE (www favourites) */ + 0x0a, 0x8a, 0x01, /* USAGE (Mail) */ + 0x0a, 0x92, 0x01, /* USAGE (Calculator) */ + + 0x81, 0x62, /* Input (data, variable, relative, preferreed) */ + 0xC0 /* End Collection */ +}; + + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) consumer_function = +{ + .hid_descriptor = + { + .bLength = sizeof (consumer_function),.bDescriptorType = + USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (consumer_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor consumer_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 2, + .bInterval = 0x1 //0x20, +}; + +const struct usb_interface_descriptor consumer_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 1, /* consumer */ + .iInterface = 6, + + .endpoint = &consumer_endpoint, + + .extra = &consumer_function, + .extralen = sizeof (consumer_function), +}; + + +void +consumer_get_descriptor (uint8_t ** buf, uint16_t * len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) consumer_report_descriptor; + *len = sizeof (consumer_report_descriptor); +} + diff --git a/app/dfu.c b/app/dfu.c new file mode 100644 index 0000000..285c2b2 --- /dev/null +++ b/app/dfu.c @@ -0,0 +1,80 @@ +#include "project.h" + + +extern uint32_t dfu_flag; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof (struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor dfu_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 4, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 9, + + .extra = &dfu_function, + .extralen = sizeof (dfu_function), +}; + + +static void +dfu_detach_complete (usbd_device * usbd_dev, struct usb_setup_data *req) +{ + (void) req; + (void) usbd_dev; + + dfu_flag = 0xfee1dead; + + scb_reset_core (); +} + +int +dfu_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, + uint8_t ** buf, uint16_t * len, + void (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) +{ + (void) buf; + (void) len; + (void) usbd_dev; + + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) + { + case DFU_GETSTATUS: + { + (*buf)[0] = DFU_STATUS_OK; + (*buf)[1] = 0; + (*buf)[2] = 0; + (*buf)[3] = 0; + (*buf)[4] = STATE_APP_IDLE; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = STATE_APP_IDLE; + *len = 1; + return 1; + case DFU_DETACH: + *complete = dfu_detach_complete; + return 1; + } + + return 0; + +} diff --git a/app/gdb.script b/app/gdb.script new file mode 100644 index 0000000..7cf9d09 --- /dev/null +++ b/app/gdb.script @@ -0,0 +1,2 @@ +target remote localhost:3333 +cont diff --git a/app/keyboard.c b/app/keyboard.c new file mode 100644 index 0000000..44eb6c6 --- /dev/null +++ b/app/keyboard.c @@ -0,0 +1,117 @@ +#include "project.h" + +static const uint8_t keyboard_report_descriptor[] = { + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x06, /* Usage (Keyboard) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x05, 0x07, /* Usage page (Key Codes) */ + 0x19, 0xE0, /* Usage minimum (224) */ + 0x29, 0xE7, /* Usage maximum (231) */ + 0x15, 0x00, /* Logical minimum (0) */ + 0x25, 0x01, /* Logical maximum (1) */ + 0x75, 0x01, /* Report size (1) */ + 0x95, 0x08, /* Report count (8) */ + 0x81, 0x02, /* Input (data, variable, absolute) */ + 0x95, 0x01, /* Report count (1) */ + 0x75, 0x08, /* Report size (8) */ + 0x81, 0x01, /* Input (constant) */ + 0x95, 0x06, /* Report count (6) */ + 0x75, 0x08, /* Report size (8) */ + 0x15, 0x00, /* Logical minimum (0) */ + //0x25, 0x65, /* Logical maximum (101) */ + 0x26, 0xff, 0x00, /* Logical maximum (255) */ + //0x26, 0x7f, 0x00, /* Logical maximum (255) */ + 0x05, 0x07, /* Usage page (key codes) */ + 0x19, 0x00, /* Usage minimum (0) */ + 0x2A, 0xff, 0x00, /* Usage maximum (255) */ +// 0x29, 0x65, /* Usage maximum (101) */ +// 0x2A, 0xff, 0x03, /* Usage maximum (1023) */ + 0x81, 0x00, /* Input (data, array) */ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x80, /* USAGE (System Control) */ + 0xA1, 0x01, /* COLLECTION (Application) */ + 0x75, 0x02, /* REPORT_SIZE (2) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x15, 0x01, /* LOGICAL_MIN (1) */ + 0x25, 0x03, /* LOGICAL_MAX (3) */ + 0x09, 0x82, /* USAGE (System Sleep) */ + 0x09, 0x81, /* USAGE (System Power Down) */ + 0x09, 0x83, /* USAGE (System Wake Up) */ + 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x81, 0x03, /* INPUT (Cnst Var Abs) */ + 0xc0, /* END COLLECTION */ +0xC0 /* End Collection */ +}; + + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) keyboard_function = +{ + .hid_descriptor = + { + .bLength = sizeof (keyboard_function),.bDescriptorType = + USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (keyboard_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor keyboard_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 9, + .bInterval = 0x1 //0x20, +}; + +const struct usb_interface_descriptor keyboard_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 1, /* keyboard */ + .iInterface = 5, + + .endpoint = &keyboard_endpoint, + + .extra = &keyboard_function, + .extralen = sizeof (keyboard_function), +}; + + +void +keyboard_get_descriptor (uint8_t ** buf, uint16_t * len) +{ + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) keyboard_report_descriptor; + *len = sizeof (keyboard_report_descriptor); +} + +void +keyboard_test (void) +{ + static int c = 0; + uint8_t buf[9] = { 0, 0, 0, 0, 0, 0, 0, 0 ,0}; + + + buf[0] = (c >> 1) & 7; + + buf[2] = (c & 1) ? 12 : 0; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x81, buf, 9); +} diff --git a/app/pins.h b/app/pins.h new file mode 100644 index 0000000..4c55d74 --- /dev/null +++ b/app/pins.h @@ -0,0 +1,49 @@ +#ifndef _PINS_H_ +#define _PINS_H_ + +/* st seem to change these with every chip revision */ + +#define MAP_AF(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, a ); \ + } while (0) + +/* STM32F1 doesn't have AF pull up, but also doesn't disconnect af inputs so just use regular pull up */ +#define MAP_AF_PU(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, a); \ + gpio_set( a ## _PORT, a); \ + } while (0) + +#define MAP_AF_OD(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, a ); \ + } while (0) + + +#define MAP_OUTPUT_PP(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, a ); \ + } while (0) + + +#define MAP_OUTPUT_OD(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, a ); \ + } while (0) + + +/* STM32F1 madly uses the output register to drive the other end of the resistor, so pull up */ +/* requires us to write a 1 there */ + +#define MAP_INPUT_PU(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, a); \ + gpio_set( a ## _PORT, a); \ + } while (0) + + +#define MAP_INPUT(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, a); \ + } while (0) + + +#define CLEAR(a) gpio_clear( a ## _PORT, a) +#define SET(a) gpio_set( a ## _PORT, a) +#define GET(a) gpio_get( a ## _PORT, a) + +#endif diff --git a/app/project.h b/app/project.h new file mode 100644 index 0000000..24ca271 --- /dev/null +++ b/app/project.h @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "ring.h" +#include "pins.h" + + +#include "id.h" + + +#include "prototypes.h" diff --git a/app/prototypes.h b/app/prototypes.h new file mode 100644 index 0000000..52b504f --- /dev/null +++ b/app/prototypes.h @@ -0,0 +1,57 @@ +/* tims_keyboard.c */ +extern int main(void); +/* dfu.c */ +extern const struct usb_dfu_descriptor dfu_function; +extern const struct usb_interface_descriptor dfu_iface; +extern int dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); +/* keyboard.c */ +extern const struct usb_endpoint_descriptor keyboard_endpoint; +extern const struct usb_interface_descriptor keyboard_iface; +extern void keyboard_get_descriptor(uint8_t **buf, uint16_t *len); +extern void keyboard_test(void); +/* usb.c */ +extern const struct usb_device_descriptor dev; +extern const struct usb_interface ifaces[]; +extern const struct usb_config_descriptor config; +extern usbd_device *usbd_dev; +extern void usb_set_config(usbd_device *usbd_dev, uint16_t wValue); +extern void usb_init(void); +extern void usb_run(void); +/* consumer.c */ +extern const struct usb_endpoint_descriptor consumer_endpoint; +extern const struct usb_interface_descriptor consumer_iface; +extern void consumer_get_descriptor(uint8_t **buf, uint16_t *len); +/* ps2.c */ +extern void exti0_isr(void); +extern void atkbd_set(int clk, int dat); +extern int atkbd_send(uint8_t d); +extern void atkbd_init(void); +/* ring.c */ +extern void ring_init(volatile ring_t *r, uint8_t *buf, size_t len); +extern int ring_write_byte(volatile ring_t *r, uint8_t c); +extern int ring_read_byte(volatile ring_t *r, uint8_t *c); +extern int ring_write(volatile ring_t *r, uint8_t *buf, size_t len, int blocking); +extern int ring_empty(volatile ring_t *r); +/* usart.c */ +extern ring_t rx1_ring; +extern ring_t tx1_ring; +extern void usart1_isr(void); +extern void usart1_queue(uint8_t d); +extern void usart1_drain(void); +extern int usart1_write(char *ptr, int len, int blocking); +extern void usart_init(void); +/* stdio.c */ +extern int _open(const char *name, int flags, int mode); +extern int _close(int file); +extern int _write(int file, char *buf, int nbytes); +extern int _read(int file, char *buf, int nbytes); +extern int _lseek(int file, int offset, int whence); +extern int isatty(int file); +extern void stdio_drain(void); +/* ticker.c */ +extern volatile uint32_t ticks; +extern void delay_us(uint32_t d); +extern void sys_tick_handler(void); +extern void delay_ms(uint32_t d); +extern int timed_out(uint32_t then, unsigned int ms); +extern void ticker_init(void); diff --git a/app/ring.c b/app/ring.c new file mode 100644 index 0000000..8f13567 --- /dev/null +++ b/app/ring.c @@ -0,0 +1,75 @@ +#include "project.h" + + +static inline size_t +ring_next (volatile ring_t * r, size_t p) +{ + p++; + if (p >= r->size) + p -= r->size; + return p; +} + +void +ring_init (volatile ring_t * r, uint8_t * buf, size_t len) +{ + r->data = buf; + r->size = len; + r->write = 0; + r->read = 0; +} + +int +ring_write_byte (volatile ring_t * r, uint8_t c) +{ + size_t n = ring_next (r, r->write); + + if (n == r->read) + return -EAGAIN; + + r->data[r->write] = c; + + r->write = n; + + return 0; +} + + +int +ring_read_byte (volatile ring_t * r, uint8_t * c) +{ + size_t n = ring_next (r, r->read); + + if (r->read == r->write) + return -EAGAIN; + + *c = r->data[r->read]; + r->read = n; + + return 0; +} + +int +ring_write (volatile ring_t * r, uint8_t * buf, size_t len, int blocking) +{ + while (len--) + { + if (blocking) { + while (ring_write_byte (r, *buf)); + buf++; + } else { + if (ring_write_byte (r, *(buf++))) + return -EAGAIN; + } + } + + return 0; +} + + + +int +ring_empty (volatile ring_t * r) +{ + return (r->read == r->write) ? 1 : 0; +} diff --git a/app/ring.h b/app/ring.h new file mode 100644 index 0000000..e551a5e --- /dev/null +++ b/app/ring.h @@ -0,0 +1,14 @@ +#ifndef _RING_H_ +#define _RING_H_ + +typedef struct ring +{ + uint8_t *data; + size_t size; + size_t write; + size_t read; +} ring_t; + +#endif + + diff --git a/app/stdio.c b/app/stdio.c new file mode 100644 index 0000000..b3fee4f --- /dev/null +++ b/app/stdio.c @@ -0,0 +1,83 @@ +#include "project.h" + +int +_open (const char *name, + int flags, + int mode) +{ + errno = ENOSYS; + return -1; /* Always fails */ + +} /* _open () */ + +int +_close (int file) +{ + errno = EBADF; + return -1; /* Always fails */ + +} /* _close () */ + +int +_write (int file, + char *buf, + int nbytes) +{ + + int ret; + + + ret=usart1_write(buf,nbytes, 1); + + if (ret<0) { + errno=-ret; + return -1; + } + + return ret; +} /* _write () */ + + +int +_read (int file, + char *buf, + int nbytes) +{ + + errno=-EAGAIN; + return -1; /* EOF */ + +} /* _read () */ + +#if 0 +int +_fstat (int file, + struct stat *st) +{ + st->st_mode = S_IFCHR; + return 0; + +} /* _fstat () */ +#endif + +int +_lseek (int file, + int offset, + int whence) +{ + return 0; + +} /* _lseek () */ + +int +isatty (int file) +{ + return 1; + +} /* _isatty () */ + + +void stdio_drain(void) +{ +usart1_drain(); +} diff --git a/app/ticker.c b/app/ticker.c new file mode 100644 index 0000000..0f7d856 --- /dev/null +++ b/app/ticker.c @@ -0,0 +1,76 @@ +#include "project.h" + + +static volatile uint32_t delay_ms_count; +volatile uint32_t ticks; +static uint32_t scale=10; + +void +delay_us (uint32_t d) +{ + d *= scale; + while (d--) + { + __asm__ ("nop"); + } +} + +void +sys_tick_handler (void) +{ + if (delay_ms_count) + delay_ms_count--; + + ticks++; +} + + + +void +delay_ms (uint32_t d) +{ + delay_ms_count = d; + while (delay_ms_count); +} + +int timed_out(uint32_t then,unsigned int ms) +{ +then=ticks-then; + +if (then>ms) return 1; +return 0; +} + + +void ticker_init(void) +{ + uint32_t v,w; + + /*Start periodic timer*/ + + systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); + /* 48MHz / 8 = > 6Mhz */ + systick_set_reload (6000); + /* 6MHz / 6000 => 1kHz */ + systick_interrupt_enable (); + systick_counter_enable (); + + /*Calibrate the delay loop */ + + + do + { + scale--; + v = ticks; + while (v == ticks); + delay_us (1000); + w = ticks; + v++; + w -= v; + } + while (w); + + +} + + diff --git a/app/tims_keyboard.c b/app/tims_keyboard.c new file mode 100644 index 0000000..e6a4aae --- /dev/null +++ b/app/tims_keyboard.c @@ -0,0 +1,44 @@ +#include "project.h" +extern uint32_t dfu_flag; + +int +main (void) +{ + + +// rcc_clock_setup_in_hsi_out_48mhz (); + //nvic_set_priority_grouping(NVIC_PriorityGroup_4); + + /*set up pll*/ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /*turn on clocks to periferals*/ + rcc_periph_clock_enable (RCC_GPIOA); + rcc_periph_clock_enable (RCC_GPIOB); + rcc_periph_clock_enable (RCC_GPIOC); + rcc_periph_clock_enable (RCC_AFIO); + rcc_periph_clock_enable (RCC_USART1); + rcc_periph_clock_enable (RCC_USART1); + dwt_enable_cycle_counter(); + + /*Change interrupt priorities so that USART trumps everything else */ + nvic_set_priority(NVIC_USART1_IRQ, 0x40); + nvic_set_priority(NVIC_EXTI0_IRQ, 0xff); + nvic_set_priority(NVIC_SYSTICK_IRQ, 0xff); + + usart_init(); + + ticker_init(); + + + printf("Hello world!\r\n"); + + atkbd_init(); + + usb_init (); + + + usb_run (); + + return 0; +} diff --git a/app/tims_keyboard.ld b/app/tims_keyboard.ld new file mode 100644 index 0000000..a2d5a9c --- /dev/null +++ b/app/tims_keyboard.ld @@ -0,0 +1,43 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08002000, LENGTH = 120K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + +dfu_shared_location = ORIGIN(ram) + LENGTH(ram) - 1024; + +/* PROVIDE(_stack = dfu_shared_location ); */ + +SECTIONS +{ + .dfu_shared dfu_shared_location :{ + dfu_flag = .; + } +} + + diff --git a/app/usart.c b/app/usart.c new file mode 100644 index 0000000..74e5383 --- /dev/null +++ b/app/usart.c @@ -0,0 +1,100 @@ +#include "project.h" + +#define BUFFER_SIZE 256 + + +#define USART1_TX GPIO_USART1_TX +#define USART1_TX_PORT GPIOA + +#define USART1_RX GPIO_USART1_RX +#define USART1_RX_PORT GPIOA + + + +ring_t rx1_ring; +static uint8_t rx1_ring_buf[BUFFER_SIZE]; + +ring_t tx1_ring; +static uint8_t tx1_ring_buf[BUFFER_SIZE]; + +void +usart1_isr (void) +{ + uint8_t data; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1 (USART1) & USART_CR1_RXNEIE) != 0) && + ((USART_SR (USART1) & USART_SR_RXNE) != 0)) + { + data = usart_recv (USART1); + ring_write_byte(&rx1_ring,data); + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1 (USART1) & USART_CR1_TXEIE) != 0) && + ((USART_SR (USART1) & USART_SR_TXE) != 0)) + { + + if (ring_read_byte (&tx1_ring, &data)) + { + /*No more data, Disable the TXE interrupt, it's no longer needed. */ + usart_disable_tx_interrupt(USART1); + } + else + { + usart_send_blocking (USART1, data); + } + } + +} + + +void +usart1_queue (uint8_t d) +{ + ring_write_byte (&tx1_ring, d); + usart_enable_tx_interrupt(USART1); +} + +void +usart1_drain (void) +{ + while (!ring_empty (&tx1_ring)); +} + + +int +usart1_write (char *ptr, int len,int blocking) +{ + int ret; + + ret = ring_write (&tx1_ring, (uint8_t *) ptr, len,blocking); + usart_enable_tx_interrupt(USART1); + return ret; +} + + +void +usart_init (void) +{ + ring_init (&rx1_ring, rx1_ring_buf, sizeof (rx1_ring_buf)); + ring_init (&tx1_ring, tx1_ring_buf, sizeof (tx1_ring_buf)); + + nvic_enable_irq (NVIC_USART1_IRQ); + + + MAP_AF(USART1_TX); + MAP_AF_PU(USART1_RX); + + + usart_set_baudrate (USART1, 38400); + usart_set_databits (USART1, 8); + usart_set_stopbits (USART1, USART_STOPBITS_1); + usart_set_parity (USART1, USART_PARITY_NONE); + usart_set_flow_control (USART1, USART_FLOWCONTROL_NONE); + usart_set_mode (USART1, USART_MODE_TX_RX); + + USART_CR1 (USART1) |= USART_CR1_RXNEIE; + + usart_enable (USART1); +} diff --git a/app/usb.c b/app/usb.c new file mode 100644 index 0000000..659a491 --- /dev/null +++ b/app/usb.c @@ -0,0 +1,133 @@ +#include "project.h" + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = ID_VENDOR, + .idProduct = ID_PRODUCT, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_interface ifaces[] = { + { + .num_altsetting = 1, + .altsetting = &keyboard_iface, + }, + { + .num_altsetting = 1, + .altsetting = &consumer_iface, + }, + { + .num_altsetting = 1, + .altsetting = &dfu_iface, + }, +}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 3, + .bConfigurationValue = 1, + .iConfiguration = 4, + .bmAttributes = 0xa0, + .bMaxPower = 0x31, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "James", + "Tim's keyboard", + "000001", + "composite", + "keyboard", + "media buttons", + "dfu device", +}; + + + +usbd_device *usbd_dev; + + +static int +usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, + uint8_t ** buf, uint16_t * len, + void (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) +{ + + (void) complete; + (void) usbd_dev; + + if ((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || (req->wValue != 0x2200)) + return 0; + + switch (req->wIndex) + { + case 0: + keyboard_get_descriptor (buf, len); + return 1; + case 1: + consumer_get_descriptor (buf, len); + return 1; + } + + *len = 0; + return 0; +} + + +void +usb_set_config (usbd_device * usbd_dev, uint16_t wValue) +{ + (void) wValue; + (void) usbd_dev; + + usbd_ep_setup (usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + usbd_ep_setup (usbd_dev, 0x82, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_STANDARD | + USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usb_control_request); + + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); +} + +/* Buffer to be used for control requests. */ +static uint8_t usbd_control_buffer[128]; + +void +usb_init (void) +{ + + usbd_dev = + usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, 8, + usbd_control_buffer, sizeof (usbd_control_buffer)); + + usbd_register_set_config_callback (usbd_dev, usb_set_config); + +} + +void +usb_run (void) +{ + while (1) + usbd_poll (usbd_dev); +} -- cgit v1.2.3