4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \author Andrea Righi <arighi@develer.com>
35 * \brief USB/Serial converter for the STM32-P103 evaluation board.
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.
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.
45 * For example, on a Linux PC:
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
55 #include "hw/hw_led.h"
57 #include <cfg/debug.h>
62 #include <drv/timer.h>
64 #include <drv/usbser.h>
66 #include <kern/monitor.h>
73 static Serial ser_port;
74 static USBSerial usb_port;
76 INLINE void LED_TOGGLE(void)
78 static int led_status;
80 if ((led_status = !led_status) != 0)
86 static void init(void)
88 /* Enable all the interrupts */
91 /* Initialize debugging module (allow kprintf(), etc.) */
93 /* Initialize system timer */
95 /* Initialize LED driver */
99 /* Kernel initialization */
102 /* Initialize the serial driver */
103 ser_init(&ser_port, SER_UART2);
105 * Hard-code the baud rate to 115.200 bps.
107 * TODO: implement the baud rate settings as well as other UART
108 * settings over the USB connection.
110 ser_setbaudrate(&ser_port, 115200);
112 /* Initialize usb-serial driver */
113 usbser_init(&usb_port, 0);
116 /* Process that reads from the USB port and writes to the UART port */
117 static void NORETURN usb_serial_process(void)
119 iptr_t type = proc_currentUserData();
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;
128 c = kfile_getc(in_fd);
129 if (UNLIKELY(c == EOF))
131 kfile_clearerr(in_fd);
134 kfile_putc(c, out_fd);
137 * Toggle the STAT LED when some data passes through the
146 /* Hardware initialization */
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);
154 /* Main process will never be scheduled. */