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 
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 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
64 #define COUNTER_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.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
71 
77 #define COUNTER_RETVAL uint8_t
78 
79 #define COUNTER_OK 0x00
80 #define COUNTER_INIT_ERROR 0xFF
81 
87 #define COUNTER_CMD_CLR 0x00
88 #define COUNTER_CMD_WR 0x80
89 #define COUNTER_CMD_LOAD 0xC0
90 #define COUNTER_CMD_RD 0X40
91 
97 #define COUNTER_MDR0 0x08
98 #define COUNTER_MDR1 0x10
99 #define COUNTER_DTR 0x18
100 #define COUNTER_CNTR 0x20
101 #define COUNTER_OTR 0x28
102 #define COUNTER_STR 0x30
103 
109 #define COUNTER_NON_QUAD 0x00
110 #define COUNTER_1X_QUAD 0x01
111 #define COUNTER_2X_QUAD 0x02
112 #define COUNTER_4X_QUAD 0x03
113 
119 #define COUNTER_FREE_RUN 0x00
120 #define COUNTER_SINGLE_COUNT 0x04
121 #define COUNTER_RANGE_LIMIT 0x08
122 #define COUNTER_N_MODULE 0x0C
123 
129 #define COUNTER_INDEX_DISABLED 0x00
130 #define COUNTER_INDEX_LOAD_CNTR 0x10
131 #define COUNTER_INDEX_RESET_CNTR 0x20
132 #define COUNTER_INDEX_LOAD_OTR 0x30
133 
139 #define COUNTER_INDEX_NEG 0x00
140 #define COUNTER_INDEX_POS 0x40
141 
146 #define COUNTER_FILTER_CLOCK_DIV1 0x00
147 #define COUNTER_FILTER_CLOCK_DIV2 0x80
148 
154 #define COUNTER_MODE_32 0x00
155 #define COUNTER_MODE_24 0x01
156 #define COUNTER_MODE_16 0x02
157 #define COUNTER_MODE_8 0x03
158 
164 #define COUNTER_ENABLE 0x00
165 #define COUNTER_DISABLE 0x04
166 
173 #define COUNTER_FLAG_DISABLE 0x00
174 #define COUNTER_FLAG_IDX 0x10
175 #define COUNTER_FLAG_CMP 0x20
176 #define COUNTER_FLAG_BW 0x40
177 #define COUNTER_FLAG_FY 0x80
178  // End group macro
181 // --------------------------------------------------------------- PUBLIC TYPES
190 typedef struct
191 {
192  // Output pins
193 
194  digital_out_t en;
195  digital_out_t cs;
196 
197  // Input pins
198 
199  digital_in_t int_pin;
200 
201  // Modules
202 
203  spi_master_t spi;
204  pin_name_t chip_select;
205 
206  // Variables
207 
208  uint8_t buffer_size;
209 
210 } counter_t;
211 
215 typedef struct
216 {
217  // Communication gpio pins
218 
219  pin_name_t miso;
220  pin_name_t mosi;
221  pin_name_t sck;
222  pin_name_t cs;
223 
224  // Additional gpio pins
225 
226  pin_name_t en;
227  pin_name_t int_pin;
228 
229  // static variable
230 
231  uint32_t spi_speed;
232  spi_master_mode_t spi_mode;
233  spi_master_chip_select_polarity_t cs_polarity;
234 
235 } counter_cfg_t;
236  // End types group
238 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
239 
244 #ifdef __cplusplus
245 extern "C"{
246 #endif
247 
257 
266 
275 
288 (
289  counter_t *ctx,
290  uint8_t *wr_buf,
291  uint16_t wr_len,
292  uint8_t *rd_buf,
293  uint16_t rd_len
294 );
295 
307 void counter_write_mdr0 ( counter_t *ctx, uint8_t settings );
308 
320 void counter_write_mdr1 ( counter_t *ctx, uint8_t settings );
321 
335 void counter_write_dtr ( counter_t *ctx, uint8_t* buffer );
336 
350 
365 
374 uint8_t counter_read_mdr0 ( counter_t *ctx );
375 
384 uint8_t counter_read_mdr1 ( counter_t *ctx );
385 
394 int32_t counter_read_otr ( counter_t *ctx );
395 
404 int32_t counter_read_cntr ( counter_t *ctx );
405 
414 int32_t counter_read_dtr ( counter_t *ctx );
415 
439 uint8_t counter_read_str ( counter_t *ctx );
440 
449 
458 
467 
488 
511 
521 void counter_init_advanced ( counter_t *ctx, uint8_t mdr0_set, uint8_t mdr1_set );
522 
530 void counter_enable ( counter_t *ctx );
531 
540 
549 
558 
567 uint8_t get_int_state ( counter_t *ctx );
568 
569 
570 #ifdef __cplusplus
571 }
572 #endif
573 #endif // _COUNTER_H_
574  // End public_function group
577 
578 // ------------------------------------------------------------------------- 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:221
counter_t::chip_select
pin_name_t chip_select
Definition: counter.h:204
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:199
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:208
counter_t::cs
digital_out_t cs
Definition: counter.h:195
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:77
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:203
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:191
counter_t::en
digital_out_t en
Definition: counter.h:194
counter_clear_mrd0
void counter_clear_mrd0(counter_t *ctx)
Clears MDR0.
counter_cfg_t
Click configuration structure definition.
Definition: counter.h:216
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:232
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:220
counter_cfg_t::spi_speed
uint32_t spi_speed
Definition: counter.h:231
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:227
counter_cfg_t::en
pin_name_t en
Definition: counter.h:226
counter_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: counter.h:233
counter_cfg_t::cs
pin_name_t cs
Definition: counter.h:222
counter_cfg_t::miso
pin_name_t miso
Definition: counter.h:219
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.