This demo uses the selected hardware platform as a USB MIDI device. Connect the device to the computer. Open a MIDI recording software package. Each MIDI recording software interface is different so the following instructions may not apply the to software package you are using. Please refer to the user’s manual for the software package you are using for more details of how to configure that tool for a USB MIDI input. In this demo each time you press the button on the board, it will cycle through a series of notes. The port that button is connected to is Netcruzer Port 5 (old port name X5). The button must short port 5 to ground when pressed. To change port, change following line in HardwareProfile.h: "#define sw2 PIN_05"
2012-08-08, David H. (DH):
#define THIS_IS_MAIN_FILE //Uniquely identifies this as the file with the main application entry function main()
#include "HardwareProfile.h"
#include "./USB/usb.h"
#include "./USB/usb_function_midi.h"
#define RX_BUFFER_ADDRESS_TAG
#define TX_BUFFER_ADDRESS_TAG
#define MIDI_EVENT_ADDRESS_TAG
unsigned char ReceivedDataBuffer[64] RX_BUFFER_ADDRESS_TAG;
unsigned char ToSendDataBuffer[64] TX_BUFFER_ADDRESS_TAG;
USB_AUDIO_MIDI_EVENT_PACKET midiData MIDI_EVENT_ADDRESS_TAG;
USB_HANDLE USBTxHandle = 0;
USB_HANDLE USBRxHandle = 0;
USB_VOLATILE BYTE msCounter;
void blinkUSBStatus(void);
BOOL Switch2IsPressed(void);
BOOL Switch3IsPressed(void);
static void InitializeSystem(void);
void ProcessIO(void);
void UserInit(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void usbCBSendResume(void);
WORD_VAL ReadPOT(void);
int main(void)
{
InitializeSystem();
#if defined(USB_INTERRUPT)
USBDeviceAttach();
#endif
while(1)
{
#if defined(USB_POLLING)
USBDeviceTasks();
#endif
ProcessIO();
}
}
static void InitializeSystem(void)
{
#if defined(USE_USB_BUS_SENSE_IO)
#endif
#if defined(USE_SELF_POWER_SENSE_IO)
#endif
UserInit();
USBDeviceInit();
}
void UserInit(void)
{
mInitAllLEDs();
mInitAllSwitches();
}
void ProcessIO(void)
{
static BYTE pitch = 0x3C;
static BOOL sentNoteOff = TRUE;
blinkUSBStatus();
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
if(!USBHandleBusy(USBRxHandle))
{
USBRxHandle = USBRxOnePacket(MIDI_EP,(BYTE*)&ReceivedDataBuffer,64);
}
if(sw2==0)
{
if(msCounter == 0)
{
if(sentNoteOff == TRUE)
{
if(!USBHandleBusy(USBTxHandle))
{
msCounter = 100;
midiData.Val = 0;
midiData.CableNumber = 0;
midiData.CodeIndexNumber = MIDI_CIN_NOTE_ON;
midiData.DATA_0 = 0x90;
midiData.DATA_1 = pitch;
midiData.DATA_2 = 0x7F;
USBTxHandle = USBTxOnePacket(MIDI_EP,(BYTE*)&midiData,4);
sentNoteOff = FALSE;
}
}
}
}
else
{
if(msCounter == 0)
{
if(sentNoteOff == FALSE)
{
if(!USBHandleBusy(USBTxHandle))
{
msCounter = 100;
midiData.Val = 0;
midiData.CableNumber = 0;
midiData.CodeIndexNumber = MIDI_CIN_NOTE_ON;
midiData.DATA_0 = 0x90;
midiData.DATA_1 = pitch++;
midiData.DATA_2 = 0x00;
if(pitch == 0x49)
{
pitch = 0x3C;
}
USBTxHandle = USBTxOnePacket(MIDI_EP,(BYTE*)&midiData,4);
sentNoteOff = TRUE;
}
}
}
}
}
void blinkUSBStatus(void)
{
static WORD led_count=0;
if(led_count == 0)led_count = 64000U;
led_count--;
#define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();}
#define mLED_Both_On() {mLED_1_On();mLED_2_On();}
#define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();}
#define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();}
if(USBSuspendControl == 1)
{
if(led_count==0)
{
mLED_1_Toggle();
if(mGetLED_1())
{
mLED_2_On();
}
else
{
mLED_2_Off();
}
}
}
else
{
if(USBDeviceState == DETACHED_STATE)
{
mLED_Both_Off();
}
else if(USBDeviceState == ATTACHED_STATE)
{
mLED_Both_On();
}
else if(USBDeviceState == POWERED_STATE)
{
mLED_Only_1_On();
}
else if(USBDeviceState == DEFAULT_STATE)
{
mLED_Only_2_On();
}
else if(USBDeviceState == ADDRESS_STATE)
{
if(led_count == 0)
{
mLED_1_Toggle();
mLED_2_Off();
}
}
else if(USBDeviceState == CONFIGURED_STATE)
{
if(led_count==0)
{
mLED_1_Toggle();
if(mGetLED_1())
{
mLED_2_Off();
}
else
{
mLED_2_On();
}
}
}
}
}
void USBCBSuspend(void)
{
}
void USBCBWakeFromSuspend(void)
{
}
void USBCB_SOF_Handler(void)
{
if(msCounter != 0)
{
msCounter--;
}
}
void USBCBErrorHandler(void) {
}
void USBCBCheckOtherReq(void) {
#if defined(USB_IS_CDC)
USBCheckCDCRequest();
#elif defined(USB_IS_HID)
USBCheckHIDRequest();
#endif
}
void USBCBStdSetDscHandler(void) {
}
void USBCBInitEP(void) {
#if defined(USB_IS_CDC)
CDCInitEP();
#elif defined(USB_IS_HID)
USBEnableEndpoint(HID_EP,USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&PacketFromPC,64);
#endif
}
void usbCBSendResume(void) {
static WORD delay_count;
if (USBGetRemoteWakeupStatus() == TRUE) {
if (USBIsBusSuspended() == TRUE) {
USBMaskInterrupts();
USBCBWakeFromSuspend();
USBSuspendControl = 0;
USBBusIsSuspended = FALSE;
delay_count = 3600U;
do {
delay_count--;
} while (delay_count);
USBResumeControl = 1;
delay_count = 1800U;
do {
delay_count--;
} while (delay_count);
USBResumeControl = 0;
USBUnmaskInterrupts();
}
}
}
#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void) {
}
#endif
BOOL USER_USB_CALLBACK_EVENT_HANDLER(int event, void *pdata, WORD size)
{
switch( event )
{
case EVENT_TRANSFER:
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER_TERMINATED:
break;
default:
break;
}
return TRUE;
}