STM32-P103: add usbserial preset
[bertos.git] / boards / stm32-p103 / examples / usbserial / 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 USB/Serial converter for the STM32-P103 evaluation board.
36  *
37  * This example implements a real USB/serial converter using the STM32-P103
38  * evaluation board. Two independent processes are created: one that reads from
39  * the USB port and writes to the UART port, the other does the opposite.
40  *
41  * Compile and flash the firmware image to the STM32-P103. Then, the board can
42  * be connected to any USB port of a standard PC and it will be recognized as a
43  * generic USB/serial converter.
44  *
45  * For example, on a Linux PC:
46  *
47  * $ dmesg
48  * [18058.280545] usb 1-8.1.3: new full speed USB device using ehci_hcd and address 25
49  * [18058.392775] usb 1-8.1.3: configuration #1 chosen from 1 choice
50  * [18058.393118] usbserial_generic 1-8.1.3:1.0: generic converter detected
51  * [18058.393332] usb 1-8.1.3: generic converter now attached to ttyUSB1
52  *
53  */
54
55 #include "hw/hw_led.h"
56
57 #include <cfg/debug.h>
58 #include <cfg/log.h>
59
60 #include <cpu/irq.h>
61
62 #include <drv/timer.h>
63 #include <drv/ser.h>
64 #include <drv/usbser.h>
65
66 #include <kern/monitor.h>
67
68 enum {
69         USB_TO_SERIAL,
70         SERIAL_TO_USB,
71 };
72
73 static Serial ser_port;
74 static USBSerial usb_port;
75
76 INLINE void LED_TOGGLE(void)
77 {
78         static int led_status;
79
80         if ((led_status = !led_status) != 0)
81                 ATOMIC(LED_ON());
82         else
83                 ATOMIC(LED_OFF());
84 }
85
86 static void init(void)
87 {
88         /* Enable all the interrupts */
89         IRQ_ENABLE;
90
91         /* Initialize debugging module (allow kprintf(), etc.) */
92         kdbg_init();
93         /* Initialize system timer */
94         timer_init();
95         /* Initialize LED driver */
96         LED_INIT();
97         LED_OFF();
98
99         /* Kernel initialization */
100         proc_init();
101
102         /* Initialize the serial driver */
103         ser_init(&ser_port, SER_UART2);
104         /*
105          * Hard-code the baud rate to 115.200 bps.
106          *
107          * TODO: implement the baud rate settings as well as other UART
108          * settings over the USB connection.
109          */
110         ser_setbaudrate(&ser_port, 115200);
111
112         /* Initialize usb-serial driver */
113         usbser_init(&usb_port, 0);
114 }
115
116 /* Process that reads from the USB port and writes to the UART port */
117 static void NORETURN usb_serial_process(void)
118 {
119         iptr_t type = proc_currentUserData();
120
121         KFile *in_fd = (type == USB_TO_SERIAL) ? &usb_port.fd : &ser_port.fd;
122         KFile *out_fd = (type == USB_TO_SERIAL) ? &ser_port.fd : &usb_port.fd;
123
124         while (1)
125         {
126                 int c;
127
128                 c = kfile_getc(in_fd);
129                 if (UNLIKELY(c == EOF))
130                 {
131                         kfile_clearerr(in_fd);
132                         continue;
133                 }
134                 kfile_putc(c, out_fd);
135
136                 /*
137                  * Toggle the STAT LED when some data passes through the
138                  * usb-seral link
139                  */
140                 LED_TOGGLE();
141         }
142 }
143
144 int main(void)
145 {
146         /* Hardware initialization */
147         init();
148
149         proc_new(usb_serial_process, (iptr_t)USB_TO_SERIAL,
150                         KERN_MINSTACKSIZE * 2, NULL);
151         proc_new(usb_serial_process, (iptr_t)SERIAL_TO_USB,
152                         KERN_MINSTACKSIZE * 2, NULL);
153
154         /* Main process will never be scheduled. */
155         sig_wait(SIG_USER0);
156         ASSERT(0);
157 }