/** INCLUDES *******************************************************/ #if defined(__18F14K50) #include #else #include #endif #include "./USB/usb.h" #include "HardwareProfile.h" #include "./USB/usb_function_cdc.h" /** CONFIGURATION **************************************************/ #if defined(__18F14K50) #pragma config CPUDIV = NOCLKDIV ,USBDIV = OFF, FOSC = HS #pragma config PLLEN = OFF, PCLKEN = ON, HFOFST = OFF, DEBUG = OFF #pragma config PWRTEN = ON, BOREN = OFF, BORV = 30, MCLRE = ON #pragma config FCMEN = OFF, IESO = OFF, WDTEN = OFF, WDTPS = 1, LVP = OFF #pragma config XINST = OFF, STVREN = ON, BBSIZ = OFF #pragma config CP0 = OFF, CP1 = OFF, CPB = OFF, CPD = OFF #pragma config WRT0 = OFF, WRT1 = OFF, WRTC = OFF, WRTB = OFF, WRTD = OFF #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF #else _CONFIG1(WINDIS_OFF & FWDTEN_OFF & ICS_PGx1 & GCP_OFF & JTAGEN_OFF) _CONFIG2(IESO_ON & PLLDIV_DIV2 & PLL96MHZ_ON & FNOSC_FRCPLL & FCKSM_CSDCMD & OSCIOFNC_ON & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_NONE) _CONFIG3(WPFP_WPFP0 & SOSCSEL_IO & WUTSEL_LEG & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPENDMEM) _CONFIG4(DSWDTPS_DSWDTPS3 & DSWDTOSC_LPRC & RTCOSC_SOSC & DSBOREN_OFF & DSWDTEN_OFF) #endif /** I N C L U D E S **********************************************************/ #include "GenericTypeDefs.h" #include "Compiler.h" #include "usb_config.h" #include "./USB/usb_device.h" #include "./USB/usb.h" #include "HardwareProfile.h" /** V A R I A B L E S ********************************************************/ #pragma udata char USB_In_Buffer[64]; char USB_Out_Buffer[64]; BYTE old_sw1,old_sw2; /** P R I V A T E P R O T O T Y P E S ***************************************/ static void InitializeSystem(void); void UserInit(void); void ProcessIO(void); BOOL Switch1IsPressed(void); BOOL Switch2IsPressed(void); void USBDeviceTasks(void); void USBCBSendResume(void); /** VECTOR REMAPPING ***********************************************/ #if defined(__18F14K50) #if defined(USB_INTERRUPT) void YourHighPriorityISRCode(); void YourLowPriorityISRCode(); #pragma udata #define REMAPPED_RESET_VECTOR_ADDRESS 0x00 #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08 #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18 #pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS void Remapped_High_ISR (void) { _asm goto YourHighPriorityISRCode _endasm } #pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS void Remapped_Low_ISR (void) { _asm goto YourLowPriorityISRCode _endasm } #pragma code #pragma interrupt YourHighPriorityISRCode void YourHighPriorityISRCode() { USBDeviceTasks(); } #pragma interruptlow YourLowPriorityISRCode void YourLowPriorityISRCode() { } #endif #endif #if 0 void __attribute__((interrupt, auto_psv)) _T1Interrupt(void) { } #endif /** DECLARATIONS ***************************************************/ #pragma code /******************************************************************** * Function: void main(void) *********************************************************************/ void main(void) { InitializeSystem(); #if defined(USB_INTERRUPT) USBDeviceAttach(); #endif LATBbits.LATB4 = 0; //LED On PORTB = 0x10 ^ 0xff; while(1) { #if defined(USB_POLLING) // Check bus status and service USB interrupts. USBDeviceTasks(); #endif // Application-specific tasks. // Application related code may be added here, or in the ProcessIO() function. ProcessIO(); } } /******************************************************************** * Function: static void InitializeSystem(void) *******************************************************************/ static void InitializeSystem(void) { UserInit(); #if defined(USE_USB_BUS_SENSE_IO) tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h #endif #if defined(USE_SELF_POWER_SENSE_IO) tris_self_power = INPUT_PIN; // See HardwareProfile.h #endif USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware variables to known states. } /****************************************************************************** * Function: void UserInit(void) *****************************************************************************/ void UserInit(void) { #if defined(__18F14K50) /* 入出力ポート設定 */ TRISA = 0x00; TRISB = 0x00; TRISC = 0xf0; ADCON0bits.ADON = 0; //AD OFF #else unsigned int pll_startup_counter = 600; CLKDIV = 0x0000; // CPU:32MHz CLKDIVbits.PLLEN = 1; // 96MHz PLL On, while(pll_startup_counter--); RCONbits.SWDTEN = 0; //ウオッチドック・ソフトウエア OFF AD1PCFG = 0xFFFF; //AD OFF /* 入出力ポート設定 */ TRISA = 0x0000; // TRISB = 0x0003; // #endif led01 = 1; //LED OFF led02 = 1; //Initialize all of the push buttons mInitAllSwitches(); old_sw1 = sw1; old_sw2 = sw2; } /******************************************************************** * Function: void ProcessIO(void) *******************************************************************/ void ProcessIO(void) { BYTE numBytesRead; BYTE i; BYTE txbuff[ 10 ]; // User Application USB tasks if( (USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1) ) return; if( USBUSARTIsTxTrfReady() == TRUE ) //送信準備確認 TRUE:送信可能 FALSE:送信中 { if(Switch1IsPressed()) { led01 = 0; //LED1 ON putrsUSBUSART("Button 1 LED 1 On \r\n"); //ROM内文字列の送信準備、0x00(含む)デリミタ } if(Switch2IsPressed()) { led01 = 1; //LED1 OFF txbuff[ 0 ] = 'L'; txbuff[ 1 ] = 'E'; txbuff[ 2 ] = 'D'; txbuff[ 3 ] = ' '; txbuff[ 4 ] = 'O'; txbuff[ 5 ] = 'f'; txbuff[ 6 ] = 'f'; txbuff[ 7 ] = '\r'; txbuff[ 8 ] = '\n'; txbuff[ 9 ] = 0x00; putsUSBUSART( txbuff ); //RAM内文字列の送信準備、0x00(含む)デリミタ } } if( USBUSARTIsTxTrfReady() == TRUE ) //送信準備確認 { numBytesRead = getsUSBUSART( USB_Out_Buffer, 64 ); if( numBytesRead != 0 ) { for( i=0; i < numBytesRead; i++ ) { led02 = led02 ^ 1; //LED2 ON-OFF switch(USB_Out_Buffer[i]) { case 0x0A: case 0x0D: USB_In_Buffer[i] = USB_Out_Buffer[i]; break; default: USB_In_Buffer[i] = USB_Out_Buffer[i] + 1; break; } } putUSBUSART( USB_In_Buffer, numBytesRead ); //RAM内文字列の送信準備、バイナリデータ:文字数指定 } } CDCTxService(); //送信コマンド } /****************************************************************************** * Function: BOOL Switch1IsPressed(void) *****************************************************************************/ BOOL Switch1IsPressed(void) { if(sw1 != old_sw1) { old_sw1 = sw1; // Save new value if(sw1 == 0) // If pressed return TRUE; // Was pressed } return FALSE; // Was not pressed } /****************************************************************************** * Function: BOOL Switch2IsPressed(void) *****************************************************************************/ BOOL Switch2IsPressed(void) { if(sw2 != old_sw2) { old_sw2 = sw2; // Save new value if(sw2 == 0) // If pressed return TRUE; // Was pressed } return FALSE; // Was not pressed } // ****************************************************************************************************** // ************** USB Callback Functions **************************************************************** // ****************************************************************************************************** // The USB firmware stack will call the callback functions USBCBxxx() in response to certain USB related // events. For example, if the host PC is powering down, it will stop sending out Start of Frame (SOF) // packets to your device. In response to this, all USB devices are supposed to decrease their power // consumption from the USB Vbus to <2.5mA each. The USB module detects this condition (which according // to the USB specifications is 3+ms of no bus activity/SOF packets) and then calls the USBCBSuspend() // function. You should modify these callback functions to take appropriate actions for each of these // conditions. For example, in the USBCBSuspend(), you may wish to add code that will decrease power // consumption from Vbus to <2.5mA (such as by clock switching, turning off LEDs, putting the // microcontroller to sleep, etc.). Then, in the USBCBWakeFromSuspend() function, you may then wish to // add code that undoes the power saving things done in the USBCBSuspend() function. // The USBCBSendResume() function is special, in that the USB stack will not automatically call this // function. This function is meant to be called from the application firmware instead. See the // additional comments near the function. /****************************************************************************** * Function: void USBCBSuspend(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: Call back that is invoked when a USB suspend is detected * * Note: None *****************************************************************************/ void USBCBSuspend(void) { //Example power saving code. Insert appropriate code here for the desired //application behavior. If the microcontroller will be put to sleep, a //process similar to that shown below may be used: //ConfigureIOPinsForLowPower(); //SaveStateOfAllInterruptEnableBits(); //DisableAllInterruptEnableBits(); //EnableOnlyTheInterruptsWhichWillBeUsedToWakeTheMicro(); //should enable at least USBActivityIF as a wake source //Sleep(); //RestoreStateOfAllPreviouslySavedInterruptEnableBits(); //Preferrably, this should be done in the USBCBWakeFromSuspend() function instead. //RestoreIOPinsToNormal(); //Preferrably, this should be done in the USBCBWakeFromSuspend() function instead. //IMPORTANT NOTE: Do not clear the USBActivityIF (ACTVIF) bit here. This bit is //cleared inside the usb_device.c file. Clearing USBActivityIF here will cause //things to not work as intended. /* #if defined(__C30__) USBSleepOnSuspend(); #endif */ }