-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_dev_midi.h
137 lines (110 loc) · 3.81 KB
/
usb_dev_midi.h
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
#ifndef USB_DEV_MIDI_H_
#define USB_DEV_MIDI_H_
#include <stdint.h>
#include <stdbool.h>
#include <usbmidi_types.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "driverlib/qei.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "usblib/usblib.h"
#include "usblib/device/usbdevice.h"
#include "utils/uartstdio.h"
#include "utils/ustdlib.h"
#include "midi.h"
#include "usbmidi.h"
#define SYSTICKS_PER_SECOND 100
#define SYSTICK_PERIOD_MS (1000 / SYSTICKS_PER_SECOND)
uint32_t g_ui32SysTickCount;
uint32_t g_ui32SysClock;
USBMIDI_Message_t txmsg;
USBMIDI_Message_t rxmsg;
void SysTickIntHandler(void) {
g_ui32SysTickCount++;
}
void Initialize(void) {
// Enable PORTF
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)){}
HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE+GPIO_O_CR) |= GPIO_PIN_0;
// Enable PORTD
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOD)){}
HWREG(GPIO_PORTD_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTD_BASE+GPIO_O_CR) |= GPIO_PIN_7;
// Configure GPIO for LEDS
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
// Configure GPIO for Buttons
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_0);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
// Configure USBAnalog
MAP_GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
// Get SysClock
g_ui32SysClock = MAP_SysCtlClockGet();
// Enable system tick
MAP_SysTickPeriodSet(g_ui32SysClock / SYSTICKS_PER_SECOND);
MAP_SysTickIntEnable();
MAP_SysTickEnable();
}
void ConfigureUART0(void) {
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
UARTStdioConfig(0, 115200, 16000000);
}
void noteOn(short channel, uint8_t note, uint8_t velocity) {
txmsg.header = USB_MIDI_HEADER(1, USB_MIDI_CIN_NOTEON);
txmsg.byte1 = MIDI_MSG_NOTEON | channel;
txmsg.byte2 = note;
txmsg.byte3 = velocity;
USBMIDI_InEpMsgWrite(&txmsg);
}
void noteOff(short channel, uint8_t note, uint8_t velocity) {
txmsg.header = USB_MIDI_HEADER(1, USB_MIDI_CIN_NOTEOFF);
txmsg.byte1 = MIDI_MSG_NOTEOFF | channel;
txmsg.byte2 = note;
txmsg.byte3 = velocity;
USBMIDI_InEpMsgWrite(&txmsg);
}
void controlChange(short channel, uint8_t key, uint8_t value) {
txmsg.header = USB_MIDI_HEADER(1, USB_MIDI_CIN_CTRLCHANGE);
txmsg.byte1 = MIDI_MSG_CTRLCHANGE | channel;
txmsg.byte2 = key;
txmsg.byte3 = value;
USBMIDI_InEpMsgWrite(&txmsg);
}
void pitchBend(short channel, uint16_t pitch) {
txmsg.header = USB_MIDI_HEADER(1, USB_MIDI_CIN_PITCHBEND);
txmsg.byte1 = MIDI_MSG_PITCHBEND | channel;
txmsg.byte2 = pitch & 0x7F; // lsb
txmsg.byte3 = (pitch >> 7) & 0x7F; // msb
USBMIDI_InEpMsgWrite(&txmsg);
}
void MIDI_USB_Rx_Task(void) {
while(USBMIDI_OutEpFIFO_Pop(&rxmsg)) {
UARTprintf("%02x : %02x : %02x : %02x\n", rxmsg.header, rxmsg.byte1, rxmsg.byte2, rxmsg.byte3);
}
}
void MIDI_USB_Loop_Task(void) {
while(USBMIDI_OutEpFIFO_Pop(&rxmsg)) {
if(rxmsg.header!=0) {
USBMIDI_InEpMsgWrite(&rxmsg);
MAP_SysCtlDelay(10000);
}
}
}
#endif