PK } } / USB Performance Meter\USB Performance Meter.mcp[HEADER]
magic_cookie={66E99B07-E706-4689-9E80-9B2582898A13}
file_version=1.0
device=PIC18F2550
[PATH_INFO]
BuildDirPolicy=BuildDirIsProjectDir
dir_src=
dir_bin=
dir_tmp=
dir_sin=
dir_inc=
dir_lib=
dir_lkr=
[CAT_FILTERS]
filter_src=*.c;*.as;*.asm;*.usb
filter_inc=*.h;
filter_obj=*.obj;*.p1;*.hex
filter_lib=*.lib;*.lpp
filter_lkr=*.unknown
[CAT_SUBFOLDERS]
subfolder_src=USB Stack
subfolder_inc=USB Stack
subfolder_obj=
subfolder_lib=
subfolder_lkr=
[FILE_SUBFOLDERS]
file_000=.
file_001=USB Stack
file_002=USB Stack
file_003=.
file_004=.
file_005=.
file_006=.
file_007=USB Stack
file_008=USB Stack
file_009=USB Stack
file_010=USB Stack
file_011=USB Stack
file_012=USB Stack
file_013=USB Stack
file_014=USB Stack
file_015=.
file_016=.
file_017=USB Stack
[GENERATED_FILES]
file_000=no
file_001=no
file_002=no
file_003=no
file_004=no
file_005=no
file_006=no
file_007=no
file_008=no
file_009=no
file_010=no
file_011=no
file_012=no
file_013=no
file_014=no
file_015=no
file_016=no
file_017=no
[OTHER_FILES]
file_000=no
file_001=no
file_002=no
file_003=no
file_004=no
file_005=no
file_006=no
file_007=no
file_008=no
file_009=no
file_010=no
file_011=no
file_012=no
file_013=no
file_014=no
file_015=no
file_016=no
file_017=no
[FILE_INFO]
file_000=main.c
file_001=usb_device.c
file_002=usb_function_hid.c
file_003=usbcallback.c
file_004=usb_descriptors.c
file_005=genericHID.h
file_006=HardwareProfile.h
file_007=Compiler.h
file_008=GenericTypeDefs.h
file_009=usb.h
file_010=usb_ch9.h
file_011=usb_common.h
file_012=usb_device.h
file_013=usb_hal.h
file_014=usb_hal_pic18.h
file_015=usb_config.h
file_016=usbcallback.h
file_017=usb_function_hid.h
[SUITE_INFO]
suite_guid={507D93FD-16F1-4270-980F-0C7C0207E6D3}
suite_state=
[TOOL_SETTINGS]
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}=C9=3,2
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}000=
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}001=
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}002=
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}003=
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}004=
[ACTIVE_FILE_SETTINGS]
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}000_active=yes
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}001_active=yes
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}002_active=yes
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}003_active=yes
TS{3FF1D5F2-E530-4850-9F70-F61D55BD1AC9}004_active=yes
[INSTRUMENTED_TRACE]
enable=0
transport=0
format=0
[CUSTOM_BUILD]
Pre-Build=
Pre-BuildEnabled=1
Post-Build=
Post-BuildEnabled=1
PK a= = USB Performance Meter\main.c/************************************************************************
main.c
USB Performance Monitor - PIC18F2550 firmware
Copyright (C) 2010 Simon Inns
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Email: simon.inns@gmail.com
************************************************************************/
#ifndef MAIN_C
#define MAIN_C
// Global includes
#include
#include
// Local includes
#include "usb.h"
#include "HardwareProfile.h"
#include "usb_function_hid.h"
#include "genericHID.h"
// PIC 18F2550 fuse configuration:
// Config word 1 (Oscillator configuration)
// 20Mhz crystal input scaled to 48Mhz and configured for USB operation
__CONFIG(1, USBPLL & IESODIS & FCMDIS & HSPLL & CPUDIV1 & PLLDIV5);
// Config word 2
__CONFIG(2, VREGEN & PWRTDIS & BOREN & BORV20 & WDTDIS & WDTPS32K);
// Config word 3
__CONFIG(3, PBDIGITAL & LPT1DIS & MCLRDIS);
// Config word 4
__CONFIG(4, XINSTDIS & STVREN & LVPDIS & ICPORTDIS & DEBUGDIS);
// Config word 5, 6 and 7 (protection configuration)
__CONFIG(5, UNPROTECT);
__CONFIG(6, UNPROTECT);
__CONFIG(7, UNPROTECT);
// Globals for RGB LED PWM ----------------------------------------------------
int pwmCounter = 0;
int grnActualBrightness = 0;
int bluActualBrightness = 0;
int redActualBrightness = 0;
int grnTargetBrightness = 0;
int bluTargetBrightness = 0;
int redTargetBrightness = 0;
int grnFadeSpeed = 0;
int bluFadeSpeed = 0;
int redFadeSpeed = 0;
int grnFadeCounter = 0;
int bluFadeCounter = 0;
int redFadeCounter = 0;
// Globals for meter control --------------------------------------------------
int meter1ActualValue = 0;
int meter1TargetValue = 0;
int meter1DropRate = 0;
int meter1DropCounter = 0;
int meter2ActualValue = 0;
int meter2TargetValue = 0;
int meter2DropRate = 0;
int meter2DropCounter = 0;
// Globals for device settings
int ledFadeOnRateSetting = 100;
int ledFadeOffRateSetting = 200;
int meter1SensitivitySetting = 100;
int meter2SensitivitySetting = 100;
// Globals for hardware button
int buttonPressed = 0; // 0 = not pressed, 1 = pressed
int buttonDebounceCounter = 0;
// Globals for soft USB detach function
int softUsbDetach = 0; // 0 = false (attached), 1 = true (detached)
// Low priority interrupt procedure
void interrupt low_priority lpHandler(void)
{
// Is this timer0 interrupting?
if (TMR0IF)
{
// LED Control --------------------------------------------------------
// Perform the PWM brightness control
if (grnActualBrightness > pwmCounter) GRN = 0; else GRN = 1;
if (bluActualBrightness > pwmCounter) BLU = 0; else BLU = 1;
if (redActualBrightness > pwmCounter) RED = 0; else RED = 1;
pwmCounter++;
if (pwmCounter > 127) pwmCounter = 0;
// Perform fading control (Fading for each colour is independent and
// supports fading up and down as well as an immediate change
// Green LED control
if (grnTargetBrightness != grnActualBrightness)
{
// If fade is off then just set the brightness
if (grnFadeSpeed == -1) grnActualBrightness = grnTargetBrightness;
else
{
grnFadeCounter++;
if (grnFadeCounter == grnFadeSpeed)
{
if (grnTargetBrightness > grnActualBrightness) grnActualBrightness++;
else grnActualBrightness--;
grnFadeCounter = 0;
}
}
}
// Blue LED control
if (bluTargetBrightness != bluActualBrightness)
{
// If fade is off then just set the brightness
if (bluFadeSpeed == -1) bluActualBrightness = bluTargetBrightness;
else
{
bluFadeCounter++;
if (bluFadeCounter == bluFadeSpeed)
{
if (bluTargetBrightness > bluActualBrightness) bluActualBrightness++;
else bluActualBrightness--;
bluFadeCounter = 0;
}
}
}
// Red LED control
if (redTargetBrightness != redActualBrightness)
{
// If fade is off then just set the brightness
if (redFadeSpeed == -1) redActualBrightness = redTargetBrightness;
else
{
redFadeCounter++;
if (redFadeCounter == redFadeSpeed)
{
if (redTargetBrightness > redActualBrightness) redActualBrightness++;
else redActualBrightness--;
redFadeCounter = 0;
}
}
}
// Meter control ------------------------------------------------------
// Meter 1 (CPU)
if ((meter1TargetValue > meter1ActualValue) || meter1DropRate == 0)
meter1ActualValue = meter1TargetValue;
else
{
meter1DropCounter++;
if (meter1DropCounter == meter1DropRate)
{
meter1ActualValue-=2;
if(meter1ActualValue < meter1TargetValue) meter1ActualValue = meter1TargetValue;
meter1DropCounter = 0;
}
}
// Meter 2 (HDD)
if ((meter2TargetValue > meter2ActualValue) || meter2DropRate == 0)
meter2ActualValue = meter2TargetValue;
else
{
meter2DropCounter++;
if (meter2DropCounter == meter2DropRate)
{
meter2ActualValue-=2;
if(meter2ActualValue < meter2TargetValue) meter2ActualValue = meter2TargetValue;
meter2DropCounter = 0;
}
}
// Set the PWM generation for the meters
CCPR1L = meter2ActualValue;
CCPR2L = meter1ActualValue;
// Get ready for the next interrupt -----------------------------------
TMR0L = 255 - 98; // Reset the timer0 counter
TMR0IF = 0; // Clear the timer0 interrupt flag
}
}
// Set the RGB LED colour (input is 0-255) and fade time in milliseconds
void setRgbLedColour(unsigned char red, unsigned char green, unsigned char blue)
{
// Here we specify the minimum intensity (if we allow the intensity to be too low
// there aren't enough brightness levels to fade smoothly
if (red != 0 && red < 20) red = 20;
if (green != 0 && green < 20) green = 20;
if (blue != 0 && blue < 20) blue = 20;
// Scale the intensities to a range of 0-127 and normalise the brightness output
float redScale = (47.0 * (float)red / 71.0) / 2.0;
float greenScale = ((float)green) / 2.0;
float blueScale = (47.0 * (float)blue / 64.0) / 2.0;
// If the value are the same as currently set, just return
if ( ((int)redScale == redTargetBrightness) &&
((int)blueScale == bluTargetBrightness) &&
((int)greenScale == grnTargetBrightness)) return;
int fadeTime;
if (red == 0 && blue == 0 && green ==0) fadeTime = ledFadeOffRateSetting;
else fadeTime = ledFadeOnRateSetting;
// Calculate the difference between the current colour and the new colour
// this ensures the fade speed is the same no matter the difference in brightness levels
// between the three colours
float redFadeSpeedFloat =
1 / (((float)redTargetBrightness - redScale) / (((float)fadeTime / 130.0) * 1000.0));
float grnFadeSpeedFloat =
1 / (((float)grnTargetBrightness - greenScale) / (((float)fadeTime / 130.0) * 1000.0));
float bluFadeSpeedFloat =
1 / (((float)bluTargetBrightness - blueScale) / (((float)fadeTime / 130.0) * 1000.0));
// Set the fade speed as an integer value
redFadeSpeed = (int)redFadeSpeedFloat;
grnFadeSpeed = (int)grnFadeSpeedFloat;
bluFadeSpeed = (int)bluFadeSpeedFloat;
// Ensure that the fade speed increment is always positive
if (redFadeSpeed < 0) redFadeSpeed = -redFadeSpeed;
if (grnFadeSpeed < 0) grnFadeSpeed = -grnFadeSpeed;
if (bluFadeSpeed < 0) bluFadeSpeed = -bluFadeSpeed;
// If the fade speed is too fast we set it to -1 which tells the
// interrupt routine to change brightness immediately
if (redFadeSpeed == 0) redFadeSpeed = -1;
if (grnFadeSpeed == 0) grnFadeSpeed = -1;
if (bluFadeSpeed == 0) bluFadeSpeed = -1;
// Set the target brightness levels
redTargetBrightness = (int)redScale;
grnTargetBrightness = (int)greenScale;
bluTargetBrightness = (int)blueScale;
redFadeCounter = 0;
grnFadeCounter = 0;
bluFadeCounter = 0;
}
// Set the meter values (range 0-100%) and the meter sensitivity (0-100%)
void setMeter(unsigned char meter1Percent, unsigned char meter2Percent,
unsigned char meter1Sensitivity, unsigned char meter2Sensitivity)
{
double meter1;
double meter2;
double meter1position;
double meter2position;
// Set the meter drop-out rates ('sensitivity')
if (meter1Sensitivity > 100) meter1Sensitivity = 100;
if (meter2Sensitivity > 100) meter2Sensitivity = 100;
meter1DropRate = (100 - meter1Sensitivity) / 2;
meter2DropRate = (100 - meter2Sensitivity) / 2;
// Set meter 1 (CPU) %
if (meter1Percent > 100) meter1Percent = 100;
meter1 = (double)meter1Percent;
// Use a polynomial to calculate the real 0-255 based on the logarithmic scale
meter1position = (255.0/309.0) * (0.0117 * pow(meter1, 2.0) + 1.8663 * meter1 + 3.1713);
// Set the PWM duty-cycle
meter1TargetValue = (unsigned char)meter1position;
// Set meter 2 (HDD) %
if (meter2Percent > 100) meter2Percent = 100;
meter2 = (double)meter2Percent;
// Use a polynomial to calculate the real 0-255 based on the logarithmic scale
meter2position = (255.0/309.0) * (0.0117 * pow(meter2, 2.0) + 1.8663 * meter2 + 3.1713);
// Set the PWM duty-cycle
meter2TargetValue = (unsigned char)meter2position;
}
// Check the hardware button and perform debounce
void checkButton(void)
{
// Debounce the button ON
// Current button pressed state is 'not pressed', check the physical port pin
if (BUTTON0 == 0 && buttonPressed == 0) // is pressed?
{
// Physical button is pressed
buttonDebounceCounter++;
if (buttonDebounceCounter == 5) buttonPressed = 1;
}
else
{
buttonDebounceCounter = 0;
}
}
// Initialise system
static void InitialiseSystem(void)
{
// General PIC initialisation ---------------------------------------------
// Configure ports as inputs (1) or outputs(0)
TRISA = 0b00000000;
TRISB = 0b00000001;
TRISC = 0b00000000;
// Clear/Set ports
PORTA = 0b00000111;
PORTB = 0b00000000;
PORTC = 0b00000000;
// Turn off analogue ports
ADCON1 = 0x0F;
// Enable weak pull ups on PORTB (required for the hardware button)
RBPU = 0;
// Timer0 and interupt initialisation -------------------------------------
// Enable interrupts with priority
IPEN = 1;
// Set up timer0
// LED cycle speed is 60Hz with 128 resolution steps (16,667 mS per cycle
// with 16.67 mS per resolution step)
TMR0IP = 0; // Set timer0 interrupt to low priority
TMR0IF = 0; // Clear the timer0 interrupt flag
TMR0L = 255 - 98; // Reset the timer0 counter
T0CON = 0b11000011; // Timer0 on, 8-bit and 1:16 prescaler
TMR0IE = 1; // Enable the timer0 interrupt
// Enable interrupts
GIEH = 1; // Global enable all high priority interrupts
GIEL = 1; // Global enable all low priority interrupts
// PWM modules set up -----------------------------------------------------
// Set up timer2 for PWM
PR2 = 0b11111111;
T2CON = 0b00000111;
// Configure PWM module 1 at 0% duty-cycle
CCPR1L = 0b00000000;
CCP1CON = 0b00111100;
// Configure PWM module 2 at 0% duty-cycle
CCPR2L = 0b00000000;
CCP2CON = 0b0011110;
// USB set up -------------------------------------------------------------
// USB stack and parameter initialisation
#if defined(USE_SELF_POWER_SENSE_IO)
tris_self_power = INPUT_PIN;
#endif
// Initialize the variable holding the handle for the last
// transmission
USBOutHandle = 0;
USBInHandle = 0;
// Call the stack initialisation functions
USBDeviceInit();
}
// Process input and output
void ProcessIO(void)
{
// If we are not in the configured state just return
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
// Check the status of the hardware button
checkButton();
// Check if data was received from the host.
if(!HIDRxHandleBusy(USBOutHandle))
{
switch(ReceivedDataBuffer[0])
{
case 0x80:
// Command 0x80: Update the device
// Byte[0] - The command 0x80
// Byte[1] - The CPU meter percentage (0-100) %
// Byte[2] - The HDD meter percentage (0-100) %
// Byte[3] - The LED red value (0-255) R
// Byte[4] - The LED green value (0-255) G
// Byte[5] - The LED blue value (0-255) B
// Byte[6] - Detach USB on button flag (0-1) false, true
// Update the meters
setMeter(ReceivedDataBuffer[1], ReceivedDataBuffer[2],
meter1SensitivitySetting, meter2SensitivitySetting);
// Update the LED
setRgbLedColour(ReceivedDataBuffer[3], ReceivedDataBuffer[4], ReceivedDataBuffer[5]);
// Check for soft USB detach command
if (ReceivedDataBuffer[6] == 1 && buttonPressed == 1) softUsbDetach = 1;
// Now send back the button state to the host
if (buttonPressed == 1 && ReceivedDataBuffer[6] == 0)
{
ToSendDataBuffer[0] = 0x80;
ToSendDataBuffer[1] = buttonPressed;
buttonPressed = 0;
}
else
{
ToSendDataBuffer[0] = 0x80;
ToSendDataBuffer[1] = 0;
}
// Transmit the response to the host
if(!HIDTxHandleBusy(USBInHandle))
{
USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
}
break;
case 0x81:
// Command 0x81: Update the device settings
// Byte[0] - The command 0x81
// Byte[1] - The meter1 sensitivity (0-100) %
// Byte[2] - The meter2 sensitivity (0-100) %
// Byte[3] - The LED fade on rate low-byte (0-255) (microseconds)
// Byte[4] - The LED fade on rate high-byte (0-255) (microseconds)
// Byte[5] - The LED fade off rate low-byte (0-255) (microseconds)
// Byte[6] - The LED fade off rate high-byte (0-255) (microseconds)
meter1SensitivitySetting = ReceivedDataBuffer[1];
meter2SensitivitySetting = ReceivedDataBuffer[2];
ledFadeOnRateSetting = (ReceivedDataBuffer[4] << 8) + ReceivedDataBuffer[3];
ledFadeOffRateSetting = (ReceivedDataBuffer[6] << 8) + ReceivedDataBuffer[5];
break;
}
// Re-arm the OUT endpoint for the next packet
USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
}
}
// Main function
void main(void)
{
// Note: The USB soft-detach mode allows the user to detach the USB
// device by pressing the hardware button, and then reattach it by
// pressing again. This is useful if the device is attached to a KVM
// and you want to temporarily disable the USB before switching to another
// machine.
while(1)
{
// If we are not detached initialise and start the USB device
if (softUsbDetach == 0) InitialiseSystem();
while(softUsbDetach == 0)
{
// Check bus status and service USB interrupts.
USBDeviceTasks();
// Application-specific tasks.
ProcessIO();
}
// We are in USB soft-detach mode
// Reset the meters and the LED to 'off'
setMeter(0, 0, meter1SensitivitySetting, meter2SensitivitySetting);
setRgbLedColour(0, 0, 0);
// Disable the USB device
UCON = 0b00000000;
UCFG = 0b00001000;
// Wait for a button press before re-attaching the USB device
buttonPressed = 0;
for (int debounce = 0; debounce < 10; debounce++)
while (BUTTON0 == 0); // Wait for the button to be released
while (buttonPressed == 0) checkButton();
// Enable the USB device
UCON = 0b00000000;
UCFG = 0b00000000;
buttonPressed = 0;
softUsbDetach = 0;
}
}
#endif
PK -Dep p " USB Performance Meter\usb_device.c/********************************************************************
File Information:
FileName: usb_device.c
Dependencies: See INCLUDES section
Processor: PIC18 or PIC24 USB Microcontrollers
Hardware: The code is natively intended to be used on the following
hardware platforms: PICDEM FS USB Demo Board,
PIC18F87J50 FS USB Plug-In Module, or
Explorer 16 + PIC24 USB PIM. The firmware may be
modified for use on other USB platforms by editing the
HardwareProfile.h file.
Complier: Microchip C18 (for PIC18) or C30 (for PIC24)
Company: Microchip Technology, Inc.
Software License Agreement:
The software supplied herewith by Microchip Technology Incorporated
(the Company) for its PIC Microcontroller is intended and
supplied to you, the Companys customer, for use solely and
exclusively on Microchip PIC Microcontroller products. The
software is owned by the Company and/or its supplier, and is
protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the
user to criminal sanctions under applicable laws, as well as to
civil liability for the breach of the terms and conditions of this
license.
THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
Summary:
This file contains functions, macros, definitions, variables,
datatypes, etc. that are required for usage with the MCHPFSUSB device
stack. This file should be included in projects that use the device stack.
This file is located in the "\\\Microchip\\USB"
directory.
Description:
USB Device Stack File
This file contains functions, macros, definitions, variables,
datatypes, etc. that are required for usage with the MCHPFSUSB device
stack. This file should be included in projects that use the device stack.
This file is located in the "\\\Microchip\\USB"
directory.
When including this file in a new project, this file can either be
referenced from the directory in which it was installed or copied
directly into the user application folder. If the first method is
chosen to keep the file located in the folder in which it is installed
then include paths need to be added so that the library and the
application both know where to reference each others files. If the
application folder is located in the same folder as the Microchip
folder (like the current demo folders), then the following include
paths need to be added to the application's project:
.
..\\..\\MicrochipInclude
If a different directory structure is used, modify the paths as
required. An example using absolute paths instead of relative paths
would be the following:
C:\\Microchip Solutions\\Microchip\\Include
C:\\Microchip Solutions\\My Demo Application
********************************************************************
File Description:
Change History:
Rev Description
---- -----------
2.6 Added USBCancelIO() function. Moved and some stack
defintions to be more consistant with the host stack.
********************************************************************/
/** INCLUDES *******************************************************/
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "USB.h"
#include "HardwareProfile.h"
#if defined(USB_USE_MSD)
#include "./USB/usb_function_msd.h"
#endif
#if defined(__C32__)
#if (USB_PING_PONG_MODE != USB_PING_PONG__FULL_PING_PONG)
#error "PIC32 only supports full ping pong mode. A different mode other than full ping pong is selected in the usb_config.h file."
#endif
#endif
//#define DEBUG_MODE
#ifdef DEBUG_MODE
#include "uart2.h"
#endif
/** Definitions ****************************************************/
/* v2.1 fix - Short Packet States - Used by Control Transfer Read - CTRL_TRF_TX */
#define SHORT_PKT_NOT_USED 0
#define SHORT_PKT_PENDING 1
#define SHORT_PKT_SENT 2
/* Control Transfer States */
#define WAIT_SETUP 0
#define CTRL_TRF_TX 1
#define CTRL_TRF_RX 2
#if (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0000 // Used in USB Device Mode only
#define USB_NEXT_EP0_IN_PING_PONG 0x0000 // Used in USB Device Mode only
#define USB_NEXT_PING_PONG 0x0000 // Used in USB Device Mode only
#define EP0_OUT_EVEN 0 // Used in USB Device Mode only
#define EP0_OUT_ODD 0 // Used in USB Device Mode only
#define EP0_IN_EVEN 1 // Used in USB Device Mode only
#define EP0_IN_ODD 1 // Used in USB Device Mode only
#define EP1_OUT_EVEN 2 // Used in USB Device Mode only
#define EP1_OUT_ODD 2 // Used in USB Device Mode only
#define EP1_IN_EVEN 3 // Used in USB Device Mode only
#define EP1_IN_ODD 3 // Used in USB Device Mode only
#define EP2_OUT_EVEN 4 // Used in USB Device Mode only
#define EP2_OUT_ODD 4 // Used in USB Device Mode only
#define EP2_IN_EVEN 5 // Used in USB Device Mode only
#define EP2_IN_ODD 5 // Used in USB Device Mode only
#define EP3_OUT_EVEN 6 // Used in USB Device Mode only
#define EP3_OUT_ODD 6 // Used in USB Device Mode only
#define EP3_IN_EVEN 7 // Used in USB Device Mode only
#define EP3_IN_ODD 7 // Used in USB Device Mode only
#define EP4_OUT_EVEN 8 // Used in USB Device Mode only
#define EP4_OUT_ODD 8 // Used in USB Device Mode only
#define EP4_IN_EVEN 9 // Used in USB Device Mode only
#define EP4_IN_ODD 9 // Used in USB Device Mode only
#define EP5_OUT_EVEN 10 // Used in USB Device Mode only
#define EP5_OUT_ODD 10 // Used in USB Device Mode only
#define EP5_IN_EVEN 11 // Used in USB Device Mode only
#define EP5_IN_ODD 11 // Used in USB Device Mode only
#define EP6_OUT_EVEN 12 // Used in USB Device Mode only
#define EP6_OUT_ODD 12 // Used in USB Device Mode only
#define EP6_IN_EVEN 13 // Used in USB Device Mode only
#define EP6_IN_ODD 13 // Used in USB Device Mode only
#define EP7_OUT_EVEN 14 // Used in USB Device Mode only
#define EP7_OUT_ODD 14 // Used in USB Device Mode only
#define EP7_IN_EVEN 15 // Used in USB Device Mode only
#define EP7_IN_ODD 15 // Used in USB Device Mode only
#define EP8_OUT_EVEN 16 // Used in USB Device Mode only
#define EP8_OUT_ODD 16 // Used in USB Device Mode only
#define EP8_IN_EVEN 17 // Used in USB Device Mode only
#define EP8_IN_ODD 17 // Used in USB Device Mode only
#define EP9_OUT_EVEN 18 // Used in USB Device Mode only
#define EP9_OUT_ODD 18 // Used in USB Device Mode only
#define EP9_IN_EVEN 19 // Used in USB Device Mode only
#define EP9_IN_ODD 19 // Used in USB Device Mode only
#define EP10_OUT_EVEN 20 // Used in USB Device Mode only
#define EP10_OUT_ODD 20 // Used in USB Device Mode only
#define EP10_IN_EVEN 21 // Used in USB Device Mode only
#define EP10_IN_ODD 21 // Used in USB Device Mode only
#define EP11_OUT_EVEN 22 // Used in USB Device Mode only
#define EP11_OUT_ODD 22 // Used in USB Device Mode only
#define EP11_IN_EVEN 23 // Used in USB Device Mode only
#define EP11_IN_ODD 23 // Used in USB Device Mode only
#define EP12_OUT_EVEN 24 // Used in USB Device Mode only
#define EP12_OUT_ODD 24 // Used in USB Device Mode only
#define EP12_IN_EVEN 25 // Used in USB Device Mode only
#define EP12_IN_ODD 25 // Used in USB Device Mode only
#define EP13_OUT_EVEN 26 // Used in USB Device Mode only
#define EP13_OUT_ODD 26 // Used in USB Device Mode only
#define EP13_IN_EVEN 27 // Used in USB Device Mode only
#define EP13_IN_ODD 27 // Used in USB Device Mode only
#define EP14_OUT_EVEN 28 // Used in USB Device Mode only
#define EP14_OUT_ODD 28 // Used in USB Device Mode only
#define EP14_IN_EVEN 29 // Used in USB Device Mode only
#define EP14_IN_ODD 29 // Used in USB Device Mode only
#define EP15_OUT_EVEN 30 // Used in USB Device Mode only
#define EP15_OUT_ODD 30 // Used in USB Device Mode only
#define EP15_IN_EVEN 31 // Used in USB Device Mode only
#define EP15_IN_ODD 31 // Used in USB Device Mode only
#define EP(ep,dir,pp) (2*ep+dir) // Used in USB Device Mode only
#define BD(ep,dir,pp) ((8 * ep) + (4 * dir)) // Used in USB Device Mode only
#elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0004
#define USB_NEXT_EP0_IN_PING_PONG 0x0000
#define USB_NEXT_PING_PONG 0x0000
#define EP0_OUT_EVEN 0
#define EP0_OUT_ODD 1
#define EP0_IN_EVEN 2
#define EP0_IN_ODD 2
#define EP1_OUT_EVEN 3
#define EP1_OUT_ODD 3
#define EP1_IN_EVEN 4
#define EP1_IN_ODD 4
#define EP2_OUT_EVEN 5
#define EP2_OUT_ODD 5
#define EP2_IN_EVEN 6
#define EP2_IN_ODD 6
#define EP3_OUT_EVEN 7
#define EP3_OUT_ODD 7
#define EP3_IN_EVEN 8
#define EP3_IN_ODD 8
#define EP4_OUT_EVEN 9
#define EP4_OUT_ODD 9
#define EP4_IN_EVEN 10
#define EP4_IN_ODD 10
#define EP5_OUT_EVEN 11
#define EP5_OUT_ODD 11
#define EP5_IN_EVEN 12
#define EP5_IN_ODD 12
#define EP6_OUT_EVEN 13
#define EP6_OUT_ODD 13
#define EP6_IN_EVEN 14
#define EP6_IN_ODD 14
#define EP7_OUT_EVEN 15
#define EP7_OUT_ODD 15
#define EP7_IN_EVEN 16
#define EP7_IN_ODD 16
#define EP8_OUT_EVEN 17
#define EP8_OUT_ODD 17
#define EP8_IN_EVEN 18
#define EP8_IN_ODD 18
#define EP9_OUT_EVEN 19
#define EP9_OUT_ODD 19
#define EP9_IN_EVEN 20
#define EP9_IN_ODD 20
#define EP10_OUT_EVEN 21
#define EP10_OUT_ODD 21
#define EP10_IN_EVEN 22
#define EP10_IN_ODD 22
#define EP11_OUT_EVEN 23
#define EP11_OUT_ODD 23
#define EP11_IN_EVEN 24
#define EP11_IN_ODD 24
#define EP12_OUT_EVEN 25
#define EP12_OUT_ODD 25
#define EP12_IN_EVEN 26
#define EP12_IN_ODD 26
#define EP13_OUT_EVEN 27
#define EP13_OUT_ODD 27
#define EP13_IN_EVEN 28
#define EP13_IN_ODD 28
#define EP14_OUT_EVEN 29
#define EP14_OUT_ODD 29
#define EP14_IN_EVEN 30
#define EP14_IN_ODD 30
#define EP15_OUT_EVEN 31
#define EP15_OUT_ODD 31
#define EP15_IN_EVEN 32
#define EP15_IN_ODD 32
#define EP(ep,dir,pp) (2*ep+dir+(((ep==0)&&(dir==0))?pp:2))
#define BD(ep,dir,pp) (4*(ep+dir+(((ep==0)&&(dir==0))?pp:2)))
#elif (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
#if defined (__18CXX) || defined(__C30__)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0004
#define USB_NEXT_EP0_IN_PING_PONG 0x0004
#define USB_NEXT_PING_PONG 0x0004
#elif defined(__C32__)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0008
#define USB_NEXT_EP0_IN_PING_PONG 0x0008
#define USB_NEXT_PING_PONG 0x0008
#else
#error "Not defined for this compiler"
#endif
#define EP0_OUT_EVEN 0
#define EP0_OUT_ODD 1
#define EP0_IN_EVEN 2
#define EP0_IN_ODD 3
#define EP1_OUT_EVEN 4
#define EP1_OUT_ODD 5
#define EP1_IN_EVEN 6
#define EP1_IN_ODD 7
#define EP2_OUT_EVEN 8
#define EP2_OUT_ODD 9
#define EP2_IN_EVEN 10
#define EP2_IN_ODD 11
#define EP3_OUT_EVEN 12
#define EP3_OUT_ODD 13
#define EP3_IN_EVEN 14
#define EP3_IN_ODD 15
#define EP4_OUT_EVEN 16
#define EP4_OUT_ODD 17
#define EP4_IN_EVEN 18
#define EP4_IN_ODD 19
#define EP5_OUT_EVEN 20
#define EP5_OUT_ODD 21
#define EP5_IN_EVEN 22
#define EP5_IN_ODD 23
#define EP6_OUT_EVEN 24
#define EP6_OUT_ODD 25
#define EP6_IN_EVEN 26
#define EP6_IN_ODD 27
#define EP7_OUT_EVEN 28
#define EP7_OUT_ODD 29
#define EP7_IN_EVEN 30
#define EP7_IN_ODD 31
#define EP8_OUT_EVEN 32
#define EP8_OUT_ODD 33
#define EP8_IN_EVEN 34
#define EP8_IN_ODD 35
#define EP9_OUT_EVEN 36
#define EP9_OUT_ODD 37
#define EP9_IN_EVEN 38
#define EP9_IN_ODD 39
#define EP10_OUT_EVEN 40
#define EP10_OUT_ODD 41
#define EP10_IN_EVEN 42
#define EP10_IN_ODD 43
#define EP11_OUT_EVEN 44
#define EP11_OUT_ODD 45
#define EP11_IN_EVEN 46
#define EP11_IN_ODD 47
#define EP12_OUT_EVEN 48
#define EP12_OUT_ODD 49
#define EP12_IN_EVEN 50
#define EP12_IN_ODD 51
#define EP13_OUT_EVEN 52
#define EP13_OUT_ODD 53
#define EP13_IN_EVEN 54
#define EP13_IN_ODD 55
#define EP14_OUT_EVEN 56
#define EP14_OUT_ODD 57
#define EP14_IN_EVEN 58
#define EP14_IN_ODD 59
#define EP15_OUT_EVEN 60
#define EP15_OUT_ODD 61
#define EP15_IN_EVEN 62
#define EP15_IN_ODD 63
#define EP(ep,dir,pp) (4*ep+2*dir+pp)
#if defined (__18CXX) || defined(__C30__)
#define BD(ep,dir,pp) (4*(4*ep+2*dir+pp))
#elif defined(__C32__)
#define BD(ep,dir,pp) (8*(4*ep+2*dir+pp))
#else
#error "Not defined for this compiler"
#endif
#elif (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0000
#define USB_NEXT_EP0_IN_PING_PONG 0x0000
#define USB_NEXT_PING_PONG 0x0004
#define EP0_OUT_EVEN 0
#define EP0_OUT_ODD 0
#define EP0_IN_EVEN 1
#define EP0_IN_ODD 1
#define EP1_OUT_EVEN 2
#define EP1_OUT_ODD 3
#define EP1_IN_EVEN 4
#define EP1_IN_ODD 5
#define EP2_OUT_EVEN 6
#define EP2_OUT_ODD 7
#define EP2_IN_EVEN 8
#define EP2_IN_ODD 9
#define EP3_OUT_EVEN 10
#define EP3_OUT_ODD 11
#define EP3_IN_EVEN 12
#define EP3_IN_ODD 13
#define EP4_OUT_EVEN 14
#define EP4_OUT_ODD 15
#define EP4_IN_EVEN 16
#define EP4_IN_ODD 17
#define EP5_OUT_EVEN 18
#define EP5_OUT_ODD 19
#define EP5_IN_EVEN 20
#define EP5_IN_ODD 21
#define EP6_OUT_EVEN 22
#define EP6_OUT_ODD 23
#define EP6_IN_EVEN 24
#define EP6_IN_ODD 25
#define EP7_OUT_EVEN 26
#define EP7_OUT_ODD 27
#define EP7_IN_EVEN 28
#define EP7_IN_ODD 29
#define EP8_OUT_EVEN 30
#define EP8_OUT_ODD 31
#define EP8_IN_EVEN 32
#define EP8_IN_ODD 33
#define EP9_OUT_EVEN 34
#define EP9_OUT_ODD 35
#define EP9_IN_EVEN 36
#define EP9_IN_ODD 37
#define EP10_OUT_EVEN 38
#define EP10_OUT_ODD 39
#define EP10_IN_EVEN 40
#define EP10_IN_ODD 41
#define EP11_OUT_EVEN 42
#define EP11_OUT_ODD 43
#define EP11_IN_EVEN 44
#define EP11_IN_ODD 45
#define EP12_OUT_EVEN 46
#define EP12_OUT_ODD 47
#define EP12_IN_EVEN 48
#define EP12_IN_ODD 49
#define EP13_OUT_EVEN 50
#define EP13_OUT_ODD 51
#define EP13_IN_EVEN 52
#define EP13_IN_ODD 53
#define EP14_OUT_EVEN 54
#define EP14_OUT_ODD 55
#define EP14_IN_EVEN 56
#define EP14_IN_ODD 57
#define EP15_OUT_EVEN 58
#define EP15_OUT_ODD 59
#define EP15_IN_EVEN 60
#define EP15_IN_ODD 61
#define EP(ep,dir,pp) (4*ep+2*dir+((ep==0)?0:(pp-2)))
#define BD(ep,dir,pp) (4*(4*ep+2*dir+((ep==0)?0:(pp-2))))
#else
#error "No ping pong mode defined."
#endif
/** VARIABLES ******************************************************/
#if !defined(PICC18PORT)
#pragma udata
#endif
USB_VOLATILE USB_DEVICE_STATE USBDeviceState;
USB_VOLATILE BYTE USBActiveConfiguration;
USB_VOLATILE BYTE USBAlternateInterface[USB_MAX_NUM_INT];
volatile BDT_ENTRY *pBDTEntryEP0OutCurrent;
volatile BDT_ENTRY *pBDTEntryEP0OutNext;
volatile BDT_ENTRY *pBDTEntryOut[USB_MAX_EP_NUMBER+1];
volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1];
USB_VOLATILE BYTE shortPacketStatus;
USB_VOLATILE BYTE controlTransferState;
USB_VOLATILE IN_PIPE inPipes[1];
USB_VOLATILE OUT_PIPE outPipes[1];
USB_VOLATILE BYTE *pDst;
USB_VOLATILE BOOL RemoteWakeup;
USB_VOLATILE BYTE USTATcopy;
USB_VOLATILE BOOL BothEP0OutUOWNsSet;
USB_VOLATILE WORD USBInMaxPacketSize[USB_MAX_EP_NUMBER];
USB_VOLATILE BYTE *USBInData[USB_MAX_EP_NUMBER];
/** USB FIXED LOCATION VARIABLES ***********************************/
#if !defined(PICC18PORT)
#if defined(__18CXX)
#if defined(__18F14K50) || defined(__18F13K50) || defined(__18LF14K50) || defined(__18LF13K50)
#pragma udata USB_BDT=0x200 //See Linker Script,usb2:0x200-0x2FF(256-byte)
#else
#pragma udata USB_BDT=0x400 //See Linker Script,usb4:0x400-0x4FF(256-byte)
#endif
#endif
#endif
/********************************************************************
* Section A: Buffer Descriptor Table
* - 0x400 - 0x4FF(max)
* - USB_MAX_EP_NUMBER is defined in usb_config.h
*******************************************************************/
#if !defined(PICC18PORT)
#if (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG)
volatile BDT_ENTRY BDT[(USB_MAX_EP_NUMBER + 1) * 2] __attribute__ ((aligned (512)));
#elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY)
volatile BDT_ENTRY BDT[((USB_MAX_EP_NUMBER + 1) * 2)+1] __attribute__ ((aligned (512)));
#elif (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
volatile BDT_ENTRY BDT[(USB_MAX_EP_NUMBER + 1) * 4] __attribute__ ((aligned (512)));
#elif (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0)
volatile BDT_ENTRY BDT[((USB_MAX_EP_NUMBER + 1) * 4)-2] __attribute__ ((aligned (512)));
#else
#error "No ping pong mode defined."
#endif
#else
// PICC18PORT only supports full ping pong mode (nothing else was tested)
volatile BDT_ENTRY BDT[(USB_MAX_EP_NUMBER + 1) * 4] __attribute__ ((aligned (512))) @ 0x400;
#endif
//#if defined(__18CXX)
//#pragma udata usbram5=0x400 //See Linker Script,usb5:0x500-0x5FF(256-byte)
//#endif
/********************************************************************
* Section B: EP0 Buffer Space
*******************************************************************/
#if defined(PICC18PORT)
// Doesn't seem to be any good way of doing this in Hi-Tech C
// So the variables here have to be mapped manually into USBRAM
// or nothing will work...
volatile CTRL_TRF_SETUP SetupPkt @ 0x500; // 8-byte only
volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE] @ 0x520;
#else
volatile CTRL_TRF_SETUP SetupPkt; // 8-byte only
volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE];
#endif
/********************************************************************
* Section C: non-EP0 Buffer Space
*******************************************************************/
// Can provide compile time option to do software pingpong
#if defined(USB_USE_HID)
#if defined(PICC18PORT)
// Doesn't seem to be any good way of doing this in Hi-Tech C
// So the variables here have to be mapped manually into USBRAM
// or nothing will work...
volatile unsigned char hid_report_out[HID_INT_OUT_EP_SIZE] @ 0x530;
volatile unsigned char hid_report_in[HID_INT_IN_EP_SIZE] @ 0x570;
#else
volatile unsigned char hid_report_out[HID_INT_OUT_EP_SIZE];
volatile unsigned char hid_report_in[HID_INT_IN_EP_SIZE];
#endif
#endif
#if defined(USB_USE_MSD)
//volatile far USB_MSD_CBW_CSW msd_cbw_csw;
volatile USB_MSD_CBW msd_cbw;
volatile USB_MSD_CSW msd_csw;
//#pragma udata
#if defined(__18CXX)
#pragma udata myMSD=MSD_BUFFER_ADDRESS
#endif
volatile char msd_buffer[512];
#endif
#if !defined(PICC18PORT)
#if defined(__18CXX)
#pragma udata
#endif
#endif
//Depricated in v2.2 - will be removed in a future revision
#if !defined(USB_USER_DEVICE_DESCRIPTOR)
//Device descriptor
extern ROM USB_DEVICE_DESCRIPTOR device_dsc;
#else
USB_USER_DEVICE_DESCRIPTOR_INCLUDE;
#endif
#if !defined(USB_USER_CONFIG_DESCRIPTOR)
//Array of configuration descriptors
extern ROM BYTE *ROM USB_CD_Ptr[];
#else
USB_USER_CONFIG_DESCRIPTOR_INCLUDE;
#endif
extern ROM BYTE *ROM USB_SD_Ptr[];
/** DECLARATIONS ***************************************************/
#if !defined(PICC18PORT)
#pragma code
#endif
/** Macros *********************************************************/
/****** Event callback enabling/disabling macros ********************
This section of code is used to disable specific USB events that may not be
desired by the user. This can save code size and increase throughput and
decrease CPU utiliazation.
********************************************************************/
#if defined USB_DISABLE_SUSPEND_HANDLER
#define USB_SUSPEND_HANDLER(event,pointer,size)
#warning "Disabling the suspend handler is not recommended. Proper suspend handling is required to create a compilant USB device."
#else
#define USB_SUSPEND_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_WAKEUP_FROM_SUSPEND_HANDLER
#define USB_WAKEUP_FROM_SUSPEND_HANDLER(event,pointer,size)
#warning "Disabling the wake from suspend handler is not recommended. Proper suspend handling is required to create a compilant USB device."
#else
#define USB_WAKEUP_FROM_SUSPEND_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_SOF_HANDLER
#define USB_SOF_HANDLER(event,pointer,size)
#else
#define USB_SOF_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_ERROR_HANDLER
#define USB_ERROR_HANDLER(event,pointer,size)
#else
#define USB_ERROR_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER
#define USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER(event,pointer,size)
#else
#define USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_SET_DESCRIPTOR_HANDLER
#define USB_SET_DESCRIPTOR_HANDLER(event,pointer,size)
#else
#define USB_SET_DESCRIPTOR_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_SET_CONFIGURATION_HANDLER
#define USB_SET_CONFIGURATION_HANDLER(event,pointer,size)
#else
#define USB_SET_CONFIGURATION_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_TRANSFER_COMPLETE_HANDLER
#define USB_TRASFER_COMPLETE_HANDLER(event,pointer,size)
#else
#define USB_TRASFER_COMPLETE_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
/** Function Prototypes ********************************************/
//External
//This is the prototype for the required user event handler
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size);
//Internal
void USBCtrlEPService(void);
void USBCtrlTrfSetupHandler(void);
void USBCtrlTrfInHandler(void);
void USBCheckStdRequest(void);
void USBStdGetDscHandler(void);
void USBCtrlEPServiceComplete(void);
void USBCtrlTrfTxService(void);
void USBPrepareForNextSetupTrf(void);
void USBCtrlTrfRxService(void);
void USBStdSetCfgHandler(void);
void USBStdGetStatusHandler(void);
void USBStdFeatureReqHandler(void);
void USBCtrlTrfOutHandler(void);
BOOL USBIsTxBusy(BYTE EPNumber);
void USBPut(BYTE EPNum, BYTE Data);
void USBEPService(void);
void USBConfigureEndpoint(BYTE EPNum, BYTE direction);
void USBProtocolResetHandler(void);
void USBWakeFromSuspend(void);
void USBSuspend(void);
void USBStallHandler(void);
USB_HANDLE USBTransferOnePacket(BYTE ep, BYTE dir, BYTE* data, BYTE len);
void USBEnableEndpoint(BYTE ep,BYTE options);
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBDeviceInit(void)
Description:
This function initializes the device stack
it in the default state
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
The USB module will be completely reset including
all of the internal variables, registers, and
interrupt flags.
***************************************************************************/
//DOM-IGNORE-END
void USBDeviceInit(void)
{
BYTE i;
// Clear all USB error flags
USBClearInterruptRegister(U1EIR);
// Clears all USB interrupts
USBClearInterruptRegister(U1IR);
SetConfigurationOptions();
//power up the module
USBPowerModule();
//set the address of the BDT (if applicable)
USBSetBDTAddress(BDT);
// Assert reset request to all of the Ping Pong buffer pointers
USBPingPongBufferReset = 1;
// Reset to default address
U1ADDR = 0x00;
//Clear all of the endpoint control registers
memset((void*)&U1EP1,0x00,(USB_MAX_EP_NUMBER-1));
//Stop trying to reset ping pong buffer pointers
USBPingPongBufferReset = 0;
//Clear all of the BDT entries
for(i=0;i<(sizeof(BDT)/sizeof(BDT_ENTRY));i++)
{
BDT[i].Val = 0x00;
}
// Initialize EP0 as a Ctrl EP
U1EP0 = EP_CTRL|USB_HANDSHAKE_ENABLED;
// Flush any pending transactions
while(USBTransactionCompleteIF == 1)
{
USBClearInterruptFlag(USBTransactionCompleteIFReg,USBTransactionCompleteIFBitNum);
}
//clear all of the internal pipe information
inPipes[0].info.Val = 0;
outPipes[0].info.Val = 0;
outPipes[0].wCount.Val = 0;
// Make sure packet processing is enabled
USBPacketDisable = 0;
//Get ready for the first packet
pBDTEntryIn[0] = (volatile BDT_ENTRY*)&BDT[EP0_IN_EVEN];
// Clear active configuration
USBActiveConfiguration = 0;
//Indicate that we are now in the detached state
USBDeviceState = DETACHED_STATE;
}
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBDeviceTasks(void)
Description:
This function is the main state machine of the
USB device side stack. This function should be
called periodically to receive and transmit
packets through the stack. This function should
be called preferably once every 100us
during the enumeration process. After the
enumeration process this function still needs to
be called periodically to respond to various
situations on the bus but is more relaxed in its
time requirements. This function should also
be called at least as fast as the OUT data
expected from the PC.
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
//DOM-IGNORE-END
#if defined(USB_INTERRUPT)
#if defined(__18CXX)
void USBDeviceTasks(void)
#elif defined(__C30__)
//void __attribute__((interrupt,auto_psv,address(0xA800))) _USB1Interrupt()
void __attribute__((interrupt,auto_psv)) _USB1Interrupt()
#elif defined(__PIC32MX__)
#pragma interrupt _USB1Interrupt ipl4 vector 45
void _USB1Interrupt( void )
#endif
#else
void USBDeviceTasks(void)
#endif
{
BYTE i;
#ifdef USB_SUPPORT_OTG
//SRP Time Out Check
if (USBOTGSRPIsReady())
{
if (USBT1MSECIF && USBT1MSECIE)
{
if (USBOTGGetSRPTimeOutFlag())
{
if (USBOTGIsSRPTimeOutExpired())
{
USB_OTGEventHandler(0,OTG_EVENT_SRP_FAILED,0,0);
}
}
//Clear Interrupt Flag
USBClearInterruptFlag(USBT1MSECIFReg,USBT1MSECIFBitNum);
}
}
#endif
#if defined(USB_POLLING)
//If the interrupt option is selected then the customer is required
// to notify the stack when the device is attached or removed from the
// bus by calling the USBDeviceAttach() and USBDeviceDetach() functions.
if (USB_BUS_SENSE != 1)
{
// Disable module & detach from bus
U1CON = 0;
// Mask all USB interrupts
U1IE = 0;
//Move to the detached state
USBDeviceState = DETACHED_STATE;
#ifdef USB_SUPPORT_OTG
//Disable D+ Pullup
U1OTGCONbits.DPPULUP = 0;
//Disable HNP
USBOTGDisableHnp();
//Deactivate HNP
USBOTGDeactivateHnp();
//If ID Pin Changed State
if (USBIDIF && USBIDIE)
{
//Re-detect & Initialize
USBOTGInitialize();
//Clear ID Interrupt Flag
USBClearInterruptFlag(USBIDIFReg,USBIDIFBitNum);
}
#endif
#ifdef __C30__
//USBClearInterruptFlag(U1OTGIR, 3);
#endif
//return so that we don't go through the rest of
//the state machine
USBClearUSBInterrupt();
return;
}
#ifdef USB_SUPPORT_OTG
//If Session Is Started Then
else
{
//If SRP Is Ready
if (USBOTGSRPIsReady())
{
//Clear SRPReady
USBOTGClearSRPReady();
//Clear SRP Timeout Flag
USBOTGClearSRPTimeOutFlag();
//Indicate Session Started
UART2PrintString( "\r\n***** USB OTG B Event - Session Started *****\r\n" );
}
}
#endif //#ifdef USB_SUPPORT_OTG
//if we are in the detached state
if(USBDeviceState == DETACHED_STATE)
{
//Initialize register to known value
U1CON = 0;
// Mask all USB interrupts
U1IE = 0;
//Enable/set things like: pull ups, full/low-speed mode,
//set the ping pong mode, and set internal transceiver
SetConfigurationOptions();
// Enable module & attach to bus
while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;}
//moved to the attached state
USBDeviceState = ATTACHED_STATE;
#ifdef USB_SUPPORT_OTG
U1OTGCON |= USB_OTG_DPLUS_ENABLE | USB_OTG_ENABLE;
#endif
}
#endif //#if defined(USB_POLLING)
if(USBDeviceState == ATTACHED_STATE)
{
/*
* After enabling the USB module, it takes some time for the
* voltage on the D+ or D- line to rise high enough to get out
* of the SE0 condition. The USB Reset interrupt should not be
* unmasked until the SE0 condition is cleared. This helps
* prevent the firmware from misinterpreting this unique event
* as a USB bus reset from the USB host.
*/
if(!USBSE0Event)
{
USBClearInterruptRegister(U1IR);// Clear all USB interrupts
#if defined(USB_POLLING)
U1IE=0; // Mask all USB interrupts
#endif
USBResetIE = 1; // Unmask RESET interrupt
USBIdleIE = 1; // Unmask IDLE interrupt
USBDeviceState = POWERED_STATE;
}
}
#ifdef USB_SUPPORT_OTG
//If ID Pin Changed State
if (USBIDIF && USBIDIE)
{
//Re-detect & Initialize
USBOTGInitialize();
USBClearInterruptFlag(USBIDIFReg,USBIDIFBitNum);
}
#endif
/*
* Task A: Service USB Activity Interrupt
*/
if(USBActivityIF && USBActivityIE)
{
USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
#if defined(USB_SUPPORT_OTG)
U1OTGIR = 0x10;
#else
USBWakeFromSuspend();
#endif
}
/*
* Pointless to continue servicing if the device is in suspend mode.
*/
if(USBSuspendControl==1)
{
USBClearUSBInterrupt();
return;
}
/*
* Task B: Service USB Bus Reset Interrupt.
* When bus reset is received during suspend, ACTVIF will be set first,
* once the UCONbits.SUSPND is clear, then the URSTIF bit will be asserted.
* This is why URSTIF is checked after ACTVIF.
*
* The USB reset flag is masked when the USB state is in
* DETACHED_STATE or ATTACHED_STATE, and therefore cannot
* cause a USB reset event during these two states.
*/
if(USBResetIF && USBResetIE)
{
USBDeviceInit();
USBDeviceState = DEFAULT_STATE;
/********************************************************************
Bug Fix: Feb 26, 2007 v2.1 (#F1)
*********************************************************************
In the original firmware, if an OUT token is sent by the host
before a SETUP token is sent, the firmware would respond with an ACK.
This is not a correct response, the firmware should have sent a STALL.
This is a minor non-compliance since a compliant host should not
send an OUT before sending a SETUP token. The fix allows a SETUP
transaction to be accepted while stalling OUT transactions.
********************************************************************/
BDT[EP0_OUT_EVEN].ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
BDT[EP0_OUT_EVEN].CNT = USB_EP0_BUFF_SIZE;
BDT[EP0_OUT_EVEN].STAT.Val &= ~_STAT_MASK;
BDT[EP0_OUT_EVEN].STAT.Val |= _USIE|_DAT0|_DTSEN|_BSTALL;
#ifdef USB_SUPPORT_OTG
//Disable HNP
USBOTGDisableHnp();
//Deactivate HNP
USBOTGDeactivateHnp();
#endif
USBClearInterruptFlag(USBResetIFReg,USBResetIFBitNum);
}
/*
* Task C: Service other USB interrupts
*/
if(USBIdleIF && USBIdleIE)
{
#ifdef USB_SUPPORT_OTG
//If Suspended, Try to switch to Host
USBOTGSelectRole(ROLE_HOST);
#else
USBSuspend();
#endif
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
}
if(USBSOFIF && USBSOFIE)
{
USB_SOF_HANDLER(EVENT_SOF,0,1);
USBClearInterruptFlag(USBSOFIFReg,USBSOFIFBitNum);
}
if(USBStallIF && USBStallIE)
{
USBStallHandler();
}
if(USBErrorIF && USBErrorIE)
{
USB_ERROR_HANDLER(EVENT_BUS_ERROR,0,1);
USBClearInterruptRegister(U1EIR); // This clears UERRIF
}
/*
* Pointless to continue servicing if the host has not sent a bus reset.
* Once bus reset is received, the device transitions into the DEFAULT
* state and is ready for communication.
*/
if(USBDeviceState < DEFAULT_STATE)
{
USBClearUSBInterrupt();
return;
}
/*
* Task D: Servicing USB Transaction Complete Interrupt
*/
if(USBTransactionCompleteIE)
{
for(i = 0; i < 4; i++) //Drain or deplete the USAT FIFO entries. If the USB FIFO ever gets full, USB bandwidth
{ //utilization can be compromised, and the device won't be able to receive SETUP packets.
if(USBTransactionCompleteIF)
{
USTATcopy = U1STAT;
USBClearInterruptFlag(USBTransactionCompleteIFReg,USBTransactionCompleteIFBitNum);
/*
* USBCtrlEPService only services transactions over EP0.
* It ignores all other EP transactions.
*/
if((USTATcopy & ENDPOINT_MASK) == 0)
{
USBCtrlEPService();
}
else
{
USB_TRASFER_COMPLETE_HANDLER(
EVENT_TRANSFER,
(BYTE*)&USTATcopy,
0);
}
}//end if(USBTransactionCompleteIF)
else
break; //USTAT FIFO must be empty.
}//end for()
}//end if(USBTransactionCompleteIE)
USBClearUSBInterrupt();
}//end of USBDeviceTasks()
/********************************************************************
* Function: void USBStallHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects:
*
* Overview: This function handles the event of a STALL
* occuring on the bus
*
* Note: None
*******************************************************************/
void USBStallHandler(void)
{
/*
* Does not really have to do anything here,
* even for the control endpoint.
* All BDs of Endpoint 0 are owned by SIE right now,
* but once a Setup Transaction is received, the ownership
* for EP0_OUT will be returned to CPU.
* When the Setup Transaction is serviced, the ownership
* for EP0_IN will then be forced back to CPU by firmware.
*/
/* v2b fix */
if(U1EP0bits.EPSTALL == 1)
{
// UOWN - if 0, owned by CPU, if 1, owned by SIE
if((pBDTEntryEP0OutCurrent->STAT.Val == _USIE) && (pBDTEntryIn[0]->STAT.Val == (_USIE|_BSTALL)))
{
// Set ep0Bo to stall also
pBDTEntryEP0OutCurrent->STAT.Val = _USIE|_DAT0|_DTSEN|_BSTALL;
}//end if
U1EP0bits.EPSTALL = 0; // Clear stall status
}//end if
USBClearInterruptFlag(USBStallIFReg,USBStallIFBitNum);
}
/********************************************************************
* Function: void USBSuspend(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects:
*
* Overview: This function handles if the host tries to
* suspend the device
*
* Note: None
*******************************************************************/
void USBSuspend(void)
{
/*
* NOTE: Do not clear UIRbits.ACTVIF here!
* Reason:
* ACTVIF is only generated once an IDLEIF has been generated.
* This is a 1:1 ratio interrupt generation.
* For every IDLEIF, there will be only one ACTVIF regardless of
* the number of subsequent bus transitions.
*
* If the ACTIF is cleared here, a problem could occur when:
* [ IDLE ][bus activity ->
* <--- 3 ms -----> ^
* ^ ACTVIF=1
* IDLEIF=1
* # # # # (#=Program polling flags)
* ^
* This polling loop will see both
* IDLEIF=1 and ACTVIF=1.
* However, the program services IDLEIF first
* because ACTIVIE=0.
* If this routine clears the only ACTIVIF,
* then it can never get out of the suspend
* mode.
*/
USBActivityIE = 1; // Enable bus activity interrupt
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
#if defined(__18CXX)
U1CONbits.SUSPND = 1; // Put USB module in power conserve
// mode, SIE clock inactive
#endif
/*
* At this point the PIC can go into sleep,idle, or
* switch to a slower clock, etc. This should be done in the
* USBCBSuspend() if necessary.
*/
USB_SUSPEND_HANDLER(EVENT_SUSPEND,0,0);
}
/********************************************************************
* Function: void USBWakeFromSuspend(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview:
*
* Note: None
*******************************************************************/
void USBWakeFromSuspend(void)
{
/*
* If using clock switching, the place to restore the original
* microcontroller core clock frequency is in the USBCBWakeFromSuspend() callback
*/
USB_WAKEUP_FROM_SUSPEND_HANDLER(EVENT_RESUME,0,0);
#if defined(__18CXX)
//To avoid improperly clocking the USB module, make sure the oscillator
//settings are consistant with USB operation before clearing the SUSPND bit.
//Make sure the correct oscillator settings are selected in the
//"USB_WAKEUP_FROM_SUSPEND_HANDLER(EVENT_RESUME,0,0)" handler.
U1CONbits.SUSPND = 0; // Bring USB module out of power conserve
// mode.
#endif
USBActivityIE = 0;
/********************************************************************
Bug Fix: Feb 26, 2007 v2.1
*********************************************************************
The ACTVIF bit cannot be cleared immediately after the USB module wakes
up from Suspend or while the USB module is suspended. A few clock cycles
are required to synchronize the internal hardware state machine before
the ACTIVIF bit can be cleared by firmware. Clearing the ACTVIF bit
before the internal hardware is synchronized may not have an effect on
the value of ACTVIF. Additonally, if the USB module uses the clock from
the 96 MHz PLL source, then after clearing the SUSPND bit, the USB
module may not be immediately operational while waiting for the 96 MHz
PLL to lock.
********************************************************************/
// UIRbits.ACTVIF = 0; // Removed
#if defined(__18CXX)
while(USBActivityIF)
#endif
{
USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
} // Added
}//end USBWakeFromSuspend
/********************************************************************
* Function: void USBCtrlEPService(void)
*
* PreCondition: USTAT is loaded with a valid endpoint address.
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: USBCtrlEPService checks for three transaction
* types that it knows how to service and services
* them:
* 1. EP0 SETUP
* 2. EP0 OUT
* 3. EP0 IN
* It ignores all other types (i.e. EP1, EP2, etc.)
*
* Note: None
*******************************************************************/
void USBCtrlEPService(void)
{
//Check if the last transaction was on EP0 OUT endpoint (of any kind, to either the even or odd buffer if ping pong buffers used)
if((USTATcopy & USTAT_EP0_PP_MASK) == USTAT_EP0_OUT_EVEN)
{
//Point to the EP0 OUT buffer of the buffer that arrived
#if defined(__18CXX)
pBDTEntryEP0OutCurrent = (volatile BDT_ENTRY*)&BDT[(USTATcopy & USTAT_EP_MASK)>>1];
#elif defined(__C30__) || defined(__C32__)
pBDTEntryEP0OutCurrent = (volatile BDT_ENTRY*)&BDT[(USTATcopy & USTAT_EP_MASK)>>2];
#else
#error "unimplemented"
#endif
//Set the next out to the current out packet
pBDTEntryEP0OutNext = pBDTEntryEP0OutCurrent;
//Toggle it to the next ping pong buffer (if applicable)
((BYTE_VAL*)&pBDTEntryEP0OutNext)->Val ^= USB_NEXT_EP0_OUT_PING_PONG;
//If the current EP0 OUT buffer has a SETUP packet
if(pBDTEntryEP0OutCurrent->STAT.PID == PID_SETUP)
{
//Check if the SETUP transaction data went into the CtrlTrfData buffer.
//If so, need to copy it to the SetupPkt buffer so that it can be
//processed correctly by USBCtrlTrfSetupHandler().
if(pBDTEntryEP0OutCurrent->ADR == (BYTE*)ConvertToPhysicalAddress(&CtrlTrfData))
{
unsigned char setup_cnt;
pBDTEntryEP0OutCurrent->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
for(setup_cnt = 0; setup_cnt < sizeof(CTRL_TRF_SETUP); setup_cnt++)
{
*(((BYTE*)&SetupPkt)+setup_cnt) = *(((BYTE*)&CtrlTrfData)+setup_cnt);
}//end for
}
//Handle the control transfer (parse the 8-byte SETUP command and figure out what to do)
USBCtrlTrfSetupHandler();
}
else
{
//Handle the DATA transfer
USBCtrlTrfOutHandler();
}
}
else if((USTATcopy & USTAT_EP0_PP_MASK) == USTAT_EP0_IN)
{
//Otherwise the transmission was and EP0 IN
// so take care of the IN transfer
USBCtrlTrfInHandler();
}
}//end USBCtrlEPService
/********************************************************************
* Function: void USBCtrlTrfSetupHandler(void)
*
* PreCondition: SetupPkt buffer is loaded with valid USB Setup Data
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine is a task dispatcher and has 3 stages.
* 1. It initializes the control transfer state machine.
* 2. It calls on each of the module that may know how to
* service the Setup Request from the host.
* Module Example: USBD, HID, CDC, MSD, ...
* A callback function, USBCBCheckOtherReq(),
* is required to call other module handlers.
* 3. Once each of the modules has had a chance to check if
* it is responsible for servicing the request, stage 3
* then checks direction of the transfer to determine how
* to prepare EP0 for the control transfer.
* Refer to USBCtrlEPServiceComplete() for more details.
*
* Note: Microchip USB Firmware has three different states for
* the control transfer state machine:
* 1. WAIT_SETUP
* 2. CTRL_TRF_TX (device sends data to host through IN transactions)
* 3. CTRL_TRF_RX (device receives data from host through OUT transactions)
* Refer to firmware manual to find out how one state
* is transitioned to another.
*
* A Control Transfer is composed of many USB transactions.
* When transferring data over multiple transactions,
* it is important to keep track of data source, data
* destination, and data count. These three parameters are
* stored in pSrc,pDst, and wCount. A flag is used to
* note if the data source is from ROM or RAM.
*
*******************************************************************/
void USBCtrlTrfSetupHandler(void)
{
//if the SIE currently owns the buffer
if(pBDTEntryIn[0]->STAT.UOWN != 0)
{
//give control back to the CPU
// Compensate for after a STALL
pBDTEntryIn[0]->STAT.Val = _UCPU;
}
//Keep track of if a short packet has been sent yet or not
shortPacketStatus = SHORT_PKT_NOT_USED;
/* Stage 1 */
controlTransferState = WAIT_SETUP;
inPipes[0].wCount.Val = 0;
inPipes[0].info.Val = 0;
/* Stage 2 */
USBCheckStdRequest();
USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER(EVENT_EP0_REQUEST,0,0);
/* Stage 3 */
USBCtrlEPServiceComplete();
}//end USBCtrlTrfSetupHandler
/******************************************************************************
* Function: void USBCtrlTrfOutHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine handles an OUT transaction according to
* which control transfer state is currently active.
*
* Note: Note that if the the control transfer was from
* host to device, the session owner should be notified
* at the end of each OUT transaction to service the
* received data.
*
*****************************************************************************/
void USBCtrlTrfOutHandler(void)
{
if(controlTransferState == CTRL_TRF_RX)
{
USBCtrlTrfRxService(); //Copies the newly received data into the appropriate buffer and configures EP0 OUT for next transaction.
}
else //In this case the last OUT transaction must have been a status stage of a CTRL_TRF_TX
{
//Prepare EP0 OUT for the next SETUP transaction, however, it may have
//already been prepared if ping-pong buffering was enabled on EP0 OUT,
//and the last control transfer was of direction: device to host, see
//USBCtrlEPServiceComplete(). If it was already prepared, do not want
//to do anything to the BDT.
USBPrepareForNextSetupTrf();
if(BothEP0OutUOWNsSet == FALSE)
{
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT0|_DTSEN|_BSTALL;
}
else
{
BothEP0OutUOWNsSet = FALSE;
}
}
}
/******************************************************************************
* Function: void USBCtrlTrfInHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine handles an IN transaction according to
* which control transfer state is currently active.
*
* Note: A Set Address Request must not change the acutal address
* of the device until the completion of the control
* transfer. The end of the control transfer for Set Address
* Request is an IN transaction. Therefore it is necessary
* to service this unique situation when the condition is
* right. Macro mUSBCheckAdrPendingState is defined in
* usb9.h and its function is to specifically service this
* event.
*****************************************************************************/
void USBCtrlTrfInHandler(void)
{
BYTE lastDTS;
lastDTS = pBDTEntryIn[0]->STAT.DTS;
//switch to the next ping pong buffer
((BYTE_VAL*)&pBDTEntryIn[0])->Val ^= USB_NEXT_EP0_IN_PING_PONG;
//mUSBCheckAdrPendingState(); // Must check if in ADR_PENDING_STATE
if(USBDeviceState == ADR_PENDING_STATE)
{
U1ADDR = SetupPkt.bDevADR.Val;
if(U1ADDR > 0)
{
USBDeviceState=ADDRESS_STATE;
}
else
{
USBDeviceState=DEFAULT_STATE;
}
}//end if
if(controlTransferState == CTRL_TRF_TX)
{
pBDTEntryIn[0]->ADR = (BYTE *)ConvertToPhysicalAddress(CtrlTrfData);
USBCtrlTrfTxService();
/* v2b fix */
if(shortPacketStatus == SHORT_PKT_SENT)
{
// If a short packet has been sent, don't want to send any more,
// stall next time if host is still trying to read.
pBDTEntryIn[0]->STAT.Val = _USIE|_BSTALL;
}
else
{
if(lastDTS == 0)
{
pBDTEntryIn[0]->STAT.Val = _USIE|_DAT1|_DTSEN;
}
else
{
pBDTEntryIn[0]->STAT.Val = _USIE|_DAT0|_DTSEN;
}
}//end if(...)else
}
else // must have been a CTRL_TRF_RX status stage IN packet
{
USBPrepareForNextSetupTrf();
}
}
/********************************************************************
* Function: void USBPrepareForNextSetupTrf(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: The routine forces EP0 OUT to be ready for a new
* Setup transaction, and forces EP0 IN to be owned
* by CPU.
*
* Note: None
*******************************************************************/
void USBPrepareForNextSetupTrf(void)
{
controlTransferState = WAIT_SETUP;
//Don't need to do anything to EP0 OUT BDT here, as EP0 OUT next is
//already configured and ready to receive a SETUP transaction. This is
//done in the USBCtrlTrfOutHandler() or USBCtrlEPServiceComplete() function,
//depending upon the type of the last control transfer.
pBDTEntryIn[0]->STAT.Val = _UCPU;
{
BDT_ENTRY* p;
p = (BDT_ENTRY*)(((unsigned int)pBDTEntryIn[0])^USB_NEXT_EP0_IN_PING_PONG);
p->STAT.Val = _UCPU;
}
//if someone is still expecting data from the control transfer
// then make sure to terminate that request and let them know that
// they are done
if(outPipes[0].info.bits.busy == 1)
{
if(outPipes[0].pFunc != NULL)
{
outPipes[0].pFunc();
}
outPipes[0].info.bits.busy = 0;
}
}//end USBPrepareForNextSetupTrf
/********************************************************************
* Function: void USBCheckStdRequest(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine checks the setup data packet to see
* if it knows how to handle it
*
* Note: None
*******************************************************************/
void USBCheckStdRequest(void)
{
if(SetupPkt.RequestType != USB_SETUP_TYPE_STANDARD_BITFIELD) return;
switch(SetupPkt.bRequest)
{
case USB_REQUEST_SET_ADDRESS:
inPipes[0].info.bits.busy = 1; // This will generate a zero length packet
USBDeviceState = ADR_PENDING_STATE; // Update state only
/* See USBCtrlTrfInHandler() for the next step */
break;
case USB_REQUEST_GET_DESCRIPTOR:
USBStdGetDscHandler();
break;
case USB_REQUEST_SET_CONFIGURATION:
USBStdSetCfgHandler();
break;
case USB_REQUEST_GET_CONFIGURATION:
inPipes[0].pSrc.bRam = (BYTE*)&USBActiveConfiguration; // Set Source
inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
inPipes[0].wCount.v[0] = 1; // Set data count
inPipes[0].info.bits.busy = 1;
break;
case USB_REQUEST_GET_STATUS:
USBStdGetStatusHandler();
break;
case USB_REQUEST_CLEAR_FEATURE:
case USB_REQUEST_SET_FEATURE:
USBStdFeatureReqHandler();
break;
case USB_REQUEST_GET_INTERFACE:
inPipes[0].pSrc.bRam = (BYTE*)&USBAlternateInterface[SetupPkt.bIntfID]; // Set source
inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
inPipes[0].wCount.v[0] = 1; // Set data count
inPipes[0].info.bits.busy = 1;
break;
case USB_REQUEST_SET_INTERFACE:
inPipes[0].info.bits.busy = 1;
USBAlternateInterface[SetupPkt.bIntfID] = SetupPkt.bAltID;
break;
case USB_REQUEST_SET_DESCRIPTOR:
USB_SET_DESCRIPTOR_HANDLER(EVENT_SET_DESCRIPTOR,0,0);
break;
case USB_REQUEST_SYNCH_FRAME:
default:
break;
}//end switch
}//end USBCheckStdRequest
/********************************************************************
* Function: void USBStdFeatureReqHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine handles the standard SET & CLEAR
* FEATURES requests
*
* Note: None
*******************************************************************/
void USBStdFeatureReqHandler(void)
{
BDT_ENTRY *p;
#if defined(__C32__)
DWORD* pUEP;
#else
unsigned int* pUEP;
#endif
#ifdef USB_SUPPORT_OTG
if ((SetupPkt.bFeature == OTG_FEATURE_B_HNP_ENABLE)&&
(SetupPkt.Recipient == USB_SETUP_RECIPIENT_DEVICE_BITFIELD))
{
inPipes[0].info.bits.busy = 1;
if(SetupPkt.bRequest == USB_REQUEST_SET_FEATURE)
USBOTGEnableHnp();
else
USBOTGDisableHnp();
}
if ((SetupPkt.bFeature == OTG_FEATURE_A_HNP_SUPPORT)&&
(SetupPkt.Recipient == USB_SETUP_RECIPIENT_DEVICE_BITFIELD))
{
inPipes[0].info.bits.busy = 1;
if(SetupPkt.bRequest == USB_REQUEST_SET_FEATURE)
USBOTGEnableSupportHnp();
else
USBOTGDisableSupportHnp();
}
if ((SetupPkt.bFeature == OTG_FEATURE_A_ALT_HNP_SUPPORT)&&
(SetupPkt.Recipient == USB_SETUP_RECIPIENT_DEVICE_BITFIELD))
{
inPipes[0].info.bits.busy = 1;
if(SetupPkt.bRequest == USB_REQUEST_SET_FEATURE)
USBOTGEnableAltHnp();
else
USBOTGDisableAltHnp();
}
#endif
if((SetupPkt.bFeature == USB_FEATURE_DEVICE_REMOTE_WAKEUP)&&
(SetupPkt.Recipient == USB_SETUP_RECIPIENT_DEVICE_BITFIELD))
{
inPipes[0].info.bits.busy = 1;
if(SetupPkt.bRequest == USB_REQUEST_SET_FEATURE)
RemoteWakeup = TRUE;
else
RemoteWakeup = FALSE;
}//end if
if((SetupPkt.bFeature == USB_FEATURE_ENDPOINT_HALT)&&
(SetupPkt.Recipient == USB_SETUP_RECIPIENT_ENDPOINT_BITFIELD)&&
(SetupPkt.EPNum != 0))
{
inPipes[0].info.bits.busy = 1;
/* Must do address calculation here */
if(SetupPkt.EPDir == 0)
{
p = (BDT_ENTRY*)pBDTEntryOut[SetupPkt.EPNum];
}
else
{
p = (BDT_ENTRY*)pBDTEntryIn[SetupPkt.EPNum];
}
//if it was a SET_FEATURE request
if(SetupPkt.bRequest == USB_REQUEST_SET_FEATURE)
{
//Then STALL the endpoint
p->STAT.Val = _USIE|_BSTALL;
}
else
{
//If it was not a SET_FEATURE
//point to the appropriate UEP register
#if defined(__C32__)
pUEP = (DWORD*)(&U1EP0);
pUEP += (SetupPkt.EPNum*4);
#else
pUEP = (unsigned int*)(&U1EP0+SetupPkt.EPNum);
#endif
//Clear the STALL bit in the UEP register
*pUEP &= ~UEP_STALL;
if(SetupPkt.EPDir == 1) // IN
{
//If the endpoint is an IN endpoint then we
// need to return it to the CPU and reset the
// DTS bit so that the next transfer is correct
#if (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0) || (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
p->STAT.Val = _UCPU|_DAT0;
//toggle over the to the next buffer
((BYTE_VAL*)&p)->Val ^= USB_NEXT_PING_PONG;
p->STAT.Val = _UCPU|_DAT1;
#else
p->STAT.Val = _UCPU|_DAT1;
#endif
}
else
{
//If the endpoint was an OUT endpoint then we
// need to give control of the endpoint back to
// the SIE so that the function driver can
// receive the data as they expected. Also need
// to set the DTS bit so the next packet will be
// correct
#if (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0) || (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
p->STAT.Val = _USIE|_DAT0|_DTSEN;
//toggle over the to the next buffer
((BYTE_VAL*)&p)->Val ^= USB_NEXT_PING_PONG;
p->STAT.Val = _USIE|_DAT1|_DTSEN;
#else
p->STAT.Val = _USIE|_DAT1|_DTSEN;
#endif
}
}//end if
}//end if
}//end USBStdFeatureReqHandler
/********************************************************************
* Function: void USBStdGetDscHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine handles the standard GET_DESCRIPTOR
* request.
*
* Note: None
*******************************************************************/
void USBStdGetDscHandler(void)
{
if(SetupPkt.bmRequestType == 0x80)
{
inPipes[0].info.Val = USB_EP0_ROM | USB_EP0_BUSY | USB_EP0_INCLUDE_ZERO;
switch(SetupPkt.bDescriptorType)
{
case USB_DESCRIPTOR_DEVICE:
#if !defined(USB_USER_DEVICE_DESCRIPTOR)
inPipes[0].pSrc.bRom = (ROM BYTE*)&device_dsc;
#else
inPipes[0].pSrc.bRom = (ROM BYTE*)USB_USER_DEVICE_DESCRIPTOR;
#endif
inPipes[0].wCount.Val = sizeof(device_dsc);
break;
case USB_DESCRIPTOR_CONFIGURATION:
#if !defined(USB_USER_CONFIG_DESCRIPTOR)
inPipes[0].pSrc.bRom = *(USB_CD_Ptr+SetupPkt.bDscIndex);
#else
inPipes[0].pSrc.bRom = *(USB_USER_CONFIG_DESCRIPTOR+SetupPkt.bDscIndex);
#endif
inPipes[0].wCount.Val = *(inPipes[0].pSrc.wRom+1); // Set data count
break;
case USB_DESCRIPTOR_STRING:
//USB_NUM_STRING_DESCRIPTORS was introduced as optional in release v2.3. In v2.4 and
// later it is now manditory. This should be defined in usb_config.h and should
// indicate the number of string descriptors.
if(SetupPkt.bDscIndexSTAT.Val & _BSTALL) // Use _BSTALL as a bit mask
CtrlTrfData[0]=0x01; // Set bit0
break;
}
}//end switch
if(inPipes[0].info.bits.busy == 1)
{
inPipes[0].pSrc.bRam = (BYTE*)&CtrlTrfData; // Set Source
inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
inPipes[0].wCount.v[0] = 2; // Set data count
}//end if(...)
}//end USBStdGetStatusHandler
/******************************************************************************
* Function: void USBCtrlEPServiceComplete(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine wrap up the remaining tasks in servicing
* a Setup Request. Its main task is to set the endpoint
* controls appropriately for a given situation. See code
* below.
* There are three main scenarios:
* a) There was no handler for the Request, in this case
* a STALL should be sent out.
* b) The host has requested a read control transfer,
* endpoints are required to be setup in a specific way.
* c) The host has requested a write control transfer, or
* a control data stage is not required, endpoints are
* required to be setup in a specific way.
*
* Packet processing is resumed by clearing PKTDIS bit.
*
* Note: None
*****************************************************************************/
void USBCtrlEPServiceComplete(void)
{
/*
* PKTDIS bit is set when a Setup Transaction is received.
* Clear to resume packet processing.
*/
USBPacketDisable = 0;
if(inPipes[0].info.bits.busy == 0)
{
if(outPipes[0].info.bits.busy == 1)
{
controlTransferState = CTRL_TRF_RX;
/*
* Control Write:
* ... |
*
* 1. Prepare IN EP to respond to early termination
*
* This is the same as a Zero Length Packet Response
* for control transfer without a data stage
*/
pBDTEntryIn[0]->CNT = 0;
pBDTEntryIn[0]->STAT.Val = _USIE|_DAT1|_DTSEN;
/*
* 2. Prepare OUT EP to receive data.
*/
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&CtrlTrfData);
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT1|_DTSEN;
}
else
{
/*
* If no one knows how to service this request then stall.
* Must also prepare EP0 to receive the next SETUP transaction.
*/
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT0|_DTSEN|_BSTALL;
pBDTEntryIn[0]->STAT.Val = _USIE|_BSTALL;
}
}
else // A module has claimed ownership of the control transfer session.
{
if(outPipes[0].info.bits.busy == 0)
{
if(SetupPkt.DataDir == USB_SETUP_DEVICE_TO_HOST_BITFIELD)
{
if(SetupPkt.wLength < inPipes[0].wCount.Val)
{
inPipes[0].wCount.Val = SetupPkt.wLength;
}
USBCtrlTrfTxService();
controlTransferState = CTRL_TRF_TX;
/*
* Control Read:
* ... |
* 1. Prepare OUT EP to respond to early termination
*
* NOTE:
* If something went wrong during the control transfer,
* the last status stage may not be sent by the host.
* When this happens, two different things could happen
* depending on the host.
* a) The host could send out a RESET.
* b) The host could send out a new SETUP transaction
* without sending a RESET first.
* To properly handle case (b), the OUT EP must be setup
* to receive either a zero length OUT transaction, or a
* new SETUP transaction.
*
* Furthermore, the Cnt byte should be set to prepare for
* the SETUP data (8-byte or more), and the buffer address
* should be pointed to SetupPkt.
*/
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
pBDTEntryEP0OutNext->STAT.Val = _USIE; // Note: DTSEN is 0
BothEP0OutUOWNsSet = FALSE; //Indicator flag used in USBCtrlTrfOutHandler()
#if (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY) || (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
pBDTEntryEP0OutCurrent->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutCurrent->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
pBDTEntryEP0OutCurrent->STAT.Val = _USIE|_BSTALL; //Prepare endpoint to accept a SETUP transaction
BothEP0OutUOWNsSet = TRUE; //Indicator flag used in USBCtrlTrfOutHandler()
#endif
/*
* 2. Prepare IN EP to transfer data, Cnt should have
* been initialized by responsible request owner.
*/
pBDTEntryIn[0]->ADR = (BYTE*)ConvertToPhysicalAddress(&CtrlTrfData);
pBDTEntryIn[0]->STAT.Val = _USIE|_DAT1|_DTSEN;
}
else // (SetupPkt.DataDir == USB_SETUP_DIRECTION_HOST_TO_DEVICE)
{
controlTransferState = CTRL_TRF_RX;
/*
* Control Write:
* ... |
*
* 1. Prepare IN EP to respond to early termination
*
* This is the same as a Zero Length Packet Response
* for control transfer without a data stage
*/
pBDTEntryIn[0]->CNT = 0;
pBDTEntryIn[0]->STAT.Val = _USIE|_DAT1|_DTSEN;
/*
* 2. Prepare OUT EP to receive data.
*/
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&CtrlTrfData);
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT1|_DTSEN;
}
}
}//end if(ctrl_trf_session_owner == MUID_NULL)
}//end USBCtrlEPServiceComplete
/******************************************************************************
* Function: void USBCtrlTrfTxService(void)
*
* PreCondition: pSrc, wCount, and usb_stat.ctrl_trf_mem are setup properly.
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine is used for device to host control transfers
* (IN transactions). This function takes care of managing a
* transfer over multiple USB transactions.
* This routine should be called from only two places.
* One from USBCtrlEPServiceComplete() and one from
* USBCtrlTrfInHandler().
*
* Note: This routine works with isochronous endpoint larger than
* 256 bytes and is shown here as an example of how to deal
* with BC9 and BC8. In reality, a control endpoint can never
* be larger than 64 bytes.
*****************************************************************************/
void USBCtrlTrfTxService(void)
{
WORD_VAL byteToSend;
/*
* First, have to figure out how many byte of data to send.
*/
if(inPipes[0].wCount.Val < USB_EP0_BUFF_SIZE)
{
byteToSend.Val = inPipes[0].wCount.Val;
/* v2b fix */
if(shortPacketStatus == SHORT_PKT_NOT_USED)
{
shortPacketStatus = SHORT_PKT_PENDING;
}
else if(shortPacketStatus == SHORT_PKT_PENDING)
{
shortPacketStatus = SHORT_PKT_SENT;
}//end if
/* end v2b fix for this section */
}
else
{
byteToSend.Val = USB_EP0_BUFF_SIZE;
}
/*
* Next, load the number of bytes to send to BC9..0 in buffer descriptor
*/
#if defined(__18CXX)
pBDTEntryIn[0]->STAT.BC9 = 0;
pBDTEntryIn[0]->STAT.BC8 = 0;
#endif
#if defined(__18CXX) || defined(__C30__)
pBDTEntryIn[0]->STAT.Val |= byteToSend.byte.HB;
pBDTEntryIn[0]->CNT = byteToSend.byte.LB;
#elif defined(__C32__)
pBDTEntryIn[0]->CNT = byteToSend.Val;
#else
#error "Not defined for this compiler"
#endif
/*
* Subtract the number of bytes just about to be sent from the total.
*/
inPipes[0].wCount.Val = inPipes[0].wCount.Val - byteToSend.Val;
pDst = (USB_VOLATILE BYTE*)CtrlTrfData; // Set destination pointer
if(inPipes[0].info.bits.ctrl_trf_mem == USB_EP0_ROM) // Determine type of memory source
{
while(byteToSend.Val)
{
*pDst++ = *inPipes[0].pSrc.bRom++;
byteToSend.Val--;
}//end while(byte_to_send.Val)
}
else // RAM
{
while(byteToSend.Val)
{
*pDst++ = *inPipes[0].pSrc.bRam++;
byteToSend.Val--;
}//end while(byte_to_send.Val)
}//end if(usb_stat.ctrl_trf_mem == _ROM)
}//end USBCtrlTrfTxService
/******************************************************************************
* Function: void USBCtrlTrfRxService(void)
*
* PreCondition: pDst and wCount are setup properly.
* pSrc is always &CtrlTrfData
* usb_stat.ctrl_trf_mem is always USB_EP0_RAM.
* wCount should be set to 0 at the start of each control
* transfer.
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine is used for host to device control transfers
* (uses OUT transactions). This function receives the data that arrives
* on EP0 OUT, and copies it into the appropriate outPipes[0].pDst.bRam
* buffer. Once the host has sent all the data it was intending
* to send, this function will call the appropriate outPipes[0].pFunc()
* handler (unless it is NULL), so that it can be used by the
* intended target firmware.
*
* Note: None
*****************************************************************************/
void USBCtrlTrfRxService(void)
{
BYTE byteToRead;
BYTE i;
byteToRead = pBDTEntryEP0OutCurrent->CNT;
/*
* Accumulate total number of bytes read
*/
if(byteToRead > outPipes[0].wCount.Val)
{
byteToRead = outPipes[0].wCount.Val;
}
else
{
outPipes[0].wCount.Val = outPipes[0].wCount.Val - byteToRead;
}
for(i=0;i 0)
{
/*
* Don't have to worry about overwriting _KEEP bit
* because if _KEEP was set, TRNIF would not have been
* generated in the first place.
*/
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&CtrlTrfData);
if(pBDTEntryEP0OutCurrent->STAT.DTS == 0)
{
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT1|_DTSEN;
}
else
{
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT0|_DTSEN;
}
}
else
{
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
//Configure EP0 OUT to receive the next SETUP transaction for any future
//control transfers. However, set BSTALL in case the host tries to send
//more data than it claims it was going to send.
pBDTEntryEP0OutNext->STAT.Val = _USIE|_BSTALL;
if(outPipes[0].pFunc != NULL)
{
outPipes[0].pFunc();
}
outPipes[0].info.bits.busy = 0;
}
}//end USBCtrlTrfRxService
/********************************************************************
* Function: void USBStdSetCfgHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This routine first disables all endpoints by
* clearing UEP registers. It then configures
* (initializes) endpoints by calling the callback
* function USBCBInitEP().
*
* Note: None
*******************************************************************/
void USBStdSetCfgHandler(void)
{
// This will generate a zero length packet
inPipes[0].info.bits.busy = 1;
//disable all endpoints except endpoint 0
memset((void*)&U1EP1,0x00,(USB_MAX_EP_NUMBER-1));
//clear the alternate interface settings
memset((void*)&USBAlternateInterface,0x00,USB_MAX_NUM_INT);
//set the current configuration
USBActiveConfiguration = SetupPkt.bConfigurationValue;
//if the configuration value == 0
if(USBActiveConfiguration == 0)
{
//Go back to the addressed state
USBDeviceState = ADDRESS_STATE;
}
else
{
//Otherwise go to the configured state
USBDeviceState = CONFIGURED_STATE;
//initialize the required endpoints
USB_SET_CONFIGURATION_HANDLER(EVENT_CONFIGURED,(void*)&USBActiveConfiguration,1);
}//end if(SetupPkt.bConfigurationValue == 0)
}//end USBStdSetCfgHandler
/********************************************************************
* Function: void USBConfigureEndpoint(BYTE EPNum, BYTE direction)
*
* PreCondition: None
*
* Input: BYTE EPNum - the endpoint to be configured
* BYTE direction - the direction to be configured
*
* Output: None
*
* Side Effects: None
*
* Overview: This function will configure the specified
* endpoint
*
* Note: None
*******************************************************************/
void USBConfigureEndpoint(BYTE EPNum, BYTE direction)
{
volatile BDT_ENTRY* handle;
#if defined(PICC18PORT)
unsigned char workaround;
#endif
handle = (volatile BDT_ENTRY*)&BDT[EP0_OUT_EVEN];
#if defined(PICC18PORT)
// This is in here because in HI-TECH C it throws implicit signed to unsigned
// conversion error if you try to do it all in one line. There is no reason
// for this... looks like it maybe a compiler bug/feature ;)
workaround = BD(EPNum,direction,0);
workaround = workaround/sizeof(BDT_ENTRY);
handle += workaround;
#else
handle += BD(EPNum,direction,0)/sizeof(BDT_ENTRY);
#endif
handle->STAT.UOWN = 0;
if(direction == 0)
{
pBDTEntryOut[EPNum] = handle;
}
else
{
pBDTEntryIn[EPNum] = handle;
}
#if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
handle->STAT.DTS = 0;
(handle+1)->STAT.DTS = 1;
#elif (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG)
//Set DTS to one because the first thing we will do
//when transmitting is toggle the bit
handle->STAT.DTS = 1;
#elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY)
if(EPNum != 0)
{
handle->STAT.DTS = 1;
}
#elif (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0)
if(EPNum != 0)
{
handle->STAT.DTS = 0;
(handle+1)->STAT.DTS = 1;
}
#endif
}
/*****************************************************************************************************************
Function:
void USBEnableEndpoint(BYTE ep, BYTE options)
Summary:
This function will enable the specified endpoint with the specified
options
Description:
This function will enable the specified endpoint with the specified
options.
Typical Usage:
void USBCBInitEP(void)
{
USBEnableEndpoint(MSD_DATA_IN_EP,USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
USBMSDInit();
}
In the above example endpoint number MSD_DATA_IN_EP is being configured
for both IN and OUT traffic with handshaking enabled. Also since
MSD_DATA_IN_EP is not endpoint 0 (MSD does not allow this), then we can
explicitly disable SETUP packets on this endpoint.
Conditions:
None
Input:
BYTE ep - the endpoint to be configured
BYTE options - optional settings for the endpoint. The options should
be ORed together to form a single options string. The
available optional settings for the endpoint. The
options should be ORed together to form a single options
string. The available options are the following\:
* USB_HANDSHAKE_ENABLED enables USB handshaking (ACK,
NAK)
* USB_HANDSHAKE_DISABLED disables USB handshaking (ACK,
NAK)
* USB_OUT_ENABLED enables the out direction
* USB_OUT_DISABLED disables the out direction
* USB_IN_ENABLED enables the in direction
* USB_IN_DISABLED disables the in direction
* USB_ALLOW_SETUP enables control transfers
* USB_DISALLOW_SETUP disables control transfers
* USB_STALL_ENDPOINT STALLs this endpoint
Return:
None
Remarks:
None
*****************************************************************************************************************/
void USBEnableEndpoint(BYTE ep, BYTE options)
{
//Set the options to the appropriate endpoint control register
//*((unsigned char*)(&U1EP0+ep)) = options;
{
unsigned char* p;
#if defined(__C32__)
p = (unsigned char*)(&U1EP0+(4*ep));
#else
p = (unsigned char*)(&U1EP0+ep);
#endif
*p = options;
}
if(options & USB_OUT_ENABLED)
{
USBConfigureEndpoint(ep,0);
}
if(options & USB_IN_ENABLED)
{
USBConfigureEndpoint(ep,1);
}
}
/********************************************************************
* Function: void USBStallEndpoint(BYTE ep, BYTE dir)
*
* PreCondition: None
*
* Input:
* BYTE ep - the endpoint the data will be transmitted on
* BYTE dir - the direction of the transfer
*
* Output: None
*
* Side Effects: Endpoint is STALLed
*
* Overview: STALLs the specified endpoint
*
* Note: None
*******************************************************************/
void USBStallEndpoint(BYTE ep, BYTE dir)
{
BDT_ENTRY *p;
if(ep == 0)
{
/*
* If no one knows how to service this request then stall.
* Must also prepare EP0 to receive the next SETUP transaction.
*/
pBDTEntryEP0OutNext->CNT = USB_EP0_BUFF_SIZE;
pBDTEntryEP0OutNext->ADR = (BYTE*)ConvertToPhysicalAddress(&SetupPkt);
/* v2b fix */
pBDTEntryEP0OutNext->STAT.Val = _USIE|_DAT0|_DTSEN|_BSTALL;
pBDTEntryIn[0]->STAT.Val = _USIE|_BSTALL;
}
else
{
p = (BDT_ENTRY*)(&BDT[EP(ep,dir,0)]);
p->STAT.Val |= _BSTALL | _USIE;
//If the device is in FULL or ALL_BUT_EP0 ping pong modes
//then stall that entry as well
#if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) || \
(USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0)
p = (BDT_ENTRY*)(&BDT[EP(ep,dir,1)]);
p->STAT.Val |= _BSTALL | _USIE;
#endif
}
}
/********************************************************************
* Function: USB_HANDLE USBTransferOnePacket(
* BYTE ep,
* BYTE dir,
* BYTE* data,
* BYTE len)
*
* PreCondition: None
*
* Input:
* BYTE ep - the endpoint the data will be transmitted on
* BYTE dir - the direction of the transfer
This value is either OUT_FROM_HOST or IN_TO_HOST
* BYTE* data - pointer to the data to be sent
* BYTE len - length of the data needing to be sent
*
* Output:
* USB_HANDLE - handle to the transfer
*
* Side Effects: None
*
* Overview: Transfers one packet over the USB
*
* Note: None
*******************************************************************/
USB_HANDLE USBTransferOnePacket(BYTE ep,BYTE dir,BYTE* data,BYTE len)
{
volatile BDT_ENTRY* handle;
//If the direction is IN
if(dir != 0)
{
//point to the IN BDT of the specified endpoint
handle = pBDTEntryIn[ep];
}
else
{
//else point to the OUT BDT of the specified endpoint
handle = pBDTEntryOut[ep];
}
//Toggle the DTS bit if required
#if (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG)
handle->STAT.Val ^= _DTSMASK;
#elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY)
if(ep != 0)
{
handle->STAT.Val ^= _DTSMASK;
}
#endif
//Set the data pointer, data length, and enable the endpoint
handle->ADR = (BYTE*)ConvertToPhysicalAddress(data);
handle->CNT = len;
handle->STAT.Val &= _DTSMASK;
handle->STAT.Val |= _USIE | _DTSEN;
//Point to the next buffer for ping pong purposes.
if(dir != 0)
{
//toggle over the to the next buffer for an IN endpoint
((BYTE_VAL*)&pBDTEntryIn[ep])->Val ^= USB_NEXT_PING_PONG;
}
else
{
//toggle over the to the next buffer for an OUT endpoint
((BYTE_VAL*)&pBDTEntryOut[ep])->Val ^= USB_NEXT_PING_PONG;
}
return (USB_HANDLE)handle;
}
/**************************************************************************
Function:
void USBCancelIO(BYTE endpoint)
Description:
This function cancels the transfers pending on the specified endpoint.
This function can only be used after a SETUP packet is received and
before that setup packet is handled. This is the time period in which
the EVENT_EP0_REQUEST is thrown, before the event handler function
returns to the stack.
Precondition:
Parameters:
BYTE endpoint - the endpoint number you wish to cancel the transfers for
Return Values:
None
Remarks:
None
**************************************************************************/
void USBCancelIO(BYTE endpoint)
{
if(USBPacketDisable == 1)
{
//The PKTDIS bit is currently set right now. It is therefore "safe"
//to mess with the BDT right now.
pBDTEntryIn[endpoint]->Val &= _DTSMASK; //Makes UOWN = 0 (_UCPU mode). Deactivates endpoint. Only sends NAKs.
pBDTEntryIn[endpoint]->Val ^= _DTSMASK; //Toggle the DTS bit. This packet didn't get sent yet, and the next call to USBTransferOnePacket() will re-toggle the DTS bit back to the original (correct) value.
//Need to do additional handling if ping-pong buffering is being used
#if ((USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) || (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0))
//Point to the next buffer for ping pong purposes. UOWN getting cleared
//(either due to SIE clearing it after a transaction, or the firmware
//clearing it) makes hardware ping pong pointer advance.
((BYTE_VAL*)&pBDTEntryIn[endpoint])->Val ^= USB_NEXT_PING_PONG;
pBDTEntryIn[endpoint]->STAT.Val &= _DTSMASK;
pBDTEntryIn[endpoint]->STAT.Val ^= _DTSMASK;
#endif
}
}
/**************************************************************************
Function:
void USBDeviceDetach(void)
Description:
Precondition:
Parameters:
None
Return Values:
None
Remarks:
None
**************************************************************************/
#if defined(USB_INTERRUPT)
void USBDeviceDetach(void)
{
//If the interrupt option is selected then the customer is required
// to notify the stack when the device is attached or removed from the
// bus by calling the USBDeviceAttach() and USBDeviceDetach() functions.
if (USB_BUS_SENSE != 1)
{
// Disable module & detach from bus
U1CON = 0;
// Mask all USB interrupts
U1IE = 0;
//Move to the detached state
USBDeviceState = DETACHED_STATE;
#ifdef USB_SUPPORT_OTG
//Disable D+ Pullup
U1OTGCONbits.DPPULUP = 0;
//Disable HNP
USBOTGDisableHnp();
//Deactivate HNP
USBOTGDeactivateHnp();
//If ID Pin Changed State
if (USBIDIF && USBIDIE)
{
//Re-detect & Initialize
USBOTGInitialize();
//Clear ID Interrupt Flag
USBClearInterruptFlag(USBIDIFReg,USBIDIFBitNum);
}
#endif
#ifdef __C30__
//USBClearInterruptFlag(U1OTGIR, 3);
#endif
//return so that we don't go through the rest of
//the state machine
return;
}
#ifdef USB_SUPPORT_OTG
//If Session Is Started Then
else
{
//If SRP Is Ready
if (USBOTGSRPIsReady())
{
//Clear SRPReady
USBOTGClearSRPReady();
//Clear SRP Timeout Flag
USBOTGClearSRPTimeOutFlag();
//Indicate Session Started
UART2PrintString( "\r\n***** USB OTG B Event - Session Started *****\r\n" );
}
}
#endif
}
#endif //#if defined(USB_INTERRUPT)
/**************************************************************************
Function:
void USBDeviceAttach(void)
Description:
Precondition:
For normal USB devices:
Make sure that if the module was previously on, that it has been turned off
for a long time (ex: 100ms+) before calling this function to re-enable the module.
If the device turns off the D+ (for full speed) or D- (for low speed) ~1.5k ohm
pull up resistor, and then turns it back on very quickly, common hosts will sometimes
reject this event, since no human could ever unplug and reattach a USB device in a
microseconds (or nanoseconds) timescale. The host could simply treat this as some kind
of glitch and ignore the event altogether.
Parameters:
None
Return Values:
None
Remarks:
None
**************************************************************************/
#if defined(USB_INTERRUPT)
void USBDeviceAttach(void)
{
//if we are in the detached state
if(USBDeviceState == DETACHED_STATE)
{
//Initialize registers to known states.
U1CON = 0;
// Mask all USB interrupts
U1IE = 0;
//Configure things like: pull ups, full/low-speed mode,
//set the ping pong mode, and set internal transceiver
SetConfigurationOptions();
USBEnableInterrupts();
// Enable module & attach to bus
while(!U1CONbits.USBEN){U1CONbits.USBEN = 1;}
//moved to the attached state
USBDeviceState = ATTACHED_STATE;
#ifdef USB_SUPPORT_OTG
U1OTGCON = USB_OTG_DPLUS_ENABLE | USB_OTG_ENABLE;
#endif
}
}
#endif //#if defined(USB_INTERRUPT)
/** EOF USBDevice.c *****************************************************/
PK a ( USB Performance Meter\usb_function_hid.c/********************************************************************
File Information:
FileName: usb_function_hid.c
Dependencies: See INCLUDES section
Processor: PIC18 or PIC24 USB Microcontrollers
Hardware: The code is natively intended to be used on the following
hardware platforms: PICDEM FS USB Demo Board,
PIC18F87J50 FS USB Plug-In Module, or
Explorer 16 + PIC24 USB PIM. The firmware may be
modified for use on other USB platforms by editing the
HardwareProfile.h file.
Complier: Microchip C18 (for PIC18) or C30 (for PIC24)
Company: Microchip Technology, Inc.
Software License Agreement:
The software supplied herewith by Microchip Technology Incorporated
(the Company) for its PIC Microcontroller is intended and
supplied to you, the Companys customer, for use solely and
exclusively on Microchip PIC Microcontroller products. The
software is owned by the Company and/or its supplier, and is
protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the
user to criminal sanctions under applicable laws, as well as to
civil liability for the breach of the terms and conditions of this
license.
THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
File Description:
Change History:
Rev Date Description
1.0 11/19/2004 Initial release
2.1 02/26/2007 Updated for simplicity and to use common
coding style
Summary:
This file contains all of functions, macros, definitions, variables,
datatypes, etc. that are required for usage with the HID function
driver. This file should be included in projects that use the HID
\function driver.
This file is located in the "\\\Microchip\\USB\\HID
Device Driver" directory.
Description:
USB HID Function Driver File
This file contains all of functions, macros, definitions, variables,
datatypes, etc. that are required for usage with the HID function
driver. This file should be included in projects that use the HID
\function driver.
This file is located in the "\\\Microchip\\USB\\HID
Device Driver" directory.
When including this file in a new project, this file can either be
referenced from the directory in which it was installed or copied
directly into the user application folder. If the first method is
chosen to keep the file located in the folder in which it is installed
then include paths need to be added so that the library and the
application both know where to reference each others files. If the
application folder is located in the same folder as the Microchip
folder (like the current demo folders), then the following include
paths need to be added to the application's project:
..\\Include
..\\..\\Include
..\\..\\Microchip\\Include
..\\..\\\
..\\..\\..\\\
If a different directory structure is used, modify the paths as
required. An example using absolute paths instead of relative paths
would be the following:
C:\\Microchip Solutions\\Microchip\\Include
C:\\Microchip Solutions\\My Demo Application
*******************************************************************/
#ifndef USB_FUNCTION_HID_C
#define USB_FUNCTION_HID_C
/** INCLUDES *******************************************************/
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb.h"
#include "usb_function_hid.h"
/** VARIABLES ******************************************************/
#if !defined(PICC18PORT)
#pragma udata
#endif
BYTE idle_rate;
BYTE active_protocol; // [0] Boot Protocol [1] Report Protocol
/** EXTERNAL PROTOTYPES ********************************************/
#if defined USER_GET_REPORT_HANDLER
void USER_GET_REPORT_HANDLER(void);
#endif
#if defined USER_SET_REPORT_HANDLER
void USER_SET_REPORT_HANDLER(void);
#endif
/** Section: DECLARATIONS ***************************************************/
#if !defined(PICC18PORT)
#pragma code
#endif
/** Section: CLASS SPECIFIC REQUESTS ****************************************/
/********************************************************************
Function:
void USBCheckHIDRequest(void)
Summary:
This routine handles HID specific request that happen on EP0.
This function should be called from the USBCBCheckOtherReq() call back
function whenever implementing a HID device.
Description:
This routine handles HID specific request that happen on EP0. These
include, but are not limited to, requests for the HID report
descriptors. This function should be called from the
USBCBCheckOtherReq() call back function whenever using an HID device.
Typical Usage:
void USBCBCheckOtherReq(void)
{
//Since the stack didn't handle the request I need to check
// my class drivers to see if it is for them
USBCheckHIDRequest();
}
PreCondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
*******************************************************************/
void USBCheckHIDRequest(void)
{
if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return;
if(SetupPkt.bIntfID != HID_INTF_ID) return;
/*
* There are two standard requests that hid.c may support.
* 1. GET_DSC(DSC_HID,DSC_RPT,DSC_PHY);
* 2. SET_DSC(DSC_HID,DSC_RPT,DSC_PHY);
*/
if(SetupPkt.bRequest == USB_REQUEST_GET_DESCRIPTOR)
{
switch(SetupPkt.bDescriptorType)
{
case DSC_HID:
if(USBActiveConfiguration == 1)
{
USBEP0SendROMPtr(
(ROM BYTE*)&configDescriptor1 + 18,
sizeof(USB_HID_DSC)+3,
USB_EP0_INCLUDE_ZERO);
}
break;
case DSC_RPT:
if(USBActiveConfiguration == 1)
{
USBEP0SendROMPtr(
(ROM BYTE*)&hid_rpt01,
sizeof(hid_rpt01), //See usbcfg.h
USB_EP0_INCLUDE_ZERO);
}
break;
case DSC_PHY:
USBEP0Transmit(USB_EP0_NO_DATA);
break;
}//end switch(SetupPkt.bDescriptorType)
}//end if(SetupPkt.bRequest == GET_DSC)
if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD)
{
return;
}
switch(SetupPkt.bRequest)
{
case GET_REPORT:
#if defined USER_GET_REPORT_HANDLER
USER_GET_REPORT_HANDLER();
#endif
break;
case SET_REPORT:
#if defined USER_SET_REPORT_HANDLER
USER_SET_REPORT_HANDLER();
#endif
break;
case GET_IDLE:
USBEP0SendRAMPtr(
(BYTE*)&idle_rate,
1,
USB_EP0_INCLUDE_ZERO);
break;
case SET_IDLE:
USBEP0Transmit(USB_EP0_NO_DATA);
idle_rate = SetupPkt.W_Value.byte.HB;
break;
case GET_PROTOCOL:
USBEP0SendRAMPtr(
(BYTE*)&active_protocol,
1,
USB_EP0_NO_OPTIONS);
break;
case SET_PROTOCOL:
USBEP0Transmit(USB_EP0_NO_DATA);
active_protocol = SetupPkt.W_Value.byte.LB;
break;
}//end switch(SetupPkt.bRequest)
}//end USBCheckHIDRequest
/** USER API *******************************************************/
#endif
/** EOF usb_function_hid.c ******************************************************/
PK r"3= = # USB Performance Meter\usbcallback.c// usbcallback.c
#include "usb.h"
#include "genericHID.h"
#include "HardwareProfile.h"
#include "usb_function_hid.h"
#include "GenericTypeDefs.h"
#include "usb.h"
// ******************************************************************************************************
// ************** USB Callback Functions ****************************************************************
// ******************************************************************************************************
// The USB firmware stack will call the callback functions USBCBxxx() in response to certain USB related
// events. For example, if the host PC is powering down, it will stop sending out Start of Frame (SOF)
// packets to your device. In response to this, all USB devices are supposed to decrease their power
// consumption from the USB Vbus to <2.5mA each. The USB module detects this condition (which according
// to the USB specifications is 3+ms of no bus activity/SOF packets) and then calls the USBCBSuspend()
// function. You should modify these callback functions to take appropriate actions for each of these
// conditions. For example, in the USBCBSuspend(), you may wish to add code that will decrease power
// consumption from Vbus to <2.5mA (such as by clock switching, turning off LEDs, putting the
// microcontroller to sleep, etc.). Then, in the USBCBWakeFromSuspend() function, you may then wish to
// add code that undoes the power saving things done in the USBCBSuspend() function.
// The USBCBSendResume() function is special, in that the USB stack will not automatically call this
// function. This function is meant to be called from the application firmware instead. See the
// additional comments near the function.
/******************************************************************************
* Function: void USBCBSuspend(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Call back that is invoked when a USB suspend is detected
*
* Note: None
*****************************************************************************/
void USBCBSuspend(void)
{
//Example power saving code. Insert appropriate code here for the desired
//application behavior. If the microcontroller will be put to sleep, a
//process similar to that shown below may be used:
//ConfigureIOPinsForLowPower();
//SaveStateOfAllInterruptEnableBits();
//DisableAllInterruptEnableBits();
//EnableOnlyTheInterruptsWhichWillBeUsedToWakeTheMicro(); //should enable at least USBActivityIF as a wake source
//Sleep();
//RestoreStateOfAllPreviouslySavedInterruptEnableBits(); //Preferrably, this should be done in the USBCBWakeFromSuspend() function instead.
//RestoreIOPinsToNormal(); //Preferrably, this should be done in the USBCBWakeFromSuspend() function instead.
//IMPORTANT NOTE: Do not clear the USBActivityIF (ACTVIF) bit here. This bit is
//cleared inside the usb_device.c file. Clearing USBActivityIF here will cause
//things to not work as intended.
#if defined(__C30__)
#if 0
U1EIR = 0xFFFF;
U1IR = 0xFFFF;
U1OTGIR = 0xFFFF;
IFS5bits.USB1IF = 0;
IEC5bits.USB1IE = 1;
U1OTGIEbits.ACTVIE = 1;
U1OTGIRbits.ACTVIF = 1;
Sleep();
#endif
#endif
}
/******************************************************************************
* Function: void _USB1Interrupt(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is called when the USB interrupt bit is set
* In this example the interrupt is only used when the device
* goes to sleep when it receives a USB suspend command
*
* Note: None
*****************************************************************************/
#if 0
void __attribute__ ((interrupt)) _USB1Interrupt(void)
{
#if !defined(self_powered)
if(U1OTGIRbits.ACTVIF)
{
IEC5bits.USB1IE = 0;
U1OTGIEbits.ACTVIE = 0;
IFS5bits.USB1IF = 0;
//USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
//USBSuspendControl = 0;
}
#endif
}
#endif
/******************************************************************************
* Function: void USBCBWakeFromSuspend(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: The host may put USB peripheral devices in low power
* suspend mode (by "sending" 3+ms of idle). Once in suspend
* mode, the host may wake the device back up by sending non-
* idle state signalling.
*
* This call back is invoked when a wakeup from USB suspend
* is detected.
*
* Note: None
*****************************************************************************/
void USBCBWakeFromSuspend(void)
{
// If clock switching or other power savings measures were taken when
// executing the USBCBSuspend() function, now would be a good time to
// switch back to normal full power run mode conditions. The host allows
// a few milliseconds of wakeup time, after which the device must be
// fully back to normal, and capable of receiving and processing USB
// packets. In order to do this, the USB module must receive proper
// clocking (IE: 48MHz clock must be available to SIE for full speed USB
// operation).
}
/********************************************************************
* Function: void USBCB_SOF_Handler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: The USB host sends out a SOF packet to full-speed
* devices every 1 ms. This interrupt may be useful
* for isochronous pipes. End designers should
* implement callback routine as necessary.
*
* Note: None
*******************************************************************/
void USBCB_SOF_Handler(void)
{
// No need to clear UIRbits.SOFIF to 0 here.
// Callback caller is already doing that.
}
/*******************************************************************
* Function: void USBCBErrorHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: The purpose of this callback is mainly for
* debugging during development. Check UEIR to see
* which error causes the interrupt.
*
* Note: None
*******************************************************************/
void USBCBErrorHandler(void)
{
// No need to clear UEIR to 0 here.
// Callback caller is already doing that.
// Typically, user firmware does not need to do anything special
// if a USB error occurs. For example, if the host sends an OUT
// packet to your device, but the packet gets corrupted (ex:
// because of a bad connection, or the user unplugs the
// USB cable during the transmission) this will typically set
// one or more USB error interrupt flags. Nothing specific
// needs to be done however, since the SIE will automatically
// send a "NAK" packet to the host. In response to this, the
// host will normally retry to send the packet again, and no
// data loss occurs. The system will typically recover
// automatically, without the need for application firmware
// intervention.
// Nevertheless, this callback function is provided, such as
// for debugging purposes.
}
/*******************************************************************
* Function: void USBCBCheckOtherReq(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: When SETUP packets arrive from the host, some
* firmware must process the request and respond
* appropriately to fulfill the request. Some of
* the SETUP packets will be for standard
* USB "chapter 9" (as in, fulfilling chapter 9 of
* the official USB specifications) requests, while
* others may be specific to the USB device class
* that is being implemented. For example, a HID
* class device needs to be able to respond to
* "GET REPORT" type of requests. This
* is not a standard USB chapter 9 request, and
* therefore not handled by usb_device.c. Instead
* this request should be handled by class specific
* firmware, such as that contained in usb_function_hid.c.
*
* Note: None
*******************************************************************/
void USBCBCheckOtherReq(void)
{
USBCheckHIDRequest();
}//end
/*******************************************************************
* Function: void USBCBStdSetDscHandler(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: The USBCBStdSetDscHandler() callback function is
* called when a SETUP, bRequest: SET_DESCRIPTOR request
* arrives. Typically SET_DESCRIPTOR requests are
* not used in most applications, and it is
* optional to support this type of request.
*
* Note: None
*******************************************************************/
void USBCBStdSetDscHandler(void)
{
// Must claim session ownership if supporting this request
}//end
/*******************************************************************
* Function: void USBCBInitEP(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is called when the device becomes
* initialized, which occurs after the host sends a
* SET_CONFIGURATION (wValue not = 0) request. This
* callback function should initialize the endpoints
* for the device's usage according to the current
* configuration.
*
* Note: None
*******************************************************************/
void USBCBInitEP(void)
{
//enable the HID endpoint
USBEnableEndpoint(HID_EP,USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
//Re-arm the OUT endpoint for the next packet
USBOutHandle = HIDRxPacket(HID_EP,(BYTE*)&ReceivedDataBuffer,64);
}
/********************************************************************
* Function: void USBCBSendResume(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: The USB specifications allow some types of USB
* peripheral devices to wake up a host PC (such
* as if it is in a low power suspend to RAM state).
* This can be a very useful feature in some
* USB applications, such as an Infrared remote
* control receiver. If a user presses the "power"
* button on a remote control, it is nice that the
* IR receiver can detect this signalling, and then
* send a USB "command" to the PC to wake up.
*
* The USBCBSendResume() "callback" function is used
* to send this special USB signalling which wakes
* up the PC. This function may be called by
* application firmware to wake up the PC. This
* function should only be called when:
*
* 1. The USB driver used on the host PC supports
* the remote wakeup capability.
* 2. The USB configuration descriptor indicates
* the device is remote wakeup capable in the
* bmAttributes field.
* 3. The USB host PC is currently sleeping,
* and has previously sent your device a SET
* FEATURE setup packet which "armed" the
* remote wakeup capability.
*
* This callback should send a RESUME signal that
* has the period of 1-15ms.
*
* Note: Interrupt vs. Polling
* -Primary clock
* -Secondary clock ***** MAKE NOTES ABOUT THIS *******
* > Can switch to primary first by calling USBCBWakeFromSuspend()
* The modifiable section in this routine should be changed
* to meet the application needs. Current implementation
* temporary blocks other functions from executing for a
* period of 1-13 ms depending on the core frequency.
*
* According to USB 2.0 specification section 7.1.7.7,
* "The remote wakeup device must hold the resume signaling
* for at lest 1 ms but for no more than 15 ms."
* The idea here is to use a delay counter loop, using a
* common value that would work over a wide range of core
* frequencies.
* That value selected is 1800. See table below:
* ==========================================================
* Core Freq(MHz) MIP RESUME Signal Period (ms)
* ==========================================================
* 48 12 1.05
* 4 1 12.6
* ==========================================================
* * These timing could be incorrect when using code
* optimization or extended instruction mode,
* or when having other interrupts enabled.
* Make sure to verify using the MPLAB SIM's Stopwatch
* and verify the actual signal on an oscilloscope.
*******************************************************************/
void USBCBSendResume(void)
{
static WORD delay_count;
USBResumeControl = 1; // Start RESUME signaling
delay_count = 1800U; // Set RESUME line for 1-13 ms
do
{
delay_count--;
}while(delay_count);
USBResumeControl = 0;
}
/*******************************************************************
* Function: BOOL USER_USB_CALLBACK_EVENT_HANDLER(
* USB_EVENT event, void *pdata, WORD size)
*
* PreCondition: None
*
* Input: USB_EVENT event - the type of event
* void *pdata - pointer to the event data
* WORD size - size of the event data
*
* Output: None
*
* Side Effects: None
*
* Overview: This function is called from the USB stack to
* notify a user application that a USB event
* occured. This callback is in interrupt context
* when the USB_INTERRUPT option is selected.
*
* Note: None
*******************************************************************/
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event)
{
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER:
Nop();
break;
default:
break;
}
return TRUE;
}PK *8CO3 O3 ' USB Performance Meter\usb_descriptors.c/********************************************************************
FileName: usb_descriptors.c
Dependencies: See INCLUDES section
Processor: PIC18 or PIC24 USB Microcontrollers
Hardware: The code is natively intended to be used on the following
hardware platforms: PICDEM FS USB Demo Board,
PIC18F87J50 FS USB Plug-In Module, or
Explorer 16 + PIC24 USB PIM. The firmware may be
modified for use on other USB platforms by editing the
HardwareProfile.h file.
Complier: Microchip C18 (for PIC18) or C30 (for PIC24)
Company: Microchip Technology, Inc.
Software License Agreement:
The software supplied herewith by Microchip Technology Incorporated
(the Company) for its PIC Microcontroller is intended and
supplied to you, the Companys customer, for use solely and
exclusively on Microchip PIC Microcontroller products. The
software is owned by the Company and/or its supplier, and is
protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the
user to criminal sanctions under applicable laws, as well as to
civil liability for the breach of the terms and conditions of this
license.
THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*********************************************************************
-usb_descriptors.c-
-------------------------------------------------------------------
Filling in the descriptor values in the usb_descriptors.c file:
-------------------------------------------------------------------
[Device Descriptors]
The device descriptor is defined as a USB_DEVICE_DESCRIPTOR type.
This type is defined in usb_ch9.h Each entry into this structure
needs to be the correct length for the data type of the entry.
[Configuration Descriptors]
The configuration descriptor was changed in v2.x from a structure
to a BYTE array. Given that the configuration is now a byte array
each byte of multi-byte fields must be listed individually. This
means that for fields like the total size of the configuration where
the field is a 16-bit value "64,0," is the correct entry for a
configuration that is only 64 bytes long and not "64," which is one
too few bytes.
The configuration attribute must always have the _DEFAULT
definition at the minimum. Additional options can be ORed
to the _DEFAULT attribute. Available options are _SELF and _RWU.
These definitions are defined in the usb_device.h file. The
_SELF tells the USB host that this device is self-powered. The
_RWU tells the USB host that this device supports Remote Wakeup.
[Endpoint Descriptors]
Like the configuration descriptor, the endpoint descriptors were
changed in v2.x of the stack from a structure to a BYTE array. As
endpoint descriptors also has a field that are multi-byte entities,
please be sure to specify both bytes of the field. For example, for
the endpoint size an endpoint that is 64 bytes needs to have the size
defined as "64,0," instead of "64,"
Take the following example:
// Endpoint Descriptor //
0x07, //the size of this descriptor //
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
_EP02_IN, //EndpointAddress
_INT, //Attributes
0x08,0x00, //size (note: 2 bytes)
0x02, //Interval
The first two parameters are self-explanatory. They specify the
length of this endpoint descriptor (7) and the descriptor type.
The next parameter identifies the endpoint, the definitions are
defined in usb_device.h and has the following naming
convention:
_EP<##>_
where ## is the endpoint number and dir is the direction of
transfer. The dir has the value of either 'OUT' or 'IN'.
The next parameter identifies the type of the endpoint. Available
options are _BULK, _INT, _ISO, and _CTRL. The _CTRL is not
typically used because the default control transfer endpoint is
not defined in the USB descriptors. When _ISO option is used,
addition options can be ORed to _ISO. Example:
_ISO|_AD|_FE
This describes the endpoint as an isochronous pipe with adaptive
and feedback attributes. See usb_device.h and the USB
specification for details. The next parameter defines the size of
the endpoint. The last parameter in the polling interval.
-------------------------------------------------------------------
Adding a USB String
-------------------------------------------------------------------
A string descriptor array should have the following format:
rom struct{byte bLength;byte bDscType;word string[size];}sdxxx={
sizeof(sdxxx),DSC_STR,};
The above structure provides a means for the C compiler to
calculate the length of string descriptor sdxxx, where xxx is the
index number. The first two bytes of the descriptor are descriptor
length and type. The rest are string texts which must be
in the unicode format. The unicode format is achieved by declaring
each character as a word type. The whole text string is declared
as a word array with the number of characters equals to .
has to be manually counted and entered into the array
declaration. Let's study this through an example:
if the string is "USB" , then the string descriptor should be:
(Using index 02)
rom struct{byte bLength;byte bDscType;word string[3];}sd002={
sizeof(sd002),DSC_STR,'U','S','B'};
A USB project may have multiple strings and the firmware supports
the management of multiple strings through a look-up table.
The look-up table is defined as:
rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
The above declaration has 3 strings, sd000, sd001, and sd002.
Strings can be removed or added. sd000 is a specialized string
descriptor. It defines the language code, usually this is
US English (0x0409). The index of the string must match the index
position of the USB_SD_Ptr array, &sd000 must be in position
USB_SD_Ptr[0], &sd001 must be in position USB_SD_Ptr[1] and so on.
The look-up table USB_SD_Ptr is used by the get string handler
function.
-------------------------------------------------------------------
The look-up table scheme also applies to the configuration
descriptor. A USB device may have multiple configuration
descriptors, i.e. CFG01, CFG02, etc. To add a configuration
descriptor, user must implement a structure similar to CFG01.
The next step is to add the configuration descriptor name, i.e.
cfg01, cfg02,.., to the look-up table USB_CD_Ptr. USB_CD_Ptr[0]
is a dummy place holder since configuration 0 is the un-configured
state according to the definition in the USB specification.
********************************************************************/
/*********************************************************************
* Descriptor specific type definitions are defined in:
* usb_device.h
*
* Configuration options are defined in:
* usb_config.h
********************************************************************/
#ifndef __USB_DESCRIPTORS_C
#define __USB_DESCRIPTORS_C
/** INCLUDES *******************************************************/
#include "usb.h"
#include "usb_function_hid.h"
/* Device Descriptor */
ROM USB_DEVICE_DESCRIPTOR device_dsc=
{
0x12, // Size of this descriptor in bytes
USB_DESCRIPTOR_DEVICE, // DEVICE descriptor type
0x0200, // USB Spec Release Number in BCD format
0x00, // Class Code
0x00, // Subclass code
0x00, // Protocol code
USB_EP0_BUFF_SIZE, // Max packet size for EP0, see usb_config.h
0x04D8, // Vendor ID
0x0044, // Product ID
0x0100, // Device release number in BCD format
0x01, // Manufacturer string index
0x02, // Product string index
0x03, // Device serial number string index
0x01 // Number of possible configurations
};
/* Configuration 1 Descriptor */
ROM BYTE configDescriptor1[]={
/* Configuration Descriptor */
0x09,//sizeof(USB_CFG_DSC), // Size of this descriptor in bytes
USB_DESCRIPTOR_CONFIGURATION, // CONFIGURATION descriptor type
0x29,0x00, // Total length of data for this cfg
1, // Number of interfaces in this cfg
1, // Index value of this configuration
0, // Configuration string index
_DEFAULT | _SELF, // Attributes, see usb_device.h
50, // Max power consumption (2X mA)
/* Interface Descriptor */
0x09,//sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
USB_DESCRIPTOR_INTERFACE, // INTERFACE descriptor type
0, // Interface Number
0, // Alternate Setting Number
2, // Number of endpoints in this intf
HID_INTF, // Class code
0, // Subclass code
0, // Protocol code
0, // Interface string index
/* HID Class-Specific Descriptor */
0x09,//sizeof(USB_HID_DSC)+3, // Size of this descriptor in bytes
DSC_HID, // HID descriptor type
0x11,0x01, // HID Spec Release Number in BCD format (1.11)
0x00, // Country Code (0x00 for Not supported)
HID_NUM_OF_DSC, // Number of class descriptors, see usbcfg.h
DSC_RPT, // Report descriptor type
HID_RPT01_SIZE,0x00,//sizeof(hid_rpt01), // Size of the report descriptor
/* Endpoint Descriptor */
0x07,/*sizeof(USB_EP_DSC)*/
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
HID_EP | _EP_IN, //EndpointAddress
_INTERRUPT, //Attributes
0x40,0x00, //size
0x01, //Interval
/* Endpoint Descriptor */
0x07,/*sizeof(USB_EP_DSC)*/
USB_DESCRIPTOR_ENDPOINT, //Endpoint Descriptor
HID_EP | _EP_OUT, //EndpointAddress
_INTERRUPT, //Attributes
0x40,0x00, //size
0x01 //Interval
};
//Language code string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[1];}sd000={
sizeof(sd000),USB_DESCRIPTOR_STRING,{0x0409
}};
//Manufacturer string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[10];}sd001={
sizeof(sd001),USB_DESCRIPTOR_STRING,
{'S','i','m','o','n',' ','I','n','n','s'
}};
//Product string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[21];}sd002={
sizeof(sd002),USB_DESCRIPTOR_STRING,
{'U','S','B',' ','P','e','r','f','o','r','m','a','n','c','e',' ','M','e','t','e','r'
}};
//Device serial number string descriptor
ROM struct{BYTE bLength;BYTE bDscType;WORD string[7];}sd003={
sizeof(sd002),USB_DESCRIPTOR_STRING,
{'W','F','F','_','1','.','0'
}};
//Class specific descriptor - HID
ROM struct{BYTE report[HID_RPT01_SIZE];}hid_rpt01={
{
0x06, 0x00, 0xFF, // Usage Page = 0xFF00 (Vendor Defined Page 1)
0x09, 0x01, // Usage (Vendor Usage 1)
0xA1, 0x01, // Collection (Application)
0x19, 0x01, // Usage Minimum
0x29, 0x40, // Usage Maximum //64 input usages total (0x01 to 0x40)
0x15, 0x00, // Logical Minimum (data bytes in the report may have minimum value = 0x00)
0x26, 0xFF, 0x00, // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
0x75, 0x08, // Report Size: 8-bit field size
0x95, 0x40, // Report Count: Make sixty-four 8-bit fields (the next time the parser hits an "Input", "Output", or "Feature" item)
0x81, 0x00, // Input (Data, Array, Abs): Instantiates input packet fields based on the above report size, count, logical min/max, and usage.
0x19, 0x01, // Usage Minimum
0x29, 0x40, // Usage Maximum //64 output usages total (0x01 to 0x40)
0x91, 0x00, // Output (Data, Array, Abs): Instantiates output packet fields. Uses same report size and count as "Input" fields, since nothing new/different was specified to the parser since the "Input" item.
0xC0} // End Collection
};
//Array of configuration descriptors
ROM BYTE *ROM USB_CD_Ptr[]=
{
(ROM BYTE *ROM)&configDescriptor1
};
//Array of string descriptors
ROM BYTE *ROM USB_SD_Ptr[]=
{
(ROM BYTE *ROM)&sd000,
(ROM BYTE *ROM)&sd001,
(ROM BYTE *ROM)&sd002,
(ROM BYTE *ROM)&sd003
};
/** EOF usb_descriptors.c ***************************************************/
#endif
PK sww " USB Performance Meter\genericHID.h// genericHID.h
#ifndef GENERICHID_H
#define GENERICHID_H
unsigned char ReceivedDataBuffer[64] @ 0x480;
unsigned char ToSendDataBuffer[64] @ 0x4C0;
USB_HANDLE USBOutHandle = 0;
USB_HANDLE USBInHandle = 0;
BOOL blinkStatusValid = TRUE;
#endifPK H H ' USB Performance Meter\HardwareProfile.h/************************************************************************
HardwareProfile.h
USB Performance Monitor - PIC18F2550 firmware
Copyright (C) 2010 Simon Inns
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Email: simon.inns@gmail.com
************************************************************************/
#ifndef HARDWARE_PROFILE_H
#define HARDWARE_PROFILE_H
// Defines required for the USB stack
#define self_power 1
#define USE_USB_BUS_SENSE_IO
#define USB_BUS_SENSE 1
#define DEMO_BOARD PICDEM_FS_USB
#define PICDEM_FS_USB
#define CLOCK_FREQ 48000000
// Fosc frequency (48 Mhz)
#define _XTAL_FREQ 48000000
// Common useful definitions
#define ON 1
#define OFF 0
#define WRITE 0
#define READ 1
// PIC to hardware pin mapping
#define GRN RA0
#define BLU RA1
#define RED RA2
#define METER0 RC1
#define METER1 RC2
#define BUTTON0 RB0
// I/O pin definitions
#define INPUT_PIN 1
#define OUTPUT_PIN 0
#endif
PK 5S/ / USB Performance Meter\Compiler.h/*********************************************************************
*
* Compiler and hardware specific definitions
*
*********************************************************************
* FileName: Compiler.h
* Dependencies: None
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.00 or higher
* Microchip C30 v3.01 or higher
* Microchip C18 v3.13 or higher
* HI-TECH PICC-18 STD 9.50PL3 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2008 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c and
* ENC28J60.h ported to a non-Microchip device used in
* conjunction with a Microchip ethernet controller for the
* sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Howard Schlunder 10/03/2006 Original, copied from old Compiler.h
* Howard Schlunder 11/07/2007 Reorganized and simplified
********************************************************************/
#ifndef __COMPILER_H
#define __COMPILER_H
// Include proper device header file
#if defined(__18CXX) || defined(HI_TECH_C)
// All PIC18 processors
#if defined(HI_TECH_C) && defined(__PICC18__) // HI TECH PICC-18 compiler
#define __18CXX
#include
#define PICC18PORT
#else // Microchip C18 compiler
#include
#endif
#elif defined(__PIC24F__) // Microchip C30 compiler
// PIC24F processor
#include
#elif defined(__PIC24H__) // Microchip C30 compiler
// PIC24H processor
#include
#elif defined(__dsPIC33F__) // Microchip C30 compiler
// dsPIC33F processor
#include
#elif defined(__dsPIC30F__) // Microchip C30 compiler
// dsPIC30F processor
#include
#elif defined(__PIC32MX__) // Microchip C32 compiler
#if !defined(__C32__)
#define __C32__
#endif
#include
#include
#else
#error Unknown processor or compiler. See Compiler.h
#endif
#include
#include
#include
// Base RAM and ROM pointer types for given architecture
#if defined(__PIC32MX__)
#define PTR_BASE DWORD
#define ROM_PTR_BASE DWORD
#elif defined(__C30__)
#define PTR_BASE WORD
#define ROM_PTR_BASE WORD
#elif defined(__18CXX)
#define PTR_BASE WORD
#define ROM_PTR_BASE unsigned short long
#if defined(HI_TECH_C)
#undef ROM_PTR_BASE
#define ROM_PTR_BASE DWORD
#endif
#endif
// Definitions that apply to all except Microchip MPLAB C Compiler for PIC18 MCUs (formerly C18)
#if !defined(__18CXX) || (defined(HI_TECH_C) && defined(__PICC18__))
#define memcmppgm2ram(a,b,c) memcmp(a,b,c)
#define strcmppgm2ram(a,b) strcmp(a,b)
#define memcpypgm2ram(a,b,c) memcpy(a,b,c)
#define strcpypgm2ram(a,b) strcpy(a,b)
#define strncpypgm2ram(a,b,c) strncpy(a,b,c)
#define strstrrampgm(a,b) strstr(a,b)
#define strlenpgm(a) strlen(a)
#define strchrpgm(a,b) strchr(a,b)
#define strcatpgm2ram(a,b) strcat(a,b)
#endif
// Definitions that apply to all 8-bit products
// (PIC18)
#if defined(__18CXX)
#define __attribute__(a)
#define FAR far
// Microchip C18 specific defines
#if !defined(HI_TECH_C)
#define ROM rom
#define strcpypgm2ram(a, b) strcpypgm2ram(a,(far rom char*)b)
#endif
// HI TECH PICC-18 STD specific defines
#if defined(HI_TECH_C)
#define ROM const
#define rom
#define Nop() asm("NOP");
#define ClrWdt() asm("CLRWDT");
#define Reset() asm("RESET");
#endif
// Definitions that apply to all 16-bit and 32-bit products
// (PIC24F, PIC24H, dsPIC30F, dsPIC33F, and PIC32)
#else
#define ROM const
// 16-bit specific defines (PIC24F, PIC24H, dsPIC30F, dsPIC33F)
#if defined(__C30__)
#define Reset() asm("reset")
#define FAR __attribute__((far))
#endif
// 32-bit specific defines (PIC32)
#if defined(__PIC32MX__)
#define persistent
#define far
#define FAR
#define Reset() SoftReset()
#define ClrWdt() (WDTCONSET = _WDTCON_WDTCLR_MASK)
// MPLAB C Compiler for PIC32 MCUs version 1.04 and below don't have a
// Nop() function. However, version 1.05 has Nop() declared as _nop().
#if !defined(Nop)
#define Nop() asm("nop")
#endif
#endif
#endif
#endif
PK {A<