From 12287ff0a55f929bf840dcb4780d3f77b862c434 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 2 Nov 2014 18:17:44 +0000 Subject: fish --- Makefile.include | 10 +- Makefile.rules | 18 +-- app/Makefile | 48 ++++++++ app/dfu.c | 82 +++++++++++++ app/keyboard.c | 140 ++++++++++++++++++++++ app/kvm.c | 91 ++++++++++++++ app/kvm.ld | 43 +++++++ app/main.c | 57 +++++++++ app/mouse.c | 115 ++++++++++++++++++ app/project.h | 23 ++++ app/prototypes.h | 43 +++++++ app/ring.c | 61 ++++++++++ app/ring.h | 9 ++ app/tablet.c | 122 +++++++++++++++++++ app/usart.c | 104 ++++++++++++++++ app/usb.c | 146 +++++++++++++++++++++++ board/STM32F103R_BOARD.cfg | 3 + boot/Makefile | 32 +++++ boot/project.h | 23 ++++ boot/prototypes.h | 18 +++ boot/usbdfu.c | 288 +++++++++++++++++++++++++++++++++++++++++++++ boot/usbdfu.ld | 40 +++++++ interface/j-link.cfg | 5 + src/Makefile | 43 ------- src/dfu.c | 60 ---------- src/keyboard.c | 140 ---------------------- src/kvm.c | 91 -------------- src/kvm.ld | 31 ----- src/main.c | 55 --------- src/mouse.c | 115 ------------------ src/project.h | 23 ---- src/prototypes.h | 43 ------- src/ring.c | 61 ---------- src/ring.h | 9 -- src/tablet.c | 122 ------------------- src/usart.c | 104 ---------------- src/usb.c | 146 ----------------------- 37 files changed, 1511 insertions(+), 1053 deletions(-) create mode 100644 app/Makefile create mode 100644 app/dfu.c create mode 100644 app/keyboard.c create mode 100644 app/kvm.c create mode 100644 app/kvm.ld create mode 100644 app/main.c create mode 100644 app/mouse.c 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/tablet.c create mode 100644 app/usart.c create mode 100644 app/usb.c create mode 100644 board/STM32F103R_BOARD.cfg create mode 100644 boot/Makefile create mode 100644 boot/project.h create mode 100644 boot/prototypes.h create mode 100644 boot/usbdfu.c create mode 100644 boot/usbdfu.ld create mode 100644 interface/j-link.cfg delete mode 100644 src/Makefile delete mode 100644 src/dfu.c delete mode 100644 src/keyboard.c delete mode 100644 src/kvm.c delete mode 100644 src/kvm.ld delete mode 100644 src/main.c delete mode 100644 src/mouse.c delete mode 100644 src/project.h delete mode 100644 src/prototypes.h delete mode 100644 src/ring.c delete mode 100644 src/ring.h delete mode 100644 src/tablet.c delete mode 100644 src/usart.c delete mode 100644 src/usb.c diff --git a/Makefile.include b/Makefile.include index adfecf6..3eccc25 100644 --- a/Makefile.include +++ b/Makefile.include @@ -27,9 +27,13 @@ ARCH_FLAGS = -mthumb -mcpu=cortex-m3 $(FP_FLAGS) -mfix-cortex-m3-ldrd ################################################################################ # OpenOCD specific variables -OOCD ?= openocd -OOCD_INTERFACE ?= flossjtag -OOCD_BOARD ?= olimex_stm32_h103 +#OOCD ?= openocd +#OOCD_INTERFACE ?= flossjtag +#OOCD_BOARD ?= olimex_stm32_h103 + +OOCD ?= openocd +OOCD_INTERFACE ?= ../interface/j-link.cfg +OOCD_BOARD ?= ../board/STM32F103R_BOARD.cfg ################################################################################ # Black Magic Probe specific variables diff --git a/Makefile.rules b/Makefile.rules index b67e343..73bdf20 100644 --- a/Makefile.rules +++ b/Makefile.rules @@ -123,7 +123,7 @@ LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group ############################################################################### ############################################################################### -.SUFFIXES: .elf .bin .hex .srec .list .map .images +.SUFFIXES: .elf .bin .hex .srec .list .map .images .dfu .SECONDEXPANSION: .SECONDARY: @@ -138,7 +138,7 @@ list: $(BINARY).list images: $(BINARY).images flash: $(BINARY).flash -%.images: %.bin %.hex %.srec %.list %.map +%.images: %.bin %.hex %.srec %.list %.map %.dfu @#printf "*** $* images generated ***\n" %.bin: %.elf @@ -149,6 +149,10 @@ flash: $(BINARY).flash @#printf " OBJCOPY $(*).hex\n" $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex +%.dfu: %.elf + @#printf " OBJCOPY $(*).dfu\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).dfu + %.srec: %.elf @#printf " OBJCOPY $(*).srec\n" $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec @@ -179,7 +183,7 @@ fish: clean: @#printf " CLEAN\n" - $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map *~ + $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map *~ *.dfu stylecheck: $(STYLECHECKFILES:=.stylecheck) styleclean: $(STYLECHECKFILES:=.styleclean) @@ -207,8 +211,8 @@ ifeq ($(OOCD_SERIAL),) %.flash: %.hex @printf " FLASH $<\n" @# IMPORTANT: Don't use "resume", only "reset" will work correctly! - $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ - -f board/$(OOCD_BOARD).cfg \ + $(Q)$(OOCD) -f $(OOCD_INTERFACE) \ + -f $(OOCD_BOARD) \ -c "init" -c "reset init" \ -c "flash write_image erase $(*).hex" \ -c "reset" \ @@ -217,8 +221,8 @@ else %.flash: %.hex @printf " FLASH $<\n" @# IMPORTANT: Don't use "resume", only "reset" will work correctly! - $(Q)$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \ - -f board/$(OOCD_BOARD).cfg \ + $(Q)$(OOCD) -f $(OOCD_INTERFACE) \ + -f $(OOCD_BOARD) \ -c "ft2232_serial $(OOCD_SERIAL)" \ -c "init" -c "reset init" \ -c "flash write_image erase $(*).hex" \ diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..08febc7 --- /dev/null +++ b/app/Makefile @@ -0,0 +1,48 @@ +## +## 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=kvm + +V=1 +default: ${PROG}.elf + +CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c usart.c kvm.c ring.c + + +BINARY = ${PROG} +OBJS = ${CSRCS:%.c=%.o} + +${PROG}.dfu:${PROG}.elf + ${OBJCOPY} -Obinary $< $@ + +dfu:${PROG}.dfu + dfu-util -R -a 0 -d 1d6b:0ee3 -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 + +include ../Makefile.include diff --git a/app/dfu.c b/app/dfu.c new file mode 100644 index 0000000..554aca9 --- /dev/null +++ b/app/dfu.c @@ -0,0 +1,82 @@ +#include "project.h" + +#ifdef INCLUDE_DFU_INTERFACE + +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 = 3, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 0, + + .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; + +} +#endif diff --git a/app/keyboard.c b/app/keyboard.c new file mode 100644 index 0000000..e9046a3 --- /dev/null +++ b/app/keyboard.c @@ -0,0 +1,140 @@ +#include "project.h" + +#if 0 +static const uint8_t keyboard_report_descriptor[] = { + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x01, /* USAGE (Keyboard */ + 0xa1, 0x01, /* COLLECTION (Application) */ + 0x05, 0x07, /* USAGE_PAGE (Key Codes) */ + 0x19, 0xe0, /* USAGE_MINIMUM (Control Left) */ + 0x29, 0xe7, /* USAGE_MAXIMUM (GUI Right) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0x95, 0x08, /* REPORT_COUNT (8) */ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */ + 0x95, 0x05, /* REPORT_COUNT (5) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ +#if 0 + 0x05, 0x08, /* USAGE_PAGE (LEDs) */ + 0x19, 0x01, /* USAGE_MINIMUM (NumLock) */ + 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */ + 0x ? ?, 0x02, /* OUTPUT (Data,Var,Abs) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x75, 0x03, /* REPORT_SIZE (3) */ + 0x ? ?, 0x01, /* OUTPUT (Cnst,Ary,Abs) */ + 0x95, 0x06, /* REPORT_COUNT (6) */ + 0x75, 0x03, /* REPORT_SIZE (8) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0xff, 0x00, /*LOGICAL_MAXIMUM (255) */ +#endif + 0x05, 0x07, /* USAGE_PAGE (Key Codes) */ + 0x19, 0x00, /* USAGE_MINIMUM (?) */ + 0x29, 0x98, /* USAGE_MAXIMUM (LANG 9) */ + 0xc0 /* END_COLLECTION */ +}; +#endif + +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) */ + 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) */ + 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 = 8, + .bInterval = 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 = 0, + + .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[8] = { 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, 8); +} diff --git a/app/kvm.c b/app/kvm.c new file mode 100644 index 0000000..3c8a900 --- /dev/null +++ b/app/kvm.c @@ -0,0 +1,91 @@ +#include "project.h" + + +static enum +{ + STATE_SYNC = 0, + STATE_ADDR = 1, + STATE_ENDPOINT = 2, + STATE_LEN = 3, + STATE_DATA = 4, +} state = STATE_SYNC; + + +static uint8_t buf[16]; +static int addr; +static int endpoint; +static unsigned int len; +static unsigned int ptr; + +void +kvm_dispatch (void) +{ +#if 0 + printf + ("Addr %d, Write to ep %x, %d bytes %02x %02x %02x %02x ...\r\n", + addr, endpoint, len, buf[0], buf[1], buf[2], buf[3]); +#endif + + if (len) + usbd_ep_write_packet (usbd_dev, endpoint, buf, len); +} + +void +kvm_recv (uint8_t d) +{ +// printf ("S%d V0x%02x\r\n", state, d); + + if (state == STATE_ADDR) + { + addr = d; + usart_queue (d - 1); + state = STATE_ENDPOINT; + return; + } + else + { + usart_queue (d); + } + + + switch (state) + { + case STATE_SYNC: + if (d != 0x5a) + break; + state = STATE_ADDR; + break; + case STATE_ENDPOINT: + endpoint = d; + if ((endpoint < 0x81) || (endpoint > 0x83)) + { + state = STATE_SYNC; + } + else + { + state = STATE_LEN; + } + break; + case STATE_LEN: + len = d; + ptr = 0; + if (len > sizeof (buf)) + { + state = STATE_SYNC; + } + else + { + state = STATE_DATA; + } + break; + case STATE_DATA: + buf[ptr++] = d; + if (ptr >= len) + { + if (!addr) + kvm_dispatch (); + state = STATE_SYNC; + } + break; + } +} diff --git a/app/kvm.ld b/app/kvm.ld new file mode 100644 index 0000000..a2d5a9c --- /dev/null +++ b/app/kvm.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/main.c b/app/main.c new file mode 100644 index 0000000..508580d --- /dev/null +++ b/app/main.c @@ -0,0 +1,57 @@ +#include "project.h" +extern uint32_t dfu_flag; + +void +sys_tick_handler (void) +{ +#if 0 + printf ("fish\r\n"); + keyboard_test (); + mouse_test (); + tablet_test (); +#endif +} + +int +main (void) +{ + int i; + + + rcc_clock_setup_in_hsi_out_48mhz (); + rcc_periph_clock_enable (RCC_GPIOC); + rcc_periph_clock_enable (RCC_GPIOA); + rcc_periph_clock_enable (RCC_AFIO); + rcc_periph_clock_enable (RCC_USART1); + + + usart_init (); + + usb_init (); + + systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); + /* 48MHz / 8 = > 6Mhz */ + systick_set_reload (3000000); + /* 6MHz / 3000000 => 2Hz */ + + systick_interrupt_enable (); + systick_counter_enable (); + + gpio_set (GPIOC, GPIO11); + gpio_set_mode (GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + + + + for (i = 0; i < 0x80000; i++) + __asm__ ("nop"); + + gpio_clear (GPIOC, GPIO11); + + printf ("Hello world main=%p\r\n", main); + printf ("dfu_flag=%08lx\r\n", dfu_flag); + + usb_run (); + + return 0; +} diff --git a/app/mouse.c b/app/mouse.c new file mode 100644 index 0000000..6e0a207 --- /dev/null +++ b/app/mouse.c @@ -0,0 +1,115 @@ +#include "project.h" + +static const uint8_t mouse_report_descriptor[] = { + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x02, /* USAGE (Mouse) */ + 0xa1, 0x01, /* COLLECTION (Application) */ + 0x09, 0x01, /* USAGE (Pointer) */ + 0xa1, 0x00, /* COLLECTION (Physical) */ + 0x05, 0x09, /* USAGE_PAGE (Button) */ + 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ + 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x95, 0x03, /* REPORT_COUNT (3) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0x81, 0x02, /* INPUT (Data,Var,Abs) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x75, 0x05, /* REPORT_SIZE (5) */ + 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x30, /* USAGE (X) */ + 0x09, 0x31, /* USAGE (Y) */ + 0x09, 0x38, /* USAGE (Wheel) */ + 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ + 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x95, 0x03, /* REPORT_COUNT (3) */ + 0x81, 0x06, /* INPUT (Data,Var,Rel) */ + 0xc0, /* END_COLLECTION */ + 0x09, 0x3c, /* USAGE (Motion Wakeup) */ + 0x05, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */ + 0x09, 0x01, /* USAGE (Vendor Usage 1) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ + 0x95, 0x02, /* REPORT_COUNT (2) */ + 0xb1, 0x22, /* FEATURE (Data,Var,Abs,NPrf) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0xb1, 0x01, /* FEATURE (Cnst,Ary,Abs) */ + 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)) mouse_function = +{ + .hid_descriptor = + { + .bLength = sizeof (mouse_function),.bDescriptorType = USB_DT_HID,.bcdHID = + 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (mouse_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor mouse_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 4, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor mouse_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* mouse */ + .iInterface = 0, + + .endpoint = &mouse_endpoint, + + .extra = &mouse_function, + .extralen = sizeof (mouse_function), +}; + + +void +mouse_get_descriptor (uint8_t ** buf, uint16_t * len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) mouse_report_descriptor; + *len = sizeof (mouse_report_descriptor); +} + +void +mouse_test (void) +{ + static int c = 1; + uint8_t buf[4] = { 0, 0, 0, 0 }; + + buf[0] = c & 7; + + buf[1] = c & 8 ? -1 : 1; + buf[2] = c & 16 ? -1 : 1; + buf[3] = c & 32 ? -1 : 1; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x82, buf, 4); +} diff --git a/app/project.h b/app/project.h new file mode 100644 index 0000000..0323569 --- /dev/null +++ b/app/project.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define INCLUDE_DFU_INTERFACE + + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +#include +#include + +#include "ring.h" + +#include "prototypes.h" diff --git a/app/prototypes.h b/app/prototypes.h new file mode 100644 index 0000000..0e971df --- /dev/null +++ b/app/prototypes.h @@ -0,0 +1,43 @@ +/* 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)); +/* mouse.c */ +extern const struct usb_endpoint_descriptor mouse_endpoint; +extern const struct usb_interface_descriptor mouse_iface; +extern void mouse_get_descriptor(uint8_t **buf, uint16_t *len); +extern void mouse_test(void); +/* 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); +/* main.c */ +extern void sys_tick_handler(void); +extern int main(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); +/* tablet.c */ +extern const struct usb_endpoint_descriptor tablet_endpoint; +extern const struct usb_interface_descriptor tablet_iface; +extern void tablet_get_descriptor(uint8_t **buf, uint16_t *len); +extern void tablet_test(void); +/* usart.c */ +extern void usart1_isr(void); +extern int _write(int file, char *ptr, int len); +extern void usart_queue(uint8_t d); +extern void usart_init(void); +/* kvm.c */ +extern void kvm_dispatch(void); +extern void kvm_recv(uint8_t d); +/* ring.c */ +extern void ring_init(ring_t *r, uint8_t *buf, size_t len); +extern int ring_write_byte(ring_t *r, uint8_t c); +extern int ring_read_byte(ring_t *r, uint8_t *c); +extern int ring_write(ring_t *r, uint8_t *buf, size_t len); diff --git a/app/ring.c b/app/ring.c new file mode 100644 index 0000000..4f38e44 --- /dev/null +++ b/app/ring.c @@ -0,0 +1,61 @@ +#include "project.h" + + +static inline size_t +ring_next (ring_t * r, size_t p) +{ + p++; + if (p >= r->size) + p -= r->size; + return p; +} + +void +ring_init (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 (ring_t * r, uint8_t c) +{ + size_t n = ring_next (r, r->write); + + if (n == r->read) + return -1; + + r->data[r->write] = c; + r->write = n; + + return 0; +} + + +int +ring_read_byte (ring_t * r, uint8_t * c) +{ + size_t n = ring_next (r, r->read); + + if (r->read == r->write) + return -1; + + *c = r->data[r->read]; + r->read = n; + + return 0; +} + +int +ring_write (ring_t * r, uint8_t * buf, size_t len) +{ + while (len--) + { + if (ring_write_byte (r, *(buf++))) + return -1; + } + + return 0; +} diff --git a/app/ring.h b/app/ring.h new file mode 100644 index 0000000..4e3a22c --- /dev/null +++ b/app/ring.h @@ -0,0 +1,9 @@ +typedef struct ring +{ + uint8_t *data; + size_t size; + size_t write; + size_t read; +} ring_t; + + diff --git a/app/tablet.c b/app/tablet.c new file mode 100644 index 0000000..1073b37 --- /dev/null +++ b/app/tablet.c @@ -0,0 +1,122 @@ +#include "project.h" + +static const uint8_t tablet_report_descriptor[] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x02, // USAGE (Mouse) + 0xa1, 0x01, // COLLECTION (Application) + 0x09, 0x01, // USAGE (Pointer) + 0xa1, 0x00, // COLLECTION (Physical) + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x02, // REPORT_COUNT (2) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x38, // USAGE (Wheel) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x03, // USAGE_MAXIMUM (Button 3) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x95, 0x03, // REPORT_COUNT (3) + 0x75, 0x01, // REPORT_SIZE (1) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x95, 0x01, // REPORT_COUNT (1) + 0x75, 0x05, // REPORT_SIZE (5) + 0x81, 0x01, // INPUT (Cnst,Ary,Abs) + 0xc0, // END_COLLECTION + 0x09, 0x3c, // USAGE (Button 60) + 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1) + 0x09, 0x01, // USAGE (Vendor Usage 1) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x25, 0x01, // LOGICAL_MAXIMUM (1) + 0x75, 0x01, // REPORT_SIZE (1) + 0x95, 0x02, // REPORT_COUNT (2) + 0xb1, 0x22, // FEATURE (Data,Var,Abs,NPrf) + 0x75, 0x06, // REPORT_SIZE (6) + 0x95, 0x01, // REPORT_COUNT (1) + 0xb1, 0x01, // FEATURE (Cnst,Ary,Abs) + 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)) tablet_function = +{ + .hid_descriptor = + { + .bLength = sizeof (tablet_function),.bDescriptorType = + USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (tablet_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor tablet_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 6, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor tablet_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 2, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* tablet */ + .iInterface = 0, + + .endpoint = &tablet_endpoint, + + .extra = &tablet_function, + .extralen = sizeof (tablet_function), +}; + + +void +tablet_get_descriptor (uint8_t ** buf, uint16_t * len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) tablet_report_descriptor; + *len = sizeof (tablet_report_descriptor); +} + +void +tablet_test (void) +{ + static int c = 0; + uint8_t buf[6] = { 0, 0, 0, 0, 0 }; + + buf[0] = c & 0xff; + buf[1] = (c & 0x7fff) >> 8; + buf[2] = (32767 - (c & 0x7fff)) & 0xff; + buf[3] = (32767 - (c & 0x7fff)) >> 8; + buf[4] = (c & 8) ? -1 : 1; + buf[5] = c; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x83, buf, 6); +} diff --git a/app/usart.c b/app/usart.c new file mode 100644 index 0000000..f510682 --- /dev/null +++ b/app/usart.c @@ -0,0 +1,104 @@ +#include "project.h" + +#define BUFFER_SIZE 256 + +static ring_t output_ring; +static uint8_t output_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)) + { + + /* Retrieve the data from the peripheral. */ + data = usart_recv (USART1); + + kvm_recv (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 (&output_ring, &data)) + { + /*No more data, Disable the TXE interrupt, it's no longer needed. */ + USART_CR1 (USART1) &= ~USART_CR1_TXEIE; + } + else + { + usart_send (USART1, data); + } + } + +} + +int +_write (int file, char *ptr, int len) +{ + int ret; + + if (file == 1) + { + ret = ring_write (&output_ring, (uint8_t *) ptr, len); + + if (ret < 0) + ret = -ret; + + USART_CR1 (USART1) |= USART_CR1_TXEIE; + return ret; + } + + errno = EIO; + return -1; +} + +void +usart_queue (uint8_t d) +{ + ring_write_byte (&output_ring, d); + USART_CR1 (USART1) |= USART_CR1_TXEIE; + +#if 0 + printf ("0x%02x ", d); + fflush (stdout); +#endif +} + + +void +usart_init (void) +{ + ring_init (&output_ring, output_ring_buf, sizeof (output_ring_buf)); + + /* Enable the USART1 interrupt. */ + nvic_enable_irq (NVIC_USART1_IRQ); + + /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */ + gpio_set_mode (GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); + + /* Setup UART parameters. */ + 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); + + /* Enable USART1 Receive interrupt. */ + USART_CR1 (USART1) |= USART_CR1_RXNEIE; + + /* Finally enable the USART. */ + usart_enable (USART1); +} diff --git a/app/usb.c b/app/usb.c new file mode 100644 index 0000000..f796162 --- /dev/null +++ b/app/usb.c @@ -0,0 +1,146 @@ +#include "project.h" + +/* Define this to include the DFU APP interface. */ +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 = 0x1d6b, + .idProduct = 0x0ee3, + .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 = &mouse_iface, + }, + { + .num_altsetting = 1, + .altsetting = &tablet_iface, + }, +#ifdef INCLUDE_DFU_INTERFACE + { + .num_altsetting = 1, + .altsetting = &dfu_iface, +#endif + } +}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, +#ifdef INCLUDE_DFU_INTERFACE + .bNumInterfaces = 4, +#else + .bNumInterfaces = 3, +#endif + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Cabbages are good for you", + "fish", + "soup", +}; + + + +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: + mouse_get_descriptor (buf, len); + return 1; + case 2: + tablet_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_ep_setup (usbd_dev, 0x83, 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); + +#ifdef INCLUDE_DFU_INTERFACE + 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); +#endif +} + +/* 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, 3, + 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); +} diff --git a/board/STM32F103R_BOARD.cfg b/board/STM32F103R_BOARD.cfg new file mode 100644 index 0000000..6d1d72b --- /dev/null +++ b/board/STM32F103R_BOARD.cfg @@ -0,0 +1,3 @@ +# + +source [find target/stm32f1x.cfg] diff --git a/boot/Makefile b/boot/Makefile new file mode 100644 index 0000000..679662b --- /dev/null +++ b/boot/Makefile @@ -0,0 +1,32 @@ +## +## 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 . +## + + +CSRCS=usbdfu.c +PROG = usbdfu + +CPROTO=cproto +V=1 +BINARY = ${PROG} +OBJS = ${CSRCS:%.c=%.o} + +include ../Makefile.include + +CFLAGS += -g + diff --git a/boot/project.h b/boot/project.h new file mode 100644 index 0000000..0323569 --- /dev/null +++ b/boot/project.h @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define INCLUDE_DFU_INTERFACE + + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +#include +#include + +#include "ring.h" + +#include "prototypes.h" diff --git a/boot/prototypes.h b/boot/prototypes.h new file mode 100644 index 0000000..45d399c --- /dev/null +++ b/boot/prototypes.h @@ -0,0 +1,18 @@ +/* usbdfu.c */ +extern uint8_t usbd_control_buffer[1024]; +extern const struct usb_device_descriptor dev; +extern const struct usb_dfu_descriptor dfu_function; +extern const struct usb_interface_descriptor iface; +extern const struct usb_interface ifaces[]; +extern const struct usb_config_descriptor config; +extern int main(void); +/* ring.c */ +extern void ring_init(ring_t *r, uint8_t *buf, size_t len); +extern int ring_write_byte(ring_t *r, uint8_t c); +extern int ring_read_byte(ring_t *r, uint8_t *c); +extern int ring_write(ring_t *r, uint8_t *buf, size_t len); +/* usart.c */ +extern void usart1_isr(void); +extern int _write(int file, char *ptr, int len); +extern void usart_queue(uint8_t d); +extern void usart_init(void); diff --git a/boot/usbdfu.c b/boot/usbdfu.c new file mode 100644 index 0000000..33d48ec --- /dev/null +++ b/boot/usbdfu.c @@ -0,0 +1,288 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +void usb_set_config (usbd_device * usbd_dev, uint16_t wValue); + +/* We need a special large control buffer for this device: */ +uint8_t usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +extern uint32_t dfu_flag; + +static struct { + uint8_t buf[sizeof(usbd_control_buffer)]; + uint16_t len; + uint32_t addr; + uint16_t blocknum; +} prog; + +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 = 0x1d6b, + .idProduct = 0x0ee3, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +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 iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Cabbages are good for you", + "fish", + "soup", + /* This string is used by ST Microelectronics' DfuSe utility. */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg", +}; + +static uint8_t usbdfu_getstatus(usbd_device *usbd_dev, uint32_t *bwPollTimeout) +{ + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete. */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + int i; + (void)req; + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNBUSY: + flash_unlock(); + if (prog.blocknum == 0) { + switch (prog.buf[0]) { + case CMD_ERASE: + { + uint32_t *dat = (uint32_t *)(prog.buf + 1); + flash_erase_page(*dat); + } + case CMD_SETADDR: + { + uint32_t *dat = (uint32_t *)(prog.buf + 1); + prog.addr = *dat; + } + } + } else { + uint32_t baseaddr = prog.addr + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for (i = 0; i < prog.len; i += 2) { + uint16_t *dat = (uint16_t *)(prog.buf + i); + flash_program_half_word(baseaddr + i, + *dat); + } + } + flash_lock(); + + /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return. */ + default: + return; + } +} + +static int usbdfu_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)) +{ + + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) { + case DFU_DNLOAD: + if ((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS. */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE. */ + if (usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* Abort returns to dfuIDLE state. */ + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_UPLOAD: + /* Upload not supported for now. */ + return 0; + case DFU_GETSTATUS: { + uint32_t bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + (*buf)[0] = usbdfu_getstatus(usbd_dev, &bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + *complete = usbdfu_getstatus_complete; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + return 0; +} + +void +usb_set_config (usbd_device * usbd_dev, uint16_t wValue) +{ + (void) wValue; + (void) usbd_dev; + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); + +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_periph_clock_enable(RCC_GPIOA); + + if (dfu_flag!=0xfee1dead) { + /* Boot the application if it's valid. */ + if ((*(volatile uint32_t *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address. */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer. */ + asm volatile("msr msp, %0"::"g" + (*(volatile uint32_t *)APP_ADDRESS)); + /* Jump to application. */ + (*(void (**)())(APP_ADDRESS + 4))(); + } + } + + dfu_flag=0; + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_periph_clock_enable(RCC_GPIOC); + + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + gpio_set(GPIOC, GPIO11); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 4, usbd_control_buffer, sizeof(usbd_control_buffer)); + + usbd_register_set_config_callback (usbd_dev, usb_set_config); + + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/boot/usbdfu.ld b/boot/usbdfu.ld new file mode 100644 index 0000000..8680dc9 --- /dev/null +++ b/boot/usbdfu.ld @@ -0,0 +1,40 @@ +/* + * 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 = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + +dfu_shared_location = ORIGIN(ram) + LENGTH(ram) - 1024; + +SECTIONS +{ + .dfu_shared dfu_shared_location :{ + dfu_flag = .; + } +} + diff --git a/interface/j-link.cfg b/interface/j-link.cfg new file mode 100644 index 0000000..3e95768 --- /dev/null +++ b/interface/j-link.cfg @@ -0,0 +1,5 @@ +# +telnet_port 4444 +gdb_port 3333 + +source [find interface/jlink.cfg] diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index f7f445a..0000000 --- a/src/Makefile +++ /dev/null @@ -1,43 +0,0 @@ -## -## 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=kvm - -V=1 -default: ${PROG}.elf - -CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c usart.c kvm.c ring.c - - -BINARY = ${PROG} -OBJS = ${CSRCS:%.c=%.o} - - -program: ${PROG}.elf - echo halt | nc -t localhost 4444 - echo flash write_image erase ${PWD}/$< | 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 - -include ../Makefile.include diff --git a/src/dfu.c b/src/dfu.c deleted file mode 100644 index e9de46f..0000000 --- a/src/dfu.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "project.h" - -#ifdef INCLUDE_DFU_INTERFACE - -const struct usb_dfu_descriptor dfu_function = { - .bLength = sizeof (struct usb_dfu_descriptor), - .bDescriptorType = DFU_FUNCTIONAL, - .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, - .wDetachTimeout = 255, - .wTransferSize = 1024, - .bcdDFUVersion = 0x011A, -}; - -const struct usb_interface_descriptor dfu_iface = { - .bLength = USB_DT_INTERFACE_SIZE, - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 3, - .bAlternateSetting = 0, - .bNumEndpoints = 0, - .bInterfaceClass = 0xFE, - .bInterfaceSubClass = 1, - .bInterfaceProtocol = 1, - .iInterface = 0, - - .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; - - gpio_set_mode (GPIOA, GPIO_MODE_INPUT, 0, GPIO15); - gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); - gpio_set (GPIOA, GPIO10); - 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 != 0x21) || (req->bRequest != DFU_DETACH)) - return 0; /* Only accept class request. */ - - *complete = dfu_detach_complete; - - return 1; -} -#endif diff --git a/src/keyboard.c b/src/keyboard.c deleted file mode 100644 index e9046a3..0000000 --- a/src/keyboard.c +++ /dev/null @@ -1,140 +0,0 @@ -#include "project.h" - -#if 0 -static const uint8_t keyboard_report_descriptor[] = { - 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ - 0x09, 0x01, /* USAGE (Keyboard */ - 0xa1, 0x01, /* COLLECTION (Application) */ - 0x05, 0x07, /* USAGE_PAGE (Key Codes) */ - 0x19, 0xe0, /* USAGE_MINIMUM (Control Left) */ - 0x29, 0xe7, /* USAGE_MAXIMUM (GUI Right) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ - 0x75, 0x01, /* REPORT_SIZE (1) */ - 0x95, 0x08, /* REPORT_COUNT (8) */ - 0x81, 0x02, /* INPUT (Data,Var,Abs) */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0x75, 0x08, /* REPORT_SIZE (8) */ - 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */ - 0x95, 0x05, /* REPORT_COUNT (5) */ - 0x75, 0x01, /* REPORT_SIZE (1) */ -#if 0 - 0x05, 0x08, /* USAGE_PAGE (LEDs) */ - 0x19, 0x01, /* USAGE_MINIMUM (NumLock) */ - 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */ - 0x ? ?, 0x02, /* OUTPUT (Data,Var,Abs) */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0x75, 0x03, /* REPORT_SIZE (3) */ - 0x ? ?, 0x01, /* OUTPUT (Cnst,Ary,Abs) */ - 0x95, 0x06, /* REPORT_COUNT (6) */ - 0x75, 0x03, /* REPORT_SIZE (8) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0xff, 0x00, /*LOGICAL_MAXIMUM (255) */ -#endif - 0x05, 0x07, /* USAGE_PAGE (Key Codes) */ - 0x19, 0x00, /* USAGE_MINIMUM (?) */ - 0x29, 0x98, /* USAGE_MAXIMUM (LANG 9) */ - 0xc0 /* END_COLLECTION */ -}; -#endif - -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) */ - 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) */ - 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 = 8, - .bInterval = 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 = 0, - - .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[8] = { 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, 8); -} diff --git a/src/kvm.c b/src/kvm.c deleted file mode 100644 index f5e65c8..0000000 --- a/src/kvm.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "project.h" - - -static enum -{ - STATE_SYNC = 0, - STATE_ADDR = 1, - STATE_ENDPOINT = 2, - STATE_LEN = 3, - STATE_DATA = 4, -} state = STATE_SYNC; - - -static uint8_t buf[16]; -static int addr; -static int endpoint; -static unsigned int len; -static unsigned int ptr; - -void -kvm_dispatch (void) -{ -#if 0 - printf - ("Addr %d, Write to ep %x, %d bytes %02x %02x %02x %02x ...\r\n", - addr, endpoint, len, buf[0], buf[1], buf[2], buf[3]); -#endif - - if(len) - usbd_ep_write_packet (usbd_dev, endpoint, buf, len); -} - -void -kvm_recv (uint8_t d) -{ -// printf ("S%d V0x%02x\r\n", state, d); - - if (state == STATE_ADDR) - { - addr = d; - usart_queue (d - 1); - state = STATE_ENDPOINT; - return; - } - else - { - usart_queue (d); - } - - - switch (state) - { - case STATE_SYNC: - if (d != 0x5a) - break; - state = STATE_ADDR; - break; - case STATE_ENDPOINT: - endpoint = d; - if ((endpoint < 0x81) || (endpoint > 0x83)) - { - state = STATE_SYNC; - } - else - { - state = STATE_LEN; - } - break; - case STATE_LEN: - len = d; - ptr = 0; - if (len > sizeof (buf)) - { - state = STATE_SYNC; - } - else - { - state = STATE_DATA; - } - break; - case STATE_DATA: - buf[ptr++] = d; - if (ptr >= len) - { - if (!addr) - kvm_dispatch (); - state = STATE_SYNC; - } - break; - } -} diff --git a/src/kvm.ld b/src/kvm.ld deleted file mode 100644 index e15beca..0000000 --- a/src/kvm.ld +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 = 0x08000000, LENGTH = 128K - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K -} - -/* Include the common ld script. */ -INCLUDE libopencm3_stm32f1.ld - diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 3058108..0000000 --- a/src/main.c +++ /dev/null @@ -1,55 +0,0 @@ -#include "project.h" - -void -sys_tick_handler (void) -{ -#if 0 - printf ("fish\r\n"); - keyboard_test (); - mouse_test (); - tablet_test (); -#endif -} - -int -main (void) -{ - int i; - - - rcc_clock_setup_in_hsi_out_48mhz (); - rcc_periph_clock_enable (RCC_GPIOC); - rcc_periph_clock_enable (RCC_GPIOA); - rcc_periph_clock_enable (RCC_AFIO); - rcc_periph_clock_enable (RCC_USART1); - - - usart_init (); - - usb_init (); - - systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); - /* 48MHz / 8 = > 6Mhz */ - systick_set_reload (3000000); - /* 6MHz / 3000000 => 2Hz */ - - systick_interrupt_enable (); - systick_counter_enable (); - - gpio_set (GPIOC, GPIO11); - gpio_set_mode (GPIOC, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); - - - - for (i = 0; i < 0x80000; i++) - __asm__ ("nop"); - - gpio_clear (GPIOC, GPIO11); - - printf ("Hello world\r\n"); - - usb_run (); - - return 0; -} diff --git a/src/mouse.c b/src/mouse.c deleted file mode 100644 index 6e0a207..0000000 --- a/src/mouse.c +++ /dev/null @@ -1,115 +0,0 @@ -#include "project.h" - -static const uint8_t mouse_report_descriptor[] = { - 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ - 0x09, 0x02, /* USAGE (Mouse) */ - 0xa1, 0x01, /* COLLECTION (Application) */ - 0x09, 0x01, /* USAGE (Pointer) */ - 0xa1, 0x00, /* COLLECTION (Physical) */ - 0x05, 0x09, /* USAGE_PAGE (Button) */ - 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */ - 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ - 0x95, 0x03, /* REPORT_COUNT (3) */ - 0x75, 0x01, /* REPORT_SIZE (1) */ - 0x81, 0x02, /* INPUT (Data,Var,Abs) */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0x75, 0x05, /* REPORT_SIZE (5) */ - 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */ - 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ - 0x09, 0x30, /* USAGE (X) */ - 0x09, 0x31, /* USAGE (Y) */ - 0x09, 0x38, /* USAGE (Wheel) */ - 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */ - 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */ - 0x75, 0x08, /* REPORT_SIZE (8) */ - 0x95, 0x03, /* REPORT_COUNT (3) */ - 0x81, 0x06, /* INPUT (Data,Var,Rel) */ - 0xc0, /* END_COLLECTION */ - 0x09, 0x3c, /* USAGE (Motion Wakeup) */ - 0x05, 0xff, /* USAGE_PAGE (Vendor Defined Page 1) */ - 0x09, 0x01, /* USAGE (Vendor Usage 1) */ - 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ - 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ - 0x75, 0x01, /* REPORT_SIZE (1) */ - 0x95, 0x02, /* REPORT_COUNT (2) */ - 0xb1, 0x22, /* FEATURE (Data,Var,Abs,NPrf) */ - 0x75, 0x06, /* REPORT_SIZE (6) */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0xb1, 0x01, /* FEATURE (Cnst,Ary,Abs) */ - 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)) mouse_function = -{ - .hid_descriptor = - { - .bLength = sizeof (mouse_function),.bDescriptorType = USB_DT_HID,.bcdHID = - 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} - ,.hid_report = - { - .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = - sizeof (mouse_report_descriptor),} -,}; - -const struct usb_endpoint_descriptor mouse_endpoint = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x82, - .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 4, - .bInterval = 0x20, -}; - -const struct usb_interface_descriptor mouse_iface = { - .bLength = USB_DT_INTERFACE_SIZE, - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 1, - .bAlternateSetting = 0, - .bNumEndpoints = 1, - .bInterfaceClass = USB_CLASS_HID, - .bInterfaceSubClass = 1, /* boot */ - .bInterfaceProtocol = 2, /* mouse */ - .iInterface = 0, - - .endpoint = &mouse_endpoint, - - .extra = &mouse_function, - .extralen = sizeof (mouse_function), -}; - - -void -mouse_get_descriptor (uint8_t ** buf, uint16_t * len) -{ - - /* Handle the HID report descriptor. */ - *buf = (uint8_t *) mouse_report_descriptor; - *len = sizeof (mouse_report_descriptor); -} - -void -mouse_test (void) -{ - static int c = 1; - uint8_t buf[4] = { 0, 0, 0, 0 }; - - buf[0] = c & 7; - - buf[1] = c & 8 ? -1 : 1; - buf[2] = c & 16 ? -1 : 1; - buf[3] = c & 32 ? -1 : 1; - - c++; - - usbd_ep_write_packet (usbd_dev, 0x82, buf, 4); -} diff --git a/src/project.h b/src/project.h deleted file mode 100644 index 0323569..0000000 --- a/src/project.h +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#define INCLUDE_DFU_INTERFACE - - -#ifdef INCLUDE_DFU_INTERFACE -#include -#include -#endif - -#include -#include - -#include "ring.h" - -#include "prototypes.h" diff --git a/src/prototypes.h b/src/prototypes.h deleted file mode 100644 index 0e971df..0000000 --- a/src/prototypes.h +++ /dev/null @@ -1,43 +0,0 @@ -/* 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)); -/* mouse.c */ -extern const struct usb_endpoint_descriptor mouse_endpoint; -extern const struct usb_interface_descriptor mouse_iface; -extern void mouse_get_descriptor(uint8_t **buf, uint16_t *len); -extern void mouse_test(void); -/* 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); -/* main.c */ -extern void sys_tick_handler(void); -extern int main(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); -/* tablet.c */ -extern const struct usb_endpoint_descriptor tablet_endpoint; -extern const struct usb_interface_descriptor tablet_iface; -extern void tablet_get_descriptor(uint8_t **buf, uint16_t *len); -extern void tablet_test(void); -/* usart.c */ -extern void usart1_isr(void); -extern int _write(int file, char *ptr, int len); -extern void usart_queue(uint8_t d); -extern void usart_init(void); -/* kvm.c */ -extern void kvm_dispatch(void); -extern void kvm_recv(uint8_t d); -/* ring.c */ -extern void ring_init(ring_t *r, uint8_t *buf, size_t len); -extern int ring_write_byte(ring_t *r, uint8_t c); -extern int ring_read_byte(ring_t *r, uint8_t *c); -extern int ring_write(ring_t *r, uint8_t *buf, size_t len); diff --git a/src/ring.c b/src/ring.c deleted file mode 100644 index 4f38e44..0000000 --- a/src/ring.c +++ /dev/null @@ -1,61 +0,0 @@ -#include "project.h" - - -static inline size_t -ring_next (ring_t * r, size_t p) -{ - p++; - if (p >= r->size) - p -= r->size; - return p; -} - -void -ring_init (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 (ring_t * r, uint8_t c) -{ - size_t n = ring_next (r, r->write); - - if (n == r->read) - return -1; - - r->data[r->write] = c; - r->write = n; - - return 0; -} - - -int -ring_read_byte (ring_t * r, uint8_t * c) -{ - size_t n = ring_next (r, r->read); - - if (r->read == r->write) - return -1; - - *c = r->data[r->read]; - r->read = n; - - return 0; -} - -int -ring_write (ring_t * r, uint8_t * buf, size_t len) -{ - while (len--) - { - if (ring_write_byte (r, *(buf++))) - return -1; - } - - return 0; -} diff --git a/src/ring.h b/src/ring.h deleted file mode 100644 index 4e3a22c..0000000 --- a/src/ring.h +++ /dev/null @@ -1,9 +0,0 @@ -typedef struct ring -{ - uint8_t *data; - size_t size; - size_t write; - size_t read; -} ring_t; - - diff --git a/src/tablet.c b/src/tablet.c deleted file mode 100644 index 1073b37..0000000 --- a/src/tablet.c +++ /dev/null @@ -1,122 +0,0 @@ -#include "project.h" - -static const uint8_t tablet_report_descriptor[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767) - 0x75, 0x10, // REPORT_SIZE (16) - 0x95, 0x02, // REPORT_COUNT (2) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x38, // USAGE (Wheel) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7f, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x06, // INPUT (Data,Var,Rel) - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x03, // USAGE_MAXIMUM (Button 3) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x95, 0x03, // REPORT_COUNT (3) - 0x75, 0x01, // REPORT_SIZE (1) - 0x81, 0x02, // INPUT (Data,Var,Abs) - 0x95, 0x01, // REPORT_COUNT (1) - 0x75, 0x05, // REPORT_SIZE (5) - 0x81, 0x01, // INPUT (Cnst,Ary,Abs) - 0xc0, // END_COLLECTION - 0x09, 0x3c, // USAGE (Button 60) - 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1) - 0x09, 0x01, // USAGE (Vendor Usage 1) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x02, // REPORT_COUNT (2) - 0xb1, 0x22, // FEATURE (Data,Var,Abs,NPrf) - 0x75, 0x06, // REPORT_SIZE (6) - 0x95, 0x01, // REPORT_COUNT (1) - 0xb1, 0x01, // FEATURE (Cnst,Ary,Abs) - 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)) tablet_function = -{ - .hid_descriptor = - { - .bLength = sizeof (tablet_function),.bDescriptorType = - USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} - ,.hid_report = - { - .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = - sizeof (tablet_report_descriptor),} -,}; - -const struct usb_endpoint_descriptor tablet_endpoint = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x83, - .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 6, - .bInterval = 0x20, -}; - -const struct usb_interface_descriptor tablet_iface = { - .bLength = USB_DT_INTERFACE_SIZE, - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 2, - .bAlternateSetting = 0, - .bNumEndpoints = 1, - .bInterfaceClass = USB_CLASS_HID, - .bInterfaceSubClass = 1, /* boot */ - .bInterfaceProtocol = 2, /* tablet */ - .iInterface = 0, - - .endpoint = &tablet_endpoint, - - .extra = &tablet_function, - .extralen = sizeof (tablet_function), -}; - - -void -tablet_get_descriptor (uint8_t ** buf, uint16_t * len) -{ - - /* Handle the HID report descriptor. */ - *buf = (uint8_t *) tablet_report_descriptor; - *len = sizeof (tablet_report_descriptor); -} - -void -tablet_test (void) -{ - static int c = 0; - uint8_t buf[6] = { 0, 0, 0, 0, 0 }; - - buf[0] = c & 0xff; - buf[1] = (c & 0x7fff) >> 8; - buf[2] = (32767 - (c & 0x7fff)) & 0xff; - buf[3] = (32767 - (c & 0x7fff)) >> 8; - buf[4] = (c & 8) ? -1 : 1; - buf[5] = c; - - c++; - - usbd_ep_write_packet (usbd_dev, 0x83, buf, 6); -} diff --git a/src/usart.c b/src/usart.c deleted file mode 100644 index b0ea2a4..0000000 --- a/src/usart.c +++ /dev/null @@ -1,104 +0,0 @@ -#include "project.h" - -#define BUFFER_SIZE 256 - -static ring_t output_ring; -static uint8_t output_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)) - { - - /* Retrieve the data from the peripheral. */ - data = usart_recv (USART1); - - kvm_recv (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 (&output_ring, &data)) - { - /*No more data, Disable the TXE interrupt, it's no longer needed. */ - USART_CR1 (USART1) &= ~USART_CR1_TXEIE; - } - else - { - usart_send (USART1, data); - } - } - -} - -int -_write (int file, char *ptr, int len) -{ - int ret; - - if (file == 1) - { - ret = ring_write (&output_ring, (uint8_t *) ptr, len); - - if (ret < 0) - ret = -ret; - - USART_CR1 (USART1) |= USART_CR1_TXEIE; - return ret; - } - - errno = EIO; - return -1; -} - -void -usart_queue (uint8_t d) -{ - ring_write_byte(&output_ring,d); - USART_CR1 (USART1) |= USART_CR1_TXEIE; - -#if 0 - printf ("0x%02x ", d); - fflush (stdout); -#endif -} - - -void -usart_init (void) -{ - ring_init (&output_ring, output_ring_buf, sizeof (output_ring_buf)); - - /* Enable the USART1 interrupt. */ - nvic_enable_irq (NVIC_USART1_IRQ); - - /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ - gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, - GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); - - /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */ - gpio_set_mode (GPIOA, GPIO_MODE_INPUT, - GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); - - /* Setup UART parameters. */ - 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); - - /* Enable USART1 Receive interrupt. */ - USART_CR1 (USART1) |= USART_CR1_RXNEIE; - - /* Finally enable the USART. */ - usart_enable (USART1); -} diff --git a/src/usb.c b/src/usb.c deleted file mode 100644 index f796162..0000000 --- a/src/usb.c +++ /dev/null @@ -1,146 +0,0 @@ -#include "project.h" - -/* Define this to include the DFU APP interface. */ -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 = 0x1d6b, - .idProduct = 0x0ee3, - .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 = &mouse_iface, - }, - { - .num_altsetting = 1, - .altsetting = &tablet_iface, - }, -#ifdef INCLUDE_DFU_INTERFACE - { - .num_altsetting = 1, - .altsetting = &dfu_iface, -#endif - } -}; - -const struct usb_config_descriptor config = { - .bLength = USB_DT_CONFIGURATION_SIZE, - .bDescriptorType = USB_DT_CONFIGURATION, - .wTotalLength = 0, -#ifdef INCLUDE_DFU_INTERFACE - .bNumInterfaces = 4, -#else - .bNumInterfaces = 3, -#endif - .bConfigurationValue = 1, - .iConfiguration = 0, - .bmAttributes = 0xC0, - .bMaxPower = 0x32, - - .interface = ifaces, -}; - -static const char *usb_strings[] = { - "Cabbages are good for you", - "fish", - "soup", -}; - - - -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: - mouse_get_descriptor (buf, len); - return 1; - case 2: - tablet_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_ep_setup (usbd_dev, 0x83, 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); - -#ifdef INCLUDE_DFU_INTERFACE - 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); -#endif -} - -/* 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, 3, - 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