//! Remove an error from the list void err_remove() { if( !_err_list ) return; // No error to remove err_desc_t *removed = _err_list; _err_list = _err_list->next; if( !_err_list ) { // Reached end _err_tail = 0; } _err_counts[removed->status] = 0; /* Decrement count, possibly turn off the respective LED */ switch(removed->type) { case ERROR: _num_errs--; if(_num_errs<=0) mLED_3_Off(); break; case WARNING: default: _num_warns--; if(_num_warns<=0) mLED_4_Off(); break; } // Don't free the special nomem descriptor, it's static if( removed == &_nomem_desc ) { // Clear the counter, since we've removed the error. _nomem_count = 0; return; } free(removed); }
/* * Main program entry point. */ int main (void) { AD1PCFG = 0xFFFF; //Initialize all of the LED pins LATE |= 0x000F; TRISE &= 0xFFF0; USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware //variables to known states. PMCON = 0; for (;;) { // Check bus status and service USB interrupts. USBDeviceTasks(); // Interrupt or polling method. If using polling, must call // this function periodically. This function will take care // of processing and responding to SETUP transactions // (such as during the enumeration process when you first // plug in). USB hosts require that USB devices should accept // and process SETUP packets in a timely fashion. Therefore, // when using polling, this function should be called // frequently (such as once about every 100 microseconds) at any // time that a SETUP packet might reasonably be expected to // be sent by the host to your device. In most cases, the // USBDeviceTasks() function does not take very long to // execute (~50 instruction cycles) before it returns. // Application-specific tasks. // Blink the LEDs according to the USB device status BlinkUSBStatus(); // User Application USB tasks if (USBDeviceState >= CONFIGURED_STATE && ! (U1PWRC & PIC32_U1PWRC_USUSPEND)) { unsigned nbytes_read; static unsigned char inbuf[64], outbuf[64]; static unsigned led3_count = 0; // Pull in some new data if there is new data to pull in nbytes_read = getsUSBUSART ((char*) inbuf, 64); if (nbytes_read != 0) { snprintf (outbuf, sizeof(outbuf), "Received %d bytes: %02x...\r\n", nbytes_read, inbuf[0]); putUSBUSART ((char*) outbuf, strlen (outbuf)); mLED_2_Toggle(); mLED_3_On(); led3_count = 10000; } if (led3_count) { // Turn off LED3 when timeout expired. led3_count--; if (led3_count == 0) mLED_3_Off(); } CDCTxService(); } } }
//! Remove all errors void err_clear() { while(err_peek()) err_remove(); /* Make sure counts and LEDs are reset */ _num_errs = 0; _num_warns = 0; mLED_3_Off(); mLED_4_Off(); }
/******************************************************************** * Function: void BlinkUSBStatus(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: BlinkUSBStatus turns on and off LEDs * corresponding to the USB device state. * * Note: mLED macros can be found in HardwareProfile.h * USBDeviceState is declared and updated in * usb_device.c. *******************************************************************/ void BlinkUSBStatus(void) { static WORD led_count=0; char a=0; if(led_count == 0)led_count = 10000U; led_count--; #define mLED_Both_Off() {mLED_1_Off();mLED_2_Off();} #define mLED_Both_On() {mLED_1_On();mLED_2_On();} #define mLED_Only_1_On() {mLED_1_On();mLED_2_Off();} #define mLED_Only_2_On() {mLED_1_Off();mLED_2_On();} a = is_clear(); if(a=1) { mLED_3_On(); } else { mLED_3_Off(); } if(USBSuspendControl == 1) { if(led_count==0) { mLED_1_Toggle(); if(mGetLED_1()) { mLED_2_On(); } else { mLED_2_Off(); } }//end if } else { if(USBDeviceState == DETACHED_STATE) { mLED_Both_Off(); } else if(USBDeviceState == ATTACHED_STATE) { mLED_Both_On(); } else if(USBDeviceState == POWERED_STATE) { mLED_Only_1_On(); } else if(USBDeviceState == DEFAULT_STATE) { mLED_Only_2_On(); } else if(USBDeviceState == ADDRESS_STATE) { if(led_count == 0) { mLED_1_Toggle(); mLED_2_Off(); }//end if } else if(USBDeviceState == CONFIGURED_STATE) { if(led_count==0) { mLED_1_Toggle(); if(mGetLED_1()) { mLED_2_Off(); } else { mLED_2_On(); } }//end if }//end if(...) }//end if(UCONbits.SUSPND...) }//end BlinkUSBStatus
/****************************************************************************** * Function: void ServiceRequests(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: USB traffic can be generated * * Overview: This function takes in the commands from the PC from the * application and executes the commands requested * * Note: None *****************************************************************************/ void ServiceRequests(void) { BYTE index; //Check to see if data has arrived if(!USBHandleBusy(USBGenericOutHandle)) { //if the handle is no longer busy then the last //transmission is complete counter = 0; INPacket.CMD=OUTPacket.CMD; INPacket.len=OUTPacket.len; //process the command switch(OUTPacket.CMD) { case READ_VERSION: //dataPacket._byte[1] is len INPacket._byte[2] = MINOR_VERSION; INPacket._byte[3] = MAJOR_VERSION; counter=0x04; break; case ID_BOARD: counter = 0x01; if(OUTPacket.ID == 0) { mLED_3_Off();mLED_4_Off(); } else if(OUTPacket.ID == 1) { mLED_3_Off();mLED_4_On(); } else if(OUTPacket.ID == 2) { mLED_3_On();mLED_4_Off(); } else if(OUTPacket.ID == 3) { mLED_3_On();mLED_4_On(); } else counter = 0x00; break; case UPDATE_LED: #if defined(PIC18F87J50_PIM) || defined(PIC18F46J50_PIM) || defined(PIC18F47J53_PIM) blinkStatusValid = FALSE; #endif // LED1 & LED2 are used as USB event indicators. if(OUTPacket.led_num == 3) { if(OUTPacket.led_status) { mLED_3_On(); } else { mLED_3_Off(); } counter = 0x01; }//end if else if(OUTPacket.led_num == 4) { if(OUTPacket.led_status) { mLED_4_On(); } else { mLED_4_Off(); } counter = 0x01; }//end if else break; case SET_TEMP_REAL: temp_mode = TEMP_REAL_TIME; ResetTempLog(); counter = 0x01; break; case RD_TEMP: if(AcquireTemperature()) { INPacket._byte[1] = temperature.v[0]; INPacket._byte[2] = temperature.v[1]; counter=0x03; }//end if break; case SET_TEMP_LOGGING: temp_mode = TEMP_LOGGING; ResetTempLog(); counter=0x01; break; case RD_TEMP_LOGGING: counter = (valid_temp<<1)+2; // Update count in byte INPacket.len = (valid_temp<<1); for(index = valid_temp; index > 0; index--) { if(pTemp == 0) pTemp = 29; else pTemp--; INPacket._word[index] = temp_data[pTemp]; }//end for ResetTempLog(); // Once read, log will restart break; case RD_POT: { WORD_VAL w; mInitPOT(); w = ReadPOT(); INPacket._byte[1] = w.v[0]; INPacket._byte[2] = w.v[1]; counter=0x03; } break; case RESET: Reset(); break; default: break; }//end switch() if(counter != 0) { if(!USBHandleBusy(USBGenericInHandle)) { USBGenericInHandle = USBGenWrite(USBGEN_EP_NUM,(BYTE*)&INPacket,counter); } }//end if //Re-arm the OUT endpoint for the next packet USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE); }//end if }//end ServiceRequests