15#include "stm32l4xx_hal.h"
18#define X_ORDER_MASK 0x0070
19#define Y_ORDER_MASK 0x0007
20#define SIGN_MASK 0x0080
21#define GET_X_ORDER(m) ( ((m) & X_ORDER_MASK) >> 4)
22#define GET_Y_ORDER(m) ( ((m) & Y_ORDER_MASK) )
23#define GET_SIGN(m) ( ((m) & SIGN_MASK) >> 7)
25#define UV_INPUT_FRACTION 15
26#define UV_OUTPUT_FRACTION 12
29#define ADC_THRESHOLD 16000
30#define INPUT_FRACTION_HIGH 7
31#define INPUT_FRACTION_LOW 15
32#define LUX_OUTPUT_FRACTION 12
34#define NUMCOEFF_HIGH 4
37#define UV_I2C_SEND(ADDRESS,LEN,TIME) MZ_I2C_Master_Transmit(MZ_ADDONUV_I2C_INSTANCE,SLAVE_ADD,(uint8_t *) ADDRESS,LEN,TIME)
38#define UV_I2C_GET(ADDRESS,LEN,TIME) MZ_I2C_Master_Receive(MZ_ADDONUV_I2C_INSTANCE,SLAVE_ADD,(uint8_t *) ADDRESS,LEN,TIME)
39#define SI1133_CONFIRM_COUNT (5)
76 _Buff[0] = (uint8_t)w_addr;
95 _Buff[0] = (uint8_t)w_addr;
96 memcpy(&_Buff[1], data, len);
108 uint8_t _Buf = (uint8_t)r_addr;
125 uint8_t _Buf = (uint8_t)r_addr;
171 if (
SI_OK != _ret) {
return _ret; }
187 uint8_t response_initial;
193 if (
SI_OK != _ret) {
return _ret; }
202 if (
SI_OK != _ret) {
return _ret; }
210 if (
SI_OK != _ret) {
return _ret; }
227 if (
SI_OK != _ret) {
return _ret; }
238 if (
SI_OK != _ret) {
return _ret; }
279 uint8_t param_Buff[2];
280 uint8_t response_initial;
285 if (
SI_OK != _ret) {
return _ret; }
289 if (
SI_OK != _ret) {
return _ret; }
293 param_Buff[0] = value;
294 param_Buff[1] = 0x80 + ((uint8_t)address & 0x3F);
297 if (
SI_OK != _ret) {
return _ret; }
305 if (
SI_OK != _ret) {
return _ret; }
330 value = ( (input << fraction) / mag) >> -shift;
332 value = ( (input << fraction) / mag) << shift;
340 uint8_t input_fraction,
341 uint8_t output_fraction,
345 uint8_t info, x_order, y_order, counter;
348 int32_t output = 0, x1, x2, y1, y2;
350 for ( counter = 0; counter < num_coeff; counter++ ) {
355 shift = ( (uint16_t) kp->
info & 0xff00) >> 8;
368 if ( (x_order == 0) && (y_order == 0) ) {
369 output += sign * mag << output_fraction;
395 output += sign * x1 * x2 * y1 * y2;
494 samples->irq_status = buffer[0];
496 samples->ch0 = buffer[1] << 16;
497 samples->ch0 |= (buffer[2] << 8);
498 samples->ch0 |= buffer[3];
499 if ( samples->ch0 & 0x800000 ) {
500 samples->ch0 |= 0xFF000000;
503 samples->ch1 = buffer[4] << 16;
504 samples->ch1 |= buffer[5] << 8;
505 samples->ch1 |= buffer[6];
506 if ( samples->ch1 & 0x800000 ) {
507 samples->ch1 |= 0xFF000000;
510 samples->ch2 = buffer[7] << 16;
511 samples->ch2 |= buffer[8] << 8;
512 samples->ch2 |= buffer[9];
513 if ( samples->ch2 & 0x800000 ) {
514 samples->ch2 |= 0xFF000000;
517 samples->ch3 = buffer[10] << 16;
518 samples->ch3 |= buffer[11] << 8;
519 samples->ch3 |= buffer[12];
520 if ( samples->ch3 & 0x800000 ) {
521 samples->ch3 |= 0xFF000000;
548 while ( response != 0x0F ) {
554 memset(&_samples,0,
sizeof(_samples));
558 *lux = (float)
SI1133_get_lux(_samples.ch1, _samples.ch3, _samples.ch2);
578 *lux = (float)
SI1133_get_lux(samples.ch1, samples.ch3, samples.ch2);
static Si1133_error SI1133_pause_measurement(void)
#define UV_INPUT_FRACTION
Si1133_error SI1133_deinit(void)
#define SI1133_CONFIRM_COUNT
#define INPUT_FRACTION_HIGH
static Si1133_error SI1133_measure(Si1133_samples *_s1)
static Si1133_error SI1133_Write_Reg(Si1133_register w_addr, uint8_t data)
static int32_t SI1133_calculate_polynomial_helper(int32_t input, int8_t fraction, uint16_t mag, int8_t shift)
Si1133_error SI1133_measure_lux_uv(float *lux, float *uvi)
#define UV_I2C_SEND(ADDRESS, LEN, TIME)
float SI1133_get_uv_index(void)
const Si1133_coeff Si1133_uk[2]
static Si1133_error SI1133_Write_Reg_block(Si1133_register w_addr, uint8_t len, uint8_t *data)
#define UV_OUTPUT_FRACTION
Si1133_error SI1133_get_measurement(float *lux, float *uvi)
#define LUX_OUTPUT_FRACTION
const Si1133_LuxCoeff Si1133_lk
Si1133_error SI1133_init(void)
#define UV_I2C_GET(ADDRESS, LEN, TIME)
static Si1133_error SI1133_send_cmd(Si1133_command command)
static Si1133_error SI1133_wait_until_sleep(void)
static Si1133_error SI1133_force_measurement(void)
static Si1133_error SI1133_set_parameter(Si1133_parameter address, uint8_t value)
static int32_t SI1133_calculate_polynomial(int32_t x, int32_t y, uint8_t input_fraction, uint8_t output_fraction, uint8_t num_coeff, const Si1133_coeff *kp)
#define INPUT_FRACTION_LOW
static int32_t SI1133_get_lux(int32_t vis_high, int32_t vis_low, int32_t ir)
static int32_t SI1133_get_uv(int32_t uv)
Si1133_error SI1133_get_hardware_id(uint8_t *hardware_id)
static Si1133_error SI1133_Read_Reg(Si1133_register r_addr, uint8_t *buf)
static Si1133_error SI1133_reset(void)
float SI1133_get_light_level(void)
static Si1133_error SI1133_Read_Reg_block(Si1133_register r_addr, uint8_t len, uint8_t *buf)
@ SI1133_REG_IRQ_STATUS
Interrupt status.
@ SI1133_REG_COMMAND
Initiated action in Sensor when specific codes written here.
@ SI1133_REG_IRQ_ENABLE
Interrupt enable
@ SI1133_REG_PART_ID
Part ID.
@ SI1133_REG_HOSTIN0
Host 0 -Data for parameter table on PARAM_SET write to COMMAND register.
@ SI1133_REG_RESPONSE0
Chip state and error status.
@ SI1133_RSP0_CHIPSTAT_MASK
Chip state mask in Response0 register.
@ SI1133_RSP0_SLEEP
Sleep state indicator bit mask in Response0 register.
@ SI1133_RSP0_COUNTER_MASK
Command counter and error indicator mask in Response0 register.
@ SI1133_CMD_RESET
Forces a Reset.
@ SI1133_CMD_PAUSE_CH
Pauses autonomous measurements.
@ SI1133_CMD_FORCE_CH
Initiates a set of measurements specified in CHAN_LIST parameter.
@ SI1133_CMD_RESET_CMD_CTR
Resets the command counter.
@ SI1133_CMD_START
Starts autonomous measurements.
@ SI1133_PARAM_ADCPOST2
ADC resolution, shift and threshold settings for Channel 2.
@ SI1133_PARAM_ADCSENS1
ADC sensitivity setting for Channel 1.
@ SI1133_PARAM_ADCSENS3
ADC sensitivity setting for Channel 3.
@ SI1133_PARAM_ADCCONFIG3
ADC config for Channel 3
@ SI1133_PARAM_ADCCONFIG1
ADC config for Channel 1.
@ SI1133_PARAM_CH_LIST
Channel list.
@ SI1133_PARAM_ADCCONFIG2
ADC config for Channel 2.
@ SI1133_PARAM_ADCSENS0
ADC sensitivity setting for Channel 0.
@ SI1133_PARAM_ADCSENS2
ADC sensitivity setting for Channel 2.
@ SI1133_PARAM_ADCCONFIG0
ADC config for Channel 0.
@ SI1133_PARAM_ADCPOST0
ADC resolution, shift and threshold settings for Channel 0.
@ SI1133_PARAM_ADCPOST3
ADC resolution, shift and threshold settings for Channel 3.
@ SI1133_PARAM_ADCPOST1
ADC resolution, shift and threshold settings for Channel 1.
Si1133_coeff coeff_low[9]
Low amplitude coeffs
Si1133_coeff coeff_high[4]
High amplitude coeffs.