Ultibo API
C/C++ API for Ultibo Core
Loading...
Searching...
No Matches
pwm.h
Go to the documentation of this file.
1/*
2 * This file is part of the Ultibo project, https://ultibo.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2026 Garry Wood <garry@softoz.com.au>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26#ifndef _ULTIBO_PWM_H
27#define _ULTIBO_PWM_H
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#include "ultibo/globaltypes.h"
34#include "ultibo/globalconst.h"
35#include "ultibo/devices.h"
36
38#define PWM_NAME_PREFIX "PWM"
39
41#define PWM_TYPE_NONE 0
42
43#define PWM_TYPE_MAX 0
44
46#define PWM_STATE_DISABLED 0
47#define PWM_STATE_ENABLED 1
48
49#define PWM_STATE_MAX 1
50
52#define PWM_FLAG_NONE 0x00000000
53#define PWM_FLAG_GPIO 0x00000001
54#define PWM_FLAG_MODE 0x00000002
55#define PWM_FLAG_RANGE 0x00000004
56#define PWM_FLAG_FREQUENCY 0x00000008
57#define PWM_FLAG_POLARITY 0x00000010
58
60#define PWM_MODE_MARKSPACE 0
61#define PWM_MODE_BALANCED 1
62#define PWM_MODE_SERIALIZED 2
63
64#define PWM_MODE_MAX 2
65
67#define PWM_POLARITY_NORMAL 0
68#define PWM_POLARITY_INVERSE 1
69
70#define PWM_POLARITY_MAX 1
71
73
77{
78 uint32_t flags;
79 uint32_t gpio;
80 uint32_t mode;
81 uint32_t range;
82 uint32_t frequency;
83 uint32_t polarity;
84 uint32_t dutyns;
85 uint32_t periodns;
86 uint32_t minperiod;
87};
88
90typedef struct _PWM_DEVICE PWM_DEVICE;
91
93typedef uint32_t STDCALL (*pwm_enumerate_cb)(PWM_DEVICE *pwm, void *data);
95typedef uint32_t STDCALL (*pwm_notification_cb)(DEVICE *device, void *data, uint32_t notification);
96
98typedef uint32_t STDCALL (*pwm_device_start_proc)(PWM_DEVICE *pwm);
99typedef uint32_t STDCALL (*pwm_device_stop_proc)(PWM_DEVICE *pwm);
100
101typedef uint32_t STDCALL (*pwm_device_write_proc)(PWM_DEVICE *pwm, uint32_t value);
102
104typedef uint32_t STDCALL (*pwm_device_set_gpio_proc)(PWM_DEVICE *pwm, uint32_t gpio);
106typedef uint32_t STDCALL (*pwm_device_set_mode_proc)(PWM_DEVICE *pwm, uint32_t mode);
108typedef uint32_t STDCALL (*pwm_device_set_range_proc)(PWM_DEVICE *pwm, uint32_t range);
110typedef uint32_t STDCALL (*pwm_device_set_frequency_proc)(PWM_DEVICE *pwm, uint32_t frequency);
112typedef uint32_t STDCALL (*pwm_device_set_polarity_proc)(PWM_DEVICE *pwm, uint32_t polarity);
113
114typedef uint32_t STDCALL (*pwm_device_configure_proc)(PWM_DEVICE *pwm, uint32_t dutyns, uint32_t periodns);
115
117
159
161
168
175
185uint32_t STDCALL pwm_device_write(PWM_DEVICE *pwm, uint32_t value);
186
193
200uint32_t STDCALL pwm_device_set_gpio(PWM_DEVICE *pwm, uint32_t gpio);
201
208
215uint32_t STDCALL pwm_device_set_mode(PWM_DEVICE *pwm, uint32_t mode);
216
223
233uint32_t STDCALL pwm_device_set_range(PWM_DEVICE *pwm, uint32_t range);
234
241
248uint32_t STDCALL pwm_device_set_frequency(PWM_DEVICE *pwm, uint32_t frequency);
249
256
263uint32_t STDCALL pwm_device_set_polarity(PWM_DEVICE *pwm, uint32_t polarity);
264
272uint32_t STDCALL pwm_device_configure(PWM_DEVICE *pwm, uint32_t dutyns, uint32_t periodns);
273
282
290
296
303
308
313
318
322uint32_t STDCALL pwm_device_enumerate(pwm_enumerate_cb callback, void *data);
323
324uint32_t STDCALL pwm_device_notification(PWM_DEVICE *pwm, pwm_notification_cb callback, void *data, uint32_t notification, uint32_t flags);
325
327
331uint32_t STDCALL pwm_get_count(void);
332
337
342
347
351uint32_t STDCALL pwm_type_to_string(uint32_t pwmtype, char *string, uint32_t len);
352
356uint32_t STDCALL pwm_state_to_string(uint32_t pwmstate, char *string, uint32_t len);
357
361uint32_t STDCALL pwm_mode_to_string(uint32_t pwmmode, char *string, uint32_t len);
362
366uint32_t STDCALL pwm_polarity_to_string(uint32_t pwmpolarity, char *string, uint32_t len);
367
368#ifdef __cplusplus
369}
370#endif
371
372#endif // _ULTIBO_PWM_H
struct _DEVICE DEVICE
Definition devices.h:373
#define STDCALL
Definition globaltypes.h:45
HANDLE MUTEX_HANDLE
Definition globaltypes.h:105
uint32_t STDCALL pwm_device_get_frequency(PWM_DEVICE *pwm)
Get the clock frequency of the specified PWM device.
uint32_t STDCALL(* pwm_device_stop_proc)(PWM_DEVICE *pwm)
Definition pwm.h:99
PWM_DEVICE *STDCALL pwm_device_find_by_description(const char *description)
uint32_t STDCALL pwm_device_get_gpio(PWM_DEVICE *pwm)
Get the GPIO pin used by the specified PWM device.
uint32_t STDCALL pwm_device_write(PWM_DEVICE *pwm, uint32_t value)
Write a value to the specified PWM device.
uint32_t STDCALL(* pwm_device_get_frequency_proc)(PWM_DEVICE *pwm)
Definition pwm.h:109
uint32_t STDCALL(* pwm_device_set_mode_proc)(PWM_DEVICE *pwm, uint32_t mode)
Definition pwm.h:106
uint32_t STDCALL pwm_type_to_string(uint32_t pwmtype, char *string, uint32_t len)
Convert a PWM type value to a string.
uint32_t STDCALL(* pwm_device_write_proc)(PWM_DEVICE *pwm, uint32_t value)
Definition pwm.h:101
uint32_t STDCALL pwm_mode_to_string(uint32_t pwmmode, char *string, uint32_t len)
Convert a PWM mode value to a string.
uint32_t STDCALL(* pwm_device_set_gpio_proc)(PWM_DEVICE *pwm, uint32_t gpio)
Definition pwm.h:104
uint32_t STDCALL(* pwm_device_get_properties_proc)(PWM_DEVICE *pwm, PWM_PROPERTIES *properties)
Definition pwm.h:116
uint32_t STDCALL pwm_device_set_default(PWM_DEVICE *pwm)
Set the current default PWM device.
uint32_t STDCALL pwm_device_set_polarity(PWM_DEVICE *pwm, uint32_t polarity)
Set the current polarity for the specified PWM device.
uint32_t STDCALL(* pwm_device_set_polarity_proc)(PWM_DEVICE *pwm, uint32_t polarity)
Definition pwm.h:112
uint32_t STDCALL pwm_get_count(void)
Get the current PWM count.
uint32_t STDCALL pwm_device_set_gpio(PWM_DEVICE *pwm, uint32_t gpio)
Set the GPIO pin used by the specified PWM device.
uint32_t STDCALL pwm_device_get_properties(PWM_DEVICE *pwm, PWM_PROPERTIES *properties)
Get the properties for the specified PWM device.
PWM_DEVICE *STDCALL pwm_device_find_by_name(const char *name)
uint32_t STDCALL pwm_device_set_mode(PWM_DEVICE *pwm, uint32_t mode)
Set the current mode for the specified PWM device.
uint32_t STDCALL(* pwm_device_set_frequency_proc)(PWM_DEVICE *pwm, uint32_t frequency)
Definition pwm.h:110
uint32_t STDCALL(* pwm_device_get_range_proc)(PWM_DEVICE *pwm)
Definition pwm.h:107
uint32_t STDCALL(* pwm_device_get_gpio_proc)(PWM_DEVICE *pwm)
Definition pwm.h:103
PWM_DEVICE *STDCALL pwm_device_create_ex(uint32_t size)
Create a new PWM entry.
uint32_t STDCALL(* pwm_device_get_mode_proc)(PWM_DEVICE *pwm)
Definition pwm.h:105
uint32_t STDCALL(* pwm_notification_cb)(DEVICE *device, void *data, uint32_t notification)
Definition pwm.h:95
PWM_DEVICE *STDCALL pwm_device_get_default(void)
Get the current default PWM device.
PWM_DEVICE *STDCALL pwm_device_create(void)
Create a new PWM entry.
uint32_t STDCALL(* pwm_device_set_range_proc)(PWM_DEVICE *pwm, uint32_t range)
Definition pwm.h:108
uint32_t STDCALL pwm_device_get_range(PWM_DEVICE *pwm)
Get the current range of the specified PWM device.
PWM_DEVICE *STDCALL pwm_device_find(uint32_t pwmid)
uint32_t STDCALL(* pwm_device_get_polarity_proc)(PWM_DEVICE *pwm)
Definition pwm.h:111
uint32_t STDCALL(* pwm_device_configure_proc)(PWM_DEVICE *pwm, uint32_t dutyns, uint32_t periodns)
Definition pwm.h:114
struct _PWM_DEVICE PWM_DEVICE
Definition pwm.h:90
uint32_t STDCALL pwm_device_deregister(PWM_DEVICE *pwm)
Deregister a PWM from the PWM table.
uint32_t STDCALL(* pwm_device_start_proc)(PWM_DEVICE *pwm)
Definition pwm.h:98
uint32_t STDCALL pwm_device_register(PWM_DEVICE *pwm)
Register a new PWM in the PWM table.
uint32_t STDCALL pwm_polarity_to_string(uint32_t pwmpolarity, char *string, uint32_t len)
Convert a PWM polarity value to a string.
uint32_t STDCALL pwm_device_set_range(PWM_DEVICE *pwm, uint32_t range)
Set the current range for the specified PWM device.
uint32_t STDCALL pwm_device_stop(PWM_DEVICE *pwm)
Stop the specified PWM device.
uint32_t STDCALL pwm_device_start(PWM_DEVICE *pwm)
Start the specified PWM device.
uint32_t STDCALL(* pwm_enumerate_cb)(PWM_DEVICE *pwm, void *data)
Definition pwm.h:93
struct _PWM_PROPERTIES PWM_PROPERTIES
Definition pwm.h:75
uint32_t STDCALL pwm_device_properties(PWM_DEVICE *pwm, PWM_PROPERTIES *properties)
Get the properties for the specified PWM device.
uint32_t STDCALL pwm_device_get_polarity(PWM_DEVICE *pwm)
Get the current polarity of the specified PWM device.
uint32_t STDCALL pwm_device_set_frequency(PWM_DEVICE *pwm, uint32_t frequency)
Set the clock frequency for the specified PWM device.
PWM_DEVICE *STDCALL pwm_device_check(PWM_DEVICE *pwm)
Check if the supplied PWM is in the PWM table.
uint32_t STDCALL pwm_device_get_mode(PWM_DEVICE *pwm)
Get the current mode of the specified PWM device.
uint32_t STDCALL pwm_device_notification(PWM_DEVICE *pwm, pwm_notification_cb callback, void *data, uint32_t notification, uint32_t flags)
uint32_t STDCALL pwm_device_enumerate(pwm_enumerate_cb callback, void *data)
uint32_t STDCALL pwm_state_to_string(uint32_t pwmstate, char *string, uint32_t len)
Convert a PWM state value to a string.
uint32_t STDCALL pwm_device_destroy(PWM_DEVICE *pwm)
Destroy an existing PWM entry.
uint32_t STDCALL pwm_device_configure(PWM_DEVICE *pwm, uint32_t dutyns, uint32_t periodns)
Set the configuration of the specified PWM device.
Definition pwm.h:119
uint32_t periodns
Period Nanonseconds.
Definition pwm.h:153
uint32_t pwmstate
PWM state (eg PWM_STATE_ENABLED).
Definition pwm.h:124
pwm_device_set_frequency_proc devicesetfrequency
A Device specific DeviceSetFrequency method implementing the standard PWM device interface (Or nil if...
Definition pwm.h:135
pwm_device_set_mode_proc devicesetmode
A Device specific DeviceSetMode method implementing the standard PWM device interface (Or nil if the ...
Definition pwm.h:131
pwm_device_configure_proc deviceconfigure
A Device specific DeviceConfigure method implementing the standard PWM device interface (Mandatory).
Definition pwm.h:138
MUTEX_HANDLE lock
Device lock.
Definition pwm.h:146
uint32_t configcount
Definition pwm.h:144
pwm_device_set_polarity_proc devicesetpolarity
A Device specific DeviceSetPolarity method implementing the standard PWM device interface (Or nil if ...
Definition pwm.h:137
pwm_device_get_polarity_proc devicegetpolarity
A Device specific DeviceGetPolarity method implementing the standard PWM device interface (Or nil if ...
Definition pwm.h:136
PWM_DEVICE * next
Next entry in PWM table.
Definition pwm.h:157
uint32_t writecount
Definition pwm.h:143
uint32_t dutyns
Duty Nanoseconds.
Definition pwm.h:152
DEVICE device
The Device entry for this PWM.
Definition pwm.h:121
pwm_device_start_proc devicestart
A Device specific DeviceStart method implementing the standard PWM device interface (Mandatory).
Definition pwm.h:125
uint32_t mode
Device Mode.
Definition pwm.h:148
uint32_t getcount
Definition pwm.h:141
uint32_t range
Device Range.
Definition pwm.h:149
pwm_device_get_frequency_proc devicegetfrequency
A Device specific DeviceGetFrequency method implementing the standard PWM device interface (Or nil if...
Definition pwm.h:134
pwm_device_get_range_proc devicegetrange
A Device specific DeviceGetRange method implementing the standard PWM device interface (Or nil if the...
Definition pwm.h:132
PWM_PROPERTIES properties
Device properties.
Definition pwm.h:154
uint32_t polarity
Output Polarity.
Definition pwm.h:151
uint32_t setcount
Definition pwm.h:142
PWM_DEVICE * prev
Previous entry in PWM table.
Definition pwm.h:156
pwm_device_write_proc devicewrite
A Device specific DeviceWrite method implementing the standard PWM device interface (Mandatory).
Definition pwm.h:127
uint32_t pwmid
Unique Id of this PWM in the PWM table.
Definition pwm.h:123
pwm_device_set_range_proc devicesetrange
A Device specific DeviceSetRange method implementing the standard PWM device interface (Or nil if the...
Definition pwm.h:133
pwm_device_get_properties_proc devicegetproperties
A Device specific DeviceGetProperties method implementing the standard PWM device interface (Or nil i...
Definition pwm.h:139
uint32_t frequency
Clock Frequency.
Definition pwm.h:150
uint32_t gpio
GPIO Pin.
Definition pwm.h:147
pwm_device_get_mode_proc devicegetmode
A Device specific DeviceGetMode method implementing the standard PWM device interface (Or nil if the ...
Definition pwm.h:130
pwm_device_set_gpio_proc devicesetgpio
A Device specific DeviceSetGPIO method implementing the standard PWM device interface (Or nil if the ...
Definition pwm.h:129
pwm_device_stop_proc devicestop
A Device specific DeviceStop method implementing the standard PWM device interface (Mandatory).
Definition pwm.h:126
pwm_device_get_gpio_proc devicegetgpio
A Device specific DeviceGetGPIO method implementing the standard PWM device interface (Or nil if the ...
Definition pwm.h:128
Definition pwm.h:77
uint32_t periodns
Definition pwm.h:85
uint32_t dutyns
Definition pwm.h:84
uint32_t minperiod
Definition pwm.h:86
uint32_t mode
Definition pwm.h:80
uint32_t range
Definition pwm.h:81
uint32_t flags
Device flags (eg PWM_FLAG_MODE).
Definition pwm.h:78
uint32_t polarity
Definition pwm.h:83
uint32_t frequency
Definition pwm.h:82
uint32_t gpio
Definition pwm.h:79