spectrometer 2.0.0.0
spectrometer.h
Go to the documentation of this file.
1/*
2 * MikroSDK - MikroE Software Development Kit
3 * Copyright© 2020 MikroElektronika d.o.o.
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 * OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
33// ----------------------------------------------------------------------------
34
35#ifndef SPECTROMETER_H
36#define SPECTROMETER_H
37
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54#include "drv_i2c_master.h"
55
56
57// -------------------------------------------------------------- PUBLIC MACROS
67#define SPECTROMETER_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
69 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
70 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
77#define SPECTROMETER_RETVAL uint8_t
78
79#define SPECTROMETER_OK 0x00
80#define SPECTROMETER_INIT_ERROR 0xFF
87#define SPECTROMETER_I2C_DEV_ADR 0x39
94#define SPECTROMETER_ASTAT 0x60
95#define SPECTROMETER_CH0_DATA_L 0x61
96#define SPECTROMETER_CH0_DATA_H 0x62
97#define SPECTROMETER_ITIME_L 0x63
98#define SPECTROMETER_ITIME_M 0x64
99#define SPECTROMETER_ITIME_H 0x65
100#define SPECTROMETER_CH1_DATA_L 0x66
101#define SPECTROMETER_CH1_DATA_H 0x67
102#define SPECTROMETER_CH2_DATA_L 0x68
103#define SPECTROMETER_CH2_DATA_H 0x69
104#define SPECTROMETER_CH3_DATA_L 0x6A
105#define SPECTROMETER_CH3_DATA_H 0x6B
106#define SPECTROMETER_CH4_DATA_L 0x6C
107#define SPECTROMETER_CH4_DATA_H 0x6D
108#define SPECTROMETER_CH5_DATA_L 0x6E
109#define SPECTROMETER_CH5_DATA_H 0x6F
110#define SPECTROMETER_CFG 0x70
111#define SPECTROMETER_STAT 0x71
112#define SPECTROMETER_SYNC_EDGE 0x72
113#define SPECTROMETER_GPIO 0x73
114#define SPECTROMETER_LED 0x74
115#define SPECTROMETER_ENABLE 0x80
116#define SPECTROMETER_ATIME 0x81
117#define SPECTROMETER_WTIME 0x83
118#define SPECTROMETER_SP_TH_L_LSB 0x84
119#define SPECTROMETER_SP_TH_L_MSB 0x85
120#define SPECTROMETER_SP_TH_H_LSB 0x86
121#define SPECTROMETER_SP_TH_H_MSB 0x87
122#define SPECTROMETER_AUXID 0x90
123#define SPECTROMETER_REVID 0x91
124#define SPECTROMETER_ID 0x92
125#define SPECTROMETER_STATUS 0x93
126#define SPECTROMETER_ASTATUS 0x94
127#define SPECTROMETER_CH0_ADC_DATA_L 0x95
128#define SPECTROMETER_CH0_ADC_DATA_H 0x96
129#define SPECTROMETER_CH1_ADC_DATA_L 0x97
130#define SPECTROMETER_CH1_ADC_DATA_H 0x98
131#define SPECTROMETER_CH2_ADC_DATA_L 0x99
132#define SPECTROMETER_CH2_ADC_DATA_H 0x9A
133#define SPECTROMETER_CH3_ADC_DATA_L 0x9B
134#define SPECTROMETER_CH3_ADC_DATA_H 0x9C
135#define SPECTROMETER_CH4_ADC_DATA_L 0x9D
136#define SPECTROMETER_CH4_ADC_DATA_H 0x9E
137#define SPECTROMETER_CH5_ADC_DATA_L 0x9F
138#define SPECTROMETER_CH5_ADC_DATA_H 0xA0
139#define SPECTROMETER_STAT_2 0xA3
140#define SPECTROMETER_STAT_3 0xA4
141#define SPECTROMETER_STAT_5 0xA6
142#define SPECTROMETER_STAT_6 0xA7
143#define SPECTROMETER_CFG_0 0xA9
144#define SPECTROMETER_CFG_1 0xAA
145#define SPECTROMETER_CFG_3 0xAC
146#define SPECTROMETER_CFG_6 0xAF
147#define SPECTROMETER_CFG_8 0xB1
148#define SPECTROMETER_CFG_9 0xB2
149#define SPECTROMETER_CFG_10 0xB3
150#define SPECTROMETER_CFG_12 0xB5
151#define SPECTROMETER_PERS 0xBD
152#define SPECTROMETER_GPIO_2 0xBE
153#define SPECTROMETER_ASTEP_L 0xCA
154#define SPECTROMETER_ASTEP_H 0xCB
155#define SPECTROMETER_AGC_GAIN_MAX 0xCF
156#define SPECTROMETER_AZ_CFG 0xD6
157#define SPECTROMETER_FD_TIME_1 0xD8
158#define SPECTROMETER_FD_TIME_2 0xDA
159#define SPECTROMETER_FD_CFG0 0xD7
160#define SPECTROMETER_FD_STAT 0xDB
161#define SPECTROMETER_INTENAB 0xF9
162#define SPECTROMETER_CTL 0xFA
163#define SPECTROMETER_FIFO_MAP 0xFC
164#define SPECTROMETER_FIFO_LVL 0xFD
165#define SPECTROMETER_FDATA_L 0xFE
166#define SPECTROMETER_FDATA_H 0xFF
173#define SPECTROMETER_CFG_LED_SEL 0x08
174#define SPECTROMETER_CFG_INT_SEL 0x04
175#define SPECTROMETER_CFG_INT_MODE_SYND 0x03
176#define SPECTROMETER_CFG_INT_MODE_SYNS 0x01
177#define SPECTROMETER_CFG_INT_MODE_SPM 0x00
184#define SPECTROMETER_STAT_WAIT_SYNC 0x02
185#define SPECTROMETER_STAT_READY 0x01
192#define SPECTROMETER_GPIO_PD_GPIO 0x02
193#define SPECTROMETER_GPIO_PD_INT 0x01
200#define SPECTROMETER_LED_LED_ACT 0x80
201#define SPECTROMETER_LED_LED_DRV_4MA 0x00
202#define SPECTROMETER_LED_LED_DRV_6MA 0x01
203#define SPECTROMETER_LED_LED_DRV_8MA 0x02
204#define SPECTROMETER_LED_LED_DRV_10MA 0x03
205#define SPECTROMETER_LED_LED_DRV_12MA 0x04
206#define SPECTROMETER_LED_LED_DRV_256MA 0x7E
207#define SPECTROMETER_LED_LED_DRV_258MA 0x7F
214#define SPECTROMETER_EN_FDEN 0x40
215#define SPECTROMETER_EN_SMUXEN 0x10
216#define SPECTROMETER_EN_WEN 0x08
217#define SPECTROMETER_EN_SP_EN 0x02
218#define SPECTROMETER_EN_PON 0x01
225#define SPECTROMETER_STATUS_ASAT 0x80
226#define SPECTROMETER_STATUS_AINT 0x08
227#define SPECTROMETER_STATUS_FINT 0x04
228#define SPECTROMETER_STATUS_CINT 0x02
229#define SPECTROMETER_STATUS_SINT 0x01
236#define SPECTROMETER_STAT_2_AVALID 0x40
237#define SPECTROMETER_STAT_2_ASAT_DIG 0x10
238#define SPECTROMETER_STAT_2_ASAT_ANA 0x08
239#define SPECTROMETER_STAT_2_FDSAT_ANA 0x02
240#define SPECTROMETER_STAT_2_FDSAT_DIG 0x01
247#define SPECTROMETER_STAT_3_INT_SP_H 0x20
248#define SPECTROMETER_STAT_3_INT_SP_L 0x10
255#define SPECTROMETER_STAT_5_SINT_FD 0x08
256#define SPECTROMETER_STAT_5_SINT_SMUX 0x04
263#define SPECTROMETER_STAT_6_FIFO_OV 0x80
264#define SPECTROMETER_STAT_6_OVTEMP 0x20
265#define SPECTROMETER_STAT_6_FD_TRIG 0x10
266#define SPECTROMETER_STAT_6_SP_TRIG 0x04
267#define SPECTROMETER_STAT_6_SAI_ACT 0x02
268#define SPECTROMETER_STAT_6_INT_BUSY 0x01
275#define SPECTROMETER_CFG_0_LOW_POW 0x20
276#define SPECTROMETER_CFG_0_REG_BANK_1 0x10
277#define SPECTROMETER_CFG_0_REG_BANK_0 0x00
278#define SPECTROMETER_CFG_0_WLONG 0x04
285#define SPECTROMETER_CFG_1_AGAIN_512 0x0A
286#define SPECTROMETER_CFG_1_AGAIN_256 0x09
287#define SPECTROMETER_CFG_1_AGAIN_128 0x08
288#define SPECTROMETER_CFG_1_AGAIN_64 0x07
289#define SPECTROMETER_CFG_1_AGAIN_32 0x06
290#define SPECTROMETER_CFG_1_AGAIN_16 0x05
291#define SPECTROMETER_CFG_1_AGAIN_8 0x04
292#define SPECTROMETER_CFG_1_AGAIN_4 0x03
293#define SPECTROMETER_CFG_1_AGAIN_2 0x02
294#define SPECTROMETER_CFG_1_AGAIN_1 0x01
295#define SPECTROMETER_CFG_1_AGAIN_05 0x00
302#define SPECTROMETER_CFG_3_SAI 0x10
309#define SPECTROMETER_CFG_6_SMUX_CMD_2 0x10
310#define SPECTROMETER_CFG_6_SMUX_CMD_1 0x08
311#define SPECTROMETER_CFG_6_SMUX_CMD_0 0x00
318#define SPECTROMETER_CFG_8_FIFO_TH_16 0xC0
319#define SPECTROMETER_CFG_8_FIFO_TH_8 0x80
320#define SPECTROMETER_CFG_8_FIFO_TH_4 0x40
321#define SPECTROMETER_CFG_8_FIFO_TH_1 0x00
322#define SPECTROMETER_CFG_8_FD_AGC 0x08
323#define SPECTROMETER_CFG_8_SP_AGC 0x04
330#define SPECTROMETER_CFG_9_SIEN_FD 0x40
331#define SPECTROMETER_CFG_9_SIEN_SMUX 0x10
338#define SPECTROMETER_CFG_10_AGC_H_3 0xC0
339#define SPECTROMETER_CFG_10_AGC_H_2 0x80
340#define SPECTROMETER_CFG_10_AGC_H_1 0x40
341#define SPECTROMETER_CFG_10_AGC_H_0 0x00
342#define SPECTROMETER_CFG_10_AGC_L_3 0x60
343#define SPECTROMETER_CFG_10_AGC_L_2 0x20
344#define SPECTROMETER_CFG_10_AGC_L_1 0x10
345#define SPECTROMETER_CFG_10_AGC_L_0 0x00
346#define SPECTROMETER_CFG_10_FD_PERS_DEF 0x02
353#define SPECTROMETER_CFG_12_SP_TH_CH_4 0x04
354#define SPECTROMETER_CFG_12_SP_TH_CH_3 0x03
355#define SPECTROMETER_CFG_12_SP_TH_CH_2 0x02
356#define SPECTROMETER_CFG_12_SP_TH_CH_1 0x01
357#define SPECTROMETER_CFG_12_SP_TH_CH_0 0x00
364#define SPECTROMETER_PERS_APERS_60 0x0F
365#define SPECTROMETER_PERS_APERS_55 0x0E
366#define SPECTROMETER_PERS_APERS_50 0x0D
367#define SPECTROMETER_PERS_APERS_45 0x0C
368#define SPECTROMETER_PERS_APERS_40 0x0B
369#define SPECTROMETER_PERS_APERS_35 0x0A
370#define SPECTROMETER_PERS_APERS_30 0x09
371#define SPECTROMETER_PERS_APERS_25 0x08
372#define SPECTROMETER_PERS_APERS_20 0x07
373#define SPECTROMETER_PERS_APERS_15 0x06
374#define SPECTROMETER_PERS_APERS_10 0x05
375#define SPECTROMETER_PERS_APERS_5 0x04
376#define SPECTROMETER_PERS_APERS_3 0x03
377#define SPECTROMETER_PERS_APERS_2 0x02
378#define SPECTROMETER_PERS_APERS_1 0x01
379#define SPECTROMETER_PERS_APERS 0x00
386#define SPECTROMETER_GPIO_2_GPIO_INV 0x08
387#define SPECTROMETER_GPIO_2_GPIO_IN 0x04
388#define SPECTROMETER_GPIO_2_GPIO_OUT 0x02
395#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_512 0xA0
396#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_256 0x90
397#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_128 0x80
398#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_64 0x70
399#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_32 0x60
400#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_16 0x50
401#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_8 0x40
402#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_4 0x30
403#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_2 0x20
404#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_1 0x10
405#define SPECTROMETER_AGC_GAIN_MAX_AGC_FD_GAIN_MAX_05 0x00
406#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_512 0x0A
407#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_256 0x09
408#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_128 0x08
409#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_64 0x07
410#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_32 0x06
411#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_16 0x05
412#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_8 0x04
413#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_4 0x03
414#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_2 0x02
415#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_1 0x01
416#define SPECTROMETER_AGC_GAIN_MAX_AGC_AGAIN_MAX_05 0x00
423#define SPECTROMETER_FD_CFG0_FD_FIFO 0x80
430#define SPECTROMETER_FD_STAT_FD_VALID 0x20
431#define SPECTROMETER_FD_STAT_FD_SAT 0x10
432#define SPECTROMETER_FD_STAT_FD_120HZ_VALID 0x08
433#define SPECTROMETER_FD_STAT_FD_100HZ_VALID 0x04
434#define SPECTROMETER_FD_STAT_FD_120HZ 0x02
435#define SPECTROMETER_FD_STAT_FD_100HZ 0x01
442#define SPECTROMETER_INTENAB_ASIEN 0x80
443#define SPECTROMETER_INTENAB_SP_IEN 0x08
444#define SPECTROMETER_INTENAB_F_IEN 0x04
445#define SPECTROMETER_INTENAB_SIEN 0x01
452#define SPECTROMETER_CTL_AZ_SP_MAN 0x04
453#define SPECTROMETER_CTL_FIFO_CLR 0x02
454#define SPECTROMETER_CTL_CLR_SAI_ACT 0x01
461#define SPECTROMETER_FIFO_MAP_WR_DATA_CH6 0x40
462#define SPECTROMETER_FIFO_MAP_WR_DATA_CH5 0x20
463#define SPECTROMETER_FIFO_MAP_WR_DATA_CH3 0x10
464#define SPECTROMETER_FIFO_MAP_WR_DATA_CH2 0x08
465#define SPECTROMETER_FIFO_MAP_WR_DATA_CH1 0x04
466#define SPECTROMETER_FIFO_MAP_WR_DATA_CH0 0x02
467#define SPECTROMETER_FIFO_MAP_WR_ASTAT 0x01
474#define SPECTROMETER_FD_TIME_2_FD_GAIN_05 0x00
475#define SPECTROMETER_FD_TIME_2_FD_GAIN_1 0x08
476#define SPECTROMETER_FD_TIME_2_FD_GAIN_2 0x10
477#define SPECTROMETER_FD_TIME_2_FD_GAIN_4 0x18
478#define SPECTROMETER_FD_TIME_2_FD_GAIN_8 0x20
479#define SPECTROMETER_FD_TIME_2_FD_GAIN_16 0x28
480#define SPECTROMETER_FD_TIME_2_FD_GAIN_32 0x30
481#define SPECTROMETER_FD_TIME_2_FD_GAIN_64 0x38
482#define SPECTROMETER_FD_TIME_2_FD_GAIN_128 0x40
483#define SPECTROMETER_FD_TIME_2_FD_GAIN_256 0x48
484#define SPECTROMETER_FD_TIME_2_FD_GAIN_512 0x50
491#define SPECTROMETER_UNKNOWN_FREQ 0x01
492#define SPECTROMETER_DETECTED_100_HZ 0x02
493#define SPECTROMETER_DETECTED_120_HZ 0x03
494#define SPECTROMETER_READING_ERROR 0x00
501#define SPECTROMETER_ID_VALUE 0x24
504 // End group macro
505// --------------------------------------------------------------- PUBLIC TYPES
514typedef struct
515{
516 // Input pins
517
518 digital_in_t int_pin;
519
520 // Modules
521
522 i2c_master_t i2c;
523
524 // ctx variable
525
527
529
533typedef struct
534{
535 // Communication gpio pins
536
537 pin_name_t scl;
538 pin_name_t sda;
539
540 // Additional gpio pins
541
542 pin_name_t int_pin;
543
544 // static variable
545
546 uint32_t i2c_speed;
547 uint8_t i2c_address;
548
550
551 // End types group
552// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
553
559#ifdef __cplusplus
560extern "C"{
561#endif
562
572
582
593void spectrometer_generic_write ( spectrometer_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
594
605void spectrometer_generic_read ( spectrometer_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
606
617
630void spectrometer_sp_en ( spectrometer_t *ctx, uint8_t state );
631
643
654
666
676
686
696
707void spectrometer_reg_bank_cfg ( spectrometer_t *ctx, uint8_t reg_bank );
708
718
727void spectrometer_raw_rd_val_mode_1 ( spectrometer_t *ctx, uint8_t *adc_data );
728
737void spectrometer_raw_rd_val_mode_2 ( spectrometer_t *ctx, uint8_t *adc_data );
738
749
760
761#ifdef __cplusplus
762}
763#endif
764#endif // _SPECTROMETER_H_
765
766 // End public_function group
768
769// ------------------------------------------------------------------------- END
#define SPECTROMETER_RETVAL
Definition spectrometer.h:77
void spectrometer_f1f4_clr_nir(spectrometer_t *ctx)
SMUX Configuration for F1,F2,F3,F4,NIR,Clear function.
uint8_t spectrometer_flicker_detection(spectrometer_t *ctx)
name function
void spectrometer_smux_en(spectrometer_t *ctx)
Starting the SMUX function.
void spectrometer_pon(spectrometer_t *ctx)
Set the power on bit function.
void spectrometer_raw_rd_val_mode_1(spectrometer_t *ctx, uint8_t *adc_data)
Raw Read Mode 1 function.
void spectrometer_generic_read(spectrometer_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
void spectrometer_reg_bank_cfg(spectrometer_t *ctx, uint8_t reg_bank)
Select Register bank function.
void spectrometer_cfg_setup(spectrometer_cfg_t *cfg)
Config Object Initialization function.
void spectrometer_sp_en(spectrometer_t *ctx, uint8_t state)
Set the spectral measurement enabled bit function.
SPECTROMETER_RETVAL spectrometer_init(spectrometer_t *ctx, spectrometer_cfg_t *cfg)
Initialization function.
uint8_t spectrometer_get_data_ready(spectrometer_t *ctx)
Reading and Polling the the AVALID bit function.
uint8_t spectrometer_get_int(spectrometer_t *ctx)
Get Interrupt state function.
void spectrometer_raw_rd_val_mode_2(spectrometer_t *ctx, uint8_t *adc_data)
Raw Read Mode 2 function.
uint8_t spectrometer_get_smux_en(spectrometer_t *ctx)
Reading and Polling the the SMUX Enable bit function.
void spectrometer_f5f8_clr_nir(spectrometer_t *ctx)
SMUX Configuration for F5,F6,F7,F8,NIR,Clear function.
void spectrometer_def_cfg(spectrometer_t *ctx)
Default configuration function.
void spectrometer_generic_write(spectrometer_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
void spectrometer_fd_cfg(spectrometer_t *ctx)
Flicker Detect Configuration function.
Click configuration structure definition.
Definition spectrometer.h:534
uint32_t i2c_speed
Definition spectrometer.h:546
pin_name_t scl
Definition spectrometer.h:537
pin_name_t int_pin
Definition spectrometer.h:542
pin_name_t sda
Definition spectrometer.h:538
uint8_t i2c_address
Definition spectrometer.h:547
Click ctx object definition.
Definition spectrometer.h:515
digital_in_t int_pin
Definition spectrometer.h:518
i2c_master_t i2c
Definition spectrometer.h:522
uint8_t slave_address
Definition spectrometer.h:526