USB demo not using the system to do it all. This means all USB defines, includes and other code must be done in our program, it is not done by the system. This demo monitors the USB port for any Debug messages.
- If "hi" is received, it replies with "Hello"
- If "hello" is received, it replies with "G'Day Mate!"
Use the "Netcruzer USB Terminal" app to send and receive USB Debug messages. It can be downloaded here: https://netcruzer.com/usbterminal/
This project is located in the "src/demos/usb/device_HID_demo_no_nzusb" folder of the Netcruzer Download. To compile for Netcruzer Board, open this project in MPLAB X, and select the "Project Configuration" for desired board. For example "SBC66ZL_R1" for the SBC66ZL Revision 1 board. For details click here A common error is "The system cannot find the path specified". This generally means you don't have the required XC16 compiler version installed. Go to "Project Properties", and select your installed XC16 compiler in the "Project Configuration" section.
2013-07-22, 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"
#if defined(USB_IS_CDC)
#include "USB\usb_function_cdc.h"
#elif defined(USB_IS_HID)
#include "USB\usb_function_hid.h"
#endif
void blinkUSBStatus(void);
void usbCBSendResume(void);
void usbInit(void);
void ProcessIO(void);
void LEDTask(void);
USB_HANDLE USBOutHandle = 0;
USB_HANDLE USBInHandle = 0;
BYTE usbState;
WORD sysLedMask;
WORD sysLedPattern;
int main(void)
{
WORD tmrFlashLed = 0;
DIR_SYSLED = OUTPUT_PIN;
sysLedMask = 0x0001;
sysLedPattern = 0x0f0f;
usbInit();
#if defined(USB_INTERRUPT)
USBDeviceAttach();
#endif
while(1)
{
LEDTask();
}
#if defined(USB_POLLING)
USBDeviceTasks();
#endif
ProcessIO();
}
}
void usbInit(void)
{
#if defined(USE_USB_BUS_SENSE_IO)
#endif
#if defined(USE_SELF_POWER_SENSE_IO)
#endif
USBOutHandle = 0;
USBInHandle = 0;
USBDeviceInit();
usbState = IDLE_STATE;
}
void LEDTask(void) {
sysLedMask = sysLedMask << 1;
if (sysLedMask == 0) {
sysLedMask = 0x0001;
}
if ( sysLedPattern & sysLedMask) {
LAT_SYSLED = 1;
}
else {
LAT_SYSLED = 0;
}
}
void ProcessIO(void) {
blinkUSBStatus();
if((USBDeviceState < CONFIGURED_STATE) || USBIsDeviceSuspended()) return;
if(!HIDTxHandleBusy(USBInHandle))
{
if(!HIDRxHandleBusy(USBOutHandle))
{
switch(PacketFromPC.
Command)
{
case CMDUSB_DEVICE_INFO:
PacketToPC.Command = PacketFromPC.Command;
PacketToPC.
DeviceInfo.
BoardID = USBHID_BOARD_ID_MAIN;
PacketToPC.DeviceInfo.
BoardRev = USBHID_BOARD_REV;
USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&PacketToPC.
Contents[0],64);
break;
case CMDUSB_DEBUG_MESSAGE:
if ( (PacketFromPC.
Size==3) && (PacketFromPC.
Data[0]==
'h') && (PacketFromPC.Data[1]==
'i')) {
PacketToPC.Command = PacketFromPC.Command;
PacketToPC.Size = 7;
PacketToPC.Data[0] = '\n';
PacketToPC.Data[1] = 'H';
PacketToPC.Data[2] = 'e';
PacketToPC.Data[3] = 'l';
PacketToPC.Data[4] = 'l';
PacketToPC.Data[5] = 'o';
PacketToPC.Data[6] = 0;
USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&PacketToPC.Contents[0],64);
}
else if (
strcmp(
"hello", (
const char*)PacketFromPC.Data) == 0) {
const char* replyStr = "\nG'Day Mate!";
PacketToPC.Command = CMDUSB_DEBUG_MESSAGE;
PacketToPC.Size =
strlen(replyStr)+1;
strcpy((
char*)PacketToPC.Data, replyStr);
USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&PacketToPC.Contents[0],64);
}
break;
}
USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&PacketFromPC,64);
}
}
}
void blinkUSBStatus(void)
{
if(USBIsDeviceSuspended())
{
}
else
{
if(USBDeviceState == CONFIGURED_STATE)
{
sysLedPattern = 0b0000111110000101;
}
else
{
sysLedPattern = 0b0000011111000001;
}
}
}
void USBCBSuspend(void)
{
#if defined(__C30__)
#endif
}
void USBCBWakeFromSuspend(void)
{
}
void USBCB_SOF_Handler(void)
{
}
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;
}