dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ComUsbBridge.c
Go to the documentation of this file.
1 
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with this program; if not, see <http://www.gnu.org/licenses/>
30  *
31  * Additional note on redistribution: The copyright and license notices above
32  * must be maintained in each individual source file that is a derivative work
33  * of this source file; otherwise redistribution is prohibited.
34  */
35 
36 // ****************
37 
38 #include "openpilot.h"
39 
40 #include "modulesettings.h"
41 #include "pios_thread.h"
42 #include <pios_hal.h>
43 #include "pios_modules.h"
44 
45 #include <stdbool.h>
46 
47 // ****************
48 // Private functions
49 
50 static void com2UsbBridgeTask(void *parameters);
51 static void usb2ComBridgeTask(void *parameters);
52 static void updateSettings();
53 
54 // ****************
55 // Private constants
56 
57 #if defined(PIOS_COMUSBBRIDGE_STACK_SIZE)
58 #define STACK_SIZE_BYTES PIOS_COMUSBBRIDGE_STACK_SIZE
59 #else
60 #define STACK_SIZE_BYTES 480
61 #endif
62 
63 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
64 
65 #define BRIDGE_BUF_LEN 10
66 
67 // ****************
68 // Private variables
69 
70 static struct pios_thread *com2UsbBridgeTaskHandle;
71 static struct pios_thread *usb2ComBridgeTaskHandle;
72 
73 static uint32_t usart_port;
74 static uint32_t vcp_port;
75 
76 static bool module_enabled = false;
77 
84 static int32_t comUsbBridgeStart(void)
85 {
86  if (module_enabled) {
87  // Start tasks
89  TaskMonitorAdd(TASKINFO_RUNNING_COM2USBBRIDGE, com2UsbBridgeTaskHandle);
91  TaskMonitorAdd(TASKINFO_RUNNING_USB2COMBRIDGE, usb2ComBridgeTaskHandle);
92  return 0;
93  }
94 
95  return -1;
96 }
102 static int32_t comUsbBridgeInitialize(void)
103 {
104  // TODO: Get from settings object
107 
108  /* Only run the module if we have a VCP port and a selected USART port */
109  if (!usart_port || !vcp_port) {
110  module_enabled = false;
111  return 0;
112  }
113 
114 #ifdef MODULE_ComUsbBridge_BUILTIN
115  module_enabled = true;
116 #else
118 #endif
119 
120  return 0;
121 }
123 
124 
128 static void com2UsbBridgeTask(void *parameters)
129 {
130  /* Handle usart -> vcp direction */
131  volatile uint32_t tx_errors = 0;
132  while (1) {
133  uint32_t rx_bytes;
134 
135  uint8_t com2usb_buf[BRIDGE_BUF_LEN];
136 
137  rx_bytes = PIOS_COM_ReceiveBuffer(usart_port, com2usb_buf, BRIDGE_BUF_LEN, 500);
138  if (rx_bytes > 0) {
139  /* Bytes available to transfer */
140  if (PIOS_COM_SendBuffer(vcp_port, com2usb_buf, rx_bytes) != rx_bytes) {
141  /* Error on transmit */
142  tx_errors++;
143  }
144  }
145  }
146 }
147 
148 static void usb2ComBridgeTask(void * parameters)
149 {
150  updateSettings();
151 
152  /* Handle vcp -> usart direction */
153  volatile uint32_t tx_errors = 0;
154  while (1) {
155  uint32_t rx_bytes;
156 
157  uint8_t usb2com_buf[BRIDGE_BUF_LEN];
158 
159  rx_bytes = PIOS_COM_ReceiveBuffer(vcp_port, usb2com_buf, BRIDGE_BUF_LEN, 500);
160  if (rx_bytes > 0) {
161  /* Bytes available to transfer */
162  if (PIOS_COM_SendBuffer(usart_port, usb2com_buf, rx_bytes) != rx_bytes) {
163  /* Error on transmit */
164  tx_errors++;
165  }
166  }
167  }
168 }
169 
170 
171 static void updateSettings()
172 {
173  if (usart_port) {
174  // Retrieve settings
175  uint8_t speed;
176  ModuleSettingsComUsbBridgeSpeedGet(&speed);
177 
179  }
180 }
181 
uint16_t speed
Definition: msp_messages.h:101
static int32_t comUsbBridgeStart(void)
Definition: ComUsbBridge.c:84
static void updateSettings()
Definition: ComUsbBridge.c:171
#define PIOS_COM_VCP
Definition: pios_board.h:116
static uint32_t vcp_port
Definition: ComUsbBridge.c:74
bool PIOS_Modules_IsEnabled(enum pios_modules module)
Definition: pios_modules.c:41
static struct pios_thread * com2UsbBridgeTaskHandle
Definition: ComUsbBridge.c:70
static int32_t comUsbBridgeInitialize(void)
Definition: ComUsbBridge.c:102
static bool module_enabled
Definition: ComUsbBridge.c:76
#define BRIDGE_BUF_LEN
Definition: ComUsbBridge.c:65
void PIOS_HAL_ConfigureSerialSpeed(uintptr_t com_id, HwSharedSpeedBpsOptions speed)
Definition: pios_hal.c:1115
static struct pios_thread * usb2ComBridgeTaskHandle
Definition: ComUsbBridge.c:71
#define MODULE_INITCALL(ifn, sfn)
Definition: pios_initcall.h:67
uint16_t PIOS_COM_ReceiveBuffer(uintptr_t com_id, uint8_t *buf, uint16_t buf_len, uint32_t timeout_ms)
static void com2UsbBridgeTask(void *parameters)
Definition: ComUsbBridge.c:128
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
Definition: pios_thread.c:89
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
Definition: taskmonitor.c:67
#define TASK_PRIORITY
Definition: ComUsbBridge.c:63
#define PIOS_COM_BRIDGE
Definition: pios_board.h:115
PIOS_COM_SendBuffer(shub_global->frsky_port, shub_global->serial_buf, msg_length)
static void usb2ComBridgeTask(void *parameters)
Definition: ComUsbBridge.c:148
Includes PiOS and core architecture components.
#define STACK_SIZE_BYTES
Definition: ComUsbBridge.c:60
static uint32_t usart_port
Definition: ComUsbBridge.c:73