summaryrefslogtreecommitdiffstatshomepage
path: root/stm/usb.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm/usb.c')
-rw-r--r--stm/usb.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/stm/usb.c b/stm/usb.c
index 8f408b7b15..0fe56d3212 100644
--- a/stm/usb.c
+++ b/stm/usb.c
@@ -11,6 +11,7 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
+#include "pendsv.h"
#include "usb.h"
#ifdef USE_DEVICE_MODE
@@ -20,10 +21,12 @@ extern CDC_IF_Prop_TypeDef VCP_fops;
USB_OTG_CORE_HANDLE USB_OTG_Core;
static int dev_is_enabled = 0;
-int dev_is_connected=0; /* used by usbd_cdc_vcp */
+uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
static char rx_buf[64];
static int rx_buf_in;
static int rx_buf_out;
+static int interrupt_char = VCP_CHAR_NONE;
+mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
void pyb_usb_dev_init(void) {
#ifdef USE_DEVICE_MODE
@@ -34,7 +37,11 @@ void pyb_usb_dev_init(void) {
}
rx_buf_in = 0;
rx_buf_out = 0;
+ interrupt_char = VCP_CHAR_NONE;
dev_is_enabled = 1;
+
+ // create an exception object for interrupting by VCP
+ mp_const_vcp_interrupt = mp_obj_new_exception(qstr_from_str("VCPInterrupt"));
#endif
}
@@ -43,12 +50,27 @@ bool usb_vcp_is_enabled(void) {
}
bool usb_vcp_is_connected(void) {
- return dev_is_connected;
+ return APP_dev_is_connected;
+}
+
+void usb_vcp_set_interrupt_char(int c) {
+ if (dev_is_enabled) {
+ interrupt_char = c;
+ }
}
void usb_vcp_receive(const char *buf, uint32_t len) {
if (dev_is_enabled) {
for (int i = 0; i < len; i++) {
+
+ // catch special interrupt character
+ if (buf[i] == interrupt_char) {
+ // raise exception when interrupts are finished
+ pendsv_nlr_jump(mp_const_vcp_interrupt);
+ interrupt_char = VCP_CHAR_NONE;
+ continue;
+ }
+
rx_buf[rx_buf_in++] = buf[i];
if (rx_buf_in >= sizeof(rx_buf)) {
rx_buf_in = 0;