Skip to content

Commit d2cc304

Browse files
committed
[examples] Add WeAct-U585CI examples
1 parent b6d4e06 commit d2cc304

File tree

6 files changed

+475
-1
lines changed

6 files changed

+475
-1
lines changed

.github/workflows/linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ jobs:
190190
- name: Examples STM32U3 Series
191191
if: always()
192192
run: |
193-
(cd examples && ../tools/scripts/examples_compile.py nucleo_u385rg-q)
193+
(cd examples && ../tools/scripts/examples_compile.py nucleo_u385rg-q weact_u585ci)
194194
- name: Examples STM32U5 Series
195195
if: always()
196196
run: |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2017, Niklas Hauser
3+
* Copyright (c) 2017, Sascha Schade
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
#include <modm/board.hpp>
12+
13+
int main()
14+
{
15+
Board::initialize();
16+
Board::Leds::setOutput();
17+
18+
while (true)
19+
{
20+
Board::Leds::toggle();
21+
modm::delay(Board::Button::read() ? 250ms : 500ms);
22+
}
23+
return 0;
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<library>
2+
<extends>modm:weact-u585ci</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/weact_u585ci/blink</option>
5+
</options>
6+
<modules>
7+
<module>modm:architecture:delay</module>
8+
<module>modm:build:scons</module>
9+
</modules>
10+
<collectors>
11+
<collect name="modm:build:openocd.source">interface/stlink.cfg</collect>
12+
</collectors>
13+
</library>

examples/weact_u585ci/usb/main.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)