EFM32 Gecko Software Documentation  efm32g-doc-5.1.2
si114x_uv_index.c
Go to the documentation of this file.
1 /**************************************************************************/
16 #include "si114x_functions.h"
17 
19 #define UV_TASKLIST 0x80
20 #define UV_IRQ_EN IE_ALS_EVRYSAMPLE
21 #define SI1146_DEVICE_ID 0x46
24 /**************************************************************************/
29 static void * si114x_handle;
30 
31 /**************************************************************************/
41 {
42  u16 data;
43  volatile u8 regval;
44  int retval = 0;
46 
47 
48  regval = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA0); /*read sample data from si114x */
49  data = regval;
50  regval = Si114xReadFromRegister(si114x_handle, REG_AUX_DATA1);
51  data |= regval << 8;
52 
53  //check for saturation after the forced measurement and clear it if found
54  //otherwise the next si114x cmd will not be performed. Also this must be
55  //done after reading the AUX_DATA register
56  regval = Si114xReadFromRegister(si114x_handle, REG_RESPONSE);
57  while((regval & 0x80) != 0) //response == 0x8x means saturation occured
58  { // Send the NOP Command to clear the error...we cannot use Si114xNop()
59  // because it first checks if REG_RESPONSE < 0 and if so it does not
60  // perform the cmd. Since we have a saturation REG_RESPONSE will be <0
61  Si114xWriteToRegister(si114x_handle, REG_COMMAND, 0x00);
62  regval = Si114xReadFromRegister(si114x_handle, REG_RESPONSE);
63 
64  }
65 
66 
67  /*round to nearest*/
68  *uvIndex = data + 50;
69  *uvIndex /= 100;
70  /*clear irq*/
71 
72  if(*uvIndex > 10)
73  *uvIndex = data + 50;
74  Si114xWriteToRegister(si114x_handle, REG_IRQ_STATUS, 0xff);
75  return retval;
76 }
77 
78 
79 /**************************************************************************/
88 int Si114x_EnableVisRange(int vis_range_bit)
89 { s16 retval = 0;
90  if( vis_range_bit == 1 )
91  retval += Si114xParamSet(si114x_handle, PARAM_ALSVIS_ADC_MISC, RANGE_EN);
92  else
93  retval += Si114xParamSet(si114x_handle, PARAM_ALSVIS_ADC_MISC, 0);
94 
95  return retval;
96 }
97 
98 
99 
100 /**************************************************************************/
110 int Si114x_ConfigureUV(int fullInit, HANDLE *handle)
111 {
112  s16 retval = 0;
113  SI114X_CAL_S si114x_cal;
114 
115  /* Set handle */
116  si114x_handle = *handle;
117 
118  /* Note that the Si114xReset() actually performs the following functions: */
119  /* 1. Pauses all prior measurements */
120  /* 2. Clear i2c registers that need to be cleared */
121  /* 3. Clears irq status to make sure INT* is negated */
122  /* 4. Delays 10 ms */
123  /* 5. Sends HW Key */
124  if (fullInit)
125  retval += Si114xReset(si114x_handle);
126 
127  /* UV Coefficients */
128  si114x_get_calibration(si114x_handle, &si114x_cal, 0);
129  si114x_set_ucoef(si114x_handle, 0, &si114x_cal);
130 
131 
132  if (fullInit)
133  {
134  retval += Si114xParamSet(si114x_handle, PARAM_CH_LIST, UV_TASKLIST);
135  retval += Si114xWriteToRegister(si114x_handle, REG_IRQ_ENABLE, IE_ALS_EVRYSAMPLE);
136 
137  retval += Si114xParamSet(si114x_handle, PARAM_PS_ADC_MISC, 0x24); /* PS_ADC_MISC to high signal range */
138  retval += Si114xParamSet(si114x_handle, PARAM_PS1_ADC_MUX, 0x00); /* PS1_ADCMUX, PS2_ADCMUX, PS3_ADCMUX to small photodiode */
139  retval += Si114xParamSet(si114x_handle, PARAM_PS2_ADC_MUX, 0x00);
140  retval += Si114xParamSet(si114x_handle, PARAM_PS3_ADC_MUX, 0x00);
141  }
142  /* Configure the ALS IR channel for the same settings as PS */
143  retval += Si114xParamSet(si114x_handle, PARAM_ALSIR_ADC_MISC, RANGE_EN);
144  retval += Si114xParamSet(si114x_handle, PARAM_ALSVIS_ADC_MISC, RANGE_EN);
145  /* If nothing went wrong after all of this time, the value */
146  /* returned will be 0. Otherwise, it will be some negative */
147  /* number */
148  return retval;
149 }
SI114X_CAL_S Data Structure.
#define UV_TASKLIST
Si114x function prototypes, structure and bit definitions.
unsigned short int u16
Definition: bmp280.h:283
int16_t Si114xParamSet(HANDLE si114x_handle, uint8_t address, uint8_t value)
Writes a byte to an Si113x/4x Parameter.
int16_t Si114xWriteToRegister(HANDLE si114x_handle, uint8_t address, uint8_t data)
Writes to Si114x Register.
Definition: si1147_i2c.c:205
int16_t si114x_set_ucoef(HANDLE si114x_handle, uint8_t *input_ucoef, SI114X_CAL_S *si114x_cal)
Initializes the Si113x/46/47/48 UCOEF Registers.
int Si114x_ConfigureUV(int fullInit, HANDLE *handle)
Initializes and configures the Si1146 sensor for UV index measurements.
signed short int s16
Definition: bmp280.h:277
int16_t Si114xPsAlsForce(HANDLE si114x_handle)
Sends a PSALSFORCE command to the Si113x/4x.
unsigned char u8
Definition: bmp280.h:282
int Si114x_EnableVisRange(int vis_range_bit)
Set the VIS_RANGE bit of the optical sensor Si114x.
int16_t Si114xReadFromRegister(HANDLE si114x_handle, uint8_t address)
Reads from Si114x register.
Definition: si1147_i2c.c:219
int16_t si114x_get_calibration(HANDLE si114x_handle, SI114X_CAL_S *si114x_cal, uint8_t security)
Populates the SI114X_CAL_S structure.
static void * si114x_handle
Si114x PGM toolkit functions uses a void* to pass hardware parameters through to the lower level i2c ...
int Si114x_MeasureUVIndex(u16 *uvIndex)
Reads the UV index measurement data from the Si114x.
int16_t Si114xReset(HANDLE si114x_handle)
Resets the Si113x/4x, clears any interrupts and initializes the HW_KEY register.