pwm  2.0.0.0
pwm.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 PWM_H
36 #define PWM_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_i2c_master.h"
50 
51 // -------------------------------------------------------------- PUBLIC MACROS
61 #define PWM_MAP_MIKROBUS( cfg, mikrobus ) \
62  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
63  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
64  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
65 
71 #define PWM_MAX_RESOLUTION 4096
72 
78 #define PWM_RETVAL uint8_t
79 #define PWM_OK 0x00
80 #define PWM_INIT_ERROR 0xFF
81  // End group macro
84 
85  // -------------------------------------------------------------- PRIVATE MACROS
95 #define PWM_DISABLE 0x00
96 #define PWM_ENABLE 0x01
97 
103 #define PWM_RESTART 0x80
104 #define PWM_EXTCLK 0x40
105 #define PWM_AI 0x20
106 #define PWM_SLEEP 0x10
107 #define PWM_SUB_1 0x08
108 #define PWM_SUB_2 0x04
109 #define PWM_SUB_3 0x02
110 #define PWM_ALL_CALL 0x01
111 
117 #define PWM_INVRT 0x10
118 #define PWM_OCH 0x08
119 #define PWM_OUTDRV 0x04
120 #define PWM_OUTNE1 0x02
121 #define PWM_OUTNE0 0x01
122 
128 #define PWM_I2C_ADDRESS 0x40
129 
135 #define PWM_MODE1_REG 0x00
136 
142 #define PWM_MODE2_REG 0x01
143 
149 #define PWM_PRE_SCALE 0xFE
150 
156 #define PWM_TEST 0xFF
157 
163 #define PWM_CH0_ON_L 0x06
164 #define PWM_CH0_ON_H 0x07
165 #define PWM_CH0_OFF_L 0x08
166 #define PWM_CH0_OFF_H 0x09
167 
173 #define PWM_CH1_ON_L 0x0A
174 #define PWM_CH1_ON_H 0x0B
175 #define PWM_CH1_OFF_L 0x0C
176 #define PWM_CH1_OFF_H 0x0D
177 
183 #define PWM_CH2_ON_L 0x0E
184 #define PWM_CH2_ON_H 0x0F
185 #define PWM_CH2_OFF_L 0x10
186 #define PWM_CH2_OFF_H 0x11
187 
193 #define PWM_CH3_ON_L 0x12
194 #define PWM_CH3_ON_H 0x13
195 #define PWM_CH3_OFF_L 0x14
196 #define PWM_CH3_OFF_H 0x15
197 
203 #define PWM_CH4_ON_L 0x16
204 #define PWM_CH4_ON_H 0x17
205 #define PWM_CH4_OFF_L 0x18
206 #define PWM_CH4_OFF_H 0x19
207 
213 #define PWM_CH5_ON_L 0x1A
214 #define PWM_CH5_ON_H 0x1B
215 #define PWM_CH5_OFF_L 0x1C
216 #define PWM_CH5_OFF_H 0x1D
217 
223 #define PWM_CH6_ON_L 0x1E
224 #define PWM_CH6_ON_H 0x1F
225 #define PWM_CH6_OFF_L 0x20
226 #define PWM_CH6_OFF_H 0x21
227 
233 #define PWM_CH7_ON_L 0x22
234 #define PWM_CH7_ON_H 0x23
235 #define PWM_CH7_OFF_L 0x24
236 #define PWM_CH7_OFF_H 0x25
237 
243 #define PWM_CH8_ON_L 0x26
244 #define PWM_CH8_ON_H 0x27
245 #define PWM_CH8_OFF_L 0x28
246 #define PWM_CH8_OFF_H 0x29
247 
253 #define PWM_CH9_ON_L 0x2A
254 #define PWM_CH9_ON_H 0x2B
255 #define PWM_CH9_OFF_L 0x2C
256 #define PWM_CH9_OFF_H 0x2D
257 
263 #define PWM_CH10_ON_L 0x2E
264 #define PWM_CH10_ON_H 0x2F
265 #define PWM_CH10_OFF_L 0x30
266 #define PWM_CH10_OFF_H 0x31
267 
273 #define PWM_CH11_ON_L 0x32
274 #define PWM_CH11_ON_H 0x33
275 #define PWM_CH11_OFF_L 0x34
276 #define PWM_CH11_OFF_H 0x35
277 
283 #define PWM_CH12_ON_L 0x36
284 #define PWM_CH12_ON_H 0x37
285 #define PWM_CH12_OFF_L 0x38
286 #define PWM_CH12_OFF_H 0x39
287 
293 #define PWM_CH13_ON_L 0x3A
294 #define PWM_CH13_ON_H 0x3B
295 #define PWM_CH13_OFF_L 0x3C
296 #define PWM_CH13_OFF_H 0x3D
297 
303 #define PWM_CH14_ON_L 0x3E
304 #define PWM_CH14_ON_H 0x3F
305 #define PWM_CH14_OFF_L 0x40
306 #define PWM_CH14_OFF_H 0x41
307 
313 #define PWM_CH15_ON_L 0x42
314 #define PWM_CH15_ON_H 0x43
315 #define PWM_CH15_OFF_L 0x44
316 #define PWM_CH15_OFF_H 0x45
317 
323 #define PWM_ALL_CH_ON_L 0xFA
324 #define PWM_ALL_CH_ON_H 0xFB
325 #define PWM_ALL_CH_OFF_L 0xFC
326 #define PWM_ALL_CH_OFF_H 0xFD
327  // End group macro
330 // --------------------------------------------------------------- PUBLIC TYPES
339 typedef struct
340 {
341  // Output pins
342 
343  digital_out_t rst;
344 
345  // Modules
346 
347  i2c_master_t i2c;
348 
349  // ctx variable
350 
351  uint8_t slave_address;
352 
353 } pwm_t;
354 
358 typedef struct
359 {
360  // Communication gpio pins
361 
362  pin_name_t scl;
363  pin_name_t sda;
364 
365  // Additional gpio pins
366 
367  pin_name_t rst;
368 
369  // static variable
370 
371  uint32_t i2c_speed;
372  uint8_t i2c_address;
373 
374 } pwm_cfg_t;
375  // End types group
377 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
378 
384 #ifdef __cplusplus
385 extern "C"{
386 #endif
387 
396 void pwm_cfg_setup ( pwm_cfg_t *cfg );
397 
407 
418 void pwm_generic_write ( pwm_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
419 
430 void pwm_generic_read ( pwm_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
431 
432 
441 void pwm_set_output ( pwm_t *ctx, uint8_t en_ou );
442 
468 void pwm_dev_config ( pwm_t *ctx, uint8_t *config );
469 
490 void pwm_output_config ( pwm_t *ctx, uint8_t *config1 );
491 
503 void pwm_channel_state ( pwm_t *ctx, uint8_t chann_id, uint8_t state );
504 
515 void pwm_set_channel_raw ( pwm_t *ctx, uint8_t chann_id, uint16_t raw_off_set, uint16_t raw_dc );
516 
527 void pwm_all_chann_state( pwm_t *ctx, uint8_t state );
528 
537 void pwm_set_pre_scale ( pwm_t *ctx, uint8_t pre_scale );
538 
547 void pwm_set_all_raw ( pwm_t *ctx, uint16_t raw_dc );
548 
549 #ifdef __cplusplus
550 }
551 #endif
552 #endif // _PWM_H_
553  // End public_function group
556 
557 // ------------------------------------------------------------------------- END
pwm_set_all_raw
void pwm_set_all_raw(pwm_t *ctx, uint16_t raw_dc)
Set all channels raw function.
pwm_cfg_setup
void pwm_cfg_setup(pwm_cfg_t *cfg)
Config Object Initialization function.
pwm_set_channel_raw
void pwm_set_channel_raw(pwm_t *ctx, uint8_t chann_id, uint16_t raw_off_set, uint16_t raw_dc)
Set channel raw function.
pwm_cfg_t
Click configuration structure definition.
Definition: pwm.h:359
pwm_t::i2c
i2c_master_t i2c
Definition: pwm.h:347
pwm_set_pre_scale
void pwm_set_pre_scale(pwm_t *ctx, uint8_t pre_scale)
Set pre scale function.
pwm_cfg_t::sda
pin_name_t sda
Definition: pwm.h:363
pwm_generic_read
void pwm_generic_read(pwm_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
pwm_init
PWM_RETVAL pwm_init(pwm_t *ctx, pwm_cfg_t *cfg)
Initialization function.
PWM_RETVAL
#define PWM_RETVAL
Definition: pwm.h:78
pwm_channel_state
void pwm_channel_state(pwm_t *ctx, uint8_t chann_id, uint8_t state)
Channel state function.
pwm_t::slave_address
uint8_t slave_address
Definition: pwm.h:351
pwm_dev_config
void pwm_dev_config(pwm_t *ctx, uint8_t *config)
Device configuration function.
pwm_cfg_t::scl
pin_name_t scl
Definition: pwm.h:362
pwm_cfg_t::rst
pin_name_t rst
Definition: pwm.h:367
pwm_all_chann_state
void pwm_all_chann_state(pwm_t *ctx, uint8_t state)
All channels state function.
pwm_generic_write
void pwm_generic_write(pwm_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
pwm_cfg_t::i2c_address
uint8_t i2c_address
Definition: pwm.h:372
pwm_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: pwm.h:371
pwm_t::rst
digital_out_t rst
Definition: pwm.h:343
pwm_output_config
void pwm_output_config(pwm_t *ctx, uint8_t *config1)
Output configuration function.
pwm_set_output
void pwm_set_output(pwm_t *ctx, uint8_t en_ou)
Output state set function.
pwm_t
Click ctx object definition.
Definition: pwm.h:340