summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorroot <root@lab2.panaceas.james.local>2014-11-02 15:16:42 +0000
committerroot <root@lab2.panaceas.james.local>2014-11-02 15:16:42 +0000
commit479e719a64d75374f00438498cf91ba2601a63f1 (patch)
tree6f2164598cbe9c78396060dc45f5307172d36a0c
parent87e61d21e5ce588c9701d92def188fa97674c3cd (diff)
downloadstm32_usb_kvm-479e719a64d75374f00438498cf91ba2601a63f1.tar.gz
stm32_usb_kvm-479e719a64d75374f00438498cf91ba2601a63f1.tar.bz2
stm32_usb_kvm-479e719a64d75374f00438498cf91ba2601a63f1.zip
fish
-rw-r--r--Makefile.rules4
-rw-r--r--src/Makefile2
-rw-r--r--src/dfu.c78
-rw-r--r--src/keyboard.c9
-rw-r--r--src/kvm.c91
-rw-r--r--src/main.c64
-rw-r--r--src/mouse.c9
-rw-r--r--src/project.h6
-rw-r--r--src/prototypes.h15
-rw-r--r--src/ring.c61
-rw-r--r--src/ring.h9
-rw-r--r--src/tablet.c109
-rw-r--r--src/uart.c7
-rw-r--r--src/usart.c104
-rw-r--r--src/usb.c46
15 files changed, 450 insertions, 164 deletions
diff --git a/Makefile.rules b/Makefile.rules
index 685caad..b67e343 100644
--- a/Makefile.rules
+++ b/Makefile.rules
@@ -80,7 +80,7 @@ SCRIPT_DIR = $(OPENCM3_DIR)/scripts
CFLAGS += -Os -g
CFLAGS += -Wextra -Wimplicit-function-declaration
-CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes
+CFLAGS += -Wmissing-prototypes -Wstrict-prototypes
CFLAGS += -fno-common -ffunction-sections -fdata-sections
###############################################################################
@@ -179,7 +179,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 *~
stylecheck: $(STYLECHECKFILES:=.stylecheck)
styleclean: $(STYLECHECKFILES:=.styleclean)
diff --git a/src/Makefile b/src/Makefile
index 1a9f2b2..f7f445a 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -23,7 +23,7 @@ PROG=kvm
V=1
default: ${PROG}.elf
-CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c uart.c
+CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c usart.c kvm.c ring.c
BINARY = ${PROG}
diff --git a/src/dfu.c b/src/dfu.c
index 56420ad..e9de46f 100644
--- a/src/dfu.c
+++ b/src/dfu.c
@@ -3,56 +3,58 @@
#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,
+ .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),
+ .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)
+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();
+ (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))
+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;
+ (void) buf;
+ (void) len;
+ (void) usbd_dev;
- if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH))
- return 0; /* Only accept class request. */
+ if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH))
+ return 0; /* Only accept class request. */
- *complete = dfu_detach_complete;
+ *complete = dfu_detach_complete;
- return 1;
+ return 1;
}
#endif
-
-
diff --git a/src/keyboard.c b/src/keyboard.c
index e4ed7e2..e9046a3 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -114,7 +114,8 @@ const struct usb_interface_descriptor keyboard_iface = {
};
-void keyboard_get_descriptor(uint8_t **buf,uint16_t *len)
+void
+keyboard_get_descriptor (uint8_t ** buf, uint16_t * len)
{
/* Handle the HID report descriptor. */
@@ -126,12 +127,12 @@ void
keyboard_test (void)
{
static int c = 0;
- uint8_t buf[8] = { 0, 0, 0, 0 ,0,0,0,0};
+ uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
- buf[0]=(c>> 1) & 7;
+ buf[0] = (c >> 1) & 7;
- buf[2]= (c&1) ? 12:0;
+ buf[2] = (c & 1) ? 12 : 0;
c++;
diff --git a/src/kvm.c b/src/kvm.c
new file mode 100644
index 0000000..f5e65c8
--- /dev/null
+++ b/src/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/src/main.c b/src/main.c
index 6b13a82..3058108 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,49 +1,55 @@
#include "project.h"
-void sys_tick_handler(void)
+void
+sys_tick_handler (void)
{
- keyboard_test();
- mouse_test();
- tablet_test();
+#if 0
+ printf ("fish\r\n");
+ keyboard_test ();
+ mouse_test ();
+ tablet_test ();
+#endif
}
-int main(void)
+int
+main (void)
{
- int i;
+ 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);
+ 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);
- uart_init();
+ usart_init ();
- usb_init();
+ usb_init ();
- systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
- /* 48MHz / 8 = > 6Mhz */
- systick_set_reload(3000000);
- /* 6MHz / 3000000 => 2Hz */
+ systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8);
+ /* 48MHz / 8 = > 6Mhz */
+ systick_set_reload (3000000);
+ /* 6MHz / 3000000 => 2Hz */
- systick_interrupt_enable();
- systick_counter_enable();
+ systick_interrupt_enable ();
+ systick_counter_enable ();
- gpio_set(GPIOC, GPIO11);
- gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL, GPIO11);
+ 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);
+ for (i = 0; i < 0x80000; i++)
+ __asm__ ("nop");
- usb_run();
+ gpio_clear (GPIOC, GPIO11);
- return 0;
-}
+ printf ("Hello world\r\n");
+
+ usb_run ();
+ return 0;
+}
diff --git a/src/mouse.c b/src/mouse.c
index acb4166..6e0a207 100644
--- a/src/mouse.c
+++ b/src/mouse.c
@@ -88,7 +88,8 @@ const struct usb_interface_descriptor mouse_iface = {
};
-void mouse_get_descriptor(uint8_t **buf,uint16_t *len)
+void
+mouse_get_descriptor (uint8_t ** buf, uint16_t * len)
{
/* Handle the HID report descriptor. */
@@ -104,9 +105,9 @@ mouse_test (void)
buf[0] = c & 7;
- buf[1] = c &8 ? -1:1;
- buf[2] = c &16 ? -1:1;
- buf[3] = c &32 ? -1:1;
+ buf[1] = c & 8 ? -1 : 1;
+ buf[2] = c & 16 ? -1 : 1;
+ buf[3] = c & 32 ? -1 : 1;
c++;
diff --git a/src/project.h b/src/project.h
index de86ece..0323569 100644
--- a/src/project.h
+++ b/src/project.h
@@ -1,7 +1,9 @@
#include <stdlib.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/systick.h>
+#include <libopencm3/cm3/nvic.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/hid.h>
@@ -13,5 +15,9 @@
#include <libopencm3/usb/dfu.h>
#endif
+#include <stdio.h>
+#include <errno.h>
+
+#include "ring.h"
#include "prototypes.h"
diff --git a/src/prototypes.h b/src/prototypes.h
index 9b4d2c9..0e971df 100644
--- a/src/prototypes.h
+++ b/src/prototypes.h
@@ -28,5 +28,16 @@ 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);
-/* uart.c */
-extern void uart_init(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
new file mode 100644
index 0000000..4f38e44
--- /dev/null
+++ b/src/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/src/ring.h b/src/ring.h
new file mode 100644
index 0000000..4e3a22c
--- /dev/null
+++ b/src/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/src/tablet.c b/src/tablet.c
index 87d2f2a..1073b37 100644
--- a/src/tablet.c
+++ b/src/tablet.c
@@ -1,50 +1,50 @@
#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
+ 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
@@ -59,8 +59,8 @@ static const struct
{
.hid_descriptor =
{
- .bLength = sizeof (tablet_function),.bDescriptorType = USB_DT_HID,.bcdHID =
- 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,}
+ .bLength = sizeof (tablet_function),.bDescriptorType =
+ USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,}
,.hid_report =
{
.bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength =
@@ -94,7 +94,8 @@ const struct usb_interface_descriptor tablet_iface = {
};
-void tablet_get_descriptor(uint8_t **buf,uint16_t *len)
+void
+tablet_get_descriptor (uint8_t ** buf, uint16_t * len)
{
/* Handle the HID report descriptor. */
@@ -106,14 +107,14 @@ void
tablet_test (void)
{
static int c = 0;
- uint8_t buf[6] = { 0, 0, 0, 0,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;
+ 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++;
diff --git a/src/uart.c b/src/uart.c
deleted file mode 100644
index 70a6334..0000000
--- a/src/uart.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "project.h"
-
-
-
-void uart_init(void)
-{
-}
diff --git a/src/usart.c b/src/usart.c
new file mode 100644
index 0000000..b0ea2a4
--- /dev/null
+++ b/src/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/src/usb.c b/src/usb.c
index b004c78..f796162 100644
--- a/src/usb.c
+++ b/src/usb.c
@@ -18,25 +18,25 @@ const struct usb_device_descriptor dev = {
.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,
- },
+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,
+ {
+ .num_altsetting = 1,
+ .altsetting = &dfu_iface,
#endif
- }
+ }
};
const struct usb_config_descriptor config = {
@@ -84,14 +84,14 @@ usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req,
switch (req->wIndex)
{
case 0:
- keyboard_get_descriptor(buf,len);
- return 1;
+ keyboard_get_descriptor (buf, len);
+ return 1;
case 1:
- mouse_get_descriptor(buf,len);
- return 1;
+ mouse_get_descriptor (buf, len);
+ return 1;
case 2:
- tablet_get_descriptor(buf,len);
- return 1;
+ tablet_get_descriptor (buf, len);
+ return 1;
}
*len = 0;