ammeter  2.0.0.0
ammeter.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 AMMETER_H
36 #define AMMETER_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_spi_master.h"
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
66 #define AMMETER_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
68  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
69  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
70  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
71 
77 #define AMMETER_RETVAL uint8_t
78 
79 #define AMMETER_OK 0x00
80 #define AMMETER_INIT_ERROR 0xFF
81 
87 #define AMMETER_SAMPLE_NUM 10
88 #define AMMETER_FILTER_USEFULL_DATA 0x1FFE
89 #define AMMETER_CALC_COEF 1024.0
90 #define AMMETER_STABULISATION_COEF 2.0
91  // End group macro
94 // --------------------------------------------------------------- PUBLIC TYPES
103 typedef struct
104 {
105  digital_out_t cs;
106 
107  // Modules
108 
109  spi_master_t spi;
110  pin_name_t chip_select;
111 
112 } ammeter_t;
113 
117 typedef struct
118 {
119  // Communication gpio pins
120 
121  pin_name_t miso;
122  pin_name_t mosi;
123  pin_name_t sck;
124  pin_name_t cs;
125 
126  // static variable
127 
128  uint32_t spi_speed;
129  spi_master_mode_t spi_mode;
130  spi_master_chip_select_polarity_t cs_polarity;
131 
132 } ammeter_cfg_t;
133  // End types group
135 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
136 
141 #ifdef __cplusplus
142 extern "C"{
143 #endif
144 
154 
164 
174 uint16_t ammeter_data_read ( ammeter_t *ctx );
175 
185 uint16_t ammeter_get_raw_data ( ammeter_t *ctx );
186 
199 
212 
213 #ifdef __cplusplus
214 }
215 #endif
216 #endif // _AMMETER_H_
217  // End public_function group
220 
221 // ------------------------------------------------------------------------- END
ammeter_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: ammeter.h:129
ammeter_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: ammeter.h:130
ammeter_cfg_t
Click configuration structure definition.
Definition: ammeter.h:118
AMMETER_RETVAL
#define AMMETER_RETVAL
Definition: ammeter.h:77
ammeter_cfg_t::mosi
pin_name_t mosi
Definition: ammeter.h:122
ammeter_t
Click ctx object definition.
Definition: ammeter.h:104
ammeter_cfg_setup
void ammeter_cfg_setup(ammeter_cfg_t *cfg)
Config Object Initialization function.
ammeter_data_read
uint16_t ammeter_data_read(ammeter_t *ctx)
Data read function.
ammeter_get_raw_data
uint16_t ammeter_get_raw_data(ammeter_t *ctx)
Get raw data function.
ammeter_get_avg_data
float ammeter_get_avg_data(ammeter_t *ctx)
Get average average data function.
ammeter_t::spi
spi_master_t spi
Definition: ammeter.h:109
ammeter_cfg_t::cs
pin_name_t cs
Definition: ammeter.h:124
ammeter_init
AMMETER_RETVAL ammeter_init(ammeter_t *ctx, ammeter_cfg_t *cfg)
Initialization function.
ammeter_cfg_t::sck
pin_name_t sck
Definition: ammeter.h:123
ammeter_t::chip_select
pin_name_t chip_select
Definition: ammeter.h:110
ammeter_cfg_t::miso
pin_name_t miso
Definition: ammeter.h:121
ammeter_t::cs
digital_out_t cs
Definition: ammeter.h:105
ammeter_cfg_t::spi_speed
uint32_t spi_speed
Definition: ammeter.h:128
ammeter_amperage
float ammeter_amperage(ammeter_t *ctx)
Get amperage function.