This demo uses the selected hardware platform as a USB MIDI device, and supports a serial LCD display (LCD2S type) with keypad. 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 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
#define LCD2S_I2C_ADR 80 // Default LCD I2C address when both switches on back of LCD2S board are set to 0
#define CMD_GET_STATUS 0xD0
#define CMD_GET_KEY 0xD1
#define CMD_GET_GPIO 0xD2
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;
unsigned char lcdFreeBuf = 50;
#define RXBUF_LCD_BUTTON_SIZE 16
#define CIRBUF_RX_LCD_BUTTON (&cbufRxLcdButton)
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)
{
unsigned char c;
char buf[50];
WORD readDly = 0xffff;
InitializeSystem();
#if defined(USB_INTERRUPT)
USBDeviceAttach();
#else
#error "This project must have USB_INTERRUPT defined. Does NOT support polling!"
#endif
while(1)
{
if (--readDly == 0) {
if (i2c1ReadSlaveReg(LCD2S_I2C_ADR, CMD_GET_STATUS, &c, 1) == 0) {
lcdFreeBuf = c & 0x7f;
if (lcdFreeBuf < 4) {
}
if (c & 0x80) {
i2c1ReadSlaveReg(LCD2S_I2C_ADR, CMD_GET_KEY, &c, 1);
}
}
}
}
i2c1WriteMessage(LCD2S_I2C_ADR, buf, sizeof(buf));
}
ProcessIO();
}
}
static void InitializeSystem(void)
{
WORD delay;
static BYTE rxbufLcdButton[RXBUF_LCD_BUTTON_SIZE];
cbufRxLcdButton.
flagVal = 0;
cbufRxLcdButton.
maxOffset = RXBUF_LCD_BUTTON_SIZE - 1;
cbufRxLcdButton.
put = cbufRxLcdButton.
get = 0;
cbufRxLcdButton.
buf = rxbufLcdButton;
delay = 30000;
while (delay-- != 0);
{
BYTE confCmdArr[] = {0x95, 0x39, 0x73, 0x0F, 0x6A};
if (i2c1WriteMessage(LCD2S_I2C_ADR, confCmdArr, sizeof(confCmdArr)) != 0) {
}
else {
}
}
#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;
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)
{
}
void USBCBStdSetDscHandler(void)
{
}
void USBCBInitEP(void)
{
USBEnableEndpoint(MIDI_EP,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
USBRxHandle = USBRxOnePacket(MIDI_EP,(BYTE*)&ReceivedDataBuffer,64);
}
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();
}
}
}
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;
}