/******************************************************************************* * Function Name: USBFS_1_GetChar ******************************************************************************** * * Summary: * Reads one byte of received data from the buffer. * * Parameters: * None. * * Return: * Received one character. * * Global variables: * USBFS_1_cdc_data_out_ep: CDC OUT endpoint number used. * * Reentrant: * No. * *******************************************************************************/ uint8 USBFS_1_GetChar(void) { uint8 rxData; USBFS_1_ReadOutEP(USBFS_1_cdc_data_out_ep, &rxData, 1u); return(rxData); }
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; } } }
/******************************************************************************* * Function Name: USBFS_1_GetAll ******************************************************************************** * * Summary: * Gets all bytes of received data from the input buffer and places it into a * specified data array. USBFS_1_DataIsReady() API should be called * before, to be sure that data is received from the Host. * * Parameters: * pData: Pointer to the data array where data will be placed. * * Return: * Number of bytes received. * * Global variables: * USBFS_1_cdc_data_out_ep: CDC OUT endpoint number used. * USBFS_1_EP[].bufferSize: EP max packet size is used as a length * to read all data from the EP buffer. * * Reentrant: * No. * *******************************************************************************/ uint16 USBFS_1_GetAll(uint8* pData) { return (USBFS_1_ReadOutEP(USBFS_1_cdc_data_out_ep, pData, USBFS_1_EP[USBFS_1_cdc_data_out_ep].bufferSize)); }
/******************************************************************************* * Function Name: USBFS_1_GetData ******************************************************************************** * * Summary: * Gets a specified number of bytes from the input buffer and places it in a * data array specified by the passed pointer. * USBFS_1_DataIsReady() API should be called before, to be sure * that data is received from the Host. * * Parameters: * pData: Pointer to the data array where data will be placed. * Length: Number of bytes to read into the data array from the RX buffer. * Maximum length is limited by the the number of received bytes. * * Return: * Number of bytes received. * * Global variables: * USBFS_1_cdc_data_out_ep: CDC OUT endpoint number used. * * Reentrant: * No. * *******************************************************************************/ uint16 USBFS_1_GetData(uint8* pData, uint16 length) { return(USBFS_1_ReadOutEP(USBFS_1_cdc_data_out_ep, pData, length)); }