2013-10-21 09:56:56 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-13 19:02:15 +01:00
|
|
|
#include "usb_core.h"
|
|
|
|
#include "usbd_core.h"
|
2013-10-17 22:50:21 +01:00
|
|
|
#include "usbd_cdc_core.h"
|
2013-10-13 19:02:15 +01:00
|
|
|
#include "usbd_pyb_core.h"
|
2013-10-13 00:42:20 +01:00
|
|
|
#include "usbd_usr.h"
|
|
|
|
#include "usbd_desc.h"
|
2013-10-21 09:56:56 +01:00
|
|
|
|
|
|
|
#include "misc.h"
|
2014-01-22 22:55:07 +00:00
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
2013-10-19 15:37:09 +01:00
|
|
|
#include "usb.h"
|
2013-10-13 00:42:20 +01:00
|
|
|
|
2014-01-22 22:55:07 +00:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-17 22:50:21 +01:00
|
|
|
extern CDC_IF_Prop_TypeDef VCP_fops;
|
2014-01-22 22:55:07 +00:00
|
|
|
#endif
|
2013-10-13 00:42:20 +01:00
|
|
|
|
|
|
|
USB_OTG_CORE_HANDLE USB_OTG_dev;
|
2013-10-21 23:46:04 +01:00
|
|
|
|
|
|
|
static int is_enabled = 0;
|
|
|
|
static char rx_buf[64];
|
|
|
|
static int rx_buf_in;
|
|
|
|
static int rx_buf_out;
|
2013-10-13 00:42:20 +01:00
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
void usb_init(void) {
|
2013-10-22 23:09:25 +01:00
|
|
|
if (!is_enabled) {
|
|
|
|
// only init USB once in the device's power-lifetime
|
2014-01-22 22:55:07 +00:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-22 23:09:25 +01:00
|
|
|
USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb);
|
2013-10-25 20:53:54 +01:00
|
|
|
//USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb);
|
2014-01-22 22:55:07 +00:00
|
|
|
#endif
|
2013-10-22 23:09:25 +01:00
|
|
|
}
|
2013-10-21 09:56:56 +01:00
|
|
|
rx_buf_in = 0;
|
|
|
|
rx_buf_out = 0;
|
2013-10-17 22:50:21 +01:00
|
|
|
is_enabled = 1;
|
2013-10-13 00:42:20 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
bool usb_vcp_is_enabled(void) {
|
2013-10-21 23:46:04 +01:00
|
|
|
return is_enabled;
|
|
|
|
}
|
|
|
|
|
2013-10-21 09:56:56 +01:00
|
|
|
void usb_vcp_receive(const char *buf, uint32_t len) {
|
2013-10-21 10:42:06 +01:00
|
|
|
if (is_enabled) {
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
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 09:56:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
int usb_vcp_rx_any(void) {
|
2013-10-21 09:56:56 +01: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 20:39:20 +01:00
|
|
|
char usb_vcp_rx_get(void) {
|
2013-10-21 09:56:56 +01: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) {
|
2013-10-17 22:50:21 +01:00
|
|
|
if (is_enabled) {
|
2014-01-22 22:55:07 +00:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-21 09:56:56 +01:00
|
|
|
VCP_fops.pIf_DataTx((const uint8_t*)str, len);
|
2014-01-22 22:55:07 +00:00
|
|
|
#endif
|
2013-10-17 22:50:21 +01:00
|
|
|
}
|
2013-10-13 00:42:20 +01:00
|
|
|
}
|
2013-10-22 21:13:36 +01:00
|
|
|
|
2014-01-13 00:20:06 +00:00
|
|
|
#include "usbd_conf.h"
|
2013-10-22 21:13:36 +01:00
|
|
|
|
|
|
|
/* These are external variables imported from CDC core to be used for IN
|
|
|
|
transfer management. */
|
2014-01-22 22:55:07 +00:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-22 21:13:36 +01: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 22:55:07 +00:00
|
|
|
#endif
|
2013-10-22 21:13:36 +01:00
|
|
|
|
|
|
|
void usb_vcp_send_strn_cooked(const char *str, int len) {
|
2014-01-22 22:55:07 +00:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-22 21:13:36 +01: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 22:55:07 +00:00
|
|
|
#endif
|
2013-10-22 21:13:36 +01:00
|
|
|
}
|
2013-10-25 20:53:54 +01:00
|
|
|
|
|
|
|
void usb_hid_send_report(uint8_t *buf) {
|
2014-01-22 22:55:07 +00:00
|
|
|
#ifdef USE_DEVICE_MODE
|
2013-10-25 20:53:54 +01:00
|
|
|
USBD_HID_SendReport(&USB_OTG_dev, buf, 4);
|
2014-01-22 22:55:07 +00:00
|
|
|
#endif
|
2013-10-25 20:53:54 +01:00
|
|
|
}
|
2014-01-13 00:20:06 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// code for experimental USB OTG support
|
|
|
|
|
|
|
|
#ifdef USE_HOST_MODE
|
|
|
|
|
|
|
|
#include "lib-otg/usbh_core.h"
|
|
|
|
#include "lib-otg/usbh_usr.h"
|
|
|
|
#include "lib-otg/usbh_hid_core.h"
|
2014-01-22 22:55:07 +00:00
|
|
|
#include "lib-otg/usb_hcd_int.h"
|
2014-01-13 00:20:06 +00:00
|
|
|
|
|
|
|
__ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END ;
|
|
|
|
|
|
|
|
static int host_is_enabled = 0;
|
|
|
|
void pyb_usbh_init(void) {
|
|
|
|
if (!host_is_enabled) {
|
|
|
|
// only init USBH once in the device's power-lifetime
|
|
|
|
/* Init Host Library */
|
|
|
|
USBH_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USB_Host, &HID_cb, &USR_Callbacks);
|
|
|
|
}
|
|
|
|
host_is_enabled = 1;
|
|
|
|
}
|
|
|
|
|
2014-01-22 22:55:07 +00:00
|
|
|
mp_obj_t pyb_usbh_process(void) {
|
2014-01-13 00:20:06 +00:00
|
|
|
USBH_Process(&USB_OTG_dev, &USB_Host);
|
2014-01-22 22:55:07 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t pyb_usbh_connect(void) {
|
|
|
|
USBH_HCD_INT_fops->DevConnected(&USB_OTG_dev);
|
|
|
|
return mp_const_none;
|
2014-01-13 00:20:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 22:16:15 +00:00
|
|
|
mp_obj_t pyb_usbh_info(void) {
|
|
|
|
printf("GOTGCTL:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GOTGCTL);
|
|
|
|
printf("GOTGINT:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GOTGINT);
|
|
|
|
printf("GAHBCFG:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GAHBCFG);
|
|
|
|
printf("GUSBCFG:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GUSBCFG);
|
|
|
|
printf("GRSTCTL:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GRSTCTL);
|
|
|
|
printf("GINTSTS:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GINTSTS);
|
|
|
|
printf("GINTMSK:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GINTMSK);
|
|
|
|
//printf("GRXSTSR:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GRXSTSR);
|
|
|
|
//printf("GRXSTSP:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GRXSTSP);
|
|
|
|
//printf("GRXFSIZ:%08x\n", (unsigned int)USB_OTG_dev.regs.GREGS->GRXFSIZ);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2014-01-13 00:20:06 +00:00
|
|
|
#endif // USE_HOST_MODE
|