thunder  2.0.0.0
thunder.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 THUNDER_H
36 #define THUNDER_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 #include "spi_specifics.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define THUNDER_MAP_MIKROBUS( cfg, mikrobus ) \
55  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
56  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
57  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
58  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
59  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
60 
70 #define THUNDER_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
71 #define THUNDER_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
72 
77 #define THUNDER_OK 0x00
78 #define THUNDER_ERROR 0xFF
79 
85 #define THUNDER_DEV_ADDR_0 0x00
86 #define THUNDER_DEV_ADDR_1 0x01
87 #define THUNDER_DEV_ADDR_2 0x02
88 #define THUNDER_DEV_ADDR_3 0x03
89 
95 #define THUNDER_REG_CONFIG_0 0x00
96 #define THUNDER_REG_CONFIG_1 0x01
97 #define THUNDER_REG_CONFIG_2 0x02
98 #define THUNDER_REG_CONFIG_3_INTERR 0x03
99 #define THUNDER_REG_ENERGY_LIGHTNING_LSBYTE 0x04
100 #define THUNDER_REG_ENERGY_LIGHTNING_MSBYTE 0x05
101 #define THUNDER_REG_ENERGY_LIGHTNING_MMSBYTE 0x06
102 #define THUNDER_REG_DISTANCE 0x07
103 #define THUNDER_REG_INTERR_PIN_CONFIG 0x08
104 #define THUNDER_REG_LDLUT_1 0x09
105 #define THUNDER_REG_LDLUT_2 0x0A
106 #define THUNDER_REG_LDLUT_3 0x0B
107 #define THUNDER_REG_LDLUT_4 0x0C
108 #define THUNDER_REG_LDLUT_5 0x0D
109 #define THUNDER_REG_LDLUT_6 0x0E
110 #define THUNDER_REG_LDLUT_7 0x0F
111 #define THUNDER_REG_LDLUT_8 0x10
112 #define THUNDER_REG_LDLUT_9 0x11
113 #define THUNDER_REG_LDLUT_10 0x12
114 #define THUNDER_REG_LDLUT_11 0x13
115 #define THUNDER_REG_LDLUT_12 0x14
116 #define THUNDER_REG_LDLUT_13 0x15
117 #define THUNDER_REG_LDLUT_14 0x16
118 #define THUNDER_REG_LDLUT_15 0x17
119 #define THUNDER_REG_LDLUT_16 0x18
120 #define THUNDER_REG_LDLUT_17 0x19
121 #define THUNDER_REG_LDLUT_18 0x1A
122 #define THUNDER_REG_LDLUT_19 0x1B
123 #define THUNDER_REG_LDLUT_20 0x1C
124 #define THUNDER_REG_LDLUT_21 0x1D
125 #define THUNDER_REG_LDLUT_22 0x1E
126 #define THUNDER_REG_LDLUT_23 0x1F
127 #define THUNDER_REG_LDLUT_24 0x20
128 #define THUNDER_REG_LDLUT_25 0x21
129 #define THUNDER_REG_LDLUT_26 0x22
130 #define THUNDER_REG_LDLUT_27 0x23
131 #define THUNDER_REG_LDLUT_28 0x24
132 #define THUNDER_REG_LDLUT_29 0x25
133 #define THUNDER_REG_LDLUT_30 0x26
134 #define THUNDER_REG_LDLUT_31 0x27
135 #define THUNDER_REG_LDLUT_32 0x28
136 #define THUNDER_REG_LDLUT_33 0x29
137 #define THUNDER_REG_LDLUT_34 0x2A
138 #define THUNDER_REG_LDLUT_35 0x2B
139 #define THUNDER_REG_LDLUT_36 0x2C
140 #define THUNDER_REG_LDLUT_37 0x2D
141 #define THUNDER_REG_LDLUT_38 0x2E
142 #define THUNDER_REG_LDLUT_39 0x2F
143 #define THUNDER_REG_LDLUT_40 0x30
144 #define THUNDER_REG_LDLUT_41 0x31
145 #define THUNDER_REG_LDLUT_42 0x32
146 #define THUNDER_REG_RESET 0x3C
147 #define THUNDER_REG_CALIB 0x3D
148 
154 #define THUNDER_INDOOR 0x24
155 #define THUNDER_OUTDOOR 0x1C
156 #define THUNDER_NORMAL_MODE 0x00
157 #define THUNDER_POWER_DOWN 0x01
158 
164 #define THUNDER_DEFAULT_NOISE_FLOOR_LVL 0x20
165 #define THUNDER_DEFAULT_WDG_THRESHOLD 0x02
166 
172 #define THUNDER_CLEAR_STAT 0xC0
173 #define THUNDER_1_LIGHTNING 0x80
174 #define THUNDER_5_LIGHTNINGS 0x90
175 #define THUNDER_9_LIGHTNINGS 0xA0
176 #define THUNDER_16_LIGHTNINGS 0xB0
177 #define THUNDER_DEFAULT_SREJ 0x82
178 
184 #define THUNDER_DIV_RATIO_16 0x00
185 #define THUNDER_DIV_RATIO_32 0x40
186 #define THUNDER_DIV_RATIO_64 0x80
187 #define THUNDER_DIV_RATIO_128 0xC0
188 #define THUNDER_MASK_DISTURBER 0x20
189 #define THUNDER_UNMASK_DISTURBER 0x00
190 #define THUNDER_NO_INTERR 0x00
191 #define THUNDER_NOISE_LEVEL_INTERR 0x01
192 #define THUNDER_DISTURBER_INTERR 0x04
193 #define THUNDER_LIGHTNING_INTERR 0x08
194 
200 #define THUNDER_LCO_ON_IRQ_PIN 0x80
201 #define THUNDER_SRCO_ON_IRQ_PIN 0x40
202 #define THUNDER_TRCO_ON_IRQ_PIN 0x20
203 #define THUNDER_NO_DISPLAY_ON_IRQ_PIN 0x00
204 #define THUNDER_DEFAULT_TUNE_CAP 0x03
205 
211 #define THUNDER_RESET_COMM 0x00
212 #define THUNDER_CALIB_RCO_COMM 0x01
213  // End group macro
216 // --------------------------------------------------------------- PUBLIC TYPES
225 typedef struct
226 {
227  // Input pins
228  digital_in_t irq;
229 
230  // Modules
231  spi_master_t spi;
232  pin_name_t chip_select;
233 
234 } thunder_t;
235 
239 typedef struct
240 {
241  // Communication gpio pins
242  pin_name_t miso;
243  pin_name_t mosi;
244  pin_name_t sck;
245  pin_name_t cs;
246 
247  // Additional gpio pins
248  pin_name_t irq;
249 
250  // static variable
251  uint32_t spi_speed;
252  spi_master_mode_t spi_mode;
253  spi_master_chip_select_polarity_t cs_polarity;
254 
255 } thunder_cfg_t;
256  // End types group
258 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
259 
264 #ifdef __cplusplus
265 extern "C"{
266 #endif
267 
277 
287 err_t thunder_init ( thunder_t *ctx, thunder_cfg_t *cfg );
288 
297 
310 err_t thunder_generic_write ( thunder_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
311 
324 err_t thunder_generic_read ( thunder_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
325 
337 err_t thunder_write_reg ( thunder_t *ctx, uint8_t reg, uint8_t data_in );
338 
350 err_t thunder_read_reg ( thunder_t *ctx, uint8_t reg, uint8_t *data_out );
351 
360 void thunder_send_command ( thunder_t *ctx, uint8_t command );
361 
371 uint8_t thunder_check_int ( thunder_t *ctx );
372 
382 void thunder_get_storm_info ( thunder_t *ctx, uint32_t *energy_out, uint8_t *distance_out );
383 
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif // _THUNDER_H_
399  // End public_function group
402 
403 // ------------------------------------------------------------------------- END
thunder_generic_write
err_t thunder_generic_write(thunder_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
Generic write function.
thunder_t::irq
digital_in_t irq
Definition: thunder.h:228
spi_specifics.h
This file contains SPI specific macros, functions, etc.
thunder_cfg_t::spi_speed
uint32_t spi_speed
Definition: thunder.h:251
thunder_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: thunder.h:252
thunder_read_reg
err_t thunder_read_reg(thunder_t *ctx, uint8_t reg, uint8_t *data_out)
Read reg function.
thunder_cfg_t::mosi
pin_name_t mosi
Definition: thunder.h:243
thunder_cfg_t::irq
pin_name_t irq
Definition: thunder.h:248
thunder_t::spi
spi_master_t spi
Definition: thunder.h:231
thunder_get_storm_info
void thunder_get_storm_info(thunder_t *ctx, uint32_t *energy_out, uint8_t *distance_out)
Storm Information Get function.
thunder_cfg_setup
void thunder_cfg_setup(thunder_cfg_t *cfg)
Config Object Initialization function.
thunder_cfg_t
Click configuration structure definition.
Definition: thunder.h:240
thunder_default_cfg
void thunder_default_cfg(thunder_t *ctx)
Click Default Configuration function.
thunder_cfg_t::miso
pin_name_t miso
Definition: thunder.h:242
thunder_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: thunder.h:253
thunder_write_reg
err_t thunder_write_reg(thunder_t *ctx, uint8_t reg, uint8_t data_in)
Write reg function.
thunder_check_int
uint8_t thunder_check_int(thunder_t *ctx)
Interrupt Check function.
thunder_init
err_t thunder_init(thunder_t *ctx, thunder_cfg_t *cfg)
Initialization function.
thunder_send_command
void thunder_send_command(thunder_t *ctx, uint8_t command)
Command Send function.
thunder_cfg_t::cs
pin_name_t cs
Definition: thunder.h:245
thunder_generic_read
err_t thunder_generic_read(thunder_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
Generic read function.
thunder_cfg_t::sck
pin_name_t sck
Definition: thunder.h:244
thunder_t::chip_select
pin_name_t chip_select
Definition: thunder.h:232
thunder_t
Click ctx object definition.
Definition: thunder.h:226
thunder_check_irq_pin
uint8_t thunder_check_irq_pin(thunder_t *ctx)
IRQ Pin Check function.