Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Max3421E support for usb host #9164

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ TAGS

# Uncrustify formatting
*.uncrustify

# clangd cache
##############
.cache
2 changes: 1 addition & 1 deletion docs/shared_bindings_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"select": "MICROPY_PY_SELECT_SELECT",
"sys": "CIRCUITPY_SYS",
"terminalio": "CIRCUITPY_DISPLAYIO",
"usb": "CIRCUITPY_USB_HOST",
"usb": "CIRCUITPY_PYUSB",
}

MODULES_NOT_IN_BINDINGS = [ "binascii", "errno", "json", "re", "ulab" ]
Expand Down
2 changes: 1 addition & 1 deletion lib/tinyusb
Submodule tinyusb updated 72 files
+13 −7 .github/workflows/build_esp.yml
+1 −1 .github/workflows/cifuzz.yml
+15 −9 .github/workflows/cmake_arm.yml
+1 −1 .github/workflows/codeql.yml
+65 −0 .github/workflows/labeler.yml
+2 −2 .idea/runConfigurations/stlink.xml
+36 −11 docs/reference/index.rst
+1 −0 examples/build_system/make/make.mk
+3 −1 examples/build_system/make/toolchain/arm_gcc.mk
+41 −0 examples/device/uac2_headset/src/main.c
+3 −0 examples/device/uac2_headset/src/tusb_config.h
+6 −1 examples/device/uac2_headset/src/usb_descriptors.c
+9 −6 examples/device/uac2_headset/src/usb_descriptors.h
+1 −0 examples/host/CMakeLists.txt
+0 −1 examples/host/cdc_msc_hid_freertos/only.txt
+1 −0 examples/host/cdc_msc_hid_freertos/skip.txt
+1 −0 hw/bsp/board_api.h
+16 −27 hw/bsp/family_support.cmake
+6 −5 hw/bsp/nrf/boards/pca10056/board.h
+0 −3 hw/bsp/nrf/boards/pca10095/board.cmake
+6 −5 hw/bsp/nrf/boards/pca10095/board.h
+1 −14 hw/bsp/nrf/family.c
+4 −0 hw/bsp/rp2040/boards/feather_rp2040_max3421/board.cmake
+1 −1 hw/bsp/rp2040/family.cmake
+9 −0 hw/bsp/samd21/boards/sparkfun_samd21_mini_usb/board.cmake
+51 −0 hw/bsp/samd21/boards/sparkfun_samd21_mini_usb/board.h
+9 −0 hw/bsp/samd21/boards/sparkfun_samd21_mini_usb/board.mk
+146 −0 hw/bsp/samd21/boards/sparkfun_samd21_mini_usb/sparkfun_samd21_mini_usb.ld
+2 −0 hw/bsp/samd21/boards/trinket_m0/board.h
+16 −2 hw/bsp/samd21/family.c
+8 −0 hw/bsp/stm32h5/boards/stm32h503nucleo/board.cmake
+122 −0 hw/bsp/stm32h5/boards/stm32h503nucleo/board.h
+7 −0 hw/bsp/stm32h5/boards/stm32h503nucleo/board.mk
+12 −6 hw/bsp/stm32h5/family.c
+25 −0 src/class/audio/audio.h
+59 −39 src/class/audio/audio_device.c
+12 −16 src/class/audio/audio_device.h
+13 −3 src/class/cdc/cdc_host.c
+2 −1 src/class/cdc/cdc_host.h
+8 −3 src/class/hid/hid_host.c
+2 −1 src/class/hid/hid_host.h
+7 −1 src/class/msc/msc_host.c
+2 −1 src/class/msc/msc_host.h
+12 −11 src/class/video/video.h
+154 −104 src/class/video/video_device.c
+10 −21 src/common/tusb_fifo.h
+9 −10 src/common/tusb_private.h
+6 −1 src/device/usbd.c
+9 −4 src/device/usbd.h
+4 −1 src/host/hcd.h
+6 −2 src/host/hub.c
+6 −7 src/host/hub.h
+164 −109 src/host/usbh.c
+16 −2 src/host/usbh.h
+2 −4 src/host/usbh_pvt.h
+3 −0 src/osal/osal.h
+17 −2 src/osal/osal_freertos.h
+31 −30 src/osal/osal_mynewt.h
+16 −0 src/osal/osal_none.h
+43 −62 src/osal/osal_pico.h
+36 −24 src/osal/osal_rtthread.h
+32 −29 src/osal/osal_rtx4.h
+170 −106 src/portable/analog/max3421/hcd_max3421.c
+2 −2 src/portable/nordic/nrf5x/dcd_nrf5x.c
+4 −6 src/portable/raspberrypi/rp2040/hcd_rp2040.c
+22 −8 src/portable/sony/cxd56/dcd_cxd56.c
+33 −20 src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
+76 −105 src/portable/st/stm32_fsdev/dcd_stm32_fsdev.h
+169 −144 src/portable/synopsys/dwc2/dcd_dwc2.c
+113 −179 src/tusb.c
+5 −1 src/tusb_option.h
+11 −10 tools/build_utils.py
80 changes: 80 additions & 0 deletions ports/atmel-samd/common-hal/max3421e/Max3421E.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
*
* 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.
*/

#include "shared-module/max3421e/Max3421E.h"

#include "common-hal/max3421e/Max3421E.h"

#include "lib/tinyusb/src/host/usbh.h"
#include "supervisor/usb.h"

#include "eic_handler.h"
#include "py/runtime.h"
#include "samd/external_interrupts.h"

#include "shared-bindings/microcontroller/Pin.h"

void samd_max3421e_interrupt_handler(uint8_t channel) {
max3421e_interrupt_handler((max3421e_max3421e_obj_t *)get_eic_channel_data(channel));
}

// Setup irq on self->irq to call tuh_int_handler(rhport, true) on falling edge
void common_hal_max3421e_max3421e_init_irq(max3421e_max3421e_obj_t *self) {
const mcu_pin_obj_t *pin = self->irq.pin;
if (!pin->has_extint) {
raise_ValueError_invalid_pin();
}
if (eic_get_enable()) {
if (!eic_channel_free(pin->extint_channel)) {
mp_raise_RuntimeError(MP_ERROR_TEXT("Internal resource(s) in use"));
}
} else {
turn_on_external_interrupt_controller();
}

set_eic_channel_data(pin->extint_channel, self);
set_eic_handler(pin->extint_channel, EIC_HANDLER_MAX3421E);
turn_on_eic_channel(pin->extint_channel, EIC_CONFIG_SENSE0_LOW_Val);
}

void common_hal_max3421e_max3421e_deinit_irq(max3421e_max3421e_obj_t *self) {
const mcu_pin_obj_t *pin = self->irq.pin;
set_eic_handler(pin->extint_channel, EIC_HANDLER_NO_INTERRUPT);
turn_off_eic_channel(pin->extint_channel);
reset_pin_number(pin->extint_channel);
}

// Enable or disable the irq interrupt.
void common_hal_max3421e_max3421e_irq_enabled(max3421e_max3421e_obj_t *self, bool enabled) {
const mcu_pin_obj_t *pin = self->irq.pin;
uint32_t mask = 1 << pin->extint_channel;
// Don't use the turn_(on|off) API because it may deinit the eic completely.
if (!enabled) {
EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos;
} else {
EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos;
}
}
31 changes: 31 additions & 0 deletions ports/atmel-samd/common-hal/max3421e/Max3421E.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
*
* 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.
*/

#pragma once

#include <stdint.h>

void samd_max3421e_interrupt_handler(uint8_t channel);
7 changes: 7 additions & 0 deletions ports/atmel-samd/eic_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* THE SOFTWARE.
*/

#include "common-hal/max3421e/Max3421E.h"
#include "common-hal/pulseio/PulseIn.h"
#include "common-hal/ps2io/Ps2.h"
#include "common-hal/rotaryio/IncrementalEncoder.h"
Expand Down Expand Up @@ -73,6 +74,12 @@ void shared_eic_handler(uint8_t channel) {
break;
#endif

#if CIRCUITPY_MAX3421E
case EIC_HANDLER_MAX3421E:
samd_max3421e_interrupt_handler(channel);
break;
#endif

default:
break;
}
Expand Down
6 changes: 2 additions & 4 deletions ports/atmel-samd/eic_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_EIC_HANDLER_H
#define MICROPY_INCLUDED_ATMEL_SAMD_EIC_HANDLER_H
#pragma once

#define EIC_HANDLER_NO_INTERRUPT 0x0
#define EIC_HANDLER_PULSEIN 0x1
#define EIC_HANDLER_INCREMENTAL_ENCODER 0x2
#define EIC_HANDLER_PS2 0x3
#define EIC_HANDLER_COUNTER 0x04
#define EIC_HANDLER_ALARM 0x05
#define EIC_HANDLER_MAX3421E 0x06

void set_eic_handler(uint8_t channel, uint8_t eic_handler);
void shared_eic_handler(uint8_t channel);

#endif // MICROPY_INCLUDED_ATMEL_SAMD_EIC_HANDLER_H
8 changes: 8 additions & 0 deletions ports/atmel-samd/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ ifeq ($(CIRCUITPY_FULL_BUILD),0)
CIRCUITPY_LTO_PARTITION ?= one
endif

# 20A skus have 1MB flash and can fit more.
ifeq ($(patsubst %20A,,$(CHIP_VARIANT)),)
HAS_1MB_FLASH = 1
else
HAS_1MB_FLASH = 0
endif

# The ?='s allow overriding in mpconfigboard.mk.


Expand All @@ -107,6 +114,7 @@ CIRCUITPY_ALARM ?= 1
CIRCUITPY_BITMAPFILTER ?= 0
CIRCUITPY_FLOPPYIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_MAX3421E ?= $(HAS_1MB_FLASH)
CIRCUITPY_PS2IO ?= 1
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FRAMEBUFFERIO)
CIRCUITPY_SAMD ?= 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
USB_VID = 0x239A
USB_PID = 0x8114
USB_PRODUCT = "Adafruit Feather ESP32S3 No PSRAM"
USB_PRODUCT = "Feather ESP32S3 No PSRAM"
USB_MANUFACTURER = "Adafruit"

IDF_TARGET = esp32s3
Expand Down
33 changes: 23 additions & 10 deletions ports/espressif/common-hal/busio/SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <string.h>

#include "freertos/projdefs.h"
#include "py/runtime.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
Expand All @@ -37,6 +38,7 @@

static bool spi_never_reset[SOC_SPI_PERIPH_NUM];
static spi_device_handle_t spi_handle[SOC_SPI_PERIPH_NUM];
static StaticSemaphore_t spi_mutex[SOC_SPI_PERIPH_NUM];

static bool spi_bus_is_free(spi_host_device_t host_id) {
return spi_bus_get_attr(host_id) == NULL;
Expand Down Expand Up @@ -121,6 +123,8 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
claim_pin(miso);
}
claim_pin(clock);

self->mutex = xSemaphoreCreateMutexStatic(&spi_mutex[self->host_id]);
}

void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
Expand All @@ -143,14 +147,22 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
return;
}

// Wait for any other users of this to finish.
while (!common_hal_busio_spi_try_lock(self)) {
}

// Mark us as deinit early in case we are used in an interrupt.
common_hal_reset_pin(self->clock);
self->clock = NULL;

spi_never_reset[self->host_id] = false;
spi_bus_remove_device(spi_handle[self->host_id]);
spi_bus_free(self->host_id);

vSemaphoreDelete(self->mutex);

common_hal_reset_pin(self->MOSI);
common_hal_reset_pin(self->MISO);
common_hal_reset_pin(self->clock);
self->clock = NULL;
}

bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
Expand All @@ -167,20 +179,18 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
}

bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
bool grabbed_lock = false;
if (!self->has_lock) {
grabbed_lock = true;
self->has_lock = true;
if (common_hal_busio_spi_deinited(self)) {
return false;
}
return grabbed_lock;
return xSemaphoreTake(self->mutex, 1) == pdTRUE;
}

bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {
return self->has_lock;
return xSemaphoreGetMutexHolder(self->mutex) == xTaskGetCurrentTaskHandle();
}

void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
self->has_lock = false;
xSemaphoreGive(self->mutex);
}

bool common_hal_busio_spi_write(busio_spi_obj_t *self,
Expand Down Expand Up @@ -229,7 +239,10 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,

transactions[0].flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
transactions[0].length = bits_to_send;
spi_device_transmit(spi_handle[self->host_id], &transactions[0]);
esp_err_t result = spi_device_transmit(spi_handle[self->host_id], &transactions[0]);
if (result != ESP_OK) {
return false;
}

if (data_in != NULL) {
memcpy(data_in, &transactions[0].rx_data, len);
Expand Down
7 changes: 2 additions & 5 deletions ports/espressif/common-hal/busio/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BUSIO_SPI_H
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BUSIO_SPI_H
#pragma once

#include "driver/spi_master.h"
#include "shared-bindings/microcontroller/Pin.h"
Expand All @@ -44,9 +43,7 @@ typedef struct {
uint8_t polarity;
uint32_t baudrate;

bool has_lock;
SemaphoreHandle_t mutex;
} busio_spi_obj_t;

void spi_reset(void);

#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BUSIO_SPI_H
Loading
Loading