pwm2  2.0.0.0
pwm2.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 PWM2_H
36 #define PWM2_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_pwm.h"
55 #include "drv_spi_master.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
67 #define PWM2_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.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM )
73 
79 #define PWM2_RETVAL uint8_t
80 
81 #define PWM2_OK 0x00
82 #define PWM2_INIT_ERROR 0xFF
83 
89 #define PWM2_DEF_FREQ 20000
90 
96 #define PWM2_CLOCK_25_MHZ 25000000
97 #define PWM2_CLOCK_15_MHZ 15000000
98 #define PWM2_CLOCK_5_MHZ 5000000
99 
105 #define PWM2_100_PERCENT_DUTY 4095
106 #define PWM2_75_PERCENT_DUTY 3072
107 #define PWM2_50_PERCENT_DUTY 2048
108 #define PWM2_25_PERCENT_DUTY 1024
109 
115 #define PWM2_NO_CORRECTION 0
116  // End group macro
119 // --------------------------------------------------------------- PUBLIC TYPES
128 typedef struct
129 {
130 
131  // Output pins
132 
133  digital_out_t cs;
134 
135  // Input pins
136 
137 
138  // Modules
139  spi_master_t spi;
140  pin_name_t chip_select;
141  pwm_t pwm;
142 
143  // ctx variable
144 
145  uint32_t pwm_freq;
146 
147 } pwm2_t;
148 
149 typedef struct
150 {
151  uint16_t width;
152  uint8_t correction;
153 
154 } pwm_channel_t;
155 
159 typedef struct
160 {
161  // Communication gpio pins
162  pin_name_t miso;
163  pin_name_t mosi;
164  pin_name_t sck;
165  pin_name_t cs;
166  pin_name_t pwm;
167 
168  // Additional gpio pins
169 
170 
171  // static variable
172  uint32_t spi_speed;
173  spi_master_mode_t spi_mode;
174  spi_master_chip_select_polarity_t cs_polarity;
175  uint32_t dev_pwm_freq;
176 
177 } pwm2_cfg_t;
178  // End types group
180 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
181 
187 #ifdef __cplusplus
188 extern "C"{
189 #endif
190 
200 
210 
219 void pwm2_set_duty_cycle ( pwm2_t *ctx, float duty_cycle );
220 
228 void pwm2_pwm_stop ( pwm2_t *ctx );
229 
237 void pwm2_pwm_start ( pwm2_t *ctx );
238 
250 void pwm2_generic_transfer ( pwm2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
251 
259  static void send_reset_pulse ( pwm2_t *ctx );
260 
268 static void send_pulse ( pwm2_t *ctx );
269 
277 static void send_frame_buffer ( pwm2_t *ctx );
278 
286 static void send_async_update_frame ( pwm2_t *ctx );
287 
295 static void send_correction_frame ( pwm2_t *ctx );
296 
304 static void send_output_enable_frame ( pwm2_t *ctx );
305 
313 static void send_correction_toggle_frame ( pwm2_t *ctx );
314 
322 static void set_parameters ( pwm2_t *ctx );
323 
332 
343 uint8_t pwm2_set_channel ( pwm2_t *ctx, uint8_t n_channel, uint16_t width, uint8_t correction );
344 
352 void pwm2_default_cfg ( pwm2_t *ctx );
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 #endif // _PWM2_H_
358  // End public_function group
361 
362 // ------------------------------------------------------------------------- END
pwm2_set_duty_cycle
void pwm2_set_duty_cycle(pwm2_t *ctx, float duty_cycle)
Generic sets PWM duty cycle.
pwm2_cfg_t::miso
pin_name_t miso
Definition: pwm2.h:162
pwm2_t::chip_select
pin_name_t chip_select
Definition: pwm2.h:140
pwm2_cfg_t::spi_speed
uint32_t spi_speed
Definition: pwm2.h:172
pwm2_cfg_t::mosi
pin_name_t mosi
Definition: pwm2.h:163
pwm2_cfg_setup
void pwm2_cfg_setup(pwm2_cfg_t *cfg)
Config Object Initialization function.
pwm2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: pwm2.h:173
pwm2_t
Click ctx object definition.
Definition: pwm2.h:129
pwm2_t::cs
digital_out_t cs
Definition: pwm2.h:133
pwm2_cfg_t
Click configuration structure definition.
Definition: pwm2.h:160
pwm2_generic_transfer
void pwm2_generic_transfer(pwm2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.
pwm2_t::pwm
pwm_t pwm
Definition: pwm2.h:141
pwm2_cfg_t::cs
pin_name_t cs
Definition: pwm2.h:165
pwm2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: pwm2.h:174
pwm_channel_t
Definition: pwm2.h:150
pwm_channel_t::correction
uint8_t correction
Definition: pwm2.h:152
pwm2_pwm_stop
void pwm2_pwm_stop(pwm2_t *ctx)
Stop PWM module.
pwm2_init
PWM2_RETVAL pwm2_init(pwm2_t *ctx, pwm2_cfg_t *cfg)
Initialization function.
pwm2_toggle_phase_shift
void pwm2_toggle_phase_shift(pwm2_t *ctx)
Toggle phase shift.
pwm2_set_channel
uint8_t pwm2_set_channel(pwm2_t *ctx, uint8_t n_channel, uint16_t width, uint8_t correction)
Set channel function.
pwm2_cfg_t::sck
pin_name_t sck
Definition: pwm2.h:164
pwm_channel_t::width
uint16_t width
Definition: pwm2.h:151
pwm2_t::pwm_freq
uint32_t pwm_freq
Definition: pwm2.h:145
pwm2_default_cfg
void pwm2_default_cfg(pwm2_t *ctx)
Click Default Configuration function.
pwm2_pwm_start
void pwm2_pwm_start(pwm2_t *ctx)
Start PWM module.
pwm2_t::spi
spi_master_t spi
Definition: pwm2.h:139
PWM2_RETVAL
#define PWM2_RETVAL
Definition: pwm2.h:79
pwm2_cfg_t::pwm
pin_name_t pwm
Definition: pwm2.h:166
pwm2_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: pwm2.h:175