accurrent  2.0.0.0
accurrent.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 ACCURRENT_H
36 #define ACCURRENT_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_spi_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
53 #define ACCURRENT_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
55  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
56  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.an = MIKROBUS( mikrobus, MIKROBUS_AN )
59 
65 #define ACCURRENT_OK 0
66 #define ACCURRENT_ERROR -1
67 
73 #define ACCURRENT_ADC_ITERATIONS 10
74 #define ACCURRENT_ADC_RESOLUTION 0x0FFF
75 #define ACCURRENT_AC_TO_DC_V_SCALE 1.8f
76 #define ACCURRENT_SENSOR_RESOLUTION 30.0f
77 #define ACCURRENT_A_TO_MA_SCALE 1000.0f
78  // End group macro
82 // --------------------------------------------------------------- PUBLIC TYPES
91 typedef struct
92 {
93  digital_out_t cs;
94 
95  // Input pins
96  digital_in_t an;
97 
98  // Modules
99  spi_master_t spi;
100  pin_name_t chip_select;
101 
102 } accurrent_t;
103 
107 typedef struct
108 {
109  // Communication gpio pins
110  pin_name_t miso;
111  pin_name_t mosi;
112  pin_name_t sck;
113  pin_name_t cs;
114 
115  // Additional gpio pins
116  pin_name_t an;
117 
118  // static variable
119  uint32_t spi_speed;
120  spi_master_mode_t spi_mode;
121  spi_master_chip_select_polarity_t cs_polarity;
122 
124  // End types group
126 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
127 
132 #ifdef __cplusplus
133 extern "C"{
134 #endif
135 
145 
155 
167 void accurrent_generic_transfer ( accurrent_t *ctx, uint8_t *wr_buf, uint16_t wr_len,
168  uint8_t *rd_buf, uint16_t rd_len );
169 
178 
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 #endif // _ACCURRENT_H_
192  // End public_function group
195 
196 // ------------------------------------------------------------------------- END
accurrent_cfg_t
Click configuration structure definition.
Definition: accurrent.h:108
accurrent_t::chip_select
pin_name_t chip_select
Definition: accurrent.h:100
accurrent_init
err_t accurrent_init(accurrent_t *ctx, accurrent_cfg_t *cfg)
Initialization function.
accurrent_t
Click ctx object definition.
Definition: accurrent.h:92
accurrent_cfg_t::cs
pin_name_t cs
Definition: accurrent.h:113
accurrent_cfg_setup
void accurrent_cfg_setup(accurrent_cfg_t *cfg)
Config Object Initialization function.
accurrent_t::an
digital_in_t an
Definition: accurrent.h:96
accurrent_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: accurrent.h:120
accurrent_t::cs
digital_out_t cs
Definition: accurrent.h:93
accurrent_get_a
float accurrent_get_a(accurrent_t *ctx)
Gets current value of AC Current in A.
accurrent_get_ma
float accurrent_get_ma(accurrent_t *ctx)
Gets current value of AC Current in mA.
accurrent_cfg_t::spi_speed
uint32_t spi_speed
Definition: accurrent.h:119
accurrent_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: accurrent.h:121
accurrent_cfg_t::mosi
pin_name_t mosi
Definition: accurrent.h:111
accurrent_generic_transfer
void accurrent_generic_transfer(accurrent_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
accurrent_cfg_t::an
pin_name_t an
Definition: accurrent.h:116
accurrent_t::spi
spi_master_t spi
Definition: accurrent.h:99
accurrent_cfg_t::miso
pin_name_t miso
Definition: accurrent.h:110
accurrent_cfg_t::sck
pin_name_t sck
Definition: accurrent.h:112