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 )
79#define PWM2_RETVAL uint8_t
80
81#define PWM2_OK 0x00
82#define PWM2_INIT_ERROR 0xFF
89#define PWM2_DEF_FREQ 20000
96#define PWM2_CLOCK_25_MHZ 25000000
97#define PWM2_CLOCK_15_MHZ 15000000
98#define PWM2_CLOCK_5_MHZ 5000000
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
115#define PWM2_NO_CORRECTION 0
118 // End group macro
119// --------------------------------------------------------------- PUBLIC TYPES
128typedef 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
149typedef struct
150{
151 uint16_t width;
152 uint8_t correction;
153
155
159typedef 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
179 // End types group
180// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
181
187#ifdef __cplusplus
188extern "C"{
189#endif
190
200
210
219void pwm2_set_duty_cycle ( pwm2_t *ctx, float duty_cycle );
220
228void pwm2_pwm_stop ( pwm2_t *ctx );
229
237void pwm2_pwm_start ( pwm2_t *ctx );
238
250void 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
268static void send_pulse ( pwm2_t *ctx );
269
277static void send_frame_buffer ( pwm2_t *ctx );
278
286static void send_async_update_frame ( pwm2_t *ctx );
287
295static void send_correction_frame ( pwm2_t *ctx );
296
304static void send_output_enable_frame ( pwm2_t *ctx );
305
313static void send_correction_toggle_frame ( pwm2_t *ctx );
314
322static void set_parameters ( pwm2_t *ctx );
323
332
343uint8_t pwm2_set_channel ( pwm2_t *ctx, uint8_t n_channel, uint16_t width, uint8_t correction );
344
353
354#ifdef __cplusplus
355}
356#endif
357#endif // _PWM2_H_
358
359 // End public_function group
361
362// ------------------------------------------------------------------------- END
#define PWM2_RETVAL
Definition pwm2.h:79
void pwm2_set_duty_cycle(pwm2_t *ctx, float duty_cycle)
Generic sets PWM duty cycle.
void pwm2_toggle_phase_shift(pwm2_t *ctx)
Toggle phase shift.
uint8_t pwm2_set_channel(pwm2_t *ctx, uint8_t n_channel, uint16_t width, uint8_t correction)
Set channel function.
void pwm2_cfg_setup(pwm2_cfg_t *cfg)
Config Object Initialization function.
PWM2_RETVAL pwm2_init(pwm2_t *ctx, pwm2_cfg_t *cfg)
Initialization function.
void pwm2_default_cfg(pwm2_t *ctx)
Click Default Configuration function.
void pwm2_pwm_start(pwm2_t *ctx)
Start PWM module.
void pwm2_pwm_stop(pwm2_t *ctx)
Stop PWM module.
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.
Click configuration structure definition.
Definition pwm2.h:160
spi_master_chip_select_polarity_t cs_polarity
Definition pwm2.h:174
pin_name_t sck
Definition pwm2.h:164
spi_master_mode_t spi_mode
Definition pwm2.h:173
pin_name_t mosi
Definition pwm2.h:163
uint32_t spi_speed
Definition pwm2.h:172
uint32_t dev_pwm_freq
Definition pwm2.h:175
pin_name_t pwm
Definition pwm2.h:166
pin_name_t miso
Definition pwm2.h:162
pin_name_t cs
Definition pwm2.h:165
Click ctx object definition.
Definition pwm2.h:129
digital_out_t cs
Definition pwm2.h:133
spi_master_t spi
Definition pwm2.h:139
uint32_t pwm_freq
Definition pwm2.h:145
pin_name_t chip_select
Definition pwm2.h:140
pwm_t pwm
Definition pwm2.h:141
Definition pwm2.h:150
uint8_t correction
Definition pwm2.h:152
uint16_t width
Definition pwm2.h:151