Example #1
0
/*
 * 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();
		}
	}
}
Example #2
0
File: error.c Project: ipeet/ubw32
//! Record an out-of-memory
static void _handle_nomem() {
    if(!_err_counts[UWRT_NOMEM]) {
        // All this only needs to be done the first time 
        mLED_3_On();
        ++_num_errs;  
        /* Fill struct */
        _nomem_desc.errno = 0;
        _nomem_desc.type = ERROR;
        _nomem_desc.status = UWRT_NOMEM;
        _nomem_desc.details = 0;
        /* Add to error list */
        if( !_err_list ) {
            // Special case: empty list
            _err_list = &_nomem_desc;
            _err_tail = _err_list;
        } else {
            _err_tail->next = &_nomem_desc;
            _err_tail = &_nomem_desc;
        }
    }
    if( _err_counts[UWRT_NOMEM]+1 != 0 ) ++_err_counts[UWRT_NOMEM];
}
Example #3
0
/********************************************************************
 * 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
Example #4
0
File: error.c Project: ipeet/ubw32
//! Add a new error
void err_add(enum err_type type, enum uwrt_err status, const char* details) {
    if(!_err_initd) _init_err();

    /* Make sure we're not exceeding the active error count limit */
    if( _num_errs + _num_warns >= MAX_ERRS ) {
        // Handle this as an out-of-memory
        _handle_nomem();
        return;
    }

    // Out-of-memory errors use a special statically-allocated descriptor 
    if( status == UWRT_NOMEM ) _handle_nomem();

    /* Allocate an error descriptor */
    err_desc_t * desc = 0;
    if( (status!=UWRT_SUCCESS) && (status!=UWRT_ERRNO) && (_err_counts[status]) ) {
        /* If this has a uwrt_err that isn't ERRNO or SUCCESS, and we've seen this
         * error before, we just need to record the additional occurrence, since
         * we don't store replicas */
        if( _err_counts[status]+1 != 0 ) ++_err_counts[status];
        return;
    } else {
        desc = malloc(sizeof(err_desc_t));
        if( !desc ) {
            _handle_nomem();
            return;
        }
    }

    /* Trip appropriate LED, update appropriate count */
    switch(type) {
        case ERROR:
            mLED_3_On();
            ++_num_errs;
            break;
        case WARNING:
        default:  
            mLED_4_On();
            ++_num_warns;
            break;
    }

    /* Fill descriptor */
    desc->errno = errno;
    desc->type = type;
    desc->status = status;
    desc->details = details;
    desc->next = 0;

    // Record occurrence
    if(_err_counts[status]+1 != 0) ++_err_counts[status];

    /* Add descriptor to error list */
    if( !_err_list ) {
        // Special case: empty list
        _err_list = desc;
        _err_tail = desc;
    } else {
        _err_tail->next = desc;
        _err_tail = desc;
    }
}
Example #5
0
/******************************************************************************
 * 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