CCS PCH C Compiler, Version 4.090, 47914 28-V-10 17:50Filename: D:\MLAB\Designs\galileo_experiment\SW\main.lstROM used: 9692 bytes (30%)Largest free fragment is 23072RAM used: 1650 (81%) at main() level1869 (91%) worst caseStack: 17 worst case (8 in main + 9 for interrupts)*0000: GOTO 1F62*0008: MOVWF 05000A: MOVFF FD8,06000E: MOVFF FE0,070012: MOVLB 00014: MOVFF FE9,0D0018: MOVFF FEA,08001C: MOVFF FE1,090020: MOVFF FE2,0A0024: MOVFF FD9,0B0028: MOVFF FDA,0C002C: MOVFF FF3,140030: MOVFF FF4,150034: MOVFF FFA,160038: MOVFF 00,0F003C: MOVFF 01,100040: MOVFF 02,110044: MOVFF 03,120048: MOVFF 04,13004C: BTFSS FF2.5004E: GOTO 00580052: BTFSC FF2.20054: GOTO 025E0058: BTFSS FA0.5005A: GOTO 0064005E: BTFSC FA1.50060: GOTO 12740064: MOVFF 0F,000068: MOVFF 10,01006C: MOVFF 11,020070: MOVFF 12,030074: MOVFF 13,040078: MOVFF 0D,FE9007C: MOVFF 08,FEA0080: BSF 08.70082: MOVFF 09,FE10086: MOVFF 0A,FE2008A: MOVFF 0B,FD9008E: MOVFF 0C,FDA0092: MOVFF 14,FF30096: MOVFF 15,FF4009A: MOVFF 16,FFA009E: MOVF 05,W00A0: MOVFF 07,FE000A4: MOVFF 06,FD800A8: RETFIE 0.................... #include "main.h".................... #include <18F4550.h>.................... //////// Standard Header file for the PIC18F4550 device ////////////////.................... #device PIC18F4550.................... #list........................................ #device adc=10........................................ #FUSES NOWDT //No Watch Dog Timer.................... #FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale.................... #FUSES HSPLL //Crystal/Resonator with PLL enabled.................... #FUSES NOPROTECT //Code not protected from reading.................... #FUSES NOBROWNOUT //No brownout reset.................... #FUSES BORV20 //Brownout reset at 2.0V.................... #FUSES NOPUT //No Power Up Timer.................... #FUSES NOCPD //No EE protection.................... #FUSES STVREN //Stack full/underflow will cause reset.................... #FUSES NODEBUG //No Debug mode for ICD.................... #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O.................... #FUSES NOWRT //Program memory not write protected.................... #FUSES NOWRTD //Data EEPROM not write protected.................... #FUSES IESO //Internal External Switch Over mode enabled.................... #FUSES FCMEN //Fail-safe clock monitor enabled.................... #FUSES PBADEN //PORTB pins are configured as analog input channels on RESET.................... #FUSES NOWRTC //configuration not registers write protected.................... #FUSES NOWRTB //Boot block not write protected.................... #FUSES NOEBTR //Memory not protected from table reads.................... #FUSES NOEBTRB //Boot block not protected from table reads.................... #FUSES NOCPB //No Boot Block code protection.................... #FUSES MCLR //Master Clear pin enabled.................... #FUSES LPT1OSC //Timer1 configured for low-power operation.................... #FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode).................... #FUSES PLL5 //Divide By 5(20MHz oscillator input).................... #FUSES CPUDIV4 //System Clock by 4.................... #FUSES USBDIV //USB clock source comes from PLL divide by 2.................... #FUSES VREGEN //USB voltage regulator enabled.................... #FUSES ICPRT //ICPRT enabled........................................ #use delay(clock=24000000)................................................................................ #define REV "$Rev$"........................................ unsigned int32 time; // pocitadlo preteceni casovace........................................ #define BUFLEN 100........................................ #int_RTCC.................... void RTCC_isr(void) // preruseni od pretekleho casovace.................... {.................... time++;*025E: MOVLW 010260: ADDWF 19,F0262: BTFSC FD8.00264: INCF 1A,F0266: BTFSC FD8.20268: INCF 1B,F026A: BTFSC FD8.2026C: INCF 1C,F.................... }........................................ // Includes all USB code and interrupts, as well as the CDC API.................... #include <usb_cdc.h>.................... /////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// usb_cdc.h ////.................... //// ////.................... //// Library for adding a virtual COM port on your PC over USB using ////.................... //// the standard Communication Device Class (CDC) specification. ////.................... //// Including this file in your code will add all USB code, ////.................... //// interrupts, descriptors and handlers required. No other ////.................... //// modifications need to be made. ////.................... //// ////.................... //// This library creates a virtual RS232 link between the PC and ////.................... //// the PIC, therefore the library provided will be familiar to ////.................... //// anyone with standard UART stream I/O: ////.................... //// ////.................... //// usb_cdc_kbhit() - Returns TRUE if there is one or more ////.................... //// character received and waiting in the receive buffer. ////.................... //// ////.................... //// usb_cdc_getc() - Gets a character from the receive buffer. If ////.................... //// there is no data in the receive buffer it will wait until ////.................... //// there is data in the receive buffer. If you do not want ////.................... //// to wait in an infinit loop, use usb_cdc_kbhit() first to ////.................... //// check if there is data before calling usb_cdc_getc(). ////.................... //// ////.................... //// usb_cdc_putc(char c) - Puts a character into the transmit ////.................... //// buffer. If the transmit buffer is full it will wait until ////.................... //// the transmit buffer is not full before putting the char ////.................... //// into the transmit buffer. The transmit buffer is read by ////.................... //// the PC very quickly, and therefore the buffer should only ////.................... //// be full for a few milli-seconds. If you are concerned ////.................... //// and don't want to be stuck in a long or infinite loop, ////.................... //// use usb_cdc_putready() to see if there is space in the ////.................... //// transmit buffer before putting data into the transmit ////.................... //// buffer. ////.................... //// ////.................... //// usb_cdc_putready() - Returns TRUE if there is room left in the ////.................... //// transmit buffer for another character. ////.................... //// ////.................... //// usb_cdc_connected() - Returns TRUE if we received a ////.................... //// Set_Line_Coding. On most serial terminal programs (such ////.................... //// as Hyperterminal), they will send a Set_Line_Coding ////.................... //// message when the program starts and it opens the virtual ////.................... //// COM port. This is a simple way to determine if the PC ////.................... //// is ready to display data on a serial terminal program, ////.................... //// but is not garaunteed to work all the time or on other ////.................... //// terminal programs. ////.................... //// ////.................... //// usb_cdc_putc_fast(char c) - Similar to usb_cdc_putc(), except ////.................... //// if the transmit buffer is full it will skip the char. ////.................... //// ////.................... //// usb_cdc_line_coding - A structure used for Set_Line_Coding and ////.................... //// Get_Line_Coding. Most of the time you can ignore this. ////.................... //// ////.................... //// usb_cdc_break - If the PC has sent a break command, this will ////.................... //// hold the break time (in milli-seconds). If the PC sends ////.................... //// a value of 0xFFFF the device is supposed to hold the ////.................... //// break until it sends a value of 0 ////.................... //// ////.................... //// usb_cdc_carrier - Where Set_Control_Line_State value is stored. ////.................... //// Of most relevance is the field dte_present, which is the ////.................... //// DTR setting. ////.................... //// ////.................... //// The following functions are also provided, and are ports of the ////.................... //// I/O functions in input.c. See input.c and the CCS manual for ////.................... //// documentation: ////.................... //// get_float_usb() - Read a float number from the user ////.................... //// get_long_usb() - Read a long number from the user ////.................... //// get_int_usb() - Read an integer number from the user ////.................... //// get_string_usb(char *s, int max) - Read a string from the user. ////.................... //// gethex_usb() - Read a byte, in HEX, from the user ////.................... //// gethex1_usb() - Read a HEX character ////.................... //// ////.................... //// This driver will load all the rest of the USB code, and a set ////.................... //// of descriptors that will properly describe a CDC device for a ////.................... //// virtual COM port (usb_desc_cdc.h) ////.................... //// ////.................... //// An .INF file is provided (cdc_NTXP.inf) that will load the ////.................... //// standard CDC drivers for a virtual COM port in Windows ////.................... //// NT/2000/XP and above. ////.................... //// ////.................... /////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// VERSION HISTORY ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// Nov 26th, 2007: ////.................... //// usb_cdc_put_buffer_free() should be more stable. ////.................... //// Fixed a hang-up that would happen if you sent ////.................... //// exactly 64 bytes. ////.................... //// ////.................... //// Nov 6th, 2007: ////.................... //// Compatabible with latest pic18_usb.h, which ////.................... //// reduces RAM footprint of USB driver. ////.................... //// This driver now fits on 18F4450/2450 PICs. ////.................... //// ////.................... //// October 27th, 2005: Changed the way incoming packets are ////.................... //// handled in that CDC driver will not flush ////.................... //// endpoint until user has handled all data. This ////.................... //// will prevent overflows as the USB will NAK ////.................... //// incoming packets until it is ready to receive ////.................... //// more. ////.................... //// When using 18F4550 family, the RX buffer is ////.................... //// mapped directly to the endpoint buffer - this ////.................... //// saves a chunk of RAM. ////.................... //// When using the 18F4550 family, you can increase ////.................... //// the TX and RX size over 64 bytes. ////.................... //// No longer send 0len packets in the TBE interrupt. ////.................... //// Hopefully fixed bugs that caused random crashes ////.................... //// if you tried sending more than 64 bytes. ////.................... //// ////.................... //// July 6th, 2005: Global interrupts disabled when writing to TX ////.................... //// buffer. ////.................... //// ////.................... //// July 1st, 2005: Initial Release. ////.................... //// ////.................... /////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2005 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... /////////////////////////////////////////////////////////////////////////............................................................ //api for the user:.................... #define usb_cdc_kbhit() (usb_cdc_get_buffer_status.got).................... #define usb_cdc_putready() (usb_cdc_put_buffer_nextin<USB_CDC_DATA_IN_SIZE).................... #define usb_cdc_connected() (usb_cdc_got_set_line_coding)026E: BCF FF2.20270: GOTO 0064.................... void usb_cdc_putc_fast(char c);.................... char usb_cdc_getc(void);.................... void usb_cdc_putc(char c);........................................ //input.c ported to use CDC:.................... float get_float_usb();.................... signed long get_long_usb();.................... signed int get_int_usb();.................... void get_string_usb(char* s, unsigned int max);.................... BYTE gethex_usb();.................... BYTE gethex1_usb();........................................ //functions automatically called by USB handler code.................... void usb_isr_tkn_cdc(void);.................... void usb_cdc_init(void);.................... void usb_isr_tok_out_cdc_control_dne(void);.................... void usb_isr_tok_in_cdc_data_dne(void);.................... void usb_isr_tok_out_cdc_data_dne(void);........................................ void usb_cdc_flush_out_buffer(void);........................................ /////////////////////////////////////////////////////////////////////////////.................... //.................... // Include the CCS USB Libraries. See the comments at the top of these.................... // files for more information.................... //.................... /////////////////////////////////////////////////////////////////////////////.................... #ifndef __USB_PIC_PERIF__.................... #define __USB_PIC_PERIF__ 1.................... #endif........................................ #if __USB_PIC_PERIF__.................... #if defined(__PCH__).................... #include <pic18_usb.h> //Microchip 18Fxx5x hardware layer for usb.c.................... /////////////////////////////////////////////////////////////////////////.................... //// pic18_usb.h ////.................... //// ////.................... //// Hardware layer for CCS's USB library. This hardware layer ////.................... //// supports the USB peripheral on the PIC18 family chips. Current ////.................... //// supported families are: ////.................... //// PIC18F2455/2550/4455/4550 ////.................... //// PIC18F2450/4450 ////.................... //// PIC18F2458/2553/4458/4553 ////.................... //// PIC18F13K50/14K50 ////.................... //// PIC18F65J50/66J50/66J55/67J50/85J50/86J50/86J55/87J50 ////.................... //// ////.................... //// This file is part of CCS's PIC USB driver code. See USB.H ////.................... //// for more documentation and a list of examples. ////.................... //// ////.................... //// See the section labeled EXTRA USER FUNCTIONS for functions ////.................... //// that are specific to only this HW peripheral that you may ////.................... //// find useful to use in your application. ////.................... //// ////.................... //// *********** NOTE ABOUT 18F2450/4450 LIMITATIONS ********** ////.................... //// Due to the limited USB RAM of this family, a limitation of ////.................... //// this driver is that there are only 3 endpoints (0, 1 and 2). ////.................... //// The HW actually supports more endpoints, but to simplify ////.................... //// driver development this driver will only support the first 3 ////.................... //// so there is an easier memory block to work with. ////.................... //// ////.................... //// USB_MAX_EP0_PACKET_LENGTH will also be set to 8 regardless ////.................... //// of USB speed, to save RAM. ////.................... //// ////.................... //// ************** NOTE ABOUT HW REQUIREMENTS **************** ////.................... //// If you are not using internal pullups, you will need to put ////.................... //// an internal pullup resistor on D+ or D+ depending on if you ////.................... //// want to use slow speed or full speed. This code configures ////.................... //// the device to use internal pullups, see usb_init() if you ////.................... //// want to change that. ////.................... //// ////.................... //// You need approximately 470nF cap on Vusb, even if you are ////.................... //// using the internal 3.3V USB regulator. ////.................... //// ////.................... //// To run at full speed, you must use the oscillator ////.................... //// configuration (PLLx) to set the PLL divide to 4MHz. You can ////.................... //// configure the MCU clock to any speed (up to 48MHz) but the ////.................... //// PLL must run at 4Mhz to provide the USB peripheral with a ////.................... //// 96MHz clock. See the datasheet for details. ////.................... //// ////.................... //// To run at slow speed you must configure your MCU to run at ////.................... //// 24Mhz. See the datasheet for details. ////.................... //// ////.................... //// **************** NOTE ABOUT INTERRUPTS ****************** ////.................... //// This driver uses INT_USB. It requires INT_USB to interrupt the ////.................... //// PIC when an event has happened on the USB Bus. Therfore ////.................... //// this code enables interrupts. A user modification can be made ////.................... //// to poll the USB interrupt flag instead of relying on an ////.................... //// interrupt. ////.................... //// ////.................... /////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// 18F14K50 family added. ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// Nov 3rd, 2008: ////.................... //// * 4553 family added. ////.................... //// ////.................... //// Dec 18, 2007: ////.................... //// * usb_kbhit() moved to device driver. ////.................... //// * USB Token handler changed to workaround a flaw in the USB ////.................... //// peripheral to prevent the USB peripheral from ////.................... //// inadvertantly STALLing the endpoint. Happened most often ////.................... //// in bulk demo applications, especially CDC. ////.................... //// ////.................... //// 11-6-07: Fixed a bug where usb_data_buffer[] was reserving ////.................... //// too much RAM. ////.................... //// USB_MAX_EP0_PACKET_LENGTH value can be overwritten. ////.................... //// For the 18F4450/2450 family this should be 8 ////.................... //// because of limited RAM. Reducing this value ////.................... //// frees RAM for application. ////.................... //// Based upon which endpoints have been configured for ////.................... //// use, will free up unused USB data RAM for ////.................... //// application dynamically. This should free up ////.................... //// at least 128 bytes of RAM. ////.................... //// CDC now fits on a 18F4450/2450 ////.................... //// ////.................... //// 09-19-07: Fixed problems with 18F4450 family. ////.................... //// ////.................... //// 07-17-07: Added 18F4450,2450 support ////.................... //// ////.................... //// 07-13-07: Added 87J50 family support ////.................... //// ////.................... //// 11-01-05: usb_detach(), usb_attach() and usb_init_cs() ////.................... //// changed for the better. ////.................... //// ////.................... //// 10-28-05: Added usb_rx_packet_size() ////.................... //// ////.................... //// 07-13-05: usb_put_packet() changed for 16bit packet sizes ////.................... //// usb_flush_in() changed for 16bit packet sizes ////.................... //// usb_get_packet() changed for 16bit packet sizes ////.................... //// usb_flush_out() changed for 16bit packet sizes ////.................... //// usb_set_configured() changed for 16bit packet sizes ////.................... //// ////.................... //// 06-30-05: usb_tbe() added ////.................... //// The way endpoint 0 DTS is set has been changed. ////.................... //// ////.................... //// 06-20-05: Initial Release ////.................... //// ////.................... //// 05-13-05: Beta Release (Full Speed works) ////.................... //// ////.................... //// 03-21-05: Initial Alpha Release ////.................... //// ////.................... /////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2005 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... /////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __PIC18_USB_H__.................... #DEFINE __PIC18_USB_H__........................................ #DEFINE __USB_HARDWARE__........................................ //let the USB Stack know that we are using a PIC with internal USB peripheral.................... #DEFINE __PIC__ 1........................................ #ifndef USB_USE_FULL_SPEED.................... #define USB_USE_FULL_SPEED TRUE.................... #endif........................................ #ifndef USB_MAX_EP0_PACKET_LENGTH.................... #if ((getenv("DEVICE")=="PIC18F2450") || (getenv("DEVICE")=="PIC18F4450")).................... //due to limited ram, force max packet length to 8 for this chip.................... #define USB_MAX_EP0_PACKET_LENGTH 8.................... #else.................... #if USB_USE_FULL_SPEED==FALSE.................... //slow speed requires 8byte max packet size for endpoint 0.................... #DEFINE USB_MAX_EP0_PACKET_LENGTH 8.................... #else.................... //for full speed you can still use 8bytes, but 64 will be faster.................... #DEFINE USB_MAX_EP0_PACKET_LENGTH 64.................... #endif.................... #endif.................... #endif........................................ #if (!USB_USE_FULL_SPEED && (USB_MAX_EP0_PACKET_LENGTH!=8)).................... #error Slow speed devices must use a max packet size of 8 for endpoint 0!.................... #endif........................................ #if USB_MAX_EP0_PACKET_LENGTH < 8.................... #error Max Endpoint 0 length can't be less than 8!.................... #endif........................................ #if USB_MAX_EP0_PACKET_LENGTH > 64.................... #error Max Endpoint 0 length can't be greater than 64!.................... #endif........................................ #include <usb_hw_layer.h>.................... ///////////////////////////////////////////////////////////////////////////.................... //// usb_hw_layer.h ////.................... //// ////.................... //// HW Layer for the USB Stack. ////.................... //// ////.................... //// HW drivers for the USB stack must support this API. ////.................... //// ////.................... //// The HW driver must also have a token handler that will call ////.................... //// usb_token_reset() when the device is init/reset, will call ////.................... //// usb_isr_tok_setup_dne() when it receives a setup packet, ////.................... //// will call usb_isr_tok_out_dne() when a new packet is received, ////.................... //// and will call usb_isr_tok_in_dne() when a new packet is sent. ////.................... //// See usb.h for documentation of these functions. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2009 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... ///////////////////////////////////////////////////////////////////////////........................................ #ifndef __USB_HW_LAYER_H__.................... #define __USB_HW_LAYER_H__........................................ enum USB_DTS_BIT {USB_DTS_DATA1=1, USB_DTS_TOGGLE=2, USB_DTS_DATA0=0,.................... USB_DTS_STALL=3, USB_DTS_USERX=4};............................................................ // USER LEVEL FUNCTIONS:........................................ /*****************************************************************************.................... /* usb_init().................... /*.................... /* Summary: See API section of USB.H for documentation..................... /*.................... /*****************************************************************************/.................... void usb_init(void);........................................ /*****************************************************************************.................... /* usb_init_cs().................... /*.................... /* Summary: See API section of USB.H for documentation..................... /*.................... /*****************************************************************************/.................... void usb_init_cs(void);........................................ /*****************************************************************************.................... /* usb_task().................... /*.................... /* Summary: See API section of USB.H for documentation..................... /*.................... /*****************************************************************************/.................... void usb_task(void);........................................ /******************************************************************************.................... /* usb_attach().................... /*.................... /* Summary: Attach the D+/D- lines to the USB bus. Enable the USB peripheral..................... /*.................... /* You should wait until UCON_SE0 is clear before enabling reset/idle interrupt..................... /*.................... /* Shouldn't be called by user, let usb_task() handle this..................... /*.................... /* NOTE: If you are writing a new HW driver, this routine should call.................... /* usb_token_reset() to reset the CH9 stack..................... /*.................... /*****************************************************************************/.................... void usb_attach(void);........................................ /******************************************************************************.................... /* usb_detach().................... /*.................... /* Summary: Remove the D+/D- lines from the USB bus. Basically, disable USB..................... /*.................... /* Shouldn't be called by user, let usb_task() handle this..................... /*.................... /* If the user does call this function while USB is connected, the USB.................... /* peripheral will be disabled only momentarily because usb_task() will.................... /* reconnect. But this is a great way to cause the PC/HOST to cause a USB.................... /* reset of the device..................... /*.................... /* NOTE: If you are writing a new HW driver, this routine should call.................... /* usb_token_reset() to reset the CH9 stack..................... /*.................... /*****************************************************************************/.................... void usb_detach(void);........................................ /******************************************************************************.................... /* usb_put_packet().................... /*.................... /* Input: endpoint - endpoint to send packet to (0..15)..................... /* ptr - points to data to send..................... /* len - amount of data to send..................... /* toggle - whether to send data with a DATA0 pid, a DATA1 pid,.................... /* or toggle from the last DATAx pid..................... /*.................... /* Output: TRUE if data was sent correctly, FALSE if it was not. The only.................... /* reason it will return FALSE is if because the TX buffer is still full.................... /* from the last time it tried to send a packet..................... /*.................... /* Summary: Sends one packet out the EP to the host. Notice that there is a.................... /* difference between a packet and a message. If you wanted to send a.................... /* 512 byte message on an endpoint that only supported 64 byte packets,.................... /* this would be accomplished this by sending 8 64-byte packets,.................... /* followed by a 0 length packet. If the last (or only packet) being.................... /* sent is less than the max packet size defined in your descriptor.................... /* then you do not need to send a 0 length packet to identify.................... /* an end of message..................... /*.................... /* usb_puts() (provided in usb.c) will send a multi-packet message.................... /* correctly and know if a 0 lenght packet needs to be sent..................... /*.................... /* The result will be invalid if the specified input has not been configured for.................... /* use by the API with USB_EPx_TX_SIZE.................... /*.................... /*****************************************************************************/.................... int1 usb_put_packet(int8 endpoint, int8 * ptr, unsigned int16 len, USB_DTS_BIT tgl);........................................ /*******************************************************************************.................... /* usb_get_packet(endpoint, *ptr, max).................... /*.................... /* Input: endpoint - endpoint to get data from.................... /* ptr - where to save data to local PIC RAM.................... /* max - max amount of data to receive from buffer.................... /*.................... /* Output: the amount of data taken from the buffer and saved to ptr..................... /*.................... /* NOTE - IF THERE IS NO PACKET TO GET YOU WILL GET INVALID RESULTS!.................... /* VERIFY WITH USB_KBHIT() BEFORE YOU CALL USB_GET_PACKET()!.................... /*.................... /* Summary: Gets a packet of data from the USB endpoint buffer..................... /* Until this is called, if there is data in the receive buffer the.................... /* PC/HOST will be prevented from sending more data. Upon completion.................... /* of this call, the endpoint will be freed and the PC/HOST will be.................... /* allowed to send a new packet. Only receives one packet, if you need.................... /* to receive multiple packets or handle 0-length terminator packets,.................... /* then use usb_gets()..................... /*.................... /* The result will be invalid if the specified input has not been configured for.................... /* use by the API with USB_EPx_RX_SIZE.................... /*.................... /********************************************************************************/.................... unsigned int16 usb_get_packet(int8 endpoint, int8 * ptr, unsigned int16 max);........................................ /****************************************************************************.................... /* usb_kbhit(endpoint).................... /*.................... /* Input: endpoint - endpoint to check (0..15).................... /*.................... /* Output: TRUE if there is new data in RX buffer, FALSE if there is not..................... /*.................... /* Summary: Similar to kbhit(), sees if there is new data in the RX USB buffers..................... /* See API section of USB.H for more information..................... /*.................... /* The result will be invalid if the specified input has not been configured for.................... /* use by the API with USB_EPx_RX_SIZE.................... /*.................... /*****************************************************************************/.................... int1 usb_kbhit(int8 en);........................................ /******************************************************************************.................... /* usb_tbe(endpoint).................... /*.................... /* Input: endpoint - endpoint to check.................... /* ptr - where to save data to local PIC RAM.................... /* max - max amount of data to receive from buffer.................... /*.................... /* Output: returns TRUE if this endpoint's IN buffer (PIC-PC) is empty and ready.................... /* returns FALSE if this endpoint's IN buffer is still processing the last.................... /* transmit or if this endpoint is invalid..................... /*.................... /* The result will be invalid if the specified input has not been configured for.................... /* use by the API with USB_EPx_TX_SIZE.................... /*.................... /*****************************************************************************/.................... int1 usb_tbe(int8 en);........................................ //////////////// END USER LEVEL FUNCTIONS /////////////////////////////////////............................................................ ///.................... /// BEGIN STACK LEVEL FUNCTIONS: These functions are meant to be called by.................... /// the USB stack, and are not meant to be called by the.................... /// user/application..................... ///........................................ /*****************************************************************************.................... /* usb_stall_ep().................... /*.................... /* Input: endpoint - endpoint to stall..................... /* top most bit indicates direction (set is IN, clear is OUT).................... /*.................... /* Summary: Stalls specified endpoint. If endpoint is stalled it will.................... /* send STALL packet if the host tries to access this endpoint's.................... /* buffer..................... /*.................... /* NOTE: WE ASSUME ENDPOINT IS VALID. USB.C SHOULD CHECK IF VALID BEFORE.................... /* CALLING THIS ROUTINE..................... /*.................... /*****************************************************************************/.................... void usb_stall_ep(int8 endpoint);............................................................ /*****************************************************************************.................... /* usb_unstall_ep(endpoint, direction).................... /*.................... /* Input: endpoint - endpoint to un-stall..................... /* top most bit indicates direction (set is IN, clear is OUT).................... /*.................... /* Summary: Un-stalls endpoint..................... /*.................... /* NOTE: WE ASSUME ENDPOINT IS VALID. USB.C SHOULD CHECK IF VALID BEFORE.................... /* CALLING THIS ROUTINE..................... /*.................... /*****************************************************************************/.................... void usb_unstall_ep(int8 endpoint);........................................ /*****************************************************************************.................... /* usb_endpoint_stalled().................... /*.................... /* Input: endpoint - endpoint to check.................... /* top most bit indicates direction (set is IN, clear is OUT).................... /*.................... /* Output: returns a TRUE if endpoint is stalled, FALSE if it is not..................... /*.................... /* Summary: Looks to see if an endpoint is stalled, or not. Does not look to.................... /* see if endpoint has been issued a STALL, just whether or not it is.................... /* configured to STALL on the next packet. See Set_Feature and.................... /* Clear_Feature Chapter 9 requests..................... /*.................... /* NOTE: WE ASSUME ENDPOINT IS VALID. USB.C SHOULD CHECK IF VALID BEFORE.................... /* CALLING THIS ROUTINE..................... /*.................... /*****************************************************************************/.................... int1 usb_endpoint_stalled(int8 endpoint);........................................ /*****************************************************************************.................... /* usb_set_address(address).................... /*.................... /* Input: address - address the host specified that we use.................... /*.................... /* Summary: Configures the USB Peripheral for the specified device address..................... /* The host will now talk to use with the following address..................... /*.................... /*****************************************************************************/.................... void usb_set_address(int8 address);........................................ /******************************************************************************.................... /* usb_set_configured().................... /*.................... /* Input: config - Configuration to use. 0 to uncofigure device..................... /*.................... /* Summary: Configures or unconfigures device. If configuring device it will.................... /* enable all the endpoints the user specified for this configuration..................... /* If un-configuring device it will disable all endpoints..................... /*.................... /* NOTE: CCS only provides code to handle 1 configuration..................... /*.................... /*****************************************************************************/.................... void usb_set_configured(int8 config);........................................ /******************************************************************************.................... /* usb_disable_endpoints().................... /*.................... /* Summary: Disables endpoints (all endpoints but 0).................... /*.................... /*****************************************************************************/.................... void usb_disable_endpoints(void);............................................................ /*******************************************************************************.................... /* usb_disable_endpoint().................... /*.................... /* Input: Endpoint to disable (0..15).................... /*.................... /* Summary: Disables specified endpoint.................... /*.................... /********************************************************************************/.................... void usb_disable_endpoint(int8 en);........................................ /**************************************************************.................... /* usb_request_send_response(len).................... /* usb_request_get_data().................... /* usb_request_stall().................... /*.................... /* Input: len - size of packet to send.................... /*.................... /* Summary: One of these 3 functions will be called by the USB stack after.................... /* handling a SETUP packet..................... /*.................... /* After we process a SETUP request, we have 1 of three responses:.................... /* 1.) send a response IN packet.................... /* 2.) wait for followup OUT packet(s) with data.................... /* 3.) stall because we don't support that SETUP request.................... /*.................... /* If we are sending data, the array usb_ep0_tx_buffer[] will hold.................... /* the response and the USB Request handler code will call.................... /* usb_request_send_response() to let us know how big the packet is..................... /*.................... /* If we are waiting for more data, usb_request_get_data() will.................... /* be called by the USB request handler code to configure the EP0 OUT.................... /* endpoint to be ready for more data.................... /*.................... /* If we don't support a request, usb_request_stall() will be called.................... /* by the USB request handler code to stall the endpoint 0..................... /*.................... /* The operation of these functions depends on how SETUP packets.................... /* are handled in the hardware layer..................... /*.................... /***************************************************************/.................... void usb_request_send_response(unsigned int8 len);.................... void usb_request_get_data(void);.................... void usb_request_stall(void);............................................................ #endif............................................................ //////////////////////// EXTRA USER FUNCTIONS /////////////////////////////////........................................ /**************************************************************.................... /* usb_flush_in().................... /*.................... /* Input: endpoint - which endpoint to mark for transfer.................... /* len - length of data that is being tramsferred.................... /* tgl - Data toggle synchronization for this packet.................... /*.................... /* Output: TRUE if success, FALSE if error (we don't control the endpoint).................... /*.................... /* Summary: Marks the endpoint ready for transmission. You must.................... /* have already loaded the endpoint buffer with data..................... /* (IN is PIC -> PC)..................... /* This routine is useful if you want to setup an endpoint by.................... /* writing to the buffer directly..................... /* This routine is not necessary if you use usb_put_packet()..................... /***************************************************************/.................... int1 usb_flush_in(int8 endpoint, int16 len, USB_DTS_BIT tgl);........................................ /**************************************************************.................... /* usb_flush_out().................... /*.................... /* Input: endpoint - which endpoint to mark for release.................... /* tgl - Data toggle synchronization to expect in the next packet.................... /*.................... /* Output: NONE.................... /*.................... /* Summary: Clears the previously received packet, and then marks this.................... /* endpoint's receive buffer as ready for more data..................... /* (OUT is PC -> PIC). This routine is useful if you do not.................... /* want to use usb_get_packet(), instead you want to handle with.................... /* the endpoint buffer directly. Also see usb_rx_packet_size()..................... /* This routine is not necessary if you use usb_get_packet()..................... /***************************************************************/.................... void usb_flush_out(int8 endpoint, USB_DTS_BIT tgl);........................................ /**************************************************************.................... /* usb_rx_packet_size().................... /*.................... /* Input: endpoint - which endpoint to mark to check.................... /*.................... /* Output: Returns the number of bytes in the endpoint's receive buffer..................... /*.................... /* Summary: Read the number of data stored in the receive buffer. When you.................... /* have handled the data, use usb_flush_out() to clear the buffer. The.................... /* result may be invalid if usb_kbhit() returns false for this endpoint..................... /* This routine is not necessary if you use usb_get_packet()..................... /***************************************************************/.................... int16 usb_rx_packet_size(int8 endpoint);........................................ #ENDIF........................................ #else.................... #include <pic24_usb.h> //Microchip 18Fxx5x hardware layer for usb.c.................... #endif.................... #else.................... #include <usbn960x.h>.................... #endif.................... #include <usb_desc_cdc.h> //USB Configuration and Device descriptors for this UBS device.................... ///////////////////////////////////////////////////////////////////////////.................... //// usb_desc_cdc.h ////.................... //// ////.................... //// An example set of device / configuration descriptors for use with ////.................... //// CCS's CDC Virtual COM Port driver (see usb_cdc.h) ////.................... //// ////.................... //// This file is part of CCS's PIC USB driver code. See USB.H ////.................... //// for more documentation and a list of examples. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// 10/28/05: ////.................... //// Bulk endpoint sizes updated to allow more than 255 byte ////.................... //// packets. ////.................... //// Changed device to USB 1.10 ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2005 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... ///////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __USB_DESCRIPTORS__.................... #DEFINE __USB_DESCRIPTORS__........................................ ///////// config options, although it's best to leave alone for this demo /////.................... #define USB_CONFIG_PID 0x0033.................... #define USB_CONFIG_VID 0x0461.................... #define USB_CONFIG_BUS_POWER 100 //100mA (range is 0..500).................... #define USB_CONFIG_VERSION 0x0100 //01.00 //range is 00.00 to 99.99.................... //////// end config ///////////////////////////////////////////////////////////........................................ #DEFINE USB_HID_DEVICE FALSE.................... #DEFINE USB_CDC_DEVICE TRUE........................................ #define USB_CDC_COMM_IN_ENDPOINT 1.................... #define USB_CDC_COMM_IN_SIZE 8.................... #define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT.................... #define USB_EP1_TX_SIZE USB_CDC_COMM_IN_SIZE........................................ //pic to pc endpoint config.................... #define USB_CDC_DATA_IN_ENDPOINT 2.................... #define USB_CDC_DATA_IN_SIZE 64.................... #define USB_EP2_TX_ENABLE USB_ENABLE_BULK.................... #define USB_EP2_TX_SIZE USB_CDC_DATA_IN_SIZE........................................ //pc to pic endpoint config.................... #define USB_CDC_DATA_OUT_ENDPOINT 2.................... #define USB_CDC_DATA_OUT_SIZE 64.................... #define USB_EP2_RX_ENABLE USB_ENABLE_BULK.................... #define USB_EP2_RX_SIZE USB_CDC_DATA_OUT_SIZE........................................ #include <usb.h>.................... ///////////////////////////////////////////////////////////////////////////.................... //// usb.h ////.................... //// ////.................... //// Function protypes, defintions and globals used by CCS USB driver ////.................... //// ////.................... //// This file is part of CCS's USB driver code ////.................... //// ////.................... //// The following USB examples are provided by CCS: ////.................... //// ex_usb_mouse.c - A HID Mouse. ////.................... //// ex_usb_hid.c - A custom application using HID protocol. ////.................... //// ex_usb_kbmouse.c - A HID Mouse/Keyboard combo using multiple ////.................... //// interfaces. ////.................... //// ex_usb_kbmouse2.c - A HID Mouse/Keyboard combo using multiple ////.................... //// HID Reports. ////.................... //// ex_usb_scope.c - A digital oscilloscope using a custom ////.................... //// protocol requiring custom Windows drivers. ////.................... //// ex_usb_serial.c - ////.................... //// ex_usb_serial2.c - Two examples of using the CDC driver for ////.................... //// a virtual COM port. ////.................... //// ex_usb_bootloader.c - CDC/Virtual COM port bootloader. ////.................... //// ex_usb_loadmouse.c - A conversion of ex_usb_mouse.c that is ////.................... //// compatible with ex_usb_mouse.c. ////.................... //// ////.................... //// ********************** API ********************* ////.................... //// ////.................... //// These are the functions that are meant to be called by the user: ////.................... //// ////.................... //// usb_init() - Initializes the USB stack, the USB peripheral and ////.................... //// attaches the unit to the usb bus. Enables ////.................... //// interrupts. Will wait in an infinite loop until ////.................... //// the device enumerates - if you are using ////.................... //// connection sense or if the processor should run ////.................... //// even if it's not connected to USB then use ////.................... //// usb_init_cs() instead. ////.................... //// ////.................... //// usb_init_cs() - A smaller usb_init(), does not attach unit ////.................... //// to usb bus or enable interrupts. Since this does ////.................... //// not attach to the USB, you must periodically call ////.................... //// usb_task(). See usb_task() for more information. ////.................... //// ////.................... //// usb_task() - If usb_init_cs() was used to initiate the USB ////.................... //// peripheral, usb_task() should then be called periodically ////.................... //// to check the connection sense pin. If the connection ////.................... //// sense pin denotes USB is connected and the USB peripheral ////.................... //// is not attached, this will attach the USB peripheral ////.................... //// so the PC can start the enumeration process (and it ////.................... //// will enable interrupts). If the connection sense pin ////.................... //// denotes USB is not attached and the USB peripheral is ////.................... //// running, this will reset the USB peripheral and wait ////.................... //// for USB to reconnect (and usb_enumerated() will start ////.................... //// returning FALSE). If connection sense (USB_CON_SENSE_PIN) ////.................... //// is not defined the usb_task() assumes that USB is always ////.................... //// connected. ////.................... //// ////.................... //// usb_attached() - Returns TRUE if the device is attached to a ////.................... //// USB cable. A macro that looks at the defined ////.................... //// connection sense pin. If this returns TRUE ////.................... //// it does not mean the PC has connected to it, ////.................... //// you need to use usb_enumerated() to check this. ////.................... //// ////.................... //// usb_enumerated() - Returns TRUE if device has been enumerated ////.................... //// (configured) by host, FALSE if it has not. ////.................... //// Do not try to use the USB peripheral for ////.................... //// sending and receiving packets until you ////.................... //// are enumerated. ////.................... //// ////.................... //// usb_wait_for_enumeration() - Sits in an infinte loop until device ////.................... //// is enumerated. ////.................... //// ////.................... //// usb_tbe(endpoint) - Returns TRUE if the endpoint transmit buffer ////.................... //// is free and ready to accept a new packet for transmission. ////.................... //// ////.................... //// usb_put_packet(endpoint, ptr, len, tgl) - Sends one packet to the ////.................... //// host. If you need to send a message that ////.................... //// spans more than one packet then use ////.................... //// usb_puts(). Fore more detailed documentation ////.................... //// see usb_hw_layer.h ////.................... //// ////.................... //// usb_puts(endpoint, ptr, len, timeout) - Sends a multiple packet ////.................... //// message to the host. If you only need to send one packet, ////.................... //// it is more effecient to use usb_put_packet(). This is ////.................... //// documented in more detail above the prototype in USB.H. ////.................... //// ////.................... //// usb_kbhit(endpoint) - Returns true if OUT endpoint contains data ////.................... //// from host. This will remain TRUE until ////.................... //// usb_put_packet() or usb_flush_out() used. ////.................... //// This function will return an invalid response ////.................... //// if specified endpoint is not enabled for ////.................... //// receiving data. ////.................... //// ////.................... //// len = usb_get_packet(endpoint, ptr, max) - Gets one packet that ////.................... //// from the host/PC. usb_kbhit() must return TRUE before you ////.................... //// call this routine or your data may not be valid. This ////.................... //// only receives one packet, if you are trying to receive a ////.................... //// multi-packet message use usb_gets(). For more detailed ////.................... //// documentation see usb_hw_layer.h. ////.................... //// ////.................... //// len = usb_gets(endpoint, ptr, max, timeout) - Gets multiple ////.................... //// packets from the host, you would use this instead ////.................... //// of usb_get_packet() if you wanted to handle multi-packet ////.................... //// messages. This is documented in more detail above the ////.................... //// prototype in USB.H. ////.................... //// ////.................... //// ////.................... //// ********* DEFINITIONS / CONFIGURATION ********** ////.................... //// ////.................... //// The following definitions are declared here, but can be ////.................... //// overwritten in your code. Unless needed otherwise, leave ////.................... //// to default value. If confused about a definition read the ////.................... //// comments at each defintion ////.................... //// ////.................... //// USB_HID_IDLE (TRUE) - Set to TRUE if your device supports ////.................... //// Set_Idle HID class request. Set to ////.................... //// False if you do not (device will ////.................... //// send a Wrong-state if computer ////.................... //// sends a Set_Idle / Get_Idle command) ////.................... //// NOTE: If you set to TRUE you must ////.................... //// provide your own code. See ////.................... //// usb_isr_tkn_setup_ClassInterface() in ////.................... //// usb.c ////.................... //// ////.................... //// USB_HID_BOOT_PROTOCOL (FALSE) - Set to TRUE if your device supports ////.................... //// Set_Protocl HID class request. Set to ////.................... //// False if you do not (device will ////.................... //// send a Wrong-state if computer ////.................... //// sends a Set_Protocl / Get_Protocol ////.................... //// command). ////.................... //// NOTE: If you set to TRUE you must ////.................... //// provide your own code in the ////.................... //// application that properly send boot ////.................... //// or HID packets. ////.................... //// ////.................... //// USB_MAX_EP0_PACKET_LENGTH (8) - Max Packet size for Endpoint 0. ////.................... //// The hardware level driver (ex ////.................... //// pic18_usb.h will define this value if ////.................... //// not already overwritten). Increasing ////.................... //// this size will speed up the ////.................... //// enumeration process. ////.................... //// ////.................... //// USB_EPx_RX_ENABLE (USB_ENABLE_DISABLED) - Where x is the ////.................... //// endpoint number. Change this define ////.................... //// to specify what kind of transfer method ////.................... //// this RX (PC to device) endpoint uses. ////.................... //// Here is the list of valid transfer methods: ////.................... //// USB_ENABLE_CONTROL ////.................... //// USB_ENABLE_ISOCHRONOUS ////.................... //// USB_ENABLE_BULK ////.................... //// USB_ENABLE_INTERRUPT ////.................... //// USB_ENABLE_DISABLED ////.................... //// Don't forget that you must specify the ////.................... //// transfer method properly in your endpoint ////.................... //// descriptor, too. ////.................... //// ////.................... //// USB_EPx_RX_SIZE (0) - For some hardware it is important to know ////.................... //// how much memory to reserve for receiving ////.................... //// packets. ////.................... //// ////.................... //// *** NOTE *** You must have both USB_EPx_RX_ENABLE defined and ////.................... //// USB_EPx_RX_SIZE defined (non 0) to enable a RX ////.................... //// endpoint. ////.................... //// *** NOTE *** Endpoint 0 is always enabled. ////.................... //// ////.................... //// USB_EPx_TX_ENABLE (USB_ENABLE_DISABLED) - Where x is the ////.................... //// endpoint number. Change this define ////.................... //// to specify what kind of transfer method ////.................... //// this TX (device to PC) endpoint uses. ////.................... //// Here is the list of valid transfer methods: ////.................... //// USB_ENABLE_CONTROL ////.................... //// USB_ENABLE_ISOCHRONOUS ////.................... //// USB_ENABLE_BULK ////.................... //// USB_ENABLE_INTERRUPT ////.................... //// USB_ENABLE_DISABLED ////.................... //// Don't forget that you must specify the ////.................... //// transfer method properly in your endpoint ////.................... //// descriptor, too. ////.................... //// ////.................... //// USB_EPx_TX_SIZE (0) - For some hardware it is important to know ////.................... //// how much memory to reserve for transmitting ////.................... //// packets. ////.................... //// ////.................... //// *** NOTE *** You must have both USB_EPx_TX_ENABLE defined and ////.................... //// USB_EPx_TX_SIZE defined (non 0) to enable a TX ////.................... //// endpoint. ////.................... //// ////.................... //// ////.................... //// USB_HID_DEVICE (TRUE) - HID devices require extra code to handle ////.................... //// HID requests. You can disable to save ////.................... //// ROM space if you are not using a HID ////.................... //// device. If you are not using a HID ////.................... //// device you must provide your own O/S ////.................... //// (Windows) driver. ////.................... //// ////.................... //// The other definitions should not be changed. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// July 13th, 2005: ////.................... //// usb_ep_tx_size[] and usb_ep_rx_size[] changed to 16bits ////.................... //// ////.................... //// June 20th, 2005: ////.................... //// Initial 18fxx5x release ////.................... //// ////.................... //// May 13th, 2005: ////.................... //// Beta release, only works with 18Fxx5x hardware layer. ////.................... //// Now supports multiple interfaces (many defines in descriptors ////.................... //// will have to be changed, see examples) ////.................... //// ////.................... //// Mar 21st, 2005: ////.................... //// Initial Alpha Release with PIC18Fxx5x support. (ONLY TESTED ////.................... //// WITH 18F4550) ////.................... //// usb_gets() and usb_puts() changed (see usb.c) ////.................... //// ////.................... //// June 24th, 2004: ////.................... //// Optimization and cleanup. ////.................... //// The following definitions changed: ////.................... //// USB_EPx_TX_ENABLE and USB_EPx_RX_ENABLE have changed. See usb.h ////.................... //// USB_CONFIG_DESCRIPTORS[] removed ////.................... //// USB_CONFIG_DESC_LEN changed to USB_DESC_CONFIG_LEN ////.................... //// USB_INTERFACE_DESC_LEN changed to USB_DESC_INTERFACE_LEN ////.................... //// USB_CLASS_DESC_LEN changed to USB_DESC_CLASS_LEN ////.................... //// USB_ENDPOINT_DESC_LEN changed to USB_DESC_ENDPOINT_LEN ////.................... //// USB_CONFIG_DESC_KEY changed to USB_DESC_CONFIG_TYPE ////.................... //// USB_INTERFACE_DESC_KEY changed to USB_DESC_INTERFACE_TYPE ////.................... //// USB_CLASS_DESC_KEY changed to USB_DESC_CLASS_TYPE ////.................... //// USB_ENDPOINT_DESC_KEY changed to USB_DESC_ENDPOINT_TYPE ////.................... //// USB_STRING_X[] arrays removed, see USB_STRING_DESC[] and ////.................... //// USB_STRING_DESC_OFFSET[] ////.................... //// dev_req, curr_config, status_device and getdesc_type global ////.................... //// variables moved into struct USB_stack_status ////.................... //// ////.................... //// October 15th, 2003: Support for boot protocol added. ////.................... //// Set USB_HID_BOOT_PROTOCOL to TRUE to support this. ////.................... //// The array hid_protocol[] saves which protocol mode each ////.................... //// interface is in. It is your applications job to send ////.................... //// data that either fit the boot protocol or HID protocol. ////.................... //// ////.................... //// May 6th, 2003: Fixed a potential stack overflow using PCM ////.................... //// ////.................... //// October 29th, 2002: New definition added to USB_STATES ////.................... //// ////.................... //// August 2nd, 2002: Initial Public Release ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2009 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... ///////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __USB_PROTOTYPES__.................... #DEFINE __USB_PROTOTYPES__........................................ //// CONFIGURATION ////////////////////////////////////////////////////////////........................................ #ifndef USB_CON_SENSE_PIN.................... #define USB_CON_SENSE_PIN 0.................... #endif........................................ #IFNDEF USB_HID_BOOT_PROTOCOL.................... #DEFINE USB_HID_BOOT_PROTOCOL FALSE.................... #ENDIF........................................ #IFNDEF USB_HID_IDLE.................... #DEFINE USB_HID_IDLE FALSE.................... #ENDIF........................................ //should the compiler add the extra HID handler code? Defaults to yes..................... #IFNDEF USB_HID_DEVICE.................... #DEFINE USB_HID_DEVICE TRUE.................... #ENDIF........................................ #IFNDEF USB_CDC_DEVICE.................... #DEFINE USB_CDC_DEVICE FALSE.................... #ENDIF........................................ //set to false to opt for less RAM, true to opt for less ROM.................... #ifndef USB_OPT_FOR_ROM.................... #define USB_OPT_FOR_ROM TRUE.................... #endif........................................ #IFNDEF USB_MAX_EP0_PACKET_LENGTH.................... #DEFINE USB_MAX_EP0_PACKET_LENGTH 8.................... #ENDIF............................................................ ////// USER-LEVEL API /////////////////////////////////////////////////////////........................................ /**************************************************************.................... /* usb_enumerated().................... /*.................... /* Input: Global variable USB_Curr_Config.................... /* Returns: Returns a 1 if device is configured / enumerated,.................... /* Returns a 0 if device is un-configured / not enumerated..................... /*.................... /* Summary: See API section of USB.H for more documentation..................... /***************************************************************/.................... int1 usb_enumerated(void);........................................ /**************************************************************.................... /* usb_wait_for_enumeration().................... /*.................... /* Input: Global variable USB_Curr_Config.................... /*.................... /* Summary: Waits in-definately until device is configured / enumerated..................... /* See API section of USB.H for more information..................... /***************************************************************/.................... void usb_wait_for_enumeration(void);........................................ /****************************************************************************.................... /* usb_gets(endpoint, ptr, max, timeout).................... /*.................... /* Input: endpoint - endpoint to get data from.................... /* ptr - place / array to store data to.................... /* max - max amount of data to get from USB and store into ptr.................... /* timeout - time in milliseconds, for each packet, to wait before.................... /* timeout. set to 0 for no timeout..................... /*.................... /* Output: Amount of data returned. It may be less than max..................... /*.................... /* Summary: Gets data from the host. Will get multiple-packet messages.................... /* and finish when either it receives a 0-len packet or a packet.................... /* of less size than maximum..................... /*.................... /*****************************************************************************/.................... unsigned int16 usb_gets(int8 endpoint, int8 * ptr, unsigned int16 max, unsigned int16 timeout);........................................ /****************************************************************************.................... /* usb_puts().................... /*.................... /* Inputs: endpoint - endpoint to send data out.................... /* ptr - points to array of data to send.................... /* len - amount of data to send.................... /* timeout - time in milli-seconds, for each packet, to wait before.................... /* timeout. set to 0 for no timeout..................... /*.................... /* Outputs: Returns TRUE if message sent succesfully, FALSE if it was not.................... /* sent before timeout period expired..................... /*.................... /* Summary: Used for sending multiple packets of data as one message. This.................... /* function can still be used to send messages consiting of only one.................... /* packet. See usb_put_packet() documentation for the rules about when.................... /* multiple packet messages or 0-lenght packets are needed..................... /*.................... /*****************************************************************************/.................... int1 usb_puts(int8 endpoint, int8 * ptr, unsigned int16 len, unsigned int8 timeout);........................................ /******************************************************************************.................... /* usb_attached().................... /*.................... /* Summary: Returns TRUE if the device is attached to a USB cable..................... /* See the API section of USB.H for more documentation..................... /*.................... /*****************************************************************************/.................... #if USB_CON_SENSE_PIN.................... #define usb_attached() input(USB_CON_SENSE_PIN).................... #else.................... #define usb_attached() TRUE.................... #endif........................................ ////// END USER-LEVEL API /////////////////////////////////////////////////////............................................................ ////// STACK-LEVEL API USED BY HW DRIVERS ////////////////////////////////////........................................ enum USB_STATES {GET_DESCRIPTOR=1,SET_ADDRESS=2,NONE=0};........................................ enum USB_GETDESC_TYPES {USB_GETDESC_CONFIG_TYPE=0,USB_GETDESC_HIDREPORT_TYPE=1,USB_GETDESC_STRING_TYPE=2,USB_GETDESC_DEVICE_TYPE=3};........................................ #if USB_OPT_FOR_ROM.................... typedef struct {.................... USB_STATES dev_req; //what did the last setup token set us up to do?. init at none.................... int curr_config; //our current config. start at none/powered (NOT THAT THIS LIMITS US TO 3 CONFIGURATIONS).................... int status_device; //Holds our state for Set_Feature and Clear_Feature.................... USB_GETDESC_TYPES getdesc_type; //which get_descriptor() we are handling.................... } TYPE_USB_STACK_STATUS;.................... #else.................... typedef struct {.................... USB_STATES dev_req:2; //what did the last setup token set us up to do?. init at none.................... int Curr_config:2; //our current config. start at none/powered (NOT THAT THIS LIMITS US TO 3 CONFIGURATIONS).................... int status_device:2; //Holds our state for Set_Feature and Clear_Feature.................... USB_GETDESC_TYPES getdesc_type:2; //which get_descriptor() we are handling.................... } TYPE_USB_STACK_STATUS;.................... #endif........................................ extern TYPE_USB_STACK_STATUS USB_stack_status;........................................ /**************************************************************.................... /* usb_token_reset().................... /*.................... /* Output: No output (but many global registers are modified).................... /*.................... /* Summary: Resets the token handler to initial (unconfigured) state..................... /***************************************************************/.................... void usb_token_reset(void);........................................ /**************************************************************.................... /* usb_isr_tok_setup_dne().................... /*.................... /* Input: usb_ep0_rx_buffer[] contains the the setup packet..................... /*.................... /* Output: None (many globals are changed).................... /*.................... /* Summary: This function is that handles the setup token..................... /* We must handle all relevant requests, such as Set_Configuration,.................... /* Get_Descriptor, etc..................... /*.................... /* usb_ep0_rx_buffer[] contains setup data packet, which has the.................... /* following records:.................... /* -------------------------------------------------------------------------------------------.................... /* usb_ep0_rx_buffer[ 0 ]=bmRequestType; Where the setup packet goes.................... /* bit7 (0) host-to-device.................... /* (1) device-to-host.................... /* bit6-5 (00) usb standard request;.................... /* (01) class request;.................... /* (10) vendor request.................... /* (11) reserved.................... /* bit4-0 (0000) device.................... /* (0001) interface.................... /* (0010) endpoint.................... /* (0011) other element.................... /* (0100) to (1111) reserved.................... /* usb_ep0_rx_buffer[ 1 ]=bRequest ; the request.................... /* usb_ep0_rx_buffer[2,3]=wValue ; a value which corresponds to request.................... /* usb_ep0_rx_buffer[4,5]=wIndex ; could correspond to interface or endpoint....................... /* usb_ep0_rx_buffer[6,7]=wLength ; number of bytes in next data packet;.................... /* for host-to-device, this exactly how many bytes in data packet..................... /* for device-to-host, this is the maximum bytes that can fit one packet..................... /***************************************************************/.................... void usb_isr_tok_setup_dne(void);........................................ /**************************************************************.................... /* usb_isr_tok_out_dne().................... /*.................... /* Input: endpoint contains which endpoint we are receiving data (0..15).................... /*.................... /* Summary: Processes out tokens (out is respective of the host, so actualy.................... /* incoming to the pic), but not out setup tokens. Normally when.................... /* data is received it is left in the buffer (user would use.................... /* usb_kbhit() and usb_get_packet() to receive data), but certain.................... /* libraries (like CDC) have to answer setup packets..................... /*.................... /***************************************************************/.................... void usb_isr_tok_out_dne(int8 endpoint);........................................ /**************************************************************.................... /* usb_isr_tok_in_dne(endpoint).................... /*.................... /* Input: endpoint - which endpoint we are processing a setup token..................... /*.................... /* Summary: This handles an IN packet (HOST <- PIC). For endpoint 0, this.................... /* is usually to setup a response packet to a setup packet. Endpoints 1..15.................... /* are generally ignored, and the user has to use usb_tbe() to determine if.................... /* if the buffer is ready for a new transmit packet (there are special cases,.................... /* like CDC which handles the CDC protocl)..................... /*.................... /***************************************************************/.................... void usb_isr_tok_in_dne(int8 endpoint);........................................ ////// END STACK-LEVEL API USED BY HW DRIVERS /////////////////////////////////............................................................ //CCS only supports one configuration at this time.................... #DEFINE USB_NUM_CONFIGURATIONS 1 //DO NOT CHANGE........................................ //PID values for tokens (see page 48 of USB Complete ed.1).................... #define PID_IN 0x09 //device to host transactions.................... #define PID_OUT 0x01 //host to device transactions.................... #define PID_SETUP 0x0D //host to device setup transaction.................... #define PID_ACK 0x02 //receiver accepts error-free data packet.................... #define PID_DATA0 0x03 //data packet with even sync bit.................... #define PID_SOF 0x05 //start of framer marker and frame number.................... #define PID_NAK 0x0A //receiver can't accept data or sender cant send data or has no data to transmit.................... #define PID_DATA1 0x0B //data packet with odd sync bit.................... #define PID_PRE 0x0C //preamble issued by host. enables downstream traffic to low-speed device.................... #define PID_STALL 0x0E //a control request isnt supported or the endpoint is halted........................................ //Key which identifies descritpors.................... #DEFINE USB_DESC_DEVICE_TYPE 0x01 //#DEFINE USB_DEVICE_DESC_KEY 0x01.................... #DEFINE USB_DESC_CONFIG_TYPE 0x02 //#DEFINE USB_CONFIG_DESC_KEY 0x02.................... #DEFINE USB_DESC_STRING_TYPE 0x03 //#DEFINE USB_STRING_DESC_KEY 0x03.................... #DEFINE USB_DESC_INTERFACE_TYPE 0x04 //#DEFINE USB_INTERFACE_DESC_KEY 0x04.................... #DEFINE USB_DESC_ENDPOINT_TYPE 0x05 //#DEFINE USB_ENDPOINT_DESC_KEY 0x05.................... #DEFINE USB_DESC_CLASS_TYPE 0x21 //#DEFINE USB_CLASS_DESC_KEY 0x21.................... #DEFINE USB_DESC_HIDREPORT_TYPE 0x22........................................ //The length of each descriptor.................... #DEFINE USB_DESC_DEVICE_LEN 18 //#DEFINE USB_DEVICE_DESC_LEN 18.................... #DEFINE USB_DESC_CONFIG_LEN 9 //#DEFINE USB_CONFIG_DESC_LEN 9.................... #DEFINE USB_DESC_INTERFACE_LEN 9 //#DEFINE USB_INTERFACE_DESC_LEN 9.................... #DEFINE USB_DESC_CLASS_LEN 9 //#DEFINE USB_CLASS_DESC_LEN 9.................... #DEFINE USB_DESC_ENDPOINT_LEN 7 //#DEFINE USB_ENDPOINT_DESC_LEN 7........................................ //Standard USB Setup bRequest Codes.................... #define USB_STANDARD_REQUEST_GET_STATUS 0x00.................... #define USB_STANDARD_REQUEST_CLEAR_FEATURE 0x01.................... #define USB_STANDARD_REQUEST_SET_FEATURE 0x03.................... #define USB_STANDARD_REQUEST_SET_ADDRESS 0x05.................... #define USB_STANDARD_REQUEST_GET_DESCRIPTOR 0x06.................... #define USB_STANDARD_REQUEST_SET_DESCRIPTOR 0x07.................... #define USB_STANDARD_REQUEST_GET_CONFIGURATION 0x08.................... #define USB_STANDARD_REQUEST_SET_CONFIGURATION 0x09.................... #define USB_STANDARD_REQUEST_GET_INTERFACE 0x0A.................... #define USB_STANDARD_REQUEST_SET_INTERFACE 0x0B.................... #define USB_STANDARD_REQUEST_SYNCH_FRAME 0x0C........................................ //HID Class Setup bRequest Codes.................... #define USB_HID_REQUEST_GET_REPORT 0x01.................... #define USB_HID_REQUEST_GET_IDLE 0x02.................... #define USB_HID_REQUEST_GET_PROTOCOL 0x03.................... #define USB_HID_REQUEST_SET_REPORT 0x09.................... #define USB_HID_REQUEST_SET_IDLE 0x0A.................... #define USB_HID_REQUEST_SET_PROTOCOL 0x0B........................................ //types of endpoints as defined in the descriptor.................... #define USB_ENDPOINT_TYPE_CONTROL 0x00.................... #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01.................... #define USB_ENDPOINT_TYPE_BULK 0x02.................... #define USB_ENDPOINT_TYPE_INTERRUPT 0x03........................................ //types of endpoints used internally in this api.................... #define USB_ENABLE_DISABLED -1.................... #define USB_ENABLE_BULK USB_ENDPOINT_TYPE_BULK.................... #define USB_ENABLE_ISOCHRONOUS USB_ENDPOINT_TYPE_ISOCHRONOUS.................... #define USB_ENABLE_INTERRUPT USB_ENDPOINT_TYPE_INTERRUPT.................... #define USB_ENABLE_CONTROL USB_ENDPOINT_TYPE_CONTROL............................................................ //*** ENABLE RX ENDPOINTS AND BUFFERS........................................ //--------- endpoint 0 defines ----------.................... #define USB_EP0_TX_ENABLE USB_ENABLE_CONTROL.................... #define USB_EP0_RX_ENABLE USB_ENABLE_CONTROL.................... #define USB_EP0_RX_SIZE USB_MAX_EP0_PACKET_LENGTH //endpoint 0 is setup, and should always be the MAX_PACKET_LENGTH. Slow speed specifies 8.................... #define USB_EP0_TX_SIZE USB_MAX_EP0_PACKET_LENGTH //endpoint 0 is setup, and should always be the MAX_PACKET_LENGTH. Slow speed specifies 8........................................ //--------- endpoint 1 defines ----------.................... #ifndef USB_EP1_TX_ENABLE.................... #define USB_EP1_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP1_RX_ENABLE.................... #define USB_EP1_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP1_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP1_RX_SIZE.................... #undef USB_EP1_RX_SIZE.................... #endif.................... #define USB_EP1_RX_SIZE 0.................... #else.................... #ifndef USB_EP1_RX_SIZE.................... #error You enabled EP1 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP1_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP1_TX_SIZE.................... #undef USB_EP1_TX_SIZE.................... #endif.................... #define USB_EP1_TX_SIZE 0.................... #else.................... #ifndef USB_EP1_TX_SIZE.................... #error You enabled EP1 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 2 defines ----------.................... #ifndef USB_EP2_TX_ENABLE.................... #define USB_EP2_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP2_RX_ENABLE.................... #define USB_EP2_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP2_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP2_RX_SIZE.................... #undef USB_EP2_RX_SIZE.................... #endif.................... #define USB_EP2_RX_SIZE 0.................... #else.................... #ifndef USB_EP2_RX_SIZE.................... #error You enabled EP2 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP2_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP2_TX_SIZE.................... #undef USB_EP2_TX_SIZE.................... #endif.................... #define USB_EP2_TX_SIZE 0.................... #else.................... #ifndef USB_EP2_TX_SIZE.................... #error You enabled EP2 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 3 defines ----------.................... #ifndef USB_EP3_TX_ENABLE.................... #define USB_EP3_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP3_RX_ENABLE.................... #define USB_EP3_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP3_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP3_RX_SIZE.................... #undef USB_EP3_RX_SIZE.................... #endif.................... #define USB_EP3_RX_SIZE 0.................... #else.................... #ifndef USB_EP3_RX_SIZE.................... #error You enabled EP3 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP3_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP3_TX_SIZE.................... #undef USB_EP3_TX_SIZE.................... #endif.................... #define USB_EP3_TX_SIZE 0.................... #else.................... #ifndef USB_EP3_TX_SIZE.................... #error You enabled EP3 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 4 defines ----------.................... #ifndef USB_EP4_TX_ENABLE.................... #define USB_EP4_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP4_RX_ENABLE.................... #define USB_EP4_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP4_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP4_RX_SIZE.................... #undef USB_EP4_RX_SIZE.................... #endif.................... #define USB_EP4_RX_SIZE 0.................... #else.................... #ifndef USB_EP4_RX_SIZE.................... #error You enabled EP4 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP4_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP4_TX_SIZE.................... #undef USB_EP4_TX_SIZE.................... #endif.................... #define USB_EP4_TX_SIZE 0.................... #else.................... #ifndef USB_EP4_TX_SIZE.................... #error You enabled EP4 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 5 defines ----------.................... #ifndef USB_EP5_TX_ENABLE.................... #define USB_EP5_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP5_RX_ENABLE.................... #define USB_EP5_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP5_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP5_RX_SIZE.................... #undef USB_EP5_RX_SIZE.................... #endif.................... #define USB_EP5_RX_SIZE 0.................... #else.................... #ifndef USB_EP5_RX_SIZE.................... #error You enabled EP5 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP5_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP5_TX_SIZE.................... #undef USB_EP5_TX_SIZE.................... #endif.................... #define USB_EP5_TX_SIZE 0.................... #else.................... #ifndef USB_EP5_TX_SIZE.................... #error You enabled EP5 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 6 defines ----------.................... #ifndef USB_EP6_TX_ENABLE.................... #define USB_EP6_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP6_RX_ENABLE.................... #define USB_EP6_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP6_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP6_RX_SIZE.................... #undef USB_EP6_RX_SIZE.................... #endif.................... #define USB_EP6_RX_SIZE 0.................... #else.................... #ifndef USB_EP6_RX_SIZE.................... #error You enabled EP6 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP6_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP6_TX_SIZE.................... #undef USB_EP6_TX_SIZE.................... #endif.................... #define USB_EP6_TX_SIZE 0.................... #else.................... #ifndef USB_EP6_TX_SIZE.................... #error You enabled EP6 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 7 defines ----------.................... #ifndef USB_EP7_TX_ENABLE.................... #define USB_EP7_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP7_RX_ENABLE.................... #define USB_EP7_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP7_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP7_RX_SIZE.................... #undef USB_EP7_RX_SIZE.................... #endif.................... #define USB_EP7_RX_SIZE 0.................... #else.................... #ifndef USB_EP7_RX_SIZE.................... #error You enabled EP7 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP7_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP7_TX_SIZE.................... #undef USB_EP7_TX_SIZE.................... #endif.................... #define USB_EP7_TX_SIZE 0.................... #else.................... #ifndef USB_EP7_TX_SIZE.................... #error You enabled EP7 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 8 defines ----------.................... #ifndef USB_EP8_TX_ENABLE.................... #define USB_EP8_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP8_RX_ENABLE.................... #define USB_EP8_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP8_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP8_RX_SIZE.................... #undef USB_EP8_RX_SIZE.................... #endif.................... #define USB_EP8_RX_SIZE 0.................... #else.................... #ifndef USB_EP8_RX_SIZE.................... #error You enabled EP8 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP8_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP8_TX_SIZE.................... #undef USB_EP8_TX_SIZE.................... #endif.................... #define USB_EP8_TX_SIZE 0.................... #else.................... #ifndef USB_EP8_TX_SIZE.................... #error You enabled EP8 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 9 defines ----------.................... #ifndef USB_EP9_TX_ENABLE.................... #define USB_EP9_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP9_RX_ENABLE.................... #define USB_EP9_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP9_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP9_RX_SIZE.................... #undef USB_EP9_RX_SIZE.................... #endif.................... #define USB_EP9_RX_SIZE 0.................... #else.................... #ifndef USB_EP9_RX_SIZE.................... #error You enabled EP9 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP9_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP9_TX_SIZE.................... #undef USB_EP9_TX_SIZE.................... #endif.................... #define USB_EP9_TX_SIZE 0.................... #else.................... #ifndef USB_EP9_TX_SIZE.................... #error You enabled EP9 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 10 defines ----------.................... #ifndef USB_EP10_TX_ENABLE.................... #define USB_EP10_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP10_RX_ENABLE.................... #define USB_EP10_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP10_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP10_RX_SIZE.................... #undef USB_EP10_RX_SIZE.................... #endif.................... #define USB_EP10_RX_SIZE 0.................... #else.................... #ifndef USB_EP10_RX_SIZE.................... #error You enabled EP10 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP10_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP10_TX_SIZE.................... #undef USB_EP10_TX_SIZE.................... #endif.................... #define USB_EP10_TX_SIZE 0.................... #else.................... #ifndef USB_EP10_TX_SIZE.................... #error You enabled EP10 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 11 defines ----------.................... #ifndef USB_EP11_TX_ENABLE.................... #define USB_EP11_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP11_RX_ENABLE.................... #define USB_EP11_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP11_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP11_RX_SIZE.................... #undef USB_EP11_RX_SIZE.................... #endif.................... #define USB_EP11_RX_SIZE 0.................... #else.................... #ifndef USB_EP11_RX_SIZE.................... #error You enabled EP11 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP11_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP11_TX_SIZE.................... #undef USB_EP11_TX_SIZE.................... #endif.................... #define USB_EP11_TX_SIZE 0.................... #else.................... #ifndef USB_EP11_TX_SIZE.................... #error You enabled EP11 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 12 defines ----------.................... #ifndef USB_EP12_TX_ENABLE.................... #define USB_EP12_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP12_RX_ENABLE.................... #define USB_EP12_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP12_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP12_RX_SIZE.................... #undef USB_EP12_RX_SIZE.................... #endif.................... #define USB_EP12_RX_SIZE 0.................... #else.................... #ifndef USB_EP12_RX_SIZE.................... #error You enabled EP12 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP12_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP12_TX_SIZE.................... #undef USB_EP12_TX_SIZE.................... #endif.................... #define USB_EP12_TX_SIZE 0.................... #else.................... #ifndef USB_EP12_TX_SIZE.................... #error You enabled EP12 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 13 defines ----------.................... #ifndef USB_EP13_TX_ENABLE.................... #define USB_EP13_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP13_RX_ENABLE.................... #define USB_EP13_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP13_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP13_RX_SIZE.................... #undef USB_EP13_RX_SIZE.................... #endif.................... #define USB_EP13_RX_SIZE 0.................... #else.................... #ifndef USB_EP13_RX_SIZE.................... #error You enabled EP13 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP13_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP13_TX_SIZE.................... #undef USB_EP13_TX_SIZE.................... #endif.................... #define USB_EP13_TX_SIZE 0.................... #else.................... #ifndef USB_EP13_TX_SIZE.................... #error You enabled EP13 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 14 defines ----------.................... #ifndef USB_EP14_TX_ENABLE.................... #define USB_EP14_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP14_RX_ENABLE.................... #define USB_EP14_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP14_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP14_RX_SIZE.................... #undef USB_EP14_RX_SIZE.................... #endif.................... #define USB_EP14_RX_SIZE 0.................... #else.................... #ifndef USB_EP14_RX_SIZE.................... #error You enabled EP14 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP14_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP14_TX_SIZE.................... #undef USB_EP14_TX_SIZE.................... #endif.................... #define USB_EP14_TX_SIZE 0.................... #else.................... #ifndef USB_EP14_TX_SIZE.................... #error You enabled EP14 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 15 defines ----------.................... #ifndef USB_EP15_TX_ENABLE.................... #define USB_EP15_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP15_RX_ENABLE.................... #define USB_EP15_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP15_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP15_RX_SIZE.................... #undef USB_EP15_RX_SIZE.................... #endif.................... #define USB_EP15_RX_SIZE 0.................... #else.................... #ifndef USB_EP15_RX_SIZE.................... #error You enabled EP15 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP15_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP15_TX_SIZE.................... #undef USB_EP15_TX_SIZE.................... #endif.................... #define USB_EP15_TX_SIZE 0.................... #else.................... #ifndef USB_EP15_TX_SIZE.................... #error You enabled EP15 for TX but didn't specify endpoint size.................... #endif.................... #endif........................................ const int8 usb_ep_tx_type[16]={.................... USB_EP0_TX_ENABLE, USB_EP1_TX_ENABLE, USB_EP2_TX_ENABLE,.................... USB_EP3_TX_ENABLE, USB_EP4_TX_ENABLE, USB_EP5_TX_ENABLE,.................... USB_EP6_TX_ENABLE, USB_EP7_TX_ENABLE, USB_EP8_TX_ENABLE,.................... USB_EP9_TX_ENABLE, USB_EP10_TX_ENABLE, USB_EP11_TX_ENABLE,.................... USB_EP12_TX_ENABLE, USB_EP13_TX_ENABLE, USB_EP14_TX_ENABLE,.................... USB_EP15_TX_ENABLE.................... };........................................ const int8 usb_ep_rx_type[16]={.................... USB_EP0_RX_ENABLE, USB_EP1_RX_ENABLE, USB_EP2_RX_ENABLE,.................... USB_EP3_RX_ENABLE, USB_EP4_RX_ENABLE, USB_EP5_RX_ENABLE,.................... USB_EP6_RX_ENABLE, USB_EP7_RX_ENABLE, USB_EP8_RX_ENABLE,.................... USB_EP9_RX_ENABLE, USB_EP10_RX_ENABLE, USB_EP11_RX_ENABLE,.................... USB_EP12_RX_ENABLE, USB_EP13_RX_ENABLE, USB_EP14_RX_ENABLE,.................... USB_EP15_RX_ENABLE.................... };........................................ const unsigned int16 usb_ep_tx_size[16]={.................... USB_EP0_TX_SIZE, USB_EP1_TX_SIZE, USB_EP2_TX_SIZE,.................... USB_EP3_TX_SIZE, USB_EP4_TX_SIZE, USB_EP5_TX_SIZE,.................... USB_EP6_TX_SIZE, USB_EP7_TX_SIZE, USB_EP8_TX_SIZE,.................... USB_EP9_TX_SIZE, USB_EP10_TX_SIZE, USB_EP11_TX_SIZE,.................... USB_EP12_TX_SIZE, USB_EP13_TX_SIZE, USB_EP14_TX_SIZE,.................... USB_EP15_TX_SIZE.................... };........................................ const unsigned int16 usb_ep_rx_size[16]={.................... USB_EP0_RX_SIZE, USB_EP1_RX_SIZE, USB_EP2_RX_SIZE,.................... USB_EP3_RX_SIZE, USB_EP4_RX_SIZE, USB_EP5_RX_SIZE,.................... USB_EP6_RX_SIZE, USB_EP7_RX_SIZE, USB_EP8_RX_SIZE,.................... USB_EP9_RX_SIZE, USB_EP10_RX_SIZE, USB_EP11_RX_SIZE,.................... USB_EP12_RX_SIZE, USB_EP13_RX_SIZE, USB_EP14_RX_SIZE,.................... USB_EP15_RX_SIZE.................... };........................................ #ENDIF............................................................ //////////////////////////////////////////////////////////////////.................... ///.................... /// start config descriptor.................... /// right now we only support one configuration descriptor..................... /// the config, interface, class, and endpoint goes into this array..................... ///.................... //////////////////////////////////////////////////////////////////........................................ #DEFINE USB_TOTAL_CONFIG_LEN 67 //config+interface+class+endpoint+endpoint (2 endpoints)........................................ const char USB_CONFIG_DESC[] = {.................... //IN ORDER TO COMPLY WITH WINDOWS HOSTS, THE ORDER OF THIS ARRAY MUST BE:.................... // config(s).................... // interface(s).................... // class(es).................... // endpoint(s)........................................ //config_descriptor for config index 1.................... USB_DESC_CONFIG_LEN, //length of descriptor size ==0.................... USB_DESC_CONFIG_TYPE, //constant CONFIGURATION (CONFIGURATION 0x02) ==1.................... USB_TOTAL_CONFIG_LEN,0, //size of all data returned for this config ==2,3.................... 2, //number of interfaces this device supports ==4.................... 0x01, //identifier for this configuration. (IF we had more than one configurations) ==5.................... 0x00, //index of string descriptor for this configuration ==6.................... #if USB_CONFIG_BUS_POWER.................... 0x80, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't), bits 0-4 unused and bit7=1 ==7.................... #else.................... 0xC0, //bit 6=1 if self powered, bit 5=1 if supports remote wakeup (we don't), bits 0-4 unused and bit7=1 ==7.................... #endif.................... USB_CONFIG_BUS_POWER/2, //maximum bus power required (maximum milliamperes/2) (0x32 = 100mA) ==8............................................................ //interface descriptor 0 (comm class interface).................... USB_DESC_INTERFACE_LEN, //length of descriptor =9.................... USB_DESC_INTERFACE_TYPE, //constant INTERFACE (INTERFACE 0x04) =10.................... 0x00, //number defining this interface (IF we had more than one interface) ==11.................... 0x00, //alternate setting ==12.................... 1, //number of endpoints ==13.................... 0x02, //class code, 02 = Comm Interface Class ==14.................... 0x02, //subclass code, 2 = Abstract ==15.................... 0x01, //protocol code, 1 = v.25ter ==16.................... 0x00, //index of string descriptor for interface ==17........................................ //class descriptor [functional header].................... 5, //length of descriptor ==18.................... 0x24, //dscriptor type (0x24 == ) ==19.................... 0, //sub type (0=functional header) ==20.................... 0x10,0x01, // ==21,22 //cdc version........................................ //class descriptor [acm header].................... 4, //length of descriptor ==23.................... 0x24, //dscriptor type (0x24 == ) ==24.................... 2, //sub type (2=ACM) ==25.................... 2, //capabilities ==26 //we support Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State......................................... //class descriptor [union header].................... 5, //length of descriptor ==27.................... 0x24, //dscriptor type (0x24 == ) ==28.................... 6, //sub type (6=union) ==29.................... 0, //master intf ==30 //The interface number of the Communication or Dat a Cl ass interface, designated as the masteror controlling interface for the union..................... 1, //save intf0 ==31 //Interface number of first slave or associated interface in the union. *........................................ //class descriptor [call mgmt header].................... 5, //length of descriptor ==32.................... 0x24, //dscriptor type (0x24 == ) ==33.................... 1, //sub type (1=call mgmt) ==34.................... 0, //capabilities ==35 //device does not handle call management itself.................... 1, //data interface ==36 //interface number of data class interface........................................ //endpoint descriptor.................... USB_DESC_ENDPOINT_LEN, //length of descriptor ==37.................... USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (ENDPOINT 0x05) ==38.................... USB_CDC_COMM_IN_ENDPOINT | 0x80, //endpoint number and direction.................... 0x03, //transfer type supported (0x03 is interrupt) ==40.................... USB_CDC_COMM_IN_SIZE,0x00, //maximum packet size supported ==41,42.................... 250, //polling interval, in ms. (interrupt endpoint cant be smaller than 10 for slow speed devices) ==43........................................ //interface descriptor 1 (data class interface).................... USB_DESC_INTERFACE_LEN, //length of descriptor =44.................... USB_DESC_INTERFACE_TYPE, //constant INTERFACE (INTERFACE 0x04) =45.................... 0x01, //number defining this interface (IF we had more than one interface) ==46.................... 0x00, //alternate setting ==47.................... 2, //number of endpoints ==48.................... 0x0A, //class code, 0A = Data Interface Class ==49.................... 0x00, //subclass code ==50.................... 0x00, //protocol code ==51.................... 0x00, //index of string descriptor for interface ==52........................................ //endpoint descriptor.................... USB_DESC_ENDPOINT_LEN, //length of descriptor ==53.................... USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (ENDPOINT 0x05) ==54.................... USB_CDC_DATA_OUT_ENDPOINT, //endpoint number and direction (0x02 = EP2 OUT) ==55.................... 0x02, //transfer type supported (0x02 is bulk) ==56.................... (USB_CDC_DATA_OUT_SIZE+1) & 0xFF, ((USB_CDC_DATA_OUT_SIZE+1) >> 8) & 0xFF, //maximum packet size supported ==57, 58.................... 1, //polling interval, in ms. ==59........................................ //endpoint descriptor.................... USB_DESC_ENDPOINT_LEN, //length of descriptor ==60.................... USB_DESC_ENDPOINT_TYPE, //constant ENDPOINT (ENDPOINT 0x05) ==61.................... USB_CDC_DATA_IN_ENDPOINT | 0x80, //endpoint number and direction (0x82 = EP2 IN) ==62.................... 0x02, //transfer type supported (0x02 is bulk) ==63.................... (USB_CDC_DATA_IN_SIZE+1) & 0xFF, ((USB_CDC_DATA_IN_SIZE+1) >> 8) & 0xFF, //maximum packet size supported ==66, 67.................... 1, //polling interval, in ms. ==68.................... };........................................ //****** BEGIN CONFIG DESCRIPTOR LOOKUP TABLES ********.................... //since we can't make pointers to constants in certain pic16s, this is an offset table to find.................... // a specific descriptor in the above table......................................... //the maximum number of interfaces seen on any config.................... //for example, if config 1 has 1 interface and config 2 has 2 interfaces you must define this as 2.................... #define USB_MAX_NUM_INTERFACES 2........................................ //define how many interfaces there are per config. [0] is the first config, etc..................... const char USB_NUM_INTERFACES[USB_NUM_CONFIGURATIONS]={2};........................................ //define where to find class descriptors.................... //first dimension is the config number.................... //second dimension specifies which interface.................... //last dimension specifies which class in this interface to get, but most will only have 1 class per interface.................... //if a class descriptor is not valid, set the value to 0xFFFF.................... const int8 USB_CLASS_DESCRIPTORS[USB_NUM_CONFIGURATIONS][USB_MAX_NUM_INTERFACES][4]=.................... {.................... //config 1.................... //interface 0.................... //class 1-4.................... 18,23,27,32,.................... //interface 1.................... //no classes for this interface.................... 0xFF,0xFF,0xFF,0xFF.................... };........................................ #if (sizeof(USB_CONFIG_DESC) != USB_TOTAL_CONFIG_LEN).................... #error USB_TOTAL_CONFIG_LEN not defined correctly.................... #endif............................................................ //////////////////////////////////////////////////////////////////.................... ///.................... /// start device descriptors.................... ///.................... //////////////////////////////////////////////////////////////////........................................ const char USB_DEVICE_DESC[USB_DESC_DEVICE_LEN] ={.................... //starts of with device configuration. only one possible.................... USB_DESC_DEVICE_LEN, //the length of this report ==0.................... 0x01, //the constant DEVICE (DEVICE 0x01) ==1.................... 0x10,0x01, //usb version in bcd ==2,3.................... 0x02, //class code. 0x02=Communication Device Class ==4.................... 0x00, //subclass code ==5.................... 0x00, //protocol code ==6.................... USB_MAX_EP0_PACKET_LENGTH, //max packet size for endpoint 0. (SLOW SPEED SPECIFIES 8) ==7.................... USB_CONFIG_VID & 0xFF, ((USB_CONFIG_VID >> 8) & 0xFF), //vendor id ==9, 10.................... USB_CONFIG_PID & 0xFF, ((USB_CONFIG_PID >> 8) & 0xFF), //product id, don't use 0xffff ==11, 12.................... USB_CONFIG_VERSION & 0xFF, ((USB_CONFIG_VERSION >> 8) & 0xFF), //device release number ==13,14.................... 0x01, //index of string description of manufacturer. therefore we point to string_1 array (see below) ==14.................... 0x02, //index of string descriptor of the product ==15.................... 0x00, //index of string descriptor of serial number ==16.................... USB_NUM_CONFIGURATIONS //number of possible configurations ==17.................... };............................................................ //////////////////////////////////////////////////////////////////.................... ///.................... /// start string descriptors.................... /// String 0 is a special language string, and must be defined. People in U.S.A. can leave this alone..................... ///.................... /// You must define the length else get_next_string_character() will not see the string.................... /// Current code only supports 10 strings (0 thru 9).................... ///.................... //////////////////////////////////////////////////////////////////........................................ #if !defined(USB_STRINGS_OVERWRITTEN).................... //the offset of the starting location of each string. offset[0] is the start of string 0, offset[1] is the start of string 1, etc..................... char USB_STRING_DESC_OFFSET[]={0,4,12};........................................ // Here is where the "CCS" Manufacturer string and "SERIAL DEMO" are stored..................... // Strings are saved as unicode..................... // These strings are mostly only displayed during the add hardware wizard..................... // Once the operating system drivers have been installed it will usually display.................... // the name from the drivers .INF..................... char const USB_STRING_DESC[]={.................... //string 0.................... 4, //length of string index.................... USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING).................... 0x09,0x04, //Microsoft Defined for US-English.................... //string 1 - manufacturer.................... 8, //length of string index.................... USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING).................... 'C',0,.................... 'C',0,.................... 'S',0,.................... //string 2 - product.................... 24, //length of string index.................... USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING).................... 'S',0,.................... 'E',0,.................... 'R',0,.................... 'I',0,.................... 'A',0,.................... 'L',0,.................... ' ',0,.................... 'D',0,.................... 'E',0,.................... 'M',0,.................... 'O',0.................... };.................... #endif //!defined(USB_STRINGS_OVERWRITTEN)........................................ #ENDIF........................................ #include <usb.c> //handles usb setup tokens and get descriptor reports.................... ///////////////////////////////////////////////////////////////////////////.................... //// usb.c ////.................... //// ////.................... //// Standard USB request and token handler code. ////.................... //// ////.................... //// This file is part of CCS's PIC USB driver code. See USB.H ////.................... //// for more documentation and a list of examples. ////.................... //// ////.................... //// The majority of this code is called and used by the interrupt ////.................... //// generated by the hardware level, and therefore it is not meant ////.................... //// to be called by the user. The functions that are meant to be ////.................... //// called by the user (init, put packet, get packet, etc) are ////.................... //// documented in USB.H. ////.................... //// ////.................... //// ************************* NOTE ************************** ////.................... //// This code will not create a multiple configuration device. ////.................... //// If you wish to create a multiple configuration device then you ////.................... //// will have to modify these drivers. ////.................... //// ////.................... //// ************************* NOTE ************************** ////.................... //// This code does not support Get_Idle or Set_Idle HID-specific ////.................... //// requests. These requests are optional. If you want to support ////.................... //// these requests you must provide the code yourself. See ////.................... //// usb_isr_tkn_setup_ClassInterface() if you wish to add this ////.................... //// support. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// If you wish to provide your own USB peripheral hardware layer, it ////.................... //// must must provide the API as described in usb_hw_layer.h. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// Dec 14, 2007: ////.................... //// usb_kbhit() moved to device driver. ////.................... //// ////.................... //// July 13th, 2005: ////.................... //// usb_puts() packet_size and this_packet_len changed to 16bits. ////.................... //// usb_gets() len, packet_size and this_packet_len to 16bits. ////.................... //// ////.................... //// June 20th, 2005: ////.................... //// Initial 18fxx5x release. ////.................... //// usb_kbhit() removed, usb_kbhit() now implemented in hardware ////.................... //// layer. ////.................... //// ////.................... //// May 13th, 2005: ////.................... //// Beta release, only works with 18Fxx5x hardware layer. ////.................... //// Now supports multiple interfaces (many defines in descriptors ////.................... //// will have to be changed, see examples) ////.................... //// TODO: alot of indexing and length handling for descriptors is ////.................... //// only 8bit, so make sure all descriptor tables are less than ////.................... //// 256 bytes long. ////.................... //// ////.................... //// Apr 21st, 2005: ////.................... //// Initial Alpha Release with PIC18Fxx5x support. (ONLY TESTED ////.................... //// WITH 18F4550) ////.................... //// usb_puts() doesn't need max packet size as a paremeter, uses ////.................... //// usb_ep_tx_size[] defined in usb.h ////.................... //// usb_puts() timeout parameter now in ms, not seconds. ////.................... //// USB Stack no longer buffers incoming data. If there is data to ////.................... //// get usb_kbhit(en) will return TRUE and the data will sit in ////.................... //// the endpoint buffer until you usb_get_packet(), which will ////.................... //// then free the endpoint buffer for more data. This affects ////.................... //// routines such as usb_gets() and usb_kbhit(). ////.................... //// usb_gets() no longer reads buffered data (see above note), ////.................... //// now it reads multiple packets in the same way usb_puts() ////.................... //// writes multiple packets ////.................... //// usb_kbhit() is hardware specific, so has been moved to hardware ////.................... //// layer. ////.................... //// ////.................... //// Nov 11th, 2004: ////.................... //// No longer includes wrong descriptor header. ////.................... //// ////.................... //// June 24th, 2004: ////.................... //// Optimization and cleanup. ////.................... //// The following definitions changed: ////.................... //// USB_EPx_TX_ENABLE and USB_EPx_RX_ENABLE have changed. See usb.h ////.................... //// USB_CONFIG_DESCRIPTORS[] removed ////.................... //// USB_CONFIG_DESC_LEN changed to USB_DESC_CONFIG_LEN ////.................... //// USB_INTERFACE_DESC_LEN changed to USB_DESC_INTERFACE_LEN ////.................... //// USB_CLASS_DESC_LEN changed to USB_DESC_CLASS_LEN ////.................... //// USB_ENDPOINT_DESC_LEN changed to USB_DESC_ENDPOINT_LEN ////.................... //// USB_CONFIG_DESC_KEY changed to USB_DESC_CONFIG_TYPE ////.................... //// USB_INTERFACE_DESC_KEY changed to USB_DESC_INTERFACE_TYPE ////.................... //// USB_CLASS_DESC_KEY changed to USB_DESC_CLASS_TYPE ////.................... //// USB_ENDPOINT_DESC_KEY changed to USB_DESC_ENDPOINT_TYPE ////.................... //// USB_STRING_X[] arrays removed, see USB_STRING_DESC[] and ////.................... //// USB_STRING_DESC_OFFSET[] ////.................... //// dev_req, curr_config, status_device and getdesc_type global ////.................... //// variables moved into struct USB_stack_status ////.................... //// ////.................... //// December 5th, 2003: Fixed a potential bug where descriptors are ////.................... //// evenly dividable by 8 (MAX_EP0_PACKET_SIZE) ////.................... //// ////.................... //// October 15th, 2003: Support for boot protocol added. ////.................... //// Set USB_HID_BOOT_PROTOCOL to TRUE to support this. ////.................... //// The array hid_protocol[] saves which protocol mode each ////.................... //// interface is in. It is your applications job to send ////.................... //// data that either fit the boot protocol or HID protocol. ////.................... //// ////.................... //// May 6th, 2003: Fixed a potential stack overflow using PCM ////.................... //// ////.................... //// October 28th, 2002: Problem with usb_puts and timeout fixed. ////.................... //// ////.................... //// October 28th, 2002: Typo fixed in get_next_string_character(), ////.................... //// although it didn't cause any serious problems ////.................... //// ////.................... //// October 25th, 2002: Another change to usb_puts() was made to fix ////.................... //// problems with multiple packet messages ////.................... //// October 29th, 2002: Fixed a problem with multiple packet string ////.................... //// descriptors that require a 0 len packet to ////.................... //// end message. ////.................... //// ////.................... //// October 23rd, 2002: usb_puts() will bomb out of error quicker ////.................... //// ////.................... //// August 2nd, 2002: Initial Public Release ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2005 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... ///////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __USB_DRIVER__.................... #DEFINE __USB_DRIVER__........................................ #include <usb.h>.................... ///////////////////////////////////////////////////////////////////////////.................... //// usb.h ////.................... //// ////.................... //// Function protypes, defintions and globals used by CCS USB driver ////.................... //// ////.................... //// This file is part of CCS's USB driver code ////.................... //// ////.................... //// The following USB examples are provided by CCS: ////.................... //// ex_usb_mouse.c - A HID Mouse. ////.................... //// ex_usb_hid.c - A custom application using HID protocol. ////.................... //// ex_usb_kbmouse.c - A HID Mouse/Keyboard combo using multiple ////.................... //// interfaces. ////.................... //// ex_usb_kbmouse2.c - A HID Mouse/Keyboard combo using multiple ////.................... //// HID Reports. ////.................... //// ex_usb_scope.c - A digital oscilloscope using a custom ////.................... //// protocol requiring custom Windows drivers. ////.................... //// ex_usb_serial.c - ////.................... //// ex_usb_serial2.c - Two examples of using the CDC driver for ////.................... //// a virtual COM port. ////.................... //// ex_usb_bootloader.c - CDC/Virtual COM port bootloader. ////.................... //// ex_usb_loadmouse.c - A conversion of ex_usb_mouse.c that is ////.................... //// compatible with ex_usb_mouse.c. ////.................... //// ////.................... //// ********************** API ********************* ////.................... //// ////.................... //// These are the functions that are meant to be called by the user: ////.................... //// ////.................... //// usb_init() - Initializes the USB stack, the USB peripheral and ////.................... //// attaches the unit to the usb bus. Enables ////.................... //// interrupts. Will wait in an infinite loop until ////.................... //// the device enumerates - if you are using ////.................... //// connection sense or if the processor should run ////.................... //// even if it's not connected to USB then use ////.................... //// usb_init_cs() instead. ////.................... //// ////.................... //// usb_init_cs() - A smaller usb_init(), does not attach unit ////.................... //// to usb bus or enable interrupts. Since this does ////.................... //// not attach to the USB, you must periodically call ////.................... //// usb_task(). See usb_task() for more information. ////.................... //// ////.................... //// usb_task() - If usb_init_cs() was used to initiate the USB ////.................... //// peripheral, usb_task() should then be called periodically ////.................... //// to check the connection sense pin. If the connection ////.................... //// sense pin denotes USB is connected and the USB peripheral ////.................... //// is not attached, this will attach the USB peripheral ////.................... //// so the PC can start the enumeration process (and it ////.................... //// will enable interrupts). If the connection sense pin ////.................... //// denotes USB is not attached and the USB peripheral is ////.................... //// running, this will reset the USB peripheral and wait ////.................... //// for USB to reconnect (and usb_enumerated() will start ////.................... //// returning FALSE). If connection sense (USB_CON_SENSE_PIN) ////.................... //// is not defined the usb_task() assumes that USB is always ////.................... //// connected. ////.................... //// ////.................... //// usb_attached() - Returns TRUE if the device is attached to a ////.................... //// USB cable. A macro that looks at the defined ////.................... //// connection sense pin. If this returns TRUE ////.................... //// it does not mean the PC has connected to it, ////.................... //// you need to use usb_enumerated() to check this. ////.................... //// ////.................... //// usb_enumerated() - Returns TRUE if device has been enumerated ////.................... //// (configured) by host, FALSE if it has not. ////.................... //// Do not try to use the USB peripheral for ////.................... //// sending and receiving packets until you ////.................... //// are enumerated. ////.................... //// ////.................... //// usb_wait_for_enumeration() - Sits in an infinte loop until device ////.................... //// is enumerated. ////.................... //// ////.................... //// usb_tbe(endpoint) - Returns TRUE if the endpoint transmit buffer ////.................... //// is free and ready to accept a new packet for transmission. ////.................... //// ////.................... //// usb_put_packet(endpoint, ptr, len, tgl) - Sends one packet to the ////.................... //// host. If you need to send a message that ////.................... //// spans more than one packet then use ////.................... //// usb_puts(). Fore more detailed documentation ////.................... //// see usb_hw_layer.h ////.................... //// ////.................... //// usb_puts(endpoint, ptr, len, timeout) - Sends a multiple packet ////.................... //// message to the host. If you only need to send one packet, ////.................... //// it is more effecient to use usb_put_packet(). This is ////.................... //// documented in more detail above the prototype in USB.H. ////.................... //// ////.................... //// usb_kbhit(endpoint) - Returns true if OUT endpoint contains data ////.................... //// from host. This will remain TRUE until ////.................... //// usb_put_packet() or usb_flush_out() used. ////.................... //// This function will return an invalid response ////.................... //// if specified endpoint is not enabled for ////.................... //// receiving data. ////.................... //// ////.................... //// len = usb_get_packet(endpoint, ptr, max) - Gets one packet that ////.................... //// from the host/PC. usb_kbhit() must return TRUE before you ////.................... //// call this routine or your data may not be valid. This ////.................... //// only receives one packet, if you are trying to receive a ////.................... //// multi-packet message use usb_gets(). For more detailed ////.................... //// documentation see usb_hw_layer.h. ////.................... //// ////.................... //// len = usb_gets(endpoint, ptr, max, timeout) - Gets multiple ////.................... //// packets from the host, you would use this instead ////.................... //// of usb_get_packet() if you wanted to handle multi-packet ////.................... //// messages. This is documented in more detail above the ////.................... //// prototype in USB.H. ////.................... //// ////.................... //// ////.................... //// ********* DEFINITIONS / CONFIGURATION ********** ////.................... //// ////.................... //// The following definitions are declared here, but can be ////.................... //// overwritten in your code. Unless needed otherwise, leave ////.................... //// to default value. If confused about a definition read the ////.................... //// comments at each defintion ////.................... //// ////.................... //// USB_HID_IDLE (TRUE) - Set to TRUE if your device supports ////.................... //// Set_Idle HID class request. Set to ////.................... //// False if you do not (device will ////.................... //// send a Wrong-state if computer ////.................... //// sends a Set_Idle / Get_Idle command) ////.................... //// NOTE: If you set to TRUE you must ////.................... //// provide your own code. See ////.................... //// usb_isr_tkn_setup_ClassInterface() in ////.................... //// usb.c ////.................... //// ////.................... //// USB_HID_BOOT_PROTOCOL (FALSE) - Set to TRUE if your device supports ////.................... //// Set_Protocl HID class request. Set to ////.................... //// False if you do not (device will ////.................... //// send a Wrong-state if computer ////.................... //// sends a Set_Protocl / Get_Protocol ////.................... //// command). ////.................... //// NOTE: If you set to TRUE you must ////.................... //// provide your own code in the ////.................... //// application that properly send boot ////.................... //// or HID packets. ////.................... //// ////.................... //// USB_MAX_EP0_PACKET_LENGTH (8) - Max Packet size for Endpoint 0. ////.................... //// The hardware level driver (ex ////.................... //// pic18_usb.h will define this value if ////.................... //// not already overwritten). Increasing ////.................... //// this size will speed up the ////.................... //// enumeration process. ////.................... //// ////.................... //// USB_EPx_RX_ENABLE (USB_ENABLE_DISABLED) - Where x is the ////.................... //// endpoint number. Change this define ////.................... //// to specify what kind of transfer method ////.................... //// this RX (PC to device) endpoint uses. ////.................... //// Here is the list of valid transfer methods: ////.................... //// USB_ENABLE_CONTROL ////.................... //// USB_ENABLE_ISOCHRONOUS ////.................... //// USB_ENABLE_BULK ////.................... //// USB_ENABLE_INTERRUPT ////.................... //// USB_ENABLE_DISABLED ////.................... //// Don't forget that you must specify the ////.................... //// transfer method properly in your endpoint ////.................... //// descriptor, too. ////.................... //// ////.................... //// USB_EPx_RX_SIZE (0) - For some hardware it is important to know ////.................... //// how much memory to reserve for receiving ////.................... //// packets. ////.................... //// ////.................... //// *** NOTE *** You must have both USB_EPx_RX_ENABLE defined and ////.................... //// USB_EPx_RX_SIZE defined (non 0) to enable a RX ////.................... //// endpoint. ////.................... //// *** NOTE *** Endpoint 0 is always enabled. ////.................... //// ////.................... //// USB_EPx_TX_ENABLE (USB_ENABLE_DISABLED) - Where x is the ////.................... //// endpoint number. Change this define ////.................... //// to specify what kind of transfer method ////.................... //// this TX (device to PC) endpoint uses. ////.................... //// Here is the list of valid transfer methods: ////.................... //// USB_ENABLE_CONTROL ////.................... //// USB_ENABLE_ISOCHRONOUS ////.................... //// USB_ENABLE_BULK ////.................... //// USB_ENABLE_INTERRUPT ////.................... //// USB_ENABLE_DISABLED ////.................... //// Don't forget that you must specify the ////.................... //// transfer method properly in your endpoint ////.................... //// descriptor, too. ////.................... //// ////.................... //// USB_EPx_TX_SIZE (0) - For some hardware it is important to know ////.................... //// how much memory to reserve for transmitting ////.................... //// packets. ////.................... //// ////.................... //// *** NOTE *** You must have both USB_EPx_TX_ENABLE defined and ////.................... //// USB_EPx_TX_SIZE defined (non 0) to enable a TX ////.................... //// endpoint. ////.................... //// ////.................... //// ////.................... //// USB_HID_DEVICE (TRUE) - HID devices require extra code to handle ////.................... //// HID requests. You can disable to save ////.................... //// ROM space if you are not using a HID ////.................... //// device. If you are not using a HID ////.................... //// device you must provide your own O/S ////.................... //// (Windows) driver. ////.................... //// ////.................... //// The other definitions should not be changed. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// July 13th, 2005: ////.................... //// usb_ep_tx_size[] and usb_ep_rx_size[] changed to 16bits ////.................... //// ////.................... //// June 20th, 2005: ////.................... //// Initial 18fxx5x release ////.................... //// ////.................... //// May 13th, 2005: ////.................... //// Beta release, only works with 18Fxx5x hardware layer. ////.................... //// Now supports multiple interfaces (many defines in descriptors ////.................... //// will have to be changed, see examples) ////.................... //// ////.................... //// Mar 21st, 2005: ////.................... //// Initial Alpha Release with PIC18Fxx5x support. (ONLY TESTED ////.................... //// WITH 18F4550) ////.................... //// usb_gets() and usb_puts() changed (see usb.c) ////.................... //// ////.................... //// June 24th, 2004: ////.................... //// Optimization and cleanup. ////.................... //// The following definitions changed: ////.................... //// USB_EPx_TX_ENABLE and USB_EPx_RX_ENABLE have changed. See usb.h ////.................... //// USB_CONFIG_DESCRIPTORS[] removed ////.................... //// USB_CONFIG_DESC_LEN changed to USB_DESC_CONFIG_LEN ////.................... //// USB_INTERFACE_DESC_LEN changed to USB_DESC_INTERFACE_LEN ////.................... //// USB_CLASS_DESC_LEN changed to USB_DESC_CLASS_LEN ////.................... //// USB_ENDPOINT_DESC_LEN changed to USB_DESC_ENDPOINT_LEN ////.................... //// USB_CONFIG_DESC_KEY changed to USB_DESC_CONFIG_TYPE ////.................... //// USB_INTERFACE_DESC_KEY changed to USB_DESC_INTERFACE_TYPE ////.................... //// USB_CLASS_DESC_KEY changed to USB_DESC_CLASS_TYPE ////.................... //// USB_ENDPOINT_DESC_KEY changed to USB_DESC_ENDPOINT_TYPE ////.................... //// USB_STRING_X[] arrays removed, see USB_STRING_DESC[] and ////.................... //// USB_STRING_DESC_OFFSET[] ////.................... //// dev_req, curr_config, status_device and getdesc_type global ////.................... //// variables moved into struct USB_stack_status ////.................... //// ////.................... //// October 15th, 2003: Support for boot protocol added. ////.................... //// Set USB_HID_BOOT_PROTOCOL to TRUE to support this. ////.................... //// The array hid_protocol[] saves which protocol mode each ////.................... //// interface is in. It is your applications job to send ////.................... //// data that either fit the boot protocol or HID protocol. ////.................... //// ////.................... //// May 6th, 2003: Fixed a potential stack overflow using PCM ////.................... //// ////.................... //// October 29th, 2002: New definition added to USB_STATES ////.................... //// ////.................... //// August 2nd, 2002: Initial Public Release ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2009 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... ///////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __USB_PROTOTYPES__.................... #DEFINE __USB_PROTOTYPES__........................................ //// CONFIGURATION ////////////////////////////////////////////////////////////........................................ #ifndef USB_CON_SENSE_PIN.................... #define USB_CON_SENSE_PIN 0.................... #endif........................................ #IFNDEF USB_HID_BOOT_PROTOCOL.................... #DEFINE USB_HID_BOOT_PROTOCOL FALSE.................... #ENDIF........................................ #IFNDEF USB_HID_IDLE.................... #DEFINE USB_HID_IDLE FALSE.................... #ENDIF........................................ //should the compiler add the extra HID handler code? Defaults to yes..................... #IFNDEF USB_HID_DEVICE.................... #DEFINE USB_HID_DEVICE TRUE.................... #ENDIF........................................ #IFNDEF USB_CDC_DEVICE.................... #DEFINE USB_CDC_DEVICE FALSE.................... #ENDIF........................................ //set to false to opt for less RAM, true to opt for less ROM.................... #ifndef USB_OPT_FOR_ROM.................... #define USB_OPT_FOR_ROM TRUE.................... #endif........................................ #IFNDEF USB_MAX_EP0_PACKET_LENGTH.................... #DEFINE USB_MAX_EP0_PACKET_LENGTH 8.................... #ENDIF............................................................ ////// USER-LEVEL API /////////////////////////////////////////////////////////........................................ /**************************************************************.................... /* usb_enumerated().................... /*.................... /* Input: Global variable USB_Curr_Config.................... /* Returns: Returns a 1 if device is configured / enumerated,.................... /* Returns a 0 if device is un-configured / not enumerated..................... /*.................... /* Summary: See API section of USB.H for more documentation..................... /***************************************************************/.................... int1 usb_enumerated(void);........................................ /**************************************************************.................... /* usb_wait_for_enumeration().................... /*.................... /* Input: Global variable USB_Curr_Config.................... /*.................... /* Summary: Waits in-definately until device is configured / enumerated..................... /* See API section of USB.H for more information..................... /***************************************************************/.................... void usb_wait_for_enumeration(void);........................................ /****************************************************************************.................... /* usb_gets(endpoint, ptr, max, timeout).................... /*.................... /* Input: endpoint - endpoint to get data from.................... /* ptr - place / array to store data to.................... /* max - max amount of data to get from USB and store into ptr.................... /* timeout - time in milliseconds, for each packet, to wait before.................... /* timeout. set to 0 for no timeout..................... /*.................... /* Output: Amount of data returned. It may be less than max..................... /*.................... /* Summary: Gets data from the host. Will get multiple-packet messages.................... /* and finish when either it receives a 0-len packet or a packet.................... /* of less size than maximum..................... /*.................... /*****************************************************************************/.................... unsigned int16 usb_gets(int8 endpoint, int8 * ptr, unsigned int16 max, unsigned int16 timeout);........................................ /****************************************************************************.................... /* usb_puts().................... /*.................... /* Inputs: endpoint - endpoint to send data out.................... /* ptr - points to array of data to send.................... /* len - amount of data to send.................... /* timeout - time in milli-seconds, for each packet, to wait before.................... /* timeout. set to 0 for no timeout..................... /*.................... /* Outputs: Returns TRUE if message sent succesfully, FALSE if it was not.................... /* sent before timeout period expired..................... /*.................... /* Summary: Used for sending multiple packets of data as one message. This.................... /* function can still be used to send messages consiting of only one.................... /* packet. See usb_put_packet() documentation for the rules about when.................... /* multiple packet messages or 0-lenght packets are needed..................... /*.................... /*****************************************************************************/.................... int1 usb_puts(int8 endpoint, int8 * ptr, unsigned int16 len, unsigned int8 timeout);........................................ /******************************************************************************.................... /* usb_attached().................... /*.................... /* Summary: Returns TRUE if the device is attached to a USB cable..................... /* See the API section of USB.H for more documentation..................... /*.................... /*****************************************************************************/.................... #if USB_CON_SENSE_PIN.................... #define usb_attached() input(USB_CON_SENSE_PIN).................... #else.................... #define usb_attached() TRUE.................... #endif........................................ ////// END USER-LEVEL API /////////////////////////////////////////////////////............................................................ ////// STACK-LEVEL API USED BY HW DRIVERS ////////////////////////////////////........................................ enum USB_STATES {GET_DESCRIPTOR=1,SET_ADDRESS=2,NONE=0};........................................ enum USB_GETDESC_TYPES {USB_GETDESC_CONFIG_TYPE=0,USB_GETDESC_HIDREPORT_TYPE=1,USB_GETDESC_STRING_TYPE=2,USB_GETDESC_DEVICE_TYPE=3};........................................ #if USB_OPT_FOR_ROM.................... typedef struct {.................... USB_STATES dev_req; //what did the last setup token set us up to do?. init at none.................... int curr_config; //our current config. start at none/powered (NOT THAT THIS LIMITS US TO 3 CONFIGURATIONS).................... int status_device; //Holds our state for Set_Feature and Clear_Feature.................... USB_GETDESC_TYPES getdesc_type; //which get_descriptor() we are handling.................... } TYPE_USB_STACK_STATUS;.................... #else.................... typedef struct {.................... USB_STATES dev_req:2; //what did the last setup token set us up to do?. init at none.................... int Curr_config:2; //our current config. start at none/powered (NOT THAT THIS LIMITS US TO 3 CONFIGURATIONS).................... int status_device:2; //Holds our state for Set_Feature and Clear_Feature.................... USB_GETDESC_TYPES getdesc_type:2; //which get_descriptor() we are handling.................... } TYPE_USB_STACK_STATUS;.................... #endif........................................ extern TYPE_USB_STACK_STATUS USB_stack_status;........................................ /**************************************************************.................... /* usb_token_reset().................... /*.................... /* Output: No output (but many global registers are modified).................... /*.................... /* Summary: Resets the token handler to initial (unconfigured) state..................... /***************************************************************/.................... void usb_token_reset(void);........................................ /**************************************************************.................... /* usb_isr_tok_setup_dne().................... /*.................... /* Input: usb_ep0_rx_buffer[] contains the the setup packet..................... /*.................... /* Output: None (many globals are changed).................... /*.................... /* Summary: This function is that handles the setup token..................... /* We must handle all relevant requests, such as Set_Configuration,.................... /* Get_Descriptor, etc..................... /*.................... /* usb_ep0_rx_buffer[] contains setup data packet, which has the.................... /* following records:.................... /* -------------------------------------------------------------------------------------------.................... /* usb_ep0_rx_buffer[ 0 ]=bmRequestType; Where the setup packet goes.................... /* bit7 (0) host-to-device.................... /* (1) device-to-host.................... /* bit6-5 (00) usb standard request;.................... /* (01) class request;.................... /* (10) vendor request.................... /* (11) reserved.................... /* bit4-0 (0000) device.................... /* (0001) interface.................... /* (0010) endpoint.................... /* (0011) other element.................... /* (0100) to (1111) reserved.................... /* usb_ep0_rx_buffer[ 1 ]=bRequest ; the request.................... /* usb_ep0_rx_buffer[2,3]=wValue ; a value which corresponds to request.................... /* usb_ep0_rx_buffer[4,5]=wIndex ; could correspond to interface or endpoint....................... /* usb_ep0_rx_buffer[6,7]=wLength ; number of bytes in next data packet;.................... /* for host-to-device, this exactly how many bytes in data packet..................... /* for device-to-host, this is the maximum bytes that can fit one packet..................... /***************************************************************/.................... void usb_isr_tok_setup_dne(void);........................................ /**************************************************************.................... /* usb_isr_tok_out_dne().................... /*.................... /* Input: endpoint contains which endpoint we are receiving data (0..15).................... /*.................... /* Summary: Processes out tokens (out is respective of the host, so actualy.................... /* incoming to the pic), but not out setup tokens. Normally when.................... /* data is received it is left in the buffer (user would use.................... /* usb_kbhit() and usb_get_packet() to receive data), but certain.................... /* libraries (like CDC) have to answer setup packets..................... /*.................... /***************************************************************/.................... void usb_isr_tok_out_dne(int8 endpoint);........................................ /**************************************************************.................... /* usb_isr_tok_in_dne(endpoint).................... /*.................... /* Input: endpoint - which endpoint we are processing a setup token..................... /*.................... /* Summary: This handles an IN packet (HOST <- PIC). For endpoint 0, this.................... /* is usually to setup a response packet to a setup packet. Endpoints 1..15.................... /* are generally ignored, and the user has to use usb_tbe() to determine if.................... /* if the buffer is ready for a new transmit packet (there are special cases,.................... /* like CDC which handles the CDC protocl)..................... /*.................... /***************************************************************/.................... void usb_isr_tok_in_dne(int8 endpoint);........................................ ////// END STACK-LEVEL API USED BY HW DRIVERS /////////////////////////////////............................................................ //CCS only supports one configuration at this time.................... #DEFINE USB_NUM_CONFIGURATIONS 1 //DO NOT CHANGE........................................ //PID values for tokens (see page 48 of USB Complete ed.1).................... #define PID_IN 0x09 //device to host transactions.................... #define PID_OUT 0x01 //host to device transactions.................... #define PID_SETUP 0x0D //host to device setup transaction.................... #define PID_ACK 0x02 //receiver accepts error-free data packet.................... #define PID_DATA0 0x03 //data packet with even sync bit.................... #define PID_SOF 0x05 //start of framer marker and frame number.................... #define PID_NAK 0x0A //receiver can't accept data or sender cant send data or has no data to transmit.................... #define PID_DATA1 0x0B //data packet with odd sync bit.................... #define PID_PRE 0x0C //preamble issued by host. enables downstream traffic to low-speed device.................... #define PID_STALL 0x0E //a control request isnt supported or the endpoint is halted........................................ //Key which identifies descritpors.................... #DEFINE USB_DESC_DEVICE_TYPE 0x01 //#DEFINE USB_DEVICE_DESC_KEY 0x01.................... #DEFINE USB_DESC_CONFIG_TYPE 0x02 //#DEFINE USB_CONFIG_DESC_KEY 0x02.................... #DEFINE USB_DESC_STRING_TYPE 0x03 //#DEFINE USB_STRING_DESC_KEY 0x03.................... #DEFINE USB_DESC_INTERFACE_TYPE 0x04 //#DEFINE USB_INTERFACE_DESC_KEY 0x04.................... #DEFINE USB_DESC_ENDPOINT_TYPE 0x05 //#DEFINE USB_ENDPOINT_DESC_KEY 0x05.................... #DEFINE USB_DESC_CLASS_TYPE 0x21 //#DEFINE USB_CLASS_DESC_KEY 0x21.................... #DEFINE USB_DESC_HIDREPORT_TYPE 0x22........................................ //The length of each descriptor.................... #DEFINE USB_DESC_DEVICE_LEN 18 //#DEFINE USB_DEVICE_DESC_LEN 18.................... #DEFINE USB_DESC_CONFIG_LEN 9 //#DEFINE USB_CONFIG_DESC_LEN 9.................... #DEFINE USB_DESC_INTERFACE_LEN 9 //#DEFINE USB_INTERFACE_DESC_LEN 9.................... #DEFINE USB_DESC_CLASS_LEN 9 //#DEFINE USB_CLASS_DESC_LEN 9.................... #DEFINE USB_DESC_ENDPOINT_LEN 7 //#DEFINE USB_ENDPOINT_DESC_LEN 7........................................ //Standard USB Setup bRequest Codes.................... #define USB_STANDARD_REQUEST_GET_STATUS 0x00.................... #define USB_STANDARD_REQUEST_CLEAR_FEATURE 0x01.................... #define USB_STANDARD_REQUEST_SET_FEATURE 0x03.................... #define USB_STANDARD_REQUEST_SET_ADDRESS 0x05.................... #define USB_STANDARD_REQUEST_GET_DESCRIPTOR 0x06.................... #define USB_STANDARD_REQUEST_SET_DESCRIPTOR 0x07.................... #define USB_STANDARD_REQUEST_GET_CONFIGURATION 0x08.................... #define USB_STANDARD_REQUEST_SET_CONFIGURATION 0x09.................... #define USB_STANDARD_REQUEST_GET_INTERFACE 0x0A.................... #define USB_STANDARD_REQUEST_SET_INTERFACE 0x0B.................... #define USB_STANDARD_REQUEST_SYNCH_FRAME 0x0C........................................ //HID Class Setup bRequest Codes.................... #define USB_HID_REQUEST_GET_REPORT 0x01.................... #define USB_HID_REQUEST_GET_IDLE 0x02.................... #define USB_HID_REQUEST_GET_PROTOCOL 0x03.................... #define USB_HID_REQUEST_SET_REPORT 0x09.................... #define USB_HID_REQUEST_SET_IDLE 0x0A.................... #define USB_HID_REQUEST_SET_PROTOCOL 0x0B........................................ //types of endpoints as defined in the descriptor.................... #define USB_ENDPOINT_TYPE_CONTROL 0x00.................... #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01.................... #define USB_ENDPOINT_TYPE_BULK 0x02.................... #define USB_ENDPOINT_TYPE_INTERRUPT 0x03........................................ //types of endpoints used internally in this api.................... #define USB_ENABLE_DISABLED -1.................... #define USB_ENABLE_BULK USB_ENDPOINT_TYPE_BULK.................... #define USB_ENABLE_ISOCHRONOUS USB_ENDPOINT_TYPE_ISOCHRONOUS.................... #define USB_ENABLE_INTERRUPT USB_ENDPOINT_TYPE_INTERRUPT.................... #define USB_ENABLE_CONTROL USB_ENDPOINT_TYPE_CONTROL............................................................ //*** ENABLE RX ENDPOINTS AND BUFFERS........................................ //--------- endpoint 0 defines ----------.................... #define USB_EP0_TX_ENABLE USB_ENABLE_CONTROL.................... #define USB_EP0_RX_ENABLE USB_ENABLE_CONTROL.................... #define USB_EP0_RX_SIZE USB_MAX_EP0_PACKET_LENGTH //endpoint 0 is setup, and should always be the MAX_PACKET_LENGTH. Slow speed specifies 8.................... #define USB_EP0_TX_SIZE USB_MAX_EP0_PACKET_LENGTH //endpoint 0 is setup, and should always be the MAX_PACKET_LENGTH. Slow speed specifies 8........................................ //--------- endpoint 1 defines ----------.................... #ifndef USB_EP1_TX_ENABLE.................... #define USB_EP1_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP1_RX_ENABLE.................... #define USB_EP1_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP1_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP1_RX_SIZE.................... #undef USB_EP1_RX_SIZE.................... #endif.................... #define USB_EP1_RX_SIZE 0.................... #else.................... #ifndef USB_EP1_RX_SIZE.................... #error You enabled EP1 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP1_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP1_TX_SIZE.................... #undef USB_EP1_TX_SIZE.................... #endif.................... #define USB_EP1_TX_SIZE 0.................... #else.................... #ifndef USB_EP1_TX_SIZE.................... #error You enabled EP1 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 2 defines ----------.................... #ifndef USB_EP2_TX_ENABLE.................... #define USB_EP2_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP2_RX_ENABLE.................... #define USB_EP2_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP2_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP2_RX_SIZE.................... #undef USB_EP2_RX_SIZE.................... #endif.................... #define USB_EP2_RX_SIZE 0.................... #else.................... #ifndef USB_EP2_RX_SIZE.................... #error You enabled EP2 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP2_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP2_TX_SIZE.................... #undef USB_EP2_TX_SIZE.................... #endif.................... #define USB_EP2_TX_SIZE 0.................... #else.................... #ifndef USB_EP2_TX_SIZE.................... #error You enabled EP2 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 3 defines ----------.................... #ifndef USB_EP3_TX_ENABLE.................... #define USB_EP3_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP3_RX_ENABLE.................... #define USB_EP3_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP3_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP3_RX_SIZE.................... #undef USB_EP3_RX_SIZE.................... #endif.................... #define USB_EP3_RX_SIZE 0.................... #else.................... #ifndef USB_EP3_RX_SIZE.................... #error You enabled EP3 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP3_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP3_TX_SIZE.................... #undef USB_EP3_TX_SIZE.................... #endif.................... #define USB_EP3_TX_SIZE 0.................... #else.................... #ifndef USB_EP3_TX_SIZE.................... #error You enabled EP3 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 4 defines ----------.................... #ifndef USB_EP4_TX_ENABLE.................... #define USB_EP4_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP4_RX_ENABLE.................... #define USB_EP4_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP4_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP4_RX_SIZE.................... #undef USB_EP4_RX_SIZE.................... #endif.................... #define USB_EP4_RX_SIZE 0.................... #else.................... #ifndef USB_EP4_RX_SIZE.................... #error You enabled EP4 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP4_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP4_TX_SIZE.................... #undef USB_EP4_TX_SIZE.................... #endif.................... #define USB_EP4_TX_SIZE 0.................... #else.................... #ifndef USB_EP4_TX_SIZE.................... #error You enabled EP4 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 5 defines ----------.................... #ifndef USB_EP5_TX_ENABLE.................... #define USB_EP5_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP5_RX_ENABLE.................... #define USB_EP5_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP5_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP5_RX_SIZE.................... #undef USB_EP5_RX_SIZE.................... #endif.................... #define USB_EP5_RX_SIZE 0.................... #else.................... #ifndef USB_EP5_RX_SIZE.................... #error You enabled EP5 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP5_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP5_TX_SIZE.................... #undef USB_EP5_TX_SIZE.................... #endif.................... #define USB_EP5_TX_SIZE 0.................... #else.................... #ifndef USB_EP5_TX_SIZE.................... #error You enabled EP5 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 6 defines ----------.................... #ifndef USB_EP6_TX_ENABLE.................... #define USB_EP6_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP6_RX_ENABLE.................... #define USB_EP6_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP6_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP6_RX_SIZE.................... #undef USB_EP6_RX_SIZE.................... #endif.................... #define USB_EP6_RX_SIZE 0.................... #else.................... #ifndef USB_EP6_RX_SIZE.................... #error You enabled EP6 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP6_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP6_TX_SIZE.................... #undef USB_EP6_TX_SIZE.................... #endif.................... #define USB_EP6_TX_SIZE 0.................... #else.................... #ifndef USB_EP6_TX_SIZE.................... #error You enabled EP6 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 7 defines ----------.................... #ifndef USB_EP7_TX_ENABLE.................... #define USB_EP7_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP7_RX_ENABLE.................... #define USB_EP7_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP7_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP7_RX_SIZE.................... #undef USB_EP7_RX_SIZE.................... #endif.................... #define USB_EP7_RX_SIZE 0.................... #else.................... #ifndef USB_EP7_RX_SIZE.................... #error You enabled EP7 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP7_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP7_TX_SIZE.................... #undef USB_EP7_TX_SIZE.................... #endif.................... #define USB_EP7_TX_SIZE 0.................... #else.................... #ifndef USB_EP7_TX_SIZE.................... #error You enabled EP7 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 8 defines ----------.................... #ifndef USB_EP8_TX_ENABLE.................... #define USB_EP8_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP8_RX_ENABLE.................... #define USB_EP8_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP8_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP8_RX_SIZE.................... #undef USB_EP8_RX_SIZE.................... #endif.................... #define USB_EP8_RX_SIZE 0.................... #else.................... #ifndef USB_EP8_RX_SIZE.................... #error You enabled EP8 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP8_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP8_TX_SIZE.................... #undef USB_EP8_TX_SIZE.................... #endif.................... #define USB_EP8_TX_SIZE 0.................... #else.................... #ifndef USB_EP8_TX_SIZE.................... #error You enabled EP8 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 9 defines ----------.................... #ifndef USB_EP9_TX_ENABLE.................... #define USB_EP9_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP9_RX_ENABLE.................... #define USB_EP9_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP9_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP9_RX_SIZE.................... #undef USB_EP9_RX_SIZE.................... #endif.................... #define USB_EP9_RX_SIZE 0.................... #else.................... #ifndef USB_EP9_RX_SIZE.................... #error You enabled EP9 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP9_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP9_TX_SIZE.................... #undef USB_EP9_TX_SIZE.................... #endif.................... #define USB_EP9_TX_SIZE 0.................... #else.................... #ifndef USB_EP9_TX_SIZE.................... #error You enabled EP9 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 10 defines ----------.................... #ifndef USB_EP10_TX_ENABLE.................... #define USB_EP10_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP10_RX_ENABLE.................... #define USB_EP10_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP10_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP10_RX_SIZE.................... #undef USB_EP10_RX_SIZE.................... #endif.................... #define USB_EP10_RX_SIZE 0.................... #else.................... #ifndef USB_EP10_RX_SIZE.................... #error You enabled EP10 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP10_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP10_TX_SIZE.................... #undef USB_EP10_TX_SIZE.................... #endif.................... #define USB_EP10_TX_SIZE 0.................... #else.................... #ifndef USB_EP10_TX_SIZE.................... #error You enabled EP10 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 11 defines ----------.................... #ifndef USB_EP11_TX_ENABLE.................... #define USB_EP11_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP11_RX_ENABLE.................... #define USB_EP11_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP11_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP11_RX_SIZE.................... #undef USB_EP11_RX_SIZE.................... #endif.................... #define USB_EP11_RX_SIZE 0.................... #else.................... #ifndef USB_EP11_RX_SIZE.................... #error You enabled EP11 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP11_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP11_TX_SIZE.................... #undef USB_EP11_TX_SIZE.................... #endif.................... #define USB_EP11_TX_SIZE 0.................... #else.................... #ifndef USB_EP11_TX_SIZE.................... #error You enabled EP11 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 12 defines ----------.................... #ifndef USB_EP12_TX_ENABLE.................... #define USB_EP12_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP12_RX_ENABLE.................... #define USB_EP12_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP12_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP12_RX_SIZE.................... #undef USB_EP12_RX_SIZE.................... #endif.................... #define USB_EP12_RX_SIZE 0.................... #else.................... #ifndef USB_EP12_RX_SIZE.................... #error You enabled EP12 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP12_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP12_TX_SIZE.................... #undef USB_EP12_TX_SIZE.................... #endif.................... #define USB_EP12_TX_SIZE 0.................... #else.................... #ifndef USB_EP12_TX_SIZE.................... #error You enabled EP12 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 13 defines ----------.................... #ifndef USB_EP13_TX_ENABLE.................... #define USB_EP13_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP13_RX_ENABLE.................... #define USB_EP13_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP13_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP13_RX_SIZE.................... #undef USB_EP13_RX_SIZE.................... #endif.................... #define USB_EP13_RX_SIZE 0.................... #else.................... #ifndef USB_EP13_RX_SIZE.................... #error You enabled EP13 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP13_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP13_TX_SIZE.................... #undef USB_EP13_TX_SIZE.................... #endif.................... #define USB_EP13_TX_SIZE 0.................... #else.................... #ifndef USB_EP13_TX_SIZE.................... #error You enabled EP13 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 14 defines ----------.................... #ifndef USB_EP14_TX_ENABLE.................... #define USB_EP14_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP14_RX_ENABLE.................... #define USB_EP14_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP14_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP14_RX_SIZE.................... #undef USB_EP14_RX_SIZE.................... #endif.................... #define USB_EP14_RX_SIZE 0.................... #else.................... #ifndef USB_EP14_RX_SIZE.................... #error You enabled EP14 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP14_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP14_TX_SIZE.................... #undef USB_EP14_TX_SIZE.................... #endif.................... #define USB_EP14_TX_SIZE 0.................... #else.................... #ifndef USB_EP14_TX_SIZE.................... #error You enabled EP14 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 15 defines ----------.................... #ifndef USB_EP15_TX_ENABLE.................... #define USB_EP15_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP15_RX_ENABLE.................... #define USB_EP15_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP15_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP15_RX_SIZE.................... #undef USB_EP15_RX_SIZE.................... #endif.................... #define USB_EP15_RX_SIZE 0.................... #else.................... #ifndef USB_EP15_RX_SIZE.................... #error You enabled EP15 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP15_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP15_TX_SIZE.................... #undef USB_EP15_TX_SIZE.................... #endif.................... #define USB_EP15_TX_SIZE 0.................... #else.................... #ifndef USB_EP15_TX_SIZE.................... #error You enabled EP15 for TX but didn't specify endpoint size.................... #endif.................... #endif........................................ const int8 usb_ep_tx_type[16]={.................... USB_EP0_TX_ENABLE, USB_EP1_TX_ENABLE, USB_EP2_TX_ENABLE,.................... USB_EP3_TX_ENABLE, USB_EP4_TX_ENABLE, USB_EP5_TX_ENABLE,.................... USB_EP6_TX_ENABLE, USB_EP7_TX_ENABLE, USB_EP8_TX_ENABLE,.................... USB_EP9_TX_ENABLE, USB_EP10_TX_ENABLE, USB_EP11_TX_ENABLE,.................... USB_EP12_TX_ENABLE, USB_EP13_TX_ENABLE, USB_EP14_TX_ENABLE,.................... USB_EP15_TX_ENABLE.................... };........................................ const int8 usb_ep_rx_type[16]={.................... USB_EP0_RX_ENABLE, USB_EP1_RX_ENABLE, USB_EP2_RX_ENABLE,.................... USB_EP3_RX_ENABLE, USB_EP4_RX_ENABLE, USB_EP5_RX_ENABLE,.................... USB_EP6_RX_ENABLE, USB_EP7_RX_ENABLE, USB_EP8_RX_ENABLE,.................... USB_EP9_RX_ENABLE, USB_EP10_RX_ENABLE, USB_EP11_RX_ENABLE,.................... USB_EP12_RX_ENABLE, USB_EP13_RX_ENABLE, USB_EP14_RX_ENABLE,.................... USB_EP15_RX_ENABLE.................... };........................................ const unsigned int16 usb_ep_tx_size[16]={.................... USB_EP0_TX_SIZE, USB_EP1_TX_SIZE, USB_EP2_TX_SIZE,.................... USB_EP3_TX_SIZE, USB_EP4_TX_SIZE, USB_EP5_TX_SIZE,.................... USB_EP6_TX_SIZE, USB_EP7_TX_SIZE, USB_EP8_TX_SIZE,.................... USB_EP9_TX_SIZE, USB_EP10_TX_SIZE, USB_EP11_TX_SIZE,.................... USB_EP12_TX_SIZE, USB_EP13_TX_SIZE, USB_EP14_TX_SIZE,.................... USB_EP15_TX_SIZE.................... };........................................ const unsigned int16 usb_ep_rx_size[16]={.................... USB_EP0_RX_SIZE, USB_EP1_RX_SIZE, USB_EP2_RX_SIZE,.................... USB_EP3_RX_SIZE, USB_EP4_RX_SIZE, USB_EP5_RX_SIZE,.................... USB_EP6_RX_SIZE, USB_EP7_RX_SIZE, USB_EP8_RX_SIZE,.................... USB_EP9_RX_SIZE, USB_EP10_RX_SIZE, USB_EP11_RX_SIZE,.................... USB_EP12_RX_SIZE, USB_EP13_RX_SIZE, USB_EP14_RX_SIZE,.................... USB_EP15_RX_SIZE.................... };........................................ #ENDIF............................................................ #if defined(__PIC16_USB_H__).................... #include <pic_usb.c>.................... #endif........................................ #if defined(__PIC18_USB_H__).................... #include <pic18_usb.c>.................... /////////////////////////////////////////////////////////////////////////.................... //// pic18_usb.c ////.................... //// ////.................... //// Hardware layer for CCS's USB library. See pic18_usb.h more ////.................... //// documentation about the PIC18 hardware layer. ////.................... //// ////.................... //// This file is part of CCS's PIC USB driver code. See USB.H ////.................... //// for more documentation and a list of examples. ////.................... //// ////.................... /////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// 18F14K50 family added. ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// Nov 3rd, 2008: ////.................... //// * 4553 family added. ////.................... //// ////.................... //// Dec 18, 2007: ////.................... //// * usb_kbhit() moved to device driver. ////.................... //// * USB Token handler changed to workaround a flaw in the USB ////.................... //// peripheral to prevent the USB peripheral from ////.................... //// inadvertantly STALLing the endpoint. Happened most often ////.................... //// in bulk demo applications, especially CDC. ////.................... //// ////.................... //// 11-6-07: Fixed a bug where usb_data_buffer[] was reserving ////.................... //// too much RAM. ////.................... //// USB_MAX_EP0_PACKET_LENGTH value can be overwritten. ////.................... //// For the 18F4450/2450 family this should be 8 ////.................... //// because of limited RAM. Reducing this value ////.................... //// frees RAM for application. ////.................... //// Based upon which endpoints have been configured for ////.................... //// use, will free up unused USB data RAM for ////.................... //// application dynamically. This should free up ////.................... //// at least 128 bytes of RAM. ////.................... //// CDC now fits on a 18F4450/2450 ////.................... //// ////.................... //// 09-19-07: Fixed problems with 18F4450 family. ////.................... //// ////.................... //// 07-17-07: Added 18F4450,2450 support ////.................... //// ////.................... //// 07-13-07: Added 87J50 family support ////.................... //// ////.................... //// 11-01-05: usb_detach(), usb_attach() and usb_init_cs() ////.................... //// changed for the better. ////.................... //// ////.................... //// 10-28-05: Added usb_rx_packet_size() ////.................... //// ////.................... //// 07-13-05: usb_put_packet() changed for 16bit packet sizes ////.................... //// usb_flush_in() changed for 16bit packet sizes ////.................... //// usb_get_packet() changed for 16bit packet sizes ////.................... //// usb_flush_out() changed for 16bit packet sizes ////.................... //// usb_set_configured() changed for 16bit packet sizes ////.................... //// ////.................... //// 06-30-05: usb_tbe() added ////.................... //// The way endpoint 0 DTS is set has been changed. ////.................... //// ////.................... //// 06-20-05: Initial Release ////.................... //// ////.................... //// 05-13-05: Beta Release (Full Speed works) ////.................... //// ////.................... //// 03-21-05: Initial Alpha Release ////.................... //// ////.................... /////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2009 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... /////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __PIC18_USB_C__.................... #DEFINE __PIC18_USB_C__........................................ #INCLUDE <usb.h>.................... ///////////////////////////////////////////////////////////////////////////.................... //// usb.h ////.................... //// ////.................... //// Function protypes, defintions and globals used by CCS USB driver ////.................... //// ////.................... //// This file is part of CCS's USB driver code ////.................... //// ////.................... //// The following USB examples are provided by CCS: ////.................... //// ex_usb_mouse.c - A HID Mouse. ////.................... //// ex_usb_hid.c - A custom application using HID protocol. ////.................... //// ex_usb_kbmouse.c - A HID Mouse/Keyboard combo using multiple ////.................... //// interfaces. ////.................... //// ex_usb_kbmouse2.c - A HID Mouse/Keyboard combo using multiple ////.................... //// HID Reports. ////.................... //// ex_usb_scope.c - A digital oscilloscope using a custom ////.................... //// protocol requiring custom Windows drivers. ////.................... //// ex_usb_serial.c - ////.................... //// ex_usb_serial2.c - Two examples of using the CDC driver for ////.................... //// a virtual COM port. ////.................... //// ex_usb_bootloader.c - CDC/Virtual COM port bootloader. ////.................... //// ex_usb_loadmouse.c - A conversion of ex_usb_mouse.c that is ////.................... //// compatible with ex_usb_mouse.c. ////.................... //// ////.................... //// ********************** API ********************* ////.................... //// ////.................... //// These are the functions that are meant to be called by the user: ////.................... //// ////.................... //// usb_init() - Initializes the USB stack, the USB peripheral and ////.................... //// attaches the unit to the usb bus. Enables ////.................... //// interrupts. Will wait in an infinite loop until ////.................... //// the device enumerates - if you are using ////.................... //// connection sense or if the processor should run ////.................... //// even if it's not connected to USB then use ////.................... //// usb_init_cs() instead. ////.................... //// ////.................... //// usb_init_cs() - A smaller usb_init(), does not attach unit ////.................... //// to usb bus or enable interrupts. Since this does ////.................... //// not attach to the USB, you must periodically call ////.................... //// usb_task(). See usb_task() for more information. ////.................... //// ////.................... //// usb_task() - If usb_init_cs() was used to initiate the USB ////.................... //// peripheral, usb_task() should then be called periodically ////.................... //// to check the connection sense pin. If the connection ////.................... //// sense pin denotes USB is connected and the USB peripheral ////.................... //// is not attached, this will attach the USB peripheral ////.................... //// so the PC can start the enumeration process (and it ////.................... //// will enable interrupts). If the connection sense pin ////.................... //// denotes USB is not attached and the USB peripheral is ////.................... //// running, this will reset the USB peripheral and wait ////.................... //// for USB to reconnect (and usb_enumerated() will start ////.................... //// returning FALSE). If connection sense (USB_CON_SENSE_PIN) ////.................... //// is not defined the usb_task() assumes that USB is always ////.................... //// connected. ////.................... //// ////.................... //// usb_attached() - Returns TRUE if the device is attached to a ////.................... //// USB cable. A macro that looks at the defined ////.................... //// connection sense pin. If this returns TRUE ////.................... //// it does not mean the PC has connected to it, ////.................... //// you need to use usb_enumerated() to check this. ////.................... //// ////.................... //// usb_enumerated() - Returns TRUE if device has been enumerated ////.................... //// (configured) by host, FALSE if it has not. ////.................... //// Do not try to use the USB peripheral for ////.................... //// sending and receiving packets until you ////.................... //// are enumerated. ////.................... //// ////.................... //// usb_wait_for_enumeration() - Sits in an infinte loop until device ////.................... //// is enumerated. ////.................... //// ////.................... //// usb_tbe(endpoint) - Returns TRUE if the endpoint transmit buffer ////.................... //// is free and ready to accept a new packet for transmission. ////.................... //// ////.................... //// usb_put_packet(endpoint, ptr, len, tgl) - Sends one packet to the ////.................... //// host. If you need to send a message that ////.................... //// spans more than one packet then use ////.................... //// usb_puts(). Fore more detailed documentation ////.................... //// see usb_hw_layer.h ////.................... //// ////.................... //// usb_puts(endpoint, ptr, len, timeout) - Sends a multiple packet ////.................... //// message to the host. If you only need to send one packet, ////.................... //// it is more effecient to use usb_put_packet(). This is ////.................... //// documented in more detail above the prototype in USB.H. ////.................... //// ////.................... //// usb_kbhit(endpoint) - Returns true if OUT endpoint contains data ////.................... //// from host. This will remain TRUE until ////.................... //// usb_put_packet() or usb_flush_out() used. ////.................... //// This function will return an invalid response ////.................... //// if specified endpoint is not enabled for ////.................... //// receiving data. ////.................... //// ////.................... //// len = usb_get_packet(endpoint, ptr, max) - Gets one packet that ////.................... //// from the host/PC. usb_kbhit() must return TRUE before you ////.................... //// call this routine or your data may not be valid. This ////.................... //// only receives one packet, if you are trying to receive a ////.................... //// multi-packet message use usb_gets(). For more detailed ////.................... //// documentation see usb_hw_layer.h. ////.................... //// ////.................... //// len = usb_gets(endpoint, ptr, max, timeout) - Gets multiple ////.................... //// packets from the host, you would use this instead ////.................... //// of usb_get_packet() if you wanted to handle multi-packet ////.................... //// messages. This is documented in more detail above the ////.................... //// prototype in USB.H. ////.................... //// ////.................... //// ////.................... //// ********* DEFINITIONS / CONFIGURATION ********** ////.................... //// ////.................... //// The following definitions are declared here, but can be ////.................... //// overwritten in your code. Unless needed otherwise, leave ////.................... //// to default value. If confused about a definition read the ////.................... //// comments at each defintion ////.................... //// ////.................... //// USB_HID_IDLE (TRUE) - Set to TRUE if your device supports ////.................... //// Set_Idle HID class request. Set to ////.................... //// False if you do not (device will ////.................... //// send a Wrong-state if computer ////.................... //// sends a Set_Idle / Get_Idle command) ////.................... //// NOTE: If you set to TRUE you must ////.................... //// provide your own code. See ////.................... //// usb_isr_tkn_setup_ClassInterface() in ////.................... //// usb.c ////.................... //// ////.................... //// USB_HID_BOOT_PROTOCOL (FALSE) - Set to TRUE if your device supports ////.................... //// Set_Protocl HID class request. Set to ////.................... //// False if you do not (device will ////.................... //// send a Wrong-state if computer ////.................... //// sends a Set_Protocl / Get_Protocol ////.................... //// command). ////.................... //// NOTE: If you set to TRUE you must ////.................... //// provide your own code in the ////.................... //// application that properly send boot ////.................... //// or HID packets. ////.................... //// ////.................... //// USB_MAX_EP0_PACKET_LENGTH (8) - Max Packet size for Endpoint 0. ////.................... //// The hardware level driver (ex ////.................... //// pic18_usb.h will define this value if ////.................... //// not already overwritten). Increasing ////.................... //// this size will speed up the ////.................... //// enumeration process. ////.................... //// ////.................... //// USB_EPx_RX_ENABLE (USB_ENABLE_DISABLED) - Where x is the ////.................... //// endpoint number. Change this define ////.................... //// to specify what kind of transfer method ////.................... //// this RX (PC to device) endpoint uses. ////.................... //// Here is the list of valid transfer methods: ////.................... //// USB_ENABLE_CONTROL ////.................... //// USB_ENABLE_ISOCHRONOUS ////.................... //// USB_ENABLE_BULK ////.................... //// USB_ENABLE_INTERRUPT ////.................... //// USB_ENABLE_DISABLED ////.................... //// Don't forget that you must specify the ////.................... //// transfer method properly in your endpoint ////.................... //// descriptor, too. ////.................... //// ////.................... //// USB_EPx_RX_SIZE (0) - For some hardware it is important to know ////.................... //// how much memory to reserve for receiving ////.................... //// packets. ////.................... //// ////.................... //// *** NOTE *** You must have both USB_EPx_RX_ENABLE defined and ////.................... //// USB_EPx_RX_SIZE defined (non 0) to enable a RX ////.................... //// endpoint. ////.................... //// *** NOTE *** Endpoint 0 is always enabled. ////.................... //// ////.................... //// USB_EPx_TX_ENABLE (USB_ENABLE_DISABLED) - Where x is the ////.................... //// endpoint number. Change this define ////.................... //// to specify what kind of transfer method ////.................... //// this TX (device to PC) endpoint uses. ////.................... //// Here is the list of valid transfer methods: ////.................... //// USB_ENABLE_CONTROL ////.................... //// USB_ENABLE_ISOCHRONOUS ////.................... //// USB_ENABLE_BULK ////.................... //// USB_ENABLE_INTERRUPT ////.................... //// USB_ENABLE_DISABLED ////.................... //// Don't forget that you must specify the ////.................... //// transfer method properly in your endpoint ////.................... //// descriptor, too. ////.................... //// ////.................... //// USB_EPx_TX_SIZE (0) - For some hardware it is important to know ////.................... //// how much memory to reserve for transmitting ////.................... //// packets. ////.................... //// ////.................... //// *** NOTE *** You must have both USB_EPx_TX_ENABLE defined and ////.................... //// USB_EPx_TX_SIZE defined (non 0) to enable a TX ////.................... //// endpoint. ////.................... //// ////.................... //// ////.................... //// USB_HID_DEVICE (TRUE) - HID devices require extra code to handle ////.................... //// HID requests. You can disable to save ////.................... //// ROM space if you are not using a HID ////.................... //// device. If you are not using a HID ////.................... //// device you must provide your own O/S ////.................... //// (Windows) driver. ////.................... //// ////.................... //// The other definitions should not be changed. ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// Version History: ////.................... //// ////.................... //// March 5th, 2009: ////.................... //// Cleanup for Wizard. ////.................... //// PIC24 Initial release. ////.................... //// ////.................... //// July 13th, 2005: ////.................... //// usb_ep_tx_size[] and usb_ep_rx_size[] changed to 16bits ////.................... //// ////.................... //// June 20th, 2005: ////.................... //// Initial 18fxx5x release ////.................... //// ////.................... //// May 13th, 2005: ////.................... //// Beta release, only works with 18Fxx5x hardware layer. ////.................... //// Now supports multiple interfaces (many defines in descriptors ////.................... //// will have to be changed, see examples) ////.................... //// ////.................... //// Mar 21st, 2005: ////.................... //// Initial Alpha Release with PIC18Fxx5x support. (ONLY TESTED ////.................... //// WITH 18F4550) ////.................... //// usb_gets() and usb_puts() changed (see usb.c) ////.................... //// ////.................... //// June 24th, 2004: ////.................... //// Optimization and cleanup. ////.................... //// The following definitions changed: ////.................... //// USB_EPx_TX_ENABLE and USB_EPx_RX_ENABLE have changed. See usb.h ////.................... //// USB_CONFIG_DESCRIPTORS[] removed ////.................... //// USB_CONFIG_DESC_LEN changed to USB_DESC_CONFIG_LEN ////.................... //// USB_INTERFACE_DESC_LEN changed to USB_DESC_INTERFACE_LEN ////.................... //// USB_CLASS_DESC_LEN changed to USB_DESC_CLASS_LEN ////.................... //// USB_ENDPOINT_DESC_LEN changed to USB_DESC_ENDPOINT_LEN ////.................... //// USB_CONFIG_DESC_KEY changed to USB_DESC_CONFIG_TYPE ////.................... //// USB_INTERFACE_DESC_KEY changed to USB_DESC_INTERFACE_TYPE ////.................... //// USB_CLASS_DESC_KEY changed to USB_DESC_CLASS_TYPE ////.................... //// USB_ENDPOINT_DESC_KEY changed to USB_DESC_ENDPOINT_TYPE ////.................... //// USB_STRING_X[] arrays removed, see USB_STRING_DESC[] and ////.................... //// USB_STRING_DESC_OFFSET[] ////.................... //// dev_req, curr_config, status_device and getdesc_type global ////.................... //// variables moved into struct USB_stack_status ////.................... //// ////.................... //// October 15th, 2003: Support for boot protocol added. ////.................... //// Set USB_HID_BOOT_PROTOCOL to TRUE to support this. ////.................... //// The array hid_protocol[] saves which protocol mode each ////.................... //// interface is in. It is your applications job to send ////.................... //// data that either fit the boot protocol or HID protocol. ////.................... //// ////.................... //// May 6th, 2003: Fixed a potential stack overflow using PCM ////.................... //// ////.................... //// October 29th, 2002: New definition added to USB_STATES ////.................... //// ////.................... //// August 2nd, 2002: Initial Public Release ////.................... //// ////.................... ///////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2009 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS ////.................... //// C compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, ////.................... //// reproduction or distribution is permitted without written ////.................... //// permission. Derivative programs created using this software ////.................... //// in object code form are not restricted in any way. ////.................... ///////////////////////////////////////////////////////////////////////////........................................ #IFNDEF __USB_PROTOTYPES__.................... #DEFINE __USB_PROTOTYPES__........................................ //// CONFIGURATION ////////////////////////////////////////////////////////////........................................ #ifndef USB_CON_SENSE_PIN.................... #define USB_CON_SENSE_PIN 0.................... #endif........................................ #IFNDEF USB_HID_BOOT_PROTOCOL.................... #DEFINE USB_HID_BOOT_PROTOCOL FALSE.................... #ENDIF........................................ #IFNDEF USB_HID_IDLE.................... #DEFINE USB_HID_IDLE FALSE.................... #ENDIF........................................ //should the compiler add the extra HID handler code? Defaults to yes..................... #IFNDEF USB_HID_DEVICE.................... #DEFINE USB_HID_DEVICE TRUE.................... #ENDIF........................................ #IFNDEF USB_CDC_DEVICE.................... #DEFINE USB_CDC_DEVICE FALSE.................... #ENDIF........................................ //set to false to opt for less RAM, true to opt for less ROM.................... #ifndef USB_OPT_FOR_ROM.................... #define USB_OPT_FOR_ROM TRUE.................... #endif........................................ #IFNDEF USB_MAX_EP0_PACKET_LENGTH.................... #DEFINE USB_MAX_EP0_PACKET_LENGTH 8.................... #ENDIF............................................................ ////// USER-LEVEL API /////////////////////////////////////////////////////////........................................ /**************************************************************.................... /* usb_enumerated().................... /*.................... /* Input: Global variable USB_Curr_Config.................... /* Returns: Returns a 1 if device is configured / enumerated,.................... /* Returns a 0 if device is un-configured / not enumerated..................... /*.................... /* Summary: See API section of USB.H for more documentation..................... /***************************************************************/.................... int1 usb_enumerated(void);........................................ /**************************************************************.................... /* usb_wait_for_enumeration().................... /*.................... /* Input: Global variable USB_Curr_Config.................... /*.................... /* Summary: Waits in-definately until device is configured / enumerated..................... /* See API section of USB.H for more information..................... /***************************************************************/.................... void usb_wait_for_enumeration(void);........................................ /****************************************************************************.................... /* usb_gets(endpoint, ptr, max, timeout).................... /*.................... /* Input: endpoint - endpoint to get data from.................... /* ptr - place / array to store data to.................... /* max - max amount of data to get from USB and store into ptr.................... /* timeout - time in milliseconds, for each packet, to wait before.................... /* timeout. set to 0 for no timeout..................... /*.................... /* Output: Amount of data returned. It may be less than max..................... /*.................... /* Summary: Gets data from the host. Will get multiple-packet messages.................... /* and finish when either it receives a 0-len packet or a packet.................... /* of less size than maximum..................... /*.................... /*****************************************************************************/.................... unsigned int16 usb_gets(int8 endpoint, int8 * ptr, unsigned int16 max, unsigned int16 timeout);........................................ /****************************************************************************.................... /* usb_puts().................... /*.................... /* Inputs: endpoint - endpoint to send data out.................... /* ptr - points to array of data to send.................... /* len - amount of data to send.................... /* timeout - time in milli-seconds, for each packet, to wait before.................... /* timeout. set to 0 for no timeout..................... /*.................... /* Outputs: Returns TRUE if message sent succesfully, FALSE if it was not.................... /* sent before timeout period expired..................... /*.................... /* Summary: Used for sending multiple packets of data as one message. This.................... /* function can still be used to send messages consiting of only one.................... /* packet. See usb_put_packet() documentation for the rules about when.................... /* multiple packet messages or 0-lenght packets are needed..................... /*.................... /*****************************************************************************/.................... int1 usb_puts(int8 endpoint, int8 * ptr, unsigned int16 len, unsigned int8 timeout);........................................ /******************************************************************************.................... /* usb_attached().................... /*.................... /* Summary: Returns TRUE if the device is attached to a USB cable..................... /* See the API section of USB.H for more documentation..................... /*.................... /*****************************************************************************/.................... #if USB_CON_SENSE_PIN.................... #define usb_attached() input(USB_CON_SENSE_PIN).................... #else.................... #define usb_attached() TRUE.................... #endif........................................ ////// END USER-LEVEL API /////////////////////////////////////////////////////............................................................ ////// STACK-LEVEL API USED BY HW DRIVERS ////////////////////////////////////........................................ enum USB_STATES {GET_DESCRIPTOR=1,SET_ADDRESS=2,NONE=0};........................................ enum USB_GETDESC_TYPES {USB_GETDESC_CONFIG_TYPE=0,USB_GETDESC_HIDREPORT_TYPE=1,USB_GETDESC_STRING_TYPE=2,USB_GETDESC_DEVICE_TYPE=3};........................................ #if USB_OPT_FOR_ROM.................... typedef struct {.................... USB_STATES dev_req; //what did the last setup token set us up to do?. init at none.................... int curr_config; //our current config. start at none/powered (NOT THAT THIS LIMITS US TO 3 CONFIGURATIONS).................... int status_device; //Holds our state for Set_Feature and Clear_Feature.................... USB_GETDESC_TYPES getdesc_type; //which get_descriptor() we are handling.................... } TYPE_USB_STACK_STATUS;.................... #else.................... typedef struct {.................... USB_STATES dev_req:2; //what did the last setup token set us up to do?. init at none.................... int Curr_config:2; //our current config. start at none/powered (NOT THAT THIS LIMITS US TO 3 CONFIGURATIONS).................... int status_device:2; //Holds our state for Set_Feature and Clear_Feature.................... USB_GETDESC_TYPES getdesc_type:2; //which get_descriptor() we are handling.................... } TYPE_USB_STACK_STATUS;.................... #endif........................................ extern TYPE_USB_STACK_STATUS USB_stack_status;........................................ /**************************************************************.................... /* usb_token_reset().................... /*.................... /* Output: No output (but many global registers are modified).................... /*.................... /* Summary: Resets the token handler to initial (unconfigured) state..................... /***************************************************************/.................... void usb_token_reset(void);........................................ /**************************************************************.................... /* usb_isr_tok_setup_dne().................... /*.................... /* Input: usb_ep0_rx_buffer[] contains the the setup packet..................... /*.................... /* Output: None (many globals are changed).................... /*.................... /* Summary: This function is that handles the setup token..................... /* We must handle all relevant requests, such as Set_Configuration,.................... /* Get_Descriptor, etc..................... /*.................... /* usb_ep0_rx_buffer[] contains setup data packet, which has the.................... /* following records:.................... /* -------------------------------------------------------------------------------------------.................... /* usb_ep0_rx_buffer[ 0 ]=bmRequestType; Where the setup packet goes.................... /* bit7 (0) host-to-device.................... /* (1) device-to-host.................... /* bit6-5 (00) usb standard request;.................... /* (01) class request;.................... /* (10) vendor request.................... /* (11) reserved.................... /* bit4-0 (0000) device.................... /* (0001) interface.................... /* (0010) endpoint.................... /* (0011) other element.................... /* (0100) to (1111) reserved.................... /* usb_ep0_rx_buffer[ 1 ]=bRequest ; the request.................... /* usb_ep0_rx_buffer[2,3]=wValue ; a value which corresponds to request.................... /* usb_ep0_rx_buffer[4,5]=wIndex ; could correspond to interface or endpoint....................... /* usb_ep0_rx_buffer[6,7]=wLength ; number of bytes in next data packet;.................... /* for host-to-device, this exactly how many bytes in data packet..................... /* for device-to-host, this is the maximum bytes that can fit one packet..................... /***************************************************************/.................... void usb_isr_tok_setup_dne(void);........................................ /**************************************************************.................... /* usb_isr_tok_out_dne().................... /*.................... /* Input: endpoint contains which endpoint we are receiving data (0..15).................... /*.................... /* Summary: Processes out tokens (out is respective of the host, so actualy.................... /* incoming to the pic), but not out setup tokens. Normally when.................... /* data is received it is left in the buffer (user would use.................... /* usb_kbhit() and usb_get_packet() to receive data), but certain.................... /* libraries (like CDC) have to answer setup packets..................... /*.................... /***************************************************************/.................... void usb_isr_tok_out_dne(int8 endpoint);........................................ /**************************************************************.................... /* usb_isr_tok_in_dne(endpoint).................... /*.................... /* Input: endpoint - which endpoint we are processing a setup token..................... /*.................... /* Summary: This handles an IN packet (HOST <- PIC). For endpoint 0, this.................... /* is usually to setup a response packet to a setup packet. Endpoints 1..15.................... /* are generally ignored, and the user has to use usb_tbe() to determine if.................... /* if the buffer is ready for a new transmit packet (there are special cases,.................... /* like CDC which handles the CDC protocl)..................... /*.................... /***************************************************************/.................... void usb_isr_tok_in_dne(int8 endpoint);........................................ ////// END STACK-LEVEL API USED BY HW DRIVERS /////////////////////////////////............................................................ //CCS only supports one configuration at this time.................... #DEFINE USB_NUM_CONFIGURATIONS 1 //DO NOT CHANGE........................................ //PID values for tokens (see page 48 of USB Complete ed.1).................... #define PID_IN 0x09 //device to host transactions.................... #define PID_OUT 0x01 //host to device transactions.................... #define PID_SETUP 0x0D //host to device setup transaction.................... #define PID_ACK 0x02 //receiver accepts error-free data packet.................... #define PID_DATA0 0x03 //data packet with even sync bit.................... #define PID_SOF 0x05 //start of framer marker and frame number.................... #define PID_NAK 0x0A //receiver can't accept data or sender cant send data or has no data to transmit.................... #define PID_DATA1 0x0B //data packet with odd sync bit.................... #define PID_PRE 0x0C //preamble issued by host. enables downstream traffic to low-speed device.................... #define PID_STALL 0x0E //a control request isnt supported or the endpoint is halted........................................ //Key which identifies descritpors.................... #DEFINE USB_DESC_DEVICE_TYPE 0x01 //#DEFINE USB_DEVICE_DESC_KEY 0x01.................... #DEFINE USB_DESC_CONFIG_TYPE 0x02 //#DEFINE USB_CONFIG_DESC_KEY 0x02.................... #DEFINE USB_DESC_STRING_TYPE 0x03 //#DEFINE USB_STRING_DESC_KEY 0x03.................... #DEFINE USB_DESC_INTERFACE_TYPE 0x04 //#DEFINE USB_INTERFACE_DESC_KEY 0x04.................... #DEFINE USB_DESC_ENDPOINT_TYPE 0x05 //#DEFINE USB_ENDPOINT_DESC_KEY 0x05.................... #DEFINE USB_DESC_CLASS_TYPE 0x21 //#DEFINE USB_CLASS_DESC_KEY 0x21.................... #DEFINE USB_DESC_HIDREPORT_TYPE 0x22........................................ //The length of each descriptor.................... #DEFINE USB_DESC_DEVICE_LEN 18 //#DEFINE USB_DEVICE_DESC_LEN 18.................... #DEFINE USB_DESC_CONFIG_LEN 9 //#DEFINE USB_CONFIG_DESC_LEN 9.................... #DEFINE USB_DESC_INTERFACE_LEN 9 //#DEFINE USB_INTERFACE_DESC_LEN 9.................... #DEFINE USB_DESC_CLASS_LEN 9 //#DEFINE USB_CLASS_DESC_LEN 9.................... #DEFINE USB_DESC_ENDPOINT_LEN 7 //#DEFINE USB_ENDPOINT_DESC_LEN 7........................................ //Standard USB Setup bRequest Codes.................... #define USB_STANDARD_REQUEST_GET_STATUS 0x00.................... #define USB_STANDARD_REQUEST_CLEAR_FEATURE 0x01.................... #define USB_STANDARD_REQUEST_SET_FEATURE 0x03.................... #define USB_STANDARD_REQUEST_SET_ADDRESS 0x05.................... #define USB_STANDARD_REQUEST_GET_DESCRIPTOR 0x06.................... #define USB_STANDARD_REQUEST_SET_DESCRIPTOR 0x07.................... #define USB_STANDARD_REQUEST_GET_CONFIGURATION 0x08.................... #define USB_STANDARD_REQUEST_SET_CONFIGURATION 0x09.................... #define USB_STANDARD_REQUEST_GET_INTERFACE 0x0A.................... #define USB_STANDARD_REQUEST_SET_INTERFACE 0x0B.................... #define USB_STANDARD_REQUEST_SYNCH_FRAME 0x0C........................................ //HID Class Setup bRequest Codes.................... #define USB_HID_REQUEST_GET_REPORT 0x01.................... #define USB_HID_REQUEST_GET_IDLE 0x02.................... #define USB_HID_REQUEST_GET_PROTOCOL 0x03.................... #define USB_HID_REQUEST_SET_REPORT 0x09.................... #define USB_HID_REQUEST_SET_IDLE 0x0A.................... #define USB_HID_REQUEST_SET_PROTOCOL 0x0B........................................ //types of endpoints as defined in the descriptor.................... #define USB_ENDPOINT_TYPE_CONTROL 0x00.................... #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01.................... #define USB_ENDPOINT_TYPE_BULK 0x02.................... #define USB_ENDPOINT_TYPE_INTERRUPT 0x03........................................ //types of endpoints used internally in this api.................... #define USB_ENABLE_DISABLED -1.................... #define USB_ENABLE_BULK USB_ENDPOINT_TYPE_BULK.................... #define USB_ENABLE_ISOCHRONOUS USB_ENDPOINT_TYPE_ISOCHRONOUS.................... #define USB_ENABLE_INTERRUPT USB_ENDPOINT_TYPE_INTERRUPT.................... #define USB_ENABLE_CONTROL USB_ENDPOINT_TYPE_CONTROL............................................................ //*** ENABLE RX ENDPOINTS AND BUFFERS........................................ //--------- endpoint 0 defines ----------.................... #define USB_EP0_TX_ENABLE USB_ENABLE_CONTROL.................... #define USB_EP0_RX_ENABLE USB_ENABLE_CONTROL.................... #define USB_EP0_RX_SIZE USB_MAX_EP0_PACKET_LENGTH //endpoint 0 is setup, and should always be the MAX_PACKET_LENGTH. Slow speed specifies 8.................... #define USB_EP0_TX_SIZE USB_MAX_EP0_PACKET_LENGTH //endpoint 0 is setup, and should always be the MAX_PACKET_LENGTH. Slow speed specifies 8........................................ //--------- endpoint 1 defines ----------.................... #ifndef USB_EP1_TX_ENABLE.................... #define USB_EP1_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP1_RX_ENABLE.................... #define USB_EP1_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP1_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP1_RX_SIZE.................... #undef USB_EP1_RX_SIZE.................... #endif.................... #define USB_EP1_RX_SIZE 0.................... #else.................... #ifndef USB_EP1_RX_SIZE.................... #error You enabled EP1 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP1_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP1_TX_SIZE.................... #undef USB_EP1_TX_SIZE.................... #endif.................... #define USB_EP1_TX_SIZE 0.................... #else.................... #ifndef USB_EP1_TX_SIZE.................... #error You enabled EP1 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 2 defines ----------.................... #ifndef USB_EP2_TX_ENABLE.................... #define USB_EP2_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP2_RX_ENABLE.................... #define USB_EP2_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP2_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP2_RX_SIZE.................... #undef USB_EP2_RX_SIZE.................... #endif.................... #define USB_EP2_RX_SIZE 0.................... #else.................... #ifndef USB_EP2_RX_SIZE.................... #error You enabled EP2 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP2_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP2_TX_SIZE.................... #undef USB_EP2_TX_SIZE.................... #endif.................... #define USB_EP2_TX_SIZE 0.................... #else.................... #ifndef USB_EP2_TX_SIZE.................... #error You enabled EP2 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 3 defines ----------.................... #ifndef USB_EP3_TX_ENABLE.................... #define USB_EP3_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP3_RX_ENABLE.................... #define USB_EP3_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP3_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP3_RX_SIZE.................... #undef USB_EP3_RX_SIZE.................... #endif.................... #define USB_EP3_RX_SIZE 0.................... #else.................... #ifndef USB_EP3_RX_SIZE.................... #error You enabled EP3 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP3_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP3_TX_SIZE.................... #undef USB_EP3_TX_SIZE.................... #endif.................... #define USB_EP3_TX_SIZE 0.................... #else.................... #ifndef USB_EP3_TX_SIZE.................... #error You enabled EP3 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 4 defines ----------.................... #ifndef USB_EP4_TX_ENABLE.................... #define USB_EP4_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP4_RX_ENABLE.................... #define USB_EP4_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP4_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP4_RX_SIZE.................... #undef USB_EP4_RX_SIZE.................... #endif.................... #define USB_EP4_RX_SIZE 0.................... #else.................... #ifndef USB_EP4_RX_SIZE.................... #error You enabled EP4 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP4_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP4_TX_SIZE.................... #undef USB_EP4_TX_SIZE.................... #endif.................... #define USB_EP4_TX_SIZE 0.................... #else.................... #ifndef USB_EP4_TX_SIZE.................... #error You enabled EP4 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 5 defines ----------.................... #ifndef USB_EP5_TX_ENABLE.................... #define USB_EP5_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP5_RX_ENABLE.................... #define USB_EP5_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP5_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP5_RX_SIZE.................... #undef USB_EP5_RX_SIZE.................... #endif.................... #define USB_EP5_RX_SIZE 0.................... #else.................... #ifndef USB_EP5_RX_SIZE.................... #error You enabled EP5 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP5_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP5_TX_SIZE.................... #undef USB_EP5_TX_SIZE.................... #endif.................... #define USB_EP5_TX_SIZE 0.................... #else.................... #ifndef USB_EP5_TX_SIZE.................... #error You enabled EP5 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 6 defines ----------.................... #ifndef USB_EP6_TX_ENABLE.................... #define USB_EP6_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP6_RX_ENABLE.................... #define USB_EP6_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP6_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP6_RX_SIZE.................... #undef USB_EP6_RX_SIZE.................... #endif.................... #define USB_EP6_RX_SIZE 0.................... #else.................... #ifndef USB_EP6_RX_SIZE.................... #error You enabled EP6 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP6_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP6_TX_SIZE.................... #undef USB_EP6_TX_SIZE.................... #endif.................... #define USB_EP6_TX_SIZE 0.................... #else.................... #ifndef USB_EP6_TX_SIZE.................... #error You enabled EP6 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 7 defines ----------.................... #ifndef USB_EP7_TX_ENABLE.................... #define USB_EP7_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP7_RX_ENABLE.................... #define USB_EP7_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP7_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP7_RX_SIZE.................... #undef USB_EP7_RX_SIZE.................... #endif.................... #define USB_EP7_RX_SIZE 0.................... #else.................... #ifndef USB_EP7_RX_SIZE.................... #error You enabled EP7 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP7_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP7_TX_SIZE.................... #undef USB_EP7_TX_SIZE.................... #endif.................... #define USB_EP7_TX_SIZE 0.................... #else.................... #ifndef USB_EP7_TX_SIZE.................... #error You enabled EP7 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 8 defines ----------.................... #ifndef USB_EP8_TX_ENABLE.................... #define USB_EP8_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP8_RX_ENABLE.................... #define USB_EP8_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP8_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP8_RX_SIZE.................... #undef USB_EP8_RX_SIZE.................... #endif.................... #define USB_EP8_RX_SIZE 0.................... #else.................... #ifndef USB_EP8_RX_SIZE.................... #error You enabled EP8 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP8_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP8_TX_SIZE.................... #undef USB_EP8_TX_SIZE.................... #endif.................... #define USB_EP8_TX_SIZE 0.................... #else.................... #ifndef USB_EP8_TX_SIZE.................... #error You enabled EP8 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 9 defines ----------.................... #ifndef USB_EP9_TX_ENABLE.................... #define USB_EP9_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP9_RX_ENABLE.................... #define USB_EP9_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP9_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP9_RX_SIZE.................... #undef USB_EP9_RX_SIZE.................... #endif.................... #define USB_EP9_RX_SIZE 0.................... #else.................... #ifndef USB_EP9_RX_SIZE.................... #error You enabled EP9 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP9_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP9_TX_SIZE.................... #undef USB_EP9_TX_SIZE.................... #endif.................... #define USB_EP9_TX_SIZE 0.................... #else.................... #ifndef USB_EP9_TX_SIZE.................... #error You enabled EP9 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 10 defines ----------.................... #ifndef USB_EP10_TX_ENABLE.................... #define USB_EP10_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP10_RX_ENABLE.................... #define USB_EP10_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP10_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP10_RX_SIZE.................... #undef USB_EP10_RX_SIZE.................... #endif.................... #define USB_EP10_RX_SIZE 0.................... #else.................... #ifndef USB_EP10_RX_SIZE.................... #error You enabled EP10 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP10_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP10_TX_SIZE.................... #undef USB_EP10_TX_SIZE.................... #endif.................... #define USB_EP10_TX_SIZE 0.................... #else.................... #ifndef USB_EP10_TX_SIZE.................... #error You enabled EP10 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 11 defines ----------.................... #ifndef USB_EP11_TX_ENABLE.................... #define USB_EP11_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP11_RX_ENABLE.................... #define USB_EP11_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP11_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP11_RX_SIZE.................... #undef USB_EP11_RX_SIZE.................... #endif.................... #define USB_EP11_RX_SIZE 0.................... #else.................... #ifndef USB_EP11_RX_SIZE.................... #error You enabled EP11 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP11_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP11_TX_SIZE.................... #undef USB_EP11_TX_SIZE.................... #endif.................... #define USB_EP11_TX_SIZE 0.................... #else.................... #ifndef USB_EP11_TX_SIZE.................... #error You enabled EP11 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 12 defines ----------.................... #ifndef USB_EP12_TX_ENABLE.................... #define USB_EP12_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP12_RX_ENABLE.................... #define USB_EP12_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP12_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP12_RX_SIZE.................... #undef USB_EP12_RX_SIZE.................... #endif.................... #define USB_EP12_RX_SIZE 0.................... #else.................... #ifndef USB_EP12_RX_SIZE.................... #error You enabled EP12 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP12_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP12_TX_SIZE.................... #undef USB_EP12_TX_SIZE.................... #endif.................... #define USB_EP12_TX_SIZE 0.................... #else.................... #ifndef USB_EP12_TX_SIZE.................... #error You enabled EP12 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 13 defines ----------.................... #ifndef USB_EP13_TX_ENABLE.................... #define USB_EP13_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP13_RX_ENABLE.................... #define USB_EP13_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP13_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP13_RX_SIZE.................... #undef USB_EP13_RX_SIZE.................... #endif.................... #define USB_EP13_RX_SIZE 0.................... #else.................... #ifndef USB_EP13_RX_SIZE.................... #error You enabled EP13 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP13_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP13_TX_SIZE.................... #undef USB_EP13_TX_SIZE.................... #endif.................... #define USB_EP13_TX_SIZE 0.................... #else.................... #ifndef USB_EP13_TX_SIZE.................... #error You enabled EP13 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 14 defines ----------.................... #ifndef USB_EP14_TX_ENABLE.................... #define USB_EP14_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP14_RX_ENABLE.................... #define USB_EP14_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP14_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP14_RX_SIZE.................... #undef USB_EP14_RX_SIZE.................... #endif.................... #define USB_EP14_RX_SIZE 0.................... #else.................... #ifndef USB_EP14_RX_SIZE.................... #error You enabled EP14 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP14_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP14_TX_SIZE.................... #undef USB_EP14_TX_SIZE.................... #endif.................... #define USB_EP14_TX_SIZE 0.................... #else.................... #ifndef USB_EP14_TX_SIZE.................... #error You enabled EP14 for TX but didn't specify endpoint size.................... #endif.................... #endif............................................................ //--------- endpoint 15 defines ----------.................... #ifndef USB_EP15_TX_ENABLE.................... #define USB_EP15_TX_ENABLE USB_ENABLE_DISABLED.................... #endif.................... #ifndef USB_EP15_RX_ENABLE.................... #define USB_EP15_RX_ENABLE USB_ENABLE_DISABLED.................... #endif........................................ #if USB_EP15_RX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP15_RX_SIZE.................... #undef USB_EP15_RX_SIZE.................... #endif.................... #define USB_EP15_RX_SIZE 0.................... #else.................... #ifndef USB_EP15_RX_SIZE.................... #error You enabled EP15 for RX but didn't specify endpoint size.................... #endif.................... #endif........................................ #if USB_EP15_TX_ENABLE==USB_ENABLE_DISABLED.................... #ifdef USB_EP15_TX_SIZE.................... #undef USB_EP15_TX_SIZE.................... #endif.................... #define USB_EP15_TX_SIZE 0.................... #else.................... #ifndef USB_EP15_TX_SIZE.................... #error You enabled EP15 for TX but didn't specify endpoint size.................... #endif.................... #endif........................................ const int8 usb_ep_tx_type[16]={.................... USB_EP0_TX_ENABLE, USB_EP1_TX_ENABLE, USB_EP2_TX_ENABLE,.................... USB_EP3_TX_ENABLE, USB_EP4_TX_ENABLE, USB_EP5_TX_ENABLE,.................... USB_EP6_TX_ENABLE, USB_EP7_TX_ENABLE, USB_EP8_TX_ENABLE,.................... USB_EP9_TX_ENABLE, USB_EP10_TX_ENABLE, USB_EP11_TX_ENABLE,.................... USB_EP12_TX_ENABLE, USB_EP13_TX_ENABLE, USB_EP14_TX_ENABLE,.................... USB_EP15_TX_ENABLE.................... };........................................ const int8 usb_ep_rx_type[16]={.................... USB_EP0_RX_ENABLE, USB_EP1_RX_ENABLE, USB_EP2_RX_ENABLE,.................... USB_EP3_RX_ENABLE, USB_EP4_RX_ENABLE, USB_EP5_RX_ENABLE,.................... USB_EP6_RX_ENABLE, USB_EP7_RX_ENABLE, USB_EP8_RX_ENABLE,.................... USB_EP9_RX_ENABLE, USB_EP10_RX_ENABLE, USB_EP11_RX_ENABLE,.................... USB_EP12_RX_ENABLE, USB_EP13_RX_ENABLE, USB_EP14_RX_ENABLE,.................... USB_EP15_RX_ENABLE.................... };........................................ const unsigned int16 usb_ep_tx_size[16]={.................... USB_EP0_TX_SIZE, USB_EP1_TX_SIZE, USB_EP2_TX_SIZE,.................... USB_EP3_TX_SIZE, USB_EP4_TX_SIZE, USB_EP5_TX_SIZE,.................... USB_EP6_TX_SIZE, USB_EP7_TX_SIZE, USB_EP8_TX_SIZE,.................... USB_EP9_TX_SIZE, USB_EP10_TX_SIZE, USB_EP11_TX_SIZE,.................... USB_EP12_TX_SIZE, USB_EP13_TX_SIZE, USB_EP14_TX_SIZE,.................... USB_EP15_TX_SIZE.................... };........................................ const unsigned int16 usb_ep_rx_size[16]={.................... USB_EP0_RX_SIZE, USB_EP1_RX_SIZE, USB_EP2_RX_SIZE,.................... USB_EP3_RX_SIZE, USB_EP4_RX_SIZE, USB_EP5_RX_SIZE,.................... USB_EP6_RX_SIZE, USB_EP7_RX_SIZE, USB_EP8_RX_SIZE,.................... USB_EP9_RX_SIZE, USB_EP10_RX_SIZE, USB_EP11_RX_SIZE,.................... USB_EP12_RX_SIZE, USB_EP13_RX_SIZE, USB_EP14_RX_SIZE,.................... USB_EP15_RX_SIZE.................... };........................................ #ENDIF............................................................ //if you are worried that the PIC is not receiving packets because a bug in the.................... //DATA0/DATA1 synch code, you can set this to TRUE to ignore the DTS on.................... //receiving..................... #ifndef USB_IGNORE_RX_DTS.................... #define USB_IGNORE_RX_DTS FALSE.................... #endif........................................ #ifndef USB_IGNORE_TX_DTS.................... #define USB_IGNORE_TX_DTS FALSE.................... #endif........................................ #if ((getenv("DEVICE")=="PIC18F87J50") || (getenv("DEVICE")=="PIC18F86J55") || \.................... (getenv("DEVICE")=="PIC18F86J50") || (getenv("DEVICE")=="PIC18F85J50") || \.................... (getenv("DEVICE")=="PIC18F67J50") || (getenv("DEVICE")=="PIC18F66J55") || \.................... (getenv("DEVICE")=="PIC18F66J50") || (getenv("DEVICE")=="PIC18F65J50")).................... #define __USB_87J50__.................... #define USB_TOTAL_RAM_SPACE ((int16)getenv("RAM")-0x400).................... #define USB_RAM_START 0x400.................... #define USB_LAST_ALLOWED_ENDPOINT 16.................... #elif ((getenv("DEVICE")=="PIC18F2450") || (getenv("DEVICE")=="PIC18F4450")).................... #define __USB_4450__.................... #define USB_TOTAL_RAM_SPACE ((int16)0x100).................... #define USB_RAM_START 0x400.................... #define USB_LAST_ALLOWED_ENDPOINT 3.................... #elif ((getenv("DEVICE")=="PIC18F13K50") || (getenv("DEVICE")=="PIC18F14K50")).................... #define __USB_K50__.................... #define USB_TOTAL_RAM_SPACE ((int16)0x200).................... #define USB_RAM_START 0x200.................... #define USB_LAST_ALLOWED_ENDPOINT 8.................... #elif ((getenv("DEVICE")=="PIC18F2455") || (getenv("DEVICE")=="PIC18F2550") || \.................... (getenv("DEVICE")=="PIC18F4455") || (getenv("DEVICE")=="PIC18F4550") || \.................... (getenv("DEVICE")=="PIC18F2458") || (getenv("DEVICE")=="PIC18F2553") || \.................... (getenv("DEVICE")=="PIC18F4458") || (getenv("DEVICE")=="PIC18F4553") \.................... ).................... #define __USB_4550__.................... #define USB_TOTAL_RAM_SPACE ((int16)0x400).................... #define USB_RAM_START 0x400.................... #define USB_LAST_ALLOWED_ENDPOINT 16.................... #else.................... #error Unknown PIC device, USB not supported in this library..................... #endif........................................ #if USB_EP15_TX_SIZE || USB_EP15_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 15.................... #elif USB_EP14_TX_SIZE || USB_EP14_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 14.................... #elif USB_EP13_TX_SIZE || USB_EP13_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 13.................... #elif USB_EP12_TX_SIZE || USB_EP12_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 12.................... #elif USB_EP11_TX_SIZE || USB_EP11_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 11.................... #elif USB_EP10_TX_SIZE || USB_EP10_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 10.................... #elif USB_EP9_TX_SIZE || USB_EP9_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 9.................... #elif USB_EP8_TX_SIZE || USB_EP8_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 8.................... #elif USB_EP7_TX_SIZE || USB_EP7_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 7.................... #elif USB_EP6_TX_SIZE || USB_EP6_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 6.................... #elif USB_EP5_TX_SIZE || USB_EP5_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 5.................... #elif USB_EP4_TX_SIZE || USB_EP4_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 4.................... #elif USB_EP3_TX_SIZE || USB_EP3_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 3.................... #elif USB_EP2_TX_SIZE || USB_EP2_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 2.................... #elif USB_EP1_TX_SIZE || USB_EP1_RX_SIZE.................... #define USB_LAST_DEFINED_ENDPOINT 1.................... #else.................... #define USB_LAST_DEFINED_ENDPOINT 0.................... #endif........................................ #define USB_CONTROL_REGISTER_SIZE ((USB_LAST_DEFINED_ENDPOINT+1)*8)........................................ #define USB_DATA_BUFFER_NEEDED (USB_EP0_TX_SIZE+USB_EP0_RX_SIZE+USB_EP1_TX_SIZE+\.................... USB_EP1_RX_SIZE+USB_EP2_TX_SIZE+USB_EP2_RX_SIZE+\.................... USB_EP3_TX_SIZE+USB_EP3_RX_SIZE+USB_EP4_TX_SIZE+\.................... USB_EP4_RX_SIZE+USB_EP5_TX_SIZE+USB_EP5_RX_SIZE+\.................... USB_EP6_TX_SIZE+USB_EP6_RX_SIZE+USB_EP7_TX_SIZE+\.................... USB_EP7_RX_SIZE+USB_EP8_TX_SIZE+USB_EP8_RX_SIZE+\.................... USB_EP9_TX_SIZE+USB_EP9_RX_SIZE+USB_EP10_TX_SIZE+\.................... USB_EP10_RX_SIZE+USB_EP11_TX_SIZE+USB_EP11_RX_SIZE+\.................... USB_EP12_TX_SIZE+USB_EP12_RX_SIZE+USB_EP13_TX_SIZE+\.................... USB_EP13_RX_SIZE+USB_EP14_TX_SIZE+USB_EP14_RX_SIZE+\.................... USB_EP15_TX_SIZE+USB_EP15_RX_SIZE)........................................ #if ((USB_DATA_BUFFER_NEEDED+USB_CONTROL_REGISTER_SIZE) > USB_TOTAL_RAM_SPACE).................... #error You are trying to allocate more memory for endpoints than the PIC can handle.................... #endif........................................ #if (USB_LAST_DEFINED_ENDPOINT > USB_LAST_ALLOWED_ENDPOINT).................... #error You are trying ot use an invalid endpoint for this hardware!.................... #endif........................................ #define USB_DATA_BUFFER_LOCATION ((int16)USB_RAM_START+USB_CONTROL_REGISTER_SIZE)........................................ typedef struct.................... {.................... int8 stat;.................... int8 cnt;.................... int16 addr;.................... } STRUCT_BD;........................................ struct.................... {.................... struct.................... {.................... STRUCT_BD out; //pc -> pic.................... STRUCT_BD in; //pc <- pic.................... } bd[USB_LAST_DEFINED_ENDPOINT+1];.................... union.................... {.................... struct.................... {.................... int8 ep0_rx_buffer[USB_MAX_EP0_PACKET_LENGTH];.................... int8 ep0_tx_buffer[USB_MAX_EP0_PACKET_LENGTH];........................................ //these buffer definitions needed for CDC library.................... #if USB_EP1_RX_SIZE.................... int8 ep1_rx_buffer[USB_EP1_RX_SIZE];.................... #endif.................... #if USB_EP1_TX_SIZE.................... int8 ep1_tx_buffer[USB_EP1_TX_SIZE];.................... #endif.................... #if USB_EP2_RX_SIZE.................... int8 ep2_rx_buffer[USB_EP2_RX_SIZE];.................... #endif.................... #if USB_EP2_TX_SIZE.................... int8 ep2_tx_buffer[USB_EP2_TX_SIZE];.................... #endif.................... };.................... int8 general[USB_DATA_BUFFER_NEEDED];.................... } buffer;.................... } g_USBRAM;.................... #locate g_USBRAM=USB_RAM_START........................................ #define usb_ep0_rx_buffer g_USBRAM.buffer.ep0_rx_buffer.................... #define usb_ep0_tx_buffer g_USBRAM.buffer.ep0_tx_buffer........................................ //these buffer definitions needed for CDC library.................... #define usb_ep1_rx_buffer g_USBRAM.buffer.ep1_rx_buffer.................... #define usb_ep1_tx_buffer g_USBRAM.buffer.ep1_tx_buffer.................... #define usb_ep2_rx_buffer g_USBRAM.buffer.ep2_rx_buffer.................... #define usb_ep2_tx_buffer g_USBRAM.buffer.ep2_tx_buffer........................................ #define debug_usb(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z).................... //#define debug_usb printf.................... //#define debug_putc putc_tbe.................... #define debug_display_ram(x,y).................... /*.................... void debug_display_ram(int8 len, int8 *ptr) {.................... int8 max=16;.................... debug_usb(debug_putc,"%U - ",len);.................... if (max>len) {max=len;}.................... while(max--) {.................... debug_usb(debug_putc,"%X",*ptr);.................... len--;.................... ptr++;.................... }.................... if (len) {debug_usb(debug_putc,"...");}.................... }.................... */........................................ //if you enable this it will keep a counter of the 6 possible errors the.................... //pic can detect. disabling this will save you ROM, RAM and execution time..................... #if !defined(USB_USE_ERROR_COUNTER).................... #define USB_USE_ERROR_COUNTER FALSE.................... #endif........................................ #define USB_PING_PONG_MODE_OFF 0 //no ping pong.................... #define USB_PING_PONG_MODE_E0 1 //ping pong endpoint 0 only.................... #define USB_PING_PONG_MODE_ON 2 //ping pong all endpoints........................................ //NOTE - PING PONG MODE IS NOT SUPPORTED BY CCS!.................... #if !defined(USB_PING_PONG_MODE).................... #define USB_PING_PONG_MODE USB_PING_PONG_MODE_OFF.................... #endif........................................ #if USB_USE_ERROR_COUNTER.................... int ERROR_COUNTER[6];.................... #endif........................................ //---pic18fxx5x memory locations.................... #if defined(__USB_4550__) || defined(__USB_4450__).................... #byte UFRML = 0xF66.................... #byte UFRMH = 0xF67.................... #byte UIR = 0xF68.................... #byte UIE = 0xF69.................... #byte UEIR = 0xF6A.................... #byte UEIE = 0xF6B.................... #byte USTAT = 0xF6C.................... #byte UCON = 0xF6D.................... #byte UADDR = 0xF6E.................... #byte UCFG = 0xF6F.................... #define UEP0_LOC 0xF70.................... #elif defined(__USB_K50__).................... #byte UFRML = 0xF5D.................... #byte UFRMH = 0xF5E.................... #byte UIR = 0xF62.................... #byte UIE = 0xF60.................... #byte UEIR = 0xF5F.................... #byte UEIE = 0xF5B.................... #byte USTAT = 0xF63.................... #byte UCON = 0xF64.................... #byte UADDR = 0xF5C.................... #byte UCFG = 0xF61.................... #define UEP0_LOC 0xF53.................... #else.................... #byte UFRML = 0xF60.................... #byte UFRMH = 0xF61.................... #byte UIR = 0xF62.................... #byte UIE = 0xF5C.................... #byte UEIR = 0xF63.................... #byte UEIE = 0xF5D.................... #byte USTAT = 0xF64.................... #byte UCON = 0xF65.................... #byte UADDR = 0xF5E.................... #byte UCFG = 0xF5F.................... #define UEP0_LOC 0xF4C.................... #endif........................................ int8 g_UEP[16];.................... #locate g_UEP=UEP0_LOC.................... #define UEP(x) g_UEP[x]........................................ #BIT UIR_SOF = UIR.6.................... #BIT UIR_STALL = UIR.5.................... #BIT UIR_IDLE = UIR.4.................... #BIT UIR_TRN = UIR.3.................... #BIT UIR_ACTV = UIR.2.................... #BIT UIR_UERR = UIR.1.................... #BIT UIR_URST = UIR.0........................................ #BIT UIE_SOF = UIE.6.................... #BIT UIE_STALL = UIE.5.................... #BIT UIE_IDLE = UIE.4.................... #BIT UIE_TRN = UIE.3.................... #BIT UIE_ACTV = UIE.2.................... #BIT UIE_UERR = UIE.1.................... #BIT UIE_URST = UIE.0........................................ #bit UCON_PBRST=UCON.6.................... #bit UCON_SE0=UCON.5.................... #bit UCON_PKTDIS=UCON.4.................... #bit UCON_USBEN=UCON.3.................... #bit UCON_RESUME=UCON.2.................... #bit UCON_SUSPND=UCON.1........................................ #if (USB_PING_PONG_MODE==USB_PING_PONG_MODE_OFF).................... #define EP_BDxST_O(x) g_USBRAM.bd[x].out.stat.................... #define EP_BDxCNT_O(x) g_USBRAM.bd[x].out.cnt.................... #define EP_BDxADR_O(x) g_USBRAM.bd[x].out.addr.................... #define EP_BDxST_I(x) g_USBRAM.bd[x].in.stat.................... #define EP_BDxCNT_I(x) g_USBRAM.bd[x].in.cnt.................... #define EP_BDxADR_I(x) g_USBRAM.bd[x].in.addr.................... #else.................... #error Right now this driver only supports no ping pong.................... #endif........................................ //See UEPn (0xF70-0xF7F).................... #define ENDPT_DISABLED 0x00 //endpoint not used.................... #define ENDPT_IN_ONLY 0x02 //endpoint supports IN transactions only.................... #define ENDPT_OUT_ONLY 0x04 //endpoint supports OUT transactions only.................... #define ENDPT_CONTROL 0x06 //Supports IN, OUT and CONTROL transactions - Only use with EP0.................... #define ENDPT_NON_CONTROL 0x0E //Supports both IN and OUT transactions........................................ //Define the states that the USB interface can be in.................... enum {USB_STATE_DETACHED=0, USB_STATE_ATTACHED=1, USB_STATE_POWERED=2, USB_STATE_DEFAULT=3,.................... USB_STATE_ADDRESS=4, USB_STATE_CONFIGURED=5} usb_state=0;........................................ //--BDendST has their PIDs upshifed 2.................... #define USB_PIC_PID_IN 0x24 //device to host transactions.................... #define USB_PIC_PID_OUT 0x04 //host to device transactions.................... #define USB_PIC_PID_SETUP 0x34 //host to device setup transaction........................................ #define USTAT_IN_E0 4.................... #define USTAT_OUT_SETUP_E0 0........................................ #define __USB_UIF_RESET 0x01.................... #define __USB_UIF_ERROR 0x02.................... #define __USB_UIF_ACTIVE 0x04.................... #define __USB_UIF_TOKEN 0x08.................... #define __USB_UIF_IDLE 0x10.................... #define __USB_UIF_STALL 0x20.................... #define __USB_UIF_SOF 0x40........................................ #if USB_USE_ERROR_COUNTER.................... #define STANDARD_INTS __USB_UIF_STALL|__USB_UIF_IDLE|__USB_UIF_TOKEN|__USB_UIF_ACTIVE|__USB_UIF_ERROR|__USB_UIF_RESET.................... #else.................... #define STANDARD_INTS __USB_UIF_STALL|__USB_UIF_IDLE|__USB_UIF_TOKEN|__USB_UIF_ACTIVE|__USB_UIF_RESET.................... #endif........................................ #define __USB_UCFG_UTEYE 0x80.................... #if defined(__USB_4550__).................... #define __USB_UCFG_UOEMON 0x40.................... #endif.................... #define __USB_UCFG_UPUEN 0x10.................... #define __USB_UCFG_UTRDIS 0x08.................... #define __USB_UCFG_FSEN 0x04........................................ #if USB_USE_FULL_SPEED.................... #define __UCFG_VAL_ENABLED__ (__USB_UCFG_UPUEN | __USB_UCFG_FSEN | USB_PING_PONG_MODE).................... #else.................... #define __UCFG_VAL_ENABLED__ (__USB_UCFG_UPUEN | USB_PING_PONG_MODE);.................... #endif........................................ #define __UCFG_VAL_DISABLED__ 0x08........................................ int8 __setup_0_tx_size;........................................ //interrupt handler, specific to PIC18Fxx5x peripheral only.................... void usb_handle_interrupt();.................... void usb_isr_rst();.................... void usb_isr_uerr();.................... void usb_isr_sof(void);.................... void usb_isr_activity();.................... void usb_isr_uidle();.................... void usb_isr_tok_dne();.................... void usb_isr_stall(void);.................... void usb_init_ep0_setup(void);........................................ //// BEGIN User Functions:........................................ // see usb_hw_layer.h for more documentation.................... int1 usb_kbhit(int8 en).................... {.................... return((UEP(en)!=ENDPT_DISABLED)&&(!bit_test(EP_BDxST_O(en),7)));.................... }........................................ // see usb_hw_layer.h for documentation.................... int1 usb_tbe(int8 en).................... {.................... return((UEP(en)!=ENDPT_DISABLED)&&(!bit_test(EP_BDxST_I(en),7)));*0D06: CLRF 030D08: MOVLB 70D0A: MOVF x47,W0D0C: ADDLW 700D0E: MOVWF FE90D10: MOVLW 0F0D12: ADDWFC 03,W0D14: MOVWF FEA0D16: MOVF FEF,F0D18: BZ 0D500D1A: CLRF x490D1C: MOVFF 747,7480D20: CLRF x4B0D22: MOVLW 080D24: MOVWF x4A0D26: MOVLB 00D28: CALL 02BA0D2C: MOVFF 02,7490D30: MOVFF 01,7480D34: MOVLW 040D36: MOVLB 70D38: ADDWF x48,F0D3A: MOVLW 000D3C: ADDWFC x49,F0D3E: MOVFF 748,FE90D42: MOVLW 040D44: ADDWF x49,W0D46: MOVWF FEA0D48: MOVFF FEF,7480D4C: BTFSS x48.70D4E: BRA 0D540D50: MOVLW 000D52: BRA 0D560D54: MOVLW 010D56: MOVWF 01.................... }0D58: MOVLB 00D5A: RETLW 00........................................ // see usb_hw_layer.h for documentation.................... void usb_detach(void).................... {.................... UCON = 0; //disable USB hardware*12F4: CLRF F6D.................... UIE = 0; //disable USB interrupts12F6: CLRF F69.................... UCFG = __UCFG_VAL_DISABLED__;12F8: MOVLW 0812FA: MOVWF F6F........................................ // set D+/D- to inputs.................... #if defined(__USB_87J50__).................... set_tris_f(get_tris_f() | 0x18);.................... #elif defined(__USB_K50__).................... set_tris_a(get_tris_a() | 0x3);.................... #else.................... set_tris_c(get_tris_c() | 0x30);12FC: MOVF F94,W12FE: IORLW 301300: MOVLB 61302: MOVWF F94.................... #endif........................................ usb_state = USB_STATE_DETACHED;1304: CLRF 241306: CLRF 181308: BTFSC FF2.7130A: BSF 18.7130C: BCF FF2.7........................................ usb_token_reset(); //clear the chapter9 stack130E: MOVLB 01310: CALL 037E1314: BTFSC 18.71316: BSF FF2.7.................... //__usb_kbhit_status=0;.................... }1318: GOTO 131E (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_attach(void).................... {*1322: CLRF 181324: BTFSC FF2.71326: BSF 18.71328: BCF FF2.7.................... usb_token_reset();132A: CALL 037E132E: BTFSC 18.71330: BSF FF2.7.................... UCON = 0;1332: CLRF F6D.................... UCFG = __UCFG_VAL_ENABLED__;1334: MOVLW 141336: MOVWF F6F.................... UIE = 0; // Mask all USB interrupts1338: CLRF F69.................... UCON_USBEN = 1; // Enable module & attach to bus133A: BSF F6D.3.................... usb_state = USB_STATE_ATTACHED; // Defined in usbmmap.c & .h133C: MOVLW 01133E: MOVWF 24.................... }1340: GOTO 134A (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_init_cs(void).................... {.................... usb_detach();*131C: BRA 12F4.................... }131E: GOTO 136A (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_task(void).................... {.................... if (usb_attached()).................... {.................... if (UCON_USBEN==0)*1344: BTFSC F6D.31346: BRA 134A.................... {.................... debug_usb(debug_putc, "\r\n\nUSB TASK: ATTACH");.................... usb_attach();1348: BRA 1322.................... }.................... }.................... else.................... {.................... if (UCON_USBEN==1).................... {.................... debug_usb(debug_putc, "\r\n\nUSB TASK: DE-ATTACH");.................... usb_detach();.................... }.................... }........................................ if ((usb_state == USB_STATE_ATTACHED)&&(!UCON_SE0))134A: DECFSZ 24,W134C: BRA 1364134E: BTFSC F6D.51350: BRA 1364.................... {.................... UIR=0;1352: CLRF F68.................... UIE=0;1354: CLRF F69.................... enable_interrupts(INT_USB);1356: BSF FA0.5.................... enable_interrupts(GLOBAL);1358: MOVLW C0135A: IORWF FF2,F.................... UIE=__USB_UIF_IDLE | __USB_UIF_RESET; //enable IDLE and RESET USB ISR135C: MOVLW 11135E: MOVWF F69.................... usb_state=USB_STATE_POWERED;1360: MOVLW 021362: MOVWF 24.................... debug_usb(debug_putc, "\r\n\nUSB TASK: POWERED");.................... }.................... }1364: GOTO 136C (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_init(void).................... {.................... usb_init_cs();1368: BRA 131C........................................ do.................... {.................... usb_task();136A: BRA 1344.................... } while (usb_state != USB_STATE_POWERED);136C: MOVF 24,W136E: SUBLW 021370: BNZ 136A.................... }1372: GOTO 1FE4 (RETURN)............................................................ // see pic18_usb.h for documentation.................... int1 usb_flush_in(int8 endpoint, int16 len, USB_DTS_BIT tgl).................... {.................... int8 i;........................................ debug_usb(debug_putc,"\r\nPUT %X %U %LU",endpoint, tgl, len);........................................ if (usb_tbe(endpoint))*0D5C: MOVFF 742,7470D60: RCALL 0D060D62: MOVF 01,F0D64: BTFSC FD8.20D66: BRA 0E74.................... {.................... EP_BDxCNT_I(endpoint)=len;0D68: MOVLB 70D6A: CLRF x490D6C: MOVFF 742,7480D70: CLRF x4B0D72: MOVLW 080D74: MOVWF x4A0D76: MOVLB 00D78: CALL 02BA0D7C: MOVFF 02,7480D80: MOVFF 01,7470D84: MOVLW 040D86: MOVLB 70D88: ADDWF x47,F0D8A: MOVLW 000D8C: ADDWFC x48,F0D8E: MOVLW 010D90: ADDWF x47,W0D92: MOVWF 010D94: MOVLW 000D96: ADDWFC x48,W0D98: MOVWF 030D9A: MOVFF 01,FE90D9E: MOVLW 040DA0: ADDWF 03,W0DA2: MOVWF FEA0DA4: MOVFF 743,FEF........................................ debug_display_ram(len, EP_BDxADR_I(endpoint));........................................ #if USB_IGNORE_TX_DTS.................... i=0x80;.................... #else.................... if (tgl == USB_DTS_TOGGLE)0DA8: MOVF x45,W0DAA: SUBLW 020DAC: BNZ 0DEE.................... {.................... i = EP_BDxST_I(endpoint);0DAE: CLRF x490DB0: MOVFF 742,7480DB4: CLRF x4B0DB6: MOVLW 080DB8: MOVWF x4A0DBA: MOVLB 00DBC: CALL 02BA0DC0: MOVFF 02,7480DC4: MOVFF 01,7470DC8: MOVLW 040DCA: MOVLB 70DCC: ADDWF x47,F0DCE: MOVLW 000DD0: ADDWFC x48,F0DD2: MOVFF 747,FE90DD6: MOVLW 040DD8: ADDWF x48,W0DDA: MOVWF FEA0DDC: MOVFF FEF,746.................... if (bit_test(i,6))0DE0: BTFSS x46.60DE2: BRA 0DE8.................... tgl = USB_DTS_DATA0; //was DATA1, goto DATA00DE4: CLRF x45.................... else0DE6: BRA 0DEC.................... tgl = USB_DTS_DATA1; //was DATA0, goto DATA10DE8: MOVLW 010DEA: MOVWF x45.................... }.................... else if (tgl == USB_DTS_USERX)0DEC: BRA 0E220DEE: MOVF x45,W0DF0: SUBLW 040DF2: BNZ 0E22.................... {.................... i = EP_BDxST_O(endpoint);0DF4: CLRF x490DF6: MOVFF 742,7480DFA: CLRF x4B0DFC: MOVLW 080DFE: MOVWF x4A0E00: MOVLB 00E02: CALL 02BA0E06: MOVLB 70E08: MOVFF 01,FE90E0C: MOVLW 040E0E: ADDWF 02,W0E10: MOVWF FEA0E12: MOVFF FEF,746.................... if (bit_test(i,6))0E16: BTFSS x46.60E18: BRA 0E20.................... tgl = USB_DTS_DATA1;0E1A: MOVLW 010E1C: MOVWF x45.................... else0E1E: BRA 0E22.................... tgl = USB_DTS_DATA0;0E20: CLRF x45.................... }.................... if (tgl == USB_DTS_DATA1)0E22: DECFSZ x45,W0E24: BRA 0E2C.................... i=0xC8; //DATA1, UOWN0E26: MOVLW C80E28: MOVWF x46.................... else //if (tgl == USB_DTS_DATA0)0E2A: BRA 0E30.................... i=0x88; //DATA0, UOWN0E2C: MOVLW 880E2E: MOVWF x46.................... #endif........................................ //set BC8 and BC9.................... if (bit_test(len,8)) {bit_set(i,0);}0E30: BTFSC x44.00E32: BSF x46.0.................... if (bit_test(len,9)) {bit_set(i,1);}0E34: BTFSC x44.10E36: BSF x46.1........................................ debug_usb(debug_putc, " %X", i);........................................ EP_BDxST_I(endpoint) = i;//save changes0E38: CLRF x490E3A: MOVFF 742,7480E3E: CLRF x4B0E40: MOVLW 080E42: MOVWF x4A0E44: MOVLB 00E46: CALL 02BA0E4A: MOVFF 02,7480E4E: MOVFF 01,7470E52: MOVLW 040E54: MOVLB 70E56: ADDWF x47,F0E58: MOVLW 000E5A: ADDWFC x48,F0E5C: MOVFF 747,FE90E60: MOVLW 040E62: ADDWF x48,W0E64: MOVWF FEA0E66: MOVFF 746,FEF........................................ //putc('!');........................................ return(1);0E6A: MOVLW 010E6C: MOVWF 010E6E: BRA 0E7A.................... }.................... else0E70: BRA 0E720E72: MOVLB 0.................... {.................... //putc('_');.................... debug_usb(debug_putc,"\r\nPUT ERR");.................... }.................... return(0);0E74: MOVLW 000E76: MOVWF 010E78: MOVLB 7.................... }0E7A: MOVLB 00E7C: RETLW 00........................................ // see usb_hw_layer.h for documentation.................... int1 usb_put_packet(int8 endpoint, int8 * ptr, int16 len, USB_DTS_BIT tgl).................... {.................... int8 * buff_add;........................................ if (usb_tbe(endpoint))*0F88: MOVFF 73A,7470F8C: RCALL 0D060F8E: MOVF 01,F0F90: BZ 1026.................... {.................... buff_add = EP_BDxADR_I(endpoint);0F92: MOVLB 70F94: CLRF x490F96: MOVFF 73A,7480F9A: CLRF x4B0F9C: MOVLW 080F9E: MOVWF x4A0FA0: MOVLB 00FA2: CALL 02BA0FA6: MOVFF 02,7430FAA: MOVFF 01,7420FAE: MOVLW 040FB0: MOVLB 70FB2: ADDWF x42,F0FB4: MOVLW 000FB6: ADDWFC x43,F0FB8: MOVLW 020FBA: ADDWF x42,W0FBC: MOVWF 010FBE: MOVLW 000FC0: ADDWFC x43,W0FC2: MOVWF 030FC4: MOVFF 01,FE90FC8: MOVLW 040FCA: ADDWF 03,W0FCC: MOVWF FEA0FCE: MOVFF FEC,030FD2: MOVF FED,F0FD4: MOVFF FEF,7400FD8: MOVFF 03,741.................... memcpy(buff_add, ptr, len);0FDC: MOVFF 741,FEA0FE0: MOVFF 740,FE90FE4: MOVFF 73C,FE20FE8: MOVFF 73B,FE10FEC: MOVFF 73E,020FF0: MOVFF 73D,010FF4: MOVF 01,F0FF6: BZ 0FFC0FF8: INCF 02,F0FFA: BRA 10000FFC: MOVF 02,F0FFE: BZ 100C1000: MOVFF FE6,FEE1004: DECFSZ 01,F1006: BRA 10001008: DECFSZ 02,F100A: BRA 1000........................................ return(usb_flush_in(endpoint, len, tgl));100C: MOVFF 73A,7421010: MOVFF 73E,7441014: MOVFF 73D,7431018: MOVFF 73F,745101C: MOVLB 0101E: RCALL 0D5C1020: MOVF 01,W1022: BRA 102A.................... }.................... else1024: BRA 1026.................... {.................... //putc('-');.................... //printf("%X", EP_BDxST_I(endpoint));.................... debug_usb(debug_putc,"\r\nPUT ERR");.................... }........................................ return(0);1026: MOVLW 001028: MOVWF 01.................... }102A: RETLW 00........................................ // see pic18_usb.h for documentation.................... void usb_flush_out(int8 endpoint, USB_DTS_BIT tgl).................... {.................... int8 i;.................... int16 len;........................................ #if USB_IGNORE_RX_DTS.................... if (tgl == USB_DTS_STALL).................... {.................... debug_usb(debug_putc, '*');.................... EP_BDxCNT_O(endpoint) = 0x84;.................... EP_BDxST_I(endpoint) = 0x84;.................... return;.................... }.................... else.................... i=0x80;.................... #else.................... i = EP_BDxST_O(endpoint);*0BEA: MOVLB 70BEC: CLRF x490BEE: MOVFF 6FD,7480BF2: CLRF x4B0BF4: MOVLW 080BF6: MOVWF x4A0BF8: MOVLB 00BFA: CALL 02BA0BFE: MOVLB 70C00: MOVFF 01,FE90C04: MOVLW 040C06: ADDWF 02,W0C08: MOVWF FEA0C0A: MOVFF FEF,6FF0C0E: MOVLB 6.................... if (tgl == USB_DTS_TOGGLE)0C10: MOVF xFE,W0C12: SUBLW 020C14: BNZ 0C22.................... {.................... if (bit_test(i,6))0C16: BTFSS xFF.60C18: BRA 0C1E.................... tgl = USB_DTS_DATA0; //was DATA1, goto DATA00C1A: CLRF xFE.................... else0C1C: BRA 0C22.................... tgl = USB_DTS_DATA1; //was DATA0, goto DATA10C1E: MOVLW 010C20: MOVWF xFE.................... }.................... if (tgl == USB_DTS_STALL)0C22: MOVF xFE,W0C24: SUBLW 030C26: BNZ 0C64.................... {.................... i = 0x84;0C28: MOVLW 840C2A: MOVWF xFF.................... EP_BDxST_I(endpoint) = 0x84; //stall both in and out endpoints0C2C: MOVLB 70C2E: CLRF x490C30: MOVFF 6FD,7480C34: CLRF x4B0C36: MOVLW 080C38: MOVWF x4A0C3A: MOVLB 00C3C: CALL 02BA0C40: MOVFF 02,7030C44: MOVFF 01,7020C48: MOVLW 040C4A: MOVLB 70C4C: ADDWF x02,F0C4E: MOVLW 000C50: ADDWFC x03,F0C52: MOVFF 702,FE90C56: MOVLW 040C58: ADDWF x03,W0C5A: MOVWF FEA0C5C: MOVLW 840C5E: MOVWF FEF.................... }.................... else if (tgl == USB_DTS_DATA1)0C60: BRA 0C740C62: MOVLB 60C64: DECFSZ xFE,W0C66: BRA 0C6E.................... i = 0xC8; //DATA1, UOWN0C68: MOVLW C80C6A: MOVWF xFF.................... else //if (tgl == USB_DTS_DATA0)0C6C: BRA 0C72.................... i = 0x88; //DATA0, UOWN0C6E: MOVLW 880C70: MOVWF xFF0C72: MOVLB 7.................... #endif........................................ //bit_clear(__usb_kbhit_status,endpoint);........................................ len = usb_ep_rx_size[endpoint];0C74: BCF FD8.00C76: MOVLB 60C78: RLCF xFD,W0C7A: CLRF 030C7C: MOVFF FF2,7020C80: BCF FF2.70C82: MOVLB 00C84: CALL 011A0C88: TBLRD*+0C8A: MOVFF FF5,030C8E: MOVLB 70C90: BTFSC x02.70C92: BSF FF2.70C94: MOVWF x000C96: MOVFF 03,701.................... EP_BDxCNT_O(endpoint) = len;0C9A: CLRF x490C9C: MOVFF 6FD,7480CA0: CLRF x4B0CA2: MOVLW 080CA4: MOVWF x4A0CA6: MOVLB 00CA8: CALL 02BA0CAC: MOVFF 01,7020CB0: MOVLW 010CB2: MOVLB 70CB4: ADDWF 01,W0CB6: MOVWF 010CB8: MOVLW 000CBA: ADDWFC 02,W0CBC: MOVWF 030CBE: MOVFF 01,FE90CC2: MOVLW 040CC4: ADDWF 03,W0CC6: MOVWF FEA0CC8: MOVFF 700,FEF.................... if (bit_test(len,8)) {bit_set(i,0);}0CCC: BTFSS x01.00CCE: BRA 0CD60CD0: MOVLB 60CD2: BSF xFF.00CD4: MOVLB 7.................... if (bit_test(len,9)) {bit_set(i,1);}0CD6: BTFSS x01.10CD8: BRA 0CE00CDA: MOVLB 60CDC: BSF xFF.10CDE: MOVLB 7........................................ EP_BDxST_O(endpoint) = i;0CE0: CLRF x490CE2: MOVFF 6FD,7480CE6: CLRF x4B0CE8: MOVLW 080CEA: MOVWF x4A0CEC: MOVLB 00CEE: CALL 02BA0CF2: MOVLB 70CF4: MOVFF 01,FE90CF8: MOVLW 040CFA: ADDWF 02,W0CFC: MOVWF FEA0CFE: MOVFF 6FF,FEF.................... }0D02: MOVLB 00D04: RETLW 00........................................ // see pic18_usb.h for documentation.................... int16 usb_rx_packet_size(int8 endpoint).................... {.................... return(EP_BDxCNT_O(endpoint));*0EE6: MOVLB 70EE8: CLRF x490EEA: MOVFF 6FE,7480EEE: CLRF x4B0EF0: MOVLW 080EF2: MOVWF x4A0EF4: MOVLB 00EF6: CALL 02BA0EFA: MOVFF 02,7000EFE: MOVFF 01,6FF0F02: MOVLW 010F04: MOVLB 60F06: ADDWF 01,W0F08: MOVWF 010F0A: MOVLW 000F0C: MOVLB 70F0E: ADDWFC 02,W0F10: MOVWF 030F12: MOVFF 01,FE90F16: MOVLW 040F18: ADDWF 03,W0F1A: MOVWF FEA0F1C: CLRF 030F1E: MOVFF FEF,010F22: MOVFF 03,02.................... }0F26: MOVLB 00F28: GOTO 0F3A (RETURN)........................................ /// END User Functions............................................................ /// BEGIN Hardware layer functions required by USB......................................... /*****************************************************************************.................... /* usb_get_packet_buffer(endpoint, *ptr, max).................... /*.................... /* Input: endpoint - endpoint to get data from.................... /* ptr - where to save data to local PIC RAM.................... /* max - max amount of data to receive from buffer.................... /*.................... /* Output: the amount of data taken from the buffer..................... /*.................... /* Summary: Gets a packet of data from the USB buffer and puts into local PIC.................... /* RAM..................... /* Does not mark the endpoint as ready for more data. Once you are.................... /* done with data, call usb_flush_out() to mark the endpoint ready.................... /* to receive more data..................... /*.................... /*****************************************************************************/.................... static int16 usb_get_packet_buffer(int8 endpoint, int8 *ptr, int16 max).................... {.................... int8 * al;.................... int8 st;.................... int16 i;........................................ al = EP_BDxADR_O(endpoint);.................... i = EP_BDxCNT_O(endpoint);.................... st = EP_BDxST_O(endpoint);........................................ //read BC8 and BC9.................... if (bit_test(st,0)) {bit_set(i,8);}.................... if (bit_test(st,1)) {bit_set(i,9);}........................................ if (i < max) {max = i;}........................................ memcpy(ptr, al ,max);........................................ return(max);.................... }........................................ // see usb_hw_layer.h for documentation.................... unsigned int16 usb_get_packet(int8 endpoint, int8 * ptr, unsigned int16 max).................... {.................... max = usb_get_packet_buffer(endpoint, ptr, max);.................... usb_flush_out(endpoint, USB_DTS_TOGGLE);........................................ return(max);.................... }........................................ // see usb_hw_layer.h for documentation.................... void usb_stall_ep(int8 endpoint).................... {.................... int1 direction;........................................ direction = bit_test(endpoint,7);*09B4: MOVLB 609B6: BCF xFE.009B8: BTFSC xFD.709BA: BSF xFE.0.................... endpoint &= 0x7F;09BC: BCF xFD.7........................................ if (direction)09BE: BTFSS xFE.009C0: BRA 09FC.................... {.................... EP_BDxST_I(endpoint) = 0x84;09C2: MOVLB 709C4: CLRF x4909C6: MOVFF 6FD,74809CA: CLRF x4B09CC: MOVLW 0809CE: MOVWF x4A09D0: MOVLB 009D2: RCALL 02BA09D4: MOVFF 02,70009D8: MOVFF 01,6FF09DC: MOVLW 0409DE: MOVLB 609E0: ADDWF xFF,F09E2: MOVLW 0009E4: MOVLB 709E6: ADDWFC x00,F09E8: MOVFF 6FF,FE909EC: MOVLW 0409EE: MOVLB 709F0: ADDWF x00,W09F2: MOVWF FEA09F4: MOVLW 8409F6: MOVWF FEF.................... }.................... else09F8: BRA 0A1E09FA: MOVLB 6.................... {.................... EP_BDxST_O(endpoint) = 0x84;09FC: MOVLB 709FE: CLRF x490A00: MOVFF 6FD,7480A04: CLRF x4B0A06: MOVLW 080A08: MOVWF x4A0A0A: MOVLB 00A0C: RCALL 02BA0A0E: MOVFF 01,FE90A12: MOVLW 040A14: MOVLB 70A16: ADDWF 02,W0A18: MOVWF FEA0A1A: MOVLW 840A1C: MOVWF FEF.................... }.................... }0A1E: MOVLB 00A20: GOTO 0AD6 (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_unstall_ep(int8 endpoint).................... {.................... int1 direction;........................................ direction = bit_test(endpoint,7);*0946: MOVLB 60948: BCF xFE.0094A: BTFSC xFD.7094C: BSF xFE.0.................... endpoint &= 0x7F;094E: BCF xFD.7........................................ if (direction)0950: BTFSS xFE.00952: BRA 098E.................... {.................... #if USB_IGNORE_RX_DTS.................... EP_BDxST_I(endpoint) = 0x80;.................... #else.................... EP_BDxST_I(endpoint) = 0x88;0954: MOVLB 70956: CLRF x490958: MOVFF 6FD,748095C: CLRF x4B095E: MOVLW 080960: MOVWF x4A0962: MOVLB 00964: RCALL 02BA0966: MOVFF 02,700096A: MOVFF 01,6FF096E: MOVLW 040970: MOVLB 60972: ADDWF xFF,F0974: MOVLW 000976: MOVLB 70978: ADDWFC x00,F097A: MOVFF 6FF,FE9097E: MOVLW 040980: MOVLB 70982: ADDWF x00,W0984: MOVWF FEA0986: MOVLW 880988: MOVWF FEF.................... #endif.................... }.................... else098A: BRA 09AE098C: MOVLB 6.................... {.................... EP_BDxST_O(endpoint) = 0x00;098E: MOVLB 70990: CLRF x490992: MOVFF 6FD,7480996: CLRF x4B0998: MOVLW 08099A: MOVWF x4A099C: MOVLB 0099E: RCALL 02BA09A0: MOVFF 01,FE909A4: MOVLW 0409A6: MOVLB 709A8: ADDWF 02,W09AA: MOVWF FEA09AC: CLRF FEF.................... }.................... }09AE: MOVLB 009B0: GOTO 0AC6 (RETURN)........................................ // see usb_hw_layer.h for documentation.................... int1 usb_endpoint_stalled(int8 endpoint).................... {.................... int1 direction;.................... int8 st;........................................ direction = bit_test(endpoint,7);*0A24: MOVLB 60A26: BCF xFE.00A28: BTFSC xFD.70A2A: BSF xFE.0.................... endpoint &= 0x7F;0A2C: BCF xFD.7........................................ if (direction)0A2E: BTFSS xFE.00A30: BRA 0A68.................... {.................... st=EP_BDxST_I(endpoint);0A32: MOVLB 70A34: CLRF x490A36: MOVFF 6FD,7480A3A: CLRF x4B0A3C: MOVLW 080A3E: MOVWF x4A0A40: MOVLB 00A42: RCALL 02BA0A44: MOVFF 02,7010A48: MOVFF 01,7000A4C: MOVLW 040A4E: MOVLB 70A50: ADDWF x00,F0A52: MOVLW 000A54: ADDWFC x01,F0A56: MOVFF 700,FE90A5A: MOVLW 040A5C: ADDWF x01,W0A5E: MOVWF FEA0A60: MOVFF FEF,6FF0A64: MOVLB 6.................... }.................... else0A66: BRA 0A8C.................... {.................... st=EP_BDxST_O(endpoint);0A68: MOVLB 70A6A: CLRF x490A6C: MOVFF 6FD,7480A70: CLRF x4B0A72: MOVLW 080A74: MOVWF x4A0A76: MOVLB 00A78: RCALL 02BA0A7A: MOVLB 70A7C: MOVFF 01,FE90A80: MOVLW 040A82: ADDWF 02,W0A84: MOVWF FEA0A86: MOVFF FEF,6FF0A8A: MOVLB 6.................... }........................................ return(bit_test(st,7) && bit_test(st,2));0A8C: BTFSS xFF.70A8E: BRA 0A940A90: BTFSC xFF.20A92: BRA 0A980A94: MOVLW 000A96: BRA 0A9A0A98: MOVLW 010A9A: MOVWF 01.................... }0A9C: MOVLB 00A9E: GOTO 0AEE (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_set_address(int8 address).................... {.................... UADDR = address;*0F60: MOVFF 6FE,F6E........................................ if (address)0F64: MOVLB 60F66: MOVF xFE,F0F68: BZ 0F70.................... {.................... usb_state = USB_STATE_ADDRESS;0F6A: MOVLW 040F6C: MOVWF 24.................... }.................... else0F6E: BRA 0F74.................... {.................... usb_state = USB_STATE_POWERED;0F70: MOVLW 020F72: MOVWF 24.................... }.................... }0F74: MOVLB 00F76: GOTO 0F84 (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_set_configured(int8 config).................... {.................... int8 en;.................... int16 addy;.................... int8 new_uep;.................... int16 len;.................... int8 i;........................................ if (config == 0)*055C: MOVLB 6055E: MOVF xFD,F0560: BNZ 056E.................... {.................... // if config=0 then set addressed state.................... usb_state = USB_STATE_ADDRESS;0562: MOVLW 040564: MOVWF 24.................... usb_disable_endpoints();0566: MOVLB 00568: RCALL 033E.................... }.................... else056A: BRA 0786056C: MOVLB 6.................... {.................... // else set configed state.................... usb_state = USB_STATE_CONFIGURED;056E: MOVLW 050570: MOVWF 24.................... addy = (int16)USB_DATA_BUFFER_LOCATION+(2*USB_MAX_EP0_PACKET_LENGTH);0572: MOVLW 040574: MOVLB 70576: MOVWF x000578: MOVLW 98057A: MOVLB 6057C: MOVWF xFF.................... for (en=1; en<16; en++)057E: MOVLW 010580: MOVWF xFE0582: MOVF xFE,W0584: SUBLW 0F0586: BTFSS FD8.00588: BRA 0786.................... {.................... // enable and config endpoints based upon user configuration.................... usb_disable_endpoint(en);058A: MOVFF 6FE,706058E: MOVLB 00590: RCALL 02DC.................... new_uep = 0;0592: MOVLB 70594: CLRF x01.................... if (usb_ep_rx_type[en] != USB_ENABLE_DISABLED)0596: CLRF 030598: MOVLB 6059A: MOVF xFE,W059C: MOVFF FF2,70505A0: BCF FF2.705A2: MOVLB 005A4: RCALL 00CA05A6: MOVLB 705A8: BTFSC x05.705AA: BSF FF2.705AC: SUBLW FF05AE: BZ 0692.................... {.................... new_uep = 0x04;05B0: MOVLW 0405B2: MOVWF x01.................... len = usb_ep_rx_size[en];05B4: BCF FD8.005B6: MOVLB 605B8: RLCF xFE,W05BA: CLRF 0305BC: MOVFF FF2,70505C0: BCF FF2.705C2: MOVLB 005C4: RCALL 011A05C6: TBLRD*+05C8: MOVFF FF5,0305CC: MOVLB 705CE: BTFSC x05.705D0: BSF FF2.705D2: MOVWF x0205D4: MOVFF 03,703.................... EP_BDxCNT_O(en) = len;05D8: CLRF x4905DA: MOVFF 6FE,74805DE: CLRF x4B05E0: MOVLW 0805E2: MOVWF x4A05E4: MOVLB 005E6: RCALL 02BA05E8: MOVFF 01,70505EC: MOVLW 0105EE: MOVLB 705F0: ADDWF 01,W05F2: MOVWF 0105F4: MOVLW 0005F6: ADDWFC 02,W05F8: MOVWF 0305FA: MOVFF 01,FE905FE: MOVLW 040600: ADDWF 03,W0602: MOVWF FEA0604: MOVFF 702,FEF.................... EP_BDxADR_O(en) = addy;0608: CLRF x49060A: MOVFF 6FE,748060E: CLRF x4B0610: MOVLW 080612: MOVWF x4A0614: MOVLB 00616: RCALL 02BA0618: MOVFF 01,705061C: MOVLW 02061E: MOVLB 70620: ADDWF 01,W0622: MOVWF 010624: MOVLW 000626: ADDWFC 02,W0628: MOVWF 03062A: MOVFF 01,FE9062E: MOVLW 040630: ADDWF 03,W0632: MOVWF FEA0634: MOVFF 700,FEC0638: MOVF FED,F063A: MOVFF 6FF,FEF.................... addy += usb_ep_rx_size[en];063E: BCF FD8.00640: MOVLB 60642: RLCF xFE,W0644: CLRF 030646: MOVFF FF2,705064A: BCF FF2.7064C: MOVLB 0064E: RCALL 011A0650: TBLRD*+0652: MOVFF FF5,030656: MOVLB 70658: BTFSC x05.7065A: BSF FF2.7065C: MOVLB 6065E: ADDWF xFF,F0660: MOVF 03,W0662: MOVLB 70664: ADDWFC x00,F.................... #if USB_IGNORE_RX_DTS.................... i = 0x80;.................... #else.................... i = 0x88;0666: MOVLW 880668: MOVWF x04.................... #endif.................... if (bit_test(len,8)) {bit_set(i,0);}066A: BTFSC x03.0066C: BSF x04.0.................... if (bit_test(len,9)) {bit_set(i,1);}066E: BTFSC x03.10670: BSF x04.1.................... EP_BDxST_O(en) = i;0672: CLRF x490674: MOVFF 6FE,7480678: CLRF x4B067A: MOVLW 08067C: MOVWF x4A067E: MOVLB 00680: RCALL 02BA0682: MOVLB 70684: MOVFF 01,FE90688: MOVLW 04068A: ADDWF 02,W068C: MOVWF FEA068E: MOVFF 704,FEF.................... }.................... if (usb_ep_tx_type[en] != USB_ENABLE_DISABLED)0692: CLRF 030694: MOVLB 60696: MOVF xFE,W0698: MOVFF FF2,705069C: BCF FF2.7069E: MOVLB 006A0: RCALL 00AA06A2: MOVLB 706A4: BTFSC x05.706A6: BSF FF2.706A8: SUBLW FF06AA: BZ 0748.................... {.................... new_uep |= 0x02;06AC: BSF x01.1.................... EP_BDxADR_I(en) = addy;06AE: CLRF x4906B0: MOVFF 6FE,74806B4: CLRF x4B06B6: MOVLW 0806B8: MOVWF x4A06BA: MOVLB 006BC: RCALL 02BA06BE: MOVFF 02,70606C2: MOVFF 01,70506C6: MOVLW 0406C8: MOVLB 706CA: ADDWF x05,F06CC: MOVLW 0006CE: ADDWFC x06,F06D0: MOVLW 0206D2: ADDWF x05,W06D4: MOVWF 0106D6: MOVLW 0006D8: ADDWFC x06,W06DA: MOVWF 0306DC: MOVFF 01,FE906E0: MOVLW 0406E2: ADDWF 03,W06E4: MOVWF FEA06E6: MOVFF 700,FEC06EA: MOVF FED,F06EC: MOVFF 6FF,FEF.................... addy += usb_ep_tx_size[en];06F0: BCF FD8.006F2: MOVLB 606F4: RLCF xFE,W06F6: CLRF 0306F8: MOVFF FF2,70506FC: BCF FF2.706FE: MOVLB 00700: RCALL 00EA0702: TBLRD*+0704: MOVFF FF5,030708: MOVLB 7070A: BTFSC x05.7070C: BSF FF2.7070E: MOVLB 60710: ADDWF xFF,F0712: MOVF 03,W0714: MOVLB 70716: ADDWFC x00,F.................... EP_BDxST_I(en) = 0x40;0718: CLRF x49071A: MOVFF 6FE,748071E: CLRF x4B0720: MOVLW 080722: MOVWF x4A0724: MOVLB 00726: RCALL 02BA0728: MOVFF 02,706072C: MOVFF 01,7050730: MOVLW 040732: MOVLB 70734: ADDWF x05,F0736: MOVLW 000738: ADDWFC x06,F073A: MOVFF 705,FE9073E: MOVLW 040740: ADDWF x06,W0742: MOVWF FEA0744: MOVLW 400746: MOVWF FEF.................... }.................... if (new_uep == 0x06) {new_uep = 0x0E;}0748: MOVF x01,W074A: SUBLW 06074C: BNZ 0752074E: MOVLW 0E0750: MOVWF x01.................... if (usb_ep_tx_type[en] != USB_ENABLE_ISOCHRONOUS) {new_uep |= 0x10;}0752: CLRF 030754: MOVLB 60756: MOVF xFE,W0758: MOVFF FF2,705075C: BCF FF2.7075E: MOVLB 00760: RCALL 00AA0762: MOVLB 70764: BTFSC x05.70766: BSF FF2.70768: SUBLW 01076A: BTFSS FD8.2076C: BSF x01.4........................................ UEP(en) = new_uep;076E: CLRF 030770: MOVLB 60772: MOVF xFE,W0774: ADDLW 700776: MOVWF FE90778: MOVLW 0F077A: ADDWFC 03,W077C: MOVWF FEA077E: MOVFF 701,FEF.................... }0782: INCF xFE,F0784: BRA 05820786: MOVLB 0.................... }.................... }0788: GOTO 082C (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_disable_endpoint(int8 en).................... {.................... UEP(en) = ENDPT_DISABLED;*02DC: CLRF 0302DE: MOVLB 702E0: MOVF x06,W02E2: ADDLW 7002E4: MOVWF FE902E6: MOVLW 0F02E8: ADDWFC 03,W02EA: MOVWF FEA02EC: CLRF FEF.................... EP_BDxST_O(en) = 0; //clear state, deque if necessary02EE: CLRF x4902F0: MOVFF 706,74802F4: CLRF x4B02F6: MOVLW 0802F8: MOVWF x4A02FA: MOVLB 002FC: RCALL 02BA02FE: MOVLB 70300: MOVFF 01,FE90304: MOVLW 040306: ADDWF 02,W0308: MOVWF FEA030A: CLRF FEF.................... EP_BDxST_I(en) = 0; //clear state, deque if necessary030C: CLRF x49030E: MOVFF 706,7480312: CLRF x4B0314: MOVLW 080316: MOVWF x4A0318: MOVLB 0031A: RCALL 02BA031C: MOVFF 02,7080320: MOVFF 01,7070324: MOVLW 040326: MOVLB 70328: ADDWF x07,F032A: MOVLW 00032C: ADDWFC x08,F032E: MOVFF 707,FE90332: MOVLW 040334: ADDWF x08,W0336: MOVWF FEA0338: CLRF FEF.................... }033A: MOVLB 0033C: RETLW 00........................................ // see usb_hw_layer.h for documentation.................... void usb_disable_endpoints(void).................... {.................... int8 i;........................................ for (i=1; i<16; i++)033E: MOVLW 010340: MOVLB 70342: MOVWF x050344: MOVF x05,W0346: SUBLW 0F0348: BNC 0358.................... usb_disable_endpoint(i);034A: MOVFF 705,706034E: MOVLB 00350: RCALL 02DC0352: MOVLB 70354: INCF x05,F0356: BRA 0344........................................ //__usb_kbhit_status=0;.................... }0358: MOVLB 0035A: RETLW 00........................................ /// END Hardware layer functions required by USB.C............................................................ /// BEGIN USB Interrupt Service Routine........................................ static void usb_clear_trn(void).................... {.................... UIR_TRN = 0;*03AA: BCF F68.3.................... delay_cycles(6);03AC: BRA 03AE03AE: BRA 03B003B0: BRA 03B2.................... }03B2: RETLW 00........................................ /*****************************************************************************.................... /* usb_handle_interrupt().................... /*.................... /* Summary: Checks the interrupt, and acts upon event. Processing finished.................... /* tokens is the majority of this code, and is handled by usb.c.................... /*.................... /* NOTE: If you wish to change to a polling method (and not an interrupt.................... /* method), then you must call this function rapidly. If there is more.................... /* than 10ms latency the PC may think the USB device is stalled and.................... /* disable it..................... /* To switch to a polling method, remove the #int_usb line above this.................... /* fuction. Also, goto usb_init() and remove the code that enables the.................... /* USB interrupt..................... /******************************************************************************/.................... #int_usb.................... void usb_isr().................... {.................... int8 TRNAttempts;........................................ if (usb_state == USB_STATE_DETACHED) return; //should never happen, though*1274: MOVF 24,F1276: BNZ 127A1278: BRA 12EE.................... if (UIR)127A: MOVF F68,F127C: BZ 12EE.................... {.................... debug_usb(debug_putc,"\r\n\n[%X] ",UIR);........................................ //activity detected. (only enable after sleep).................... if (UIR_ACTV && UIE_ACTV) {usb_isr_activity();}127E: BTFSS F68.21280: BRA 128A1282: BTFSS F69.21284: BRA 128A1286: GOTO 0274........................................ if (UCON_SUSPND) return;128A: BTFSS F6D.1128C: BRA 1290128E: BRA 12EE........................................ if (UIR_STALL && UIE_STALL) {usb_isr_stall();} //a stall handshake was sent1290: BTFSS F68.51292: BRA 129C1294: BTFSS F69.51296: BRA 129C1298: GOTO 02A4........................................ if (UIR_UERR && UIE_UERR) {usb_isr_uerr();} //error has been detected129C: BTFSS F68.1129E: BRA 12A812A0: BTFSS F69.112A2: BRA 12A812A4: GOTO 02B2........................................ if (UIR_URST && UIE_URST) {usb_isr_rst();} //usb reset has been detected12A8: BTFSS F68.012AA: BRA 12B412AC: BTFSS F69.012AE: BRA 12B412B0: GOTO 03B4........................................ if (UIR_IDLE && UIE_IDLE) {usb_isr_uidle();} //idle time, we can go to sleep12B4: BTFSS F68.412B6: BRA 12C012B8: BTFSS F69.412BA: BRA 12C012BC: GOTO 03DE........................................ if (UIR_SOF && UIE_SOF) {usb_isr_sof();}12C0: BTFSS F68.612C2: BRA 12CC12C4: BTFSS F69.612C6: BRA 12CC12C8: GOTO 03E8........................................ TRNAttempts = 0;12CC: MOVLB 612CE: CLRF xFA.................... do.................... {.................... if (UIR_TRN && UIE_TRN)12D0: BTFSS F68.312D2: BRA 12E012D4: BTFSS F69.312D6: BRA 12E0.................... {.................... usb_isr_tok_dne();12D8: MOVLB 012DA: BRA 108A.................... }.................... else12DC: BRA 12E212DE: MOVLB 6.................... break;12E0: BRA 12EC.................... } while (TRNAttempts++ < 4);12E2: MOVLB 612E4: MOVF xFA,W12E6: INCF xFA,F12E8: SUBLW 0312EA: BC 12D012EC: MOVLB 0.................... }.................... }........................................ // SOF interrupt not handled. user must add this depending on application12EE: BCF FA1.512F0: GOTO 0064.................... void usb_isr_sof(void).................... {.................... debug_usb(debug_putc, "\r\nSOF");........................................ UIR_SOF = 0;*03E8: BCF F68.6.................... }03EA: GOTO 12CC (RETURN)........................................ /*****************************************************************************.................... /* usb_isr_rst().................... /*.................... /* Summary: The host (computer) sent us a RESET command. Reset USB device.................... /* and token handler code to initial state..................... /*.................... /******************************************************************************/.................... void usb_isr_rst(void).................... {.................... debug_usb(debug_putc,"R");........................................ UEIR = 0;*03B4: CLRF F6A.................... UIR = 0;03B6: CLRF F68.................... UEIE = 0x9F;03B8: MOVLW 9F03BA: MOVWF F6B.................... UIE = STANDARD_INTS & ~__USB_UIF_ACTIVE;03BC: MOVLW 3D03BE: MOVWF F69........................................ UADDR = 0;03C0: CLRF F6E........................................ usb_disable_endpoints();03C2: RCALL 033E........................................ usb_token_reset();03C4: RCALL 037E........................................ UEP(0) = ENDPT_CONTROL | 0x10;03C6: MOVLW 1603C8: MOVWF F70........................................ while (UIR_TRN).................... {03CA: BTFSS F68.303CC: BRA 03D2.................... usb_clear_trn();03CE: RCALL 03AA.................... }03D0: BRA 03CA........................................ UCON_PKTDIS = 0; //SIE token and packet processing enabled03D2: BCF F6D.4........................................ usb_init_ep0_setup();03D4: RCALL 0284........................................ usb_state = USB_STATE_DEFAULT; //put usb mcu into default state03D6: MOVLW 0303D8: MOVWF 24.................... }03DA: GOTO 12B4 (RETURN)........................................ /*****************************************************************************.................... /* usb_init_ep0_setup().................... /*.................... /* Summary: Configure EP0 to receive setup packets.................... /*.................... /*****************************************************************************/.................... void usb_init_ep0_setup(void).................... {.................... EP_BDxCNT_O(0) = USB_MAX_EP0_PACKET_LENGTH;*0284: MOVLW 400286: MOVLB 40288: MOVWF x01.................... EP_BDxADR_O(0) = USB_DATA_BUFFER_LOCATION;028A: MOVLW 04028C: MOVWF x03028E: MOVLW 180290: MOVWF x02.................... #if USB_IGNORE_RX_DTS.................... EP_BDxST_O(0) = 0x80; //give control to SIE, data toggle synch off.................... #else.................... EP_BDxST_O(0) = 0x88; //give control to SIE, DATA0, data toggle synch on0292: MOVLW 880294: MOVWF x00.................... #endif........................................ EP_BDxST_I(0) = 0;0296: CLRF x04.................... EP_BDxADR_I(0) = USB_DATA_BUFFER_LOCATION + (int16)USB_MAX_EP0_PACKET_LENGTH;0298: MOVLW 04029A: MOVWF x07029C: MOVLW 58029E: MOVWF x06.................... }02A0: MOVLB 002A2: RETLW 00........................................ /*******************************************************************************.................... /* usb_isr_uerr().................... /*.................... /* Summary: The USB peripheral had an error. If user specified, error counter.................... /* will incerement. If having problems check the status of these 8 bytes..................... /*.................... /* NOTE: This code is not enabled by default..................... /********************************************************************************/.................... void usb_isr_uerr(void).................... {.................... #if USB_USE_ERROR_COUNTER.................... int ints;.................... #endif........................................ debug_usb(debug_putc,"E %X ",UEIR);........................................ #if USB_USE_ERROR_COUNTER.................... ints=UEIR & UEIE; //mask off the flags with the ones that are enabled........................................ if ( bit_test(ints,0) ).................... {.................... //increment pid_error counter.................... debug_usb(debug_putc,"PID ");.................... ERROR_COUNTER[0]++;.................... }........................................ if ( bit_test(ints,1) ).................... {.................... //increment crc5 error counter.................... debug_usb(debug_putc,"CRC5 ");.................... ERROR_COUNTER[1]++;.................... }........................................ if ( bit_test(ints,2) ).................... {.................... //increment crc16 error counter.................... debug_usb(debug_putc,"CRC16 ");.................... ERROR_COUNTER[2]++;.................... }........................................ if ( bit_test(ints,3) ).................... {.................... //increment dfn8 error counter.................... debug_usb(debug_putc,"DFN8 ");.................... ERROR_COUNTER[3]++;.................... }........................................ if ( bit_test(ints,4) ).................... {.................... //increment bto error counter.................... debug_usb(debug_putc,"BTO ");.................... ERROR_COUNTER[4]++;.................... }........................................ if ( bit_test(ints,7) ).................... {.................... //increment bts error counter.................... debug_usb(debug_putc,"BTS ");.................... ERROR_COUNTER[5]++;.................... }.................... #endif........................................ UEIR = 0;*02B2: CLRF F6A.................... UIR_UERR = 0;02B4: BCF F68.1.................... }02B6: GOTO 12A8 (RETURN)........................................ /*****************************************************************************.................... /* usb_isr_uidle().................... /*.................... /* Summary: USB peripheral detected IDLE. Put the USB peripheral to sleep..................... /*.................... /*****************************************************************************/.................... void usb_isr_uidle(void).................... {.................... debug_usb(debug_putc, "I");........................................ UIE_ACTV = 1; //enable activity interrupt flag. (we are now suspended until we get an activity interrupt. nice)*03DE: BSF F69.2.................... UIR_IDLE = 0; //clear idle interrupt flag03E0: BCF F68.4.................... UCON_SUSPND = 1; //set suspend. we are now suspended03E2: BSF F6D.1.................... }03E4: GOTO 12C0 (RETURN)............................................................ /******************************************************************************.................... /* usb_isr_activity().................... /*.................... /* Summary: USB peripheral detected activity on the USB device. Wake-up the USB.................... /* peripheral..................... /*.................... /*****************************************************************************/.................... void usb_isr_activity(void).................... {.................... debug_usb(debug_putc, "A");........................................ UCON_SUSPND = 0; //turn off low power suspending*0274: BCF F6D.1.................... UIE_ACTV = 0; //clear activity interupt enabling0276: BCF F69.2........................................ while(UIR_ACTV).................... UIR_ACTV = 0;0278: BTFSS F68.2027A: BRA 0280027C: BCF F68.2027E: BRA 0278.................... }0280: GOTO 128A (RETURN)........................................ /******************************************************************************.................... /* usb_isr_stall().................... /*.................... /* Summary: Stall handshake detected..................... /*.................... /*****************************************************************************/.................... void usb_isr_stall(void).................... {.................... debug_usb(debug_putc, "S");............................................................ if (bit_test(UEP(0),0))*02A4: BTFSS F70.002A6: BRA 02AC.................... {.................... usb_init_ep0_setup();02A8: RCALL 0284.................... bit_clear(UEP(0), 0);02AA: BCF F70.0.................... }........................................ UIR_STALL = 0;02AC: BCF F68.5.................... }02AE: GOTO 129C (RETURN)........................................ // see usb_hw_layer.h for documentation.................... void usb_request_send_response(unsigned int8 len) {__setup_0_tx_size = len;}*03EE: MOVFF 700,2503F2: RETLW 00.................... void usb_request_get_data(void) {__setup_0_tx_size = 0xFE;}*0B0C: MOVLW FE0B0E: MOVWF 250B10: RETLW 00.................... void usb_request_stall(void) {__setup_0_tx_size = 0xFF;}*03F4: MOVLW FF03F6: MOVWF 2503F8: RETLW 00........................................ /*****************************************************************************.................... /* usb_isr_tok_dne().................... /*.................... /* Summary: A Token (IN/OUT/SETUP) has been received by the USB peripheral..................... /* If a setup token on EP0 was received, run the chapter 9 code and.................... /* handle the request..................... /* If an IN token on EP0 was received, continue transmitting any.................... /* unfinished requests that may take more than one packet to transmit.................... /* (if necessary)..................... /* If an OUT token on any other EP was received, mark that EP as ready.................... /* for a usb_get_packet()..................... /* Does not handle any IN or OUT tokens on EP0..................... /*.................... /*****************************************************************************/.................... void usb_isr_tok_dne(void).................... {.................... int8 en;........................................ en = USTAT>>3;*108A: RRCF F6C,W108C: MOVLB 6108E: MOVWF xFB1090: RRCF xFB,F1092: RRCF xFB,F1094: MOVLW 1F1096: ANDWF xFB,F........................................ debug_usb(debug_putc, "T ");.................... debug_usb(debug_putc, "%X ", USTAT);........................................ if (USTAT == USTAT_OUT_SETUP_E0)1098: MOVF F6C,F109A: BNZ 1138.................... {.................... //new out or setup token in the buffer.................... int8 pidKey;........................................ debug_usb(debug_putc,"%X ", EP_BDxST_O(0));........................................ pidKey = EP_BDxST_O(0) & 0x3C; //save PID109C: MOVLB 4109E: MOVF x00,W10A0: ANDLW 3C10A2: MOVLB 610A4: MOVWF xFC........................................ EP_BDxST_O(0) &= 0x43; //clear pid, prevent bdstal/pid confusion10A6: MOVLW 4310A8: MOVLB 410AA: ANDWF x00,F.................... usb_clear_trn();10AC: MOVLB 010AE: CALL 03AA........................................ if (pidKey == USB_PIC_PID_SETUP)10B2: MOVLB 610B4: MOVF xFC,W10B6: SUBLW 3410B8: BNZ 10FE.................... {.................... EP_BDxST_I(0)=0; // return the in buffer to us (dequeue any pending requests)10BA: MOVLB 410BC: CLRF x04........................................ debug_usb(debug_putc,"(%U) ", EP_BDxCNT_O(0));.................... debug_display_ram(EP_BDxCNT_O(0), usb_ep0_rx_buffer);........................................ usb_isr_tok_setup_dne();10BE: MOVLB 010C0: BRA 0BC0........................................ UCON_PKTDIS=0; // UCON,PKT_DIS ; Assuming there is nothing to dequeue, clear the packet disable bit10C2: BCF F6D.4........................................ //if setup_0_tx_size==0xFF - stall ep0 (unhandled request) (see usb_request_stall()).................... //if setup_0_tx_size==0xFE - get EP0OUT ready for a data packet, leave EP0IN alone (see usb_request_get_data()).................... //else setup_0_tx_size=size of response, get EP0OUT ready for a setup packet, mark EPOIN ready for transmit (see usb_request_send_response()).................... if (__setup_0_tx_size == 0xFF)10C4: INCFSZ 25,W10C6: BRA 10D6.................... usb_flush_out(0, USB_DTS_STALL);10C8: MOVLB 610CA: CLRF xFD10CC: MOVLW 0310CE: MOVWF xFE10D0: MOVLB 010D2: RCALL 0BEA.................... else10D4: BRA 10FA.................... {.................... usb_flush_out(0, USB_DTS_TOGGLE);10D6: MOVLB 610D8: CLRF xFD10DA: MOVLW 0210DC: MOVWF xFE10DE: MOVLB 010E0: RCALL 0BEA.................... if (__setup_0_tx_size != 0xFE)10E2: MOVF 25,W10E4: SUBLW FE10E6: BZ 10FA.................... usb_flush_in(0 ,__setup_0_tx_size, USB_DTS_USERX);10E8: MOVLB 710EA: CLRF x4210EC: CLRF x4410EE: MOVFF 25,74310F2: MOVLW 0410F4: MOVWF x4510F6: MOVLB 010F8: RCALL 0D5C.................... }.................... //why was this here?.................... //UCON_PKTDIS=0; // UCON,PKT_DIS ; Assuming there is nothing to dequeue, clear the packet disable bit.................... }.................... else if (pidKey == USB_PIC_PID_OUT)10FA: BRA 113410FC: MOVLB 610FE: MOVF xFC,W1100: SUBLW 041102: BNZ 1136.................... {.................... usb_isr_tok_out_dne(0);1104: CLRF xFD1106: MOVLB 01108: RCALL 0F42.................... usb_flush_out(0, USB_DTS_TOGGLE);110A: MOVLB 6110C: CLRF xFD110E: MOVLW 021110: MOVWF xFE1112: MOVLB 01114: RCALL 0BEA.................... if ((__setup_0_tx_size!=0xFE) && (__setup_0_tx_size!=0xFF))1116: MOVF 25,W1118: SUBLW FE111A: BZ 1134111C: INCFSZ 25,W111E: BRA 11221120: BRA 1134.................... {.................... usb_flush_in(0,__setup_0_tx_size,USB_DTS_DATA1); //send response (usually a 0len)1122: MOVLB 71124: CLRF x421126: CLRF x441128: MOVFF 25,743112C: MOVLW 01112E: MOVWF x451130: MOVLB 01132: RCALL 0D5C1134: MOVLB 6.................... }.................... }.................... }.................... else if (USTAT == USTAT_IN_E0)1136: BRA 126E1138: MOVF F6C,W113A: SUBLW 04113C: BNZ 1178.................... {.................... //pic -> host transfer completed.................... EP_BDxST_I(0) = EP_BDxST_I(0) & 0x43; //clear up any BDSTAL confusion113E: MOVLW 431140: MOVLB 41142: ANDWF x04,F.................... usb_clear_trn();1144: MOVLB 01146: CALL 03AA.................... __setup_0_tx_size = 0xFF;114A: MOVLW FF114C: MOVWF 25.................... usb_isr_tok_in_dne(0);114E: MOVLB 61150: CLRF xFD1152: MOVLB 01154: RCALL 1058.................... if (__setup_0_tx_size!=0xFF)1156: INCFSZ 25,W1158: BRA 115C115A: BRA 1170.................... usb_flush_in(0, __setup_0_tx_size, USB_DTS_TOGGLE);115C: MOVLB 7115E: CLRF x421160: CLRF x441162: MOVFF 25,7431166: MOVLW 021168: MOVWF x45116A: MOVLB 0116C: RCALL 0D5C.................... else116E: BRA 1174.................... usb_init_ep0_setup();1170: CALL 0284.................... }.................... else1174: BRA 126C1176: MOVLB 6.................... {.................... if (!bit_test(USTAT, 2))1178: BTFSC F6C.2117A: BRA 11E6.................... {.................... EP_BDxST_O(en) = EP_BDxST_O(en) & 0x43; //clear up any BDSTAL confusion117C: MOVLB 7117E: CLRF x491180: MOVFF 6FB,7481184: CLRF x4B1186: MOVLW 081188: MOVWF x4A118A: MOVLB 0118C: CALL 02BA1190: MOVFF 01,6FD1194: MOVLB 61196: MOVFF 01,01119A: MOVLW 04119C: ADDWF 02,W119E: MOVWF 0311A0: MOVFF 01,6FD11A4: MOVWF xFE11A6: MOVLB 711A8: CLRF x4911AA: MOVFF 6FB,74811AE: CLRF x4B11B0: MOVLW 0811B2: MOVWF x4A11B4: MOVLB 011B6: CALL 02BA11BA: MOVFF 01,FE911BE: MOVLW 0411C0: MOVLB 711C2: ADDWF 02,W11C4: MOVWF FEA11C6: MOVF FEF,W11C8: ANDLW 4311CA: MOVLB 611CC: MOVFF 6FE,FEA11D0: MOVFF 6FD,FE911D4: MOVWF FEF.................... usb_clear_trn();11D6: MOVLB 011D8: CALL 03AA.................... usb_isr_tok_out_dne(en);11DC: MOVFF 6FB,6FD11E0: RCALL 0F42.................... }.................... else11E2: BRA 126C11E4: MOVLB 6.................... {.................... EP_BDxST_I(en) = EP_BDxST_I(en) & 0x43; //clear up any BDSTAL confusion11E6: MOVLB 711E8: CLRF x4911EA: MOVFF 6FB,74811EE: CLRF x4B11F0: MOVLW 0811F2: MOVWF x4A11F4: MOVLB 011F6: CALL 02BA11FA: MOVFF 02,6FE11FE: MOVFF 01,6FD1202: MOVLW 041204: MOVLB 61206: ADDWF xFD,F1208: MOVLW 00120A: ADDWFC xFE,F120C: MOVFF 6FD,011210: MOVLW 041212: ADDWF xFE,W1214: MOVWF 031216: MOVFF 6FD,6FD121A: MOVWF xFE121C: MOVLB 7121E: CLRF x491220: MOVFF 6FB,7481224: CLRF x4B1226: MOVLW 081228: MOVWF x4A122A: MOVLB 0122C: CALL 02BA1230: MOVFF 02,7001234: MOVFF 01,6FF1238: MOVLW 04123A: MOVLB 6123C: ADDWF xFF,F123E: MOVLW 001240: MOVLB 71242: ADDWFC x00,F1244: MOVFF 6FF,FE91248: MOVLW 04124A: MOVLB 7124C: ADDWF x00,W124E: MOVWF FEA1250: MOVF FEF,W1252: ANDLW 431254: MOVLB 61256: MOVFF 6FE,FEA125A: MOVFF 6FD,FE9125E: MOVWF FEF.................... usb_clear_trn();1260: MOVLB 01262: CALL 03AA.................... usb_isr_tok_in_dne(en);1266: MOVFF 6FB,6FD126A: RCALL 1058126C: MOVLB 6.................... }.................... }.................... }126E: MOVLB 01270: GOTO 12DC (RETURN)........................................ /// END USB Interrupt Service Routine........................................ #ENDIF........................................ #endif........................................ #if defined(__PIC24_USB_H__).................... #include <pic24_usb.c>.................... #endif........................................ #if defined(__USBN960X_H__).................... #include <usbn960x.c>.................... #endif............................................................ #IFNDEF __USB_HARDWARE__.................... #ERROR You must include USB hardware driver..................... #ENDIF........................................ #IFNDEF __USB_DESCRIPTORS__.................... #ERROR You must include USB descriptors..................... #ENDIF........................................ TYPE_USB_STACK_STATUS USB_stack_status;........................................ int8 USB_address_pending; //save previous state because packets can take several isrs.................... int8 usb_getdesc_ptr; unsigned int8 usb_getdesc_len=0; //for reading string and config descriptors........................................ #IF USB_HID_BOOT_PROTOCOL.................... int8 hid_protocol[USB_NUM_HID_INTERFACES];.................... #ENDIF........................................ void usb_put_0len_0(void);.................... void usb_match_registers(int8 endpoint, int16 *status, int16 *buffer, int8 *size);........................................ void usb_isr_tkn_setup_StandardEndpoint(void);.................... void usb_isr_tkn_setup_StandardDevice(void);.................... void usb_isr_tkn_setup_StandardInterface(void);.................... #IF USB_HID_DEVICE.................... void usb_isr_tkn_setup_ClassInterface(void);.................... #ENDIF.................... void usb_Get_Descriptor(void);.................... void usb_copy_desc_seg_to_ep(void);.................... void usb_finish_set_address(void);........................................ int8 USB_Interface[USB_MAX_NUM_INTERFACES]; //config state for all of our interfaces, NUM_INTERFACES defined with descriptors........................................ /// BEGIN User Functions........................................ // see usb.h for documentation.................... int1 usb_enumerated(void).................... {.................... return(USB_stack_status.curr_config);.................... }........................................ // see usb.h for documentation.................... void usb_wait_for_enumeration(void).................... {.................... while (USB_stack_status.curr_config == 0) {restart_wdt();}.................... }........................................ // see USB.H for documentation.................... int1 usb_puts(int8 endpoint, int8 * ptr, unsigned int16 len, unsigned int8 timeout) {.................... unsigned int16 i=0;.................... int1 res;.................... unsigned int16 this_packet_len;.................... unsigned int16 packet_size;.................... unsigned int32 timeout_1us;........................................ packet_size = usb_ep_tx_size[endpoint];........................................ //printf("\r\nUSB PUTS %U LEN=%LU MAX_PACK=%LU\r\n", endpoint, len, packet_size);........................................ //send data packets until timeout or no more packets to send.................... while (i < len).................... {.................... timeout_1us = (int32)timeout*1000;.................... if ((len - i) > packet_size) {this_packet_len = packet_size;}.................... else {this_packet_len = len-i;}.................... //putc('*');.................... do.................... {.................... res = usb_put_packet(endpoint, ptr + i, this_packet_len, USB_DTS_TOGGLE); //send 64 byte packets.................... //putc('.');.................... if (!res).................... {.................... delay_us(1);.................... //delay_ms(500);.................... timeout_1us--;.................... }.................... } while (!res && timeout_1us);.................... i += packet_size;.................... }............................................................ //send 0len packet if needed.................... if (i==len) {.................... timeout_1us=(int32)timeout*1000;.................... do {.................... res = usb_put_packet(endpoint,0,0,USB_DTS_TOGGLE); //indicate end of message.................... if (!res) {.................... delay_us(1);.................... timeout_1us--;.................... }.................... } while (!res && timeout_1us);.................... }........................................ return(res);.................... }........................................ // see usb.h for documentation.................... unsigned int16 usb_gets(int8 endpoint, int8 * ptr, unsigned int16 max, unsigned int16 timeout) {.................... unsigned int16 ret=0;.................... unsigned int16 to;.................... unsigned int16 len;.................... unsigned int16 packet_size;.................... unsigned int16 this_packet_max;........................................ packet_size=usb_ep_rx_size[endpoint];........................................ do {.................... if (packet_size < max) {this_packet_max=packet_size;} else {this_packet_max=max;}.................... to=0;.................... do {.................... if (usb_kbhit(endpoint)) {.................... len=usb_get_packet(endpoint,ptr,this_packet_max);.................... ptr+=len;.................... max-=len;.................... ret+=len;.................... break;.................... }.................... else {.................... to++;.................... delay_ms(1);.................... }.................... } while (to!=timeout);.................... } while ((len == packet_size) && (to!=timeout) && max);........................................ return(ret);.................... }........................................ /// END User Functions............................................................ /// BEGIN USB Token, standard and HID request handler (part of ISR)........................................ // see usb.h for documentation.................... void usb_token_reset(void).................... {.................... unsigned int i;........................................ for (i=0;i<USB_MAX_NUM_INTERFACES;i++)*037E: MOVLB 70380: CLRF x190382: MOVF x19,W0384: SUBLW 010386: BNC 039C.................... USB_Interface[i] = 0; //reset each interface to default0388: CLRF 03038A: MOVF x19,W038C: ADDLW 29038E: MOVWF FE90390: MOVLW 000392: ADDWFC 03,W0394: MOVWF FEA0396: CLRF FEF........................................ #IF USB_HID_BOOT_PROTOCOL.................... for (i=0;i<USB_NUM_HID_INTERFACES; i++).................... hid_protocol[i] = 1;.................... #endif........................................ #if USB_CDC_DEVICE0398: INCF x19,F039A: BRA 0382.................... usb_cdc_init();039C: MOVLB 0039E: RCALL 035C.................... #endif........................................ USB_stack_status.curr_config = 0; //unconfigured device03A0: CLRF 1E........................................ USB_stack_status.status_device = 1; //previous state. init at none03A2: MOVLW 0103A4: MOVWF 1F.................... USB_stack_status.dev_req = NONE; //previous token request state. init at none03A6: CLRF 1D.................... }03A8: RETLW 00........................................ //send a 0len packet to endpoint 0 (optimization).................... //notice that this doesnt return the status.................... #define usb_put_0len_0() usb_request_send_response(0)........................................ /**************************************************************.................... /* usb_endpoint_is_valid(endpoint).................... /*.................... /* Input: endpoint - endpoint to check..................... /* bit 7 is direction (set is IN, clear is OUT).................... /*.................... /* Output: TRUE if endpoint is valid, FALSE if not.................... /*.................... /* Summary: Checks the dynamic configuration to see if requested.................... /* endpoint is a valid endpoint..................... /***************************************************************/.................... int1 usb_endpoint_is_valid(int8 endpoint).................... {.................... int1 direction;.................... direction = bit_test(endpoint,7);*08E8: MOVLB 608EA: BCF xFE.008EC: BTFSC xFD.708EE: BSF xFE.0.................... endpoint &= 0x7F;08F0: BCF xFD.7.................... if (direction) { //IN08F2: BTFSS xFE.008F4: BRA 091C.................... return(usb_ep_tx_type[endpoint] != USB_ENABLE_DISABLED);08F6: CLRF 0308F8: MOVF xFD,W08FA: MOVFF FF2,6FF08FE: BCF FF2.70900: MOVLB 00902: CALL 00AA0906: MOVLB 60908: BTFSC xFF.7090A: BSF FF2.7090C: SUBLW FF090E: BNZ 09140910: MOVLW 000912: BRA 09160914: MOVLW 010916: MOVWF 010918: BRA 0940.................... }.................... else { //OUT091A: BRA 0940.................... return(usb_ep_rx_type[endpoint] != USB_ENABLE_DISABLED);091C: CLRF 03091E: MOVF xFD,W0920: MOVFF FF2,6FF0924: BCF FF2.70926: MOVLB 00928: CALL 00CA092C: MOVLB 6092E: BTFSC xFF.70930: BSF FF2.70932: SUBLW FF0934: BNZ 093A0936: MOVLW 000938: BRA 093C093A: MOVLW 01093C: MOVWF 01093E: BRA 0940.................... }.................... }0940: MOVLB 00942: GOTO 0AA8 (RETURN)........................................ // see usb.h for documentation.................... void usb_isr_tok_in_dne(int8 endpoint) {.................... if (endpoint==0) {*1058: MOVLB 6105A: MOVF xFD,F105C: BNZ 107A.................... if (USB_stack_status.dev_req == GET_DESCRIPTOR) {usb_copy_desc_seg_to_ep();} //check this, we are missing report descriptor?105E: DECFSZ 1D,W1060: BRA 106C1062: MOVLB 01064: CALL 03FA.................... else if (USB_stack_status.dev_req == SET_ADDRESS) {usb_finish_set_address();}1068: BRA 1076106A: MOVLB 6106C: MOVF 1D,W106E: SUBLW 021070: BNZ 10781072: MOVLB 01074: BRA 0F7A1076: MOVLB 6.................... }.................... #if USB_CDC_DEVICE.................... else if (endpoint==USB_CDC_DATA_IN_ENDPOINT) { //see ex_usb_serial.c example and usb_cdc.h driver1078: BRA 1086107A: MOVF xFD,W107C: SUBLW 02107E: BNZ 1086.................... usb_isr_tok_in_cdc_data_dne();1080: MOVLB 01082: BRA 104E1084: MOVLB 6.................... }.................... #endif.................... }1086: MOVLB 01088: RETLW 00........................................ // see usb.h for documentation.................... void usb_isr_tok_out_dne(int8 endpoint).................... {.................... //TODO:.................... if (endpoint==0) {*0F42: MOVLB 60F44: MOVF xFD,F0F46: BNZ 0F50.................... debug_usb(debug_putc,"TOUT 0 ");.................... #if USB_CDC_DEVICE.................... usb_isr_tok_out_cdc_control_dne();0F48: MOVLB 00F4A: BRA 0E7E.................... #else.................... usb_init_ep0_setup();.................... #endif.................... }.................... #if USB_CDC_DEVICE.................... else if (endpoint==USB_CDC_DATA_OUT_ENDPOINT) { //see ex_usb_serial.c example and usb_cdc.h driver0F4C: BRA 0F5A0F4E: MOVLB 60F50: MOVF xFD,W0F52: SUBLW 020F54: BNZ 0F5C.................... usb_isr_tok_out_cdc_data_dne();0F56: MOVLB 00F58: BRA 0F2C0F5A: MOVLB 6.................... }.................... #endif.................... //else {.................... // bit_set(__usb_kbhit_status,endpoint);.................... //}.................... }0F5C: MOVLB 00F5E: RETLW 00............................................................ //---- process setup message stage -----------//........................................ // see usb.h for documentation.................... void usb_isr_tok_setup_dne(void).................... {.................... USB_stack_status.dev_req=NONE; // clear the device request..*0BC0: CLRF 1D........................................ switch(usb_ep0_rx_buffer[0] & 0x7F) {0BC2: MOVLB 40BC4: MOVF x18,W0BC6: ANDLW 7F0BC8: MOVLB 00BCA: BZ 0BDA0BCC: XORLW 010BCE: BZ 0BDC0BD0: XORLW 030BD2: BZ 0BDE0BD4: XORLW 230BD6: BZ 0BE00BD8: BRA 0BE2........................................ case 0x00: //standard to device.................... debug_usb(debug_putc," d");.................... usb_isr_tkn_setup_StandardDevice();0BDA: BRA 078C.................... break;........................................ case 0x01: //standard to interface.................... debug_usb(debug_putc," i");.................... usb_isr_tkn_setup_StandardInterface();0BDC: BRA 0840.................... break;........................................ case 0x02: //standard to endpoint.................... debug_usb(debug_putc," e");.................... usb_isr_tkn_setup_StandardEndpoint();0BDE: BRA 0AA2.................... break;........................................ #IF USB_HID_DEVICE.................... case 0x21: //class specific request. the only class this driver supports is HID.................... debug_usb(debug_putc," hid");.................... usb_isr_tkn_setup_ClassInterface();.................... break;.................... #endif........................................ #if USB_CDC_DEVICE.................... case 0x21:.................... debug_usb(debug_putc," cdc");.................... usb_isr_tkn_cdc();0BE0: BRA 0B12.................... break;.................... #endif.................... //TODO: IF YOU WANT VENDOR SPECIFC REQUEST SUPPORT YOU MUST ADD IT HERE........................................ default:.................... usb_request_stall();0BE2: RCALL 03F4.................... break;0BE4: BRA 0BE6.................... }.................... }0BE6: GOTO 10C2 (RETURN)........................................ /**************************************************************.................... /* usb_isr_tkn_setup_StandardDevice().................... /*.................... /* Input: usb_ep0_rx_buffer[1] == bRequest.................... /*.................... /* Summary: bmRequestType told us it was a Standard Device request..................... /* bRequest says which request. Only certain requests are valid,.................... /* if a non-valid request was made then return with an Wrong-Statue (IDLE).................... /*.................... /* Part of usb_isr_tok_setup_dne().................... /***************************************************************/.................... void usb_isr_tkn_setup_StandardDevice(void) {.................... switch(usb_ep0_rx_buffer[1]) {*078C: MOVLB 4078E: MOVF x19,W0790: MOVLB 00792: BZ 07AE0794: XORLW 010796: BZ 07C20798: XORLW 02079A: BZ 07DC079C: XORLW 06079E: BZ 07F407A0: XORLW 0307A2: BZ 080607A4: XORLW 0E07A6: BZ 080807A8: XORLW 0107AA: BZ 081807AC: BRA 0838........................................ case USB_STANDARD_REQUEST_GET_STATUS: //0.................... debug_usb(debug_putc,"GS");.................... usb_ep0_tx_buffer[0]=USB_stack_status.status_device;07AE: MOVFF 1F,458.................... usb_ep0_tx_buffer[1]=0;07B2: MOVLB 407B4: CLRF x59.................... usb_request_send_response(2);07B6: MOVLW 0207B8: MOVLB 707BA: MOVWF x0007BC: MOVLB 007BE: RCALL 03EE.................... break;07C0: BRA 083C........................................ case USB_STANDARD_REQUEST_CLEAR_FEATURE: //1.................... if (usb_ep0_rx_buffer[2] == 1) {07C2: MOVLB 407C4: DECFSZ x1A,W07C6: BRA 07D6.................... debug_usb(debug_putc,"CF");.................... USB_stack_status.status_device &= 1;07C8: MOVLW 0107CA: ANDWF 1F,F.................... usb_put_0len_0();07CC: MOVLB 707CE: CLRF x0007D0: MOVLB 007D2: RCALL 03EE.................... }.................... else07D4: BRA 07DA.................... usb_request_stall();07D6: MOVLB 007D8: RCALL 03F4.................... break;07DA: BRA 083C........................................ case USB_STANDARD_REQUEST_SET_FEATURE: //3.................... if (usb_ep0_rx_buffer[2] == 1) {07DC: MOVLB 407DE: DECFSZ x1A,W07E0: BRA 07EE.................... debug_usb(debug_putc,"SF");.................... USB_stack_status.status_device |= 2;07E2: BSF 1F.1.................... usb_put_0len_0();07E4: MOVLB 707E6: CLRF x0007E8: MOVLB 007EA: RCALL 03EE.................... }.................... else07EC: BRA 07F2.................... usb_request_stall();07EE: MOVLB 007F0: RCALL 03F4.................... break;07F2: BRA 083C........................................ case USB_STANDARD_REQUEST_SET_ADDRESS: //5.................... debug_usb(debug_putc,"SA");.................... USB_stack_status.dev_req=SET_ADDRESS; //currently processing set_address request07F4: MOVLW 0207F6: MOVWF 1D.................... USB_address_pending=usb_ep0_rx_buffer[2];07F8: MOVFF 41A,26.................... #ifdef __USBN__ //NATIONAL part handles this differently than pic16c7x5.................... USB_stack_status.dev_req=NONE; //currently processing set_address request.................... usb_set_address(USB_address_pending);.................... USB_stack_status.curr_config=0; // make sure current configuration is 0.................... #endif.................... usb_put_0len_0();07FC: MOVLB 707FE: CLRF x000800: MOVLB 00802: RCALL 03EE.................... break;0804: BRA 083C........................................ case USB_STANDARD_REQUEST_GET_DESCRIPTOR: //6.................... debug_usb(debug_putc,"GD");.................... usb_Get_Descriptor();0806: BRA 049C.................... break;........................................ case USB_STANDARD_REQUEST_GET_CONFIGURATION: //8.................... debug_usb(debug_putc,"GC");.................... usb_ep0_tx_buffer[0]=USB_stack_status.curr_config;0808: MOVFF 1E,458.................... usb_request_send_response(1);080C: MOVLW 01080E: MOVLB 70810: MOVWF x000812: MOVLB 00814: RCALL 03EE.................... break;0816: BRA 083C........................................ case USB_STANDARD_REQUEST_SET_CONFIGURATION: //9.................... if (usb_ep0_rx_buffer[2] <= USB_NUM_CONFIGURATIONS) {0818: MOVLB 4081A: MOVF x1A,W081C: SUBLW 01081E: BNC 0834.................... USB_stack_status.curr_config=usb_ep0_rx_buffer[2];0820: MOVFF 41A,1E.................... usb_set_configured(usb_ep0_rx_buffer[2]);0824: MOVFF 41A,6FD0828: MOVLB 0082A: BRA 055C.................... debug_usb(debug_putc,"SC%U", USB_stack_status.curr_config);.................... usb_put_0len_0();082C: MOVLB 7082E: CLRF x000830: MOVLB 00832: RCALL 03EE.................... }.................... break;0834: MOVLB 00836: BRA 083C........................................ default:.................... usb_request_stall();0838: RCALL 03F4.................... break;083A: BRA 083C.................... }.................... }083C: GOTO 0BE6 (RETURN)........................................ /**************************************************************.................... /* usb_isr_tkn_setup_StandardInterface().................... /*.................... /* Input: usb_ep0_rx_buffer[1] == bRequest.................... /*.................... /* Summary: bmRequestType told us it was a Standard Interface request..................... /* bRequest says which request. Only certain requests are valid,.................... /* if a non-valid request was made then return with an Wrong-Statue (IDLE).................... /*.................... /* Part of usb_isr_tok_setup_dne().................... /***************************************************************/.................... void usb_isr_tkn_setup_StandardInterface(void) {.................... int8 curr_config;........................................ curr_config=USB_stack_status.curr_config;0840: MOVFF 1E,6FD........................................ switch (usb_ep0_rx_buffer[1]) {0844: MOVLB 40846: MOVF x19,W0848: MOVLB 0084A: BZ 0856084C: XORLW 0A084E: BZ 08680850: XORLW 010852: BZ 08B60854: BRA 08E0.................... case USB_STANDARD_REQUEST_GET_STATUS:.................... debug_usb(debug_putc,"GS");.................... usb_ep0_tx_buffer[0]=0;0856: MOVLB 40858: CLRF x58.................... usb_ep0_tx_buffer[1]=0;085A: CLRF x59.................... usb_request_send_response(2);085C: MOVLW 02085E: MOVLB 70860: MOVWF x000862: MOVLB 00864: RCALL 03EE.................... break;0866: BRA 08E4........................................ case USB_STANDARD_REQUEST_GET_INTERFACE:.................... if ( curr_config && (usb_ep0_rx_buffer[4] < USB_NUM_INTERFACES[curr_config-1]) ) { //book says only supports configed state0868: MOVLB 6086A: MOVF xFD,F086C: BZ 08B0086E: MOVLW 010870: SUBWF xFD,W0872: CLRF 030874: MOVFF FF2,6FE0878: BCF FF2.7087A: MOVLB 0087C: RCALL 019E087E: MOVLB 60880: BTFSC xFE.70882: BSF FF2.70884: MOVWF 010886: MOVLB 40888: SUBWF x1C,W088A: BTFSS FD8.0088C: BRA 0892088E: MOVLB 60890: BRA 08B0.................... debug_usb(debug_putc,"GI");.................... usb_ep0_tx_buffer[0]=USB_Interface[usb_ep0_rx_buffer[4]];//our new outgoing byte0892: CLRF 030894: MOVF x1C,W0896: ADDLW 290898: MOVWF FE9089A: MOVLW 00089C: ADDWFC 03,W089E: MOVWF FEA08A0: MOVFF FEF,458.................... usb_request_send_response(1);; //send byte back08A4: MOVLW 0108A6: MOVLB 708A8: MOVWF x0008AA: MOVLB 008AC: RCALL 03EE.................... }.................... else08AE: BRA 08B4.................... usb_request_stall();08B0: MOVLB 008B2: RCALL 03F4.................... break;08B4: BRA 08E4........................................ case USB_STANDARD_REQUEST_SET_INTERFACE:.................... if (curr_config) { //if configured state08B6: MOVLB 608B8: MOVF xFD,F08BA: BZ 08DA.................... debug_usb(debug_putc,"SI");.................... USB_Interface[usb_ep0_rx_buffer[4]]=usb_ep0_rx_buffer[2];08BC: CLRF 0308BE: MOVLB 408C0: MOVF x1C,W08C2: ADDLW 2908C4: MOVWF FE908C6: MOVLW 0008C8: ADDWFC 03,W08CA: MOVWF FEA08CC: MOVFF 41A,FEF.................... usb_put_0len_0();08D0: MOVLB 708D2: CLRF x0008D4: MOVLB 008D6: RCALL 03EE.................... }.................... else08D8: BRA 08DE.................... usb_request_stall();08DA: MOVLB 008DC: RCALL 03F4.................... break;08DE: BRA 08E4........................................ #IF USB_HID_DEVICE.................... case USB_STANDARD_REQUEST_GET_DESCRIPTOR:.................... debug_usb(debug_putc,"GD");.................... usb_Get_Descriptor();.................... break;.................... #endif........................................ // case USB_STANDARD_REQUEST_CLEAR_FEATURE:.................... // case USB_STANDARD_REQUEST_SET_FEATURE:.................... // let default take care of these, goto wrongstate.................... default:.................... usb_request_stall();08E0: RCALL 03F4.................... break;08E2: BRA 08E4.................... }.................... }08E4: GOTO 0BE6 (RETURN)........................................ /**************************************************************.................... /* usb_isr_tkn_setup_StandardEndpoint().................... /*.................... /* Input: usb_ep0_rx_buffer[1] == bRequest.................... /*.................... /* Summary: bmRequestType told us it was a Standard Endpoint request..................... /* bRequest says which request. Only certain requests are valid,.................... /* if a non-valid request was made then return with an Wrong-Statue (IDLE).................... /*.................... /* Part of usb_isr_tok_setup_dne().................... /***************************************************************/.................... void usb_isr_tkn_setup_StandardEndpoint(void) {.................... if (usb_endpoint_is_valid(usb_ep0_rx_buffer[4])) {*0AA2: MOVFF 41C,6FD0AA6: BRA 08E80AA8: MOVF 01,F0AAA: BZ 0B08.................... switch(usb_ep0_rx_buffer[1]) {0AAC: MOVLB 40AAE: MOVF x19,W0AB0: XORLW 010AB2: MOVLB 00AB4: BZ 0AC00AB6: XORLW 020AB8: BZ 0AD00ABA: XORLW 030ABC: BZ 0AE00ABE: BRA 0B04........................................ case USB_STANDARD_REQUEST_CLEAR_FEATURE:.................... debug_usb(debug_putc,"CF");.................... usb_unstall_ep(usb_ep0_rx_buffer[4]);0AC0: MOVFF 41C,6FD0AC4: BRA 0946.................... usb_put_0len_0();0AC6: MOVLB 70AC8: CLRF x000ACA: MOVLB 00ACC: RCALL 03EE.................... break;0ACE: BRA 0B08........................................ case USB_STANDARD_REQUEST_SET_FEATURE:.................... debug_usb(debug_putc,"SF");.................... usb_stall_ep(usb_ep0_rx_buffer[4]);0AD0: MOVFF 41C,6FD0AD4: BRA 09B4.................... usb_put_0len_0();0AD6: MOVLB 70AD8: CLRF x000ADA: MOVLB 00ADC: RCALL 03EE.................... break;0ADE: BRA 0B08........................................ case USB_STANDARD_REQUEST_GET_STATUS:.................... debug_usb(debug_putc,"GS");.................... usb_ep0_tx_buffer[0]=0;0AE0: MOVLB 40AE2: CLRF x58.................... usb_ep0_tx_buffer[1]=0;0AE4: CLRF x59.................... if (usb_endpoint_stalled(usb_ep0_rx_buffer[4])) {0AE6: MOVFF 41C,6FD0AEA: MOVLB 00AEC: BRA 0A240AEE: MOVF 01,F0AF0: BZ 0AF8.................... usb_ep0_tx_buffer[0]=1;0AF2: MOVLW 010AF4: MOVLB 40AF6: MOVWF x58.................... }.................... usb_request_send_response(2);0AF8: MOVLW 020AFA: MOVLB 70AFC: MOVWF x000AFE: MOVLB 00B00: RCALL 03EE.................... break;0B02: BRA 0B08........................................ default:.................... usb_request_stall();0B04: RCALL 03F4.................... break;0B06: BRA 0B08.................... }.................... }.................... }0B08: GOTO 0BE6 (RETURN)........................................ /**************************************************************.................... /* usb_isr_tkn_setup_ClassInterface().................... /*.................... /* Input: usb_ep0_rx_buffer[1] == bRequest.................... /*.................... /* Summary: bmRequestType told us it was a Class request. The only Class this drivers supports is HID..................... /* bRequest says which request. Only certain requests are valid,.................... /* if a non-valid request was made then return with an Wrong-Statue (IDLE).................... /*.................... /* Part of usb_isr_tok_setup_dne().................... /* Only compiled if HID_DEVICE is TRUE.................... /***************************************************************/.................... #IF USB_HID_DEVICE.................... void usb_isr_tkn_setup_ClassInterface(void) {.................... switch(usb_ep0_rx_buffer[1]) {........................................ #IF USB_HID_BOOT_PROTOCOL.................... case USB_HID_REQUEST_GET_PROTOCOL: //03.................... debug_usb(debug_putc,"GP");.................... usb_ep0_tx_buffer[0]=hid_protocol[usb_ep0_rx_buffer[4]];.................... usb_request_send_response(1);.................... break;.................... #ENDIF........................................ #IF USB_HID_BOOT_PROTOCOL.................... case USB_HID_REQUEST_SET_PROTOCOL: //0b.................... debug_usb(debug_putc,"SP");.................... hid_protocol[usb_ep0_rx_buffer[4]]=usb_ep0_rx_buffer[2];.................... usb_put_0len_0(); //send 0len packet69.................... break;.................... #ENDIF........................................ #IF USB_HID_IDLE.................... case USB_HID_REQUEST_SET_IDLE: //0a.................... #error TODO: if you want to support SET_IDLE, add code here.................... #ENDIF........................................ #IF USB_HID_IDLE.................... case USB_HID_REQUEST_GET_IDLE: //02.................... #error TODO: if you want to support GET_IDLE, add code here.................... #ENDIF........................................ default:.................... usb_request_stall();.................... break;.................... }.................... }.................... #ENDIF........................................ /**************************************************************.................... /* usb_Get_Descriptor().................... /*.................... /* Input: usb_ep0_rx_buffer[3] == wValue, which descriptor we want.................... /* usb_ep0_rx_buffer[6,7] == Max length the host will accept.................... /*.................... /* Summary: Checks to see if we want a standard descriptor (Interface, Endpoint, Config, Device, String, etc.),.................... /* or a class specific (HID) descriptor. Since some pics (especially the PIC167x5) doesn't have.................... /* pointers to constants we must simulate or own by setting up global registers that say.................... /* which constant array to deal with, which position to start in this array, and the length..................... /* Once these globals are setup the first packet is sent. If a descriptor takes more than one packet.................... /* the PC will send an IN request to endpoint 0, and this will be handled by usb_isr_tok_in_dne().................... /* which will send the rest of the data..................... /*.................... /* Part of usb_isr_tok_setup_dne().................... /***************************************************************/.................... void usb_Get_Descriptor() {.................... usb_getdesc_ptr=0;*049C: CLRF 27.................... USB_stack_status.getdesc_type=USB_GETDESC_CONFIG_TYPE;049E: CLRF 20........................................ switch(usb_ep0_rx_buffer[3]) {04A0: MOVLB 404A2: MOVF x1B,W04A4: XORLW 0104A6: MOVLB 004A8: BZ 04B804AA: XORLW 0304AC: BZ 04C204AE: XORLW 0104B0: BZ 04C804B2: XORLW 2204B4: BZ 04FA04B6: BRA 053C.................... case USB_DESC_DEVICE_TYPE: //1.................... usb_getdesc_len=USB_DESC_DEVICE_LEN;04B8: MOVLW 1204BA: MOVWF 28.................... USB_stack_status.getdesc_type=USB_GETDESC_DEVICE_TYPE;04BC: MOVLW 0304BE: MOVWF 20.................... break;04C0: BRA 0540........................................ //windows hosts will send a FF max len and expect you to send all configs without asking for them individually..................... case USB_DESC_CONFIG_TYPE: //2.................... usb_getdesc_len=USB_TOTAL_CONFIG_LEN;04C2: MOVLW 4304C4: MOVWF 28.................... break;04C6: BRA 0540........................................ case USB_DESC_STRING_TYPE: //3.................... USB_stack_status.getdesc_type=USB_GETDESC_STRING_TYPE;04C8: MOVLW 0204CA: MOVWF 20.................... usb_getdesc_ptr=USB_STRING_DESC_OFFSET[usb_ep0_rx_buffer[2]];04CC: CLRF 0304CE: MOVLB 404D0: MOVF x1A,W04D2: ADDLW 2104D4: MOVWF FE904D6: MOVLW 0004D8: ADDWFC 03,W04DA: MOVWF FEA04DC: MOVFF FEF,27.................... usb_getdesc_len=USB_STRING_DESC[usb_getdesc_ptr];04E0: CLRF 0304E2: MOVF 27,W04E4: MOVFF FF2,6FD04E8: BCF FF2.704EA: MOVLB 004EC: RCALL 01EA04EE: MOVLB 604F0: BTFSC xFD.704F2: BSF FF2.704F4: MOVWF 28.................... break;04F6: MOVLB 004F8: BRA 0540........................................ #IF USB_HID_DEVICE || USB_CDC_DEVICE.................... case USB_DESC_CLASS_TYPE: //0x21.................... //TODO does this work for multiple interfaces or multiple languages?.................... //usb_getdesc_ptr=USB_CLASS_DESCRIPTORS[0][usb_ep0_rx_buffer[4]][usb_ep0_rx_buffer[2]];.................... usb_getdesc_ptr=USB_CLASS_DESCRIPTORS[0][0][usb_ep0_rx_buffer[2]];04FA: CLRF 0304FC: MOVLB 404FE: MOVF x1A,W0500: MOVFF FF2,6FD0504: BCF FF2.70506: MOVLB 00508: RCALL 01B0050A: MOVLB 6050C: BTFSC xFD.7050E: BSF FF2.70510: MOVWF 27.................... if (usb_getdesc_ptr!=0xFF) {0512: INCFSZ 27,W0514: BRA 05180516: BRA 0536.................... usb_getdesc_len=USB_CONFIG_DESC[usb_getdesc_ptr];0518: CLRF 03051A: MOVF 27,W051C: MOVFF FF2,6FD0520: BCF FF2.70522: MOVLB 00524: RCALL 014A0526: MOVLB 60528: BTFSC xFD.7052A: BSF FF2.7052C: MOVWF 28.................... break;052E: MOVLB 00530: BRA 0540.................... }.................... else {0532: BRA 053C0534: MOVLB 6.................... usb_request_stall();0536: MOVLB 00538: RCALL 03F4.................... return;053A: BRA 0558.................... }.................... #endif........................................ #IF USB_HID_DEVICE.................... case USB_DESC_HIDREPORT_TYPE: //0x22.................... usb_getdesc_ptr=USB_CLASS_SPECIFIC_DESC_LOOKUP[0][usb_ep0_rx_buffer[4]];.................... if (usb_getdesc_ptr !=0xFF) {.................... USB_stack_status.getdesc_type=USB_GETDESC_HIDREPORT_TYPE;.................... usb_getdesc_len=USB_CLASS_SPECIFIC_DESC_LOOKUP_SIZE[0][usb_ep0_rx_buffer[4]];.................... break;.................... }.................... else {.................... usb_request_stall();.................... return;.................... }.................... #endif........................................ default:.................... usb_request_stall();053C: RCALL 03F4.................... return;053E: BRA 0558.................... }.................... if (usb_ep0_rx_buffer[7]==0) {0540: MOVLB 40542: MOVF x1F,F0544: BNZ 0550.................... if (usb_getdesc_len > usb_ep0_rx_buffer[6])0546: MOVF 28,W0548: SUBWF x1E,W054A: BC 0550.................... usb_getdesc_len = usb_ep0_rx_buffer[6];054C: MOVFF 41E,28.................... }.................... USB_stack_status.dev_req=GET_DESCRIPTOR;0550: MOVLW 010552: MOVWF 1D.................... usb_copy_desc_seg_to_ep();0554: MOVLB 00556: RCALL 03FA.................... }0558: GOTO 083C (RETURN)........................................ /**************************************************************.................... /* usb_finish_set_address().................... /*.................... /* Input: USB_address_pending holds the address we were asked to set to..................... /*.................... /* Summary: Sets the address..................... /*.................... /* This code should only be run on the PIC USB peripheral, and not the.................... /* National peripheral..................... /*.................... /* Part of usb_isr_tok_setup_dne().................... /***************************************************************/.................... void usb_finish_set_address() {.................... debug_usb(debug_putc," FSA ");.................... USB_stack_status.curr_config=0; // make sure current configuration is 0*0F7A: CLRF 1E........................................ #ifdef __PIC__.................... USB_stack_status.dev_req=NONE; // no request pending0F7C: CLRF 1D.................... usb_set_address(USB_address_pending);0F7E: MOVFF 26,6FE0F82: BRA 0F60.................... #endif.................... }0F84: GOTO 1076 (RETURN)........................................ ////////////////////////////////////////////////////////////////////////////.................... ///.................... /// The following function retrieve data from constant arrays. This may.................... /// look un-optimized, but remember that you can't create a pointer to.................... /// a constant array..................... ///.................... ///////////////////////////////////////////////////////////////////////////.................... void usb_copy_desc_seg_to_ep(void) {.................... unsigned int i=0;*03FA: MOVLB 603FC: CLRF xFE.................... char c;........................................ while ((usb_getdesc_len)&&(i<USB_MAX_EP0_PACKET_LENGTH)).................... {03FE: MOVF 28,F0400: BZ 04860402: MOVF xFE,W0404: SUBLW 3F0406: BNC 0486.................... switch(USB_stack_status.getdesc_type) {0408: MOVF 20,W040A: MOVLB 0040C: BZ 0418040E: XORLW 020410: BZ 04320412: XORLW 010414: BZ 044C0416: BRA 0466.................... case USB_GETDESC_CONFIG_TYPE:.................... c=USB_CONFIG_DESC[usb_getdesc_ptr];0418: CLRF 03041A: MOVF 27,W041C: MOVFF FF2,7000420: BCF FF2.70422: RCALL 014A0424: MOVLB 70426: BTFSC x00.70428: BSF FF2.7042A: MOVFF FE8,6FF.................... break;042E: MOVLB 00430: BRA 0466........................................ #IF USB_HID_DEVICE.................... case USB_GETDESC_HIDREPORT_TYPE:.................... c=USB_CLASS_SPECIFIC_DESC[usb_getdesc_ptr];.................... break;.................... #endif........................................ case USB_GETDESC_STRING_TYPE:.................... c=USB_STRING_DESC[usb_getdesc_ptr];0432: CLRF 030434: MOVF 27,W0436: MOVFF FF2,700043A: BCF FF2.7043C: RCALL 01EA043E: MOVLB 70440: BTFSC x00.70442: BSF FF2.70444: MOVFF FE8,6FF.................... break;0448: MOVLB 0044A: BRA 0466........................................ case USB_GETDESC_DEVICE_TYPE:.................... c=USB_DEVICE_DESC[usb_getdesc_ptr];044C: CLRF 03044E: MOVF 27,W0450: MOVFF FF2,7000454: BCF FF2.70456: RCALL 01C80458: MOVLB 7045A: BTFSC x00.7045C: BSF FF2.7045E: MOVFF FE8,6FF.................... break;0462: MOVLB 00464: BRA 0466.................... }.................... usb_getdesc_ptr++;0466: INCF 27,F.................... usb_getdesc_len--;0468: DECF 28,F.................... usb_ep0_tx_buffer[i++]=c;046A: MOVLB 6046C: MOVF xFE,W046E: INCF xFE,F0470: ADDLW 580472: CLRF 030474: BTFSC FD8.00476: INCF 03,F0478: MOVWF FE9047A: MOVLW 04047C: ADDWF 03,W047E: MOVWF FEA0480: MOVFF 6FF,FEF.................... }0484: BRA 03FE........................................ if ((!usb_getdesc_len)&&(i!=USB_MAX_EP0_PACKET_LENGTH)) {0486: MOVF 28,F0488: BNZ 0492048A: MOVF xFE,W048C: SUBLW 40048E: BZ 0492.................... USB_stack_status.dev_req = NONE;0490: CLRF 1D.................... }........................................ usb_request_send_response(i);0492: MOVFF 6FE,7000496: MOVLB 00498: RCALL 03EE.................... }049A: RETLW 00........................................ #ENDIF............................................................ struct {.................... unsigned int32 dwDTERrate; //data terminal rate, in bits per second.................... unsigned int8 bCharFormat; //num of stop bits (0=1, 1=1.5, 2=2).................... unsigned int8 bParityType; //parity (0=none, 1=odd, 2=even, 3=mark, 4=space).................... unsigned int8 bDataBits; //data bits (5,6,7,8 or 16).................... } __attribute__((__packed__)) usb_cdc_line_coding;........................................ //length of time, in ms, of break signal as we received in a SendBreak message..................... //if ==0xFFFF, send break signal until we receive a 0x0000..................... unsigned int16 usb_cdc_break;........................................ unsigned int8 usb_cdc_encapsulated_cmd[8];........................................ unsigned int8 usb_cdc_put_buffer[USB_CDC_DATA_IN_SIZE];.................... #define usb_cdc_put_buffer_free() usb_tbe(USB_CDC_DATA_IN_ENDPOINT).................... #if USB_CDC_DATA_IN_SIZE>=0x100.................... unsigned int16 usb_cdc_put_buffer_nextin=0;.................... // int16 usb_cdc_last_data_packet_size;.................... #else.................... unsigned int8 usb_cdc_put_buffer_nextin=0;.................... // int8 usb_cdc_last_data_packet_size;.................... #endif........................................ struct {.................... int1 got;.................... #if USB_CDC_DATA_OUT_SIZE>=0x100.................... unsigned int16 len;.................... unsigned int16 index;.................... #else.................... unsigned int8 len;.................... unsigned int8 index;.................... #endif.................... } usb_cdc_get_buffer_status;........................................ #if defined(__PIC__).................... #define usb_cdc_get_buffer_status_buffer usb_ep2_rx_buffer.................... #else.................... unsigned int8 usb_cdc_get_buffer_status_buffer[USB_CDC_DATA_OUT_SIZE];.................... #endif........................................ int1 usb_cdc_got_set_line_coding;........................................ struct {.................... int1 dte_present; //1=DTE present, 0=DTE not present.................... int1 active; //1=activate carrier, 0=deactivate carrier.................... unsigned int reserved:6;.................... } usb_cdc_carrier;........................................ unsigned int8 g_LastCDCDataPacketSize;........................................ enum {USB_CDC_OUT_NOTHING=0, USB_CDC_OUT_COMMAND=1, USB_CDC_OUT_LINECODING=2, USB_CDC_WAIT_0LEN=3} __usb_cdc_state=0;........................................ #if defined(__PCH__).................... #byte INTCON=0xFF2.................... #bit INT_GIE=INTCON.7.................... #else.................... #word SR=0x42.................... #endif........................................ //handle OUT token done interrupt on endpoint 0 [read encapsulated cmd and line coding data].................... void usb_isr_tok_out_cdc_control_dne(void) {.................... debug_usb(debug_putc,"CDC %X ",__usb_cdc_state);........................................ switch (__usb_cdc_state) {*0E7E: MOVF x83,W0E80: XORLW 010E82: BZ 0E8A0E84: XORLW 030E86: BZ 0EB20E88: BRA 0EDA.................... //printf(putc_tbe,"@%X@\r\n", __usb_cdc_state);.................... case USB_CDC_OUT_COMMAND:.................... //usb_get_packet(0, usb_cdc_encapsulated_cmd, 8);.................... memcpy(usb_cdc_encapsulated_cmd, usb_ep0_rx_buffer,8);0E8A: CLRF FEA0E8C: MOVLW 340E8E: MOVWF FE90E90: MOVLW 040E92: MOVWF FE20E94: MOVLW 180E96: MOVWF FE10E98: MOVLW 080E9A: MOVWF 010E9C: MOVFF FE6,FEE0EA0: DECFSZ 01,F0EA2: BRA 0E9C.................... #if USB_MAX_EP0_PACKET_LENGTH==8.................... __usb_cdc_state=USB_CDC_WAIT_0LEN;.................... usb_request_get_data();.................... #else.................... usb_put_0len_0();0EA4: MOVLB 70EA6: CLRF x000EA8: MOVLB 00EAA: CALL 03EE.................... __usb_cdc_state=0;0EAE: CLRF x83.................... #endif.................... break;0EB0: BRA 0EE2........................................ #if USB_MAX_EP0_PACKET_LENGTH==8.................... case USB_CDC_WAIT_0LEN:.................... usb_put_0len_0();.................... __usb_cdc_state=0;.................... break;.................... #endif........................................ case USB_CDC_OUT_LINECODING:.................... //usb_get_packet(0, &usb_cdc_line_coding, 7);.................... //printf(putc_tbe,"\r\n!GSLC FIN!\r\n");.................... memcpy(&usb_cdc_line_coding, usb_ep0_rx_buffer,7);0EB2: CLRF FEA0EB4: MOVLW 2B0EB6: MOVWF FE90EB8: MOVLW 040EBA: MOVWF FE20EBC: MOVLW 180EBE: MOVWF FE10EC0: MOVLW 070EC2: MOVWF 010EC4: MOVFF FE6,FEE0EC8: DECFSZ 01,F0ECA: BRA 0EC4.................... __usb_cdc_state=0;0ECC: CLRF x83.................... usb_put_0len_0();0ECE: MOVLB 70ED0: CLRF x000ED2: MOVLB 00ED4: CALL 03EE.................... break;0ED8: BRA 0EE2........................................ default:.................... __usb_cdc_state=0;0EDA: CLRF x83.................... usb_init_ep0_setup();0EDC: CALL 0284.................... break;0EE0: BRA 0EE2.................... }.................... }0EE2: GOTO 0F4C (RETURN)........................................ //handle IN token on 0 (setup packet).................... void usb_isr_tkn_cdc(void) {.................... //make sure the request goes to a CDC interface.................... if ((usb_ep0_rx_buffer[4] == 1) || (usb_ep0_rx_buffer[4] == 0)) {*0B12: MOVLB 40B14: DECFSZ x1C,W0B16: BRA 0B1A0B18: BRA 0B1E0B1A: MOVF x1C,F0B1C: BNZ 0BBA.................... //printf(putc_tbe,"!%X!\r\n", usb_ep0_rx_buffer[1]);.................... switch(usb_ep0_rx_buffer[1]) {0B1E: MOVF x19,W0B20: MOVLB 00B22: BZ 0B3A0B24: XORLW 010B26: BZ 0B420B28: XORLW 210B2A: BZ 0B640B2C: XORLW 010B2E: BZ 0B6E0B30: XORLW 030B32: BZ 0B940B34: XORLW 010B36: BZ 0BA20B38: BRA 0BB4.................... case 0x00: //send_encapsulated_command.................... __usb_cdc_state=USB_CDC_OUT_COMMAND;0B3A: MOVLW 010B3C: MOVWF x83.................... usb_request_get_data();0B3E: RCALL 0B0C.................... break;0B40: BRA 0BB8........................................ case 0x01: //get_encapsulated_command.................... memcpy(usb_ep0_tx_buffer, usb_cdc_encapsulated_cmd, 8);0B42: MOVLW 040B44: MOVWF FEA0B46: MOVLW 580B48: MOVWF FE90B4A: CLRF FE20B4C: MOVLW 340B4E: MOVWF FE10B50: MOVLW 080B52: MOVWF 010B54: MOVFF FE6,FEE0B58: DECFSZ 01,F0B5A: BRA 0B54.................... usb_request_send_response(usb_ep0_rx_buffer[6]); //send wLength bytes0B5C: MOVFF 41E,7000B60: RCALL 03EE.................... break;0B62: BRA 0BB8........................................ case 0x20: //set_line_coding.................... debug_usb(debug_putc,"!GSLC!");.................... __usb_cdc_state=USB_CDC_OUT_LINECODING;0B64: MOVLW 020B66: MOVWF x83.................... usb_cdc_got_set_line_coding=TRUE;0B68: BSF x80.0.................... usb_request_get_data();0B6A: RCALL 0B0C.................... break;0B6C: BRA 0BB8........................................ case 0x21: //get_line_coding.................... memcpy(usb_ep0_tx_buffer, &usb_cdc_line_coding, sizeof(usb_cdc_line_coding));0B6E: MOVLW 040B70: MOVWF FEA0B72: MOVLW 580B74: MOVWF FE90B76: CLRF FE20B78: MOVLW 2B0B7A: MOVWF FE10B7C: MOVLW 070B7E: MOVWF 010B80: MOVFF FE6,FEE0B84: DECFSZ 01,F0B86: BRA 0B80.................... usb_request_send_response(sizeof(usb_cdc_line_coding)); //send wLength bytes0B88: MOVLW 070B8A: MOVLB 70B8C: MOVWF x000B8E: MOVLB 00B90: RCALL 03EE.................... break;0B92: BRA 0BB8........................................ case 0x22: //set_control_line_state.................... usb_cdc_carrier=usb_ep0_rx_buffer[2];0B94: MOVFF 41A,81.................... usb_put_0len_0();0B98: MOVLB 70B9A: CLRF x000B9C: MOVLB 00B9E: RCALL 03EE.................... break;0BA0: BRA 0BB8........................................ case 0x23: //send_break.................... usb_cdc_break=make16(usb_ep0_rx_buffer[2],usb_ep0_rx_buffer[3]);0BA2: MOVFF 41A,330BA6: MOVFF 41B,32.................... usb_put_0len_0();0BAA: MOVLB 70BAC: CLRF x000BAE: MOVLB 00BB0: RCALL 03EE.................... break;0BB2: BRA 0BB8........................................ default:.................... usb_request_stall();0BB4: RCALL 03F4.................... break;0BB6: BRA 0BB80BB8: MOVLB 4.................... }.................... }.................... }0BBA: MOVLB 00BBC: GOTO 0BE6 (RETURN)........................................ //handle OUT token done interrupt on endpoint 3 [buffer incoming received chars].................... void usb_isr_tok_out_cdc_data_dne(void) {.................... usb_cdc_get_buffer_status.got=TRUE;*0F2C: BSF x7D.0.................... usb_cdc_get_buffer_status.index=0;0F2E: CLRF x7F.................... #if (defined(__PIC__) && __PIC__).................... usb_cdc_get_buffer_status.len=usb_rx_packet_size(USB_CDC_DATA_OUT_ENDPOINT);0F30: MOVLW 020F32: MOVLB 60F34: MOVWF xFE0F36: MOVLB 00F38: BRA 0EE60F3A: MOVFF 01,7E.................... #else.................... usb_cdc_get_buffer_status.len=usb_get_packet_buffer(.................... USB_CDC_DATA_OUT_ENDPOINT,&usb_cdc_get_buffer_status_buffer[0],USB_CDC_DATA_OUT_SIZE);.................... #endif.................... }0F3E: GOTO 0F5A (RETURN)........................................ //handle IN token done interrupt on endpoint 2 [transmit buffered characters].................... void usb_isr_tok_in_cdc_data_dne(void).................... {.................... //putc('!');.................... if (usb_cdc_put_buffer_nextin /* || (g_LastCDCDataPacketSize == USB_CDC_DATA_IN_SIZE)*/ )*104E: MOVF x7C,F1050: BZ 1054.................... {.................... //printf("FL3 %LU\r\n", (int16)usb_cdc_put_buffer_nextin);.................... usb_cdc_flush_out_buffer();1052: RCALL 102C.................... }.................... }1054: GOTO 1084 (RETURN)........................................ void usb_cdc_flush_out_buffer(void).................... {.................... //printf(putc_tbe, "FL %U\r\n", usb_cdc_put_buffer_nextin);.................... usb_put_packet(USB_CDC_DATA_IN_ENDPOINT,usb_cdc_put_buffer,usb_cdc_put_buffer_nextin,USB_DTS_TOGGLE);*102C: MOVLW 02102E: MOVLB 71030: MOVWF x3A1032: CLRF x3C1034: MOVLW 3C1036: MOVWF x3B1038: CLRF x3E103A: MOVFF 7C,73D103E: MOVLW 021040: MOVWF x3F1042: MOVLB 01044: RCALL 0F88.................... g_LastCDCDataPacketSize = usb_cdc_put_buffer_nextin;1046: MOVFF 7C,82.................... usb_cdc_put_buffer_nextin=0;104A: CLRF x7C.................... }104C: RETLW 00........................................ void usb_cdc_init(void).................... {.................... usb_cdc_line_coding.dwDTERrate = 9600;*035C: CLRF 2E035E: CLRF 2D0360: MOVLW 250362: MOVWF 2C0364: MOVLW 800366: MOVWF 2B.................... usb_cdc_line_coding.bCharFormat = 0;0368: CLRF 2F.................... usb_cdc_line_coding.bParityType = 0;036A: CLRF 30.................... usb_cdc_line_coding.bDataBits = 8;036C: MOVLW 08036E: MOVWF 31.................... (int8)usb_cdc_carrier = 0;0370: CLRF x81.................... usb_cdc_got_set_line_coding = FALSE;0372: BCF x80.0.................... usb_cdc_break = 0;0374: CLRF 330376: CLRF 32.................... usb_cdc_put_buffer_nextin = 0;0378: CLRF x7C.................... usb_cdc_get_buffer_status.got = 0;037A: BCF x7D.0.................... }037C: RETLW 00........................................ ////////////////// END USB CONTROL HANDLING //////////////////////////////////........................................ ////////////////// BEGIN USB<->RS232 CDC LIBRARY /////////////////////////////........................................ void usb_cdc_get_discard(void).................... {.................... usb_cdc_get_buffer_status.got = FALSE;.................... usb_flush_out(USB_CDC_DATA_OUT_ENDPOINT, USB_DTS_TOGGLE);.................... }........................................ char usb_cdc_getc(void).................... {.................... char c;........................................ while (!usb_cdc_kbhit()) {}........................................ c=usb_cdc_get_buffer_status_buffer[usb_cdc_get_buffer_status.index++];........................................ if (usb_cdc_get_buffer_status.index >= usb_cdc_get_buffer_status.len).................... {.................... usb_cdc_get_discard();.................... }........................................ return(c);.................... }........................................ void usb_cdc_putc_fast(char c).................... {.................... #if defined(__PCH__).................... int1 old_gie;........................................ //disable global interrupts.................... old_gie = INT_GIE;*1376: MOVLB 61378: BCF xDD.0137A: BTFSC FF2.7137C: BSF xDD.0.................... INT_GIE = 0;137E: BCF FF2.7.................... #else.................... int16 oldSR;........................................ oldSR = SR;.................... SR |= 0xE0;.................... #endif........................................ if (usb_cdc_put_buffer_nextin >= USB_CDC_DATA_IN_SIZE) {1380: MOVLB 01382: MOVF x7C,W1384: SUBLW 3F1386: BC 138C.................... usb_cdc_put_buffer_nextin = USB_CDC_DATA_IN_SIZE-1; //we just overflowed the buffer!1388: MOVLW 3F138A: MOVWF x7C.................... }.................... usb_cdc_put_buffer[usb_cdc_put_buffer_nextin++] = c;138C: MOVF x7C,W138E: INCF x7C,F1390: CLRF 031392: ADDLW 3C1394: MOVWF FE91396: MOVLW 001398: ADDWFC 03,W139A: MOVWF FEA139C: MOVFF 6DC,FEF........................................ //renable global interrupts.................... #if defined(__PCH__).................... INT_GIE = old_gie;13A0: BCF FF2.713A2: MOVLB 613A4: BTFSC xDD.013A6: BSF FF2.713A8: CLRF 1813AA: BTFSC FF2.713AC: BSF 18.713AE: BCF FF2.7.................... #else.................... SR = oldSR;.................... #endif........................................ /*.................... if (usb_tbe(USB_CDC_DATA_IN_ENDPOINT)) {.................... if (usb_cdc_put_buffer_nextin).................... usb_cdc_flush_out_buffer();.................... }.................... */.................... if (usb_cdc_put_buffer_free()) {13B0: MOVLW 0213B2: MOVLB 713B4: MOVWF x4713B6: MOVLB 013B8: RCALL 0D0613BA: BTFSC 18.713BC: BSF FF2.713BE: MOVF 01,F13C0: BZ 13D013C2: CLRF 1813C4: BTFSC FF2.713C6: BSF 18.713C8: BCF FF2.7.................... //printf("FL2 %LU\r\n", (int16)usb_cdc_put_buffer_nextin);.................... usb_cdc_flush_out_buffer();13CA: RCALL 102C13CC: BTFSC 18.713CE: BSF FF2.7.................... }.................... //putc('*');.................... }13D0: GOTO 140A (RETURN)........................................ void usb_cdc_putc(char c).................... {.................... while (!usb_cdc_putready()).................... {13D4: MOVF x7C,W13D6: SUBLW 3F13D8: BC 140413DA: CLRF 1813DC: BTFSC FF2.713DE: BSF 18.713E0: BCF FF2.7.................... if (usb_cdc_put_buffer_free())13E2: MOVLW 0213E4: MOVLB 713E6: MOVWF x4713E8: MOVLB 013EA: RCALL 0D0613EC: BTFSC 18.713EE: BSF FF2.713F0: MOVF 01,F13F2: BZ 140213F4: CLRF 1813F6: BTFSC FF2.713F8: BSF 18.713FA: BCF FF2.7.................... {.................... //printf("FL1 %LU\r\n", (int16)usb_cdc_put_buffer_nextin);.................... usb_cdc_flush_out_buffer();13FC: RCALL 102C13FE: BTFSC 18.71400: BSF FF2.7.................... }.................... //delay_ms(500);.................... //printf(putc_tbe,"TBE=%U CNT=%U LST=%U\r\n",usb_tbe(USB_CDC_DATA_IN_ENDPOINT), usb_cdc_put_buffer_nextin, usb_cdc_last_data_packet_size);.................... }1402: BRA 13D4.................... //putc('.');.................... //putc(c);.................... usb_cdc_putc_fast(c);1404: MOVFF 6DB,6DC1408: BRA 1376.................... }140A: RETLW 00........................................ #include <ctype.h>.................... ////////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2003 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS C ////.................... //// compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, reproduction ////.................... //// or distribution is permitted without written permission. ////.................... //// Derivative programs created using this software in object code ////.................... //// form are not restricted in any way. ////.................... ////////////////////////////////////////////////////////////////////////////........................................ #ifndef _CTYPE.................... #define _CTYPE........................................ #define islower(x) isamong(x,"abcdefghijklmnopqrstuvwxyz").................... #define isupper(x) isamong(x,"ABCDEFGHIJKLMNOPQRSTUVWXYZ").................... #define isalnum(x) isamong(x,"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").................... #define isalpha(x) isamong(x,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").................... #define isdigit(x) isamong(x,"0123456789").................... #define isspace(x) ((x)==' ').................... #define isxdigit(x) isamong(x,"0123456789ABCDEFabcdef").................... #define iscntrl(x) ((x)<' ').................... #define isprint(x) ((x)>=' ').................... #define isgraph(x) ((x)>' ').................... #define ispunct(x) (((x)>' ')&&!isalnum(x))........................................ #endif................................................................................ BYTE gethex1_usb().................... {.................... char digit;........................................ digit = usb_cdc_getc();........................................ usb_cdc_putc(digit);........................................ if(digit<='9').................... return(digit-'0');.................... else.................... return((toupper(digit)-'A')+10);.................... }........................................ BYTE gethex_usb() {.................... unsigned int8 lo,hi;........................................ hi = gethex1_usb();.................... lo = gethex1_usb();.................... if(lo==0xdd).................... return(hi);.................... else.................... return( hi*16+lo );.................... }........................................ void get_string_usb(char* s, unsigned int max) {.................... unsigned int len;.................... char c;........................................ --max;.................... len=0;.................... do {.................... c=usb_cdc_getc();.................... if(c==8) { // Backspace.................... if(len>0) {.................... len--;.................... usb_cdc_putc(c);.................... usb_cdc_putc(' ');.................... usb_cdc_putc(c);.................... }.................... } else if ((c>=' ')&&(c<='~')).................... if(len<max) {.................... s[len++]=c;.................... usb_cdc_putc(c);.................... }.................... } while(c!=13);.................... s[len]=0;.................... }............................................................ // stdlib.h is required for the ato_ conversions.................... // in the following functions.................... #ifdef _STDLIB........................................ signed int get_int_usb() {.................... char s[7];.................... signed int i;........................................ get_string_usb(s, 7);........................................ i=atoi(s);.................... return(i);.................... }........................................ signed long get_long_usb() {.................... char s[13];.................... signed long l;........................................ get_string_usb(s, 13);.................... l=atol(s);.................... return(l);.................... }........................................ float get_float_usb() {.................... char s[20];.................... float f;........................................ get_string_usb(s, 20);.................... f = atof(s);.................... return(f);.................... }........................................ #endif........................................ #include <math.h>.................... ////////////////////////////////////////////////////////////////////////////.................... //// (C) Copyright 1996,2008 Custom Computer Services ////.................... //// This source code may only be used by licensed users of the CCS C ////.................... //// compiler. This source code may only be distributed to other ////.................... //// licensed users of the CCS C compiler. No other use, reproduction ////.................... //// or distribution is permitted without written permission. ////.................... //// Derivative programs created using this software in object code ////.................... //// form are not restricted in any way. ////.................... ////////////////////////////////////////////////////////////////////////////.................... //// ////.................... //// History: ////.................... //// * 9/20/2001 : Improvments are made to sin/cos code. ////.................... //// The code now is small, much faster, ////.................... //// and more accurate. ////.................... //// * 2/21/2007 : Compiler handles & operator differently and does.................... //// not return generic (int8 *) so type cast is done ////.................... //// ////.................... ////////////////////////////////////////////////////////////////////////////........................................ #ifndef MATH_H.................... #define MATH_H........................................ #ifdef PI.................... #undef PI.................... #endif.................... #define PI 3.1415926535897932............................................................ #define SQRT2 1.4142135623730950........................................ //float const ps[4] = {5.9304945, 21.125224, 8.9403076, 0.29730279};.................... //float const qs[4] = {1.0000000, 15.035723, 17.764134, 2.4934718};........................................ ///////////////////////////// Round Functions //////////////////////////////........................................ float32 CEIL_FLOOR(float32 x, unsigned int8 n).................... {.................... float32 y, res;.................... unsigned int16 l;.................... int1 s;........................................ s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y <= 32768.0).................... res = (float32)(unsigned int16)y;........................................ else if (y < 10000000.0).................... {.................... l = (unsigned int16)(y/32768.0);.................... y = 32768.0*(y/32768.0 - (float32)l);.................... res = 32768.0*(float32)l;.................... res += (float32)(unsigned int16)y;.................... }........................................ else.................... res = y;........................................ y = y - (float32)(unsigned int16)y;........................................ if (s).................... res = -res;........................................ if (y != 0).................... {.................... if (s == 1 && n == 0).................... res -= 1.0;........................................ if (s == 0 && n == 1).................... res += 1.0;.................... }.................... if (x == 0).................... res = 0;........................................ return (res);.................... }........................................ // Overloaded Functions to take care for new Data types in PCD.................... // Overloaded function CEIL_FLOOR() for data type - Float48.................... #if defined(__PCD__).................... float48 CEIL_FLOOR(float48 x, unsigned int8 n).................... {.................... float48 y, res;.................... unsigned int16 l;.................... int1 s;........................................ s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y <= 32768.0).................... res = (float48)(unsigned int16)y;........................................ else if (y < 10000000.0).................... {.................... l = (unsigned int16)(y/32768.0);.................... y = 32768.0*(y/32768.0 - (float48)l);.................... res = 32768.0*(float32)l;.................... res += (float48)(unsigned int16)y;.................... }........................................ else.................... res = y;........................................ y = y - (float48)(unsigned int16)y;........................................ if (s).................... res = -res;........................................ if (y != 0).................... {.................... if (s == 1 && n == 0).................... res -= 1.0;........................................ if (s == 0 && n == 1).................... res += 1.0;.................... }.................... if (x == 0).................... res = 0;........................................ return (res);.................... }............................................................ // Overloaded function CEIL_FLOOR() for data type - Float64.................... float64 CEIL_FLOOR(float64 x, unsigned int8 n).................... {.................... float64 y, res;.................... unsigned int16 l;.................... int1 s;........................................ s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y <= 32768.0).................... res = (float64)(unsigned int16)y;........................................ else if (y < 10000000.0).................... {.................... l = (unsigned int16)(y/32768.0);.................... y = 32768.0*(y/32768.0 - (float64)l);.................... res = 32768.0*(float64)l;.................... res += (float64)(unsigned int16)y;.................... }........................................ else.................... res = y;........................................ y = y - (float64)(unsigned int16)y;........................................ if (s).................... res = -res;........................................ if (y != 0).................... {.................... if (s == 1 && n == 0).................... res -= 1.0;........................................ if (s == 0 && n == 1).................... res += 1.0;.................... }.................... if (x == 0).................... res = 0;........................................ return (res);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float floor(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : rounds down the number x..................... // Date : N/A.................... //.................... float32 floor(float32 x).................... {.................... return CEIL_FLOOR(x, 0);.................... }.................... // Following 2 functions are overloaded functions of floor() for PCD.................... // Overloaded function floor() for data type - Float48.................... #if defined(__PCD__).................... float48 floor(float48 x).................... {.................... return CEIL_FLOOR(x, 0);.................... }........................................ // Overloaded function floor() for data type - Float64.................... float64 floor(float64 x).................... {.................... return CEIL_FLOOR(x, 0);.................... }.................... #endif............................................................ ////////////////////////////////////////////////////////////////////////////.................... // float ceil(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : rounds up the number x..................... // Date : N/A.................... //.................... float32 ceil(float32 x).................... {.................... return CEIL_FLOOR(x, 1);.................... }.................... // Following 2 functions are overloaded functions of ceil() for PCD.................... // Overloaded function ceil() for data type - Float48.................... #if defined(__PCD__).................... float48 ceil(float48 x).................... {.................... return CEIL_FLOOR(x, 1);.................... }........................................ // Overloaded function ceil() for data type - Float64.................... float64 ceil(float64 x).................... {.................... return CEIL_FLOOR(x, 1);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float fabs(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : Computes the absolute value of floating point number x.................... // Returns : returns the absolute value of x.................... // Date : N/A.................... //.................... #define fabs abs........................................ ////////////////////////////////////////////////////////////////////////////.................... // float fmod(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : Computes the floating point remainder of x/y.................... // Returns : returns the value of x= i*y, for some integer i such that, if y.................... // is non zero, the result has the same isgn of x na dmagnitude less than the.................... // magnitude of y. If y is zero then a domain error occurs..................... // Date : N/A.................... //........................................ float fmod(float32 x,float32 y).................... {.................... float32 i;.................... if (y!=0.0).................... {.................... i=(x/y < 0.0)? ceil(x/y): floor(x/y);.................... return(x-(i*y));.................... }.................... else.................... {.................... #ifdef _ERRNO.................... {.................... errno=EDOM;.................... }.................... #endif.................... }.................... }.................... //Overloaded function for fmod() for PCD.................... // Overloaded function fmod() for data type - Float48.................... #if defined(__PCD__).................... float48 fmod(float48 x,float48 y).................... {.................... float48 i;.................... if (y!=0.0).................... {.................... i=(x/y < 0.0)? ceil(x/y): floor(x/y);.................... return(x-(i*y));.................... }.................... else.................... {.................... #ifdef _ERRNO.................... {.................... errno=EDOM;.................... }.................... #endif.................... }.................... }.................... // Overloaded function fmod() for data type - Float64.................... float64 fmod(float64 x,float64 y).................... {.................... float64 i;.................... if (y!=0.0).................... {.................... i=(x/y < 0.0)? ceil(x/y): floor(x/y);.................... return(x-(i*y));.................... }.................... else.................... {.................... #ifdef _ERRNO.................... {.................... errno=EDOM;.................... }.................... #endif.................... }.................... }.................... #endif.................... //////////////////// Exponential and logarithmic functions ////////////////////.................... ////////////////////////////////////////////////////////////////////////////.................... // float exp(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the value (e^x).................... // Date : N/A.................... //.................... #define LN2 0.6931471805599453........................................ float const pe[6] = {0.000207455774, 0.00127100575, 0.00965065093,.................... 0.0554965651, 0.240227138, 0.693147172};............................................................ float32 exp(float32 x).................... {.................... float32 y, res, r;.................... #if defined(__PCD__).................... int8 data1;.................... #endif.................... signed int8 n;.................... int1 s;.................... #ifdef _ERRNO.................... if(x > 88.722838).................... {.................... errno=ERANGE;.................... return(0);.................... }.................... #endif.................... n = (signed int16)(x/LN2);.................... s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... n = -n;.................... y = -y;.................... }........................................ res = 0.0;.................... #if !defined(__PCD__).................... *((unsigned int8 *)(&res)) = n + 0x7F;.................... #endif........................................ #if defined(__PCD__) // Takes care of IEEE format for PCD.................... data1 = n+0x7F;.................... if(bit_test(data1,0)).................... bit_set(*(((unsigned int8 *)(&res)+2)),7);.................... rotate_right(&data1,1);.................... bit_clear(data1,7);.................... *(((unsigned int8 *)(&res)+3)) = data1;.................... #endif........................................ y = y/LN2 - (float32)n;........................................ r = pe[0]*y + pe[1];.................... r = r*y + pe[2];.................... r = r*y + pe[3];.................... r = r*y + pe[4];.................... r = r*y + pe[5];........................................ res = res*(1.0 + y*r);........................................ if (s).................... res = 1.0/res;.................... return(res);.................... }............................................................ //Overloaded function for exp() for PCD.................... // Overloaded function exp() for data type - Float48.................... #if defined(__PCD__).................... float48 exp(float48 x).................... {.................... float48 y, res, r;.................... int8 data1;.................... signed int8 n;.................... int1 s;.................... #ifdef _ERRNO.................... if(x > 88.722838).................... {.................... errno=ERANGE;.................... return(0);.................... }.................... #endif.................... n = (signed int16)(x/LN2);.................... s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... n = -n;.................... y = -y;.................... }........................................ res = 0.0;........................................ data1 = n+0x7F;.................... if(bit_test(data1,0)).................... bit_set(*(((unsigned int8 *)(&res)+4)),7);.................... rotate_right(&data1,1);.................... bit_clear(data1,7);.................... *(((unsigned int8 *)(&res)+5)) = data1;........................................ y = y/LN2 - (float48)n;........................................ r = pe[0]*y + pe[1];.................... r = r*y + pe[2];.................... r = r*y + pe[3];.................... r = r*y + pe[4];.................... r = r*y + pe[5];........................................ res = res*(1.0 + y*r);........................................ if (s).................... res = 1.0/res;.................... return(res);.................... }........................................ // Overloaded function exp() for data type - Float64.................... float64 exp(float64 x).................... {.................... float64 y, res, r;.................... unsigned int16 data1, data2;.................... unsigned int16 *p;.................... signed int16 n;.................... int1 s;.................... #ifdef _ERRNO.................... if(x > 709.7827128).................... {.................... errno=ERANGE;.................... return(0);.................... }.................... #endif.................... n = (signed int16)(x/LN2);.................... s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... n = -n;.................... y = -y;.................... }........................................ res = 0.0;........................................ #if !defined(__PCD__).................... *((unsigned int16 *)(&res)) = n + 0x7F;.................... #endif.................... p= (((unsigned int16 *)(&res))+3);.................... data1 = *p;.................... data2 = *p;.................... data1 = n + 0x3FF;.................... data1 = data1 <<4;.................... if(bit_test(data2,15)).................... bit_set(data1,15);.................... data2 = data2 & 0x000F;.................... data1 ^= data2;........................................ *(((unsigned int16 *)(&res)+3)) = data1;............................................................ y = y/LN2 - (float64)n;........................................ r = pe[0]*y + pe[1];.................... r = r*y + pe[2];.................... r = r*y + pe[3];.................... r = r*y + pe[4];.................... r = r*y + pe[5];........................................ res = res*(1.0 + y*r);........................................ if (s).................... res = 1.0/res;.................... return(res);.................... }........................................ #ENDIF............................................................ /************************************************************/........................................ float32 const pl[4] = {0.45145214, -9.0558803, 26.940971, -19.860189};.................... float32 const ql[4] = {1.0000000, -8.1354259, 16.780517, -9.9300943};........................................ ////////////////////////////////////////////////////////////////////////////.................... // float log(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the the natural log of x.................... // Date : N/A.................... //.................... float32 log(float32 x).................... {.................... float32 y, res, r, y2;.................... #if defined(__PCD__).................... unsigned int8 data1,data2;.................... #endif.................... signed int8 n;.................... #ifdef _ERRNO.................... if(x <0).................... {.................... errno=EDOM;.................... }.................... if(x ==0).................... {.................... errno=ERANGE;.................... return(0);.................... }.................... #endif.................... y = x;........................................ if (y != 1.0).................... {.................... #if !defined(__PCD__).................... *((unsigned int8 *)(&y)) = 0x7E;.................... #endif........................................ #if defined(__PCD__) // Takes care of IEEE format.................... data2 = *(((unsigned int8 *)(&y))+3);.................... *(((unsigned int8 *)(&y))+3) = 0x3F;.................... data1 = *(((unsigned int8 *)(&y))+2);.................... bit_clear(data1,7);.................... *(((unsigned int8 *)(&y))+2) = data1;.................... if(bit_test(data2,7)).................... bit_set(*(((unsigned int8 *)(&y))+3),7);.................... #endif........................................ y = (y - 1.0)/(y + 1.0);........................................ y2=y*y;........................................ res = pl[0]*y2 + pl[1];.................... res = res*y2 + pl[2];.................... res = res*y2 + pl[3];........................................ r = ql[0]*y2 + ql[1];.................... r = r*y2 + ql[2];.................... r = r*y2 + ql[3];........................................ res = y*res/r;.................... #if !defined(__PCD__).................... n = *((unsigned int8 *)(&x)) - 0x7E;.................... #endif.................... #if defined(__PCD__).................... data1 = *(((unsigned int8 *)(&x)+3));.................... rotate_left(&data1,1);.................... data2 = *(((unsigned int8 *)(&x)+2));.................... if(bit_test (data2,7)).................... bit_set(data1,0);.................... n = data1 - 0x7E;.................... #endif........................................ if (n<0).................... r = -(float32)-n;.................... else.................... r = (float32)n;........................................ res += r*LN2;.................... }........................................ else.................... res = 0.0;........................................ return(res);.................... }........................................ //Overloaded function for log() for PCD.................... // Overloaded function log() for data type - Float48.................... #if defined(__PCD__).................... float48 log(float48 x).................... {.................... float48 y, res, r, y2;.................... unsigned int8 data1,data2;.................... signed int8 n;.................... #ifdef _ERRNO.................... if(x <0).................... {.................... errno=EDOM;.................... }.................... if(x ==0).................... {.................... errno=ERANGE;.................... return(0);.................... }.................... #endif.................... y = x;........................................ if (y != 1.0).................... {........................................ #if !defined(__PCD__).................... *((unsigned int8 *)(&y)) = 0x7E;.................... #endif.................... data2 = *(((unsigned int8 *)(&y))+5);.................... *(((unsigned int8 *)(&y))+5) = 0x3F;.................... data1 = *(((unsigned int8 *)(&y))+4);.................... bit_clear(data1,7);.................... *(((unsigned int8 *)(&y))+4) = data1;........................................ if(bit_test(data2,7)).................... bit_set(*(((unsigned int8 *)(&y))+4),7);.................... y = (y - 1.0)/(y + 1.0);........................................ y2=y*y;........................................ res = pl[0]*y2 + pl[1];.................... res = res*y2 + pl[2];.................... res = res*y2 + pl[3];........................................ r = ql[0]*y2 + ql[1];.................... r = r*y2 + ql[2];.................... r = r*y2 + ql[3];........................................ res = y*res/r;........................................ data1 = *(((unsigned int8 *)(&x)+5));.................... rotate_left(&data1,1);.................... data2 = *(((unsigned int8 *)(&x)+4));.................... if(bit_test (data2,7)).................... bit_set(data1,0);........................................ n = data1 - 0x7E;........................................ if (n<0).................... r = -(float48)-n;.................... else.................... r = (float48)n;........................................ res += r*LN2;.................... }........................................ else.................... res = 0.0;........................................ return(res);.................... }........................................ // Overloaded function log() for data type - Float48.................... #if defined(__PCD__).................... float32 const pl_64[4] = {0.45145214, -9.0558803, 26.940971, -19.860189};.................... float32 const ql_64[4] = {1.0000000, -8.1354259, 16.780517, -9.9300943};.................... #endif.................... float64 log(float64 x).................... {.................... float64 y, res, r, y2;.................... unsigned int16 data1,data2;.................... unsigned int16 *p;.................... signed int16 n;.................... #ifdef _ERRNO.................... if(x <0).................... {.................... errno=EDOM;.................... }.................... if(x ==0).................... {.................... errno=ERANGE;.................... return(0);.................... }.................... #endif.................... y = x;........................................ if (y != 1.0).................... {.................... #if !defined(__PCD__).................... *((unsigned int8 *)(&y)) = 0x7E;.................... #endif.................... p= (((unsigned int16 *)(&y))+3);.................... data1 = *p;.................... data2 = *p;.................... data1 = 0x3FE;.................... data1 = data1 <<4;.................... if(bit_test (data2,15)).................... bit_set(data1,15);.................... data2 = data2 & 0x000F;.................... data1 ^=data2;........................................ *p = data1;........................................ y = (y - 1.0)/(y + 1.0);........................................ y2=y*y;........................................ res = pl_64[0]*y2 + pl_64[1];.................... res = res*y2 + pl_64[2];.................... res = res*y2 + pl_64[3];........................................ r = ql_64[0]*y2 + ql_64[1];.................... r = r*y2 + ql_64[2];.................... r = r*y2 + ql_64[3];........................................ res = y*res/r;........................................ p= (((unsigned int16 *)(&x))+3);.................... data1 = *p;.................... bit_clear(data1,15);.................... data1 = data1 >>4;.................... n = data1 - 0x3FE;............................................................ if (n<0).................... r = -(float64)-n;.................... else.................... r = (float64)n;........................................ res += r*LN2;.................... }........................................ else.................... res = 0.0;........................................ return(res);.................... }.................... #endif............................................................ #define LN10 2.3025850929940456........................................ ////////////////////////////////////////////////////////////////////////////.................... // float log10(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the the log base 10 of x.................... // Date : N/A.................... //.................... float32 log10(float32 x).................... {.................... float32 r;........................................ r = log(x);.................... r = r/LN10;.................... return(r);.................... }........................................ //Overloaded functions for log10() for PCD.................... // Overloaded function log10() for data type - Float48.................... #if defined(__PCD__).................... float48 log10(float48 x).................... {.................... float48 r;........................................ r = log(x);.................... r = r/LN10;.................... return(r);.................... }........................................ // Overloaded function log10() for data type - Float64.................... float64 log10(float64 x).................... {.................... float64 r;........................................ r = log(x);.................... r = r/LN10;.................... return(r);.................... }.................... #endif.................... ////////////////////////////////////////////////////////////////////////////.................... // float modf(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description :breaks the argument value int integral and fractional parts,.................... // ach of which have the same sign as the argument. It stores the integral part.................... // as a float in the object pointed to by the iptr.................... // Returns : returns the signed fractional part of value..................... // Date : N/A.................... //........................................ float32 modf(float32 value,float32 *iptr).................... {.................... *iptr=(value < 0.0)? ceil(value): floor(value);.................... return(value - *iptr);.................... }.................... //Overloaded functions for modf() for PCD.................... // Overloaded function modf() for data type - Float48.................... #if defined(__PCD__).................... float48 modf(float48 value,float48 *iptr).................... {.................... *iptr=(value < 0.0)? ceil(value): floor(value);.................... return(value - *iptr);.................... }.................... // Overloaded function modf() for data type - Float64.................... float64 modf(float64 value,float64 *iptr).................... {.................... *iptr=(value < 0.0)? ceil(value): floor(value);.................... return(value - *iptr);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float pwr(float x,float y).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the value (x^y).................... // Date : N/A.................... //.................... float32 pwr(float32 x,float32 y).................... {.................... if(x>=0).................... return( exp(y*log(x)) );.................... else.................... return( -exp(y*log(-x)) );.................... }.................... //Overloaded functions for pwr() for PCD.................... // Overloaded function pwr() for data type - Float48.................... #if defined(__PCD__).................... float48 pwr(float48 x,float48 y).................... {.................... if(x>=0).................... return( exp(y*log(x)) );.................... else.................... return( -exp(y*log(-x)) );.................... }.................... // Overloaded function pwr() for data type - Float64.................... float64 pwr(float64 x,float64 y).................... {.................... if(x>=0).................... return( exp(y*log(x)) );.................... else.................... return( -exp(y*log(-x)) );.................... }.................... #endif........................................ //////////////////// Power functions ////////////////////........................................ ////////////////////////////////////////////////////////////////////////////.................... // float pow(float x,float y).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the value (x^y).................... // Date : N/A.................... //.................... float32 pow(float32 x,float32 y).................... {.................... if(x>=0).................... return( exp(y*log(x)) );.................... else.................... return( -exp(y*log(-x)) );.................... }.................... //Overloaded functions for pow() for PCD.................... // Overloaded function for pow() data type - Float48.................... #if defined(__PCD__).................... float48 pow(float48 x,float48 y).................... {.................... if(x>=0).................... return( exp(y*log(x)) );.................... else.................... return( -exp(y*log(-x)) );.................... }........................................ // Overloaded function pow() for data type - Float64.................... float64 pow(float64 x,float64 y).................... {.................... if(x>=0).................... return( exp(y*log(x)) );.................... else.................... return( -exp(y*log(-x)) );.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float sqrt(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the square root of x.................... // Date : N/A.................... //.................... float32 sqrt(float32 x).................... {.................... float32 y, res;.................... #if defined(__PCD__).................... unsigned int16 data1,data2;.................... #endif.................... BYTE *p;........................................ #ifdef _ERRNO.................... if(x < 0).................... {.................... errno=EDOM;.................... }.................... #endif........................................ if( x<=0.0)*19C2: MOVFF 6DF,6ED19C6: MOVFF 6DE,6EC19CA: MOVFF 6DD,6EB19CE: MOVFF 6DC,6EA19D2: MOVLB 619D4: CLRF xF119D6: CLRF xF019D8: CLRF xEF19DA: CLRF xEE19DC: MOVLB 019DE: RCALL 194819E0: BC 19E419E2: BNZ 19EE.................... return(0.0);19E4: CLRF 0019E6: CLRF 0119E8: CLRF 0219EA: CLRF 0319EC: BRA 1ADE........................................ y=x;19EE: MOVFF 6DF,6E319F2: MOVFF 6DE,6E219F6: MOVFF 6DD,6E119FA: MOVFF 6DC,6E0........................................ #if !defined(__PCD__).................... p=&y;19FE: MOVLW 061A00: MOVLB 61A02: MOVWF xE91A04: MOVLW E01A06: MOVWF xE8.................... (*p)=(BYTE)((((unsigned int16)(*p)) + 127) >> 1);1A08: MOVFF 6E8,FE91A0C: MOVFF 6E9,FEA1A10: MOVF FEF,W1A12: CLRF xED1A14: MOVWF xEC1A16: MOVLW 7F1A18: ADDWF xEC,F1A1A: MOVLW 001A1C: ADDWFC xED,F1A1E: BCF FD8.01A20: RRCF xED,W1A22: RRCF xEC,W1A24: MOVFF 6E9,FEA1A28: MOVFF 6E8,FE91A2C: MOVWF FEF.................... #endif........................................ #if defined(__PCD__).................... p = (((unsigned int8 *)(&y))+3);.................... data1 = *(((unsigned int8 *)(&y))+3);.................... data2 = *(((unsigned int8 *)(&y))+2);.................... rotate_left(&data1,1);.................... if(bit_test(data2,7)).................... bit_set(data1,0);.................... data1 = ((data1+127) >>1);.................... bit_clear(data2,7);.................... if(bit_test(data1,0)).................... bit_set(data2,7);.................... data1 = data1 >>1;.................... *(((unsigned int8 *)(&y))+3) = data1;.................... *(((unsigned int8 *)(&y))+2) = data2;........................................ #endif........................................ do {.................... res=y;1A2E: MOVFF 6E3,6E71A32: MOVFF 6E2,6E61A36: MOVFF 6E1,6E51A3A: MOVFF 6E0,6E4.................... y+=(x/y);1A3E: MOVFF 6DF,6ED1A42: MOVFF 6DE,6EC1A46: MOVFF 6DD,6EB1A4A: MOVFF 6DC,6EA1A4E: MOVFF 6E3,6F11A52: MOVFF 6E2,6F01A56: MOVFF 6E1,6EF1A5A: MOVFF 6E0,6EE1A5E: MOVLB 01A60: RCALL 16BA1A62: BCF FD8.11A64: MOVFF 6E3,6ED1A68: MOVFF 6E2,6EC1A6C: MOVFF 6E1,6EB1A70: MOVFF 6E0,6EA1A74: MOVFF 03,6F11A78: MOVFF 02,6F01A7C: MOVFF 01,6EF1A80: MOVFF 00,6EE1A84: RCALL 14421A86: MOVFF 03,6E31A8A: MOVFF 02,6E21A8E: MOVFF 01,6E11A92: MOVFF 00,6E0........................................ #if !defined(__PCD__).................... (*p)--;1A96: MOVLB 61A98: MOVFF 6E8,FE91A9C: MOVFF 6E9,FEA1AA0: DECF FEF,F.................... #endif........................................ #if defined(__PCD__).................... data1 = *(((unsigned int8 *)(&y))+3);.................... data2 = *(((unsigned int8 *)(&y))+2);.................... rotate_left(&data1,1);.................... if(bit_test(data2,7)).................... bit_set(data1,0);.................... data1--;.................... bit_clear(data2,7);.................... if(bit_test(data1,0)).................... bit_set(data2,7);.................... data1 = data1 >>1;.................... *(((unsigned int8 *)(&y))+3) = data1;.................... *(((unsigned int8 *)(&y))+2) = data2;........................................ #endif.................... } while(res != y);1AA2: MOVFF 6E7,6ED1AA6: MOVFF 6E6,6EC1AAA: MOVFF 6E5,6EB1AAE: MOVFF 6E4,6EA1AB2: MOVFF 6E3,6F11AB6: MOVFF 6E2,6F01ABA: MOVFF 6E1,6EF1ABE: MOVFF 6E0,6EE1AC2: MOVLB 01AC4: RCALL 19481AC6: BTFSC FD8.21AC8: BRA 1ACE1ACA: MOVLB 61ACC: BRA 1A2E........................................ return(res);1ACE: MOVFF 6E4,001AD2: MOVFF 6E5,011AD6: MOVFF 6E6,021ADA: MOVFF 6E7,03.................... }1ADE: GOTO 1CCA (RETURN).................... //Overloaded functions for sqrt() for PCD.................... // Overloaded function sqrt() for data type - Float48.................... #if defined(__PCD__).................... float48 sqrt(float48 x).................... {.................... float48 y, res;.................... unsigned int16 data1,data2;.................... BYTE *p;........................................ #ifdef _ERRNO.................... if(x < 0).................... {.................... errno=EDOM;.................... }.................... #endif........................................ if( x<=0.0).................... return(0.0);........................................ y=x;........................................ #if !defined(__PCD__).................... p=&y;.................... (*p)=(BYTE)((((unsigned int16)(*p)) + 127) >> 1);.................... #endif........................................ #if defined(__PCD__).................... p = (((unsigned int8 *)(&y))+5);.................... data1 = *(((unsigned int8 *)(&y))+5);.................... data2 = *(((unsigned int8 *)(&y))+4);.................... rotate_left(&data1,1);.................... if(bit_test(data2,7)).................... bit_set(data1,0);.................... data1 = ((data1+127) >>1);.................... bit_clear(data2,7);.................... if(bit_test(data1,0)).................... bit_set(data2,7);.................... data1 = data1 >>1;.................... *(((unsigned int8 *)(&y))+5) = data1;.................... *(((unsigned int8 *)(&y))+4) = data2;........................................ #endif........................................ do {.................... res=y;.................... y+=(x/y);........................................ #if !defined(__PCD__).................... (*p)--;.................... #endif........................................ data1 = *(((unsigned int8 *)(&y))+5);.................... data2 = *(((unsigned int8 *)(&y))+4);.................... rotate_left(&data1,1);.................... if(bit_test(data2,7)).................... bit_set(data1,0);.................... data1--;.................... bit_clear(data2,7);.................... if(bit_test(data1,0)).................... bit_set(data2,7);.................... data1 = data1 >>1;.................... *(((unsigned int8 *)(&y))+5) = data1;.................... *(((unsigned int8 *)(&y))+4) = data2;........................................ } while(res != y);........................................ return(res);.................... }........................................ // Overloaded function sqrt() for data type - Float64.................... float64 sqrt(float64 x).................... {.................... float64 y, res;.................... unsigned int16 *p;.................... unsigned int16 temp1,temp2;........................................ #ifdef _ERRNO.................... if(x < 0).................... {.................... errno=EDOM;.................... }.................... #endif........................................ if( x<=0.0).................... return(0.0);........................................ y=x;.................... p= (((unsigned int16 *)(&y))+3);.................... temp1 = *p;.................... temp2 = *p;.................... bit_clear(temp1,15);.................... temp1 = (temp1>>4)+1023;.................... temp1 = temp1 >> 1;.................... temp1 = (temp1<<4) & 0xFFF0;.................... if(bit_test(temp2,15)).................... bit_set(temp1,15);.................... temp2 = temp2 & 0x000F;.................... temp1 ^= temp2;........................................ (*p) = temp1;........................................ do {.................... res=y;.................... y+=(x/y);.................... temp1 = *p;.................... temp2 = *p;.................... bit_clear(temp1,15);.................... temp1 = (temp1>>4);.................... temp1--;.................... temp1 = (temp1<<4) & 0xFFF0;.................... if(bit_test(temp2,15)).................... bit_set(temp1,15);.................... temp2 = temp2 & 0x000F;.................... temp1 ^= temp2;.................... (*p) = temp1;........................................ } while(res != y);........................................ return(res);.................... }.................... #endif........................................ ////////////////////////////// Trig Functions //////////////////////////////.................... #ifdef PI_DIV_BY_TWO.................... #undef PI_DIV_BY_TWO.................... #endif.................... #define PI_DIV_BY_TWO 1.5707963267948966.................... #ifdef TWOBYPI.................... #undef TWOBYPI.................... #define TWOBYPI 0.6366197723675813.................... #endif.................... ////////////////////////////////////////////////////////////////////////////.................... // float cos(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the cosine value of the angle x, which is in radian.................... // Date : 9/20/2001.................... //.................... float32 cos(float32 x).................... {.................... float32 y, t, t2 = 1.0;.................... unsigned int8 quad, i;.................... float32 frac;.................... float32 p[4] = {.................... -0.499999993585,.................... 0.041666636258,.................... -0.0013888361399,.................... 0.00002476016134.................... };........................................ if (x < 0) x = -x; // absolute value of input........................................ quad = (unsigned int8)(x / PI_DIV_BY_TWO); // quadrant.................... frac = (x / PI_DIV_BY_TWO) - quad; // fractional part of input.................... quad = quad % 4; // quadrant (0 to 3)........................................ if (quad == 0 || quad == 2).................... t = frac * PI_DIV_BY_TWO;.................... else if (quad == 1).................... t = (1-frac) * PI_DIV_BY_TWO;.................... else // should be 3.................... t = (frac-1) * PI_DIV_BY_TWO;........................................ y = 0.999999999781;.................... t = t * t;.................... for (i = 0; i <= 3; i++).................... {.................... t2 = t2 * t;.................... y = y + p[i] * t2;.................... }........................................ if (quad == 2 || quad == 1).................... y = -y; // correct sign........................................ return (y);.................... }............................................................ //Overloaded functions for cos() for PCD.................... // Overloaded function cos() for data type - Float48.................... #if defined(__PCD__).................... float48 cos(float48 x).................... {.................... float48 y, t, t2 = 1.0;.................... unsigned int8 quad, i;.................... float48 frac;.................... float48 p[4] = {.................... -0.499999993585,.................... 0.041666636258,.................... -0.0013888361399,.................... 0.00002476016134.................... };........................................ if (x < 0) x = -x; // absolute value of input........................................ quad = (unsigned int8)(x / PI_DIV_BY_TWO); // quadrant.................... frac = (x / PI_DIV_BY_TWO) - quad; // fractional part of input.................... quad = quad % 4; // quadrant (0 to 3)........................................ if (quad == 0 || quad == 2).................... t = frac * PI_DIV_BY_TWO;.................... else if (quad == 1).................... t = (1-frac) * PI_DIV_BY_TWO;.................... else // should be 3.................... t = (frac-1) * PI_DIV_BY_TWO;........................................ y = 0.999999999781;.................... t = t * t;.................... for (i = 0; i <= 3; i++).................... {.................... t2 = t2 * t;.................... y = y + p[i] * t2;.................... }........................................ if (quad == 2 || quad == 1).................... y = -y; // correct sign........................................ return (y);.................... }........................................ // Overloaded function cos() for data type - Float48.................... float64 cos(float64 x).................... {.................... float64 y, t, t2 = 1.0;.................... unsigned int8 quad, i;.................... float64 frac;.................... float64 p[4] = {.................... -0.499999993585,.................... 0.041666636258,.................... -0.0013888361399,.................... 0.00002476016134.................... };........................................ if (x < 0) x = -x; // absolute value of input........................................ quad = (unsigned int8)(x / PI_DIV_BY_TWO); // quadrant.................... frac = (x / PI_DIV_BY_TWO) - quad; // fractional part of input.................... quad = quad % 4; // quadrant (0 to 3)........................................ if (quad == 0 || quad == 2).................... t = frac * PI_DIV_BY_TWO;.................... else if (quad == 1).................... t = (1-frac) * PI_DIV_BY_TWO;.................... else // should be 3.................... t = (frac-1) * PI_DIV_BY_TWO;........................................ y = 0.999999999781;.................... t = t * t;.................... for (i = 0; i <= 3; i++).................... {.................... t2 = t2 * t;.................... y = y + p[i] * t2;.................... }........................................ if (quad == 2 || quad == 1).................... y = -y; // correct sign........................................ return (y);.................... }........................................ #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float sin(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the sine value of the angle x, which is in radian.................... // Date : 9/20/2001.................... //.................... float32 sin(float32 x).................... {.................... return cos(x - PI_DIV_BY_TWO);.................... }........................................ //Overloaded functions for sin() for PCD.................... // Overloaded function sin() for data type - Float48.................... #if defined(__PCD__).................... float48 sin(float48 x).................... {.................... return cos(x - PI_DIV_BY_TWO);.................... }........................................ // Overloaded function sin() for data type - Float48.................... float64 sin(float64 x).................... {.................... return cos(x - PI_DIV_BY_TWO);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float tan(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the tangent value of the angle x, which is in radian.................... // Date : 9/20/2001.................... //.................... float32 tan(float32 x).................... {.................... float32 c, s;........................................ c = cos(x);.................... if (c == 0.0).................... return (1.0e+36);........................................ s = sin(x);.................... return(s/c);.................... }.................... //Overloaded functions for tan() for PCD.................... // Overloaded function tan() for data type - Float48.................... #if defined(__PCD__).................... float48 tan(float48 x).................... {.................... float48 c, s;........................................ c = cos(x);.................... if (c == 0.0).................... return (1.0e+36);........................................ s = sin(x);.................... return(s/c);.................... }........................................ // Overloaded function tan() for data type - Float48.................... float64 tan(float64 x).................... {.................... float64 c, s;........................................ c = cos(x);.................... if (c == 0.0).................... return (1.0e+36);........................................ s = sin(x);.................... return(s/c);.................... }.................... #endif........................................ float32 const pas[3] = {0.49559947, -4.6145309, 5.6036290};.................... float32 const qas[3] = {1.0000000, -5.5484666, 5.6036290};........................................ float32 ASIN_COS(float32 x, unsigned int8 n).................... {.................... float32 y, res, r, y2;.................... int1 s;.................... #ifdef _ERRNO.................... if(x <-1 || x > 1).................... {.................... errno=EDOM;.................... }.................... #endif.................... s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y > 0.5).................... {.................... y = sqrt((1.0 - y)/2.0);.................... n += 2;.................... }........................................ y2=y*y;........................................ res = pas[0]*y2 + pas[1];.................... res = res*y2 + pas[2];........................................ r = qas[0]*y2 + qas[1];.................... r = r*y2 + qas[2];........................................ res = y*res/r;........................................ if (n & 2) // |x| > 0.5.................... res = PI_DIV_BY_TWO - 2.0*res;.................... if (s).................... res = -res;.................... if (n & 1) // take arccos.................... res = PI_DIV_BY_TWO - res;........................................ return(res);.................... }........................................ //Overloaded functions for ASIN_COS() for PCD.................... // Overloaded function ASIN_COS() for data type - Float48.................... #if defined(__PCD__).................... float48 ASIN_COS(float48 x, unsigned int8 n).................... {.................... float48 y, res, r, y2;.................... int1 s;.................... #ifdef _ERRNO.................... if(x <-1 || x > 1).................... {.................... errno=EDOM;.................... }.................... #endif.................... s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y > 0.5).................... {.................... y = sqrt((1.0 - y)/2.0);.................... n += 2;.................... }........................................ y2=y*y;........................................ res = pas[0]*y2 + pas[1];.................... res = res*y2 + pas[2];........................................ r = qas[0]*y2 + qas[1];.................... r = r*y2 + qas[2];........................................ res = y*res/r;........................................ if (n & 2) // |x| > 0.5.................... res = PI_DIV_BY_TWO - 2.0*res;.................... if (s).................... res = -res;.................... if (n & 1) // take arccos.................... res = PI_DIV_BY_TWO - res;........................................ return(res);.................... }........................................ // Overloaded function ASIN_COS() for data type - Float64.................... float64 ASIN_COS(float64 x, unsigned int8 n).................... {.................... float64 y, res, r, y2;.................... int1 s;.................... #ifdef _ERRNO.................... if(x <-1 || x > 1).................... {.................... errno=EDOM;.................... }.................... #endif.................... s = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y > 0.5).................... {.................... y = sqrt((1.0 - y)/2.0);.................... n += 2;.................... }........................................ y2=y*y;........................................ res = pas[0]*y2 + pas[1];.................... res = res*y2 + pas[2];........................................ r = qas[0]*y2 + qas[1];.................... r = r*y2 + qas[2];........................................ res = y*res/r;........................................ if (n & 2) // |x| > 0.5.................... res = PI_DIV_BY_TWO - 2.0*res;.................... if (s).................... res = -res;.................... if (n & 1) // take arccos.................... res = PI_DIV_BY_TWO - res;........................................ return(res);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float asin(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the arcsine value of the value x..................... // Date : N/A.................... //.................... float32 asin(float32 x).................... {.................... float32 r;........................................ r = ASIN_COS(x, 0);.................... return(r);.................... }.................... //Overloaded functions for asin() for PCD.................... // Overloaded function asin() for data type - Float48.................... #if defined(__PCD__).................... float48 asin(float48 x).................... {.................... float48 r;........................................ r = ASIN_COS(x, 0);.................... return(r);.................... }........................................ // Overloaded function asin() for data type - Float64.................... float64 asin(float64 x).................... {.................... float64 r;........................................ r = ASIN_COS(x, 0);.................... return(r);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float acos(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the arccosine value of the value x..................... // Date : N/A.................... //.................... float32 acos(float32 x).................... {.................... float32 r;........................................ r = ASIN_COS(x, 1);.................... return(r);.................... }.................... //Overloaded functions for acos() for PCD.................... // Overloaded function acos() for data type - Float48.................... #if defined(__PCD__).................... float48 acos(float48 x).................... {.................... float48 r;........................................ r = ASIN_COS(x, 1);.................... return(r);.................... }........................................ // Overloaded function acos() for data type - Float64.................... float64 acos(float64 x).................... {.................... float64 r;........................................ r = ASIN_COS(x, 1);.................... return(r);.................... }.................... #endif........................................ float32 const pat[4] = {0.17630401, 5.6710795, 22.376096, 19.818457};.................... float32 const qat[4] = {1.0000000, 11.368190, 28.982246, 19.818457};........................................ ////////////////////////////////////////////////////////////////////////////.................... // float atan(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : returns the arctangent value of the value x..................... // Date : N/A.................... //.................... float32 atan(float32 x).................... {.................... float32 y, res, r;.................... int1 s, flag;........................................ s = 0;.................... flag = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y > 1.0).................... {.................... y = 1.0/y;.................... flag = 1;.................... }........................................ res = pat[0]*y*y + pat[1];.................... res = res*y*y + pat[2];.................... res = res*y*y + pat[3];........................................ r = qat[0]*y*y + qat[1];.................... r = r*y*y + qat[2];.................... r = r*y*y + qat[3];........................................ res = y*res/r;............................................................ if (flag) // for |x| > 1.................... res = PI_DIV_BY_TWO - res;.................... if (s).................... res = -res;........................................ return(res);.................... }.................... //Overloaded functions for atan() for PCD.................... // Overloaded function atan() for data type - Float48.................... #if defined(__PCD__).................... float48 atan(float48 x).................... {.................... float48 y, res, r;.................... int1 s, flag;........................................ s = 0;.................... flag = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y > 1.0).................... {.................... y = 1.0/y;.................... flag = 1;.................... }........................................ res = pat[0]*y*y + pat[1];.................... res = res*y*y + pat[2];.................... res = res*y*y + pat[3];........................................ r = qat[0]*y*y + qat[1];.................... r = r*y*y + qat[2];.................... r = r*y*y + qat[3];........................................ res = y*res/r;............................................................ if (flag) // for |x| > 1.................... res = PI_DIV_BY_TWO - res;.................... if (s).................... res = -res;........................................ return(res);.................... }........................................ // Overloaded function atan() for data type - Float64.................... float64 atan(float64 x).................... {.................... float64 y, res, r;.................... int1 s, flag;........................................ s = 0;.................... flag = 0;.................... y = x;........................................ if (x < 0).................... {.................... s = 1;.................... y = -y;.................... }........................................ if (y > 1.0).................... {.................... y = 1.0/y;.................... flag = 1;.................... }........................................ res = pat[0]*y*y + pat[1];.................... res = res*y*y + pat[2];.................... res = res*y*y + pat[3];........................................ r = qat[0]*y*y + qat[1];.................... r = r*y*y + qat[2];.................... r = r*y*y + qat[3];........................................ res = y*res/r;............................................................ if (flag) // for |x| > 1.................... res = PI_DIV_BY_TWO - res;.................... if (s).................... res = -res;........................................ return(res);.................... }.................... #endif........................................ /////////////////////////////////////////////////////////////////////////////.................... // float atan2(float y, float x).................... /////////////////////////////////////////////////////////////////////////////.................... // Description :computes the principal value of arc tangent of y/x, using the.................... // signs of both the arguments to determine the quadrant of the return value.................... // Returns : returns the arc tangent of y/x..................... // Date : N/A.................... //........................................ float32 atan2(float32 y,float32 x).................... {.................... float32 z;.................... int1 sign;.................... unsigned int8 quad;.................... sign=0;.................... quad=0; //quadrant.................... quad=((y<=0.0)?((x<=0.0)?3:4):((x<0.0)?2:1));.................... if(y<0.0).................... {.................... sign=1;.................... y=-y;.................... }.................... if(x<0.0).................... {.................... x=-x;.................... }.................... if (x==0.0).................... {.................... if(y==0.0).................... {.................... #ifdef _ERRNO.................... {.................... errno=EDOM;.................... }.................... #endif.................... }.................... else.................... {.................... if(sign).................... {.................... return (-(PI_DIV_BY_TWO));.................... }.................... else.................... {.................... return (PI_DIV_BY_TWO);.................... }.................... }.................... }.................... else.................... {.................... z=y/x;.................... switch(quad).................... {.................... case 1:.................... {.................... return atan(z);.................... break;.................... }.................... case 2:.................... {.................... // return (atan(z)+PI_DIV_BY_TWO); //2L3122.................... return (PI-atan(z));.................... break;.................... }.................... case 3:.................... {.................... return (atan(z)-PI);.................... break;.................... }.................... case 4:.................... {.................... return (-atan(z));.................... break;.................... }.................... }.................... }.................... }........................................ //Overloaded functions for atan2() for PCD.................... // Overloaded function atan2() for data type - Float48.................... #if defined(__PCD__).................... float48 atan2(float48 y,float48 x).................... {.................... float48 z;.................... int1 sign;.................... unsigned int8 quad;.................... sign=0;.................... quad=0; //quadrant.................... quad=((y<=0.0)?((x<=0.0)?3:4):((x<0.0)?2:1));.................... if(y<0.0).................... {.................... sign=1;.................... y=-y;.................... }.................... if(x<0.0).................... {.................... x=-x;.................... }.................... if (x==0.0).................... {.................... if(y==0.0).................... {.................... #ifdef _ERRNO.................... {.................... errno=EDOM;.................... }.................... #endif.................... }.................... else.................... {.................... if(sign).................... {.................... return (-(PI_DIV_BY_TWO));.................... }.................... else.................... {.................... return (PI_DIV_BY_TWO);.................... }.................... }.................... }.................... else.................... {.................... z=y/x;.................... switch(quad).................... {.................... case 1:.................... {.................... return atan(z);.................... break;.................... }.................... case 2:.................... {.................... // return (atan(z)+PI_DIV_BY_TWO); //2L3122.................... return (PI-atan(z));.................... break;.................... }.................... case 3:.................... {.................... return (atan(z)-PI);.................... break;.................... }.................... case 4:.................... {.................... return (-atan(z));.................... break;.................... }.................... }.................... }.................... }........................................ // Overloaded function atan2() for data type - Float64.................... float64 atan2(float64 y,float64 x).................... {.................... float64 z;.................... int1 sign;.................... unsigned int8 quad;.................... sign=0;.................... quad=0; //quadrant.................... quad=((y<=0.0)?((x<=0.0)?3:4):((x<0.0)?2:1));.................... if(y<0.0).................... {.................... sign=1;.................... y=-y;.................... }.................... if(x<0.0).................... {.................... x=-x;.................... }.................... if (x==0.0).................... {.................... if(y==0.0).................... {.................... #ifdef _ERRNO.................... {.................... errno=EDOM;.................... }.................... #endif.................... }.................... else.................... {.................... if(sign).................... {.................... return (-(PI_DIV_BY_TWO));.................... }.................... else.................... {.................... return (PI_DIV_BY_TWO);.................... }.................... }.................... }.................... else.................... {.................... z=y/x;.................... switch(quad).................... {.................... case 1:.................... {.................... return atan(z);.................... break;.................... }.................... case 2:.................... {.................... // return (atan(z)+PI_DIV_BY_TWO); //2L3122.................... return (PI-atan(z));.................... break;.................... }.................... case 3:.................... {.................... return (atan(z)-PI);.................... break;.................... }.................... case 4:.................... {.................... return (-atan(z));.................... break;.................... }.................... }.................... }.................... }.................... #endif........................................ //////////////////// Hyperbolic functions ////////////////////........................................ ////////////////////////////////////////////////////////////////////////////.................... // float cosh(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : Computes the hyperbolic cosine value of x.................... // Returns : returns the hyperbolic cosine value of x.................... // Date : N/A.................... //........................................ float32 cosh(float32 x).................... {.................... return ((exp(x)+exp(-x))/2);.................... }.................... //Overloaded functions for cosh() for PCD.................... // Overloaded function cosh() for data type - Float48.................... #if defined(__PCD__).................... float48 cosh(float48 x).................... {.................... return ((exp(x)+exp(-x))/2);.................... }........................................ // Overloaded function cosh() for data type - Float64.................... float64 cosh(float64 x).................... {.................... return ((exp(x)+exp(-x))/2);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float sinh(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : Computes the hyperbolic sine value of x.................... // Returns : returns the hyperbolic sine value of x.................... // Date : N/A.................... //........................................ float32 sinh(float32 x).................... {........................................ return ((exp(x) - exp(-x))/2);.................... }.................... //Overloaded functions for sinh() for PCD.................... // Overloaded function sinh() for data type - Float48.................... #if defined(__PCD__).................... float48 sinh(float48 x).................... {........................................ return ((exp(x) - exp(-x))/2);.................... }........................................ // Overloaded function sinh() for data type - Float48.................... float64 sinh(float64 x).................... {........................................ return ((exp(x) - exp(-x))/2);.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float tanh(float x).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : Computes the hyperbolic tangent value of x.................... // Returns : returns the hyperbolic tangent value of x.................... // Date : N/A.................... //........................................ float32 tanh(float32 x).................... {.................... return(sinh(x)/cosh(x));.................... }.................... //Overloaded functions for tanh() for PCD.................... // Overloaded function tanh() for data type - Float48.................... #if defined(__PCD__).................... float48 tanh(float48 x).................... {.................... return(sinh(x)/cosh(x));.................... }........................................ // Overloaded function tanh() for data type - Float64.................... float64 tanh(float64 x).................... {.................... return(sinh(x)/cosh(x));.................... }.................... #endif........................................ ////////////////////////////////////////////////////////////////////////////.................... // float frexp(float x, signed int *exp).................... ////////////////////////////////////////////////////////////////////////////.................... // Description : breaks a floating point number into a normalized fraction and an integral.................... // power of 2. It stores the integer in the signed int object pointed to by exp..................... // Returns : returns the value x, such that x is a double with magnitude in the interval.................... // [1/2,1) or zero, and value equals x times 2 raised to the power *exp.If value is zero,.................... // both parts of the result are zero..................... // Date : N/A.................... //........................................ #define LOG2 .30102999566398119521.................... float32 frexp(float32 x, signed int8 *exp).................... {.................... float32 res;.................... int1 sign = 0;.................... if(x == 0.0).................... {.................... *exp=0;.................... return (0.0);.................... }.................... if(x < 0.0).................... {.................... x=-x;.................... sign=1;.................... }.................... if (x > 1.0).................... {.................... *exp=(ceil(log10(x)/LOG2));.................... res=x/(pow(2, *exp));.................... if (res == 1).................... {.................... *exp=*exp+1;.................... res=.5;.................... }.................... }.................... else.................... {.................... if(x < 0.5).................... {.................... *exp=-1;.................... res=x*2;.................... }.................... else.................... {.................... *exp=0;.................... res=x;.................... }.................... }.................... if(sign).................... {.................... res=-res;.................... }.................... return res;.................... }........................................ //Overloaded functions for frexp() for PCD.................... // Overloaded function frexp() for data type - Float48.................... #if defined(__PCD__).................... float48 frexp(float48 x, signed int8 *exp).................... {.................... float48 res;.................... int1 sign = 0;.................... if(x == 0.0).................... {.................... *exp=0;.................... return (0.0);.................... }.................... if(x < 0.0).................... {.................... x=-x;.................... sign=1;.................... }.................... if (x > 1.0).................... {.................... *exp=(ceil(log10(x)/LOG2));.................... res=x/(pow(2, *exp));.................... if (res == 1).................... {.................... *exp=*exp+1;.................... res=.5;.................... }.................... }.................... else.................... {.................... if(x < 0.5).................... {.................... *exp=-1;.................... res=x*2;.................... }.................... else.................... {.................... *exp=0;.................... res=x;.................... }.................... }.................... if(sign).................... {.................... res=-res;.................... }.................... return res;.................... }........................................ // Overloaded function frexp() for data type - Float64.................... float64 frexp(float64 x, signed int8 *exp).................... {.................... float64 res;.................... int1 sign = 0;.................... if(x == 0.0).................... {.................... *exp=0;.................... return (0.0);.................... }.................... if(x < 0.0).................... {.................... x=-x;.................... sign=1;.................... }.................... if (x > 1.0).................... {.................... *exp=(ceil(log10(x)/LOG2));.................... res=x/(pow(2, *exp));.................... if (res == 1).................... {.................... *exp=*exp+1;.................... res=.5;.................... }.................... }.................... else.................... {.................... if(x < 0.5).................... {.................... *exp=-1;.................... res=x*2;.................... }.................... else.................... {.................... *exp=0;.................... res=x;.................... }.................... }.................... if(sign).................... {.................... res=-res;.................... }.................... return res;.................... }.................... #endif........................................ //////////////////////////////////////////////////////////////////////////////.................... // float ldexp(float x, signed int *exp).................... //////////////////////////////////////////////////////////////////////////////.................... // Description : multiplies a floating point number by an integral power of 2..................... // Returns : returns the value of x times 2 raised to the power exp..................... // Date : N/A.................... //........................................ float32 ldexp(float32 value, signed int8 exp).................... {.................... return (value * pow(2,exp));.................... }.................... //Overloaded functions for ldexp() for PCD.................... // Overloaded function ldexp() for data type - Float48........................................ #if defined(__PCD__).................... float48 ldexp(float48 value, signed int8 exp).................... {.................... return (value * pow(2,exp));.................... }.................... // Overloaded function ldexp() for data type - Float64.................... float64 ldexp(float64 value, signed int8 exp).................... {.................... return (value * pow(2,exp));.................... }.................... #endif........................................ #endif............................................................ float quadraticerror(float average, float buf[], int16 size) // compute average quadratic error.................... {.................... int16 i;.................... float err=0;1AE2: MOVLB 61AE4: CLRF xD41AE6: CLRF xD51AE8: CLRF xD61AEA: CLRF xD7........................................ for(i=0; i<size; i++) err += (buf[i]-average)*(buf[i]-average); // sum quadratic errors1AEC: CLRF xD31AEE: CLRF xD21AF0: MOVF xD3,W1AF2: SUBWF xD1,W1AF4: BTFSS FD8.01AF6: BRA 1C481AF8: BNZ 1B021AFA: MOVF xD0,W1AFC: SUBWF xD2,W1AFE: BTFSC FD8.01B00: BRA 1C481B02: RLCF xD2,W1B04: MOVWF 021B06: RLCF xD3,W1B08: MOVWF 031B0A: RLCF 02,F1B0C: RLCF 03,F1B0E: MOVLW FC1B10: ANDWF 02,F1B12: MOVF 02,W1B14: ADDWF xCE,W1B16: MOVWF FE91B18: MOVF xCF,W1B1A: ADDWFC 03,W1B1C: MOVWF FEA1B1E: MOVFF FEF,6D81B22: MOVFF FEC,6D91B26: MOVFF FEC,6DA1B2A: MOVFF FEC,6DB1B2E: MOVFF FEA,6DD1B32: MOVFF FE9,6DC1B36: BSF FD8.11B38: MOVFF 6DB,6ED1B3C: MOVFF 6DA,6EC1B40: MOVFF 6D9,6EB1B44: MOVFF 6D8,6EA1B48: MOVFF 6CD,6F11B4C: MOVFF 6CC,6F01B50: MOVFF 6CB,6EF1B54: MOVFF 6CA,6EE1B58: MOVLB 01B5A: RCALL 14421B5C: MOVFF 6DD,FEA1B60: MOVFF 6DC,FE91B64: MOVFF 00,6D81B68: MOVFF 01,6D91B6C: MOVFF 02,6DA1B70: MOVFF 03,6DB1B74: MOVLB 61B76: RLCF xD2,W1B78: MOVWF 021B7A: RLCF xD3,W1B7C: MOVWF 031B7E: RLCF 02,F1B80: RLCF 03,F1B82: MOVLW FC1B84: ANDWF 02,F1B86: MOVF 02,W1B88: ADDWF xCE,W1B8A: MOVWF FE91B8C: MOVF xCF,W1B8E: ADDWFC 03,W1B90: MOVWF FEA1B92: MOVFF FEF,6DE1B96: MOVFF FEC,6DF1B9A: MOVFF FEC,6E01B9E: MOVFF FEC,6E11BA2: MOVFF FEA,6E31BA6: MOVFF FE9,6E21BAA: BSF FD8.11BAC: MOVFF 6E1,6ED1BB0: MOVFF 6E0,6EC1BB4: MOVFF 6DF,6EB1BB8: MOVFF 6DE,6EA1BBC: MOVFF 6CD,6F11BC0: MOVFF 6CC,6F01BC4: MOVFF 6CB,6EF1BC8: MOVFF 6CA,6EE1BCC: MOVLB 01BCE: RCALL 14421BD0: MOVFF 6E3,FEA1BD4: MOVFF 6E2,FE91BD8: MOVFF 6DB,6E71BDC: MOVFF 6DA,6E61BE0: MOVFF 6D9,6E51BE4: MOVFF 6D8,6E41BE8: MOVFF 03,6EB1BEC: MOVFF 02,6EA1BF0: MOVFF 01,6E91BF4: MOVFF 00,6E81BF8: RCALL 18521BFA: MOVFF FEA,6D91BFE: MOVFF FE9,6D81C02: BCF FD8.11C04: MOVFF 6D7,6ED1C08: MOVFF 6D6,6EC1C0C: MOVFF 6D5,6EB1C10: MOVFF 6D4,6EA1C14: MOVFF 03,6F11C18: MOVFF 02,6F01C1C: MOVFF 01,6EF1C20: MOVFF 00,6EE1C24: RCALL 14421C26: MOVFF 6D9,FEA1C2A: MOVFF 6D8,FE91C2E: MOVFF 03,6D71C32: MOVFF 02,6D61C36: MOVFF 01,6D51C3A: MOVFF 00,6D41C3E: MOVLB 61C40: INCF xD2,F1C42: BTFSC FD8.21C44: INCF xD3,F1C46: BRA 1AF0.................... err = sqrt((1/(float)size)*err);1C48: MOVFF 6D1,6D91C4C: MOVFF 6D0,6D81C50: MOVLB 01C52: CALL 140C1C56: MOVLB 61C58: CLRF xED1C5A: CLRF xEC1C5C: CLRF xEB1C5E: MOVLW 7F1C60: MOVWF xEA1C62: MOVFF 03,6F11C66: MOVFF 02,6F01C6A: MOVFF 01,6EF1C6E: MOVFF 00,6EE1C72: MOVLB 01C74: RCALL 16BA1C76: MOVFF 00,6D81C7A: MOVFF 01,6D91C7E: MOVFF 02,6DA1C82: MOVFF 03,6DB1C86: MOVFF 03,6E71C8A: MOVFF 02,6E61C8E: MOVFF 01,6E51C92: MOVFF 00,6E41C96: MOVFF 6D7,6EB1C9A: MOVFF 6D6,6EA1C9E: MOVFF 6D5,6E91CA2: MOVFF 6D4,6E81CA6: RCALL 18521CA8: MOVFF 00,6D81CAC: MOVFF 01,6D91CB0: MOVFF 02,6DA1CB4: MOVFF 03,6DB1CB8: MOVFF 03,6DF1CBC: MOVFF 02,6DE1CC0: MOVFF 01,6DD1CC4: MOVFF 00,6DC1CC8: BRA 19C21CCA: MOVFF 03,6D71CCE: MOVFF 02,6D61CD2: MOVFF 01,6D51CD6: MOVFF 00,6D4.................... return err;1CDA: MOVFF 6D4,001CDE: MOVFF 6D5,011CE2: MOVFF 6D6,021CE6: MOVFF 6D7,03.................... }1CEA: RETLW 00........................................ void main().................... {*1F62: CLRF FF81F64: BCF FD0.71F66: BSF 08.71F68: CLRF FEA1F6A: CLRF FE91F6C: MOVF FC1,W1F6E: ANDLW C01F70: IORLW 0F1F72: MOVWF FC11F74: MOVLW 071F76: MOVWF FB41F78: CLRF 211F7A: MOVLW 041F7C: MOVWF 221F7E: MOVLW 0C1F80: MOVWF 231F82: CLRF 241F84: CLRF 281F86: CLRF x7C1F88: CLRF x83........................................ float x[BUFLEN], y[BUFLEN], z[BUFLEN];.................... float xavg, yavg, zavg;.................... int i;.................... port_b_pullups(FALSE);1F8A: BSF FF1.7.................... setup_psp(PSP_DISABLED);1F8C: BCF FB0.4.................... setup_spi(SPI_SS_DISABLED);1F8E: BCF FC6.51F90: BCF F94.71F92: BSF F93.01F94: BCF F93.11F96: MOVLW 011F98: MOVWF FC61F9A: MOVLW 001F9C: MOVWF FC7.................... setup_wdt(WDT_OFF);1F9E: BCF FD1.0.................... setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);1FA0: MOVLW 871FA2: MOVWF FD5.................... setup_timer_1(T1_DISABLED);1FA4: CLRF FCD.................... setup_timer_2(T2_DISABLED,0,1);1FA6: MOVLW 001FA8: MOVWF FCA1FAA: MOVLW 001FAC: MOVWF FCB.................... setup_timer_3(T3_DISABLED|T3_DIV_BY_1);1FAE: CLRF FB1.................... setup_comparator(NC_NC_NC_NC);1FB0: MOVLW 071FB2: MOVWF FB41FB4: MOVF F92,W1FB6: MOVWF F921FB8: MOVLW 131FBA: MOVWF 001FBC: DECFSZ 00,F1FBE: BRA 1FBC1FC0: BRA 1FC21FC2: MOVF FB4,W1FC4: BCF FA1.6.................... setup_vref(FALSE);1FC6: CLRF FB5.................... enable_interrupts(INT_TIMER0);1FC8: BSF FF2.5.................... enable_interrupts(GLOBAL);1FCA: MOVLW C01FCC: IORWF FF2,F........................................ setup_adc_ports(AN0_TO_AN2|VSS_VREF);1FCE: MOVF FC1,W1FD0: ANDLW C01FD2: IORLW 1C1FD4: MOVWF FC1.................... setup_adc(ADC_CLOCK_DIV_32);1FD6: BCF FC0.01FD8: BSF FC0.11FDA: BCF FC0.21FDC: BSF FC0.71FDE: BSF FC2.0........................................ usb_init(); // initialise USB module1FE0: GOTO 1368........................................ while(!usb_cdc_connected());1FE4: BTFSS x80.01FE6: BRA 1FE4.................... time=0;1FE8: CLRF 1C1FEA: CLRF 1B1FEC: CLRF 1A1FEE: CLRF 19.................... set_timer0(0);1FF0: CLRF FD71FF2: CLRF FD6.................... printf(usb_cdc_putc,"time[s] X Xerr Y Yerr Z Zerr \n\r");1FF4: MOVLB 31FF6: CLRF xB11FF8: MOVF xB1,W1FFA: MOVLB 01FFC: CALL 021E2000: MOVLB 32002: INCF xB1,F2004: MOVWF 002006: MOVFF FE8,6DB200A: MOVLB 0200C: CALL 13D42010: MOVLW 252012: MOVLB 32014: SUBWF xB1,W2016: BNZ 1FF8........................................ while(usb_cdc_connected()) // pockej nez se pripoji seriovy port PC.................... {2018: MOVLB 0201A: BTFSS x80.0201C: BRA 25DE.................... for(i=0; i <BUFLEN; i++)201E: MOVLB 32020: CLRF xB02022: MOVF xB0,W2024: SUBLW 632026: BTFSS FD8.02028: BRA 2300.................... {.................... set_adc_channel(0);202A: MOVLW 00202C: MOVWF 01202E: MOVF FC2,W2030: ANDLW C32032: IORWF 01,W2034: MOVWF FC2.................... delay_us(10);2036: MOVLW 132038: MOVWF 00203A: DECFSZ 00,F203C: BRA 203A203E: BRA 20402040: CLRF 182042: BTFSC FF2.72044: BSF 18.72046: BCF FF2.7.................... x[i]=read_adc();2048: MOVLB 7204A: CLRF x49204C: MOVFF 3B0,7482050: CLRF x4B2052: MOVLW 042054: MOVWF x4A2056: MOVLB 02058: CALL 02BA205C: BTFSC 18.7205E: BSF FF2.72060: MOVFF 02,032064: MOVF 01,W2066: ADDLW 842068: MOVWF FE9206A: MOVLW 00206C: ADDWFC 02,W206E: MOVWF FEA2070: BSF FC2.12072: BTFSC FC2.12074: BRA 20722076: MOVFF FC4,6D9207A: MOVFF FC3,6D8207E: MOVLB 02080: CALL 140C2084: MOVFF 00,FEF2088: MOVFF 01,FEC208C: MOVFF 02,FEC2090: MOVFF 03,FEC2094: CLRF 182096: BTFSC FF2.72098: BSF 18.7209A: BCF FF2.7.................... xavg+=x[i];209C: MOVLB 7209E: CLRF x4920A0: MOVFF 3B0,74820A4: CLRF x4B20A6: MOVLW 0420A8: MOVWF x4A20AA: MOVLB 020AC: CALL 02BA20B0: BTFSC 18.720B2: BSF FF2.720B4: MOVFF 02,0320B8: MOVF 01,W20BA: ADDLW 8420BC: MOVWF FE920BE: MOVLW 0020C0: ADDWFC 02,W20C2: MOVWF FEA20C4: MOVFF FEF,0020C8: MOVFF FEC,0120CC: MOVFF FEC,0220D0: MOVFF FEC,0320D4: MOVFF FEA,3B220D8: MOVFF FE9,3B120DC: BCF FD8.120DE: MOVFF 3A7,6ED20E2: MOVFF 3A6,6EC20E6: MOVFF 3A5,6EB20EA: MOVFF 3A4,6EA20EE: MOVFF 03,6F120F2: MOVFF 02,6F020F6: MOVFF 01,6EF20FA: MOVFF 00,6EE20FE: CALL 14422102: MOVFF 3B2,FEA2106: MOVFF 3B1,FE9210A: MOVFF 03,3A7210E: MOVFF 02,3A62112: MOVFF 01,3A52116: MOVFF 00,3A4........................................ set_adc_channel(1);211A: MOVLW 04211C: MOVWF 01211E: MOVF FC2,W2120: ANDLW C32122: IORWF 01,W2124: MOVWF FC2.................... delay_us(10);2126: MOVLW 132128: MOVWF 00212A: DECFSZ 00,F212C: BRA 212A212E: BRA 21302130: CLRF 182132: BTFSC FF2.72134: BSF 18.72136: BCF FF2.7.................... y[i]=read_adc();2138: MOVLB 7213A: CLRF x49213C: MOVFF 3B0,7482140: CLRF x4B2142: MOVLW 042144: MOVWF x4A2146: MOVLB 02148: CALL 02BA214C: BTFSC 18.7214E: BSF FF2.72150: MOVFF 02,032154: MOVF 01,W2156: ADDLW 142158: MOVWF FE9215A: MOVLW 02215C: ADDWFC 02,W215E: MOVWF FEA2160: BSF FC2.12162: BTFSC FC2.12164: BRA 21622166: MOVFF FC4,6D9216A: MOVFF FC3,6D8216E: MOVLB 02170: CALL 140C2174: MOVFF 00,FEF2178: MOVFF 01,FEC217C: MOVFF 02,FEC2180: MOVFF 03,FEC2184: CLRF 182186: BTFSC FF2.72188: BSF 18.7218A: BCF FF2.7.................... yavg+=y[i];218C: MOVLB 7218E: CLRF x492190: MOVFF 3B0,7482194: CLRF x4B2196: MOVLW 042198: MOVWF x4A219A: MOVLB 0219C: CALL 02BA21A0: BTFSC 18.721A2: BSF FF2.721A4: MOVFF 02,0321A8: MOVF 01,W21AA: ADDLW 1421AC: MOVWF FE921AE: MOVLW 0221B0: ADDWFC 02,W21B2: MOVWF FEA21B4: MOVFF FEF,0021B8: MOVFF FEC,0121BC: MOVFF FEC,0221C0: MOVFF FEC,0321C4: MOVFF FEA,3B221C8: MOVFF FE9,3B121CC: BCF FD8.121CE: MOVFF 3AB,6ED21D2: MOVFF 3AA,6EC21D6: MOVFF 3A9,6EB21DA: MOVFF 3A8,6EA21DE: MOVFF 03,6F121E2: MOVFF 02,6F021E6: MOVFF 01,6EF21EA: MOVFF 00,6EE21EE: CALL 144221F2: MOVFF 3B2,FEA21F6: MOVFF 3B1,FE921FA: MOVFF 03,3AB21FE: MOVFF 02,3AA2202: MOVFF 01,3A92206: MOVFF 00,3A8........................................ set_adc_channel(2);220A: MOVLW 08220C: MOVWF 01220E: MOVF FC2,W2210: ANDLW C32212: IORWF 01,W2214: MOVWF FC2.................... delay_us(10);2216: MOVLW 132218: MOVWF 00221A: DECFSZ 00,F221C: BRA 221A221E: BRA 22202220: CLRF 182222: BTFSC FF2.72224: BSF 18.72226: BCF FF2.7.................... z[i]=read_adc();2228: MOVLB 7222A: CLRF x49222C: MOVFF 3B0,7482230: CLRF x4B2232: MOVLW 042234: MOVWF x4A2236: MOVLB 02238: CALL 02BA223C: BTFSC 18.7223E: BSF FF2.72240: MOVFF 02,032244: MOVF 01,W2246: ADDLW 202248: MOVWF FE9224A: MOVLW 05224C: ADDWFC 02,W224E: MOVWF FEA2250: BSF FC2.12252: BTFSC FC2.12254: BRA 22522256: MOVFF FC4,6D9225A: MOVFF FC3,6D8225E: MOVLB 02260: CALL 140C2264: MOVFF 00,FEF2268: MOVFF 01,FEC226C: MOVFF 02,FEC2270: MOVFF 03,FEC2274: CLRF 182276: BTFSC FF2.72278: BSF 18.7227A: BCF FF2.7.................... zavg+=z[i];227C: MOVLB 7227E: CLRF x492280: MOVFF 3B0,7482284: CLRF x4B2286: MOVLW 042288: MOVWF x4A228A: MOVLB 0228C: CALL 02BA2290: BTFSC 18.72292: BSF FF2.72294: MOVFF 02,032298: MOVF 01,W229A: ADDLW 20229C: MOVWF FE9229E: MOVLW 0522A0: ADDWFC 02,W22A2: MOVWF FEA22A4: MOVFF FEF,0022A8: MOVFF FEC,0122AC: MOVFF FEC,0222B0: MOVFF FEC,0322B4: MOVFF FEA,3B222B8: MOVFF FE9,3B122BC: BCF FD8.122BE: MOVFF 3AF,6ED22C2: MOVFF 3AE,6EC22C6: MOVFF 3AD,6EB22CA: MOVFF 3AC,6EA22CE: MOVFF 03,6F122D2: MOVFF 02,6F022D6: MOVFF 01,6EF22DA: MOVFF 00,6EE22DE: CALL 144222E2: MOVFF 3B2,FEA22E6: MOVFF 3B1,FE922EA: MOVFF 03,3AF22EE: MOVFF 02,3AE22F2: MOVFF 01,3AD22F6: MOVFF 00,3AC.................... }22FA: MOVLB 322FC: INCF xB0,F22FE: BRA 2022........................................ xavg=xavg/BUFLEN;2300: MOVFF 3A7,6ED2304: MOVFF 3A6,6EC2308: MOVFF 3A5,6EB230C: MOVFF 3A4,6EA2310: MOVLB 62312: CLRF xF12314: CLRF xF02316: MOVLW 482318: MOVWF xEF231A: MOVLW 85231C: MOVWF xEE231E: MOVLB 02320: CALL 16BA2324: MOVFF 03,3A72328: MOVFF 02,3A6232C: MOVFF 01,3A52330: MOVFF 00,3A4.................... yavg=yavg/BUFLEN;2334: MOVFF 3AB,6ED2338: MOVFF 3AA,6EC233C: MOVFF 3A9,6EB2340: MOVFF 3A8,6EA2344: MOVLB 62346: CLRF xF12348: CLRF xF0234A: MOVLW 48234C: MOVWF xEF234E: MOVLW 852350: MOVWF xEE2352: MOVLB 02354: CALL 16BA2358: MOVFF 03,3AB235C: MOVFF 02,3AA2360: MOVFF 01,3A92364: MOVFF 00,3A8.................... zavg=zavg/BUFLEN;2368: MOVFF 3AF,6ED236C: MOVFF 3AE,6EC2370: MOVFF 3AD,6EB2374: MOVFF 3AC,6EA2378: MOVLB 6237A: CLRF xF1237C: CLRF xF0237E: MOVLW 482380: MOVWF xEF2382: MOVLW 852384: MOVWF xEE2386: MOVLB 02388: CALL 16BA238C: MOVFF 03,3AF2390: MOVFF 02,3AE2394: MOVFF 01,3AD2398: MOVFF 00,3AC........................................ // odesli namerene hodnoty.................... printf(usb_cdc_putc, "%7.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f \n\r",((time << 16) + get_timer0())/15625.0, xavg, quadraticerror(xavg,x,BUFLEN), yavg, quadraticerror(yavg,y,BUFLEN), zavg, quadraticerror(zavg,z,BUFLEN)); //konstanta k je kvuli prevodu do rozzumnych jednotek [s]239C: MOVFF 1A,3B523A0: MOVFF 19,3B423A4: MOVLB 323A6: CLRF xB223A8: CLRF xB323AA: MOVF FD6,W23AC: MOVFF FD7,0323B0: ADDWF xB2,F23B2: MOVF FD7,W23B4: ADDWFC xB3,F23B6: MOVLW 0023B8: ADDWFC xB4,F23BA: ADDWFC xB5,F23BC: MOVFF 3B5,6C523C0: MOVFF 3B4,6C423C4: MOVFF 3B3,6C323C8: MOVFF 3B2,6C223CC: MOVLB 023CE: GOTO 181823D2: MOVFF 03,6ED23D6: MOVFF 02,6EC23DA: MOVFF 01,6EB23DE: MOVFF 00,6EA23E2: MOVLB 623E4: CLRF xF123E6: MOVLW 2423E8: MOVWF xF023EA: MOVLW 7423EC: MOVWF xEF23EE: MOVLW 8C23F0: MOVWF xEE23F2: MOVLB 023F4: CALL 16BA23F8: MOVFF 00,3B223FC: MOVFF 01,3B32400: MOVFF 02,3B42404: MOVFF 03,3B52408: MOVFF 3A7,6CD240C: MOVFF 3A6,6CC2410: MOVFF 3A5,6CB2414: MOVFF 3A4,6CA2418: MOVLB 6241A: CLRF xCF241C: MOVLW 84241E: MOVWF xCE2420: CLRF xD12422: MOVLW 642424: MOVWF xD02426: MOVLB 02428: CALL 1AE2242C: MOVFF 00,3B62430: MOVFF 01,3B72434: MOVFF 02,3B82438: MOVFF 03,3B9243C: MOVFF 3AB,6CD2440: MOVFF 3AA,6CC2444: MOVFF 3A9,6CB2448: MOVFF 3A8,6CA244C: MOVLW 02244E: MOVLB 62450: MOVWF xCF2452: MOVLW 142454: MOVWF xCE2456: CLRF xD12458: MOVLW 64245A: MOVWF xD0245C: MOVLB 0245E: CALL 1AE22462: MOVFF 00,3BA2466: MOVFF 01,3BB246A: MOVFF 02,3BC246E: MOVFF 03,3BD2472: MOVFF 3AF,6CD2476: MOVFF 3AE,6CC247A: MOVFF 3AD,6CB247E: MOVFF 3AC,6CA2482: MOVLW 052484: MOVLB 62486: MOVWF xCF2488: MOVLW 20248A: MOVWF xCE248C: CLRF xD1248E: MOVLW 642490: MOVWF xD02492: MOVLB 02494: CALL 1AE22498: MOVFF 00,3BE249C: MOVFF 01,3BF24A0: MOVFF 02,3C024A4: MOVFF 03,3C124A8: MOVLW 0624AA: MOVWF FE924AC: MOVFF 3B5,6D124B0: MOVFF 3B4,6D024B4: MOVFF 3B3,6CF24B8: MOVFF 3B2,6CE24BC: MOVLW 0324BE: MOVLB 624C0: MOVWF xD224C2: MOVLB 024C4: RCALL 1DB024C6: MOVLW 2024C8: MOVLB 624CA: MOVWF xDB24CC: MOVLB 024CE: CALL 13D424D2: MOVLW 0324D4: MOVWF FE924D6: MOVFF 3A7,6D124DA: MOVFF 3A6,6D024DE: MOVFF 3A5,6CF24E2: MOVFF 3A4,6CE24E6: MOVLB 624E8: MOVWF xD224EA: MOVLB 024EC: RCALL 1DB024EE: MOVLW 2024F0: MOVLB 624F2: MOVWF xDB24F4: MOVLB 024F6: CALL 13D424FA: MOVLW 0324FC: MOVWF FE924FE: MOVFF 3B9,6D12502: MOVFF 3B8,6D02506: MOVFF 3B7,6CF250A: MOVFF 3B6,6CE250E: MOVLB 62510: MOVWF xD22512: MOVLB 02514: RCALL 1DB02516: MOVLW 202518: MOVLB 6251A: MOVWF xDB251C: MOVLB 0251E: CALL 13D42522: MOVLW 032524: MOVWF FE92526: MOVFF 3AB,6D1252A: MOVFF 3AA,6D0252E: MOVFF 3A9,6CF2532: MOVFF 3A8,6CE2536: MOVLB 62538: MOVWF xD2253A: MOVLB 0253C: RCALL 1DB0253E: MOVLW 202540: MOVLB 62542: MOVWF xDB2544: MOVLB 02546: CALL 13D4254A: MOVLW 03254C: MOVWF FE9254E: MOVFF 3BD,6D12552: MOVFF 3BC,6D02556: MOVFF 3BB,6CF255A: MOVFF 3BA,6CE255E: MOVLB 62560: MOVWF xD22562: MOVLB 02564: RCALL 1DB02566: MOVLW 202568: MOVLB 6256A: MOVWF xDB256C: MOVLB 0256E: CALL 13D42572: MOVLW 032574: MOVWF FE92576: MOVFF 3AF,6D1257A: MOVFF 3AE,6D0257E: MOVFF 3AD,6CF2582: MOVFF 3AC,6CE2586: MOVLB 62588: MOVWF xD2258A: MOVLB 0258C: RCALL 1DB0258E: MOVLW 202590: MOVLB 62592: MOVWF xDB2594: MOVLB 02596: CALL 13D4259A: MOVLW 03259C: MOVWF FE9259E: MOVFF 3C1,6D125A2: MOVFF 3C0,6D025A6: MOVFF 3BF,6CF25AA: MOVFF 3BE,6CE25AE: MOVLB 625B0: MOVWF xD225B2: MOVLB 025B4: CALL 1DB025B8: MOVLW 2025BA: MOVLB 625BC: MOVWF xDB25BE: MOVLB 025C0: CALL 13D425C4: MOVLW 0A25C6: MOVLB 625C8: MOVWF xDB25CA: MOVLB 025CC: CALL 13D425D0: MOVLW 0D25D2: MOVLB 625D4: MOVWF xDB25D6: MOVLB 025D8: CALL 13D4.................... }25DC: BRA 201A.................... }25DE: SLEEPConfiguration Fuses:Word 1: CE3C IESO FCMEN HSPLL PLL5 CPUDIV4 USBDIVWord 2: 0E39 NOBROWNOUT WDT128 NOWDT BORV20 NOPUT VREGENWord 3: 8700 PBADEN CCP2C1 MCLR LPT1OSCWord 4: 00A1 STVREN NODEBUG NOLVP NOXINST ICPRTWord 5: C00F NOPROTECT NOCPD NOCPBWord 6: E00F NOWRT NOWRTD NOWRTC NOWRTBWord 7: 400F NOEBTR NOEBTRB