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 )
75 
81 #define COUNTER_RETVAL uint8_t
82 
83 #define COUNTER_OK 0x00
84 #define COUNTER_INIT_ERROR 0xFF
85 
91 #define COUNTER_CMD_CLR 0x00
92 #define COUNTER_CMD_WR 0x80
93 #define COUNTER_CMD_LOAD 0xC0
94 #define COUNTER_CMD_RD 0X40
95 
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
107 
113 #define COUNTER_NON_QUAD 0x00
114 #define COUNTER_1X_QUAD 0x01
115 #define COUNTER_2X_QUAD 0x02
116 #define COUNTER_4X_QUAD 0x03
117 
123 #define COUNTER_FREE_RUN 0x00
124 #define COUNTER_SINGLE_COUNT 0x04
125 #define COUNTER_RANGE_LIMIT 0x08
126 #define COUNTER_N_MODULE 0x0C
127 
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
137 
143 #define COUNTER_INDEX_NEG 0x00
144 #define COUNTER_INDEX_POS 0x40
145 
150 #define COUNTER_FILTER_CLOCK_DIV1 0x00
151 #define COUNTER_FILTER_CLOCK_DIV2 0x80
152 
158 #define COUNTER_MODE_32 0x00
159 #define COUNTER_MODE_24 0x01
160 #define COUNTER_MODE_16 0x02
161 #define COUNTER_MODE_8 0x03
162 
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
182  // End group macro
185 // --------------------------------------------------------------- PUBLIC TYPES
194 typedef 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 
219 typedef 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 
239 } counter_cfg_t;
240  // End types group
242 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
243 
248 #ifdef __cplusplus
249 extern "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 
311 void counter_write_mdr0 ( counter_t *ctx, uint8_t settings );
312 
324 void counter_write_mdr1 ( counter_t *ctx, uint8_t settings );
325 
339 void counter_write_dtr ( counter_t *ctx, uint8_t* buffer );
340 
354 
369 
378 uint8_t counter_read_mdr0 ( counter_t *ctx );
379 
388 uint8_t counter_read_mdr1 ( counter_t *ctx );
389 
398 int32_t counter_read_otr ( counter_t *ctx );
399 
408 int32_t counter_read_cntr ( counter_t *ctx );
409 
418 int32_t counter_read_dtr ( counter_t *ctx );
419 
443 uint8_t counter_read_str ( counter_t *ctx );
444 
453 
462 
471 
492 
515 
525 void counter_init_advanced ( counter_t *ctx, uint8_t mdr0_set, uint8_t mdr1_set );
526 
534 void counter_enable ( counter_t *ctx );
535 
544 
553 
562 
571 uint8_t get_int_state ( counter_t *ctx );
572 
573 
574 #ifdef __cplusplus
575 }
576 #endif
577 #endif // _COUNTER_H_
578  // End public_function group
581 
582 // ------------------------------------------------------------------------- END
counter_read_cntr
int32_t counter_read_cntr(counter_t *ctx)
Reads CNTR.
counter_cfg_t::sck
pin_name_t sck
Definition: counter.h:225
counter_t::chip_select
pin_name_t chip_select
Definition: counter.h:208
counter_load_cntr
void counter_load_cntr(counter_t *ctx)
Loads CNTR from DTR.
counter_t::int_pin
digital_in_t int_pin
Definition: counter.h:203
counter_init
COUNTER_RETVAL counter_init(counter_t *ctx, counter_cfg_t *cfg)
Initialization function.
counter_chip_enable
void counter_chip_enable(counter_t *ctx)
Enables counting ( Hardware mode )
counter_clear_str
void counter_clear_str(counter_t *ctx)
Clears STR.
counter_t::buffer_size
uint8_t buffer_size
Definition: counter.h:212
counter_t::cs
digital_out_t cs
Definition: counter.h:199
counter_read_str
uint8_t counter_read_str(counter_t *ctx)
Reads STR.
counter_init_advanced
void counter_init_advanced(counter_t *ctx, uint8_t mdr0_set, uint8_t mdr1_set)
Initialisation of click board with specific settings.
COUNTER_RETVAL
#define COUNTER_RETVAL
Definition: counter.h:81
counter_clead_mrd1
void counter_clead_mrd1(counter_t *ctx)
Clears MDR1.
counter_chip_disable
void counter_chip_disable(counter_t *ctx)
Disables counting ( Hardware mode )
counter_t::spi
spi_master_t spi
Definition: counter.h:207
counter_read_otr
int32_t counter_read_otr(counter_t *ctx)
Reads OTR.
counter_read_mdr0
uint8_t counter_read_mdr0(counter_t *ctx)
Reads MDR0.
counter_read_dtr
int32_t counter_read_dtr(counter_t *ctx)
Reads DTR.
counter_t
Click ctx object definition.
Definition: counter.h:195
counter_t::en
digital_out_t en
Definition: counter.h:198
counter_clear_mrd0
void counter_clear_mrd0(counter_t *ctx)
Clears MDR0.
counter_cfg_t
Click configuration structure definition.
Definition: counter.h:220
counter_write_dtr
void counter_write_dtr(counter_t *ctx, uint8_t *buffer)
Writes data into DTR.
counter_generic_transfer
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.
counter_initialisation
void counter_initialisation(counter_t *ctx)
Initialisation of click board with default settings.
counter_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: counter.h:236
counter_enable
void counter_enable(counter_t *ctx)
Enables counting ( Software mode )
counter_disable
void counter_disable(counter_t *ctx)
Disables counting ( Software mode )
counter_read_mdr1
uint8_t counter_read_mdr1(counter_t *ctx)
Reads MDR1.
counter_write_mdr0
void counter_write_mdr0(counter_t *ctx, uint8_t settings)
Writes data into MDR0.
counter_default_cfg
void counter_default_cfg(counter_t *ctx)
Click Default Configuration function.
counter_cfg_t::mosi
pin_name_t mosi
Definition: counter.h:224
counter_cfg_t::spi_speed
uint32_t spi_speed
Definition: counter.h:235
get_int_state
uint8_t get_int_state(counter_t *ctx)
Function for get int-pin state.
counter_cfg_t::int_pin
pin_name_t int_pin
Definition: counter.h:231
counter_cfg_t::en
pin_name_t en
Definition: counter.h:230
counter_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: counter.h:237
counter_cfg_t::cs
pin_name_t cs
Definition: counter.h:226
counter_cfg_t::miso
pin_name_t miso
Definition: counter.h:223
counter_clear_cntr
void counter_clear_cntr(counter_t *ctx)
Clears CNTR.
counter_write_mdr1
void counter_write_mdr1(counter_t *ctx, uint8_t settings)
Writes data into MDR1.
counter_load_otr
void counter_load_otr(counter_t *ctx)
Loads OTR from CNTR.
counter_cfg_setup
void counter_cfg_setup(counter_cfg_t *cfg)
Config Object Initialization function.