LP-500 HoTT Telemetry added device definitions
[librepilot.git] / flight / modules / ComUsbBridge / ComUsbBridge.c
blob15f9f5db9703373aa1fda4a61c757dd4cb80519d
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup ComUsbBridgeModule Com Port to USB VCP Bridge Module
6 * @brief Bridge Com and USB VCP ports
7 * @{
9 * @file ComUsbBridge.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
11 * @brief Bridges selected Com Port to the USB VCP emulated serial port
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 // ****************
33 #include <openpilot.h>
35 #include "taskinfo.h"
37 #include <stdbool.h>
39 // ****************
40 // Private functions
42 static void com2UsbBridgeTask(void *parameters);
43 static void usb2ComBridgeTask(void *parameters);
44 static void usb2ComBridgeSetCtrlLine(uint32_t com_id, uint32_t mask, uint32_t state);
45 static void usb2ComBridgeSetBaudRate(uint32_t com_id, uint32_t baud);
48 // ****************
49 // Private constants
51 #define U2C_STACK_SIZE_BYTES 260
52 #define C2U_STACK_SIZE_BYTES 316
54 #define TASK_PRIORITY (tskIDLE_PRIORITY + 1)
56 #define BRIDGE_BUF_LEN 10
58 // ****************
59 // Private variables
61 static xTaskHandle com2UsbBridgeTaskHandle;
62 static xTaskHandle usb2ComBridgeTaskHandle;
64 static uint8_t *com2usb_buf;
65 static uint8_t *usb2com_buf;
67 static uint32_t usart_port;
68 static uint32_t vcp_port;
70 static bool bridge_enabled = false;
72 /**
73 * Initialise the module
74 * \return -1 if initialisation failed
75 * \return 0 on success
78 static int32_t comUsbBridgeStart(void)
80 if (bridge_enabled) {
81 // Start tasks
82 xTaskCreate(com2UsbBridgeTask, "Com2UsbBridge", C2U_STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &com2UsbBridgeTaskHandle);
83 PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_COM2USBBRIDGE, com2UsbBridgeTaskHandle);
84 xTaskCreate(usb2ComBridgeTask, "Usb2ComBridge", U2C_STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &usb2ComBridgeTaskHandle);
85 PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_USB2COMBRIDGE, usb2ComBridgeTaskHandle);
86 return 0;
89 return -1;
91 /**
92 * Initialise the module
93 * \return -1 if initialisation failed
94 * \return 0 on success
96 static int32_t comUsbBridgeInitialize(void)
98 // TODO: Get from settings object
99 usart_port = PIOS_COM_BRIDGE;
100 vcp_port = PIOS_COM_VCP;
102 // Register the call back handler for USB control line changes to simply
103 // pass these onto any handler registered on the USART
104 if (vcp_port) {
105 PIOS_COM_RegisterCtrlLineCallback(vcp_port,
106 usb2ComBridgeSetCtrlLine,
107 usart_port);
108 PIOS_COM_RegisterBaudRateCallback(vcp_port,
109 usb2ComBridgeSetBaudRate,
110 usart_port);
113 #ifdef MODULE_COMUSBBRIDGE_BUILTIN
114 bridge_enabled = true;
115 #else
117 if (usart_port && vcp_port) {
118 bridge_enabled = true;
119 } else {
120 bridge_enabled = false;
122 #endif
124 if (bridge_enabled) {
125 com2usb_buf = pios_malloc(BRIDGE_BUF_LEN);
126 PIOS_Assert(com2usb_buf);
127 usb2com_buf = pios_malloc(BRIDGE_BUF_LEN);
128 PIOS_Assert(usb2com_buf);
131 return 0;
133 MODULE_INITCALL(comUsbBridgeInitialize, comUsbBridgeStart);
136 * Main task. It does not return.
139 static void com2UsbBridgeTask(__attribute__((unused)) void *parameters)
141 /* Handle usart -> vcp direction */
142 volatile uint32_t tx_errors = 0;
144 while (1) {
145 uint32_t rx_bytes;
147 rx_bytes = PIOS_COM_ReceiveBuffer(usart_port, com2usb_buf, BRIDGE_BUF_LEN, 500);
148 if (rx_bytes > 0) {
149 /* Bytes available to transfer */
150 if (PIOS_COM_SendBuffer(vcp_port, com2usb_buf, rx_bytes) != (int32_t)rx_bytes) {
151 /* Error on transmit */
152 tx_errors++;
158 static void usb2ComBridgeTask(__attribute__((unused)) void *parameters)
160 /* Handle vcp -> usart direction */
161 volatile uint32_t tx_errors = 0;
163 while (1) {
164 uint32_t rx_bytes;
166 rx_bytes = PIOS_COM_ReceiveBuffer(vcp_port, usb2com_buf, BRIDGE_BUF_LEN, 500);
167 if (rx_bytes > 0) {
168 /* Bytes available to transfer */
169 if (PIOS_COM_SendBuffer(usart_port, usb2com_buf, rx_bytes) != (int32_t)rx_bytes) {
170 /* Error on transmit */
171 tx_errors++;
177 /* This routine is registered with the USB driver and will be called in the
178 * event of a control line state change. It will then call down to the USART
179 * driver to drive the required control line state.
181 static void usb2ComBridgeSetCtrlLine(uint32_t com_id, uint32_t mask, uint32_t state)
183 PIOS_COM_SetCtrlLine(com_id, mask, state);
186 static void usb2ComBridgeSetBaudRate(uint32_t com_id, uint32_t baud)
188 PIOS_COM_ChangeBaud(com_id, baud);