From 64f498e48a1ebf5f4585f8b44e62badece40df56 Mon Sep 17 00:00:00 2001 From: Stacey Sheldon Date: Wed, 21 Dec 2011 21:04:29 -0500 Subject: [PATCH] com-bridge: add usart to usb vcp bridge --- flight/CopterControl/Makefile | 2 +- flight/CopterControl/System/pios_board.c | 146 +++++++++++++-------- flight/Modules/ComUsbBridge/ComUsbBridge.c | 135 +++++++++++++++++++ flight/PiOS/Boards/STM32103CB_CC_Rev1.h | 6 + shared/uavobjectdefinition/hwsettings.xml | 4 +- 5 files changed, 237 insertions(+), 56 deletions(-) create mode 100644 flight/Modules/ComUsbBridge/ComUsbBridge.c diff --git a/flight/CopterControl/Makefile b/flight/CopterControl/Makefile index f0eb31cb9..66186ed86 100644 --- a/flight/CopterControl/Makefile +++ b/flight/CopterControl/Makefile @@ -70,7 +70,7 @@ ifeq ($(USE_GPS), YES) OPTMODULES += GPS endif -MODULES = Attitude Stabilization Actuator ManualControl FirmwareIAP +MODULES = Attitude Stabilization Actuator ManualControl FirmwareIAP ComUsbBridge # Telemetry must be last to grab the optional modules (why?) MODULES += Telemetry diff --git a/flight/CopterControl/System/pios_board.c b/flight/CopterControl/System/pios_board.c index a5131bf10..70c60d1bb 100644 --- a/flight/CopterControl/System/pios_board.c +++ b/flight/CopterControl/System/pios_board.c @@ -788,6 +788,9 @@ static const struct pios_sbus_cfg pios_sbus_cfg = { #define PIOS_COM_TELEM_USB_RX_BUF_LEN 192 #define PIOS_COM_TELEM_USB_TX_BUF_LEN 192 +#define PIOS_COM_BRIDGE_RX_BUF_LEN 65 +#define PIOS_COM_BRIDGE_TX_BUF_LEN 65 + #endif /* PIOS_INCLUDE_COM */ #if defined(PIOS_INCLUDE_RTC) @@ -1020,7 +1023,9 @@ const struct pios_usb_com_cfg pios_usb_com_cdc_cfg = { uint32_t pios_com_telem_rf_id; uint32_t pios_com_telem_usb_id; +uint32_t pios_com_vcp_id = 0; uint32_t pios_com_gps_id; +uint32_t pios_com_bridge_id = 0; /** * PIOS_Board_Init() @@ -1065,6 +1070,58 @@ void PIOS_Board_Init(void) { PIOS_TIM_InitClock(&tim_3_cfg); PIOS_TIM_InitClock(&tim_4_cfg); +#if defined(PIOS_INCLUDE_USB_HID) + uint32_t pios_usb_id; + PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg); + + /* Configure the usb telemetry protocol */ + uint8_t hwsettings_usb_telemetry_channel; + HwSettingsUSB_TelemetryChannelGet(&hwsettings_usb_telemetry_channel); + + switch (hwsettings_usb_telemetry_channel) { + case HWSETTINGS_USB_TELEMETRYCHANNEL_HID: +#if defined(PIOS_INCLUDE_COM) + { + uint32_t pios_usb_com_id; + if (PIOS_USB_COM_Init(&pios_usb_com_id, &pios_usb_com_hid_cfg, pios_usb_id)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_com_id, + rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, + tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { + PIOS_Assert(0); + } + } +#endif /* PIOS_INCLUDE_COM */ + break; + case HWSETTINGS_USB_TELEMETRYCHANNEL_CDC: +#if defined(PIOS_INCLUDE_COM) + { + uint32_t pios_usb_com_id; + if (PIOS_USB_COM_Init(&pios_usb_com_id, &pios_usb_com_cdc_cfg, pios_usb_id)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_cdc_com_driver, pios_usb_com_id, + rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, + tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { + PIOS_Assert(0); + } + } +#endif /* PIOS_INCLUDE_COM */ + break; + default: + PIOS_Assert(0); + } +#endif /* PIOS_INCLUDE_USB_HID */ + /* Configure the main IO port */ uint8_t hwsettings_DSMxBind; HwSettingsDSMxBindGet(&hwsettings_DSMxBind); @@ -1179,6 +1236,40 @@ void PIOS_Board_Init(void) { break; case HWSETTINGS_CC_MAINPORT_COMAUX: break; + case HWSETTINGS_CC_MAINPORT_COMBRIDGE: + { + uint32_t pios_usart_generic_id; + if (PIOS_USART_Init(&pios_usart_generic_id, &pios_usart_generic_main_cfg)) { + PIOS_Assert(0); + } + + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_BRIDGE_RX_BUF_LEN); + PIOS_Assert(rx_buffer); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_BRIDGE_TX_BUF_LEN); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_bridge_id, &pios_usart_com_driver, pios_usart_generic_id, + rx_buffer, PIOS_COM_BRIDGE_RX_BUF_LEN, + tx_buffer, PIOS_COM_BRIDGE_TX_BUF_LEN)) { + PIOS_Assert(0); + } + } + { + uint32_t pios_usb_vcp_id; + if (PIOS_USB_COM_Init(&pios_usb_vcp_id, &pios_usb_com_cdc_cfg, pios_usb_id)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_BRIDGE_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_BRIDGE_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_vcp_id, &pios_usb_cdc_com_driver, pios_usb_vcp_id, + rx_buffer, PIOS_COM_BRIDGE_RX_BUF_LEN, + tx_buffer, PIOS_COM_BRIDGE_TX_BUF_LEN)) { + PIOS_Assert(0); + } + + } + break; } /* Configure the flexi port */ @@ -1278,6 +1369,8 @@ void PIOS_Board_Init(void) { } #endif /* PIOS_INCLUDE_I2C */ break; + case HWSETTINGS_CC_FLEXIPORT_COMBRIDGE: + break; } /* Configure the rcvr port */ @@ -1349,59 +1442,6 @@ void PIOS_Board_Init(void) { PIOS_ADC_Init(); PIOS_GPIO_Init(); - -#if defined(PIOS_INCLUDE_USB_HID) - uint32_t pios_usb_id; - PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg); - - /* Configure the usb telemetry protocol */ - uint8_t hwsettings_usb_telemetry_channel; - HwSettingsUSB_TelemetryChannelGet(&hwsettings_usb_telemetry_channel); - - switch (hwsettings_usb_telemetry_channel) { - case HWSETTINGS_USB_TELEMETRYCHANNEL_HID: -#if defined(PIOS_INCLUDE_COM) - { - uint32_t pios_usb_com_id; - if (PIOS_USB_COM_Init(&pios_usb_com_id, &pios_usb_com_hid_cfg, pios_usb_id)) { - PIOS_Assert(0); - } - uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); - uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); - PIOS_Assert(rx_buffer); - PIOS_Assert(tx_buffer); - if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_com_id, - rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, - tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { - PIOS_Assert(0); - } - } -#endif /* PIOS_INCLUDE_COM */ - break; - case HWSETTINGS_USB_TELEMETRYCHANNEL_CDC: -#if defined(PIOS_INCLUDE_COM) - { - uint32_t pios_usb_com_id; - if (PIOS_USB_COM_Init(&pios_usb_com_id, &pios_usb_com_cdc_cfg, pios_usb_id)) { - PIOS_Assert(0); - } - uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); - uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); - PIOS_Assert(rx_buffer); - PIOS_Assert(tx_buffer); - if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_cdc_com_driver, pios_usb_com_id, - rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, - tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { - PIOS_Assert(0); - } - } -#endif /* PIOS_INCLUDE_COM */ - break; - default: - PIOS_Assert(0); - } -#endif /* PIOS_INCLUDE_USB_HID */ - PIOS_IAP_Init(); #ifndef ERASE_FLASH PIOS_WDG_Init(); diff --git a/flight/Modules/ComUsbBridge/ComUsbBridge.c b/flight/Modules/ComUsbBridge/ComUsbBridge.c new file mode 100644 index 000000000..c84c93c22 --- /dev/null +++ b/flight/Modules/ComUsbBridge/ComUsbBridge.c @@ -0,0 +1,135 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotModules OpenPilot Modules + * @{ + * @addtogroup ComUsbBridgeModule Com Port to USB VCP Bridge Module + * @brief Bridge Com and USB VCP ports + * @{ + * + * @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 + */ + +// **************** + +#include "openpilot.h" + +#include + +// **************** +// Private functions + +static void com2UsbBridgeTask(void *parameters); +static void usb2ComBridgeTask(void *parameters); + +// **************** +// Private constants + +#define STACK_SIZE_BYTES 280 +#define TASK_PRIORITY (tskIDLE_PRIORITY + 1) + +#define BRIDGE_BUF_LEN 10 + +// **************** +// Private variables + +static xTaskHandle com2UsbBridgeTaskHandle; +static xTaskHandle usb2ComBridgeTaskHandle; + +static uint8_t * com2usb_buf; +static uint8_t * usb2com_buf; + +static uint32_t usart_port; +static uint32_t vcp_port; + +/** + * Initialise the module + * \return -1 if initialisation failed + * \return 0 on success + */ + +static int32_t comUsbBridgeStart(void) +{ + if (usart_port && vcp_port) { + // Start tasks + xTaskCreate(com2UsbBridgeTask, (signed char *)"Com2UsbBridge", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &com2UsbBridgeTaskHandle); + xTaskCreate(usb2ComBridgeTask, (signed char *)"Usb2ComBridge", STACK_SIZE_BYTES/4, NULL, TASK_PRIORITY, &usb2ComBridgeTaskHandle); + } + + return 0; +} +/** + * Initialise the module + * \return -1 if initialisation failed + * \return 0 on success + */ +static int32_t comUsbBridgeInitialize(void) +{ + // TODO: Get from settings object + usart_port = PIOS_COM_BRIDGE; + vcp_port = PIOS_COM_VCP; + + com2usb_buf = pvPortMalloc(BRIDGE_BUF_LEN); + PIOS_Assert(com2usb_buf); + usb2com_buf = pvPortMalloc(BRIDGE_BUF_LEN); + PIOS_Assert(usb2com_buf); + + return 0; +} +MODULE_INITCALL(comUsbBridgeInitialize, comUsbBridgeStart) + +/** + * Main task. It does not return. + */ + +static void com2UsbBridgeTask(void *parameters) +{ + /* Handle usart -> vcp direction */ + while (1) { + uint32_t rx_bytes; + + rx_bytes = PIOS_COM_ReceiveBuffer(usart_port, com2usb_buf, BRIDGE_BUF_LEN, 500); + if (rx_bytes > 0) { + if (PIOS_COM_SendBuffer(vcp_port, com2usb_buf, rx_bytes)) { + vTaskDelay(10 / portTICK_RATE_MS); + } + } else { + vTaskDelay(10 / portTICK_RATE_MS); + } + } +} + +static void usb2ComBridgeTask(void * parameters) +{ + /* Handle vcp -> usart direction */ + while (1) { + uint32_t rx_bytes; + + rx_bytes = PIOS_COM_ReceiveBuffer(vcp_port, usb2com_buf, BRIDGE_BUF_LEN, 500); + if (rx_bytes > 0) { + if (PIOS_COM_SendBuffer(usart_port, usb2com_buf, rx_bytes)) { + vTaskDelay(10 / portTICK_RATE_MS); + } + } else { + vTaskDelay(10 / portTICK_RATE_MS); + } + } +} diff --git a/flight/PiOS/Boards/STM32103CB_CC_Rev1.h b/flight/PiOS/Boards/STM32103CB_CC_Rev1.h index a88589ed2..8221d659b 100644 --- a/flight/PiOS/Boards/STM32103CB_CC_Rev1.h +++ b/flight/PiOS/Boards/STM32103CB_CC_Rev1.h @@ -141,6 +141,12 @@ extern uint32_t pios_com_gps_id; #define PIOS_COM_GPS (pios_com_gps_id) #endif /* PIOS_INCLUDE_GPS */ +extern uint32_t pios_com_bridge_id; +#define PIOS_COM_BRIDGE (pios_com_bridge_id) + +extern uint32_t pios_com_vcp_id; +#define PIOS_COM_VCP (pios_com_vcp_id) + extern uint32_t pios_com_telem_usb_id; #define PIOS_COM_TELEM_USB (pios_com_telem_usb_id) diff --git a/shared/uavobjectdefinition/hwsettings.xml b/shared/uavobjectdefinition/hwsettings.xml index 0b77d0857..d259ee6c2 100644 --- a/shared/uavobjectdefinition/hwsettings.xml +++ b/shared/uavobjectdefinition/hwsettings.xml @@ -2,8 +2,8 @@ Selection of optional hardware configurations. - - + +