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 "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
54 #define COUNTER_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.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
60  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
61 
67 #define COUNTER_RETVAL uint8_t
68 
69 #define COUNTER_OK 0x00
70 #define COUNTER_INIT_ERROR 0xFF
71 
77 #define COUNTER_CMD_CLR 0x00
78 #define COUNTER_CMD_WR 0x80
79 #define COUNTER_CMD_LOAD 0xC0
80 #define COUNTER_CMD_RD 0X40
81 
87 #define COUNTER_MDR0 0x08
88 #define COUNTER_MDR1 0x10
89 #define COUNTER_DTR 0x18
90 #define COUNTER_CNTR 0x20
91 #define COUNTER_OTR 0x28
92 #define COUNTER_STR 0x30
93 
99 #define COUNTER_NON_QUAD 0x00
100 #define COUNTER_1X_QUAD 0x01
101 #define COUNTER_2X_QUAD 0x02
102 #define COUNTER_4X_QUAD 0x03
103 
109 #define COUNTER_FREE_RUN 0x00
110 #define COUNTER_SINGLE_COUNT 0x04
111 #define COUNTER_RANGE_LIMIT 0x08
112 #define COUNTER_N_MODULE 0x0C
113 
119 #define COUNTER_INDEX_DISABLED 0x00
120 #define COUNTER_INDEX_LOAD_CNTR 0x10
121 #define COUNTER_INDEX_RESET_CNTR 0x20
122 #define COUNTER_INDEX_LOAD_OTR 0x30
123 
129 #define COUNTER_INDEX_NEG 0x00
130 #define COUNTER_INDEX_POS 0x40
131 
136 #define COUNTER_FILTER_CLOCK_DIV1 0x00
137 #define COUNTER_FILTER_CLOCK_DIV2 0x80
138 
144 #define COUNTER_MODE_32 0x00
145 #define COUNTER_MODE_24 0x01
146 #define COUNTER_MODE_16 0x02
147 #define COUNTER_MODE_8 0x03
148 
154 #define COUNTER_ENABLE 0x00
155 #define COUNTER_DISABLE 0x04
156 
163 #define COUNTER_FLAG_DISABLE 0x00
164 #define COUNTER_FLAG_IDX 0x10
165 #define COUNTER_FLAG_CMP 0x20
166 #define COUNTER_FLAG_BW 0x40
167 #define COUNTER_FLAG_FY 0x80
168  // End group macro
171 // --------------------------------------------------------------- PUBLIC TYPES
180 typedef struct
181 {
182  // Output pins
183 
184  digital_out_t en;
185  digital_out_t cs;
186 
187  // Input pins
188 
189  digital_in_t int_pin;
190 
191  // Modules
192 
193  spi_master_t spi;
194  pin_name_t chip_select;
195 
196  // Variables
197 
198  uint8_t buffer_size;
199 
200 } counter_t;
201 
205 typedef struct
206 {
207  // Communication gpio pins
208 
209  pin_name_t miso;
210  pin_name_t mosi;
211  pin_name_t sck;
212  pin_name_t cs;
213 
214  // Additional gpio pins
215 
216  pin_name_t en;
217  pin_name_t int_pin;
218 
219  // static variable
220 
221  uint32_t spi_speed;
222  spi_master_mode_t spi_mode;
223  spi_master_chip_select_polarity_t cs_polarity;
224 
225 } counter_cfg_t;
226  // End types group
228 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
229 
234 #ifdef __cplusplus
235 extern "C"{
236 #endif
237 
247 
256 
265 
278 (
279  counter_t *ctx,
280  uint8_t *wr_buf,
281  uint16_t wr_len,
282  uint8_t *rd_buf,
283  uint16_t rd_len
284 );
285 
297 void counter_write_mdr0 ( counter_t *ctx, uint8_t settings );
298 
310 void counter_write_mdr1 ( counter_t *ctx, uint8_t settings );
311 
325 void counter_write_dtr ( counter_t *ctx, uint8_t* buffer );
326 
340 
355 
364 uint8_t counter_read_mdr0 ( counter_t *ctx );
365 
374 uint8_t counter_read_mdr1 ( counter_t *ctx );
375 
384 int32_t counter_read_otr ( counter_t *ctx );
385 
394 int32_t counter_read_cntr ( counter_t *ctx );
395 
404 int32_t counter_read_dtr ( counter_t *ctx );
405 
429 uint8_t counter_read_str ( counter_t *ctx );
430 
439 
448 
457 
478 
501 
511 void counter_init_advanced ( counter_t *ctx, uint8_t mdr0_set, uint8_t mdr1_set );
512 
520 void counter_enable ( counter_t *ctx );
521 
530 
539 
548 
557 uint8_t get_int_state ( counter_t *ctx );
558 
559 
560 #ifdef __cplusplus
561 }
562 #endif
563 #endif // _COUNTER_H_
564  // End public_function group
567 
568 // ------------------------------------------------------------------------- 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:211
counter_t::chip_select
pin_name_t chip_select
Definition: counter.h:194
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:189
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:198
counter_t::cs
digital_out_t cs
Definition: counter.h:185
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:67
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:193
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:181
counter_t::en
digital_out_t en
Definition: counter.h:184
counter_clear_mrd0
void counter_clear_mrd0(counter_t *ctx)
Clears MDR0.
counter_cfg_t
Click configuration structure definition.
Definition: counter.h:206
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:222
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:210
counter_cfg_t::spi_speed
uint32_t spi_speed
Definition: counter.h:221
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:217
counter_cfg_t::en
pin_name_t en
Definition: counter.h:216
counter_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: counter.h:223
counter_cfg_t::cs
pin_name_t cs
Definition: counter.h:212
counter_cfg_t::miso
pin_name_t miso
Definition: counter.h:209
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.