|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "bindings/rp2pio/StateMachine.h" |
| 28 | +#include "shared-bindings/microcontroller/Pin.h" |
| 29 | +#include "shared-bindings/microcontroller/Processor.h" |
| 30 | +#include "shared-bindings/usb_host/Port.h" |
| 31 | +#include "supervisor/usb.h" |
| 32 | + |
| 33 | +#include "src/common/pico_time/include/pico/time.h" |
| 34 | +#include "src/rp2040/hardware_structs/include/hardware/structs/mpu.h" |
| 35 | +#include "src/rp2_common/cmsis/stub/CMSIS/Device/RaspberryPi/RP2040/Include/RP2040.h" |
| 36 | +#include "src/rp2_common/hardware_dma/include/hardware/dma.h" |
| 37 | +#include "src/rp2_common/pico_multicore/include/pico/multicore.h" |
| 38 | + |
| 39 | +#include "py/runtime.h" |
| 40 | + |
| 41 | +#include "tusb.h" |
| 42 | + |
| 43 | +#include "lib/Pico-PIO-USB/src/pio_usb.h" |
| 44 | +#include "lib/Pico-PIO-USB/src/pio_usb_configuration.h" |
| 45 | + |
| 46 | +#include "supervisor/serial.h" |
| 47 | + |
| 48 | +bool usb_host_init; |
| 49 | + |
| 50 | +STATIC PIO pio_instances[2] = {pio0, pio1}; |
| 51 | +volatile bool _core1_ready = false; |
| 52 | + |
| 53 | +static void __not_in_flash_func(core1_main)(void) { |
| 54 | + // The MPU is reset before this starts. |
| 55 | + SysTick->LOAD = (uint32_t)((common_hal_mcu_processor_get_frequency() / 1000) - 1UL); |
| 56 | + SysTick->VAL = 0UL; /* Load the SysTick Counter Value */ |
| 57 | + SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | // Processor clock. |
| 58 | + SysTick_CTRL_ENABLE_Msk; |
| 59 | + |
| 60 | + // Turn off flash access. After this, it will hard fault. Better than messing |
| 61 | + // up CIRCUITPY. |
| 62 | + MPU->CTRL = MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_ENABLE_Msk; |
| 63 | + MPU->RNR = 6; // 7 is used by pico-sdk stack protection. |
| 64 | + MPU->RBAR = XIP_MAIN_BASE | MPU_RBAR_VALID_Msk; |
| 65 | + MPU->RASR = MPU_RASR_XN_Msk | // Set execute never and everything else is restricted. |
| 66 | + MPU_RASR_ENABLE_Msk | |
| 67 | + (0x1b << MPU_RASR_SIZE_Pos); // Size is 0x10000000 which masks up to SRAM region. |
| 68 | + MPU->RNR = 7; |
| 69 | + |
| 70 | + _core1_ready = true; |
| 71 | + |
| 72 | + while (true) { |
| 73 | + pio_usb_host_frame(); |
| 74 | + if (tuh_task_event_ready()) { |
| 75 | + // Queue the tinyusb background task. |
| 76 | + usb_background_schedule(); |
| 77 | + } |
| 78 | + // Wait for systick to reload. |
| 79 | + while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) { |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +STATIC uint8_t _sm_free_count(uint8_t pio_index) { |
| 85 | + PIO pio = pio_instances[pio_index]; |
| 86 | + uint8_t free_count = 0; |
| 87 | + for (size_t j = 0; j < NUM_PIO_STATE_MACHINES; j++) { |
| 88 | + if (!pio_sm_is_claimed(pio, j)) { |
| 89 | + free_count++; |
| 90 | + } |
| 91 | + } |
| 92 | + return free_count; |
| 93 | +} |
| 94 | + |
| 95 | +STATIC bool _has_program_room(uint8_t pio_index, uint8_t program_size) { |
| 96 | + PIO pio = pio_instances[pio_index]; |
| 97 | + pio_program_t program_struct = { |
| 98 | + .instructions = NULL, |
| 99 | + .length = program_size, |
| 100 | + .origin = -1 |
| 101 | + }; |
| 102 | + return pio_can_add_program(pio, &program_struct); |
| 103 | +} |
| 104 | + |
| 105 | +void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { |
| 106 | + if (dp->number + 1 != dm->number) { |
| 107 | + raise_ValueError_invalid_pins(); |
| 108 | + } |
| 109 | + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; |
| 110 | + pio_cfg.skip_alarm_pool = true; |
| 111 | + pio_cfg.pin_dp = dp->number; |
| 112 | + pio_cfg.pio_tx_num = 0; |
| 113 | + pio_cfg.pio_rx_num = 1; |
| 114 | + // PIO with room for 22 instructions |
| 115 | + // PIO with room for 31 instructions and two free SM. |
| 116 | + if (!_has_program_room(pio_cfg.pio_tx_num, 22) || _sm_free_count(pio_cfg.pio_tx_num) < 1 || |
| 117 | + !_has_program_room(pio_cfg.pio_rx_num, 31) || _sm_free_count(pio_cfg.pio_rx_num) < 2) { |
| 118 | + mp_raise_RuntimeError(translate("All state machines in use")); |
| 119 | + } |
| 120 | + pio_cfg.tx_ch = dma_claim_unused_channel(false); // DMA channel |
| 121 | + if (pio_cfg.tx_ch < 0) { |
| 122 | + mp_raise_RuntimeError(translate("All dma channels in use")); |
| 123 | + } |
| 124 | + |
| 125 | + PIO tx_pio = pio_instances[pio_cfg.pio_tx_num]; |
| 126 | + pio_cfg.sm_tx = pio_claim_unused_sm(tx_pio, false); |
| 127 | + PIO rx_pio = pio_instances[pio_cfg.pio_rx_num]; |
| 128 | + pio_cfg.sm_rx = pio_claim_unused_sm(rx_pio, false); |
| 129 | + pio_cfg.sm_eop = pio_claim_unused_sm(rx_pio, false); |
| 130 | + |
| 131 | + // Unclaim everything so that the library can. |
| 132 | + dma_channel_unclaim(pio_cfg.tx_ch); |
| 133 | + pio_sm_unclaim(tx_pio, pio_cfg.sm_tx); |
| 134 | + pio_sm_unclaim(rx_pio, pio_cfg.sm_rx); |
| 135 | + pio_sm_unclaim(rx_pio, pio_cfg.sm_eop); |
| 136 | + |
| 137 | + // Set all of the state machines to never reset. |
| 138 | + rp2pio_statemachine_never_reset(tx_pio, pio_cfg.sm_tx); |
| 139 | + rp2pio_statemachine_never_reset(rx_pio, pio_cfg.sm_rx); |
| 140 | + rp2pio_statemachine_never_reset(rx_pio, pio_cfg.sm_eop); |
| 141 | + |
| 142 | + common_hal_never_reset_pin(dp); |
| 143 | + common_hal_never_reset_pin(dm); |
| 144 | + |
| 145 | + // Core 1 will run the SOF interrupt directly. |
| 146 | + _core1_ready = false; |
| 147 | + multicore_launch_core1(core1_main); |
| 148 | + while (!_core1_ready) { |
| 149 | + } |
| 150 | + |
| 151 | + tuh_configure(TUH_OPT_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg); |
| 152 | + tuh_init(TUH_OPT_RHPORT); |
| 153 | + |
| 154 | + self->init = true; |
| 155 | + usb_host_init = true; |
| 156 | +} |
| 157 | + |
| 158 | +void common_hal_usb_host_port_deinit(usb_host_port_obj_t *self) { |
| 159 | + self->init = false; |
| 160 | + usb_host_init = false; |
| 161 | +} |
| 162 | + |
| 163 | +bool common_hal_usb_host_port_deinited(usb_host_port_obj_t *self) { |
| 164 | + return !self->init; |
| 165 | +} |
0 commit comments