diff options
-rw-r--r-- | py/mpz.c | 14 | ||||
-rw-r--r-- | py/mpz.h | 6 | ||||
-rw-r--r-- | stm/main.c | 2 | ||||
-rw-r--r-- | stm/printf.c | 16 | ||||
-rw-r--r-- | stm/stmusbd/usbd_pyb_core.c | 31 | ||||
-rw-r--r-- | stm/usb.c | 13 | ||||
-rw-r--r-- | stm/usb.h | 5 |
7 files changed, 67 insertions, 20 deletions
@@ -113,7 +113,7 @@ uint mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, uint jlen, const mpz_dig_t borrow >>= DIG_SIZE; } - for (; jlen > 0; --jlen, ++idig, ++kdig) { + for (; jlen > 0; --jlen, ++idig, ++jdig) { borrow += *jdig; *idig = borrow & DIG_MASK; borrow >>= DIG_SIZE; @@ -315,7 +315,7 @@ void mpn_div(mpz_dig_t *num_dig, machine_uint_t *num_len, mpz_dig_t *den_dig, ma #define MIN_ALLOC (4) #define ALIGN_ALLOC (2) -#define NUM_DIG_FOR_INT (sizeof(int) * 8 / DIG_SIZE + 1) +#define NUM_DIG_FOR_INT (sizeof(machine_int_t) * 8 / DIG_SIZE + 1) static const uint log_base2_floor[] = { 0, @@ -329,7 +329,7 @@ static const uint log_base2_floor[] = { 4, 4, 4, 5 }; -bool mpz_int_is_sml_int(int i) { +bool mpz_int_is_sml_int(machine_int_t i) { return -(1 << DIG_SIZE) < i && i < (1 << DIG_SIZE); } @@ -497,7 +497,7 @@ int mpz_cmp(const mpz_t *z1, const mpz_t *z2) { return cmp; } -int mpz_cmp_sml_int(const mpz_t *z, int sml_int) { +int mpz_cmp_sml_int(const mpz_t *z, machine_int_t sml_int) { int cmp; if (z->neg == 0) { if (sml_int < 0) return 1; @@ -885,13 +885,13 @@ mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) { } #endif -int mpz_as_int(const mpz_t *i) { - int val = 0; +machine_int_t mpz_as_int(const mpz_t *i) { + machine_int_t val = 0; mpz_dig_t *d = i->dig + i->len; while (--d >= i->dig) { - int oldval = val; + machine_int_t oldval = val; val = (val << DIG_SIZE) | *d; if (val < oldval) { @@ -11,7 +11,7 @@ typedef struct _mpz_t { mpz_dig_t *dig; } mpz_t; -bool mpz_int_is_sml_int(int i); +bool mpz_int_is_sml_int(machine_int_t i); void mpz_init_zero(mpz_t *z); void mpz_init_from_int(mpz_t *z, machine_int_t val); @@ -35,7 +35,7 @@ bool mpz_is_odd(const mpz_t *z); bool mpz_is_even(const mpz_t *z); int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs); -int mpz_cmp_sml_int(const mpz_t *lhs, int sml_int); +int mpz_cmp_sml_int(const mpz_t *lhs, machine_int_t sml_int); mpz_t *mpz_abs(const mpz_t *z); mpz_t *mpz_neg(const mpz_t *z); @@ -58,7 +58,7 @@ void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const m mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs); mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs); -int mpz_as_int(const mpz_t *z); +machine_int_t mpz_as_int(const mpz_t *z); machine_float_t mpz_as_float(const mpz_t *z); uint mpz_as_str_size(const mpz_t *z, uint base); char *mpz_as_str(const mpz_t *z, uint base); diff --git a/stm/main.c b/stm/main.c index 86a6570dd8..28e0e5065d 100644 --- a/stm/main.c +++ b/stm/main.c @@ -580,7 +580,7 @@ soft_reset: pyb_usb_host_init(); #elif defined(USE_DEVICE_MODE) // USB device - pyb_usb_dev_init(); + pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC); #endif // run main script diff --git a/stm/printf.c b/stm/printf.c index 7bac07ab72..cfe4204b61 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -219,9 +219,19 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { mp_float_t d = va_arg(args, double); int left = (int)d; int right = (int)((d - (mp_float_t)(int)d) * 1000000.0); - chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); - chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); - chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + if (right < 0) { + if (left == 0) { + chrs += pfenv_print_strn(pfenv, "-0", 2, flags, width); + } else { + chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); + } + chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); + chrs += pfenv_print_int(pfenv, -right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + } else { + chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); + chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); + chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + } break; } default: diff --git a/stm/stmusbd/usbd_pyb_core.c b/stm/stmusbd/usbd_pyb_core.c index d887b89c6c..5fcf9bb182 100644 --- a/stm/stmusbd/usbd_pyb_core.c +++ b/stm/stmusbd/usbd_pyb_core.c @@ -73,6 +73,11 @@ #include "usbd_msc_bot.h"
#include "usbd_msc_mem.h"
+// CDC VCP is the first interface
+// the option below is supposed to select between MSC (1) and HID (0) for the
+// second USB interface, but it does not work (only VCP+MSC works)
+// to use HID on its own (ie no VCP), the functions in usbd_pyb_core2.c can be
+// used, and selected in the call to pyb_usb_dev_init().
#define USB_PYB_USE_MSC (1)
#if USB_PYB_USE_MSC
@@ -314,7 +319,7 @@ __ALIGN_BEGIN static uint8_t usbd_pyb_CfgDesc[USB_PYB_CONFIG_DESC_SIZ] __ALIGN_E 0x01, // bNumEndpoints
0x03, // bInterfaceClass: HID Class
0x01, // bInterfaceSubClass: 0=no boot, 1=BOOT
- 0x01, // nInterfaceProtocol: 0=none, 1=keyboard, 2=mouse
+ 0x02, // nInterfaceProtocol: 0=none, 1=keyboard, 2=mouse
0x00, // iInterface:
// Descriptor of Joystick Mouse HID
@@ -340,7 +345,7 @@ __ALIGN_BEGIN static uint8_t usbd_pyb_CfgDesc[USB_PYB_CONFIG_DESC_SIZ] __ALIGN_E #endif
};
-#if 0
+#if !USB_PYB_USE_MSC
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
{
0x05, 0x01,
@@ -559,6 +564,28 @@ static uint8_t usbd_pyb_Setup(void *pdev, USB_SETUP_REQ *req) { // Standard Interface Request ------------------------------------------
case (USB_REQ_TYPE_STANDARD | USB_REQ_RECIPIENT_INTERFACE):
switch (req->bRequest) {
+ case USB_REQ_GET_DESCRIPTOR: // needed for HID; SU 0x81 0x06 0x2200 0x00 request
+ // wIndex & 0xff is the interface
+ if ((req->wIndex & 0xff) <= 1) {
+ // CDC VCP
+ } else {
+#if USB_PYB_USE_MSC
+#else
+ uint16_t len = 0;
+ uint8_t *pbuf = NULL;
+ if (req->wValue >> 8 == HID_REPORT_DESC) {
+ len = MIN(HID_MOUSE_REPORT_DESC_SIZE , req->wLength);
+ pbuf = HID_MOUSE_ReportDesc;
+ return USBD_CtlSendData(pdev, pbuf, len);
+ } else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE) {
+ pbuf = usbd_pyb_CfgDesc + /* skip VCP */ 0x09 + 0x08 + 0x09 + 0x05 + 0x05 + 0x04 + 0x05 + 0x07 + 0x09 + 0x07 + 0x07 + /* skip iface descr */ 0x09;
+ len = MIN(0x09, req->wLength);
+ return USBD_CtlSendData(pdev, pbuf, len);
+ }
+#endif
+ }
+ break;
+
case USB_REQ_GET_INTERFACE:
// wIndex & 0xff is the interface
if ((req->wIndex & 0xff) <= 1) {
@@ -28,12 +28,19 @@ 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) { +void pyb_usb_dev_init(int usb_dev_type) { #ifdef USE_DEVICE_MODE 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); + switch (usb_dev_type) { + case PYB_USB_DEV_VCP_MSC: + USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb); + break; + + case PYB_USB_DEV_HID: + USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb); + break; + } } rx_buf_in = 0; rx_buf_out = 0; @@ -4,7 +4,10 @@ #define VCP_CHAR_CTRL_C (3) #define VCP_CHAR_CTRL_D (4) -void pyb_usb_dev_init(void); +#define PYB_USB_DEV_VCP_MSC (0) +#define PYB_USB_DEV_HID (1) + +void pyb_usb_dev_init(int usb_dev_type); bool usb_vcp_is_enabled(void); bool usb_vcp_is_connected(void); void usb_vcp_set_interrupt_char(int c); |