hbridgedriver2 2.0.0.0
hbridgedriver2.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef HBRIDGEDRIVER2_H
29#define HBRIDGEDRIVER2_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53#include "drv_pwm.h"
54
55
76#define HBRIDGEDRIVER2_CMD_REG_CTL_0 0x00
77#define HBRIDGEDRIVER2_CMD_REG_STAT_0 0x10
78#define HBRIDGEDRIVER2_CMD_REG_CFG 0x3F
79#define HBRIDGEDRIVER2_ID_REG_ID_HDR 0x00
80#define HBRIDGEDRIVER2_ID_REG_VERSION 0x01
81#define HBRIDGEDRIVER2_ID_REG_P_CODE_1 0x02
82#define HBRIDGEDRIVER2_ID_REG_P_CODE_2 0x03
83#define HBRIDGEDRIVER2_ID_REG_SPI_F_ID 0x3E
84
85 // hbridgedriver2_reg
86
101#define HBRIDGEDRIVER2_GLB_FLT 0x80
102#define HBRIDGEDRIVER2_GLB_TF 0x40
103#define HBRIDGEDRIVER2_GLB_RESB_NORMAL 0x20
104#define HBRIDGEDRIVER2_GLB_TSD 0x10
105#define HBRIDGEDRIVER2_GLB_UOV_OC 0x04
106#define HBRIDGEDRIVER2_GLB_NRDY 0x01
107
108 // global_fault
109
119#define HBRIDGEDRIVER2_DEF_FREQ 20000
120
125#define HBRIDGEDRIVER2_ID_VAL_ID_HDR 0x4300
126#define HBRIDGEDRIVER2_ID_VAL_VERSION 0x0100
127#define HBRIDGEDRIVER2_ID_VAL_P_CODE_1 0x7500
128#define HBRIDGEDRIVER2_ID_VAL_P_CODE_2 0x3500
129#define HBRIDGEDRIVER2_ID_VAL_SPI_F_ID 0x0200
130
135#define HBRIDGEDRIVER2_CTL_0_HS1 0x0200
136#define HBRIDGEDRIVER2_CTL_0_LS1 0x0100
137#define HBRIDGEDRIVER2_CTL_0_HS2 0x0080
138#define HBRIDGEDRIVER2_CTL_0_LS2 0x0040
139#define HBRIDGEDRIVER2_CTL_0_FWH 0x0010
140#define HBRIDGEDRIVER2_CTL_0_FWA 0x0008
141#define HBRIDGEDRIVER2_CTL_0_OVR 0x0004
142#define HBRIDGEDRIVER2_CTL_0_UVR 0x0002
143#define HBRIDGEDRIVER2_CTL_0_MODE 0x0001
144
149#define HBRIDGEDRIVER2_STAT_0_OCHS1 0x0200
150#define HBRIDGEDRIVER2_STAT_0_OCLS1 0x0100
151#define HBRIDGEDRIVER2_STAT_0_OCHS2 0x0080
152#define HBRIDGEDRIVER2_STAT_0_OCLS2 0x0040
153#define HBRIDGEDRIVER2_STAT_0_VSUV 0x0008
154#define HBRIDGEDRIVER2_STAT_0_VSOV 0x0004
155#define HBRIDGEDRIVER2_STAT_0_OK 0x0000
156
161#define HBRIDGEDRIVER2_CFG_NOCRLH_250_NS 0x0000
162#define HBRIDGEDRIVER2_CFG_NOCRLH_500_NS 0x1000
163#define HBRIDGEDRIVER2_CFG_NOCRLH_750_NS 0x2000
164#define HBRIDGEDRIVER2_CFG_NOCRLH_1_US 0x3000
165#define HBRIDGEDRIVER2_CFG_NOCRLH_1_25_US 0x4000
166#define HBRIDGEDRIVER2_CFG_NOCRLH_1_5_US 0x5000
167#define HBRIDGEDRIVER2_CFG_NOCRLH_1_75_US 0x6000
168#define HBRIDGEDRIVER2_CFG_NOCRLH_2_US 0x7000
169#define HBRIDGEDRIVER2_CFG_NOCRLH_2_25_US 0x8000
170#define HBRIDGEDRIVER2_CFG_NOCRLH_2_5_US 0x9000
171#define HBRIDGEDRIVER2_CFG_NOCRLH_2_75_US 0xA000
172#define HBRIDGEDRIVER2_CFG_NOCRLH_3_US 0xB000
173#define HBRIDGEDRIVER2_CFG_NOCRLH_3_25_US 0xC000
174#define HBRIDGEDRIVER2_CFG_NOCRLH_3_5_US 0xD000
175#define HBRIDGEDRIVER2_CFG_NOCRLH_3_75_US 0xE000
176#define HBRIDGEDRIVER2_CFG_NOCRLH_4_US 0xF000
177#define HBRIDGEDRIVER2_CFG_NOCRHL_250_NS 0x0000
178#define HBRIDGEDRIVER2_CFG_NOCRHL_500_NS 0x0100
179#define HBRIDGEDRIVER2_CFG_NOCRHL_750_NS 0x0200
180#define HBRIDGEDRIVER2_CFG_NOCRHL_1_US 0x0300
181#define HBRIDGEDRIVER2_CFG_NOCRHL_1_25_US 0x0400
182#define HBRIDGEDRIVER2_CFG_NOCRHL_1_5_US 0x0500
183#define HBRIDGEDRIVER2_CFG_NOCRHL_1_75_US 0x0600
184#define HBRIDGEDRIVER2_CFG_NOCRHL_2_US 0x0700
185#define HBRIDGEDRIVER2_CFG_NOCRHL_2_25_US 0x0800
186#define HBRIDGEDRIVER2_CFG_NOCRHL_2_5_US 0x0900
187#define HBRIDGEDRIVER2_CFG_NOCRHL_2_75_US 0x0A00
188#define HBRIDGEDRIVER2_CFG_NOCRHL_3_US 0x0B00
189#define HBRIDGEDRIVER2_CFG_NOCRHL_3_25_US 0x0C00
190#define HBRIDGEDRIVER2_CFG_NOCRHL_3_5_US 0x0D00
191#define HBRIDGEDRIVER2_CFG_NOCRHL_3_75_US 0x0E00
192#define HBRIDGEDRIVER2_CFG_NOCRHL_4_US 0x0F00
193#define HBRIDGEDRIVER2_CFG_OCTH_0_25_V 0x0000
194#define HBRIDGEDRIVER2_CFG_OCTH_0_5_V 0x0020
195#define HBRIDGEDRIVER2_CFG_OCTH_0_75_V 0x0040
196#define HBRIDGEDRIVER2_CFG_OCTH_1_V 0x0060
197#define HBRIDGEDRIVER2_CFG_OCTH_1_25_V 0x0080
198#define HBRIDGEDRIVER2_CFG_OCTH_1_5_V 0x0090
199#define HBRIDGEDRIVER2_CFG_OCTH_1_75_V 0x00C0
200#define HBRIDGEDRIVER2_CFG_OCTH_2_V 0x00E0
201#define HBRIDGEDRIVER2_CFG_FULL_H_BRIDGE 0x0000
202#define HBRIDGEDRIVER2_CFG_2_HALF_BRIDGES 0x0010
203#define HBRIDGEDRIVER2_CFG_SLOW_SLEW_RATE 0x0000
204#define HBRIDGEDRIVER2_CFG_FAST_SLEW_RATE 0x0008
205
210#define HBRIDGEDRIVER2_CLEAR 0x00
211#define HBRIDGEDRIVER2_SET 0x01
212
221#define HBRIDGEDRIVER2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
222#define HBRIDGEDRIVER2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
223
224 // hbridgedriver2_set
225
240#define HBRIDGEDRIVER2_MAP_MIKROBUS( cfg, mikrobus ) \
241 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
242 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
243 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
244 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
245 cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
246 cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM )
247
248 // hbridgedriver2_map
249 // hbridgedriver2
250
255typedef struct
256{
257 // Output pins
258 digital_out_t en;
260 // Modules
261 spi_master_t spi;
263 pin_name_t chip_select;
265 pwm_t pwm;
267 uint32_t pwm_freq;
270
275typedef struct
276{
277 // Communication gpio pins
278 pin_name_t miso;
279 pin_name_t mosi;
280 pin_name_t sck;
281 pin_name_t cs;
283 // Additional gpio pins
284 pin_name_t en;
286 // static variable
287 uint32_t spi_speed;
288 spi_master_mode_t spi_mode;
289 spi_master_chip_select_polarity_t cs_polarity;
291 pin_name_t pwm;
293 uint32_t dev_pwm_freq;
296
301typedef struct
302{
303 uint16_t id_header;
304 uint16_t version;
305 uint16_t product_code_1;
306 uint16_t product_code_2;
307 uint16_t spi_frame_id;
310
315typedef struct
316{
317 uint8_t hs_1; /* HS 1 Output Control */
318 uint8_t ls_1; /* LS 1 Output Control */
319 uint8_t hs_2; /* HS 2 Output Control */
320 uint8_t ls_2; /* LS 2 Output Control */
321
322 uint8_t fw_h; /* Freewheeling High side or low side */
323 uint8_t fw_a; /* Freewheeling Active or passive */
324
325 uint8_t ovr; /* Over−voltage Recovery */
326 uint8_t uvr; /* Under−voltage Recovery */
327
328 uint8_t mode; /* Mode Control */
329
331
342
359
375
390
405
420
436err_t hbridgedriver2_write_reg ( hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t data_in );
437
453err_t hbridgedriver2_read_reg ( hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t *data_out );
454
470err_t hbridgedriver2_read_clear_reg ( hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t *data_out );
471
487err_t hbridgedriver2_read_id ( hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t *data_out );
488
503
518err_t hbridgedriver2_get_status ( hbridgedriver2_t *ctx, uint16_t *status );
519
534err_t hbridgedriver2_get_control ( hbridgedriver2_t *ctx, uint16_t *control );
535
551err_t hbridgedriver2_set_control ( hbridgedriver2_t *ctx, uint8_t *global_fault, uint16_t control );
552
567err_t hbridgedriver2_get_config ( hbridgedriver2_t *ctx, uint16_t *config );
568
584err_t hbridgedriver2_set_config ( hbridgedriver2_t *ctx, uint8_t *global_fault, uint16_t config );
585
601err_t hbridgedriver2_get_device_id ( hbridgedriver2_t *ctx, uint8_t *global_fault, hbridgedriver2_dev_id_t *dev_id );
602
618err_t hbridgedriver2_control ( hbridgedriver2_t *ctx, uint8_t *global_fault, hbridgedriver2_ctrl_t dev_ctrl );
619
634err_t hbridgedriver2_run_backward ( hbridgedriver2_t *ctx, uint8_t *global_fault );
635
650err_t hbridgedriver2_run_forward ( hbridgedriver2_t *ctx, uint8_t *global_fault );
651
666err_t hbridgedriver2_stop ( hbridgedriver2_t *ctx, uint8_t *global_fault );
667
682err_t hbridgedriver2_stop_with_brake ( hbridgedriver2_t *ctx, uint8_t *global_fault );
683
696err_t hbridgedriver2_set_duty_cycle ( hbridgedriver2_t *ctx, float duty_cycle );
697
710
723
724#ifdef __cplusplus
725}
726#endif
727#endif // HBRIDGEDRIVER2_H
728
729 // hbridgedriver2
730
731// ------------------------------------------------------------------------ END
err_t hbridgedriver2_get_status(hbridgedriver2_t *ctx, uint16_t *status)
H-Bridge Driver 2 get status function.
err_t hbridgedriver2_init(hbridgedriver2_t *ctx, hbridgedriver2_cfg_t *cfg)
H-Bridge Driver 2 initialization function.
err_t hbridgedriver2_run_backward(hbridgedriver2_t *ctx, uint8_t *global_fault)
H-Bridge Driver 2 run backward function.
err_t hbridgedriver2_run_forward(hbridgedriver2_t *ctx, uint8_t *global_fault)
H-Bridge Driver 2 run forward function.
err_t hbridgedriver2_disable(hbridgedriver2_t *ctx)
H-Bridge Driver 2 disable function.
err_t hbridgedriver2_pwm_start(hbridgedriver2_t *ctx)
H-Bridge Driver 2 start PWM module.
err_t hbridgedriver2_stop(hbridgedriver2_t *ctx, uint8_t *global_fault)
H-Bridge Driver 2 stop function.
err_t hbridgedriver2_control(hbridgedriver2_t *ctx, uint8_t *global_fault, hbridgedriver2_ctrl_t dev_ctrl)
H-Bridge Driver 2 control function.
err_t hbridgedriver2_read_reg(hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t *data_out)
H-Bridge Driver 2 data reading function.
err_t hbridgedriver2_set_control(hbridgedriver2_t *ctx, uint8_t *global_fault, uint16_t control)
H-Bridge Driver 2 set control function.
err_t hbridgedriver2_pwm_stop(hbridgedriver2_t *ctx)
H-Bridge Driver 2 stop PWM module.
err_t hbridgedriver2_default_cfg(hbridgedriver2_t *ctx)
H-Bridge Driver 2 default configuration function.
err_t hbridgedriver2_read_id(hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t *data_out)
H-Bridge Driver 2 data read ID function.
err_t hbridgedriver2_set_duty_cycle(hbridgedriver2_t *ctx, float duty_cycle)
H-Bridge Driver 2 sets PWM duty cycle.
err_t hbridgedriver2_get_device_id(hbridgedriver2_t *ctx, uint8_t *global_fault, hbridgedriver2_dev_id_t *dev_id)
H-Bridge Driver 2 get device ID function.
err_t hbridgedriver2_get_config(hbridgedriver2_t *ctx, uint16_t *config)
H-Bridge Driver 2 get config function.
err_t hbridgedriver2_set_config(hbridgedriver2_t *ctx, uint8_t *global_fault, uint16_t config)
H-Bridge Driver 2 set config function.
err_t hbridgedriver2_write_reg(hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t data_in)
H-Bridge Driver 2 data writing function.
void hbridgedriver2_cfg_setup(hbridgedriver2_cfg_t *cfg)
H-Bridge Driver 2 configuration object setup function.
err_t hbridgedriver2_set_active_mode(hbridgedriver2_t *ctx)
H-Bridge Driver 2 set active mode function.
err_t hbridgedriver2_get_control(hbridgedriver2_t *ctx, uint16_t *control)
H-Bridge Driver 2 get control function.
err_t hbridgedriver2_stop_with_brake(hbridgedriver2_t *ctx, uint8_t *global_fault)
H-Bridge Driver 2 stop with brake function.
err_t hbridgedriver2_enable(hbridgedriver2_t *ctx)
H-Bridge Driver 2 enable function.
err_t hbridgedriver2_read_clear_reg(hbridgedriver2_t *ctx, uint8_t reg, uint8_t *global_fault, uint16_t *data_out)
H-Bridge Driver 2 data read and clear function.
hbridgedriver2_return_value_t
H-Bridge Driver 2 Click return value data.
Definition hbridgedriver2.h:337
@ HBRIDGEDRIVER2_OK
Definition hbridgedriver2.h:338
@ HBRIDGEDRIVER2_ERROR
Definition hbridgedriver2.h:339
This file contains SPI specific macros, functions, etc.
H-Bridge Driver 2 Click configuration object.
Definition hbridgedriver2.h:276
spi_master_chip_select_polarity_t cs_polarity
Definition hbridgedriver2.h:289
pin_name_t sck
Definition hbridgedriver2.h:280
spi_master_mode_t spi_mode
Definition hbridgedriver2.h:288
pin_name_t mosi
Definition hbridgedriver2.h:279
uint32_t spi_speed
Definition hbridgedriver2.h:287
uint32_t dev_pwm_freq
Definition hbridgedriver2.h:293
pin_name_t en
Definition hbridgedriver2.h:284
pin_name_t pwm
Definition hbridgedriver2.h:291
pin_name_t miso
Definition hbridgedriver2.h:278
pin_name_t cs
Definition hbridgedriver2.h:281
H-Bridge Driver 2 Click device ID object.
Definition hbridgedriver2.h:316
uint8_t fw_h
Definition hbridgedriver2.h:322
uint8_t mode
Definition hbridgedriver2.h:328
uint8_t ls_1
Definition hbridgedriver2.h:318
uint8_t fw_a
Definition hbridgedriver2.h:323
uint8_t hs_2
Definition hbridgedriver2.h:319
uint8_t hs_1
Definition hbridgedriver2.h:317
uint8_t uvr
Definition hbridgedriver2.h:326
uint8_t ls_2
Definition hbridgedriver2.h:320
uint8_t ovr
Definition hbridgedriver2.h:325
H-Bridge Driver 2 Click device ID object.
Definition hbridgedriver2.h:302
uint16_t product_code_2
Definition hbridgedriver2.h:306
uint16_t version
Definition hbridgedriver2.h:304
uint16_t spi_frame_id
Definition hbridgedriver2.h:307
uint16_t id_header
Definition hbridgedriver2.h:303
uint16_t product_code_1
Definition hbridgedriver2.h:305
H-Bridge Driver 2 Click context object.
Definition hbridgedriver2.h:256
spi_master_t spi
Definition hbridgedriver2.h:261
uint32_t pwm_freq
Definition hbridgedriver2.h:267
digital_out_t en
Definition hbridgedriver2.h:258
pin_name_t chip_select
Definition hbridgedriver2.h:263
pwm_t pwm
Definition hbridgedriver2.h:265