Skip to content

Commit

Permalink
Keyboard codes transmission + interrupts (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
lekernel committed Oct 1, 2010
1 parent aee47e3 commit 2a61106
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 25 deletions.
1 change: 1 addition & 0 deletions cores/softusb/rtl/softusb_tx.v
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ always @(posedge usb_clk) begin
if(sr_rst) begin
sr_done <= 1'b1;
onecount <= 3'd0;
sr_out <= 1'b1;
end else if(gce) begin
if(sr_load) begin
sr_done <= 1'b0;
Expand Down
2 changes: 2 additions & 0 deletions softusb-input/comloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
#define COMLOC_DEBUG(offset) COMLOC(0x1001+offset)
#define COMLOC_MEVT_PRODUCE COMLOC(0x1101)
#define COMLOC_MEVT(offset) COMLOC(0x1102+offset)
#define COMLOC_KEVT_PRODUCE COMLOC(0x1142)
#define COMLOC_KEVT(offset) COMLOC(0x1143+offset)

#endif /* __COMLOC_H */
9 changes: 7 additions & 2 deletions softusb-input/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
*/

#include "progmem.h"
#include "host.h"
#include "comloc.h"

void print_char(char c)
{
COMLOC_DEBUG(COMLOC_DEBUG_PRODUCE) = c;
COMLOC_DEBUG_PRODUCE++;
unsigned char p;
p = COMLOC_DEBUG_PRODUCE;
COMLOC_DEBUG(p) = c;
p++;
COMLOC_DEBUG_PRODUCE = p;
wio8(HOST_IRQ, 1);
}

void print_string(const char *s) /* in program memory */
Expand Down
25 changes: 25 additions & 0 deletions softusb-input/host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Milkymist VJ SoC (USB firmware
* Copyright (C 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __HOST_H
#define __HOST_H

#include "io.h"

#define HOST_IRQ 0x15

#endif /* __HOST_H */
41 changes: 33 additions & 8 deletions softusb-input/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "debug.h"
#include "sie.h"
#include "timer.h"
#include "host.h"
#include "crc.h"

enum {
Expand Down Expand Up @@ -298,21 +299,45 @@ static void poll(int keyboard)
usb_tx(usb_buffer, 3);
/* DATAx */
len = usb_rx(usb_buffer, 11);
if(len < 7) return;
/* ACK */
usb_buffer[0] = 0xd2;
usb_tx(usb_buffer, 1);
/* send to host */
if(keyboard) {
dump_hex(&usb_buffer[1], len-3);
m = COMLOC_KEVT_PRODUCE;
if((len >= 6) && (usb_buffer[3] != 0x00)) {
COMLOC_KEVT(2*m+0) = usb_buffer[1];
COMLOC_KEVT(2*m+1) = usb_buffer[3];
m = (m + 1) & 0x07;
}
if((len >= 7) && (usb_buffer[4] != 0x00)) {
COMLOC_KEVT(2*m+0) = usb_buffer[1];
COMLOC_KEVT(2*m+1) = usb_buffer[4];
m = (m + 1) & 0x07;
}
if((len >= 8) && (usb_buffer[5] != 0x00)) {
COMLOC_KEVT(2*m+0) = usb_buffer[1];
COMLOC_KEVT(2*m+1) = usb_buffer[5];
m = (m + 1) & 0x07;
}
if((len >= 9) && (usb_buffer[6] != 0x00)) {
COMLOC_KEVT(2*m+0) = usb_buffer[1];
COMLOC_KEVT(2*m+1) = usb_buffer[6];
m = (m + 1) & 0x07;
}
COMLOC_KEVT_PRODUCE = m;
} else {
m = COMLOC_MEVT_PRODUCE;
COMLOC_MEVT(4*m+0) = usb_buffer[1];
COMLOC_MEVT(4*m+1) = usb_buffer[2];
COMLOC_MEVT(4*m+2) = usb_buffer[3];
COMLOC_MEVT(4*m+3) = usb_buffer[4];
COMLOC_MEVT_PRODUCE = (m + 1) & 0x0f;
if(len >= 7) {
m = COMLOC_MEVT_PRODUCE;
COMLOC_MEVT(4*m+0) = usb_buffer[1];
COMLOC_MEVT(4*m+1) = usb_buffer[2];
COMLOC_MEVT(4*m+2) = usb_buffer[3];
COMLOC_MEVT(4*m+3) = usb_buffer[4];
COMLOC_MEVT_PRODUCE = (m + 1) & 0x0f;
}
}
/* trigger host IRQ */
wio8(HOST_IRQ, 1);
}

static const char connect_fs[] PROGMEM = "Full speed device on port ";
Expand Down
2 changes: 1 addition & 1 deletion software/demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ isr.o: ../../software/include/hal/time.h ../../software/include/hal/snd.h
isr.o: ../../software/include/hw/ac97.h ../../software/include/hw/common.h
isr.o: ../../software/include/hal/pfpu.h ../../software/include/hw/pfpu.h
isr.o: ../../software/include/hal/tmu.h ../../software/include/hw/tmu.h
isr.o: cpustats.h
isr.o: ../../software/include/hal/usb.h cpustats.h
line.o: ../../software/include/base/stdlib.h
line.o: ../../software/include/base/math.h color.h line.h
main.o: ../../software/include/base/stdio.h
Expand Down
4 changes: 4 additions & 0 deletions software/demo/isr.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <hal/snd.h>
#include <hal/pfpu.h>
#include <hal/tmu.h>
#include <hal/usb.h>

#include "cpustats.h"

Expand Down Expand Up @@ -56,6 +57,9 @@ void isr()

if(irqs & IRQ_TMU)
tmu_isr();

if(irqs & IRQ_USB)
usb_isr();

cpustats_leave();
}
1 change: 0 additions & 1 deletion software/demo/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ int main()
apipe_service();
rpipe_service();
osd_service();
usb_service();
}

return 0;
Expand Down
14 changes: 11 additions & 3 deletions software/demo/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,6 @@ static int mouse_x, mouse_y;

static void mouse_cb(unsigned char buttons, char dx, char dy, unsigned char wheel)
{
//printf("%d %d\n", dx, dy);
mouse_x += dx;
mouse_y += dy;
if(mouse_x < 0)
Expand All @@ -725,14 +724,23 @@ static void mouse_cb(unsigned char buttons, char dx, char dy, unsigned char whee
vga_frontbuffer[vga_hres*mouse_y+mouse_x] = 0xffff;
}

static void keyboard_cb(unsigned char modifiers, unsigned char key)
{
if(modifiers != 0x00)
printf("%x (mod:%x)\n", key, modifiers);
else
printf("%x\n", key);
}

static void mouse()
{
mouse_x = 0;
mouse_y = 0;
usb_set_mouse_cb(mouse_cb);
while(!readchar_nonblock())
usb_service();
usb_set_keyboard_cb(keyboard_cb);
while(!readchar_nonblock());
usb_set_mouse_cb(NULL);
usb_set_keyboard_cb(NULL);
}

#ifdef WITH_MEMTEST
Expand Down
5 changes: 4 additions & 1 deletion software/include/hal/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
#define __HAL_USB_H

typedef void (*mouse_event_cb)(unsigned char buttons, char dx, char dy, unsigned char wheel);
typedef void (*keyboard_event_cb)(unsigned char modifiers, unsigned char key);

void usb_init();
void usb_service();
void usb_set_mouse_cb(mouse_event_cb cb);
void usb_set_keyboard_cb(keyboard_event_cb cb);

void usb_isr();

#endif /* __HAL_USB_H */
2 changes: 1 addition & 1 deletion software/libbase/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ blockdev.o: ../../software/include/base/string.h
blockdev.o: ../../software/include/base/stdlib.h
blockdev.o: ../../software/include/base/blockdev.h
board.o: ../../software/include/hw/sysctl.h
board.o: ../../software/include/hw/common.h
board.o: ../../software/include/hw/common.h ../../software/include/hw/gpio.h
board.o: ../../software/include/base/stdlib.h
board.o: ../../software/include/base/board.h
console.o: ../../software/include/base/uart.h
Expand Down
3 changes: 2 additions & 1 deletion software/libhal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ tmu.o: ../../software/include/hal/tmu.h
usb.o: ../../software/include/hw/softusb.h ../../software/include/hw/common.h
usb.o: ../../software/include/hw/sysctl.h
usb.o: ../../software/include/hw/capabilities.h
usb.o: ../../software/include/base/stdio.h
usb.o: ../../software/include/hw/interrupts.h
usb.o: ../../software/include/base/irq.h ../../software/include/base/stdio.h
usb.o: ../../software/include/base/stdlib.h ../../software/include/hal/usb.h
usb.o: input-comloc.h softusb-input.h
vga.o: ../../software/include/base/stdio.h
Expand Down
2 changes: 2 additions & 0 deletions software/libhal/input-comloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
#define COMLOC_DEBUG(offset) COMLOCV(SOFTUSB_DMEM_BASE+0x1001+offset)
#define COMLOC_MEVT_PRODUCE COMLOCV(SOFTUSB_DMEM_BASE+0x1101)
#define COMLOC_MEVT(offset) COMLOCV(SOFTUSB_DMEM_BASE+0x1102+offset)
#define COMLOC_KEVT_PRODUCE COMLOCV(SOFTUSB_DMEM_BASE+0x1142)
#define COMLOC_KEVT(offset) COMLOCV(SOFTUSB_DMEM_BASE+0x1143+offset)

#endif /* __INPUT_COMLOC_H */
39 changes: 32 additions & 7 deletions software/libhal/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <hw/softusb.h>
#include <hw/sysctl.h>
#include <hw/capabilities.h>
#include <hw/interrupts.h>
#include <irq.h>
#include <stdio.h>

#include <hal/usb.h>
Expand All @@ -34,12 +36,16 @@ static int debug_consume;
static mouse_event_cb mouse_cb;
static int mouse_consume;

static keyboard_event_cb keyboard_cb;
static int keyboard_consume;

void usb_init()
{
int nwords;
int i;
unsigned int *usb_dmem = (unsigned int *)SOFTUSB_DMEM_BASE;
unsigned int *usb_pmem = (unsigned int *)SOFTUSB_PMEM_BASE;
unsigned int mask;

if(!(CSR_CAPABILITIES & CAP_USB)) {
printf("USB: not supported by SoC, giving up.\n");
Expand All @@ -63,6 +69,12 @@ void usb_init()
debug_consume = 0;
mouse_consume = 0;
mouse_cb = NULL;
keyboard_consume = 0;
keyboard_cb = NULL;

mask = irq_getmask();
mask |= IRQ_USB;
irq_setmask(mask);
}

static void flush_debug_buffer()
Expand All @@ -72,12 +84,21 @@ static void flush_debug_buffer()
debug_len = 0;
}

void usb_service()
void usb_set_mouse_cb(mouse_event_cb cb)
{
mouse_cb = cb;
}

void usb_set_keyboard_cb(keyboard_event_cb cb)
{
keyboard_cb = cb;
}

void usb_isr()
{
char c;

if(!(CSR_CAPABILITIES & CAP_USB))
return;
irq_ack(IRQ_USB);

while(debug_consume != COMLOC_DEBUG_PRODUCE) {
c = COMLOC_DEBUG(debug_consume);
Expand All @@ -101,9 +122,13 @@ void usb_service()
COMLOC_MEVT(4*mouse_consume+3));
mouse_consume = (mouse_consume + 1) & 0x0f;
}

while(keyboard_consume != COMLOC_KEVT_PRODUCE) {
if(keyboard_cb != NULL)
keyboard_cb(
COMLOC_KEVT(2*keyboard_consume+0),
COMLOC_KEVT(2*keyboard_consume+1));
keyboard_consume = (keyboard_consume + 1) & 0x07;
}
}

void usb_set_mouse_cb(mouse_event_cb cb)
{
mouse_cb = cb;
}

0 comments on commit 2a61106

Please sign in to comment.