hbridge3  2.0.0.0
hbridge3.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 HBRIDGE3_H
36 #define HBRIDGE3_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_pwm.h"
41 #include "drv_spi_master.h"
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define HBRIDGE3_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
55  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
56  cfg.dis = MIKROBUS( mikrobus, MIKROBUS_RST ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.sdo = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
59  cfg.sdi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
60  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK )
61 
67 #define HBRIDGE3_RETVAL uint8_t
68 
69 #define HBRIDGE3_OK 0x00
70 #define HBRIDGE3_INIT_ERROR 0xFF
71 
77 #define HBRIDGE3_DEF_FREQ 20000
78 
84 #define HBRIDGE3_CMD_RD_DIA 0x00
85 #define HBRIDGE3_CMD_RES_DIA 0x80
86 #define HBRIDGE3_CMD_RD_REV 0x20
87 #define HBRIDGE3_CMD_RD_CTRL 0x60
88 #define HBRIDGE3_CMD_WR_CTRL 0xE0
89 #define HBRIDGE3_CMD_WR_CTRL_RD_DIA 0xC0
90 
96 #define HBRIDGE3_CTRL_VIA_PWM_DIR 0x00
97 #define HBRIDGE3_CTRL_VIA_SPI 0x80
98 #define HBRIDGE3_SPI_ENA_OUT 0x40
99 #define HBRIDGE3_SPI_DIS_OUT 0x00
100 #define HBRIDGE3_SPI_DIR_1 0x00
101 #define HBRIDGE3_SPI_DIR_2 0x20
102 #define HBRIDGE3_SPI_PWM_0 0x00
103 #define HBRIDGE3_SPI_PWM_1 0x01
104  // End group macro
106 // --------------------------------------------------------------- PUBLIC TYPES
115 typedef struct
116 {
117  // Output pins
118 
119  digital_out_t dir;
120  digital_out_t dis;
121  digital_out_t cs;
122 
123 
124  // Modules
125  spi_master_t spi;
126  pin_name_t chip_select;
127  pwm_t pwm;
128 
129  // ctx variable
130 
131  uint16_t pwm_period;
132  uint32_t pwm_freq;
133 
134 } hbridge3_t;
135 
139 typedef struct
140 {
141  // Communication gpio pins
142 
143  pin_name_t pwm;
144  pin_name_t sdo;
145  pin_name_t sdi;
146  pin_name_t sck;
147 
148  // Additional gpio pins
149 
150  pin_name_t dir;
151  pin_name_t dis;
152  pin_name_t cs;
153 
154  // static variable
155 
156  uint32_t dev_pwm_freq;
157  uint32_t spi_speed;
158  spi_master_mode_t spi_mode;
159  spi_master_chip_select_polarity_t cs_polarity;
160 
162  // End types group
164 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
165 
171 #ifdef __cplusplus
172 extern "C"{
173 #endif
174 
184 
194 
202 void hbridge3_default_cfg ( hbridge3_t *ctx, float duty_cycle );
203 
212 void hbridge3_set_duty_cycle ( hbridge3_t *ctx, float duty_cycle );
213 
222 
231 
240 void hbridge3_dir_set ( hbridge3_t *ctx, uint8_t pin_state );
241 
250 void hbridge3_dis_set ( hbridge3_t *ctx, uint8_t pin_state );
251 
260 void hbridge3_cs_set ( hbridge3_t *ctx, uint8_t pin_state );
261 
262 
273 uint8_t hbridge3_spi ( hbridge3_t *ctx, uint8_t spi_command );
274 
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 #endif // _HBRIDGE3_H_
280  // End public_function group
283 
284 // ------------------------------------------------------------------------- END
hbridge3_set_duty_cycle
void hbridge3_set_duty_cycle(hbridge3_t *ctx, float duty_cycle)
Generic sets PWM duty cycle.
hbridge3_cfg_setup
void hbridge3_cfg_setup(hbridge3_cfg_t *cfg)
Config Object Initialization function.
hbridge3_t::pwm_period
uint16_t pwm_period
Definition: hbridge3.h:131
hbridge3_pwm_start
void hbridge3_pwm_start(hbridge3_t *ctx)
Start PWM module.
hbridge3_cfg_t::sck
pin_name_t sck
Definition: hbridge3.h:146
hbridge3_cfg_t::sdo
pin_name_t sdo
Definition: hbridge3.h:144
hbridge3_t::pwm
pwm_t pwm
Definition: hbridge3.h:127
hbridge3_cs_set
void hbridge3_cs_set(hbridge3_t *ctx, uint8_t pin_state)
Setting CS pin state.
hbridge3_cfg_t::sdi
pin_name_t sdi
Definition: hbridge3.h:145
hbridge3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: hbridge3.h:159
hbridge3_t::dis
digital_out_t dis
Definition: hbridge3.h:120
hbridge3_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: hbridge3.h:158
hbridge3_t::dir
digital_out_t dir
Definition: hbridge3.h:119
hbridge3_t
Click ctx object definition.
Definition: hbridge3.h:116
hbridge3_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: hbridge3.h:156
hbridge3_t::cs
digital_out_t cs
Definition: hbridge3.h:121
hbridge3_dir_set
void hbridge3_dir_set(hbridge3_t *ctx, uint8_t pin_state)
Setting AN pin state.
hbridge3_init
HBRIDGE3_RETVAL hbridge3_init(hbridge3_t *ctx, hbridge3_cfg_t *cfg)
Initialization function.
hbridge3_cfg_t::cs
pin_name_t cs
Definition: hbridge3.h:152
hbridge3_pwm_stop
void hbridge3_pwm_stop(hbridge3_t *ctx)
Stop PWM module.
hbridge3_dis_set
void hbridge3_dis_set(hbridge3_t *ctx, uint8_t pin_state)
Setting RST pin state.
hbridge3_cfg_t
Click configuration structure definition.
Definition: hbridge3.h:140
hbridge3_t::pwm_freq
uint32_t pwm_freq
Definition: hbridge3.h:132
hbridge3_spi
uint8_t hbridge3_spi(hbridge3_t *ctx, uint8_t spi_command)
Sending SPI command.
hbridge3_cfg_t::spi_speed
uint32_t spi_speed
Definition: hbridge3.h:157
hbridge3_cfg_t::dis
pin_name_t dis
Definition: hbridge3.h:151
HBRIDGE3_RETVAL
#define HBRIDGE3_RETVAL
Definition: hbridge3.h:67
hbridge3_t::spi
spi_master_t spi
Definition: hbridge3.h:125
hbridge3_cfg_t::dir
pin_name_t dir
Definition: hbridge3.h:150
hbridge3_default_cfg
void hbridge3_default_cfg(hbridge3_t *ctx, float duty_cycle)
Click Default Configuration function.
hbridge3_t::chip_select
pin_name_t chip_select
Definition: hbridge3.h:126
hbridge3_cfg_t::pwm
pin_name_t pwm
Definition: hbridge3.h:143