vcp.cpp
Go to the documentation of this file.
1 #include "vcp.h"
2 
3 #define USB_TIMEOUT 50
4 
5 static VCP* vcpPtr = nullptr;
6 
7 void vcp_rx_callback(uint8_t byte)
8 {
9  if (vcpPtr->cb_)
10  vcpPtr->cb_(byte);
11 }
12 
13 void vcp_ls_callback(void* context, uint16_t ctrlLineState)
14 {
15  (void)context;
16  (void)ctrlLineState;
17  if (ctrlLineState != 3)
18  {
19  volatile int debug = 1;
20  (void)debug;
21  }
22 }
23 
24 void VCP::init()
25 {
26  // Initialize the GPIOs for the pins
30 
32  connected_ = false;
34  vcpPtr = this;
35 
37 }
38 
39 void VCP::write(const uint8_t* ch, uint8_t len)
40 {
41  uint32_t start = millis();
42 
43  // Don't send bytes to a disconnected USB
44  if (connected() && tx_bytes_free() > 0)
45  {
46  while (len > 0)
47  {
48  uint32_t num_bytes_sent = CDC_Send_DATA(ch, len);
49  len -= num_bytes_sent;
50  ch += num_bytes_sent;
51 
52  if (millis() > start + USB_TIMEOUT)
53  break;
54  }
55  }
57 }
58 
60 {
61  // detect if we are connected to a computer
62  if (rx_bytes_waiting())
63  connected_ = true;
64 
65  // If we were receiving bytes, but now we're not connected
66  // reset the VCP
67  if (!connected() && connected_)
68  {
70  connected_ = false;
71  }
72 }
73 
75 {
77 }
78 
80 {
81  return CDC_Send_FreeBytes();
82 }
83 
84 uint8_t VCP::read_byte()
85 {
86  uint8_t data;
87 
88  if (CDC_Receive_DATA(&data, 1))
89  {
90  connected_ = true;
91  return data;
92  }
93  else
94  return 0;
95 }
96 
98 {
99  return CDC_Send_FreeBytes() > 0;
100 }
101 
102 void VCP::put_byte(uint8_t ch)
103 {
104  if (connected())
105  CDC_Send_DATA(&ch, 1);
106 }
107 
109 {
110  return (vbus_sens_.read() == GPIO::HIGH);
111 }
112 
114 {
115  // CDC_flush();
116  return true;
117 }
119 void VCP::end_write() {}
120 
121 void VCP::register_rx_callback(void (*rx_callback_ptr)(uint8_t data))
122 {
123  cb_ = rx_callback_ptr;
124  // Register_CDC_RxCallback(&vcp_rx_callback);
125 }
127 {
128  receive_CB_ = NULL;
129  // Register_CDC_RxCallback(NULL);
130 }
131 
133 {
134  return false;
135 }
136 
138 {
141  delay(200);
144 }
void vcp_ls_callback(void *context, uint16_t ctrlLineState)
Definition: vcp.cpp:13
USBD_Class_cb_TypeDef USBD_CDC_cb
uint32_t CDC_Send_FreeBytes(void)
Definition: usbd_cdc_vcp.c:187
ROSCPP_DECL void start()
static VCP * vcpPtr
Definition: vcp.cpp:5
#define GPIOA
Definition: stm32f4xx.h:2110
volatile uint32_t millis(void)
Definition: system.c:50
#define USB_TIMEOUT
Definition: vcp.cpp:3
bool connected()
Definition: vcp.cpp:108
std::function< void(uint8_t)> cb_
Definition: vcp.h:38
void set_mode(gpio_mode_t mode)
Definition: gpio.cpp:82
GPIO tx_pin_
Definition: vcp.h:52
void begin_write()
Definition: vcp.cpp:118
bool flush() override
Definition: vcp.cpp:113
std::function< void(uint8_t)> receive_CB_
Definition: serial.h:77
void init()
Definition: vcp.cpp:24
#define GPIOC
Definition: stm32f4xx.h:2112
void USBD_Init(USB_OTG_CORE_HANDLE *pdev, USB_OTG_CORE_ID_TypeDef coreID, USBD_DEVICE *pDevice, USBD_Class_cb_TypeDef *class_cb, USBD_Usr_cb_TypeDef *usr_cb)
USBD_Init Initializes the device stack and load the class driver.
Definition: usbd_core.c:137
bool read()
Definition: gpio.cpp:63
USBD_Usr_cb_TypeDef USR_cb
Definition: usbd_usr.c:25
void register_rx_callback(void(*rx_callback_ptr)(uint8_t data)) override
Definition: vcp.cpp:121
void init(GPIO_TypeDef *BasePort, uint16_t pin, gpio_mode_t mode)
Definition: gpio.cpp:34
GPIO vbus_sens_
Definition: vcp.h:53
void CDC_SetCtrlLineStateCb(void(*cb)(void *context, uint16_t ctrlLineState), void *context)
Definition: usbd_cdc_vcp.c:338
void perform_maintenance()
Definition: vcp.cpp:59
GPIO rx_pin_
Definition: vcp.h:51
void put_byte(uint8_t ch) override
Definition: vcp.cpp:102
uint32_t tx_bytes_free() override
Definition: vcp.cpp:79
virtual void write(const uint8_t *ch, uint8_t len) override
Definition: vcp.cpp:39
uint32_t CDC_Receive_DATA(uint8_t *recvBuf, uint32_t len)
Definition: usbd_cdc_vcp.c:235
bool connected_
Definition: vcp.h:39
Definition: vcp.h:19
uint32_t CDC_Receive_BytesAvailable(void)
Definition: usbd_cdc_vcp.c:247
void write(gpio_write_t state)
Definition: gpio.cpp:41
void end_write()
Definition: vcp.cpp:119
USBD_DEVICE USR_desc
Definition: usbd_desc.c:103
void unregister_rx_callback() override
Definition: vcp.cpp:126
Definition: gpio.h:43
#define NULL
Definition: usbd_def.h:50
uint32_t CDC_Send_DATA(const uint8_t *ptrBuffer, uint32_t sendLength)
Definition: usbd_cdc_vcp.c:181
uint32_t rx_bytes_waiting() override
Definition: vcp.cpp:74
USB_OTG_CORE_HANDLE USB_OTG_dev
Definition: usbd_cdc_vcp.c:34
bool in_bulk_mode()
Definition: vcp.cpp:132
void send_disconnect_signal()
Definition: vcp.cpp:137
bool tx_buffer_empty() override
Definition: vcp.cpp:97
void vcp_rx_callback(uint8_t byte)
Definition: vcp.cpp:7
uint8_t read_byte() override
Definition: vcp.cpp:84
void delay(uint32_t ms)
Definition: system.c:101


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Sat May 9 2020 03:16:54