STM32-P103: add usbkeyboard preset
[bertos.git] / boards / stm32-p103 / examples / usbkeyboard / main.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \author Andrea Righi <arighi@develer.com>
34  *
35  * \brief BeRTOS virtual keyboard.
36  *
37  * This project implements a HID USB device using the Olimex STM32-P103
38  * evaluation board. The virtual keyboard can be connected to the USB port of a
39  * normal PC.
40  *
41  * The WAKE_UP button on the board can be pressed to send to the host the
42  * keyboard scancodes that simulate the typing of http://www.bertos.org.
43  *
44  * NOTE: this only works if the keyboard layout on the host PC is set to "US"
45  * (QWERTY).
46  *
47  * Example (keyboard detection on Linux):
48  *
49  * [24388.400573] usb 1-8.1.3: new full speed USB device using ehci_hcd and address 32
50  * [24388.623957] usb 1-8.1.3: configuration #1 chosen from 1 choice
51  * [24388.685827] input: BeRTOS USB Keyboard as /devices/pci0000:00/0000:00:1d.7/usb1/1-8/1-8.1/1-8.1.3/1-8.1.3:1.0/input/input18
52  * [24388.688953] generic-usb 0003:FFFF:0000.0007: input,hidraw3: USB HID v1.10 Keyboard [BeRTOS USB Keyboard] on usb-0000:00:1d.7-8.1.3/input0
53  *
54  */
55
56 #include "hw/hw_led.h"
57
58 #include <cfg/debug.h>
59
60 #include <cpu/irq.h>
61 #include <cpu/power.h>
62
63 #include <drv/kbd.h>
64 #include <drv/timer.h>
65 #include <drv/usbkbd.h>
66
67 static void init(void)
68 {
69         /* Enable all the interrupts */
70         IRQ_ENABLE;
71
72         /* Initialize debugging module (allow kprintf(), etc.) */
73         kdbg_init();
74         /* Initialize system timer */
75         timer_init();
76         /* Initialize LED driver */
77         LED_INIT();
78         /* Enable the WAKE_UP button on the board */
79         kbd_init();
80         /* Initialize the USB keyboard device */
81         usbkbd_init(0);
82 }
83
84 /* Simulate the pression and release of a key on the keyboard */
85 static void usb_send_key(uint8_t mod, uint8_t c)
86 {
87         usbkbd_sendEvent(mod, c);
88         usbkbd_sendEvent(0, 0);
89 }
90
91 /* XXX: note that these scancodes are valid only with the US keyboard layout */
92 static const uint16_t keys[] =
93 {
94         /* http:// */
95         0x000b, 0x0017, 0x0017, 0x0013, 0x2033, 0x0038, 0x0038,
96         /* www. */
97         0x001a, 0x001a, 0x001a, 0x0037,
98         /* bertos. */
99         0x0005, 0x0008, 0x0015, 0x0017, 0x0012, 0x0016, 0x0037,
100         /* org. */
101         0x0012, 0x0015, 0x000a,
102         /* \n */
103         0x0028,
104 };
105
106 int main(void)
107 {
108         /* Hardware initialization */
109         init();
110
111         kprintf("USB HID Keyboard configured\n");
112         kbd_setRepeatMask(K_WAKEUP);
113         while (1)
114         {
115                 unsigned int i;
116
117                 /* Wait until the WAKE_UP button is pressed */
118                 kbd_get();
119                 /* Send the keyboard scancodes */
120                 for (i = 0; i < countof(keys); i++)
121                         usb_send_key((keys[i] & 0xff00) >> 8, keys[i] & 0xff);
122         }
123 }