/******************************************************************************* * Function Name: USBFS_1_CyBtldrCommStart ****************************************************************************//** * * This function performs all required initialization for the USBFS component, * waits on enumeration, and enables communication. * * \sideeffect * This function starts the USB with 3V or 5V operation. * * \reentrant * No. * *******************************************************************************/ void USBFS_1_CyBtldrCommStart(void) { /* Enable Global Interrupts. Interrupts are mandatory for USBFS component operation. */ CyGlobalIntEnable; /* Start USBFS Operation: device 0 and with 5V or 3V operation depend on Voltage Configuration in DWR. */ USBFS_1_Start(0u, USBFS_1_DWR_POWER_OPERATION); /* USB component started, the correct enumeration will be checked in the first Read operation. */ USBFS_1_started = 1u; }
void main() { uint8 counter = 0u; /* Enable Global Interrupts */ CyGlobalIntEnable; /* Set user defined Serial Number string descriptor */ //USBFS_1_SerialNumString(&bSNstring[0u]); /* Start USBFS device 0 with 3V operation */ USBFS_1_Start(0u, USBFS_1_5V_OPERATION); /* Wait for Device to enumerate */ while(!USBFS_1_GetConfiguration()); /* Enumeration is done, load endpoint 1. Do not toggle the first time. */ USBFS_1_LoadInEP(MOUSE_ENDPOINT, mouseData, MOUSE_DATA_LEN); while(1) { /* Wait for ACK before loading data */ while(!USBFS_1_GetEPAckState(MOUSE_ENDPOINT)); /* ACK has occurred, load the endpoint and toggle the data bit */ USBFS_1_LoadInEP(1u, mouseData, MOUSE_DATA_LEN); /* When our counts hits 128 */ if(counter == 128u) { /* Start moving the mouse to the right */ mouseData[1u] = 40u; //mouseData[2u] |= 0x01; //mouseData[2u] &= ~0x01; mouseData[2u] |= 0x01; mouseData[2u] |= 0x02; mouseData[2u] |= 0x04; //mouseData[2u] |= 0x03; //mouseData[2u] &= ~0x03; //mouseData[2u] |= 0x04; //mouseData[2u] &= ~0x04; //mouseData[2u] |= 0x05; //mouseData[2u] &= ~0x05; } else if(counter == 255u) { /* Start moving the mouse to the left */ //mouseData[1u] = (uint8)-(int8)CURSOR_STEP; //mouseData[2u] &= ~0x01; //mouseData[2u] &= ~0x02; mouseData[2u] &= ~0x04; mouseData[1u] = 0u; } //CyDelay(200); /*mouseData[2u] &= ~0x02; CyDelay(200); mouseData[2u] &= ~0x03; CyDelay(200); mouseData[2u] &= ~0x04; CyDelay(200); mouseData[2u] &= ~0x05; CyDelay(200);*/ //} counter++; } }
void main() { uint8 state, var_mask, ADC_in, auto_mode, continous_mode; uint8 DigitsPrecision, MaximumWeight; int16 MaxAvgReading = null, \ MinAvgReading = null, \ ScaledRange = null; //uint8 Digit[5], ScaledRange, LastFractionResult, Remainder; /* Start the components */ ADC_DelSig_1_Start(); //ADC_DelSig_1_IRQ_Start(); //disable for manual polling instead so can single step CYGlobalIntEnable; //enable for ADC and USB interrupts /* Start the ADC conversion */ ADC_DelSig_1_StartConvert(); ADC_DelSig_1_IRQ_Disable(); //disable for manual polling instead so can single step /* Start USBFS Operation with 5V operation */ USBFS_1_Start(0u, USBFS_1_5V_OPERATION); //USBFS_1_5V_OPERATION); /* Wait for Device to enumerate i.e. detects USB settings from host PC */ while(USBFS_1_GetConfiguration() != 0u); /* Enumeration is done, enable OUT endpoint for receive data from Host */ USBFS_1_EnableOutEP(OUT_EP); state=0u; for(;;) // this is infinite state loop { if(state==0u) { /* Check that configuration is changed (there is only one configured on the USBFS )*/ if(USBFS_1_IsConfigurationChanged() != 0u) { /* Re-enable endpoint when device is configured */ if(USBFS_1_GetConfiguration() != 0u) { USBFS_1_EnableOutEP(OUT_EP); } } /* GET MODE STATE Read USB PC host application*/ if(USBFS_1_GetEPState(OUT_EP) == USBFS_1_OUT_BUFFER_FULL) { /* Read received bytes count */ length = USBFS_1_GetEPCount(OUT_EP); /* Unload the OUT buffer */ USBFS_1_ReadOutEP(OUT_EP, &in_buffer[0u], length); state= in_buffer[MODE_BYTE]; var_mask=in_buffer[SET_VARS_MASK_BYTE]; if(var_mask & CONTINOUS_MODE_MASK_BIT) continous_mode = TRUE; else continous_mode = FALSE; } } if(state & INIT_SCALE_VARS) { if(var_mask & MAX_WEIGHT_VAR_MASK_BIT) MaximumWeight=in_buffer[MAX_WEIGHT_VAR_BYTE]; if(MaximumWeight<MIN_WEIGHT_SCALE) MaximumWeight=MIN_WEIGHT_SCALE; if(MaximumWeight>MAX_WEIGHT_SCALE) MaximumWeight=MAX_WEIGHT_SCALE; if(var_mask & PRECISION_VAR_MASK_BIT) DigitsPrecision=in_buffer[PRECISION_VAR_BYTE]; if(DigitsPrecision<MIN_PRECISION_DIGITS) DigitsPrecision=MIN_PRECISION_DIGITS; if(DigitsPrecision>MAX_PRECISION_DIGITS) DigitsPrecision=MAX_PRECISION_DIGITS; if(var_mask & AUTO_MODE_MASK_BIT) auto_mode=TRUE; else auto_mode=FALSE; state=0u; } if(state & GET_SCALE_LOW_LIMIT) { MinAvgReading = get_adc_average(NUMBER_SAMPLES); clear_buffer(out_buffer,BUF_SIZE); out_buffer[0u]=(uint8)MinAvgReading; //respond with min avg adc value (this requires host request) SendOutData(out_buffer); state=0u; } if(state & GET_SCALE_HIGH_LIMIT) { MaxAvgReading = get_adc_average(NUMBER_SAMPLES); clear_buffer(out_buffer,BUF_SIZE); out_buffer[0u]=(uint8)MaxAvgReading; //respond with max avg adc value (this requires host request) SendOutData(out_buffer); state=0u; } if(state & SEND_SCALE_WEIGHT) { do { //this flag tests if the ADC interrupt occurred meaning data is ready adc_flag=Status_Reg_1_Read(); //note the interrupt is disabled so read EOC bit instead if(adc_flag==1u) { ADC_in=ADC_DelSig_1_GetResult8(); //read adc byte adc_flag=0u; clear_buffer(out_buffer,BUF_SIZE); if(auto_mode==FALSE) calculate_fixed_weight(ADC_in, out_buffer); if(auto_mode==TRUE) if( (MaxAvgReading == null) || (MinAvgReading == null) ) // Send Error Message continue; ScaledRange = (MaxAvgReading - MinAvgReading) / MaximumWeight; calculate_scaled_weight(ADC_in, out_buffer, ScaledRange); //send weight data (this requires host request) SendOutData(out_buffer); } }while(USBFS_1_GetEPState(OUT_EP) != USBFS_1_OUT_BUFFER_FULL \ && continous_mode == TRUE); state=0u; } } }