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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_spi_master.h"
51 #include "spi_specifics.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define THUNDER_MAP_MIKROBUS( cfg, mikrobus ) \
65  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
66  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
67  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
68  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.irq = MIKROBUS( mikrobus, MIKROBUS_INT )
70 
80 #define THUNDER_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
81 #define THUNDER_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
82 
87 #define THUNDER_OK 0x00
88 #define THUNDER_ERROR 0xFF
89 
95 #define THUNDER_DEV_ADDR_0 0x00
96 #define THUNDER_DEV_ADDR_1 0x01
97 #define THUNDER_DEV_ADDR_2 0x02
98 #define THUNDER_DEV_ADDR_3 0x03
99 
105 #define THUNDER_REG_CONFIG_0 0x00
106 #define THUNDER_REG_CONFIG_1 0x01
107 #define THUNDER_REG_CONFIG_2 0x02
108 #define THUNDER_REG_CONFIG_3_INTERR 0x03
109 #define THUNDER_REG_ENERGY_LIGHTNING_LSBYTE 0x04
110 #define THUNDER_REG_ENERGY_LIGHTNING_MSBYTE 0x05
111 #define THUNDER_REG_ENERGY_LIGHTNING_MMSBYTE 0x06
112 #define THUNDER_REG_DISTANCE 0x07
113 #define THUNDER_REG_INTERR_PIN_CONFIG 0x08
114 #define THUNDER_REG_LDLUT_1 0x09
115 #define THUNDER_REG_LDLUT_2 0x0A
116 #define THUNDER_REG_LDLUT_3 0x0B
117 #define THUNDER_REG_LDLUT_4 0x0C
118 #define THUNDER_REG_LDLUT_5 0x0D
119 #define THUNDER_REG_LDLUT_6 0x0E
120 #define THUNDER_REG_LDLUT_7 0x0F
121 #define THUNDER_REG_LDLUT_8 0x10
122 #define THUNDER_REG_LDLUT_9 0x11
123 #define THUNDER_REG_LDLUT_10 0x12
124 #define THUNDER_REG_LDLUT_11 0x13
125 #define THUNDER_REG_LDLUT_12 0x14
126 #define THUNDER_REG_LDLUT_13 0x15
127 #define THUNDER_REG_LDLUT_14 0x16
128 #define THUNDER_REG_LDLUT_15 0x17
129 #define THUNDER_REG_LDLUT_16 0x18
130 #define THUNDER_REG_LDLUT_17 0x19
131 #define THUNDER_REG_LDLUT_18 0x1A
132 #define THUNDER_REG_LDLUT_19 0x1B
133 #define THUNDER_REG_LDLUT_20 0x1C
134 #define THUNDER_REG_LDLUT_21 0x1D
135 #define THUNDER_REG_LDLUT_22 0x1E
136 #define THUNDER_REG_LDLUT_23 0x1F
137 #define THUNDER_REG_LDLUT_24 0x20
138 #define THUNDER_REG_LDLUT_25 0x21
139 #define THUNDER_REG_LDLUT_26 0x22
140 #define THUNDER_REG_LDLUT_27 0x23
141 #define THUNDER_REG_LDLUT_28 0x24
142 #define THUNDER_REG_LDLUT_29 0x25
143 #define THUNDER_REG_LDLUT_30 0x26
144 #define THUNDER_REG_LDLUT_31 0x27
145 #define THUNDER_REG_LDLUT_32 0x28
146 #define THUNDER_REG_LDLUT_33 0x29
147 #define THUNDER_REG_LDLUT_34 0x2A
148 #define THUNDER_REG_LDLUT_35 0x2B
149 #define THUNDER_REG_LDLUT_36 0x2C
150 #define THUNDER_REG_LDLUT_37 0x2D
151 #define THUNDER_REG_LDLUT_38 0x2E
152 #define THUNDER_REG_LDLUT_39 0x2F
153 #define THUNDER_REG_LDLUT_40 0x30
154 #define THUNDER_REG_LDLUT_41 0x31
155 #define THUNDER_REG_LDLUT_42 0x32
156 #define THUNDER_REG_RESET 0x3C
157 #define THUNDER_REG_CALIB 0x3D
158 
164 #define THUNDER_INDOOR 0x24
165 #define THUNDER_OUTDOOR 0x1C
166 #define THUNDER_NORMAL_MODE 0x00
167 #define THUNDER_POWER_DOWN 0x01
168 
174 #define THUNDER_DEFAULT_NOISE_FLOOR_LVL 0x20
175 #define THUNDER_DEFAULT_WDG_THRESHOLD 0x02
176 
182 #define THUNDER_CLEAR_STAT 0xC0
183 #define THUNDER_1_LIGHTNING 0x80
184 #define THUNDER_5_LIGHTNINGS 0x90
185 #define THUNDER_9_LIGHTNINGS 0xA0
186 #define THUNDER_16_LIGHTNINGS 0xB0
187 #define THUNDER_DEFAULT_SREJ 0x82
188 
194 #define THUNDER_DIV_RATIO_16 0x00
195 #define THUNDER_DIV_RATIO_32 0x40
196 #define THUNDER_DIV_RATIO_64 0x80
197 #define THUNDER_DIV_RATIO_128 0xC0
198 #define THUNDER_MASK_DISTURBER 0x20
199 #define THUNDER_UNMASK_DISTURBER 0x00
200 #define THUNDER_NO_INTERR 0x00
201 #define THUNDER_NOISE_LEVEL_INTERR 0x01
202 #define THUNDER_DISTURBER_INTERR 0x04
203 #define THUNDER_LIGHTNING_INTERR 0x08
204 
210 #define THUNDER_LCO_ON_IRQ_PIN 0x80
211 #define THUNDER_SRCO_ON_IRQ_PIN 0x40
212 #define THUNDER_TRCO_ON_IRQ_PIN 0x20
213 #define THUNDER_NO_DISPLAY_ON_IRQ_PIN 0x00
214 #define THUNDER_DEFAULT_TUNE_CAP 0x00
215 
221 #define THUNDER_RESET_COMM 0x00
222 #define THUNDER_CALIB_RCO_COMM 0x01
223  // End group macro
226 // --------------------------------------------------------------- PUBLIC TYPES
235 typedef struct
236 {
237  // Input pins
238  digital_in_t irq;
239 
240  // Modules
241  spi_master_t spi;
242  pin_name_t chip_select;
243 
244 } thunder_t;
245 
249 typedef struct
250 {
251  // Communication gpio pins
252  pin_name_t miso;
253  pin_name_t mosi;
254  pin_name_t sck;
255  pin_name_t cs;
256 
257  // Additional gpio pins
258  pin_name_t irq;
259 
260  // static variable
261  uint32_t spi_speed;
262  spi_master_mode_t spi_mode;
263  spi_master_chip_select_polarity_t cs_polarity;
264 
265 } thunder_cfg_t;
266  // End types group
268 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
269 
274 #ifdef __cplusplus
275 extern "C"{
276 #endif
277 
287 
297 err_t thunder_init ( thunder_t *ctx, thunder_cfg_t *cfg );
298 
307 
320 err_t thunder_generic_write ( thunder_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
321 
334 err_t thunder_generic_read ( thunder_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
335 
347 err_t thunder_write_reg ( thunder_t *ctx, uint8_t reg, uint8_t data_in );
348 
360 err_t thunder_read_reg ( thunder_t *ctx, uint8_t reg, uint8_t *data_out );
361 
370 void thunder_send_command ( thunder_t *ctx, uint8_t command );
371 
381 uint8_t thunder_check_int ( thunder_t *ctx );
382 
392 void thunder_get_storm_info ( thunder_t *ctx, uint32_t *energy_out, uint8_t *distance_out );
393 
404 
405 #ifdef __cplusplus
406 }
407 #endif
408 #endif // _THUNDER_H_
409  // End public_function group
412 
413 // ------------------------------------------------------------------------- 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:238
spi_specifics.h
This file contains SPI specific macros, functions, etc.
thunder_cfg_t::spi_speed
uint32_t spi_speed
Definition: thunder.h:261
thunder_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: thunder.h:262
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:253
thunder_cfg_t::irq
pin_name_t irq
Definition: thunder.h:258
thunder_t::spi
spi_master_t spi
Definition: thunder.h:241
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:250
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:252
thunder_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: thunder.h:263
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:255
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:254
thunder_t::chip_select
pin_name_t chip_select
Definition: thunder.h:242
thunder_t
Click ctx object definition.
Definition: thunder.h:236
thunder_check_irq_pin
uint8_t thunder_check_irq_pin(thunder_t *ctx)
IRQ Pin Check function.