2013-10-21 04:56:56 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-13 14:02:15 -04:00
|
|
|
#include "usb_core.h"
|
|
|
|
#include "usbd_core.h"
|
2013-10-17 17:50:21 -04:00
|
|
|
#include "usbd_cdc_core.h"
|
2013-10-13 14:02:15 -04:00
|
|
|
#include "usbd_pyb_core.h"
|
2013-10-12 19:42:20 -04:00
|
|
|
#include "usbd_usr.h"
|
|
|
|
#include "usbd_desc.h"
|
2013-10-21 04:56:56 -04:00
|
|
|
|
|
|
|
#include "misc.h"
|
2014-01-22 17:55:07 -05:00
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
2014-02-01 11:04:34 -05:00
|
|
|
#include "pendsv.h"
|
2013-10-19 10:37:09 -04:00
|
|
|
#include "usb.h"
|
2013-10-12 19:42:20 -04:00
|
|
|
|
2014-01-22 17:55:07 -05:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-17 17:50:21 -04:00
|
|
|
extern CDC_IF_Prop_TypeDef VCP_fops;
|
2014-01-22 17:55:07 -05:00
|
|
|
#endif
|
2013-10-12 19:42:20 -04:00
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
USB_OTG_CORE_HANDLE USB_OTG_Core;
|
2013-10-21 18:46:04 -04:00
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
static int dev_is_enabled = 0;
|
2014-02-01 11:17:06 -05:00
|
|
|
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
|
2013-10-21 18:46:04 -04:00
|
|
|
static char rx_buf[64];
|
|
|
|
static int rx_buf_in;
|
|
|
|
static int rx_buf_out;
|
2014-02-01 11:04:34 -05:00
|
|
|
static int interrupt_char = VCP_CHAR_NONE;
|
|
|
|
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
|
2013-10-12 19:42:20 -04:00
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
void pyb_usb_dev_init(void) {
|
2014-01-22 17:55:07 -05:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2014-01-26 12:41:01 -05:00
|
|
|
if (!dev_is_enabled) {
|
|
|
|
// only init USB once in the device's power-lifetime
|
|
|
|
USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
|
|
|
|
//USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
|
2013-10-22 18:09:25 -04:00
|
|
|
}
|
2013-10-21 04:56:56 -04:00
|
|
|
rx_buf_in = 0;
|
|
|
|
rx_buf_out = 0;
|
2014-02-01 11:04:34 -05:00
|
|
|
interrupt_char = VCP_CHAR_NONE;
|
2014-01-26 12:41:01 -05:00
|
|
|
dev_is_enabled = 1;
|
2014-02-01 11:04:34 -05:00
|
|
|
|
|
|
|
// create an exception object for interrupting by VCP
|
2014-02-15 11:10:44 -05:00
|
|
|
mp_const_vcp_interrupt = mp_obj_new_exception_msg(&mp_type_OSError, "VCPInterrupt");
|
2014-01-26 12:41:01 -05:00
|
|
|
#endif
|
2013-10-12 19:42:20 -04:00
|
|
|
}
|
|
|
|
|
2013-10-23 15:39:20 -04:00
|
|
|
bool usb_vcp_is_enabled(void) {
|
2014-01-26 12:41:01 -05:00
|
|
|
return dev_is_enabled;
|
2013-10-21 18:46:04 -04:00
|
|
|
}
|
|
|
|
|
2014-02-01 06:26:04 -05:00
|
|
|
bool usb_vcp_is_connected(void) {
|
2014-02-01 11:17:06 -05:00
|
|
|
return APP_dev_is_connected;
|
|
|
|
}
|
|
|
|
|
2014-02-01 11:04:34 -05:00
|
|
|
void usb_vcp_set_interrupt_char(int c) {
|
|
|
|
if (dev_is_enabled) {
|
|
|
|
interrupt_char = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 04:56:56 -04:00
|
|
|
void usb_vcp_receive(const char *buf, uint32_t len) {
|
2014-01-26 12:41:01 -05:00
|
|
|
if (dev_is_enabled) {
|
2013-10-21 05:42:06 -04:00
|
|
|
for (int i = 0; i < len; i++) {
|
2014-02-01 11:04:34 -05:00
|
|
|
|
|
|
|
// catch special interrupt character
|
|
|
|
if (buf[i] == interrupt_char) {
|
|
|
|
// raise exception when interrupts are finished
|
2014-02-15 11:10:44 -05:00
|
|
|
mp_obj_exception_clear_traceback(mp_const_vcp_interrupt);
|
2014-02-01 11:04:34 -05:00
|
|
|
pendsv_nlr_jump(mp_const_vcp_interrupt);
|
|
|
|
interrupt_char = VCP_CHAR_NONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-21 05:42:06 -04:00
|
|
|
rx_buf[rx_buf_in++] = buf[i];
|
|
|
|
if (rx_buf_in >= sizeof(rx_buf)) {
|
|
|
|
rx_buf_in = 0;
|
|
|
|
}
|
|
|
|
if (rx_buf_in == rx_buf_out) {
|
|
|
|
rx_buf_out = rx_buf_in + 1;
|
|
|
|
if (rx_buf_out >= sizeof(rx_buf)) {
|
|
|
|
rx_buf_out = 0;
|
|
|
|
}
|
2013-10-21 04:56:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 15:39:20 -04:00
|
|
|
int usb_vcp_rx_any(void) {
|
2013-10-21 04:56:56 -04:00
|
|
|
if (rx_buf_in >= rx_buf_out) {
|
|
|
|
return rx_buf_in - rx_buf_out;
|
|
|
|
} else {
|
|
|
|
return rx_buf_in + sizeof(rx_buf) - rx_buf_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 15:39:20 -04:00
|
|
|
char usb_vcp_rx_get(void) {
|
2013-10-21 04:56:56 -04:00
|
|
|
while (rx_buf_out == rx_buf_in) {
|
|
|
|
}
|
|
|
|
char c = rx_buf[rx_buf_out];
|
|
|
|
rx_buf_out += 1;
|
|
|
|
if (rx_buf_out >= sizeof(rx_buf)) {
|
|
|
|
rx_buf_out = 0;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_vcp_send_str(const char *str) {
|
|
|
|
usb_vcp_send_strn(str, strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_vcp_send_strn(const char *str, int len) {
|
2014-01-22 17:55:07 -05:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2014-01-26 12:41:01 -05:00
|
|
|
if (dev_is_enabled) {
|
2013-10-21 04:56:56 -04:00
|
|
|
VCP_fops.pIf_DataTx((const uint8_t*)str, len);
|
2013-10-17 17:50:21 -04:00
|
|
|
}
|
2014-01-26 12:41:01 -05:00
|
|
|
#endif
|
2013-10-12 19:42:20 -04:00
|
|
|
}
|
2013-10-22 16:13:36 -04:00
|
|
|
|
2014-01-12 19:20:06 -05:00
|
|
|
#include "usbd_conf.h"
|
2013-10-22 16:13:36 -04:00
|
|
|
|
|
|
|
/* These are external variables imported from CDC core to be used for IN
|
|
|
|
transfer management. */
|
2014-01-22 17:55:07 -05:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-22 16:13:36 -04:00
|
|
|
extern uint8_t APP_Rx_Buffer []; /* Write CDC received data in this buffer.
|
|
|
|
These data will be sent over USB IN endpoint
|
|
|
|
in the CDC core functions. */
|
|
|
|
extern uint32_t APP_Rx_ptr_in; /* Increment this pointer or roll it back to
|
|
|
|
start address when writing received data
|
|
|
|
in the buffer APP_Rx_Buffer. */
|
2014-01-22 17:55:07 -05:00
|
|
|
#endif
|
2013-10-22 16:13:36 -04:00
|
|
|
|
|
|
|
void usb_vcp_send_strn_cooked(const char *str, int len) {
|
2014-01-22 17:55:07 -05:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-22 16:13:36 -04:00
|
|
|
for (const char *top = str + len; str < top; str++) {
|
|
|
|
if (*str == '\n') {
|
|
|
|
APP_Rx_Buffer[APP_Rx_ptr_in] = '\r';
|
|
|
|
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) & (APP_RX_DATA_SIZE - 1);
|
|
|
|
}
|
|
|
|
APP_Rx_Buffer[APP_Rx_ptr_in] = *str;
|
|
|
|
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) & (APP_RX_DATA_SIZE - 1);
|
|
|
|
}
|
2014-01-22 17:55:07 -05:00
|
|
|
#endif
|
2013-10-22 16:13:36 -04:00
|
|
|
}
|
2013-10-25 15:53:54 -04:00
|
|
|
|
|
|
|
void usb_hid_send_report(uint8_t *buf) {
|
2014-01-22 17:55:07 -05:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2014-01-26 12:41:01 -05:00
|
|
|
USBD_HID_SendReport(&USB_OTG_Core, buf, 4);
|
2014-01-22 17:55:07 -05:00
|
|
|
#endif
|
2013-10-25 15:53:54 -04:00
|
|
|
}
|
2014-01-12 19:20:06 -05:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// code for experimental USB OTG support
|
|
|
|
|
|
|
|
#ifdef USE_HOST_MODE
|
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
#include "led.h"
|
|
|
|
#include "usbh_core.h"
|
|
|
|
#include "usbh_usr.h"
|
|
|
|
#include "usbh_hid_core.h"
|
|
|
|
#include "usbh_hid_keybd.h"
|
|
|
|
#include "usbh_hid_mouse.h"
|
2014-01-12 19:20:06 -05:00
|
|
|
|
|
|
|
__ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END ;
|
|
|
|
|
|
|
|
static int host_is_enabled = 0;
|
2014-01-26 12:41:01 -05:00
|
|
|
|
|
|
|
void pyb_usb_host_init(void) {
|
2014-01-12 19:20:06 -05:00
|
|
|
if (!host_is_enabled) {
|
|
|
|
// only init USBH once in the device's power-lifetime
|
|
|
|
/* Init Host Library */
|
2014-01-26 12:41:01 -05:00
|
|
|
USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &HID_cb, &USR_Callbacks);
|
2014-01-12 19:20:06 -05:00
|
|
|
}
|
|
|
|
host_is_enabled = 1;
|
|
|
|
}
|
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
void pyb_usb_host_process(void) {
|
|
|
|
USBH_Process(&USB_OTG_Core, &USB_Host);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t usb_keyboard_key = 0;
|
|
|
|
|
|
|
|
// TODO this is an ugly hack to get key presses
|
|
|
|
uint pyb_usb_host_get_keyboard(void) {
|
|
|
|
uint key = usb_keyboard_key;
|
|
|
|
usb_keyboard_key = 0;
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
void USR_MOUSE_Init(void) {
|
|
|
|
led_state(4, 1);
|
|
|
|
USB_OTG_BSP_mDelay(100);
|
|
|
|
led_state(4, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USR_MOUSE_ProcessData(HID_MOUSE_Data_TypeDef *data) {
|
|
|
|
led_state(4, 1);
|
|
|
|
USB_OTG_BSP_mDelay(50);
|
|
|
|
led_state(4, 0);
|
2014-01-22 17:55:07 -05:00
|
|
|
}
|
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
void USR_KEYBRD_Init(void) {
|
|
|
|
led_state(4, 1);
|
|
|
|
USB_OTG_BSP_mDelay(100);
|
|
|
|
led_state(4, 0);
|
2014-01-12 19:20:06 -05:00
|
|
|
}
|
|
|
|
|
2014-01-26 12:41:01 -05:00
|
|
|
void USR_KEYBRD_ProcessData(uint8_t pbuf) {
|
|
|
|
led_state(4, 1);
|
|
|
|
USB_OTG_BSP_mDelay(50);
|
|
|
|
led_state(4, 0);
|
|
|
|
//lcd_print_strn((char*)&pbuf, 1);
|
|
|
|
usb_keyboard_key = pbuf;
|
2014-01-23 17:16:15 -05:00
|
|
|
}
|
|
|
|
|
2014-01-12 19:20:06 -05:00
|
|
|
#endif // USE_HOST_MODE
|