counter 2.0.0.0
counter.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 COUNTER_H
36#define COUNTER_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_spi_master.h"
55
56
57// -------------------------------------------------------------- PUBLIC MACROS
68#define COUNTER_MAP_MIKROBUS( cfg, mikrobus ) \
69 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
73 cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
74 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
81#define COUNTER_RETVAL uint8_t
82
83#define COUNTER_OK 0x00
84#define COUNTER_INIT_ERROR 0xFF
91#define COUNTER_CMD_CLR 0x00
92#define COUNTER_CMD_WR 0x80
93#define COUNTER_CMD_LOAD 0xC0
94#define COUNTER_CMD_RD 0X40
101#define COUNTER_MDR0 0x08
102#define COUNTER_MDR1 0x10
103#define COUNTER_DTR 0x18
104#define COUNTER_CNTR 0x20
105#define COUNTER_OTR 0x28
106#define COUNTER_STR 0x30
113#define COUNTER_NON_QUAD 0x00
114#define COUNTER_1X_QUAD 0x01
115#define COUNTER_2X_QUAD 0x02
116#define COUNTER_4X_QUAD 0x03
123#define COUNTER_FREE_RUN 0x00
124#define COUNTER_SINGLE_COUNT 0x04
125#define COUNTER_RANGE_LIMIT 0x08
126#define COUNTER_N_MODULE 0x0C
133#define COUNTER_INDEX_DISABLED 0x00
134#define COUNTER_INDEX_LOAD_CNTR 0x10
135#define COUNTER_INDEX_RESET_CNTR 0x20
136#define COUNTER_INDEX_LOAD_OTR 0x30
143#define COUNTER_INDEX_NEG 0x00
144#define COUNTER_INDEX_POS 0x40
150#define COUNTER_FILTER_CLOCK_DIV1 0x00
151#define COUNTER_FILTER_CLOCK_DIV2 0x80
158#define COUNTER_MODE_32 0x00
159#define COUNTER_MODE_24 0x01
160#define COUNTER_MODE_16 0x02
161#define COUNTER_MODE_8 0x03
168#define COUNTER_ENABLE 0x00
169#define COUNTER_DISABLE 0x04
170
177#define COUNTER_FLAG_DISABLE 0x00
178#define COUNTER_FLAG_IDX 0x10
179#define COUNTER_FLAG_CMP 0x20
180#define COUNTER_FLAG_BW 0x40
181#define COUNTER_FLAG_FY 0x80
184 // End group macro
185// --------------------------------------------------------------- PUBLIC TYPES
194typedef struct
195{
196 // Output pins
197
198 digital_out_t en;
199 digital_out_t cs;
200
201 // Input pins
202
203 digital_in_t int_pin;
204
205 // Modules
206
207 spi_master_t spi;
208 pin_name_t chip_select;
209
210 // Variables
211
212 uint8_t buffer_size;
213
214} counter_t;
215
219typedef struct
220{
221 // Communication gpio pins
222
223 pin_name_t miso;
224 pin_name_t mosi;
225 pin_name_t sck;
226 pin_name_t cs;
227
228 // Additional gpio pins
229
230 pin_name_t en;
231 pin_name_t int_pin;
232
233 // static variable
234
235 uint32_t spi_speed;
236 spi_master_mode_t spi_mode;
237 spi_master_chip_select_polarity_t cs_polarity;
238
240
241 // End types group
242// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
243
248#ifdef __cplusplus
249extern "C"{
250#endif
251
261
270
279
292(
293 counter_t *ctx,
294 uint8_t *wr_buf,
295 uint16_t wr_len,
296 uint8_t *rd_buf,
297 uint16_t rd_len
298);
299
311void counter_write_mdr0 ( counter_t *ctx, uint8_t settings );
312
324void counter_write_mdr1 ( counter_t *ctx, uint8_t settings );
325
339void counter_write_dtr ( counter_t *ctx, uint8_t* buffer );
340
354
369
379
389
399
409
419
444
453
462
471
492
515
525void counter_init_advanced ( counter_t *ctx, uint8_t mdr0_set, uint8_t mdr1_set );
526
535
544
553
562
571uint8_t get_int_state ( counter_t *ctx );
572
573
574#ifdef __cplusplus
575}
576#endif
577#endif // _COUNTER_H_
578
579 // End public_function group
581
582// ------------------------------------------------------------------------- END
#define COUNTER_RETVAL
Definition counter.h:81
void counter_default_cfg(counter_t *ctx)
Click Default Configuration function.
uint8_t get_int_state(counter_t *ctx)
Function for get int-pin state.
void counter_clear_cntr(counter_t *ctx)
Clears CNTR.
void counter_load_cntr(counter_t *ctx)
Loads CNTR from DTR.
void counter_load_otr(counter_t *ctx)
Loads OTR from CNTR.
uint8_t counter_read_mdr1(counter_t *ctx)
Reads MDR1.
uint8_t counter_read_mdr0(counter_t *ctx)
Reads MDR0.
void counter_clead_mrd1(counter_t *ctx)
Clears MDR1.
void counter_clear_str(counter_t *ctx)
Clears STR.
void counter_clear_mrd0(counter_t *ctx)
Clears MDR0.
void counter_generic_transfer(counter_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
void counter_write_mdr0(counter_t *ctx, uint8_t settings)
Writes data into MDR0.
void counter_enable(counter_t *ctx)
Enables counting ( Software mode )
int32_t counter_read_otr(counter_t *ctx)
Reads OTR.
void counter_cfg_setup(counter_cfg_t *cfg)
Config Object Initialization function.
uint8_t counter_read_str(counter_t *ctx)
Reads STR.
int32_t counter_read_cntr(counter_t *ctx)
Reads CNTR.
int32_t counter_read_dtr(counter_t *ctx)
Reads DTR.
void counter_write_mdr1(counter_t *ctx, uint8_t settings)
Writes data into MDR1.
void counter_chip_enable(counter_t *ctx)
Enables counting ( Hardware mode )
void counter_disable(counter_t *ctx)
Disables counting ( Software mode )
void counter_chip_disable(counter_t *ctx)
Disables counting ( Hardware mode )
void counter_initialisation(counter_t *ctx)
Initialisation of click board with default settings.
COUNTER_RETVAL counter_init(counter_t *ctx, counter_cfg_t *cfg)
Initialization function.
void counter_init_advanced(counter_t *ctx, uint8_t mdr0_set, uint8_t mdr1_set)
Initialisation of click board with specific settings.
void counter_write_dtr(counter_t *ctx, uint8_t *buffer)
Writes data into DTR.
Click configuration structure definition.
Definition counter.h:220
spi_master_chip_select_polarity_t cs_polarity
Definition counter.h:237
pin_name_t sck
Definition counter.h:225
spi_master_mode_t spi_mode
Definition counter.h:236
pin_name_t mosi
Definition counter.h:224
uint32_t spi_speed
Definition counter.h:235
pin_name_t en
Definition counter.h:230
pin_name_t int_pin
Definition counter.h:231
pin_name_t miso
Definition counter.h:223
pin_name_t cs
Definition counter.h:226
Click ctx object definition.
Definition counter.h:195
digital_out_t cs
Definition counter.h:199
spi_master_t spi
Definition counter.h:207
digital_in_t int_pin
Definition counter.h:203
uint8_t buffer_size
Definition counter.h:212
digital_out_t en
Definition counter.h:198
pin_name_t chip_select
Definition counter.h:208