1 From 5b12b655f266ea29e91a7b7a46385df05bb70ed8 Mon Sep 17 00:00:00 2001
2 From: Giedrius <giedrius@blokas.io>
3 Date: Tue, 7 Jan 2020 11:04:21 +0200
4 Subject: [PATCH] Pisound: MIDI communication fixes for scaled down
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 * Increased maximum SPI communication speed to avoid running too slow
11 when the CPU is scaled down and losing MIDI data.
13 * Keep track of buffer usage in millibytes for higher precision.
15 Signed-off-by: Giedrius Trainavičius <giedrius@blokas.io>
17 sound/soc/bcm/pisound.c | 31 ++++++++++++++++++-------------
18 1 file changed, 18 insertions(+), 13 deletions(-)
20 --- a/sound/soc/bcm/pisound.c
21 +++ b/sound/soc/bcm/pisound.c
24 * Pisound Linux kernel module.
25 - * Copyright (C) 2016-2019 Vilniaus Blokas UAB, https://blokas.io/pisound
26 + * Copyright (C) 2016-2020 Vilniaus Blokas UAB, https://blokas.io/pisound
28 * This program is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU General Public License
30 @@ -326,7 +326,7 @@ static void spi_transfer(const uint8_t *
31 transfer.tx_buf = txbuf;
32 transfer.rx_buf = rxbuf;
34 - transfer.speed_hz = 100000;
35 + transfer.speed_hz = 150000;
36 transfer.delay_usecs = 10;
37 spi_message_add_tail(&transfer, &msg);
39 @@ -403,9 +403,9 @@ static struct spi_device *pisnd_spi_find
40 static void pisnd_work_handler(struct work_struct *work)
42 enum { TRANSFER_SIZE = 4 };
43 - enum { PISOUND_OUTPUT_BUFFER_SIZE = 128 };
44 - enum { MIDI_BYTES_PER_SECOND = 3125 };
45 - int out_buffer_used = 0;
46 + enum { PISOUND_OUTPUT_BUFFER_SIZE_MILLIBYTES = 127 * 1000 };
47 + enum { MIDI_MILLIBYTES_PER_JIFFIE = (3125 * 1000) / HZ };
48 + int out_buffer_used_millibytes = 0;
51 uint8_t txbuf[TRANSFER_SIZE];
52 @@ -445,7 +445,9 @@ static void pisnd_work_handler(struct wo
54 memset(txbuf, 0, sizeof(txbuf));
55 for (i = 0; i < sizeof(txbuf) &&
56 - out_buffer_used < PISOUND_OUTPUT_BUFFER_SIZE;
57 + ((out_buffer_used_millibytes+1000 <
58 + PISOUND_OUTPUT_BUFFER_SIZE_MILLIBYTES) ||
59 + g_ledFlashDurationChanged);
63 @@ -458,7 +460,7 @@ static void pisnd_work_handler(struct wo
64 } else if (kfifo_get(&spi_fifo_out, &val)) {
68 + out_buffer_used_millibytes += 1000;
72 @@ -469,12 +471,14 @@ static void pisnd_work_handler(struct wo
77 - (MIDI_BYTES_PER_SECOND / HZ) /
78 - (now - last_transfer_at);
79 - if (out_buffer_used < 0)
80 - out_buffer_used = 0;
81 - last_transfer_at = now;
82 + if (now != last_transfer_at) {
83 + out_buffer_used_millibytes -=
84 + (now - last_transfer_at) *
85 + MIDI_MILLIBYTES_PER_JIFFIE;
86 + if (out_buffer_used_millibytes < 0)
87 + out_buffer_used_millibytes = 0;
88 + last_transfer_at = now;
91 for (i = 0; i < sizeof(rxbuf); i += 2) {
93 @@ -489,6 +493,7 @@ static void pisnd_work_handler(struct wo
94 || !kfifo_is_empty(&spi_fifo_out)
95 || pisnd_spi_has_more()
96 || g_ledFlashDurationChanged
97 + || out_buffer_used_millibytes != 0
100 if (!kfifo_is_empty(&spi_fifo_in) && g_recvCallback)