void DisArmCDCInDB(void) {
    WaitInReady();
    //Inbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim In buffer
    Inbdp->BDADDR = &cdc_In_buffer[0];
    Inbdp->BDCNT = CDC_BUFFER_SIZE;
    //Inbdp->BDSTAT |= UOWN;
}
void ArmCDCInDB(void) {
    WaitInReady();
    //Inbdp->BDSTAT &= ~UOWN; // JTR3 immediately reclaim In buffer
    Inbdp->BDADDR = &cdc_In_bufferA[0];
    Inbdp->BDCNT = CDC_BUFFER_SIZE;
    //Inbdp->BDSTAT |= UOWN;
    IsInBufferA = 0xFF;
    InPtr = cdc_In_bufferA;
}
BYTE SendCDC_In_ArmNext(BYTE count) {

    CDCFunctionError = 0;
    /*
        if (WaitInReady()) {
     */
    WaitInReady();
    if (IsInBufferA) {
        Inbdp->BDADDR = cdc_In_bufferA;
        InPtr = cdc_In_bufferB;
    } else {
        Inbdp->BDADDR = cdc_In_bufferB;
        InPtr = cdc_In_bufferA;
    }
    Inbdp->BDCNT = count;
    Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN;
    IsInBufferA ^= 0xFF;
    /*
        } else {
            CDCFunctionError = 1;
        }
     */
    return CDCFunctionError;
}
void SendZLP(void) {
    WaitInReady();
    Inbdp->BDCNT = 0;
    Inbdp->BDSTAT = ((Inbdp->BDSTAT ^ DTS) & DTS) | UOWN | DTSEN;
}
Beispiel #5
0
void main(void)
{
    unsigned char c,x,y,led;
    unsigned long l;
    
	  // Default all pins to digital
    ADCON1 |= 0x0F;

    // Disable USB external transceiver to allow RB1/RB2
    UCFGbits.UTRDIS = 0;
    
    // set pin to output
    TRISBbits.TRISB0 = 0;
    TRISBbits.TRISB1 = 0;
    TRISBbits.TRISB2 = 0;
    TRISBbits.TRISB3 = 0;

    LED0 = 1;
    LED1 = LED2 = LED3 = 0;
    
    // TODO: Remove magic with macro
    usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS);
    usb_start();
    
    initCDC();
    usbbufflush();
    
    do {
        if (!TestUsbInterruptEnabled())
            USBDeviceTasks();

        switch(usb_device_state){
            case DETACHED_STATE:
                LED1 = 0; LED2 = 0; LED3 = 0;
            case ATTACHED_STATE:
                LED1 = 0; LED2 = 0; LED3 = 1;
            case POWERED_STATE:
                LED1 = 0; LED2 = 1; LED3 = 0;
            case DEFAULT_STATE:
                LED1 = 0; LED2 = 1; LED3 = 1;
            case ADR_PENDING_STATE:
                LED1 = 1; LED2 = 0; LED3 = 0;
            case ADDRESS_STATE:
                LED1 = 1; LED2 = 0; LED3 = 1;
            case CONFIGURED_STATE:
                LED1 = 1; LED2 = 1; LED3 = 0;
        }
    } while (usb_device_state < CONFIGURED_STATE);

    while(1);
#if 0

    // Initiate Osc with 8MHz (only relevant if you use the internal oscillator)
    //OSCCON = 0x70;
    
    
    
    initCDC();
    
    
    // TODO: Remove magic with macro
    usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS);

    usb_start();

#if defined (USB_INTERRUPTS)
    //EnableUsbInterrupt(USB_TRN + USB_SOF + USB_UERR + USB_URST);
    EnableUsbInterrupt(USB_STALL + USB_IDLE + USB_TRN + USB_ACTIV + USB_SOF + USB_UERR + USB_URST);
    EnableUsbInterrupts();
#endif

    usbbufflush(); //flush USB input buffer system

    led = UCFGbits.UTRDIS;
    
    
    LED0=0;
    LED1=0;
    LED2=0;
    LED3=0;
    
    while(1){
      LED1 = LED1 ? 0 : 1;
      for(l = 0; l < 10; ++l);
    }
    
    LED0=1;
    LED1=1;
    LED2=1;
    LED3=1;
    delay_ms(500);    
#if 0
    while(1)
    {
        LED0 = 1;
        delay_ms(250);
        LED0 = 0;
        delay_ms(250);
    }
#endif
    LED0 = 0;
    LED1 = 0;
    LED2 = 0;
    LED3 = 0;
    delay_ms(500);
    while(1);
    
    do
    {   LED2=1;
        LED3 = 0;
#ifndef USB_INTERRUPTS
        //service USB tasks
        //Guaranteed one pass in polling mode
        //even when usb_device_state == CONFIGURED_STATE
        if (!TestUsbInterruptEnabled())
        {
            USBDeviceTasks();
            LED3 = 1;
        }
#endif

        if ((usb_device_state < DEFAULT_STATE))
        { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){
          LED0=1;
          LED1=0;
        } else if (usb_device_state < CONFIGURED_STATE)
        {
          LED0=1;
          LED1=1;
        }else if((usb_device_state == CONFIGURED_STATE))
        {
          LED0=0;
          LED1=1;
        }
        LED2=0;
        delay_ms(1000);
    } while (usb_device_state < CONFIGURED_STATE);

    LED0 = 0;
    LED1 = 0;
    LED2 = 1;
    #if 1

    while(1)
    {
        LED0 = led;
        led = led ? 0 : 1;
#ifndef USB_INTERRUPTS
        //service USB tasks
        //Guaranteed one pass in polling mode
        //even when usb_device_state == CONFIGURED_STATE
        if (!TestUsbInterruptEnabled())
            USBDeviceTasks();
#endif
        usbbufservice(); //service USB buffer system

        if (usbbufgetbyte(&c) == 1)
        {
            WaitInReady();
            cdc_In_buffer[0] = c; //answer OK
            putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1);
        }

    }
    
    #endif
    
    // Disable USB external transceiver to allow RB1/RB2
    UCFGbits.UTRDIS = 0;
    
    // set pin to output
    TRISBbits.TRISB0 = 0;
    TRISBbits.TRISB1 = 0;
    TRISBbits.TRISB2 = 0;
    
    // start a loop blinking the led
    while(1)
    {
        LED0 = 1;
        delay_ms(250);
        LED0 = 0;
        delay_ms(250);
    }
#endif
}
void main(void){  
	u8 i,cmd, param[9],c;
	u16 temp; // TODO to be removed later on
	long l;
	signed int x, y, z;
	signed char x8, y8, z8;
	static unsigned char ledtrig;

    initCDC(); // JTR this function has been highly modified It no longer sets up CDC endpoints.

    init();			//setup the crystal, pins
    mma_init();

    usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS); // TODO: Remove magic with macro
    usb_start();
    usbbufflush(); //flush USB input buffer system



    i = mma_read( MMA_I2CAD );
	//if((i&~0b10000000)!=0x1d)while(1);
    mma_write( MMA_I2CAD, 0x1d | MMA_I2CDIS_bit ); // disable I2C

    i = mma_read( MMA_CTL1 );
    mma_write( MMA_CTL1, i |= MMA_DFBW_bit ); // high output data rate

    mma_write( MMA_MCTL, 0b00000101 ); // 2g range; measurement mode

    //mma_calibrate_offset( 0, 0, 0 );


	//LEDs
	//PWM on cathode
	TRISAbits.TRISA0=0;
	LATAbits.LATA0=1; //enable cathode

	//setup LEDs
	LATB=0;
	TRISB=0;


	ledtrig=1; //only shut LED off once
    //	Never ending loop services each task in small increments
    while (1) {
        do {
            if (!TestUsbInterruptEnabled()) //JTR3 added
                USBDeviceTasks(); ////service USB tasks Guaranteed one pass in polling mode even when usb_device_state == CONFIGURED_STATE
            if ((usb_device_state < DEFAULT_STATE)) { // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){
                //LedOff();
            } else if (usb_device_state < CONFIGURED_STATE) {
                //LedOn();
            }else if((ledtrig==1) && (usb_device_state == CONFIGURED_STATE)){
				//LedOff();
				ledtrig=0;
			}
        } while (usb_device_state < CONFIGURED_STATE);

		usbbufservice();//load any USB data into byte buffer

		if(usbbufgetbyte(&inByte) == 1){

			switch(inByte){//switch on the command
				case 0x00:
					WaitInReady();  //it's always ready, but this could be done better
					param[0]='B';
					param[1]='B';
					param[2]='I';
					param[3]='O';
					param[4]='1';
					putUnsignedCharArrayUsbUsart(param,5);
					break;
				case 0x01:
			    	mma_get_average8( 4, &x8, &y8, &z8 );
					param[0]=x8;
					param[1]=y8;
					param[2]=z8;
					WaitInReady();  //it's always ready, but this could be done better
					putUnsignedCharArrayUsbUsart(param,3);
					break;
				default: //error
					break;
			}
	
		}//if data
	}//while
//#endif

}//end main