2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-13 20:30:37 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "usbd_core.h"
|
|
|
|
#include "usbd_desc.h"
|
2014-03-29 20:00:15 -04:00
|
|
|
#include "usbd_cdc_msc_hid.h"
|
2014-03-13 20:30:37 -04:00
|
|
|
#include "usbd_cdc_interface.h"
|
2014-03-21 19:32:01 -04:00
|
|
|
#include "usbd_msc_storage.h"
|
2014-03-13 20:30:37 -04:00
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "usb.h"
|
|
|
|
|
|
|
|
#ifdef USE_DEVICE_MODE
|
|
|
|
USBD_HandleTypeDef hUSBDDevice;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int dev_is_enabled = 0;
|
|
|
|
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
|
|
|
|
|
2014-03-29 20:00:15 -04:00
|
|
|
void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium) {
|
2014-03-13 20:30:37 -04:00
|
|
|
#ifdef USE_DEVICE_MODE
|
|
|
|
if (!dev_is_enabled) {
|
|
|
|
// only init USB once in the device's power-lifetime
|
2014-03-29 20:00:15 -04:00
|
|
|
if (mode == USB_DEVICE_MODE_CDC_MSC) {
|
|
|
|
USBD_SelectMode(USBD_MODE_CDC_MSC);
|
|
|
|
} else {
|
|
|
|
USBD_SelectMode(USBD_MODE_CDC_HID);
|
|
|
|
}
|
2014-04-12 13:50:20 -04:00
|
|
|
USBD_Init(&hUSBDDevice, (USBD_DescriptorsTypeDef*)&VCP_Desc, 0);
|
2014-03-29 20:00:15 -04:00
|
|
|
USBD_RegisterClass(&hUSBDDevice, &USBD_CDC_MSC_HID);
|
|
|
|
USBD_CDC_RegisterInterface(&hUSBDDevice, (USBD_CDC_ItfTypeDef*)&USBD_CDC_fops);
|
2014-03-30 07:30:35 -04:00
|
|
|
#if MICROPY_HW_HAS_SDCARD
|
2014-03-29 20:00:15 -04:00
|
|
|
if (medium == USB_STORAGE_MEDIUM_FLASH) {
|
|
|
|
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
|
|
|
|
} else {
|
|
|
|
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_SDCARD_STORAGE_fops);
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
2014-03-30 07:30:35 -04:00
|
|
|
#else
|
|
|
|
USBD_MSC_RegisterStorage(&hUSBDDevice, (USBD_StorageTypeDef*)&USBD_FLASH_STORAGE_fops);
|
|
|
|
#endif
|
2014-03-29 20:00:15 -04:00
|
|
|
USBD_Start(&hUSBDDevice);
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
dev_is_enabled = 1;
|
|
|
|
|
|
|
|
// create an exception object for interrupting by VCP
|
|
|
|
mp_const_vcp_interrupt = mp_obj_new_exception_msg(&mp_type_OSError, "VCPInterrupt");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-21 14:25:34 -04:00
|
|
|
void pyb_usb_dev_stop(void) {
|
|
|
|
if (dev_is_enabled) {
|
|
|
|
USBD_Stop(&hUSBDDevice);
|
|
|
|
dev_is_enabled = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 20:30:37 -04:00
|
|
|
bool usb_vcp_is_enabled(void) {
|
|
|
|
return dev_is_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_vcp_is_connected(void) {
|
2014-03-22 09:21:58 -04:00
|
|
|
return USBD_CDC_IsConnected();
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_vcp_set_interrupt_char(int c) {
|
|
|
|
if (dev_is_enabled) {
|
2014-03-15 07:52:29 -04:00
|
|
|
if (c != VCP_CHAR_NONE) {
|
|
|
|
mp_obj_exception_clear_traceback(mp_const_vcp_interrupt);
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
2014-03-15 07:52:29 -04:00
|
|
|
USBD_CDC_SetInterrupt(c, mp_const_vcp_interrupt);
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-15 08:53:51 -04:00
|
|
|
int usb_vcp_rx_num(void) {
|
|
|
|
return USBD_CDC_RxNum();
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char usb_vcp_rx_get(void) {
|
2014-03-15 07:52:29 -04:00
|
|
|
return USBD_CDC_RxGet();
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_vcp_send_strn(const char *str, int len) {
|
|
|
|
#ifdef USE_DEVICE_MODE
|
|
|
|
if (dev_is_enabled) {
|
2014-03-15 07:52:29 -04:00
|
|
|
USBD_CDC_Tx(str, len);
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_vcp_send_strn_cooked(const char *str, int len) {
|
|
|
|
#ifdef USE_DEVICE_MODE
|
2014-03-15 07:52:29 -04:00
|
|
|
if (dev_is_enabled) {
|
|
|
|
for (const char *top = str + len; str < top; str++) {
|
|
|
|
if (*str == '\n') {
|
|
|
|
USBD_CDC_Tx("\r\n", 2);
|
|
|
|
} else {
|
|
|
|
USBD_CDC_Tx(str, 1);
|
|
|
|
}
|
2014-03-13 20:30:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_hid_send_report(uint8_t *buf) {
|
|
|
|
#ifdef USE_DEVICE_MODE
|
2014-03-29 17:57:30 -04:00
|
|
|
USBD_HID_SendReport(&hUSBDDevice, buf, 4);
|
2014-03-13 20:30:37 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// code for experimental USB OTG support
|
|
|
|
|
|
|
|
#ifdef USE_HOST_MODE
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
__ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END ;
|
|
|
|
|
|
|
|
static int host_is_enabled = 0;
|
|
|
|
|
|
|
|
void pyb_usb_host_init(void) {
|
|
|
|
if (!host_is_enabled) {
|
|
|
|
// only init USBH once in the device's power-lifetime
|
|
|
|
/* Init Host Library */
|
|
|
|
USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &HID_cb, &USR_Callbacks);
|
|
|
|
}
|
|
|
|
host_is_enabled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USR_KEYBRD_Init(void) {
|
|
|
|
led_state(4, 1);
|
|
|
|
USB_OTG_BSP_mDelay(100);
|
|
|
|
led_state(4, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void USR_KEYBRD_ProcessData(uint8_t pbuf) {
|
|
|
|
led_state(4, 1);
|
|
|
|
USB_OTG_BSP_mDelay(50);
|
|
|
|
led_state(4, 0);
|
|
|
|
usb_keyboard_key = pbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HOST_MODE
|