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 
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_i2c_master.h"
54 
55 // -------------------------------------------------------------- PUBLIC MACROS
65 #define PWM_MAP_MIKROBUS( cfg, mikrobus ) \
66  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
67  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
68  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST );
69 
75 #define PWM_MAX_RESOLUTION 4096
76 
82 #define PWM_RETVAL uint8_t
83 #define PWM_OK 0x00
84 #define PWM_INIT_ERROR 0xFF
85  // End group macro
88 
89  // -------------------------------------------------------------- PRIVATE MACROS
99 #define PWM_DISABLE 0x00
100 #define PWM_ENABLE 0x01
101 
107 #define PWM_RESTART 0x80
108 #define PWM_EXTCLK 0x40
109 #define PWM_AI 0x20
110 #define PWM_SLEEP 0x10
111 #define PWM_SUB_1 0x08
112 #define PWM_SUB_2 0x04
113 #define PWM_SUB_3 0x02
114 #define PWM_ALL_CALL 0x01
115 
121 #define PWM_INVRT 0x10
122 #define PWM_OCH 0x08
123 #define PWM_OUTDRV 0x04
124 #define PWM_OUTNE1 0x02
125 #define PWM_OUTNE0 0x01
126 
132 #define PWM_I2C_ADDRESS 0x40
133 
139 #define PWM_MODE1_REG 0x00
140 
146 #define PWM_MODE2_REG 0x01
147 
153 #define PWM_PRE_SCALE 0xFE
154 
160 #define PWM_TEST 0xFF
161 
167 #define PWM_CH0_ON_L 0x06
168 #define PWM_CH0_ON_H 0x07
169 #define PWM_CH0_OFF_L 0x08
170 #define PWM_CH0_OFF_H 0x09
171 
177 #define PWM_CH1_ON_L 0x0A
178 #define PWM_CH1_ON_H 0x0B
179 #define PWM_CH1_OFF_L 0x0C
180 #define PWM_CH1_OFF_H 0x0D
181 
187 #define PWM_CH2_ON_L 0x0E
188 #define PWM_CH2_ON_H 0x0F
189 #define PWM_CH2_OFF_L 0x10
190 #define PWM_CH2_OFF_H 0x11
191 
197 #define PWM_CH3_ON_L 0x12
198 #define PWM_CH3_ON_H 0x13
199 #define PWM_CH3_OFF_L 0x14
200 #define PWM_CH3_OFF_H 0x15
201 
207 #define PWM_CH4_ON_L 0x16
208 #define PWM_CH4_ON_H 0x17
209 #define PWM_CH4_OFF_L 0x18
210 #define PWM_CH4_OFF_H 0x19
211 
217 #define PWM_CH5_ON_L 0x1A
218 #define PWM_CH5_ON_H 0x1B
219 #define PWM_CH5_OFF_L 0x1C
220 #define PWM_CH5_OFF_H 0x1D
221 
227 #define PWM_CH6_ON_L 0x1E
228 #define PWM_CH6_ON_H 0x1F
229 #define PWM_CH6_OFF_L 0x20
230 #define PWM_CH6_OFF_H 0x21
231 
237 #define PWM_CH7_ON_L 0x22
238 #define PWM_CH7_ON_H 0x23
239 #define PWM_CH7_OFF_L 0x24
240 #define PWM_CH7_OFF_H 0x25
241 
247 #define PWM_CH8_ON_L 0x26
248 #define PWM_CH8_ON_H 0x27
249 #define PWM_CH8_OFF_L 0x28
250 #define PWM_CH8_OFF_H 0x29
251 
257 #define PWM_CH9_ON_L 0x2A
258 #define PWM_CH9_ON_H 0x2B
259 #define PWM_CH9_OFF_L 0x2C
260 #define PWM_CH9_OFF_H 0x2D
261 
267 #define PWM_CH10_ON_L 0x2E
268 #define PWM_CH10_ON_H 0x2F
269 #define PWM_CH10_OFF_L 0x30
270 #define PWM_CH10_OFF_H 0x31
271 
277 #define PWM_CH11_ON_L 0x32
278 #define PWM_CH11_ON_H 0x33
279 #define PWM_CH11_OFF_L 0x34
280 #define PWM_CH11_OFF_H 0x35
281 
287 #define PWM_CH12_ON_L 0x36
288 #define PWM_CH12_ON_H 0x37
289 #define PWM_CH12_OFF_L 0x38
290 #define PWM_CH12_OFF_H 0x39
291 
297 #define PWM_CH13_ON_L 0x3A
298 #define PWM_CH13_ON_H 0x3B
299 #define PWM_CH13_OFF_L 0x3C
300 #define PWM_CH13_OFF_H 0x3D
301 
307 #define PWM_CH14_ON_L 0x3E
308 #define PWM_CH14_ON_H 0x3F
309 #define PWM_CH14_OFF_L 0x40
310 #define PWM_CH14_OFF_H 0x41
311 
317 #define PWM_CH15_ON_L 0x42
318 #define PWM_CH15_ON_H 0x43
319 #define PWM_CH15_OFF_L 0x44
320 #define PWM_CH15_OFF_H 0x45
321 
327 #define PWM_ALL_CH_ON_L 0xFA
328 #define PWM_ALL_CH_ON_H 0xFB
329 #define PWM_ALL_CH_OFF_L 0xFC
330 #define PWM_ALL_CH_OFF_H 0xFD
331  // End group macro
334 // --------------------------------------------------------------- PUBLIC TYPES
343 typedef struct
344 {
345  // Output pins
346 
347  digital_out_t rst;
348 
349  // Modules
350 
351  i2c_master_t i2c;
352 
353  // ctx variable
354 
355  uint8_t slave_address;
356 
357 } pwm_t;
358 
362 typedef struct
363 {
364  // Communication gpio pins
365 
366  pin_name_t scl;
367  pin_name_t sda;
368 
369  // Additional gpio pins
370 
371  pin_name_t rst;
372 
373  // static variable
374 
375  uint32_t i2c_speed;
376  uint8_t i2c_address;
377 
378 } pwm_cfg_t;
379  // End types group
381 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
382 
388 #ifdef __cplusplus
389 extern "C"{
390 #endif
391 
400 void pwm_cfg_setup ( pwm_cfg_t *cfg );
401 
411 
422 void pwm_generic_write ( pwm_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
423 
434 void pwm_generic_read ( pwm_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
435 
436 
445 void pwm_set_output ( pwm_t *ctx, uint8_t en_ou );
446 
472 void pwm_dev_config ( pwm_t *ctx, uint8_t *config );
473 
494 void pwm_output_config ( pwm_t *ctx, uint8_t *config1 );
495 
507 void pwm_channel_state ( pwm_t *ctx, uint8_t chann_id, uint8_t state );
508 
519 void pwm_set_channel_raw ( pwm_t *ctx, uint8_t chann_id, uint16_t raw_off_set, uint16_t raw_dc );
520 
531 void pwm_all_chann_state( pwm_t *ctx, uint8_t state );
532 
541 void pwm_set_pre_scale ( pwm_t *ctx, uint8_t pre_scale );
542 
551 void pwm_set_all_raw ( pwm_t *ctx, uint16_t raw_dc );
552 
553 #ifdef __cplusplus
554 }
555 #endif
556 #endif // _PWM_H_
557  // End public_function group
560 
561 // ------------------------------------------------------------------------- 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:363
pwm_t::i2c
i2c_master_t i2c
Definition: pwm.h:351
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:367
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:82
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:355
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:366
pwm_cfg_t::rst
pin_name_t rst
Definition: pwm.h:371
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:376
pwm_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: pwm.h:375
pwm_t::rst
digital_out_t rst
Definition: pwm.h:347
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:344