partly working USB Device lib for STM32F746NG Discovery both Interface are working

Dependents:   DISCO-F746NG-USB_Device McLighTT project_Keyboard_to_the_Keyboard MIDIInstrumentPADProject ... more

Committer:
DieterGraef
Date:
Sun Jul 31 17:47:35 2016 +0000
Revision:
0:0a2eaa300982
partly working USB Device library - serial and MIDI is working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:0a2eaa300982 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
DieterGraef 0:0a2eaa300982 2 *
DieterGraef 0:0a2eaa300982 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
DieterGraef 0:0a2eaa300982 4 * and associated documentation files (the "Software"), to deal in the Software without
DieterGraef 0:0a2eaa300982 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
DieterGraef 0:0a2eaa300982 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
DieterGraef 0:0a2eaa300982 7 * Software is furnished to do so, subject to the following conditions:
DieterGraef 0:0a2eaa300982 8 *
DieterGraef 0:0a2eaa300982 9 * The above copyright notice and this permission notice shall be included in all copies or
DieterGraef 0:0a2eaa300982 10 * substantial portions of the Software.
DieterGraef 0:0a2eaa300982 11 *
DieterGraef 0:0a2eaa300982 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
DieterGraef 0:0a2eaa300982 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DieterGraef 0:0a2eaa300982 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DieterGraef 0:0a2eaa300982 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DieterGraef 0:0a2eaa300982 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DieterGraef 0:0a2eaa300982 17 */
DieterGraef 0:0a2eaa300982 18
DieterGraef 0:0a2eaa300982 19 #ifndef MIDIMESSAGE_H
DieterGraef 0:0a2eaa300982 20 #define MIDIMESSAGE_H
DieterGraef 0:0a2eaa300982 21
DieterGraef 0:0a2eaa300982 22 #include "mbed.h"
DieterGraef 0:0a2eaa300982 23
DieterGraef 0:0a2eaa300982 24 #define MAX_MIDI_MESSAGE_SIZE 256 // Max message size. SysEx can be up to 65536 but 256 should be fine for most usage
DieterGraef 0:0a2eaa300982 25
DieterGraef 0:0a2eaa300982 26 // MIDI Message Format
DieterGraef 0:0a2eaa300982 27 //
DieterGraef 0:0a2eaa300982 28 // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ]
DieterGraef 0:0a2eaa300982 29 //
DieterGraef 0:0a2eaa300982 30 // MIDI Data Messages (Channel Specific)
DieterGraef 0:0a2eaa300982 31 //
DieterGraef 0:0a2eaa300982 32 // Message msg n m
DieterGraef 0:0a2eaa300982 33 // ---------------------------------------------
DieterGraef 0:0a2eaa300982 34 // Note Off 0x8 Key Velocity
DieterGraef 0:0a2eaa300982 35 // Note On 0x9 Key Velocity
DieterGraef 0:0a2eaa300982 36 // Polyphonic Aftertouch 0xA Key Pressure
DieterGraef 0:0a2eaa300982 37 // Control Change 0xB Controller Value
DieterGraef 0:0a2eaa300982 38 // Program Change 0xC Program -
DieterGraef 0:0a2eaa300982 39 // Channel Aftertouch 0xD Pressure -
DieterGraef 0:0a2eaa300982 40 // Pitch Wheel 0xE LSB MSB
DieterGraef 0:0a2eaa300982 41
DieterGraef 0:0a2eaa300982 42 #define CABLE_NUM (0<<4)
DieterGraef 0:0a2eaa300982 43
DieterGraef 0:0a2eaa300982 44 /** A MIDI message container */
DieterGraef 0:0a2eaa300982 45 class MIDIMessage {
DieterGraef 0:0a2eaa300982 46 public:
DieterGraef 0:0a2eaa300982 47 MIDIMessage() : length(4) {}
DieterGraef 0:0a2eaa300982 48
DieterGraef 0:0a2eaa300982 49 MIDIMessage(uint8_t *buf) : length(4) {
DieterGraef 0:0a2eaa300982 50 for (int i = 0; i < 4; i++)
DieterGraef 0:0a2eaa300982 51 data[i] = buf[i];
DieterGraef 0:0a2eaa300982 52 }
DieterGraef 0:0a2eaa300982 53
DieterGraef 0:0a2eaa300982 54 // New constructor, buf is a true MIDI message (not USBMidi message) and buf_len true message length.
DieterGraef 0:0a2eaa300982 55 MIDIMessage(uint8_t *buf, int buf_len) {
DieterGraef 0:0a2eaa300982 56 length=buf_len+1;
DieterGraef 0:0a2eaa300982 57 // first byte keeped for retro-compatibility
DieterGraef 0:0a2eaa300982 58 data[0]=0;
DieterGraef 0:0a2eaa300982 59
DieterGraef 0:0a2eaa300982 60 for (int i = 0; i < buf_len; i++)
DieterGraef 0:0a2eaa300982 61 data[i+1] = buf[i];
DieterGraef 0:0a2eaa300982 62 }
DieterGraef 0:0a2eaa300982 63
DieterGraef 0:0a2eaa300982 64 // create messages
DieterGraef 0:0a2eaa300982 65
DieterGraef 0:0a2eaa300982 66 /** Create a NoteOff message
DieterGraef 0:0a2eaa300982 67 * @param key Key ID
DieterGraef 0:0a2eaa300982 68 * @param velocity Key velocity (0-127, default = 127)
DieterGraef 0:0a2eaa300982 69 * @param channel Key channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 70 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 71 */
DieterGraef 0:0a2eaa300982 72 static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) {
DieterGraef 0:0a2eaa300982 73 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 74 msg.data[0] = CABLE_NUM | 0x08;
DieterGraef 0:0a2eaa300982 75 msg.data[1] = 0x80 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 76 msg.data[2] = key & 0x7F;
DieterGraef 0:0a2eaa300982 77 msg.data[3] = velocity & 0x7F;
DieterGraef 0:0a2eaa300982 78 return msg;
DieterGraef 0:0a2eaa300982 79 }
DieterGraef 0:0a2eaa300982 80
DieterGraef 0:0a2eaa300982 81 /** Create a NoteOn message
DieterGraef 0:0a2eaa300982 82 * @param key Key ID
DieterGraef 0:0a2eaa300982 83 * @param velocity Key velocity (0-127, default = 127)
DieterGraef 0:0a2eaa300982 84 * @param channel Key channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 85 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 86 */
DieterGraef 0:0a2eaa300982 87 static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) {
DieterGraef 0:0a2eaa300982 88 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 89 msg.data[0] = CABLE_NUM | 0x09;
DieterGraef 0:0a2eaa300982 90 msg.data[1] = 0x90 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 91 msg.data[2] = key & 0x7F;
DieterGraef 0:0a2eaa300982 92 msg.data[3] = velocity & 0x7F;
DieterGraef 0:0a2eaa300982 93 return msg;
DieterGraef 0:0a2eaa300982 94 }
DieterGraef 0:0a2eaa300982 95
DieterGraef 0:0a2eaa300982 96 /** Create a PolyPhonic Aftertouch message
DieterGraef 0:0a2eaa300982 97 * @param key Key ID
DieterGraef 0:0a2eaa300982 98 * @param pressure Aftertouch pressure (0-127)
DieterGraef 0:0a2eaa300982 99 * @param channel Key channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 100 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 101 */
DieterGraef 0:0a2eaa300982 102 static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) {
DieterGraef 0:0a2eaa300982 103 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 104 msg.data[0] = CABLE_NUM | 0x0A;
DieterGraef 0:0a2eaa300982 105 msg.data[1] = 0xA0 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 106 msg.data[2] = key & 0x7F;
DieterGraef 0:0a2eaa300982 107 msg.data[3] = pressure & 0x7F;
DieterGraef 0:0a2eaa300982 108 return msg;
DieterGraef 0:0a2eaa300982 109 }
DieterGraef 0:0a2eaa300982 110
DieterGraef 0:0a2eaa300982 111 /** Create a Control Change message
DieterGraef 0:0a2eaa300982 112 * @param control Controller ID
DieterGraef 0:0a2eaa300982 113 * @param value Controller value (0-127)
DieterGraef 0:0a2eaa300982 114 * @param channel Controller channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 115 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 116 */
DieterGraef 0:0a2eaa300982 117 static MIDIMessage ControlChange(int control, int value, int channel = 0) {
DieterGraef 0:0a2eaa300982 118 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 119 msg.data[0] = CABLE_NUM | 0x0B;
DieterGraef 0:0a2eaa300982 120 msg.data[1] = 0xB0 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 121 msg.data[2] = control & 0x7F;
DieterGraef 0:0a2eaa300982 122 msg.data[3] = value & 0x7F;
DieterGraef 0:0a2eaa300982 123 return msg;
DieterGraef 0:0a2eaa300982 124 }
DieterGraef 0:0a2eaa300982 125
DieterGraef 0:0a2eaa300982 126 /** Create a Program Change message
DieterGraef 0:0a2eaa300982 127 * @param program Program ID
DieterGraef 0:0a2eaa300982 128 * @param channel Channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 129 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 130 */
DieterGraef 0:0a2eaa300982 131 static MIDIMessage ProgramChange(int program, int channel = 0) {
DieterGraef 0:0a2eaa300982 132 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 133 msg.data[0] = CABLE_NUM | 0x0C;
DieterGraef 0:0a2eaa300982 134 msg.data[1] = 0xC0 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 135 msg.data[2] = program & 0x7F;
DieterGraef 0:0a2eaa300982 136 msg.data[3] = 0x00;
DieterGraef 0:0a2eaa300982 137 return msg;
DieterGraef 0:0a2eaa300982 138 }
DieterGraef 0:0a2eaa300982 139
DieterGraef 0:0a2eaa300982 140 /** Create a Channel Aftertouch message
DieterGraef 0:0a2eaa300982 141 * @param pressure Pressure
DieterGraef 0:0a2eaa300982 142 * @param channel Key channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 143 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 144 */
DieterGraef 0:0a2eaa300982 145 static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) {
DieterGraef 0:0a2eaa300982 146 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 147 msg.data[0] = CABLE_NUM | 0x0D;
DieterGraef 0:0a2eaa300982 148 msg.data[1] = 0xD0 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 149 msg.data[2] = pressure & 0x7F;
DieterGraef 0:0a2eaa300982 150 msg.data[3] = 0x00;
DieterGraef 0:0a2eaa300982 151 return msg;
DieterGraef 0:0a2eaa300982 152 }
DieterGraef 0:0a2eaa300982 153
DieterGraef 0:0a2eaa300982 154 /** Create a Pitch Wheel message
DieterGraef 0:0a2eaa300982 155 * @param pitch Pitch (-8192 - 8191, default = 0)
DieterGraef 0:0a2eaa300982 156 * @param channel Channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 157 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 158 */
DieterGraef 0:0a2eaa300982 159 static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) {
DieterGraef 0:0a2eaa300982 160 MIDIMessage msg;
DieterGraef 0:0a2eaa300982 161 int p = pitch + 8192; // 0 - 16383, 8192 is center
DieterGraef 0:0a2eaa300982 162 msg.data[0] = CABLE_NUM | 0x0E;
DieterGraef 0:0a2eaa300982 163 msg.data[1] = 0xE0 | (channel & 0x0F);
DieterGraef 0:0a2eaa300982 164 msg.data[2] = p & 0x7F;
DieterGraef 0:0a2eaa300982 165 msg.data[3] = (p >> 7) & 0x7F;
DieterGraef 0:0a2eaa300982 166 return msg;
DieterGraef 0:0a2eaa300982 167 }
DieterGraef 0:0a2eaa300982 168
DieterGraef 0:0a2eaa300982 169 /** Create an All Notes Off message
DieterGraef 0:0a2eaa300982 170 * @param channel Channel (0-15, default 0)
DieterGraef 0:0a2eaa300982 171 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 172 */
DieterGraef 0:0a2eaa300982 173 static MIDIMessage AllNotesOff(int channel = 0) {
DieterGraef 0:0a2eaa300982 174 return ControlChange(123, 0, channel);
DieterGraef 0:0a2eaa300982 175 }
DieterGraef 0:0a2eaa300982 176
DieterGraef 0:0a2eaa300982 177 /** Create a SysEx message
DieterGraef 0:0a2eaa300982 178 * @param data SysEx data (including 0xF0 .. 0xF7)
DieterGraef 0:0a2eaa300982 179 * @param len SysEx data length
DieterGraef 0:0a2eaa300982 180 * @returns A MIDIMessage
DieterGraef 0:0a2eaa300982 181 */
DieterGraef 0:0a2eaa300982 182 static MIDIMessage SysEx(uint8_t *data, int len) {
DieterGraef 0:0a2eaa300982 183 MIDIMessage msg=MIDIMessage(data,len);
DieterGraef 0:0a2eaa300982 184 return msg;
DieterGraef 0:0a2eaa300982 185 }
DieterGraef 0:0a2eaa300982 186
DieterGraef 0:0a2eaa300982 187 // decode messages
DieterGraef 0:0a2eaa300982 188
DieterGraef 0:0a2eaa300982 189 /** MIDI Message Types */
DieterGraef 0:0a2eaa300982 190 enum MIDIMessageType {
DieterGraef 0:0a2eaa300982 191 ErrorType,
DieterGraef 0:0a2eaa300982 192 NoteOffType,
DieterGraef 0:0a2eaa300982 193 NoteOnType,
DieterGraef 0:0a2eaa300982 194 PolyphonicAftertouchType,
DieterGraef 0:0a2eaa300982 195 ControlChangeType,
DieterGraef 0:0a2eaa300982 196 ProgramChangeType,
DieterGraef 0:0a2eaa300982 197 ChannelAftertouchType,
DieterGraef 0:0a2eaa300982 198 PitchWheelType,
DieterGraef 0:0a2eaa300982 199 AllNotesOffType,
DieterGraef 0:0a2eaa300982 200 SysExType
DieterGraef 0:0a2eaa300982 201 };
DieterGraef 0:0a2eaa300982 202
DieterGraef 0:0a2eaa300982 203 /** Read the message type
DieterGraef 0:0a2eaa300982 204 * @returns MIDIMessageType
DieterGraef 0:0a2eaa300982 205 */
DieterGraef 0:0a2eaa300982 206 MIDIMessageType type() {
DieterGraef 0:0a2eaa300982 207 switch((data[1] >> 4) & 0xF) {
DieterGraef 0:0a2eaa300982 208 case 0x8: return NoteOffType;
DieterGraef 0:0a2eaa300982 209 case 0x9: return NoteOnType;
DieterGraef 0:0a2eaa300982 210 case 0xA: return PolyphonicAftertouchType;
DieterGraef 0:0a2eaa300982 211 case 0xB:
DieterGraef 0:0a2eaa300982 212 if(controller() < 120) { // standard controllers
DieterGraef 0:0a2eaa300982 213 return ControlChangeType;
DieterGraef 0:0a2eaa300982 214 } else if(controller() == 123) {
DieterGraef 0:0a2eaa300982 215 return AllNotesOffType;
DieterGraef 0:0a2eaa300982 216 } else {
DieterGraef 0:0a2eaa300982 217 return ErrorType; // unsupported atm
DieterGraef 0:0a2eaa300982 218 }
DieterGraef 0:0a2eaa300982 219 case 0xC: return ProgramChangeType;
DieterGraef 0:0a2eaa300982 220 case 0xD: return ChannelAftertouchType;
DieterGraef 0:0a2eaa300982 221 case 0xE: return PitchWheelType;
DieterGraef 0:0a2eaa300982 222 case 0xF: return SysExType;
DieterGraef 0:0a2eaa300982 223 default: return ErrorType;
DieterGraef 0:0a2eaa300982 224 }
DieterGraef 0:0a2eaa300982 225 }
DieterGraef 0:0a2eaa300982 226
DieterGraef 0:0a2eaa300982 227 /** Read the channel number */
DieterGraef 0:0a2eaa300982 228 int channel() {
DieterGraef 0:0a2eaa300982 229 return (data[1] & 0x0F);
DieterGraef 0:0a2eaa300982 230 }
DieterGraef 0:0a2eaa300982 231
DieterGraef 0:0a2eaa300982 232 /** Read the key ID */
DieterGraef 0:0a2eaa300982 233 int key() {
DieterGraef 0:0a2eaa300982 234 return (data[2] & 0x7F);
DieterGraef 0:0a2eaa300982 235 }
DieterGraef 0:0a2eaa300982 236
DieterGraef 0:0a2eaa300982 237 /** Read the velocity */
DieterGraef 0:0a2eaa300982 238 int velocity() {
DieterGraef 0:0a2eaa300982 239 return (data[3] & 0x7F);
DieterGraef 0:0a2eaa300982 240 }
DieterGraef 0:0a2eaa300982 241
DieterGraef 0:0a2eaa300982 242 /** Read the controller value */
DieterGraef 0:0a2eaa300982 243 int value() {
DieterGraef 0:0a2eaa300982 244 return (data[3] & 0x7F);
DieterGraef 0:0a2eaa300982 245 }
DieterGraef 0:0a2eaa300982 246
DieterGraef 0:0a2eaa300982 247 /** Read the aftertouch pressure */
DieterGraef 0:0a2eaa300982 248 int pressure() {
DieterGraef 0:0a2eaa300982 249 if(type() == PolyphonicAftertouchType) {
DieterGraef 0:0a2eaa300982 250 return (data[3] & 0x7F);
DieterGraef 0:0a2eaa300982 251 } else {
DieterGraef 0:0a2eaa300982 252 return (data[2] & 0x7F);
DieterGraef 0:0a2eaa300982 253 }
DieterGraef 0:0a2eaa300982 254 }
DieterGraef 0:0a2eaa300982 255
DieterGraef 0:0a2eaa300982 256 /** Read the controller number */
DieterGraef 0:0a2eaa300982 257 int controller() {
DieterGraef 0:0a2eaa300982 258 return (data[2] & 0x7F);
DieterGraef 0:0a2eaa300982 259 }
DieterGraef 0:0a2eaa300982 260
DieterGraef 0:0a2eaa300982 261 /** Read the program number */
DieterGraef 0:0a2eaa300982 262 int program() {
DieterGraef 0:0a2eaa300982 263 return (data[2] & 0x7F);
DieterGraef 0:0a2eaa300982 264 }
DieterGraef 0:0a2eaa300982 265
DieterGraef 0:0a2eaa300982 266 /** Read the pitch value */
DieterGraef 0:0a2eaa300982 267 int pitch() {
DieterGraef 0:0a2eaa300982 268 int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F);
DieterGraef 0:0a2eaa300982 269 return p - 8192; // 0 - 16383, 8192 is center
DieterGraef 0:0a2eaa300982 270 }
DieterGraef 0:0a2eaa300982 271
DieterGraef 0:0a2eaa300982 272 uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];
DieterGraef 0:0a2eaa300982 273 uint8_t length;
DieterGraef 0:0a2eaa300982 274 };
DieterGraef 0:0a2eaa300982 275
DieterGraef 0:0a2eaa300982 276 #endif