smartbuck 2.0.0.0
smartbuck.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 SMARTBUCK_H
36#define SMARTBUCK_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_i2c_master.h"
55
56// -------------------------------------------------------------- PUBLIC MACROS
66#define SMARTBUCK_MAP_MIKROBUS( cfg, mikrobus ) \
67 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
68 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
69 cfg.en1 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70 cfg.en2 = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71 cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
72 cfg.pg = MIKROBUS( mikrobus, MIKROBUS_INT )
79#define SMARTBUCK_RETVAL uint8_t
80#define SMARTBUCK_OK 0x00
81#define SMARTBUCK_INIT_ERROR 0xFF
87#define SMARTBUCK_EEPROM_SLAVE_000 0x50
88#define SMARTBUCK_EEPROM_SLAVE_001 0x51
89#define SMARTBUCK_EEPROM_SLAVE_010 0x52
90#define SMARTBUCK_EEPROM_SLAVE_011 0x53
91#define SMARTBUCK_EEPROM_SLAVE_100 0x54
92#define SMARTBUCK_EEPROM_SLAVE_101 0x55
93#define SMARTBUCK_EEPROM_SLAVE_110 0x56
94#define SMARTBUCK_EEPROM_SLAVE_111 0x57
95#define SMARTBUCK_MCP4661_SLAVE_000 0x28
96#define SMARTBUCK_MCP4661_SLAVE_001 0x29
97#define SMARTBUCK_MCP4661_SLAVE_010 0x2A
98#define SMARTBUCK_MCP4661_SLAVE_011 0x2B
99#define SMARTBUCK_MCP4661_SLAVE_100 0x2C
100#define SMARTBUCK_MCP4661_SLAVE_101 0x2D
101#define SMARTBUCK_MCP4661_SLAVE_110 0x2E
102#define SMARTBUCK_MCP4661_SLAVE_111 0x2F
103#define SMARTBUCK_PAC1934_SLAVE_0 0x10
104#define SMARTBUCK_PAC1934_SLAVE_1 0x1F
110#define SMARTBUCK_WIPER0_VOL_REG 0x00
111#define SMARTBUCK_WIPER1_VOL_REG 0x01
112#define SMARTBUCK_WIPER0_NONVOL_REG 0x02
113#define SMARTBUCK_WIPER1_NONVOL_REG 0x03
114#define SMARTBUCK_POT_TCON_VOL_REG 0x04
115#define SMARTBUCK_POT_STATUS_REG 0x05
121#define SMARTBUCK_WIPER_FULL_SCALE 0x100
122#define SMARTBUCK_WIPER_HALF_SCALE 0x080
123#define SMARTBUCK_WIPER_QUARTER_SCALE 0x040
124#define SMARTBUCK_WIPER_FULL_PER_8_SCALE 0x020
125#define SMARTBUCK_WIPER_ZERO_SCALE 0x000
131#define SMARTBUCK_TCON_GENCALL_EN 0x100
132#define SMARTBUCK_TCON_R1HW_SHDWN_EN 0x080
133#define SMARTBUCK_TCON_R1A_CONNECT 0x040
134#define SMARTBUCK_TCON_R1W_CONNECT 0x020
135#define SMARTBUCK_TCON_R1B_CONNECT 0x010
136#define SMARTBUCK_TCON_R0HW_SHDWN_EN 0x008
137#define SMARTBUCK_TCON_R0A_CONNECT 0x004
138#define SMARTBUCK_TCON_R0W_CONNECT 0x002
139#define SMARTBUCK_TCON_R0B_CONNECT 0x001
145#define SMARTBUCK_REFRESH_CMND 0x00
146#define SMARTBUCK_PAC_CTRL_REG 0x01
147#define SMARTBUCK_PAC_ACC_COUNT_REG 0x02
148#define SMARTBUCK_PAC_VPOW1_ACC_REG 0x03
149#define SMARTBUCK_PAC_VPOW2_ACC_REG 0x04
150#define SMARTBUCK_PAC_VPOW3_ACC_REG 0x05
151#define SMARTBUCK_PAC_VPOW4_ACC_REG 0x06
152#define SMARTBUCK_PAC_VBUS1_REG 0x07
153#define SMARTBUCK_PAC_VBUS2_REG 0x08
154#define SMARTBUCK_PAC_VBUS3_REG 0x09
155#define SMARTBUCK_PAC_VBUS4_REG 0x0A
156#define SMARTBUCK_PAC_VSENSE1_REG 0x0B
157#define SMARTBUCK_PAC_VSENSE2_REG 0x0C
158#define SMARTBUCK_PAC_VSENSE3_REG 0x0D
159#define SMARTBUCK_PAC_VSENSE4_REG 0x0E
160#define SMARTBUCK_PAC_VBUS1_AVG_REG 0x0F
161#define SMARTBUCK_PAC_VBUS2_AVG_REG 0x10
162#define SMARTBUCK_PAC_VBUS3_AVG_REG 0x11
163#define SMARTBUCK_PAC_VBUS4_AVG_REG 0x12
164#define SMARTBUCK_PAC_VSENSE1_AVG_REG 0x13
165#define SMARTBUCK_PAC_VSENSE2_AVG_REG 0x14
166#define SMARTBUCK_PAC_VSENSE3_AVG_REG 0x15
167#define SMARTBUCK_PAC_VSENSE4_AVG_REG 0x16
168#define SMARTBUCK_PAC_VPOW1_REG 0x17
169#define SMARTBUCK_PAC_VPOW2_REG 0x18
170#define SMARTBUCK_PAC_VPOW3_REG 0x19
171#define SMARTBUCK_PAC_VPOW4_REG 0x1A
172#define SMARTBUCK_PAC_CHANN_DIS_REG 0x1C
173#define SMARTBUCK_PAC_NEG_PWR_REG 0x1D
174#define SMARTBUCK_REFRESH_G_CMND 0x1E
175#define SMARTBUCK_REFRESH_V_CMND 0x1F
176#define SMARTBUCK_PAC_SLOW_REG 0x20
177#define SMARTBUCK_PAC_CTRL_STAT_REG 0x21
178#define SMARTBUCK_PAC_CHANN_DIS_STAT_REG 0x22
179#define SMARTBUCK_PAC_NEG_PWR_STAT_REG 0x23
180#define SMARTBUCK_PAC_CTRL_LATCH_REG 0x24
181#define SMARTBUCK_PAC_CHANN_DIS_LATCH_REG 0x25
182#define SMARTBUCK_PAC_NEG_PWR_LATCH_REG 0x26
183#define SMARTBUCK_PAC_PID_REG 0xFD
184#define SMARTBUCK_PAC_MANU_ID_REG 0xFE
185#define SMARTBUCK_PAC_REV_ID_REG 0xFF
191#define SMARTBUCK_PAC_1024_SAMPL 0x00
192#define SMARTBUCK_PAC_256_SAMPL 0x40
193#define SMARTBUCK_PAC_64_SAMPL 0x80
194#define SMARTBUCK_PAC_8_SAMPL 0xC0
195#define SMARTBUCK_PAC_SLEEP_MODE 0x20
196#define SMARTBUCK_PAC_SEQUENTIAL_MODE 0x00
197#define SMARTBUCK_PAC_SINGLE_SHOT_MODE 0x10
198#define SMARTBUCK_PAC_ALERT_PIN_EN 0x0C
199#define SMARTBUCK_PAC_OVF_ALERT_EN 0x02
205#define SMARTBUCK_WIPER0_SEL 0x00
206#define SMARTBUCK_WIPER1_SEL 0x01
207#define SMARTBUCK_REGULATOR_DIS 0x00
208#define SMARTBUCK_REGULATOR_EN 0x01
209#define SMARTBUCK_AUTO_TRICKLE_MODE 0x01
210#define SMARTBUCK_FORCE_PWM_MODE 0x00
211#define SMARTBUCK_ALL_CHANN_OFF 0xF0
212#define SMARTBUCK_ALL_CHANN_ON 0x00
213#define SMARTBUCK_CH1_ON 0x70
214#define SMARTBUCK_CH2_ON 0xB0
215#define SMARTBUCK_CH3_ON 0xD0
216#define SMARTBUCK_CH4_ON 0xE0
217#define SMARTBUCK_ALL_DATA_UNSIGN_FORM 0x00
218#define SMARTBUCK_ALL_DATA_SIGN_FORM 0xFF
219#define SMARTBUCK_CH1_VSENSE_SIGN_FORM 0x80
220#define SMARTBUCK_CH2_VSENSE_SIGN_FORM 0x40
221#define SMARTBUCK_CH3_VSENSE_SIGN_FORM 0x20
222#define SMARTBUCK_CH4_VSENSE_SIGN_FORM 0x10
223#define SMARTBUCK_CH1_VBUS_SIGN_FORM 0x08
224#define SMARTBUCK_CH2_VBUS_SIGN_FORM 0x04
225#define SMARTBUCK_CH3_VBUS_SIGN_FORM 0x02
226#define SMARTBUCK_CH4_VBUS_SIGN_FORM 0x01
227#define SMARTBUCK_SAMPL_DATA_MODE 0x00
228#define SMARTBUCK_AVRG_DATA_MODE 0x01
234#define SMARTBUCK_POT_WRITE_CMND 0x00
235#define SMARTBUCK_POT_INCR_CMND 0x04
236#define SMARTBUCK_POT_DECR_CMND 0x08
237#define SMARTBUCK_POT_READ_CMND 0x0C
238#define SMARTBUCK_PAC_GENERAL_CALL_SLAVE 0x00
239 // End group macro
240// --------------------------------------------------------------- PUBLIC TYPES
247extern uint8_t enabled_chann;
248
252typedef struct
253{
254 // Output pins
255
256 digital_out_t en1;
257 digital_out_t en2;
258 digital_out_t pwm;
259
260 // Input pins
261
262 digital_in_t pg;
263
264 // Modules
265
266 i2c_master_t i2c;
267
268 // ctx variable
269
273
275 uint8_t data_form;
276 uint8_t data_select;
277
279
283typedef struct
284{
285 // Communication gpio pins
286
287 pin_name_t scl;
288 pin_name_t sda;
289
290 // Additional gpio pins
291
292 pin_name_t en1;
293 pin_name_t en2;
294 pin_name_t pwm;
295 pin_name_t pg;
296
297 // static variable
298
299 uint32_t i2c_speed;
303
305
306#ifdef __cplusplus
307extern "C"{
308#endif
309
319
329
338
350uint8_t smartbuck_write_eeprom( smartbuck_t *ctx, uint8_t eeprom_addr, uint8_t data_in );
351
363uint8_t smartbuck_read_eeprom( smartbuck_t *ctx, uint8_t eeprom_addr, uint8_t *data_out );
364
377uint8_t smartbuck_read_seq_eeprom( smartbuck_t *ctx, uint8_t eeprom_addr, uint8_t *data_out , uint8_t n_bytes);
378
390uint8_t smartbuck_write_pot( smartbuck_t *ctx, uint8_t reg_addr, uint16_t data_in );
391
403uint8_t smartbuck_read_pot( smartbuck_t *ctx, uint8_t reg_addr, uint16_t *data_out );
404
416uint8_t smartbuck_incr_pot( smartbuck_t *ctx, uint8_t wiper_sel, uint16_t steps );
417
429uint8_t smartbuck_decr_pot( smartbuck_t *ctx, uint8_t wiper_sel, uint16_t steps );
430
439void smartbuck_en_regulator1 ( smartbuck_t *ctx, uint8_t state );
440
449void smartbuck_en_regulator2 ( smartbuck_t *ctx, uint8_t state );
450
459void smartbuck_force_pwm_mode ( smartbuck_t *ctx, uint8_t state );
460
469
480uint8_t smartbuck_send_cmd_pac ( smartbuck_t *ctx, uint8_t command );
481
493uint8_t smartbuck_write_pac ( smartbuck_t *ctx, uint8_t reg_addr, uint8_t data_in );
494
506uint8_t smartbuck_read_pac ( smartbuck_t *ctx, uint8_t reg_addr, uint8_t *data_out );
507
518uint8_t smartbuck_set_addr_ptr_pac ( smartbuck_t *ctx, uint8_t reg_addr );
519
528void smartbuck_receive_byte_pac ( smartbuck_t *ctx, uint8_t *data_out );
529
542uint8_t smartbuck_block_read_pac( smartbuck_t *ctx, uint8_t reg_addr, uint8_t *data_out, uint8_t n_bytes );
543
560uint8_t smartbuck_set_config_pac ( smartbuck_t *ctx, uint8_t en_chann, uint8_t data_format, uint8_t data_sel );
561
574void smartbuck_get_data ( smartbuck_t *ctx, float *voltage, float *current, float *power );
575
576#ifdef __cplusplus
577}
578#endif
579#endif // _SMARTBUCK_H_
580
581 // End public_function group
583
584// ------------------------------------------------------------------------- END
#define SMARTBUCK_RETVAL
Definition smartbuck.h:79
void smartbuck_en_regulator1(smartbuck_t *ctx, uint8_t state)
Regulator1 Enable function.
uint8_t enabled_chann
uint8_t smartbuck_read_pot(smartbuck_t *ctx, uint8_t reg_addr, uint16_t *data_out)
Potentiometer Read function.
void smartbuck_default_cfg(smartbuck_t *ctx)
Click Default Configuration function.
void smartbuck_receive_byte_pac(smartbuck_t *ctx, uint8_t *data_out)
Byte Receive function.
uint8_t smartbuck_read_seq_eeprom(smartbuck_t *ctx, uint8_t eeprom_addr, uint8_t *data_out, uint8_t n_bytes)
Sequential EEPROM Read function.
void smartbuck_cfg_setup(smartbuck_cfg_t *cfg)
Config Object Initialization function.
uint8_t smartbuck_write_eeprom(smartbuck_t *ctx, uint8_t eeprom_addr, uint8_t data_in)
EEPROM Write function.
uint8_t smartbuck_decr_pot(smartbuck_t *ctx, uint8_t wiper_sel, uint16_t steps)
Potentiometer Decrement function.
uint8_t smartbuck_check_diagnostic(smartbuck_t *ctx)
Diagnostic Check function.
uint8_t smartbuck_incr_pot(smartbuck_t *ctx, uint8_t wiper_sel, uint16_t steps)
Potentiometer Increment function.
uint8_t smartbuck_write_pot(smartbuck_t *ctx, uint8_t reg_addr, uint16_t data_in)
Potentiometer Write function.
uint8_t smartbuck_block_read_pac(smartbuck_t *ctx, uint8_t reg_addr, uint8_t *data_out, uint8_t n_bytes)
PAC1934 Block Read function.
uint8_t smartbuck_read_eeprom(smartbuck_t *ctx, uint8_t eeprom_addr, uint8_t *data_out)
EEPROM Read function.
uint8_t smartbuck_read_pac(smartbuck_t *ctx, uint8_t reg_addr, uint8_t *data_out)
PAC1934 Read function.
uint8_t smartbuck_send_cmd_pac(smartbuck_t *ctx, uint8_t command)
Command Send function.
uint8_t smartbuck_set_config_pac(smartbuck_t *ctx, uint8_t en_chann, uint8_t data_format, uint8_t data_sel)
PAC1934 Configuration Set function.
uint8_t smartbuck_set_addr_ptr_pac(smartbuck_t *ctx, uint8_t reg_addr)
Address Pointer Set function.
void smartbuck_en_regulator2(smartbuck_t *ctx, uint8_t state)
Regulator2 Enable function.
SMARTBUCK_RETVAL smartbuck_init(smartbuck_t *ctx, smartbuck_cfg_t *cfg)
Initialization function.
void smartbuck_get_data(smartbuck_t *ctx, float *voltage, float *current, float *power)
Data Get function.
void smartbuck_force_pwm_mode(smartbuck_t *ctx, uint8_t state)
PWM Mode Force function.
uint8_t smartbuck_write_pac(smartbuck_t *ctx, uint8_t reg_addr, uint8_t data_in)
PAC1934 Write function.
Click configuration structure definition.
Definition smartbuck.h:284
pin_name_t en2
Definition smartbuck.h:293
uint32_t i2c_speed
Definition smartbuck.h:299
uint8_t i2c_address_pac1934
Definition smartbuck.h:302
pin_name_t en1
Definition smartbuck.h:292
uint8_t i2c_address_eeprom
Definition smartbuck.h:300
pin_name_t scl
Definition smartbuck.h:287
pin_name_t pwm
Definition smartbuck.h:294
uint8_t i2c_address_mcp4661
Definition smartbuck.h:301
pin_name_t sda
Definition smartbuck.h:288
pin_name_t pg
Definition smartbuck.h:295
Click ctx object definition.
Definition smartbuck.h:253
uint8_t enabled_chann
Definition smartbuck.h:274
digital_out_t en2
Definition smartbuck.h:257
digital_out_t en1
Definition smartbuck.h:256
uint8_t slave_eeprom
Definition smartbuck.h:270
digital_out_t pwm
Definition smartbuck.h:258
i2c_master_t i2c
Definition smartbuck.h:266
uint8_t data_select
Definition smartbuck.h:276
uint8_t slave_pac1934
Definition smartbuck.h:272
uint8_t slave_mcp4661
Definition smartbuck.h:271
digital_in_t pg
Definition smartbuck.h:262
uint8_t data_form
Definition smartbuck.h:275