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 
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 
66 #define THUNDER_RETVAL uint8_t
67 
68 #define THUNDER_OK 0x00
69 #define THUNDER_INIT_ERROR 0xFF
70 
76 #define THUNDER_DEV_ADDR_0 0x00
77 #define THUNDER_DEV_ADDR_1 0x01
78 #define THUNDER_DEV_ADDR_2 0x02
79 #define THUNDER_DEV_ADDR_3 0x03
80 
86 #define THUNDER_CONFIG_0_REG 0x00
87 #define THUNDER_CONFIG_1_REG 0x01
88 #define THUNDER_CONFIG_2_REG 0x02
89 #define THUNDER_CONFIG_3_INTERR_REG 0x03
90 #define THUNDER_ENERGY_LIGHTNING_LSBYTE_REG 0x04
91 #define THUNDER_ENERGY_LIGHTNING_MSBYTE_REG 0x05
92 #define THUNDER_ENERGY_LIGHTNING_MMSBYTE_REG 0x06
93 #define THUNDER_DISTANCE_REG 0x07
94 #define THUNDER_INTERR_PIN_CONFIG_REG 0x08
95 #define THUNDER_LDLUT_1_REG 0x09
96 #define THUNDER_LDLUT_2_REG 0x0A
97 #define THUNDER_LDLUT_3_REG 0x0B
98 #define THUNDER_LDLUT_4_REG 0x0C
99 #define THUNDER_LDLUT_5_REG 0x0D
100 #define THUNDER_LDLUT_6_REG 0x0E
101 #define THUNDER_LDLUT_7_REG 0x0F
102 #define THUNDER_LDLUT_8_REG 0x10
103 #define THUNDER_LDLUT_9_REG 0x11
104 #define THUNDER_LDLUT_10_REG 0x12
105 #define THUNDER_LDLUT_11_REG 0x13
106 #define THUNDER_LDLUT_12_REG 0x14
107 #define THUNDER_LDLUT_13_REG 0x15
108 #define THUNDER_LDLUT_14_REG 0x16
109 #define THUNDER_LDLUT_15_REG 0x17
110 #define THUNDER_LDLUT_16_REG 0x18
111 #define THUNDER_LDLUT_17_REG 0x19
112 #define THUNDER_LDLUT_18_REG 0x1A
113 #define THUNDER_LDLUT_19_REG 0x1B
114 #define THUNDER_LDLUT_20_REG 0x1C
115 #define THUNDER_LDLUT_21_REG 0x1D
116 #define THUNDER_LDLUT_22_REG 0x1E
117 #define THUNDER_LDLUT_23_REG 0x1F
118 #define THUNDER_LDLUT_24_REG 0x20
119 #define THUNDER_LDLUT_25_REG 0x21
120 #define THUNDER_LDLUT_26_REG 0x22
121 #define THUNDER_LDLUT_27_REG 0x23
122 #define THUNDER_LDLUT_28_REG 0x24
123 #define THUNDER_LDLUT_29_REG 0x25
124 #define THUNDER_LDLUT_30_REG 0x26
125 #define THUNDER_LDLUT_31_REG 0x27
126 #define THUNDER_LDLUT_32_REG 0x28
127 #define THUNDER_LDLUT_33_REG 0x29
128 #define THUNDER_LDLUT_34_REG 0x2A
129 #define THUNDER_LDLUT_35_REG 0x2B
130 #define THUNDER_LDLUT_36_REG 0x2C
131 #define THUNDER_LDLUT_37_REG 0x2D
132 #define THUNDER_LDLUT_38_REG 0x2E
133 #define THUNDER_LDLUT_39_REG 0x2F
134 #define THUNDER_LDLUT_40_REG 0x30
135 #define THUNDER_LDLUT_41_REG 0x31
136 #define THUNDER_LDLUT_42_REG 0x32
137 #define THUNDER_RESET_REG 0x3C
138 #define THUNDER_CALIB_REG 0x3D
139 
145 #define THUNDER_INDOOR 0x24
146 #define THUNDER_OUTDOOR 0x1C
147 #define THUNDER_NORMAL_MODE 0x00
148 #define THUNDER_POWER_DOWN 0x01
149 
155 #define THUNDER_CLEAR_STAT 0xC0
156 #define THUNDER_1_LIGHTNING 0x80
157 #define THUNDER_5_LIGHTNINGS 0x90
158 #define THUNDER_9_LIGHTNINGS 0xA0
159 #define THUNDER_16_LIGHTNINGS 0xB0
160 
166 #define THUNDER_DIV_RATIO_16 0x00
167 #define THUNDER_DIV_RATIO_32 0x40
168 #define THUNDER_DIV_RATIO_64 0x80
169 #define THUNDER_DIV_RATIO_128 0xC0
170 #define THUNDER_MASK_DISTURBER 0x20
171 #define THUNDER_UNMASK_DISTURBER 0x00
172 #define THUNDER_NO_INTERR 0x00
173 #define THUNDER_NOISE_LEVEL_INTERR 0x01
174 #define THUNDER_DISTURBER_INTERR 0x04
175 #define THUNDER_LIGHTNING_INTERR 0x08
176 
182 #define THUNDER_LCO_ON_IRQ_PIN 0x80
183 #define THUNDER_SRCO_ON_IRQ_PIN 0x40
184 #define THUNDER_TRCO_ON_IRQ_PIN 0x20
185 #define THUNDER_NO_DISPLAY_ON_IRQ_PIN 0x00
186 
192 #define THUNDER_RESET_COMM 0x00
193 #define THUNDER_CALIB_RCO_COMM 0x01
194  // End group macro
197 // --------------------------------------------------------------- PUBLIC TYPES
206 typedef struct
207 {
208  digital_out_t cs;
209 
210  // Input pins
211 
212  digital_in_t irq;
213 
214  // Modules
215 
216  spi_master_t spi;
217  pin_name_t chip_select;
218 
219 } thunder_t;
220 
224 typedef struct
225 {
226  // Communication gpio pins
227 
228  pin_name_t miso;
229  pin_name_t mosi;
230  pin_name_t sck;
231  pin_name_t cs;
232 
233  // Additional gpio pins
234 
235  pin_name_t irq;
236 
237  // static variable
238 
239  uint32_t spi_speed;
240  uint8_t spi_mode;
241  spi_master_chip_select_polarity_t cs_polarity;
242 
243 } thunder_cfg_t;
244  // End types group
246 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
247 
252 #ifdef __cplusplus
253 extern "C"{
254 #endif
255 
264 void thunder_cfg_setup ( thunder_cfg_t *cfg );
265 
274 
282 void thunder_default_cfg ( thunder_t *ctx );
283 
296 (
297  thunder_t *ctx,
298  uint8_t *wr_buf,
299  uint16_t wr_len,
300  uint8_t *rd_buf,
301  uint16_t rd_len
302 );
303 
315 uint8_t thunder_write_reg ( thunder_t *ctx, uint8_t reg_addr, uint8_t transfer_data );
316 
329 uint8_t thunder_read_reg ( thunder_t *ctx, uint8_t reg_addr, uint8_t *data_out, uint8_t n_bytes );
330 
339 void thunder_send_command ( thunder_t *ctx, uint8_t command );
340 
349 uint8_t thunder_check_interr ( thunder_t *ctx );
350 
360 void thunder_get_storm_info ( thunder_t *ctx, uint32_t *energy_out, uint8_t *distance_out );
361 
369 uint8_t thunder_check_irq_pin ( thunder_t *ctx );
370 
371 
372 #ifdef __cplusplus
373 }
374 #endif
375 #endif // _THUNDER_H_
376  // End public_function group
379 
380 // ------------------------------------------------------------------------- END
thunder_t::cs
digital_out_t cs
Definition: thunder.h:208
thunder_t::irq
digital_in_t irq
Definition: thunder.h:212
THUNDER_RETVAL
#define THUNDER_RETVAL
Definition: thunder.h:66
thunder_cfg_t::spi_mode
uint8_t spi_mode
Definition: thunder.h:240
thunder_cfg_t::spi_speed
uint32_t spi_speed
Definition: thunder.h:239
thunder_init
THUNDER_RETVAL thunder_init(thunder_t *ctx, thunder_cfg_t *cfg)
Initialization function.
thunder_cfg_t::mosi
pin_name_t mosi
Definition: thunder.h:229
thunder_cfg_t::irq
pin_name_t irq
Definition: thunder.h:235
thunder_t::spi
spi_master_t spi
Definition: thunder.h:216
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:224
thunder_read_reg
uint8_t thunder_read_reg(thunder_t *ctx, uint8_t reg_addr, uint8_t *data_out, uint8_t n_bytes)
Generic Read function.
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:228
thunder_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: thunder.h:241
thunder_write_reg
uint8_t thunder_write_reg(thunder_t *ctx, uint8_t reg_addr, uint8_t transfer_data)
Generic Write 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:231
thunder_cfg_t::sck
pin_name_t sck
Definition: thunder.h:230
thunder_check_interr
uint8_t thunder_check_interr(thunder_t *ctx)
Interrupt Check function.
thunder_t::chip_select
pin_name_t chip_select
Definition: thunder.h:217
thunder_t
Click ctx object definition.
Definition: thunder.h:206
thunder_check_irq_pin
uint8_t thunder_check_irq_pin(thunder_t *ctx)
IRQ Pin Check function.
thunder_generic_transfer
void thunder_generic_transfer(thunder_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.