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 
74 
76 {
78 }
79 
80 
82 {
83  return CDC_Send_FreeBytes();
84 }
85 
86 
87 uint8_t VCP::read_byte()
88 {
89  uint8_t data;
90 
91  if (CDC_Receive_DATA(&data, 1))
92  {
93  connected_ = true;
94  return data;
95  }
96  else
97  return 0;
98 }
99 
101 {
102  return CDC_Send_FreeBytes() > 0;
103 }
104 
105 
106 void VCP::put_byte(uint8_t ch)
107 {
108  if (connected())
109  CDC_Send_DATA(&ch, 1);
110 }
111 
113 {
114  return (vbus_sens_.read() == GPIO::HIGH);
115 }
116 
118 {
119 // CDC_flush();
120  return true;
121 }
123 void VCP::end_write() {}
124 
125 
126 void VCP::register_rx_callback(void (*rx_callback_ptr)(uint8_t data))
127 {
128  cb_ = rx_callback_ptr;
129 // Register_CDC_RxCallback(&vcp_rx_callback);
130 }
132 {
133  receive_CB_ = NULL;
134 // Register_CDC_RxCallback(NULL);
135 }
136 
137 
139 {
140  return false;
141 }
142 
143 
145 {
148  delay(200);
151 }
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:112
std::function< void(uint8_t)> cb_
Definition: vcp.h:39
void set_mode(gpio_mode_t mode)
Definition: gpio.cpp:82
GPIO tx_pin_
Definition: vcp.h:54
void begin_write()
Definition: vcp.cpp:122
bool flush() override
Definition: vcp.cpp:117
std::function< void(uint8_t)> receive_CB_
Definition: serial.h:76
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:126
void init(GPIO_TypeDef *BasePort, uint16_t pin, gpio_mode_t mode)
Definition: gpio.cpp:34
GPIO vbus_sens_
Definition: vcp.h:55
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:53
void put_byte(uint8_t ch) override
Definition: vcp.cpp:106
uint32_t tx_bytes_free() override
Definition: vcp.cpp:81
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:40
Definition: vcp.h:20
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:123
USBD_DEVICE USR_desc
Definition: usbd_desc.c:103
void unregister_rx_callback() override
Definition: vcp.cpp:131
Definition: gpio.h:44
#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:75
USB_OTG_CORE_HANDLE USB_OTG_dev
Definition: usbd_cdc_vcp.c:34
bool in_bulk_mode()
Definition: vcp.cpp:138
void send_disconnect_signal()
Definition: vcp.cpp:144
bool tx_buffer_empty() override
Definition: vcp.cpp:100
void vcp_rx_callback(uint8_t byte)
Definition: vcp.cpp:7
uint8_t read_byte() override
Definition: vcp.cpp:87
void delay(uint32_t ms)
Definition: system.c:98


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:26