-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.c
322 lines (277 loc) · 9.82 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/drivers/gpio.h>
#include <usb_midi/usb_midi.h>
#if defined(CLOCK_FEATURE_HFCLK_DIVIDE_PRESENT) || NRF_CLOCK_HAS_HFCLK192M
#include <nrfx_clock.h>
#endif
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(usb_midi_sample);
struct k_work button_press_work;
struct k_work event_tx_work;
struct k_work_delayable rx_led_off_work;
struct k_work_delayable tx_led_off_work;
static void send_next_sysex_chunk();
/************************ App state ************************/
struct sample_app_state_t {
int usb_midi_is_available;
int tx_note_off;
int sysex_rx_byte_count;
uint8_t sysex_rx_bytes[CONFIG_SYSEX_ECHO_MAX_LENGTH];
int64_t sysex_rx_start_time;
int sysex_tx_is_echo;
int sysex_tx_byte_count;
int sysex_tx_msg_size;
int sysex_tx_in_progress;
int sysex_tx_cable_num;
int64_t sysex_tx_start_time;
};
static struct sample_app_state_t sample_app_state = {.usb_midi_is_available = 0,
.sysex_rx_byte_count = 0,
.sysex_tx_in_progress = 0,
.sysex_rx_start_time = 0,
.sysex_tx_byte_count = 0,
.sysex_tx_start_time = 0,
.sysex_tx_is_echo = 0,
.sysex_tx_in_progress = 0,
.sysex_tx_cable_num = 0,
.tx_note_off = 0,
.sysex_rx_byte_count = 0
};
static void log_sysex_transfer_time(int is_tx, int cable_num, int num_bytes, int time_ms) {
float bytes_per_s = time_ms == 0 ? 0 : (float)num_bytes / (0.001 * time_ms);
LOG_INF("sysex %s done | cable %d | %d bytes in %d ms | %d bytes/s", is_tx ? "tx" : "rx", cable_num,
num_bytes, (int)time_ms, (int)bytes_per_s);
}
static void sysex_tx_will_start(int is_echo, int msg_size, int cable_num) {
__ASSERT_NO_MSG(sample_app_state.sysex_tx_in_progress == 0);
sample_app_state.sysex_tx_in_progress = 1;
sample_app_state.sysex_tx_is_echo = is_echo;
sample_app_state.sysex_tx_byte_count = 0;
sample_app_state.sysex_tx_msg_size = msg_size;
sample_app_state.sysex_tx_cable_num = cable_num;
sample_app_state.sysex_tx_start_time = k_uptime_get();
}
/************************ LEDs ************************/
static struct gpio_dt_spec usb_midi_available_led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
static struct gpio_dt_spec midi_rx_led = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
static struct gpio_dt_spec midi_tx_led = GPIO_DT_SPEC_GET(DT_ALIAS(led2), gpios);
static void init_leds()
{
gpio_pin_configure_dt(&usb_midi_available_led, GPIO_OUTPUT_ACTIVE);
gpio_pin_set_dt(&usb_midi_available_led, 0);
gpio_pin_configure_dt(&midi_rx_led, GPIO_OUTPUT_ACTIVE);
gpio_pin_set_dt(&midi_rx_led, 0);
gpio_pin_configure_dt(&midi_tx_led, GPIO_OUTPUT_ACTIVE);
gpio_pin_set_dt(&midi_tx_led, 0);
}
static void set_usb_midi_available_led(int is_available)
{
gpio_pin_set_dt(&usb_midi_available_led, is_available);
}
static void flash_tx_led()
{
gpio_pin_set_dt(&midi_tx_led, 1);
k_work_cancel_delayable(&tx_led_off_work);
k_work_schedule(&tx_led_off_work, Z_TIMEOUT_MS(CONFIG_LED_FLASH_DURATION_MS));
}
static void flash_rx_led()
{
gpio_pin_set_dt(&midi_rx_led, 1);
k_work_cancel_delayable(&rx_led_off_work);
k_work_schedule(&rx_led_off_work, Z_TIMEOUT_MS(CONFIG_LED_FLASH_DURATION_MS));
}
/****************** Work queue callbacks ******************/
void on_event_tx(struct k_work *item)
{
if (sample_app_state.usb_midi_is_available && !sample_app_state.sysex_tx_in_progress) {
uint8_t note = CONFIG_TX_PERIODIC_NOTE_NUMBER;
uint8_t vel = CONFIG_TX_PERIODIC_NOTE_VELOCITY;
uint8_t msg[3] = {sample_app_state.tx_note_off ? 0x80 : 0x90, note, vel };
flash_tx_led();
usb_midi_tx(0, msg);
sample_app_state.tx_note_off = !sample_app_state.tx_note_off;
}
}
void on_button_press(struct k_work *item)
{
if (sample_app_state.usb_midi_is_available && !sample_app_state.sysex_tx_in_progress) {
sysex_tx_will_start(0, CONFIG_SYSEX_TX_TEST_MSG_SIZE, CONFIG_SYSEX_TX_TEST_MSG_CABLE_NUM);
send_next_sysex_chunk();
}
}
void on_rx_led_off(struct k_work *item)
{
gpio_pin_set_dt(&midi_rx_led, 0);
}
void on_tx_led_off(struct k_work *item)
{
gpio_pin_set_dt(&midi_tx_led, 0);
}
/************************ Buttons ************************/
static struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(DT_ALIAS(sw0), gpios, {0});
static struct gpio_callback button_cb_data;
static void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
k_work_submit(&button_press_work);
}
static void init_button()
{
__ASSERT_NO_MSG(device_is_ready(button.port));
int ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
__ASSERT_NO_MSG(ret == 0);
ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
__ASSERT_NO_MSG(ret == 0);
gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
ret = gpio_add_callback(button.port, &button_cb_data);
__ASSERT_NO_MSG(ret == 0);
}
/****************** USB MIDI callbacks ******************/
static void midi_message_cb(uint8_t *bytes, uint8_t num_bytes, uint8_t cable_num)
{
flash_rx_led();
}
static void sysex_start_cb(uint8_t cable_num)
{
sample_app_state.sysex_rx_start_time = k_uptime_get();
sample_app_state.sysex_rx_byte_count = 0;
sample_app_state.sysex_rx_bytes[0] = 0xf0;
sample_app_state.sysex_rx_byte_count++;
flash_rx_led();
}
static void sysex_data_cb(uint8_t *data_bytes, uint8_t num_data_bytes, uint8_t cable_num)
{
#ifdef CONFIG_SYSEX_ECHO_ENABLED
for (int i = 0; i < num_data_bytes; i++) {
int dest_idx = sample_app_state.sysex_rx_byte_count + i;
if (dest_idx == CONFIG_SYSEX_ECHO_MAX_LENGTH - 1) {
// reached max length. end stored message prematurely by adding
// end of sysex status byte.
sample_app_state.sysex_rx_bytes[dest_idx] = 0xf7;
}
else if (dest_idx >= CONFIG_SYSEX_ECHO_MAX_LENGTH) {
break;
}
else {
sample_app_state.sysex_rx_bytes[dest_idx] = data_bytes[i];
}
}
#endif
sample_app_state.sysex_rx_byte_count += num_data_bytes;
}
static void sysex_end_cb(uint8_t cable_num)
{
if (sample_app_state.sysex_rx_byte_count < CONFIG_SYSEX_ECHO_MAX_LENGTH) {
sample_app_state.sysex_rx_bytes[sample_app_state.sysex_rx_byte_count] = 0xf7;
}
sample_app_state.sysex_rx_byte_count++; // account for the last 0xf7
u_int64_t dt_ms = k_uptime_get() - sample_app_state.sysex_rx_start_time;
log_sysex_transfer_time(0, cable_num, sample_app_state.sysex_rx_byte_count, dt_ms);
flash_rx_led();
#ifdef CONFIG_SYSEX_ECHO_ENABLED
LOG_INF("Echoing received sysex");
sysex_tx_will_start(1, sample_app_state.sysex_rx_byte_count < CONFIG_SYSEX_ECHO_MAX_LENGTH ? sample_app_state.sysex_rx_byte_count : CONFIG_SYSEX_ECHO_MAX_LENGTH, cable_num);
send_next_sysex_chunk();
#endif
}
static void usb_midi_available_cb(int is_available)
{
sample_app_state.usb_midi_is_available = is_available;
set_usb_midi_available_led(is_available);
if (is_available) {
sample_app_state.tx_note_off = 0;
sample_app_state.sysex_tx_in_progress = 0;
}
}
static uint8_t get_next_sysex_tx_byte() {
if (sample_app_state.sysex_tx_is_echo) {
return sample_app_state.sysex_rx_bytes[sample_app_state.sysex_tx_byte_count];
}
else {
if (sample_app_state.sysex_tx_byte_count == 0) {
return 0xf0;
}
else if (sample_app_state.sysex_tx_byte_count == sample_app_state.sysex_tx_msg_size - 1) {
return 0xf7;
}
else {
return sample_app_state.sysex_tx_byte_count % 100;
}
}
}
static void send_next_sysex_chunk() {
__ASSERT_NO_MSG(sample_app_state.sysex_tx_in_progress);
flash_tx_led();
while (1) {
if (usb_midi_tx_buffer_is_full()) {
// tx packet is full. send it.
usb_midi_tx_buffer_send();
// nothing further for now. wait for tx done callback before
// filling the next packet.
break;
}
int sysex_msg_size = sample_app_state.sysex_tx_msg_size;
uint8_t chunk[3] = {0, 0, 0};
for (int i = 0; i < 3; i++) {
uint8_t next_sysex_byte = get_next_sysex_tx_byte();
chunk[i] = next_sysex_byte;
sample_app_state.sysex_tx_byte_count++;
if (sample_app_state.sysex_tx_byte_count == sysex_msg_size) {
break;
}
}
// Enqueue three byte sysex chunk for transmission
// TODO: check if this suceeds or not? Currently, this check is not needed
// since each MIDI message is put into a 4 byte packet and the tx buffer size
// is a multiple of 4.
usb_midi_tx_buffer_add(sample_app_state.sysex_tx_cable_num, chunk);
if (sample_app_state.sysex_tx_byte_count == sysex_msg_size) {
// No more data to add to tx packet. Send it, then we're done.
usb_midi_tx_buffer_send();
flash_tx_led();
u_int64_t dt_ms = k_uptime_get() - sample_app_state.sysex_tx_start_time;
log_sysex_transfer_time(1, sample_app_state.sysex_tx_cable_num, sysex_msg_size, dt_ms);
sample_app_state.sysex_tx_in_progress = 0;
break;
}
}
}
static void usb_midi_tx_done_cb()
{
if (sample_app_state.sysex_tx_in_progress) {
send_next_sysex_chunk();
}
}
/****************** Sample app ******************/
void main(void)
{
#if defined(CLOCK_FEATURE_HFCLK_DIVIDE_PRESENT) || NRF_CLOCK_HAS_HFCLK192M
// Run nrf5340 app core at full speed (128 MHz)
nrfx_clock_divider_set(NRF_CLOCK_DOMAIN_HFCLK, NRF_CLOCK_HFCLK_DIV_1);
#endif
init_leds();
init_button();
k_work_init(&button_press_work, on_button_press);
k_work_init(&event_tx_work, on_event_tx);
k_work_init_delayable(&rx_led_off_work, on_rx_led_off);
k_work_init_delayable(&tx_led_off_work, on_tx_led_off);
/* Register USB MIDI callbacks */
struct usb_midi_cb_t callbacks = {.available_cb = usb_midi_available_cb,
.tx_done_cb = usb_midi_tx_done_cb,
.midi_message_cb = midi_message_cb,
.sysex_data_cb = sysex_data_cb,
.sysex_end_cb = sysex_end_cb,
.sysex_start_cb = sysex_start_cb};
usb_midi_register_callbacks(&callbacks);
/* Init USB */
int enable_rc = usb_enable(NULL);
__ASSERT(enable_rc == 0, "Failed to enable USB");
/* Send MIDI messages periodically */
while (1) {
#ifdef CONFIG_TX_PERIODIC_NOTE_ENABLED
k_work_submit(&event_tx_work);
#endif
k_msleep(CONFIG_TX_PERIODIC_NOTE_INTERVAL_MS);
}
}