|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Niklas Hauser |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <tusb.h> |
| 12 | + |
| 13 | +#include <modm/board.hpp> |
| 14 | +#include <modm/io.hpp> |
| 15 | +#include <modm/processing.hpp> |
| 16 | + |
| 17 | +using namespace Board; |
| 18 | + |
| 19 | +#if CFG_TUD_CDC |
| 20 | +modm::IODeviceWrapper<UsbUart0, modm::IOBuffer::DiscardIfFull> usb_io_device0; |
| 21 | +modm::IOStream usb_stream0(usb_io_device0); |
| 22 | +#endif |
| 23 | + |
| 24 | +modm::PeriodicTimer tmr{2.5s}; |
| 25 | + |
| 26 | +// Invoked when device is mounted |
| 27 | +void tud_mount_cb() { tmr.restart(1s); } |
| 28 | +// Invoked when device is unmounted |
| 29 | +void tud_umount_cb() { tmr.restart(250ms); } |
| 30 | +// Invoked when usb bus is suspended |
| 31 | +// remote_wakeup_en : if host allow us to perform remote wakeup |
| 32 | +// Within 7ms, device must draw an average of current less than 2.5 mA from bus |
| 33 | +void tud_suspend_cb(bool) { tmr.restart(2.5s); } |
| 34 | +// Invoked when usb bus is resumed |
| 35 | +void tud_resume_cb() { tmr.restart(1s); } |
| 36 | +void midi_task(); |
| 37 | + |
| 38 | +modm_section(".noinit") uint64_t dfu_mode; |
| 39 | +constexpr uint64_t DFU_MAGIC{0xf318ea89313f2be8}; |
| 40 | + |
| 41 | +// Invoked on DFU_DETACH request to reboot to the bootloader |
| 42 | +void tud_dfu_runtime_reboot_to_dfu_cb(void) |
| 43 | +{ |
| 44 | + dfu_mode = DFU_MAGIC; |
| 45 | + // You must delay SystemReset so that TinyUSB can finish!!! |
| 46 | + tmr.restart(100ms); |
| 47 | +} |
| 48 | + |
| 49 | +int main() |
| 50 | +{ |
| 51 | + if (dfu_mode == DFU_MAGIC) |
| 52 | + { |
| 53 | + dfu_mode = 0; __DSB(); |
| 54 | + // Jump to SystemFlash *before* initializing any peripherals! |
| 55 | + asm volatile |
| 56 | + ( |
| 57 | + // Address of SystemFlash differs between devices!!! |
| 58 | + "ldr r0, =0x0BF90000" "\n\t" |
| 59 | + "ldr sp,[r0, #0]" "\n\t" |
| 60 | + "ldr r0,[r0, #4]" "\n\t" |
| 61 | + "bx r0" |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + Board::initialize(); |
| 66 | + Board::initializeUsb(); |
| 67 | + tusb_init(); |
| 68 | + |
| 69 | + while (true) |
| 70 | + { |
| 71 | + tud_task(); |
| 72 | + midi_task(); |
| 73 | + |
| 74 | +#if CFG_TUD_CDC |
| 75 | + // Do a loopback on the CDC |
| 76 | + if (char input; usb_stream0.get(input), input != modm::IOStream::eof) { |
| 77 | + usb_stream0 << input; |
| 78 | + } |
| 79 | +#endif |
| 80 | + if (tmr.execute()) |
| 81 | + { |
| 82 | + if (dfu_mode == DFU_MAGIC) NVIC_SystemReset(); |
| 83 | + Leds::toggle(); |
| 84 | + static uint8_t counter{0}; |
| 85 | +#ifdef MODM_BOARD_HAS_LOGGER |
| 86 | + MODM_LOG_INFO << "Loop counter: " << (counter++) << modm::endl; |
| 87 | +#endif |
| 88 | +#if CFG_TUD_CDC |
| 89 | + usb_stream0 << "Hello World from USB: " << (counter++) << modm::endl; |
| 90 | +#endif |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +void midi_task() |
| 99 | +{ |
| 100 | +#if CFG_TUD_MIDI |
| 101 | + static modm::PeriodicTimer tmr{286ms}; |
| 102 | + |
| 103 | + if (tmr.execute()) |
| 104 | + { |
| 105 | + // The MIT License (MIT) |
| 106 | + // Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 107 | + |
| 108 | + // Store example melody as an array of note values |
| 109 | + static uint8_t note_sequence[] = |
| 110 | + { |
| 111 | + 74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78, |
| 112 | + 74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61, |
| 113 | + 56,61,64,68,74,78,81,86,90,93,98,102 |
| 114 | + }; |
| 115 | + |
| 116 | + // The MIDI interface always creates input and output port/jack descriptors |
| 117 | + // regardless of these being used or not. Therefore incoming traffic should be read |
| 118 | + // (possibly just discarded) to avoid the sender blocking in IO |
| 119 | + uint8_t packet[4]; |
| 120 | + while ( tud_midi_available() ) tud_midi_packet_read(packet); |
| 121 | + |
| 122 | + // Variable that holds the current position in the sequence. |
| 123 | + static uint32_t note_pos = 0; |
| 124 | + uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint |
| 125 | + uint8_t const channel = 0; // 0 for channel 1 |
| 126 | + |
| 127 | + // Previous positions in the note sequence. |
| 128 | + int previous = note_pos - 1; |
| 129 | + |
| 130 | + // If we currently are at position 0, set the |
| 131 | + // previous position to the last note in the sequence. |
| 132 | + if (previous < 0) previous = sizeof(note_sequence) - 1; |
| 133 | + |
| 134 | + // Send Note On for current position at full velocity (127) on channel 1. |
| 135 | + uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 }; |
| 136 | + tud_midi_stream_write(cable_num, note_on, 3); |
| 137 | + |
| 138 | + // Send Note Off for previous note. |
| 139 | + uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0}; |
| 140 | + tud_midi_stream_write(cable_num, note_off, 3); |
| 141 | + |
| 142 | + // Increment position |
| 143 | + note_pos++; |
| 144 | + |
| 145 | + // If we are at the end of the sequence, start over. |
| 146 | + if (note_pos >= sizeof(note_sequence)) note_pos = 0; |
| 147 | + } |
| 148 | +#endif |
| 149 | +} |
0 commit comments