balancer2 2.0.0.0
balancer2.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 BALANCER2_H
36#define BALANCER2_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// -------------------------------------------------------------- PUBLIC MACROS
67#define BALANCER2_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
71 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
72 cfg.cell2 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
73 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_RST ); \
74 cfg.cell1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
75 cfg.stat = MIKROBUS( mikrobus, MIKROBUS_INT )
82#define BALANCER2_RETVAL uint8_t
83
84#define BALANCER2_OK 0x00
85#define BALANCER2_INIT_ERROR 0xFF
92#define BALANCER2_VREF_5V 5000
93#define BALANCER2_VREF_3V3 3300
100#define BALANCER2_CMD_START 0x01
101#define BALANCER2_CMD_MSBF_LSB 0x20
102#define BALANCER2_CMD_MSBF_MSB 0x00
103#define BALANCER2_CMD_SINGLE_MODE_CH0 0x80
104#define BALANCER2_CMD_SINGLE_MODE_CH1 0xC0
105#define BALANCER2_CMD_DIFF_MODE_CH0M_CH1P 0x40
106#define BALANCER2_CMD_DIFF_MODE_CH0P_CH1M 0x00
107#define BALANCER2_CMD_DUMMY 0x00
114#define BALANCER2_MASK 0x0F
121#define BALANCER2_BATT1 0xAA
122#define BALANCER2_BATT2 0xBB
123#define BALANCER2_BATT_BOTH 0xCC
130#define BALANCER2_PIN_STATE_HIGH 1
131#define BALANCER2_PIN_STATE_LOW 0
138#define BALANCER2_MODE_CHARGING 0x10
139#define BALANCER2_MODE_BALANCING_P 0x20
140#define BALANCER2_MODE_BALANCING_NP 0x30
141#define BALANCER2_MODE_BALANCING_PP 0x40
142#define BALANCER2_MODE_BALANCING_NPP 0x50
143#define BALANCER2_MODE_BALANCING_P1 0x60
144#define BALANCER2_MODE_BALANCING_NP1 0x70
145#define BALANCER2_MODE_BALANCING_P2 0x80
146#define BALANCER2_MODE_BALANCING_NP2 0x90
153#define CHN_0_MV_MULT 3.0
154#define CHN_1_MV_MULT 1.5
155#define ADC_RESOLUTION 4096.0
158 // End group macro
159// --------------------------------------------------------------- PUBLIC TYPES
168typedef struct
169{
170 // Output pins
171
172 digital_out_t cs;
173 digital_out_t cell2;
174 digital_out_t int_pin;
175 digital_out_t cell1;
176 digital_out_t stat;
177
178 // Modules
179
180 spi_master_t spi;
181 pin_name_t chip_select;
182
184
188typedef struct
189{
190 // Communication gpio pins
191
192 pin_name_t miso;
193 pin_name_t mosi;
194 pin_name_t sck;
195 pin_name_t cs;
196
197 // Additional gpio pins
198
199 pin_name_t cell2;
200 pin_name_t int_pin;
201 pin_name_t cell1;
202 pin_name_t stat;
203
204 // static variable
205
206 uint32_t spi_speed;
207 spi_master_mode_t spi_mode;
208 spi_master_chip_select_polarity_t cs_polarity;
209
211
212 // End types group
213// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
214
219#ifdef __cplusplus
220extern "C"{
221#endif
222
232
242
251
260void balancer2_cell_1_state ( balancer2_t *ctx, uint8_t state );
261
270void balancer2_cell_2_state ( balancer2_t *ctx, uint8_t state );
271
280void balancer2_supply_state ( balancer2_t *ctx, uint8_t state );
281
290void balancer2_status_led_state ( balancer2_t *ctx, uint8_t state );
291
302uint16_t balancer2_read_adc ( balancer2_t *ctx, uint8_t cmd_byte );
303
310void balancer2_set_vref ( balancer2_t *ctx, uint16_t vref );
311
322float balancer2_get_batttery_lvl ( balancer2_t *ctx, uint8_t bat_num );
323
334float balancer2_adc_to_mv ( balancer2_t *ctx, uint16_t adc_val );
335
336#ifdef __cplusplus
337}
338#endif
339#endif // _BALANCER2_H_
340
341 // End public_function group
343
344// ------------------------------------------------------------------------- END
#define BALANCER2_RETVAL
Definition balancer2.h:82
void balancer2_cfg_setup(balancer2_cfg_t *cfg)
Config Object Initialization function.
void balancer2_cell_2_state(balancer2_t *ctx, uint8_t state)
Generic function for setting cell 2 pin status.
void balancer2_status_led_state(balancer2_t *ctx, uint8_t state)
Generic function for setting status led pin status.
void balancer2_default_cfg(balancer2_t *ctx)
Click Default Configuration function.
float balancer2_get_batttery_lvl(balancer2_t *ctx, uint8_t bat_num)
Function for getting real battery lvl.
float balancer2_adc_to_mv(balancer2_t *ctx, uint16_t adc_val)
Function for converting adc value to mV.
void balancer2_supply_state(balancer2_t *ctx, uint8_t state)
Generic function for setting supply pin status.
void balancer2_cell_1_state(balancer2_t *ctx, uint8_t state)
Generic function for setting cell 1 pin status.
uint16_t balancer2_read_adc(balancer2_t *ctx, uint8_t cmd_byte)
Function for reading adc value.
BALANCER2_RETVAL balancer2_init(balancer2_t *ctx, balancer2_cfg_t *cfg)
Initialization function.
void balancer2_set_vref(balancer2_t *ctx, uint16_t vref)
Function for setting reference value.
Click configuration structure definition.
Definition balancer2.h:189
spi_master_chip_select_polarity_t cs_polarity
Definition balancer2.h:208
pin_name_t sck
Definition balancer2.h:194
pin_name_t cell1
Definition balancer2.h:201
spi_master_mode_t spi_mode
Definition balancer2.h:207
pin_name_t mosi
Definition balancer2.h:193
uint32_t spi_speed
Definition balancer2.h:206
pin_name_t int_pin
Definition balancer2.h:200
pin_name_t miso
Definition balancer2.h:192
pin_name_t stat
Definition balancer2.h:202
pin_name_t cell2
Definition balancer2.h:199
pin_name_t cs
Definition balancer2.h:195
Click ctx object definition.
Definition balancer2.h:169
digital_out_t cs
Definition balancer2.h:172
spi_master_t spi
Definition balancer2.h:180
digital_out_t stat
Definition balancer2.h:176
digital_out_t cell2
Definition balancer2.h:173
digital_out_t int_pin
Definition balancer2.h:174
pin_name_t chip_select
Definition balancer2.h:181
digital_out_t cell1
Definition balancer2.h:175