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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_pwm.h"
51 #include "drv_spi_master.h"
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define HBRIDGE3_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
65  cfg.dir = MIKROBUS( mikrobus, MIKROBUS_AN ); \
66  cfg.dis = MIKROBUS( mikrobus, MIKROBUS_RST ); \
67  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
68  cfg.sdo = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
69  cfg.sdi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
70  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK )
71 
77 #define HBRIDGE3_RETVAL uint8_t
78 
79 #define HBRIDGE3_OK 0x00
80 #define HBRIDGE3_INIT_ERROR 0xFF
81 
87 #define HBRIDGE3_DEF_FREQ 20000
88 
94 #define HBRIDGE3_CMD_RD_DIA 0x00
95 #define HBRIDGE3_CMD_RES_DIA 0x80
96 #define HBRIDGE3_CMD_RD_REV 0x20
97 #define HBRIDGE3_CMD_RD_CTRL 0x60
98 #define HBRIDGE3_CMD_WR_CTRL 0xE0
99 #define HBRIDGE3_CMD_WR_CTRL_RD_DIA 0xC0
100 
106 #define HBRIDGE3_CTRL_VIA_PWM_DIR 0x00
107 #define HBRIDGE3_CTRL_VIA_SPI 0x80
108 #define HBRIDGE3_SPI_ENA_OUT 0x40
109 #define HBRIDGE3_SPI_DIS_OUT 0x00
110 #define HBRIDGE3_SPI_DIR_1 0x00
111 #define HBRIDGE3_SPI_DIR_2 0x20
112 #define HBRIDGE3_SPI_PWM_0 0x00
113 #define HBRIDGE3_SPI_PWM_1 0x01
114  // End group macro
116 // --------------------------------------------------------------- PUBLIC TYPES
125 typedef struct
126 {
127  // Output pins
128 
129  digital_out_t dir;
130  digital_out_t dis;
131  digital_out_t cs;
132 
133 
134  // Modules
135  spi_master_t spi;
136  pin_name_t chip_select;
137  pwm_t pwm;
138 
139  // ctx variable
140 
141  uint16_t pwm_period;
142  uint32_t pwm_freq;
143 
144 } hbridge3_t;
145 
149 typedef struct
150 {
151  // Communication gpio pins
152 
153  pin_name_t pwm;
154  pin_name_t sdo;
155  pin_name_t sdi;
156  pin_name_t sck;
157 
158  // Additional gpio pins
159 
160  pin_name_t dir;
161  pin_name_t dis;
162  pin_name_t cs;
163 
164  // static variable
165 
166  uint32_t dev_pwm_freq;
167  uint32_t spi_speed;
168  spi_master_mode_t spi_mode;
169  spi_master_chip_select_polarity_t cs_polarity;
170 
172  // End types group
174 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
175 
181 #ifdef __cplusplus
182 extern "C"{
183 #endif
184 
194 
204 
212 void hbridge3_default_cfg ( hbridge3_t *ctx, float duty_cycle );
213 
222 void hbridge3_set_duty_cycle ( hbridge3_t *ctx, float duty_cycle );
223 
232 
241 
250 void hbridge3_dir_set ( hbridge3_t *ctx, uint8_t pin_state );
251 
260 void hbridge3_dis_set ( hbridge3_t *ctx, uint8_t pin_state );
261 
270 void hbridge3_cs_set ( hbridge3_t *ctx, uint8_t pin_state );
271 
272 
283 uint8_t hbridge3_spi ( hbridge3_t *ctx, uint8_t spi_command );
284 
285 
286 #ifdef __cplusplus
287 }
288 #endif
289 #endif // _HBRIDGE3_H_
290  // End public_function group
293 
294 // ------------------------------------------------------------------------- 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:141
hbridge3_pwm_start
void hbridge3_pwm_start(hbridge3_t *ctx)
Start PWM module.
hbridge3_cfg_t::sck
pin_name_t sck
Definition: hbridge3.h:156
hbridge3_cfg_t::sdo
pin_name_t sdo
Definition: hbridge3.h:154
hbridge3_t::pwm
pwm_t pwm
Definition: hbridge3.h:137
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:155
hbridge3_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: hbridge3.h:169
hbridge3_t::dis
digital_out_t dis
Definition: hbridge3.h:130
hbridge3_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: hbridge3.h:168
hbridge3_t::dir
digital_out_t dir
Definition: hbridge3.h:129
hbridge3_t
Click ctx object definition.
Definition: hbridge3.h:126
hbridge3_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: hbridge3.h:166
hbridge3_t::cs
digital_out_t cs
Definition: hbridge3.h:131
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:162
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:150
hbridge3_t::pwm_freq
uint32_t pwm_freq
Definition: hbridge3.h:142
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:167
hbridge3_cfg_t::dis
pin_name_t dis
Definition: hbridge3.h:161
HBRIDGE3_RETVAL
#define HBRIDGE3_RETVAL
Definition: hbridge3.h:77
hbridge3_t::spi
spi_master_t spi
Definition: hbridge3.h:135
hbridge3_cfg_t::dir
pin_name_t dir
Definition: hbridge3.h:160
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:136
hbridge3_cfg_t::pwm
pin_name_t pwm
Definition: hbridge3.h:153