PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches

PWM controller class. More...

Modules

 nRF5 PWM
 PWM controller driver for the nRF5 PWM peripheral.
 
 nRF5 RTC PWM
 PWM controller driver generating a PWM output with an nRF5 RTC.
 
 SF32LB LPTIM PWM
 PWM controller driver for the SF32LB low-power timers.
 

Data Structures

struct  pbl_pwm_state
 Output state. More...
 
struct  pbl_pwm
 A channel, as used by a consumer. More...
 
struct  pbl_pwm_controller_ops
 PWM controller driver operations. More...
 
struct  pbl_pwm_controller
 A PWM controller. More...
 

Macros

#define PBL_PWM_USEC(us)   ((us) * (uint32_t)PBL_NSEC_PER_USEC)
 us microseconds, in nanoseconds.
 
#define PBL_PWM_MSEC(ms)   ((ms) * (uint32_t)PBL_NSEC_PER_MSEC)
 ms milliseconds, in nanoseconds.
 
#define PBL_PWM_HZ(hz)   ((uint32_t)PBL_NSEC_PER_SEC / (hz))
 Period of a hz Hz output, in nanoseconds.
 
#define PBL_PWM_POLARITY_INVERTED   PBL_BIT(0)
 Spec flag: the output is inverted.
 
#define PBL_PWM(_ctrl, _channel, _period, _flags)    {.ctrl = (_ctrl), .channel = (_channel), .period = (_period), .flags = (_flags)}
 Initializer for a struct pbl_pwm.
 
#define PBL_PWM_CONTROLLER_STATE_DEFINE(sym, _num_channels)
 Define the class state of PWM controller sym, for the driver's define macro.
 
#define PBL_PWM_CONTROLLER_INIT(sym, _name, _init, _parent, _ops)
 Initializer for the struct pbl_pwm_controller of controller sym.
 

Enumerations

enum  pbl_pwm_polarity { PBL_PWM_POLARITY_NORMAL , PBL_PWM_POLARITY_INVERSED }
 Output polarity. More...
 

Functions

void pbl_pwm_get_state (const struct pbl_pwm *pwm, struct pbl_pwm_state *state)
 Read the last applied state of a channel; disabled before the first one.
 
void pbl_pwm_init_state (const struct pbl_pwm *pwm, struct pbl_pwm_state *state)
 Prepare a state from the channel's spec.
 
int pbl_pwm_set_relative_duty_cycle (struct pbl_pwm_state *state, uint32_t duty, uint32_t scale)
 Set the duty cycle of a state to duty / scale of its period.
 
int pbl_pwm_apply (const struct pbl_pwm *pwm, const struct pbl_pwm_state *state)
 Apply a state to a channel.
 
int pbl_pwm_disable (const struct pbl_pwm *pwm)
 Disable a channel, keeping the rest of its state.
 

Variables

const struct pbl_device_class pbl_pwm_class
 Class of the PWM controllers.
 

Detailed Description

PWM controller class.

A PWM controller is a struct pbl_pwm_controller device with numbered channels. A consumer holds a struct pbl_pwm, what a devicetree pwms entry describes: controller, channel, period and flags. As in Linux, the output is set by applying a whole state at once:

static const struct pbl_pwm s_pwm = PBL_PWM(&s_pwm0.ctrl, 0, PBL_PWM_HZ(256), 0);
struct pbl_pwm_state state;
pbl_pwm_init_state(&s_pwm, &state);
state.enabled = true;
ret = pbl_pwm_apply(&s_pwm, &state);
A channel, as used by a consumer.
Definition pwm.h:77
Output state.
Definition pwm.h:65
#define PBL_PWM_HZ(hz)
Period of a hz Hz output, in nanoseconds.
Definition pwm.h:51
void pbl_pwm_init_state(const struct pbl_pwm *pwm, struct pbl_pwm_state *state)
Prepare a state from the channel's spec.
int pbl_pwm_apply(const struct pbl_pwm *pwm, const struct pbl_pwm_state *state)
Apply a state to a channel.
int pbl_pwm_set_relative_duty_cycle(struct pbl_pwm_state *state, uint32_t duty, uint32_t scale)
Set the duty cycle of a state to duty / scale of its period.
#define PBL_PWM(_ctrl, _channel, _period, _flags)
Initializer for a struct pbl_pwm.
Definition pwm.h:96

Times are in nanoseconds. Controllers round the period and the duty cycle down to what they can generate. A disabled output stays at its inactive level, low for the normal polarity. A channel belongs to one consumer; calls are made from task context.


Data Structure Documentation

◆ pbl_pwm_state

struct pbl_pwm_state

Output state.

Data Fields
uint32_t duty_cycle Active time per period, in nanoseconds, up to period.
bool enabled Whether the output runs; when false the output is inactive.
uint32_t period Period, in nanoseconds.
enum pbl_pwm_polarity polarity Polarity.

◆ pbl_pwm

struct pbl_pwm

A channel, as used by a consumer.

Data Fields
uint8_t channel Channel number, from 0.
const struct pbl_pwm_controller * ctrl Controller.
uint32_t flags Flags, PBL_PWM_POLARITY_INVERTED.
uint32_t period Default period, in nanoseconds.

◆ pbl_pwm_controller

struct pbl_pwm_controller

A PWM controller.

Data Fields
struct pbl_device dev Device.
uint8_t num_channels Number of channels.
const struct pbl_pwm_controller_ops * ops Driver operations.
struct pbl_pwm_state * states Last applied state of each channel.

Macro Definition Documentation

◆ PBL_PWM

#define PBL_PWM (   _ctrl,
  _channel,
  _period,
  _flags 
)     {.ctrl = (_ctrl), .channel = (_channel), .period = (_period), .flags = (_flags)}

Initializer for a struct pbl_pwm.

Parameters
_ctrlController.
_channelChannel number, from 0.
_periodDefault period in nanoseconds, e.g. from PBL_PWM_HZ().
_flagsFlags, PBL_PWM_POLARITY_INVERTED or 0.

◆ PBL_PWM_CONTROLLER_INIT

#define PBL_PWM_CONTROLLER_INIT (   sym,
  _name,
  _init,
  _parent,
  _ops 
)
Value:
{ \
.dev = PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, &pbl_pwm_class), \
.ops = (_ops), \
.states = sym##_pwm_states, \
.num_channels = sizeof(sym##_pwm_states) / sizeof(sym##_pwm_states[0]), \
}
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153
const struct pbl_device_class pbl_pwm_class
Class of the PWM controllers.

Initializer for the struct pbl_pwm_controller of controller sym.

Parameters
symSymbol of the controller instance, with its state defined by PBL_PWM_CONTROLLER_STATE_DEFINE().
_nameName.
_initDevice init function, or NULL.
_parentParent device, or NULL.
_opsDriver operations.

◆ PBL_PWM_CONTROLLER_STATE_DEFINE

#define PBL_PWM_CONTROLLER_STATE_DEFINE (   sym,
  _num_channels 
)
Value:
static struct pbl_pwm_state sym##_pwm_states[_num_channels]
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132

Define the class state of PWM controller sym, for the driver's define macro.

Parameters
symSymbol of the controller instance.
_num_channelsNumber of channels.

◆ PBL_PWM_HZ

#define PBL_PWM_HZ (   hz)    ((uint32_t)PBL_NSEC_PER_SEC / (hz))

Period of a hz Hz output, in nanoseconds.

◆ PBL_PWM_MSEC

#define PBL_PWM_MSEC (   ms)    ((ms) * (uint32_t)PBL_NSEC_PER_MSEC)

ms milliseconds, in nanoseconds.

◆ PBL_PWM_POLARITY_INVERTED

#define PBL_PWM_POLARITY_INVERTED   PBL_BIT(0)

Spec flag: the output is inverted.

◆ PBL_PWM_USEC

#define PBL_PWM_USEC (   us)    ((us) * (uint32_t)PBL_NSEC_PER_USEC)

us microseconds, in nanoseconds.

Enumeration Type Documentation

◆ pbl_pwm_polarity

Output polarity.

Enumerator
PBL_PWM_POLARITY_NORMAL 

High for the duty cycle, then low.

PBL_PWM_POLARITY_INVERSED 

Low for the duty cycle, then high.

Function Documentation

◆ pbl_pwm_apply()

int pbl_pwm_apply ( const struct pbl_pwm *  pwm,
const struct pbl_pwm_state *  state 
)

Apply a state to a channel.

Parameters
pwmChannel.
stateState.
Returns
0, -EINVAL for a state the controller cannot generate, or another negative errno.

◆ pbl_pwm_disable()

int pbl_pwm_disable ( const struct pbl_pwm *  pwm)

Disable a channel, keeping the rest of its state.

Parameters
pwmChannel.
Returns
0 or a negative errno.

◆ pbl_pwm_get_state()

void pbl_pwm_get_state ( const struct pbl_pwm *  pwm,
struct pbl_pwm_state *  state 
)

Read the last applied state of a channel; disabled before the first one.

Parameters
pwmChannel.
[out]stateState.

◆ pbl_pwm_init_state()

void pbl_pwm_init_state ( const struct pbl_pwm *  pwm,
struct pbl_pwm_state *  state 
)

Prepare a state from the channel's spec.

Takes the last applied state, with the period and polarity of the spec and no duty cycle.

Parameters
pwmChannel.
[out]stateState.

◆ pbl_pwm_set_relative_duty_cycle()

int pbl_pwm_set_relative_duty_cycle ( struct pbl_pwm_state *  state,
uint32_t  duty,
uint32_t  scale 
)

Set the duty cycle of a state to duty / scale of its period.

Parameters
stateState, with its period set.
dutyDuty cycle, up to scale.
scaleScale.
Returns
0, or -EINVAL if scale is 0 or duty is above it.

Variable Documentation

◆ pbl_pwm_class

const struct pbl_device_class pbl_pwm_class
extern

Class of the PWM controllers.