Skip to content

Commit 9ee9532

Browse files
gtrainaviciuspelwell
authored andcommitted
Pisound: MIDI communication fixes for scaled down CPU.
* Increased maximum SPI communication speed to avoid running too slow when the CPU is scaled down and losing MIDI data. * Keep track of buffer usage in millibytes for higher precision. Signed-off-by: Giedrius Trainavičius <[email protected]>
1 parent c092773 commit 9ee9532

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

sound/soc/bcm/pisound.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Pisound Linux kernel module.
3-
* Copyright (C) 2016-2019 Vilniaus Blokas UAB, https://blokas.io/pisound
3+
* Copyright (C) 2016-2020 Vilniaus Blokas UAB, https://blokas.io/pisound
44
*
55
* This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License
@@ -326,7 +326,7 @@ static void spi_transfer(const uint8_t *txbuf, uint8_t *rxbuf, int len)
326326
transfer.tx_buf = txbuf;
327327
transfer.rx_buf = rxbuf;
328328
transfer.len = len;
329-
transfer.speed_hz = 100000;
329+
transfer.speed_hz = 150000;
330330
transfer.delay_usecs = 10;
331331
spi_message_add_tail(&transfer, &msg);
332332

@@ -403,9 +403,9 @@ static struct spi_device *pisnd_spi_find_device(void)
403403
static void pisnd_work_handler(struct work_struct *work)
404404
{
405405
enum { TRANSFER_SIZE = 4 };
406-
enum { PISOUND_OUTPUT_BUFFER_SIZE = 128 };
407-
enum { MIDI_BYTES_PER_SECOND = 3125 };
408-
int out_buffer_used = 0;
406+
enum { PISOUND_OUTPUT_BUFFER_SIZE_MILLIBYTES = 127 * 1000 };
407+
enum { MIDI_MILLIBYTES_PER_JIFFIE = (3125 * 1000) / HZ };
408+
int out_buffer_used_millibytes = 0;
409409
unsigned long now;
410410
uint8_t val;
411411
uint8_t txbuf[TRANSFER_SIZE];
@@ -445,7 +445,9 @@ static void pisnd_work_handler(struct work_struct *work)
445445
had_data = false;
446446
memset(txbuf, 0, sizeof(txbuf));
447447
for (i = 0; i < sizeof(txbuf) &&
448-
out_buffer_used < PISOUND_OUTPUT_BUFFER_SIZE;
448+
((out_buffer_used_millibytes+1000 <
449+
PISOUND_OUTPUT_BUFFER_SIZE_MILLIBYTES) ||
450+
g_ledFlashDurationChanged);
449451
i += 2) {
450452

451453
val = 0;
@@ -458,7 +460,7 @@ static void pisnd_work_handler(struct work_struct *work)
458460
} else if (kfifo_get(&spi_fifo_out, &val)) {
459461
txbuf[i+0] = 0x0f;
460462
txbuf[i+1] = val;
461-
++out_buffer_used;
463+
out_buffer_used_millibytes += 1000;
462464
}
463465
}
464466

@@ -469,12 +471,14 @@ static void pisnd_work_handler(struct work_struct *work)
469471
* rate.
470472
*/
471473
now = jiffies;
472-
out_buffer_used -=
473-
(MIDI_BYTES_PER_SECOND / HZ) /
474-
(now - last_transfer_at);
475-
if (out_buffer_used < 0)
476-
out_buffer_used = 0;
477-
last_transfer_at = now;
474+
if (now != last_transfer_at) {
475+
out_buffer_used_millibytes -=
476+
(now - last_transfer_at) *
477+
MIDI_MILLIBYTES_PER_JIFFIE;
478+
if (out_buffer_used_millibytes < 0)
479+
out_buffer_used_millibytes = 0;
480+
last_transfer_at = now;
481+
}
478482

479483
for (i = 0; i < sizeof(rxbuf); i += 2) {
480484
if (rxbuf[i]) {
@@ -489,6 +493,7 @@ static void pisnd_work_handler(struct work_struct *work)
489493
|| !kfifo_is_empty(&spi_fifo_out)
490494
|| pisnd_spi_has_more()
491495
|| g_ledFlashDurationChanged
496+
|| out_buffer_used_millibytes != 0
492497
);
493498

494499
if (!kfifo_is_empty(&spi_fifo_in) && g_recvCallback)

0 commit comments

Comments
 (0)