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

Upgrade TinyUSB to v0.16.0, allow dual role operations, fix HS port operations #1116

Merged
merged 6 commits into from
Jan 5, 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
2 changes: 2 additions & 0 deletions examples/generic/usb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ int main()
{
Board::initialize();
Board::initializeUsbFs();
// DISCO-F746NG also has a HS port:
// Board::initializeUsbHs();
tusb_init();

while (true)
Expand Down
45 changes: 34 additions & 11 deletions examples/generic/usb/msc_disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

#if CFG_TUD_MSC

// whether host does safe-eject
static bool ejected = false;

// Some MCU doesn't have enough 8KB SRAM to store the whole disk
// We will use Flash as read-only disk with board that has
// CFG_EXAMPLE_MSC_READONLY defined
Expand Down Expand Up @@ -136,7 +139,14 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun)
{
(void) lun;

return true; // RAM disk is always ready
// RAM disk is ready until ejected
if (ejected) {
// Additional Sense 3A-00 is NOT_FOUND
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
return false;
}

return true;
}

// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
Expand Down Expand Up @@ -165,6 +175,7 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo
}else
{
// unload disk storage
ejected = true;
}
}

Expand All @@ -177,10 +188,24 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buff
{
(void) lun;

// out of ramdisk
if ( lba >= DISK_BLOCK_NUM ) return -1;

uint8_t const* addr = msc_disk[lba] + offset;
memcpy(buffer, addr, bufsize);

return bufsize;
return (int32_t) bufsize;
}

bool tud_msc_is_writable_cb (uint8_t lun)
{
(void) lun;

#ifdef CFG_EXAMPLE_MSC_READONLY
return false;
#else
return true;
#endif
}

// Callback invoked when received WRITE10 command.
Expand All @@ -189,14 +214,17 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
{
(void) lun;

// out of ramdisk
if ( lba >= DISK_BLOCK_NUM ) return -1;

#ifndef CFG_EXAMPLE_MSC_READONLY
uint8_t* addr = msc_disk[lba] + offset;
memcpy(addr, buffer, bufsize);
#else
(void) lba; (void) offset; (void) buffer;
#endif

return bufsize;
return (int32_t) bufsize;
}

// Callback invoked when received an SCSI command not in built-in list below
Expand All @@ -207,18 +235,13 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
// read10 & write10 has their own callback and MUST not be handled here

void const* response = NULL;
uint16_t resplen = 0;
int32_t resplen = 0;

// most scsi handled is input
bool in_xfer = true;

switch (scsi_cmd[0])
{
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
// Host is about to read/write etc ... better not to disconnect disk
resplen = 0;
break;

default:
// Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
Expand All @@ -235,14 +258,14 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
{
if(in_xfer)
{
memcpy(buffer, response, resplen);
memcpy(buffer, response, (size_t) resplen);
}else
{
// SCSI output
}
}

return resplen;
return (int32_t) resplen;
}

#endif
38 changes: 26 additions & 12 deletions examples/generic/usb/project.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
<library>
<extends>modm:blue-pill-f103</extends>
<!-- <extends>modm:disco-f072rb</extends> -->
<!-- <extends>modm:black-pill-f401</extends> -->
<!-- <extends>modm:black-pill-f411</extends> -->
<!-- <extends>modm:feather-m0</extends> -->
<!-- <extends>modm:samd21-mini</extends> -->
<!-- <extends>modm:nucleo-f429zi</extends> -->
<!-- <extends>modm:disco-f072rb</extends> -->
<!-- <extends>modm:disco-f303vc</extends> -->
<!-- <extends>modm:disco-f407vg</extends> -->
<!-- <extends>modm:disco-f429zi</extends> -->
<!-- <extends>modm:disco-f469ni</extends> -->
<!-- <extends>modm:disco-f746ng</extends> -->
<!-- <extends>modm:disco-l476vg</extends> -->
<!-- <extends>modm:feather-m0</extends> -->
<!-- <extends>modm:nucleo-f429zi</extends> -->
<!-- <extends>modm:nucleo-h723zg</extends> -->
<!-- <extends>modm:nucleo-h743zi</extends> -->
<!-- <extends>modm:rp-pico</extends> -->
<!-- <extends>modm:samd21-mini</extends> -->
<options>
<option name="modm:build:build.path">../../../build/generic/usb</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
<option name="modm:tinyusb:config">device.cdc,device.msc</option>
<!-- <option name="modm:tinyusb:config">device.cdc,device.midi</option> -->
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:tinyusb</module>
<module>modm:processing:timer</module>
<module>modm:io</module>
</modules>
</library>

<!-- Required for modm:disco-f429zi -->
<!-- <option name="modm:tinyusb:device:port">hs</option> -->

<!-- Required for modm:nucleo-h723zg, modm:disco-f429zi -->
<!-- <option name="modm:tinyusb:max-speed">full</option> -->
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:tinyusb</module>
<module>modm:processing:timer</module>
<module>modm:io</module>
</modules>
<collectors>
<!-- <collect name="modm:build:cppdefines">CFG_TUSB_DEBUG=3</collect> -->
</collectors>
</library>
2 changes: 1 addition & 1 deletion examples/stm32f3_discovery/usb_dfu/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<extends>modm:disco-f303vc</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f3_discovery/usb_dfu</option>
<option name="modm:tinyusb:config">device.dfu</option>
<option name="modm:tinyusb:config">device.dfu_rt</option>
</options>
<modules>
<module>modm:build:scons</module>
Expand Down
15 changes: 13 additions & 2 deletions examples/stm32f429_discovery/blink/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,31 @@ main()
Board::initialize();

LedRed::set();
usb::VBus::setOutput(modm::Gpio::Low);
usb::Vbus::setOutput(modm::Gpio::Low);
usb::Overcurrent::setOutput(modm::Gpio::Low);

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
MODM_LOG_DEBUG << "debug" << modm::endl;
MODM_LOG_INFO << "info" << modm::endl;
MODM_LOG_WARNING << "warning" << modm::endl;
MODM_LOG_ERROR << "error" << modm::endl;

uint32_t counter(0);

while (true)
{
LedRed::toggle();
LedGreen::toggle();

modm::delay(Button::read() ? 125ms : 500ms);

usb::VBus::toggle();
usb::Vbus::toggle();
usb::Overcurrent::toggle();

modm::delay(Button::read() ? 125ms : 500ms);

MODM_LOG_INFO << "loop: " << counter++ << modm::endl;
}

return 0;
Expand Down
54 changes: 0 additions & 54 deletions examples/stm32f429_discovery/logger/main.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions examples/stm32f429_discovery/logger/project.xml

This file was deleted.

Loading
Loading