si114x_uv_index.c

Go to the documentation of this file.
00001 /**************************************************************************/
00016 #include "si114x_functions.h"
00017 
00019 #define UV_TASKLIST           0x80              
00020 #define UV_IRQ_EN             IE_ALS_EVRYSAMPLE 
00021 #define SI1146_DEVICE_ID      0x46              
00024 /**************************************************************************/
00029 static void * si114x_handle;
00030 
00031 /**************************************************************************/
00040 int Si114x_MeasureUVIndex(u16 *uvIndex)
00041 {
00042   u16 data;
00043   volatile u8 regval;
00044   int      retval = 0;
00045   Si114xPsAlsForce(si114x_handle);
00046 
00047 
00048   regval = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA0); /*read sample data from si114x */
00049   data = regval;
00050   regval = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA1);
00051   data |= regval << 8;
00052 
00053   //check for saturation after the forced measurement and clear it if found
00054   //otherwise the next si114x cmd will not be performed.  Also this must be
00055   //done after reading the AUX_DATA register
00056   regval = Si114xReadFromRegister(si114x_handle, REG_RESPONSE);
00057   while((regval & 0x80) != 0)      //response == 0x8x means saturation occured
00058   {   // Send the NOP Command to clear the error...we cannot use Si114xNop()
00059       // because it first checks if REG_RESPONSE < 0 and if so it does not
00060       // perform the cmd. Since we have a saturation REG_RESPONSE will be <0
00061       Si114xWriteToRegister(si114x_handle, REG_COMMAND, 0x00);
00062       regval = Si114xReadFromRegister(si114x_handle, REG_RESPONSE);
00063 
00064   }
00065 
00066 
00067   /*round to nearest*/
00068   *uvIndex  = data + 50;
00069   *uvIndex /= 100;
00070   /*clear irq*/
00071 
00072   if(*uvIndex > 10)
00073       *uvIndex  = data + 50;
00074   Si114xWriteToRegister(si114x_handle, REG_IRQ_STATUS, 0xff);
00075   return retval;
00076 }
00077 
00078 
00079 /**************************************************************************/
00088 int Si114x_EnableVisRange(int vis_range_bit)
00089 {   s16         retval = 0;
00090     if( vis_range_bit == 1 )
00091         retval += Si114xParamSet(si114x_handle, PARAM_ALSVIS_ADC_MISC, RANGE_EN);
00092     else
00093         retval += Si114xParamSet(si114x_handle, PARAM_ALSVIS_ADC_MISC, 0);
00094 
00095     return retval;
00096 }
00097 
00098 
00099 
00100 /**************************************************************************/
00110 int Si114x_ConfigureUV(int fullInit, HANDLE *handle)
00111 {
00112   s16          retval = 0;
00113   SI114X_CAL_S si114x_cal;
00114 
00115   /* Set handle */
00116   si114x_handle = *handle;
00117 
00118   /* Note that the Si114xReset() actually performs the following functions: */
00119   /*     1. Pauses all prior measurements */
00120   /*     2. Clear  i2c registers that need to be cleared */
00121   /*     3. Clears irq status to make sure INT* is negated */
00122   /*     4. Delays 10 ms */
00123   /*     5. Sends HW Key */
00124   if (fullInit)
00125      retval += Si114xReset(si114x_handle);
00126 
00127   /* UV Coefficients */
00128   si114x_get_calibration(si114x_handle, &si114x_cal, 0);
00129   si114x_set_ucoef(si114x_handle, 0, &si114x_cal);
00130 
00131 
00132   if (fullInit)
00133   {
00134      retval += Si114xParamSet(si114x_handle, PARAM_CH_LIST, UV_TASKLIST);
00135      retval += Si114xWriteToRegister(si114x_handle, REG_IRQ_ENABLE, IE_ALS_EVRYSAMPLE);
00136 
00137      retval += Si114xParamSet(si114x_handle, PARAM_PS_ADC_MISC, 0x24);  /* PS_ADC_MISC to high signal range */
00138      retval += Si114xParamSet(si114x_handle, PARAM_PS1_ADC_MUX, 0x00);  /* PS1_ADCMUX, PS2_ADCMUX, PS3_ADCMUX to small photodiode */
00139      retval += Si114xParamSet(si114x_handle, PARAM_PS2_ADC_MUX, 0x00);
00140      retval += Si114xParamSet(si114x_handle, PARAM_PS3_ADC_MUX, 0x00);
00141   }
00142   /* Configure the ALS IR channel for the same settings as PS */
00143   retval += Si114xParamSet(si114x_handle, PARAM_ALSIR_ADC_MISC, RANGE_EN);
00144   retval += Si114xParamSet(si114x_handle, PARAM_ALSVIS_ADC_MISC, RANGE_EN);
00145   /* If nothing went wrong after all of this time, the value */
00146   /* returned will be 0. Otherwise, it will be some negative */
00147   /* number */
00148   return retval;
00149 }