Librairie adaptée au laboratoire 2
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of X_NUCLEO_6180XA1 by
Revision 14:0effa0bbf192, committed 2015-10-19
- Comitter:
- gallonm
- Date:
- Mon Oct 19 13:54:17 2015 +0200
- Parent:
- 13:6736a00a0aa9
- Child:
- 15:454710d17358
- Commit message:
- Fixed the function Prepare() (disabled gpio interrupt by default)
Introduced FIXME for the function AlsSetThreshold()
Fixed the function InitSensor()
Fixed the function StartMeasurement), introducing the operating modes with thresholds
Fixed GetRangeError and GetAlsError
By default if the measure is invalid the value is setted 0xFFFFFFFF
Introduced the function GetMeasurement with the operating mode as parameter
Introduced the function StopMeasurement with the operating mode as parameter
Fixed the function HandleIRQ
Introduced the functions that set low and high threshold
Delete MeasureType (we need only OperatingMode)
Fixed private and public functions
Changed in this revision
--- a/Components/VL6180X/vl6180x_class.cpp Tue Oct 13 14:32:54 2015 +0200 +++ b/Components/VL6180X/vl6180x_class.cpp Mon Oct 19 13:54:17 2015 +0200 @@ -507,7 +507,7 @@ if( status<0) break; /* set range InterruptMode to new sample */ - status=VL6180x_RangeConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY ); + status=VL6180x_RangeConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_DISABLED ); if( status) break; @@ -527,7 +527,7 @@ status = VL6180x_AlsSetThresholds(dev, 0, 0xFF); if( status ) break; /* set Als InterruptMode to new sample */ - status=VL6180x_AlsConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + status=VL6180x_AlsConfigInterrupt(dev, CONFIG_GPIO_INTERRUPT_DISABLED); if( status ) { VL6180x_ErrLog("VL6180x_AlsConfigInterrupt fail"); break; @@ -727,7 +727,7 @@ LOG_FUNCTION_START("%d %d", (int )low, (int)high); - status = VL6180x_WrByte(dev, SYSALS_THRESH_LOW, low); + status = VL6180x_WrByte(dev, SYSALS_THRESH_LOW, low); //FIXME i valori passati devono essere uint16_t (vedi registri), inoltre i valori delle soglie NON sono in lux! bisogna invertire il calcolo fatto nella VL6180x_AlsGetLux() if(!status ){ status = VL6180x_WrByte(dev, SYSALS_THRESH_HIGH, high); } @@ -1323,7 +1323,7 @@ if( !status){ /* must write the scaler at least once to the device to ensure the scaler is in a known state. */ status=VL6180x_UpscaleSetScaling(dev, _GetUpscale(dev)); - VL6180x_WrByte( dev, 0x016, 0x00); /* change fresh out of set status to 0 */ + VL6180x_WrByte( dev, SYSTEM_FRESH_OUT_OF_RESET, 0x00); /* change fresh out of set status to 0 */ } LOG_FUNCTION_END(status); return status; @@ -2669,8 +2669,10 @@ int VL6180X::InitSensor(uint8_t NewAddr) //FIXME printf da sostituire con VL6180x_ErrLog e poi specificare il contenuto di questa funzione { - int status=0; - + int status; + uint16_t buf; + uint8_t buffer; + VL6180x_Off(); VL6180x_On(); status=IsPresent(); @@ -2679,53 +2681,265 @@ Device->Present=1; status=Init(); if(status) + { printf("Failed to init VL6180X sensor!\n"); + return status; + } status=Prepare(); if(status) + { printf("Failed to prepare VL6180X!\n"); - //status=SetI2CAddress(NewAddr); - if(status) - printf("Failed to change I2C address!\n"); - /* disable early convergence estimate - status=VL6180x_RangeSetEceState(Device, 0); - if(status) - printf("Failed to disable ECE!");*/ + return status; + } + if(NewAddr!=DEFAULT_DEVICE_ADDRESS) + { + //status=SetI2CAddress(NewAddr); //if new_addr!=addr_default else errore + if(status) + { + printf("Failed to change I2C address!\n"); + return status; + } + } + else + { + printf("Invalid new address!"); + return INVALID_PARAMS; + } Device->Ready=1; } return status; } -int VL6180X::StartMeasurement(OperatingMode operating_mode, void (*fptr)(void), MeasureData_t *Data) +int VL6180X::StartMeasurement(OperatingMode operating_mode, void (*fptr)(void), MeasureData_t *Data, uint16_t low, uint16_t high) { - int status; + int status, r_status, l_status; switch(operating_mode) { - case(1): - return RangeMeasPollSingleShot(Data); + case(range_single_shot_polling): + r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return RangeMeasPollSingleShot(Data); + else + return (r_status|l_status); - case(2): - return AlsMeasPollSingleShot(Data); + case(als_single_shot_polling): + r_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + l_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return AlsMeasPollSingleShot(Data); + else + return (r_status|l_status); - case(3): - return RangeMeasPollContinuousMode(Data); + case(range_continuous_polling): + r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return RangeMeasPollContinuousMode(); + else + return (r_status|l_status); - case(4): - return AlsMeasPollContinuousMode(Data); + case(als_continuous_polling): + r_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + l_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return AlsMeasPollContinuousMode(); + else + return (r_status|l_status); - case(5): - return RangeMeasIntContinuousMode(fptr); + case(range_continuous_interrupt): + r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return RangeMeasIntContinuousMode(fptr); + else + return (r_status|l_status); - case(6): - return AlsMeasIntContinuousMode(fptr); + case(als_continuous_interrupt): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return AlsMeasIntContinuousMode(fptr); + else + return (r_status|l_status); + + case(interleaved_mode_interrupt): + l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_NEW_SAMPLE_READY); + r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + return InterleavedMode(fptr); + else + return (r_status|l_status); + + case(range_continuous_polling_low_threshold): + r_status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW); + l_status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=RangeSetLowThreshold(low); + if(!status) + return RangeMeasPollContinuousMode(); + else + return status; + } + else + return (r_status|l_status); + + case(range_continuous_polling_high_threshold): + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH); + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=RangeSetHighThreshold(high); + if(!status) + return RangeMeasPollContinuousMode(); + else + return status; + } + else + return (r_status|l_status); + + case(range_continuous_polling_out_of_window): + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW); + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=VL6180x_RangeSetThresholds(Device,low,high,1); + if(!status) + return RangeMeasPollContinuousMode(); + else + return status; + } + else + return (r_status|l_status); + + case(als_continuous_polling_low_threshold): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=AlsSetLowThreshold(low); + if(!status) + return AlsMeasPollContinuousMode(); + else + return status; + } + else + return (r_status|l_status); + + case(als_continuous_polling_high_threshold): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=AlsSetHighThreshold(high); + if(!status) + return AlsMeasPollContinuousMode(); + else + return status; + } + else + return (r_status|l_status); - case(7): break; + case(als_continuous_polling_out_of_window): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=VL6180x_AlsSetThresholds(Device,low,high); + if(!status) + return AlsMeasPollContinuousMode(); + else + return status; + } + else + return (r_status|l_status); + + case(range_continuous_interrupt_low_threshold): + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW); + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=RangeSetLowThreshold(low); + if(!status) + return RangeMeasIntContinuousMode(fptr); + else + return status; + } + else + return (r_status|l_status); + + case(range_continuous_interrrupt_high_threshold): + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH); + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=RangeSetHighThreshold(high); + if(!status) + return RangeMeasIntContinuousMode(fptr); + else + return status; + } + else + return (r_status|l_status); - case(8): break; + case(range_continuous_interrupt_out_of_window): + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW); + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=VL6180x_RangeSetThresholds(Device,low,high,1); + if(!status) + return RangeMeasIntContinuousMode(fptr); + else + return status; + } + else + return (r_status|l_status); + + case(als_continuous_interrupt_low_threshold): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_LOW); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=AlsSetLowThreshold(low); + if(!status) + return AlsMeasIntContinuousMode(fptr); + else + return status; + } + else + return (r_status|l_status); - case(9): - return InterleavedMode(fptr); + case(als_continuous_interrupt_high_threshold): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_LEVEL_HIGH); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=AlsSetHighThreshold(high); + if(!status) + return AlsMeasIntContinuousMode(fptr); + else + return status; + } + else + return (r_status|l_status); + + case(als_continuous_interrupt_out_of_window): + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_OUT_OF_WINDOW); + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if((!r_status)&&(!l_status)) + { + status=VL6180x_AlsSetThresholds(Device,low,high); + if(!status) + return AlsMeasIntContinuousMode(fptr); + else + return status; + } + else + return (r_status|l_status); default: return INVALID_PARAMS; @@ -2735,20 +2949,13 @@ int VL6180X::GetRangeError(MeasureData_t *Data, VL6180x_RangeData_t RangeData) { - IntrStatus_t IntStatus; - int status; - - status=VL6180x_RangeGetInterruptStatus(Device, &IntStatus.val); - if(status) - return status; Data->range_error=RangeData.errorStatus; - Data->int_error=IntStatus.status.Error; - if((Data->int_error!=0)||(Data->range_error!=0)) + if(Data->range_error!=0) { - VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val); - status=RANGE_ERROR; + VL6180x_ErrLog("Range error %d",Data->range_error); + return RANGE_ERROR; } - return status; + return NoError_; } @@ -2756,8 +2963,11 @@ { Data->als_error=AlsData.errorStatus; if(Data->als_error!=0) - return API_ERROR; - return API_NO_ERROR; + { + VL6180x_ErrLog("Light error %d",Data->light_error); + return API_ERROR; + } + return NoError_; } @@ -2772,6 +2982,8 @@ status=GetRangeError(Data, RangeData); if(!status) Data->range_mm=RangeData.range_mm; + else + Data->range_mm=0xFFFFFFFF; } return status; } @@ -2788,12 +3000,14 @@ status=GetAlsError(Data, AlsData); if(!status) Data->lux=AlsData.lux; + else + Data->lux=0xFFFFFFFF; } return status; } -int VL6180X::RangeMeasPollContinuousMode(MeasureData_t *Data) +int VL6180X::RangeMeasPollContinuousMode() { int status; @@ -2813,7 +3027,7 @@ } -int VL6180X::AlsMeasPollContinuousMode(MeasureData_t *Data) +int VL6180X::AlsMeasPollContinuousMode() { int status; @@ -2831,67 +3045,64 @@ } return AlsStartContinuousMode(); } - - -int VL6180X::GetRangeMeasContinuousMode(MeasureData_t *Data) + + +int VL6180X::AlsGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData) { - VL6180x_RangeData_t RangeData; - int status; - - status=VL6180x_RangeGetMeasurement(Device, &RangeData); + int status; + uint8_t IntStatus; + + status=VL6180x_AlsGetInterruptStatus(dev, &IntStatus); if(!status) { - status=GetRangeError(Data, RangeData); - if(!status) - Data->range_mm=RangeData.range_mm; + if(IntStatus==RES_INT_STAT_GPIO_NEW_SAMPLE_READY) + { + status = VL6180x_AlsGetMeasurement(dev, pAlsData); + if(!status) + { + status=VL6180x_AlsClearInterrupt(Device); + if(status) + VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail"); + } + } + else + status=NOT_READY; } - return status; -} - - -int VL6180X::GetAlsMeasContinuousMode(MeasureData_t *Data) -{ - VL6180x_AlsData_t AlsData; - int status; - - status=VL6180x_AlsGetMeasurement(Device, &AlsData); - if(!status) - { - status=GetAlsError(Data, AlsData); - if(!status) - Data->lux=AlsData.lux; - } + else + VL6180x_ErrLog("Fail to get interrupt status"); return status; } int VL6180X::RangeMeasIntContinuousMode(void (*fptr)(void)) { - int status; + int status, ClrStatus; EnableInterruptMeasureDetectionIRQ(); AttachInterruptMeasureDetectionIRQ(fptr); status=SetupGPIO1(GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT, 1); - /*ClrStatus=VL6180x_ClearAllInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");*/ + ClrStatus=VL6180x_ClearAllInterrupt(Device); + if(ClrStatus) + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); if(!status) - return RangeStartContinuousMode(); + status=RangeStartContinuousMode(); + return status; } int VL6180X::AlsMeasIntContinuousMode(void (*fptr)(void)) { - int status; + int status, ClrStatus; EnableInterruptMeasureDetectionIRQ(); AttachInterruptMeasureDetectionIRQ(fptr); status=SetupGPIO1(GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT, 1); - /*ClrStatus=VL6180x_ClearAllInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");*/ + ClrStatus=VL6180x_ClearAllInterrupt(Device); + if(ClrStatus) + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); if(!status) - return AlsStartContinuousMode(); + status=AlsStartContinuousMode(); + return status; } @@ -2900,11 +3111,12 @@ int status; uint16_t integration_period, intermeasurement_period_ms; uint8_t max_convergence_time; + uint8_t buf; status=VL6180x_WrByte(Device, 0x2A3, 1); if(status) { - VL6180x_ErrLog("Failed to read INTERLEAVED_MODE_ENABLE!\n\r"); + VL6180x_ErrLog("Failed to write INTERLEAVED_MODE_ENABLE!\n\r"); return status; } status=VL6180x_RdByte(Device, SYSRANGE_MAX_CONVERGENCE_TIME, &max_convergence_time); @@ -2923,7 +3135,9 @@ integration_period&=0x01FF; intermeasurement_period_ms=((max_convergence_time+5)+(integration_period*1.1)); intermeasurement_period_ms=(intermeasurement_period_ms/0.9)+10; + intermeasurement_period_ms=200; status=VL6180x_AlsSetInterMeasurementPeriod(Device, intermeasurement_period_ms); + VL6180x_RdByte(Device, 0x03E, &buf); if(status) { VL6180x_ErrLog("Failed to write SYSALS_INTERMEASUREMENT_PERIOD!\n\r"); @@ -2935,85 +3149,427 @@ int VL6180X::InterleavedMode(void (*fptr)(void)) { - int status; + int status, ClrStatus; EnableInterruptMeasureDetectionIRQ(); AttachInterruptMeasureDetectionIRQ(fptr); status=SetupGPIO1(GPIOx_SELECT_GPIO_INTERRUPT_OUTPUT, 1); - /*ClrStatus=VL6180x_ClearAllInterrupt(Device); + ClrStatus=VL6180x_ClearAllInterrupt(Device); if(ClrStatus) - VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail");*/ + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); if(!status) - return StartInterleavedMode(); + status=StartInterleavedMode(); + return status; +} + + +int VL6180X::HandleIRQ(OperatingMode operating_mode, MeasureData_t *Data) +{ + int status; + + status=GetMeasurement(operating_mode, Data); + EnableInterruptMeasureDetectionIRQ(); + return status; } - - -void VL6180X::HandleIRQ(MeasureType measure_type, MeasureData_t *Data) + + +int VL6180X::RangeSetLowThreshold(uint16_t threshold) +{ + int status; + uint16_t low, high; + + status=VL6180x_RangeGetThresholds(Device, &low, &high); + if(!status) + status=VL6180x_RangeSetThresholds(Device, threshold, high, 1); + return status; +} + + +int VL6180X::RangeSetHighThreshold(uint16_t threshold) { - int status, ClrStatus; - /* range measure */ - if(measure_type==1) - { - VL6180x_RangeData_t RangeData; - status=RangeGetMeasurement(&RangeData); - if(status) - VL6180x_ErrLog("Failed to get range measurement"); - ClrStatus=VL6180x_RangeClearInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail"); - ClrStatus=VL6180x_ClearErrorInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); - EnableInterruptMeasureDetectionIRQ(); - status=GetRangeError(Data, RangeData); - if(!status) - Data->range_mm=RangeData.range_mm; - } - /* light measure */ - else if(measure_type==2) - { - VL6180x_AlsData_t AlsData; - status=AlsGetMeasurement(&AlsData); - if(status) - VL6180x_ErrLog("Failed to get light measurement"); - ClrStatus=VL6180x_AlsClearInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail"); - ClrStatus=VL6180x_ClearErrorInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); - EnableInterruptMeasureDetectionIRQ(); - status=GetAlsError(Data, AlsData); - if(!status) - Data->lux=AlsData.lux; - } - /* light and range measure */ - else if(measure_type==3) - { - VL6180x_RangeData_t RangeData; - VL6180x_AlsData_t AlsData; - status=RangeGetMeasurement(&RangeData); - if(status) - VL6180x_ErrLog("Failed to get range measurement"); - status=AlsGetMeasurement(&AlsData); - if(status) - VL6180x_ErrLog("Failed to get light measurement"); - ClrStatus=VL6180x_ClearAllInterrupt(Device); - if(ClrStatus) - VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); - EnableInterruptMeasureDetectionIRQ(); - status=GetAlsError(Data, AlsData); - if(!status) - Data->lux=AlsData.lux; - status=GetRangeError(Data, RangeData); - if(!status) - Data->range_mm=RangeData.range_mm; - } - else - printf("Error! Invalid parameter!"); + int status; + uint16_t low, high; + + status=VL6180x_RangeGetThresholds(Device, &low, &high); + if(!status) + status=VL6180x_RangeSetThresholds(Device, low, threshold, 1); + return status; } - - + + +int VL6180X::AlsSetLowThreshold(uint16_t threshold) +{ + int status; + uint16_t low, high; + + status=VL6180x_AlsGetThresholds(Device, &low, &high); + if(!status) + status=VL6180x_AlsSetThresholds(Device, threshold, high); + return status; +} + + +int VL6180X::AlsSetHighThreshold(uint16_t threshold) +{ + int status; + uint16_t low, high; + + status=VL6180x_AlsGetThresholds(Device, &low, &high); + if(!status) + status=VL6180x_AlsSetThresholds(Device, low, threshold); + return status; +} +int VL6180X::VL6180x_AlsGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high) +{ + int status; + + status=VL6180x_RdWord(dev, SYSALS_THRESH_LOW, low); + if(status) + { + VL6180x_ErrLog("rd SYSALS_THRESH_LOW fail"); + return status; + } + status=VL6180x_RdWord(dev, SYSALS_THRESH_HIGH, high); + if(status) + { + VL6180x_ErrLog("rd SYSALS_THRESH_HIGH fail"); + return status; + } + return status; +} + + +int VL6180X::GetMeasurement(OperatingMode operating_mode, MeasureData_t *Data) +{ + switch(operating_mode) + { + case(range_continuous_polling): + case(range_continuous_interrupt): + case(range_continuous_polling_low_threshold): + case(range_continuous_polling_high_threshold): + case(range_continuous_polling_out_of_window): + case(range_continuous_interrupt_low_threshold): + case(range_continuous_interrrupt_high_threshold): + case(range_continuous_interrupt_out_of_window): + return GetRangeMeas(operating_mode, Data); + + case(als_continuous_polling): + case(als_continuous_interrupt): + case(als_continuous_polling_low_threshold): + case(als_continuous_polling_high_threshold): + case(als_continuous_polling_out_of_window): + case(als_continuous_interrupt_low_threshold): + case(als_continuous_interrupt_high_threshold): + case(als_continuous_interrupt_out_of_window): + return GetAlsMeas(operating_mode, Data); + + case(interleaved_mode_interrupt): + return GetRangeAlsMeas(Data); + + default: + return INVALID_PARAMS; + } +} + + +int VL6180X::GetRangeMeas(OperatingMode operating_mode, MeasureData_t *Data) +{ + VL6180x_RangeData_t RangeData; + int status, ClrStatus; + IntrStatus_t IntStatus; + + status=VL6180x_RangeGetInterruptStatus(Device, &IntStatus.val); + if(!status) + { + Data->int_error=IntStatus.status.Error; + if(IntStatus.status.Error!=0) + { + VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val); + return RANGE_ERROR; + } + } + else + { + VL6180x_ErrLog("Fail to read RESULT_INTERRUPT_STATUS_GPIO"); + return status; + } + if((operating_mode==range_continuous_polling)||(operating_mode==range_continuous_interrupt)) + { + if(IntStatus.status.Range==RES_INT_STAT_GPIO_NEW_SAMPLE_READY) + status=VL6180x_RangeGetMeasurement(Device, &RangeData); + else + return NOT_READY; + } + else if((operating_mode==range_continuous_polling_low_threshold)||(operating_mode==range_continuous_interrupt_low_threshold)) + { + if(IntStatus.status.Range==RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD) + status=VL6180x_RangeGetMeasurement(Device, &RangeData); + else + return NOT_READY; + } + else if((operating_mode==range_continuous_polling_high_threshold)||(operating_mode==range_continuous_interrrupt_high_threshold)) + { + if(IntStatus.status.Range==RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD) + status=VL6180x_RangeGetMeasurement(Device, &RangeData); + else + return NOT_READY; + } + else if((operating_mode==range_continuous_polling_out_of_window)||(operating_mode==range_continuous_interrupt_out_of_window)) + { + if(IntStatus.status.Range==RES_INT_STAT_GPIO_OUT_OF_WINDOW) + status=VL6180x_RangeGetMeasurement(Device, &RangeData); + else + return NOT_READY; + } + if(!status) + { + status=GetRangeError(Data, RangeData); + if(!status) + Data->range_mm=RangeData.range_mm; + else + Data->range_mm=0xFFFFFFFF; + } + ClrStatus=VL6180x_RangeClearInterrupt(Device); + if(ClrStatus) + { + VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail"); + return ClrStatus; + } + ClrStatus=VL6180x_ClearErrorInterrupt(Device); + if(ClrStatus) + { + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); + return ClrStatus; + } + return status; +} + + +int VL6180X::GetAlsMeas(OperatingMode operating_mode, MeasureData_t *Data) +{ + VL6180x_AlsData_t AlsData; + int status, ClrStatus; + uint8_t IntStatus; + + status=VL6180x_AlsGetInterruptStatus(Device, &IntStatus); + if(status) + { + VL6180x_ErrLog("Fail to read RESULT_INTERRUPT_STATUS_GPIO"); + return status; + } + if((operating_mode==als_continuous_polling)||(operating_mode==als_continuous_interrupt)) + { + if(IntStatus==RES_INT_STAT_GPIO_NEW_SAMPLE_READY) + status=VL6180x_AlsGetMeasurement(Device, &AlsData); + else + return NOT_READY; + } + else if((operating_mode==als_continuous_polling_low_threshold)||(operating_mode==als_continuous_interrupt_low_threshold)) + { + if(IntStatus==RES_INT_STAT_GPIO_LOW_LEVEL_THRESHOLD) + status=VL6180x_AlsGetMeasurement(Device, &AlsData); + else + return NOT_READY; + } + else if((operating_mode==als_continuous_polling_high_threshold)||(operating_mode==als_continuous_interrupt_high_threshold)) + { + if(IntStatus==RES_INT_STAT_GPIO_HIGH_LEVEL_THRESHOLD) + status=VL6180x_AlsGetMeasurement(Device, &AlsData); + else + return NOT_READY; + } + else if((operating_mode==als_continuous_polling_out_of_window)||(operating_mode==als_continuous_interrupt_out_of_window)) + { + if(IntStatus==RES_INT_STAT_GPIO_OUT_OF_WINDOW) + status=VL6180x_AlsGetMeasurement(Device, &AlsData); + else + return NOT_READY; + } + if(!status) + { + status=GetAlsError(Data, AlsData); + if(!status) + Data->lux=AlsData.lux; + else + Data->lux=0xFFFFFFFF; + } + ClrStatus=VL6180x_AlsClearInterrupt(Device); + if(ClrStatus) + VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail"); + ClrStatus=VL6180x_ClearErrorInterrupt(Device); + if(ClrStatus) + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); + return status; +} + + +int VL6180X::GetRangeAlsMeas(MeasureData_t *Data) +{ + int status, ClrStatus, r_status, l_status; + IntrStatus_t IntStatus; + VL6180x_RangeData_t RangeData; + VL6180x_AlsData_t AlsData; + + status=VL6180x_RdByte(Device, RESULT_INTERRUPT_STATUS_GPIO, &IntStatus.val); + if(!status) + { + Data->int_error=IntStatus.status.Error; + if(IntStatus.status.Error!=0) + { + VL6180x_ErrLog("GPIO int Error report %d",(int)IntStatus.val); + return RANGE_ERROR; + } + } + else + { + VL6180x_ErrLog("Fail to read RESULT_INTERRUPT_STATUS_GPIO"); + return status; + } + + if(IntStatus.status.Als==RES_INT_STAT_GPIO_NEW_SAMPLE_READY) + { + r_status=VL6180x_RangeGetMeasurement(Device, &RangeData); + l_status=VL6180x_AlsGetMeasurement(Device, &AlsData); + if((!r_status)&&(!l_status)) + { + r_status=GetRangeError(Data, RangeData); + l_status=GetAlsError(Data, AlsData); + if(!r_status) + Data->range_mm=RangeData.range_mm; + else + Data->range_mm=0xFFFFFFFF; + if(!l_status) + Data->lux=AlsData.lux; + else + Data->lux=0xFFFFFFFF; + status=r_status|l_status; + } + else + { + status=r_status|l_status; + } + ClrStatus=VL6180x_ClearAllInterrupt(Device); + if(ClrStatus) + VL6180x_ErrLog("VL6180x_ClearAllInterrupt fail"); + } + else + return NOT_READY; + return status; +} + + +int VL6180X::StopMeasurement(OperatingMode operating_mode) +{ + int status; + + switch(operating_mode) + { + case(range_single_shot_polling): + case(range_continuous_polling): + case(range_continuous_interrupt): + case(range_continuous_polling_low_threshold): + case(range_continuous_polling_high_threshold): + case(range_continuous_polling_out_of_window): + case(range_continuous_interrupt_low_threshold): + case(range_continuous_interrrupt_high_threshold): + case(range_continuous_interrupt_out_of_window): + return StopRangeMeasurement(operating_mode); + + case(als_single_shot_polling): + case(als_continuous_polling): + case(als_continuous_interrupt): + case(als_continuous_polling_low_threshold): + case(als_continuous_polling_high_threshold): + case(als_continuous_polling_out_of_window): + case(als_continuous_interrupt_low_threshold): + case(als_continuous_interrupt_high_threshold): + case(als_continuous_interrupt_out_of_window): + return StopAlsMeasurement(operating_mode); + + case(interleaved_mode_interrupt): + status=StopRangeMeasurement(range_continuous_interrupt); + if(!status) + return StopAlsMeasurement(als_continuous_interrupt); + else return status; + + default: + return INVALID_PARAMS; + } +} + + +int VL6180X::StopRangeMeasurement(OperatingMode operating_mode) +{ + int status; + + if(operating_mode==range_single_shot_polling) + status=VL6180x_RangeSetSystemMode(Device, MODE_SINGLESHOT); + else + status=VL6180x_RangeSetSystemMode(Device, MODE_START_STOP|MODE_SINGLESHOT); + if(status) + return status; + status=VL6180x_RangeConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if(status) + { + VL6180x_ErrLog("VL6180x_RangeConfigInterrupt fail"\n\r); + return status; + } + status=VL6180x_RangeClearInterrupt(Device); + if(status) + { + VL6180x_ErrLog("VL6180x_RangeClearInterrupt fail"); + return status; + } + status=VL6180x_ClearErrorInterrupt(Device); + if(status) + { + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); + return status; + } + status=VL6180x_RangeSetRawThresholds(Device, 10, 200); + if(status) + VL6180x_ErrLog("VL6180x_RangeSetThresholds fail"); + return status; +} + + +int VL6180X::StopAlsMeasurement(OperatingMode operating_mode) +{ + int status; + + if(operating_mode==als_single_shot_polling) + status=VL6180x_AlsSetSystemMode(Device, MODE_SINGLESHOT); + else + status=VL6180x_AlsSetSystemMode(Device, MODE_START_STOP|MODE_SINGLESHOT); + if(status) + return status; + status=VL6180x_AlsConfigInterrupt(Device, CONFIG_GPIO_INTERRUPT_DISABLED); + if(status) + { + VL6180x_ErrLog("VL6180x_AlsConfigInterrupt fail"\n\r); + return status; + } + status=VL6180x_AlsClearInterrupt(Device); + if(status) + { + VL6180x_ErrLog("VL6180x_AlsClearInterrupt fail"); + return status; + } + status=VL6180x_ClearErrorInterrupt(Device); + if(status) + { + VL6180x_ErrLog("VL6180x_ClearErrorInterrupt fail"); + return status; + } + status=VL6180x_AlsSetThresholds(Device, 0x0, 0xFF); + if(status) + VL6180x_ErrLog("VL6180x_AlsSetThresholds fail"); + return status; +} + + + + /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
--- a/Components/VL6180X/vl6180x_class.h Tue Oct 13 14:32:54 2015 +0200 +++ b/Components/VL6180X/vl6180x_class.h Mon Oct 19 13:54:17 2015 +0200 @@ -50,7 +50,8 @@ //#include "vl6180x_appcfg.h" #include "STMPE1600_class.h" -/* data struct containing range measure, light measure and type of error provided to the user */ +/* data struct containing range measure, light measure and type of error provided to the user + in case of invalid data range_mm=0xFFFFFFFF and lux=0xFFFFFFFF */ typedef struct MeasureData { int32_t range_mm; @@ -69,20 +70,24 @@ als_continuous_polling, range_continuous_interrupt, als_continuous_interrupt, + interleaved_mode_interrupt, + range_continuous_polling_low_threshold, + range_continuous_polling_high_threshold, + range_continuous_polling_out_of_window, + als_continuous_polling_low_threshold, + als_continuous_polling_high_threshold, + als_continuous_polling_out_of_window, + range_continuous_interrupt_low_threshold, + range_continuous_interrrupt_high_threshold, + range_continuous_interrupt_out_of_window, + als_continuous_interrupt_low_threshold, + als_continuous_interrupt_high_threshold, + als_continuous_interrupt_out_of_window, + //interleaved con soglie range_continuous_als_single_shot, range_single_shot_als_continuous, - interleaved_mode, }OperatingMode; -/* type of sensor measurement */ -typedef enum -{ - range_measure=1, - light_measure, - light_range_measure, -}MeasureType; - - /** default device address */ #define DEFAULT_DEVICE_ADDRESS 0x29 @@ -119,59 +124,25 @@ /** Destructor */ //~VL6180X(){} - - void VL6180x_On(void) { + + /* turns on the sensor */ + void VL6180x_On(void) + { if (gpio0) *gpio0=1; else if (expgpio0) *expgpio0=1; } - void VL6180x_Off(void) { + /* turns off the sensor */ + void VL6180x_Off(void) + { if (gpio0) *gpio0=0; else if (expgpio0) *expgpio0=0; } - - int IsPresent() - { - int status; - - status=ReadID(); - if(status) - printf("Failed to read ID device! Device not present!\n"); //FIXME da sistemare la funzione di stampa errore ErrLog da platform.h - return status; - } - int InitSensor(uint8_t NewAddr); - - int StartMeasurement(OperatingMode operating_mode, void (*fptr)(void), MeasureData_t *Data); - - int GetRangeError(MeasureData_t *Data, VL6180x_RangeData_t RangeData); - - int GetAlsError(MeasureData_t *Data, VL6180x_AlsData_t AlsData); - - int RangeMeasPollSingleShot(MeasureData_t *Data); - - int AlsMeasPollSingleShot(MeasureData_t *Data); - - int AlsStartContinuousMode() - { - return VL6180x_AlsSetSystemMode(Device, MODE_START_STOP|MODE_CONTINUOUS); - } - - int RangeMeasPollContinuousMode(MeasureData_t *Data); - - int AlsMeasPollContinuousMode(MeasureData_t *Data); - - int GetRangeMeasContinuousMode(MeasureData_t *Data); - - int GetAlsMeasContinuousMode(MeasureData_t *Data); - - int RangeMeasIntContinuousMode(void (*fptr)(void)); - - int AlsMeasIntContinuousMode(void (*fptr)(void)); - - int InterleavedMode(void (*fptr)(void)); - - int StartInterleavedMode(); + int InitSensor(uint8_t NewAddr); + int StartMeasurement(OperatingMode operating_mode, void (*fptr)(void), MeasureData_t *Data, uint16_t low, uint16_t high); + int GetMeasurement(OperatingMode operating_mode, MeasureData_t *Data); + int StopMeasurement(OperatingMode operating_mode); /* handling functions of the interrupt_measure */ @@ -198,7 +169,7 @@ interrupt_measure.disable_irq(); } - void HandleIRQ(MeasureType measure_type, MeasureData_t *Data); // FIXME sistemare la sequenza delle funzioni completandola con i print degli errori + int HandleIRQ(OperatingMode operating_mode, MeasureData_t *Data); /* Wrapper functions */ int WaitDeviceBooted() @@ -461,7 +432,12 @@ return VL6180x_AlsGetLux(Device, piData); } + int AlsStartContinuousMode() + { + return VL6180x_AlsSetSystemMode(Device, MODE_START_STOP|MODE_CONTINUOUS); + } +private: /* api.h functions */ int VL6180x_WaitDeviceBooted(VL6180xDev_t dev); int VL6180x_InitData(VL6180xDev_t dev ); @@ -541,10 +517,40 @@ int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData); int VL6180x_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToWrite); int VL6180x_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead); + + + int IsPresent() + { + int status; + + status=ReadID(); + if(status) + printf("Failed to read ID device! Device not present!\n"); //FIXME da sistemare la funzione di stampa errore ErrLog da platform.h + return status; + } + int StopRangeMeasurement(OperatingMode operating_mode); + int StopAlsMeasurement(OperatingMode operating_mode); + int GetRangeMeas(OperatingMode operating_mode, MeasureData_t *Data); + int GetAlsMeas(OperatingMode operating_mode, MeasureData_t *Data); + int GetRangeAlsMeas(MeasureData_t *Data); + int RangeSetLowThreshold(uint16_t threshold); + int RangeSetHighThreshold(uint16_t threshold); + int AlsSetLowThreshold(uint16_t threshold); + int AlsSetHighThreshold(uint16_t threshold); + int GetRangeError(MeasureData_t *Data, VL6180x_RangeData_t RangeData); + int GetAlsError(MeasureData_t *Data, VL6180x_AlsData_t AlsData); + int RangeMeasPollSingleShot(MeasureData_t *Data); + int AlsMeasPollSingleShot(MeasureData_t *Data); + int RangeMeasPollContinuousMode(); + int AlsMeasPollContinuousMode(); + int AlsGetMeasurementIfReady(VL6180xDev_t dev, VL6180x_AlsData_t *pAlsData); + int RangeMeasIntContinuousMode(void (*fptr)(void)); + int AlsMeasIntContinuousMode(void (*fptr)(void)); + int InterleavedMode(void (*fptr)(void)); + int StartInterleavedMode(); + int VL6180x_AlsGetThresholds(VL6180xDev_t dev, uint16_t *low, uint16_t *high); - -private: /* IO Device */ DevI2C &dev_i2c; /* Digital out pin */
--- a/x_nucleo_6180xa1.cpp Tue Oct 13 14:32:54 2015 +0200 +++ b/x_nucleo_6180xa1.cpp Mon Oct 19 13:54:17 2015 +0200 @@ -56,7 +56,8 @@ int X_NUCLEO_6180XA1::InitBoard() { int status; - + //flag per verificare sensori presenti + //sensor_top->VL6180x_Off() x4 status=sensor_top->InitSensor(NEW_SENSOR_TOP_ADDRESS); if(status) {
--- a/x_nucleo_6180xa1.h Tue Oct 13 14:32:54 2015 +0200 +++ b/x_nucleo_6180xa1.h Mon Oct 19 13:54:17 2015 +0200 @@ -61,7 +61,7 @@ X_NUCLEO_6180XA1(DevI2C *ext_i2c) : dev_i2c(ext_i2c) { gpio0_top=new STMPE1600DigiOut(*dev_i2c, GPIO_12); - sensor_top=new VL6180X(*dev_i2c, *gpio0_top, PB_0); + sensor_top=new VL6180X(*dev_i2c, *gpio0_top, PB_0); } ~X_NUCLEO_6180XA1()