2011-12-22 03:04:29 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup OpenPilotModules OpenPilot Modules
|
2013-05-19 16:37:30 +02:00
|
|
|
* @{
|
2011-12-22 03:04:29 +01:00
|
|
|
* @addtogroup ComUsbBridgeModule Com Port to USB VCP Bridge Module
|
|
|
|
* @brief Bridge Com and USB VCP ports
|
2013-05-19 16:37:30 +02:00
|
|
|
* @{
|
2011-12-22 03:04:29 +01:00
|
|
|
*
|
|
|
|
* @file ComUsbBridge.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
|
|
|
|
* @brief Bridges selected Com Port to the USB VCP emulated serial port
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
// ****************
|
|
|
|
|
2013-05-02 23:31:14 +02:00
|
|
|
#include <openpilot.h>
|
|
|
|
|
|
|
|
#include "taskinfo.h"
|
2011-12-22 03:04:29 +01:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2017-04-19 15:57:34 +02:00
|
|
|
#include <pios_board_io.h>
|
|
|
|
|
2011-12-22 03:04:29 +01:00
|
|
|
// ****************
|
|
|
|
// Private functions
|
|
|
|
|
|
|
|
static void com2UsbBridgeTask(void *parameters);
|
|
|
|
static void usb2ComBridgeTask(void *parameters);
|
2015-04-26 04:05:47 +02:00
|
|
|
static void usb2ComBridgeSetCtrlLine(uint32_t com_id, uint32_t mask, uint32_t state);
|
2016-08-30 01:22:57 +02:00
|
|
|
static void usb2ComBridgeSetBaudRate(uint32_t com_id, uint32_t baud);
|
|
|
|
|
2011-12-22 03:04:29 +01:00
|
|
|
|
|
|
|
// ****************
|
|
|
|
// Private constants
|
|
|
|
|
2013-05-21 00:45:45 +02:00
|
|
|
#define U2C_STACK_SIZE_BYTES 260
|
2013-05-25 09:40:41 +02:00
|
|
|
#define C2U_STACK_SIZE_BYTES 316
|
2013-05-21 00:45:45 +02:00
|
|
|
|
2013-05-26 17:36:38 +02:00
|
|
|
#define TASK_PRIORITY (tskIDLE_PRIORITY + 1)
|
2011-12-22 03:04:29 +01:00
|
|
|
|
2013-05-26 17:36:38 +02:00
|
|
|
#define BRIDGE_BUF_LEN 10
|
2011-12-22 03:04:29 +01:00
|
|
|
|
|
|
|
// ****************
|
|
|
|
// Private variables
|
|
|
|
|
|
|
|
static xTaskHandle com2UsbBridgeTaskHandle;
|
|
|
|
static xTaskHandle usb2ComBridgeTaskHandle;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static uint8_t *com2usb_buf;
|
|
|
|
static uint8_t *usb2com_buf;
|
2011-12-22 03:04:29 +01:00
|
|
|
|
|
|
|
static uint32_t usart_port;
|
|
|
|
static uint32_t vcp_port;
|
|
|
|
|
2011-12-28 18:24:32 +01:00
|
|
|
static bool bridge_enabled = false;
|
|
|
|
|
2011-12-22 03:04:29 +01:00
|
|
|
/**
|
|
|
|
* Initialise the module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int32_t comUsbBridgeStart(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (bridge_enabled) {
|
|
|
|
// Start tasks
|
2014-06-18 01:47:43 +02:00
|
|
|
xTaskCreate(com2UsbBridgeTask, "Com2UsbBridge", C2U_STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &com2UsbBridgeTaskHandle);
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_COM2USBBRIDGE, com2UsbBridgeTaskHandle);
|
2014-06-18 01:47:43 +02:00
|
|
|
xTaskCreate(usb2ComBridgeTask, "Usb2ComBridge", U2C_STACK_SIZE_BYTES / 4, NULL, TASK_PRIORITY, &usb2ComBridgeTaskHandle);
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_TASK_MONITOR_RegisterTask(TASKINFO_RUNNING_USB2COMBRIDGE, usb2ComBridgeTaskHandle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2011-12-22 03:04:29 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Initialise the module
|
|
|
|
* \return -1 if initialisation failed
|
|
|
|
* \return 0 on success
|
|
|
|
*/
|
|
|
|
static int32_t comUsbBridgeInitialize(void)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// TODO: Get from settings object
|
2015-04-26 04:05:47 +02:00
|
|
|
usart_port = PIOS_COM_BRIDGE;
|
|
|
|
vcp_port = PIOS_COM_VCP;
|
|
|
|
|
|
|
|
// Register the call back handler for USB control line changes to simply
|
|
|
|
// pass these onto any handler registered on the USART
|
|
|
|
if (vcp_port) {
|
|
|
|
PIOS_COM_RegisterCtrlLineCallback(vcp_port,
|
|
|
|
usb2ComBridgeSetCtrlLine,
|
|
|
|
usart_port);
|
2016-08-30 01:22:57 +02:00
|
|
|
PIOS_COM_RegisterBaudRateCallback(vcp_port,
|
|
|
|
usb2ComBridgeSetBaudRate,
|
|
|
|
usart_port);
|
2015-04-26 04:05:47 +02:00
|
|
|
}
|
2011-12-22 03:04:29 +01:00
|
|
|
|
2013-03-25 15:02:16 +01:00
|
|
|
#ifdef MODULE_COMUSBBRIDGE_BUILTIN
|
2013-05-19 16:37:30 +02:00
|
|
|
bridge_enabled = true;
|
2012-01-14 22:55:55 +01:00
|
|
|
#else
|
2011-12-28 18:24:32 +01:00
|
|
|
|
2014-08-21 11:43:36 +02:00
|
|
|
if (usart_port && vcp_port) {
|
2013-05-19 16:37:30 +02:00
|
|
|
bridge_enabled = true;
|
|
|
|
} else {
|
|
|
|
bridge_enabled = false;
|
|
|
|
}
|
2012-01-14 22:55:55 +01:00
|
|
|
#endif
|
2011-12-28 18:24:32 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (bridge_enabled) {
|
2014-06-11 20:11:48 +02:00
|
|
|
com2usb_buf = pios_malloc(BRIDGE_BUF_LEN);
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(com2usb_buf);
|
2014-06-11 20:11:48 +02:00
|
|
|
usb2com_buf = pios_malloc(BRIDGE_BUF_LEN);
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_Assert(usb2com_buf);
|
|
|
|
}
|
2011-12-22 03:04:29 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2011-12-22 03:04:29 +01:00
|
|
|
}
|
2013-06-04 05:37:40 +02:00
|
|
|
MODULE_INITCALL(comUsbBridgeInitialize, comUsbBridgeStart);
|
2011-12-22 03:04:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main task. It does not return.
|
|
|
|
*/
|
|
|
|
|
2013-05-05 09:02:24 +02:00
|
|
|
static void com2UsbBridgeTask(__attribute__((unused)) void *parameters)
|
2011-12-22 03:04:29 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Handle usart -> vcp direction */
|
|
|
|
volatile uint32_t tx_errors = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint32_t rx_bytes;
|
|
|
|
|
|
|
|
rx_bytes = PIOS_COM_ReceiveBuffer(usart_port, com2usb_buf, BRIDGE_BUF_LEN, 500);
|
|
|
|
if (rx_bytes > 0) {
|
|
|
|
/* Bytes available to transfer */
|
|
|
|
if (PIOS_COM_SendBuffer(vcp_port, com2usb_buf, rx_bytes) != (int32_t)rx_bytes) {
|
|
|
|
/* Error on transmit */
|
|
|
|
tx_errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 03:04:29 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
static void usb2ComBridgeTask(__attribute__((unused)) void *parameters)
|
2011-12-22 03:04:29 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Handle vcp -> usart direction */
|
|
|
|
volatile uint32_t tx_errors = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint32_t rx_bytes;
|
|
|
|
|
|
|
|
rx_bytes = PIOS_COM_ReceiveBuffer(vcp_port, usb2com_buf, BRIDGE_BUF_LEN, 500);
|
|
|
|
if (rx_bytes > 0) {
|
|
|
|
/* Bytes available to transfer */
|
|
|
|
if (PIOS_COM_SendBuffer(usart_port, usb2com_buf, rx_bytes) != (int32_t)rx_bytes) {
|
|
|
|
/* Error on transmit */
|
|
|
|
tx_errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 03:04:29 +01:00
|
|
|
}
|
2011-12-28 18:24:32 +01:00
|
|
|
|
2015-04-26 04:05:47 +02:00
|
|
|
/* This routine is registered with the USB driver and will be called in the
|
|
|
|
* event of a control line state change. It will then call down to the USART
|
|
|
|
* driver to drive the required control line state.
|
|
|
|
*/
|
|
|
|
static void usb2ComBridgeSetCtrlLine(uint32_t com_id, uint32_t mask, uint32_t state)
|
|
|
|
{
|
|
|
|
PIOS_COM_SetCtrlLine(com_id, mask, state);
|
|
|
|
}
|
2011-12-28 18:24:32 +01:00
|
|
|
|
2016-08-30 01:22:57 +02:00
|
|
|
static void usb2ComBridgeSetBaudRate(uint32_t com_id, uint32_t baud)
|
2011-12-28 18:24:32 +01:00
|
|
|
{
|
2016-08-30 01:22:57 +02:00
|
|
|
PIOS_COM_ChangeBaud(com_id, baud);
|
2011-12-28 18:24:32 +01:00
|
|
|
}
|