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 
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 PWM2_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
55  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
56  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
57  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.pwm = MIKROBUS( mikrobus, MIKROBUS_PWM )
59 
65 #define PWM2_RETVAL uint8_t
66 
67 #define PWM2_OK 0x00
68 #define PWM2_INIT_ERROR 0xFF
69 
75 #define PWM2_DEF_FREQ 20000
76 
82 #define PWM2_CLOCK_25_MHZ 25000000
83 #define PWM2_CLOCK_15_MHZ 15000000
84 #define PWM2_CLOCK_5_MHZ 5000000
85 
91 #define PWM2_100_PERCENT_DUTY 4095
92 #define PWM2_75_PERCENT_DUTY 3072
93 #define PWM2_50_PERCENT_DUTY 2048
94 #define PWM2_25_PERCENT_DUTY 1024
95 
101 #define PWM2_NO_CORRECTION 0
102  // End group macro
105 // --------------------------------------------------------------- PUBLIC TYPES
114 typedef struct
115 {
116 
117  // Output pins
118 
119  digital_out_t cs;
120 
121  // Input pins
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  uint32_t pwm_freq;
132 
133 } pwm2_t;
134 
135 typedef struct
136 {
137  uint16_t width;
138  uint8_t correction;
139 
140 } pwm_channel_t;
141 
145 typedef struct
146 {
147  // Communication gpio pins
148  pin_name_t miso;
149  pin_name_t mosi;
150  pin_name_t sck;
151  pin_name_t cs;
152  pin_name_t pwm;
153 
154  // Additional gpio pins
155 
156 
157  // static variable
158  uint32_t spi_speed;
159  spi_master_mode_t spi_mode;
160  spi_master_chip_select_polarity_t cs_polarity;
161  uint32_t dev_pwm_freq;
162 
163 } pwm2_cfg_t;
164  // End types group
166 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
167 
173 #ifdef __cplusplus
174 extern "C"{
175 #endif
176 
186 
196 
205 void pwm2_set_duty_cycle ( pwm2_t *ctx, float duty_cycle );
206 
214 void pwm2_pwm_stop ( pwm2_t *ctx );
215 
223 void pwm2_pwm_start ( pwm2_t *ctx );
224 
236 void pwm2_generic_transfer ( pwm2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
237 
245  static void send_reset_pulse ( pwm2_t *ctx );
246 
254 static void send_pulse ( pwm2_t *ctx );
255 
263 static void send_frame_buffer ( pwm2_t *ctx );
264 
272 static void send_async_update_frame ( pwm2_t *ctx );
273 
281 static void send_correction_frame ( pwm2_t *ctx );
282 
290 static void send_output_enable_frame ( pwm2_t *ctx );
291 
299 static void send_correction_toggle_frame ( pwm2_t *ctx );
300 
308 static void set_parameters ( pwm2_t *ctx );
309 
318 
329 uint8_t pwm2_set_channel ( pwm2_t *ctx, uint8_t n_channel, uint16_t width, uint8_t correction );
330 
338 void pwm2_default_cfg ( pwm2_t *ctx );
339 
340 #ifdef __cplusplus
341 }
342 #endif
343 #endif // _PWM2_H_
344  // End public_function group
347 
348 // ------------------------------------------------------------------------- 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:148
pwm2_t::chip_select
pin_name_t chip_select
Definition: pwm2.h:126
pwm2_cfg_t::spi_speed
uint32_t spi_speed
Definition: pwm2.h:158
pwm2_cfg_t::mosi
pin_name_t mosi
Definition: pwm2.h:149
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:159
pwm2_t
Click ctx object definition.
Definition: pwm2.h:115
pwm2_t::cs
digital_out_t cs
Definition: pwm2.h:119
pwm2_cfg_t
Click configuration structure definition.
Definition: pwm2.h:146
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:127
pwm2_cfg_t::cs
pin_name_t cs
Definition: pwm2.h:151
pwm2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: pwm2.h:160
pwm_channel_t
Definition: pwm2.h:136
pwm_channel_t::correction
uint8_t correction
Definition: pwm2.h:138
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:150
pwm_channel_t::width
uint16_t width
Definition: pwm2.h:137
pwm2_t::pwm_freq
uint32_t pwm_freq
Definition: pwm2.h:131
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:125
PWM2_RETVAL
#define PWM2_RETVAL
Definition: pwm2.h:65
pwm2_cfg_t::pwm
pin_name_t pwm
Definition: pwm2.h:152
pwm2_cfg_t::dev_pwm_freq
uint32_t dev_pwm_freq
Definition: pwm2.h:161