From 435ac7081aa95b32052e5e9a186c578b374d3f55 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 28 Mar 2017 13:04:54 +0200 Subject: [PATCH] loader: add usb --- Makefile.bootloader | 1 + Makefile.loader | 7 ++++ micropython/bootloader/main.c | 14 ++++++- micropython/firmware/main.c | 22 +++++------ micropython/loader/main.c | 58 +++++++++++++++++++++++++--- micropython/trezorhal/flash.c | 21 +--------- micropython/trezorhal/usb.c | 4 -- micropython/trezorhal/usb.h | 6 ++- micropython/trezorhal/usb_hid-impl.h | 4 +- 9 files changed, 93 insertions(+), 44 deletions(-) diff --git a/Makefile.bootloader b/Makefile.bootloader index 393e24644..13514b370 100644 --- a/Makefile.bootloader +++ b/Makefile.bootloader @@ -61,6 +61,7 @@ OBJ_FW += $(addprefix $(BUILD_FW)/, \ extmod/modtrezorui/font_bitmap.o \ trezorhal/common.o \ trezorhal/image.o \ + trezorhal/flash.o \ trezorhal/sdcard.o \ trezorhal/stm32_it.o \ trezorhal/stm32_system.o \ diff --git a/Makefile.loader b/Makefile.loader index bd8de0925..13c1e5f06 100644 --- a/Makefile.loader +++ b/Makefile.loader @@ -52,6 +52,10 @@ OBJ_HAL += $(addprefix $(BUILD_MP)/,\ stmhal/hal/f4/src/stm32f4xx_hal.o \ stmhal/hal/f4/src/stm32f4xx_ll_sdmmc.o \ stmhal/hal/f4/src/stm32f4xx_ll_usb.o \ + \ + stmhal/usbdev/core/src/usbd_core.o \ + stmhal/usbdev/core/src/usbd_ctlreq.o \ + stmhal/usbdev/core/src/usbd_ioreq.o \ ) # OBJ micropython/ @@ -66,7 +70,10 @@ OBJ_FW += $(addprefix $(BUILD_FW)/, \ extmod/modtrezorui/font_robotomono_regular_20.o \ trezorhal/common.o \ trezorhal/image.o \ + trezorhal/flash.o \ trezorhal/touch.o \ + trezorhal/usb.o \ + trezorhal/usbd_conf.o \ trezorhal/stm32_it.o \ trezorhal/stm32_system.o \ trezorhal/hal/stm32f4xx_hal_sram.o \ diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index 4942bf47f..d2107d760 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -5,6 +5,7 @@ #include "common.h" #include "display.h" #include "image.h" +#include "flash.h" #include "sdcard.h" #include "version.h" @@ -144,9 +145,18 @@ int main(void) SCB->VTOR = BOOTLOADER_START; periph_init(); - sdcard_init(); + if (0 != display_init()) { + __fatal_error("display_init failed"); + } + + if (0 != flash_init()) { + __fatal_error("flash_init failed"); + } + + if (0 != sdcard_init()) { + __fatal_error("sdcard_init failed"); + } - display_init(); display_clear(); display_backlight(255); diff --git a/micropython/firmware/main.c b/micropython/firmware/main.c index 5e19cd8b3..93f1316e1 100644 --- a/micropython/firmware/main.c +++ b/micropython/firmware/main.c @@ -27,31 +27,31 @@ int usb_init_all(void) { .vendor_id = 0x1209, .product_id = 0x53C1, .release_num = 0x0002, - .product_str = (uint8_t *)"product_str", - .manufacturer_str = (uint8_t *)"manufacturer_str", - .serial_number_str = (uint8_t *)"serial_number_str", - .configuration_str = (uint8_t *)"configuration_str", - .interface_str = (uint8_t *)"interface_str", + .manufacturer_str = (const uint8_t *)"manufacturer_str", + .product_str = (const uint8_t *)"product_str", + .serial_number_str = (const uint8_t *)"serial_number_str", + .configuration_str = (const uint8_t *)"configuration_str", + .interface_str = (const uint8_t *)"interface_str", }; static uint8_t hid_rx_buffer[64]; static const usb_hid_info_t hid_info = { .iface_num = 0x00, - .ep_in = 0x81, - .ep_out = 0x01, + .ep_in = USB_EP_DIR_IN | 0x01, + .ep_out = USB_EP_DIR_OUT | 0x01, .subclass = 0, .protocol = 0, .rx_buffer = hid_rx_buffer, .max_packet_len = sizeof(hid_rx_buffer), .polling_interval = 1, .report_desc_len = 34, - .report_desc = (uint8_t*)"\x06\x00\xff\x09\x01\xa1\x01\x09\x20\x15\x00\x26\xff\x00\x75\x08\x95\x40\x81\x02\x09\x21\x15\x00\x26\xff\x00\x75\x08\x95\x40\x91\x02\xc0", + .report_desc = (const uint8_t *)"\x06\x00\xff\x09\x01\xa1\x01\x09\x20\x15\x00\x26\xff\x00\x75\x08\x95\x40\x81\x02\x09\x21\x15\x00\x26\xff\x00\x75\x08\x95\x40\x91\x02\xc0", }; static const usb_vcp_info_t vcp_info = { .iface_num = 0x01, .data_iface_num = 0x02, - .ep_cmd = 0x82, - .ep_in = 0x83, - .ep_out = 0x03, + .ep_cmd = USB_EP_DIR_IN | 0x02, + .ep_in = USB_EP_DIR_IN | 0x03, + .ep_out = USB_EP_DIR_OUT | 0x03, .polling_interval = 1, .max_cmd_packet_len = 8, .max_data_packet_len = 64, diff --git a/micropython/loader/main.c b/micropython/loader/main.c index 803372b8d..cb5a22a85 100644 --- a/micropython/loader/main.c +++ b/micropython/loader/main.c @@ -5,7 +5,9 @@ #include "common.h" #include "display.h" #include "image.h" +#include "flash.h" #include "touch.h" +#include "usb.h" #include "version.h" #define IMAGE_MAGIC 0x465A5254 // TRZF @@ -81,9 +83,42 @@ void check_and_jump(void) } } -void mainloop(void) -{ - __fatal_error("touch detected - launch aborted"); +int usb_init_all(void) { + static const usb_dev_info_t dev_info = { + .vendor_id = 0x1209, + .product_id = 0x53C0, + .release_num = 0x0002, + .manufacturer_str = (const uint8_t *)"manufacturer_str", + .product_str = (const uint8_t *)"product_str", + .serial_number_str = (const uint8_t *)"serial_number_str", + .configuration_str = (const uint8_t *)"configuration_str", + .interface_str = (const uint8_t *)"interface_str", + }; + static uint8_t hid_rx_buffer[64]; + static const usb_hid_info_t hid_info = { + .iface_num = 0x00, + .ep_in = USB_EP_DIR_IN | 0x01, + .ep_out = USB_EP_DIR_OUT | 0x01, + .subclass = 0, + .protocol = 0, + .rx_buffer = hid_rx_buffer, + .max_packet_len = sizeof(hid_rx_buffer), + .polling_interval = 1, + .report_desc_len = 34, + .report_desc = (const uint8_t *)"\x06\x00\xff\x09\x01\xa1\x01\x09\x20\x15\x00\x26\xff\x00\x75\x08\x95\x40\x81\x02\x09\x21\x15\x00\x26\xff\x00\x75\x08\x95\x40\x91\x02\xc0", + }; + + if (0 != usb_init(&dev_info)) { + __fatal_error("usb_init failed"); + } + if (0 != usb_hid_add(&hid_info)) { + __fatal_error("usb_hid_add failed"); + } + if (0 != usb_start()) { + __fatal_error("usb_start failed"); + } + + return 0; } int main(void) @@ -91,9 +126,22 @@ int main(void) SCB->VTOR = LOADER_START + HEADER_SIZE; periph_init(); - touch_init(); + if (0 != display_init()) { + __fatal_error("display_init failed"); + } + + if (0 != flash_init()) { + __fatal_error("flash_init failed"); + } + + if (0 != touch_init()) { + __fatal_error("touch_init failed"); + } + + if (0 != usb_init_all()) { + __fatal_error("usb_init_all failed"); + } - display_init(); display_clear(); display_backlight(255); diff --git a/micropython/trezorhal/flash.c b/micropython/trezorhal/flash.c index 922eead4d..0ce0a2aeb 100644 --- a/micropython/trezorhal/flash.c +++ b/micropython/trezorhal/flash.c @@ -1,27 +1,10 @@ #include STM32_HAL_H -int flash_init(void) { - // Enable the flash IRQ, which is used to also call our storage IRQ handler - // It needs to go at a higher priority than all those components that rely on - // the flash storage (eg higher than USB MSC). - HAL_NVIC_SetPriority(FLASH_IRQn, 2, 0); - HAL_NVIC_EnableIRQ(FLASH_IRQn); - +int flash_init(void) +{ return 0; } -void FLASH_IRQHandler(void) { - // This calls the real flash IRQ handler, if needed - /* - uint32_t flash_cr = FLASH->CR; - if ((flash_cr & FLASH_IT_EOP) || (flash_cr & FLASH_IT_ERR)) { - HAL_FLASH_IRQHandler(); - } - */ - // This call the storage IRQ handler, to check if the flash cache needs flushing - // storage_irq_handler(); -} - #define WANTED_WRP (OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1) #define WANTED_RDP (OB_RDP_LEVEL_2) #define WANTED_BOR (OB_BOR_LEVEL3) diff --git a/micropython/trezorhal/usb.c b/micropython/trezorhal/usb.c index 691a55391..03e663029 100644 --- a/micropython/trezorhal/usb.c +++ b/micropython/trezorhal/usb.c @@ -10,10 +10,6 @@ #define USB_MAX_CONFIG_DESC_SIZE 128 #define USB_MAX_STR_DESC_SIZE 256 -#define USB_EP_DIR_OUT 0x00 -#define USB_EP_DIR_IN 0x80 -#define USB_EP_DIR_MSK 0x80 - extern PCD_HandleTypeDef pcd_fs_handle; static USBD_HandleTypeDef usb_dev_handle; diff --git a/micropython/trezorhal/usb.h b/micropython/trezorhal/usb.h index b8193f687..938346092 100644 --- a/micropython/trezorhal/usb.h +++ b/micropython/trezorhal/usb.h @@ -1,6 +1,10 @@ #ifndef __TREZORHAL_USB_H__ #define __TREZORHAL_USB_H__ +#define USB_EP_DIR_OUT 0x00 +#define USB_EP_DIR_IN 0x80 +#define USB_EP_DIR_MSK 0x80 + typedef struct __attribute__((packed)) { uint8_t bLength; uint8_t bDescriptorType; @@ -83,8 +87,8 @@ typedef struct { uint16_t vendor_id; uint16_t product_id; uint16_t release_num; - const uint8_t *product_str; const uint8_t *manufacturer_str; + const uint8_t *product_str; const uint8_t *serial_number_str; const uint8_t *configuration_str; const uint8_t *interface_str; diff --git a/micropython/trezorhal/usb_hid-impl.h b/micropython/trezorhal/usb_hid-impl.h index 9a63d0a97..64ddfdce8 100644 --- a/micropython/trezorhal/usb_hid-impl.h +++ b/micropython/trezorhal/usb_hid-impl.h @@ -256,11 +256,11 @@ static int usb_hid_class_setup(USBD_HandleTypeDef *dev, usb_hid_state_t *state, switch (req->wValue >> 8) { case USB_DESC_TYPE_HID: - USBD_CtlSendData(dev, (uint8_t*)&state->desc_block->hid, MIN(req->wLength, sizeof(state->desc_block->hid))); + USBD_CtlSendData(dev, (uint8_t *)&state->desc_block->hid, MIN(req->wLength, sizeof(state->desc_block->hid))); break; case USB_DESC_TYPE_REPORT: - USBD_CtlSendData(dev, (uint8_t*)state->report_desc, MIN(req->wLength, state->report_desc_len)); + USBD_CtlSendData(dev, UNCONST(state->report_desc), MIN(req->wLength, state->report_desc_len)); break; } break;