From 12287ff0a55f929bf840dcb4780d3f77b862c434 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 2 Nov 2014 18:17:44 +0000 Subject: fish --- 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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 1084 insertions(+) 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 (limited to 'app') 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); +} -- cgit v1.2.3