-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmidi_serial.c
181 lines (162 loc) · 5.35 KB
/
midi_serial.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
/**
* Copyright (C) 2013, 2014 Johannes Taelman, 2020 Piers Titus van der Torren
*
* This file is part of Striso Control.
*
* Striso Control is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Striso Control is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Striso Control. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ch.h"
#include "hal.h"
#include "aux_jack.h"
#include "config.h"
#include "midi.h"
#include "midi_serial.h"
// static unsigned char StatusLengthLookup[16] = {0, 0, 0, 0, 0, 0, 0, 0, 3, // 0x80=note off, 3 bytes
// 3, // 0x90=note on, 3 bytes
// 3, // 0xa0=poly pressure, 3 bytes
// 3, // 0xb0=control change, 3 bytes
// 2, // 0xc0=program change, 2 bytes
// 2, // 0xd0=channel pressure, 2 bytes
// 3, // 0xe0=pitch bend, 3 bytes
// -1 // 0xf0=other things. may vary.
// };
// const signed char SysMsgLengthLookup[16] = {-1, // 0xf0=sysex start. may vary
// 2, // 0xf1=MIDI Time Code. 2 bytes
// 3, // 0xf2=MIDI Song position. 3 bytes
// 2, // 0xf3=MIDI Song Select. 2 bytes.
// 1, // 0xf4=undefined
// 1, // 0xf5=undefined
// 1, // 0xf6=TUNE Request
// 1, // 0xf7=sysex end.
// 1, // 0xf8=timing clock. 1 byte
// 1, // 0xf9=proposed measure end?
// 1, // 0xfa=start. 1 byte
// 1, // 0xfb=continue. 1 byte
// 1, // 0xfc=stop. 1 byte
// 1, // 0xfd=undefined
// 1, // 0xfe=active sensing. 1 byte
// 3 // 0xff= not reset, but a META-EVENT, which is always 3 bytes
// };
// unsigned char MidiByte0;
// unsigned char MidiByte1;
// unsigned char MidiByte2;
// unsigned char MidiCurData;
// unsigned char MidiNumData;
// unsigned char MidiInChannel;
// void serial_MidiInByteHandler(uint8_t data);
// void serial_MidiInByteHandler(uint8_t data) {
// int8_t len;
// if (data & 0x80) {
// len = StatusLengthLookup[data >> 4];
// if (len == -1) {
// len = SysMsgLengthLookup[data - 0xF0];
// if (len == 1) {
// MidiInMsgHandler(MIDI_DEVICE_DIN, 1, data, 0, 0);
// }
// else {
// MidiByte0 = data;
// MidiNumData = len - 1;
// MidiCurData = 0;
// }
// }
// else {
// MidiByte0 = data;
// MidiNumData = len - 1;
// MidiCurData = 0;
// }
// }
// else // not a status byte
// {
// if (MidiCurData == 0) {
// MidiByte1 = data;
// if (MidiNumData == 1) {
// // 2 byte message complete
// MidiInMsgHandler(MIDI_DEVICE_DIN, 1, MidiByte0, MidiByte1, 0);
// MidiCurData = 0;
// }
// else
// MidiCurData++;
// }
// else if (MidiCurData == 1) {
// MidiByte2 = data;
// if (MidiNumData == 2) {
// MidiInMsgHandler(MIDI_DEVICE_DIN, 1, MidiByte0, MidiByte1, MidiByte2);
// MidiCurData = 0;
// }
// }
// }
// }
// Midi OUT
void serial_MidiSend1(uint8_t b0) {
if (config.jack2_mode == JACK2_MODE_MIDI) { // or SDMIDI.state == SD_READY ?
sdPut(&SDMIDI, b0);
}
}
void serial_MidiSend2(uint8_t b0, uint8_t b1) {
if (config.jack2_mode == JACK2_MODE_MIDI) {
unsigned char tx[2];
tx[0] = b0;
tx[1] = b1;
sdWrite(&SDMIDI, tx, 2);
}
}
void serial_MidiSend3(uint8_t b0, uint8_t b1, uint8_t b2) {
if (config.jack2_mode == JACK2_MODE_MIDI) {
unsigned char tx[3];
tx[0] = b0;
tx[1] = b1;
tx[2] = b2;
sdWrite(&SDMIDI, tx, 3);
}
}
int serial_MidiGetOutputBufferPending(void) {
return oqGetFullI(&SDMIDI.oqueue);
}
// Midi UART...
static const SerialConfig sdMidiCfg = {31250, // baud
0, 0, 0};
// static THD_WORKING_AREA(waThreadMidiIn, 256);
// static THD_FUNCTION(ThreadMidiIn, arg) {
// (void)arg;
// #if CH_USE_REGISTRY
// chRegSetThreadName("midi");
// #endif
// while (1) {
// char ch;
// ch = sdGet(&SDMIDI);
// serial_MidiInByteHandler(ch);
// }
// }
void serial_midi_enable(void) {
/*
* Activates the serial driver 2 using the driver default configuration.
* PA2(TX) and PA3(RX) are routed to USART2.
*/
// RX
// palSetLineMode(LINE_AUX_UART2_RX, PAL_MODE_ALTERNATE(7));
// TX, use open drain to be safe on short-circuit
palSetLineMode(LINE_AUX_UART2_TX, PAL_MODE_ALTERNATE(7) | PAL_STM32_OTYPE_OPENDRAIN);
// palSetLineMode(LINE_AUX_UART2_TX, PAL_MODE_ALTERNATE(7));
// Enable AUX power on ring
palSetLineMode(LINE_AUX_VDD, PAL_MODE_OUTPUT_OPENDRAIN);
aux_power_enable();
if (SDMIDI.state != SD_READY) {
sdStart(&SDMIDI, &sdMidiCfg);
// chThdCreateStatic(waThreadMidiIn, sizeof(waThreadMidi), NORMALPRIO, ThreadMidi,
// NULL);
}
}
void serial_midi_disable(void) {
palSetLineMode(LINE_AUX_UART2_TX, PAL_MODE_INPUT_ANALOG);
aux_power_disable();
}