PWM¶
Note
The Datasheet specifies PWM channels 0 and 1. The Raspberry Pi has pins for PWM channels 1 and 2, you just need to add one.
Macros¶
-
PWM_OFFSET
¶ 0x20C000
This macro defines the offset at which the PWM registers are located from the peripheral base.
-
PWM_SIZE
¶ 0x24
This macro holds the size of the I2C registers which needs to be mapped.
-
RNG_CHANNEL0
¶
-
DAT_CHANNEL0
¶
-
RNG_CHANNEL1
¶
-
DAT_CHANNEL1
¶ #define RNG_CHANNEL0 PWM->RNG1 #define DAT_CHANNEL0 PWM->DAT1 #define RNG_CHANNEL1 PWM->RNG2 #define DAT_CHANNEL1 PWM->DAT2
To prevent confusion (because the Datasheet calls the PWM channels 1 and 2 and the Raspberry Pi 0 and 1) the values of the registers which need to be used “on the fly” are :code;`defined` from 2 to 1 and from 1 to 0.
Configuration Macros¶
PWM_CTL_MODE_PWM |
Use PWM mode |
PWM_CTL_MODE_SERIALISER |
Use serialiser mode |
PWM_RPTL_STOP |
If serialiser mode: Transmission stops when fifo empty |
PWM_RPTL_REPEAT |
If serialiser mode: Repeat last data when fifo empty |
PWM_SBIT_LOW |
Output low when no transmission active |
PWM_SBIT_HIGH |
Output high when no transmission active |
PWM_POLA_DEFAULT |
Polarity is default |
PWM_POLA_INVERTED |
Polarity is innverted |
PWM_USEF_DATA |
Data register is transmitted |
PWM_USEF_FIFO |
Data from fifo is transmitted |
PWM_MSEN_PWMALGORITHM |
Use PWM algorithm |
PWM_MSEN_MSRATIO |
Use MS ratio |
Registers¶
-
volatile uint32_t *
pwm_base_ptr
¶ This pointer points, when mapped, to the base of the PWM registers.
-
struct
pwm_register_map
¶ This struct maps the registers of the PWM. The names of the struct members correspond to the registers from the Datasheet:
struct pwm_register_map { uint32_t CTL; uint32_t STA; uint32_t DMAC; uint32_t: 32; uint32_t RNG1; uint32_t DAT1; uint32_t FIF1; uint32_t: 32; uint32_t RNG2; uint32_t DAT2; };
-
PWM
¶ #define PWM ((volatile struct pwm_register_map *)pwm_base_ptr)
By using this macro, the registers of the PWM can be accessed like this
PWM->RNG1
.
Enums¶
-
pwm_channel_t
¶ This enum holds the values distinguishing PWM channel 0 and 1:
typedef enum { PWM_CHANNEL0, PWM_CHANNEL1 } pwm_channel_t;
Structs¶
-
pwm_channel_config_t
¶ This struct is used to configure a PWM channel:
typedef struct { union { struct { uint32_t: 1; uint32_t mode: 1; uint32_t rptl: 1; uint32_t sbit: 1; uint32_t pola: 1; uint32_t usef: 1; uint32_t: 1; uint32_t msen: 1; }; uint32_t ctl_register; }; unsigned int divisor; uint32_t range; } pwm_channel_config_t;
-
uint32_t
ctl_register
¶ This member can be directly edited by the anonymous struct inside this union. This register maps directly to the
CTL
register, with some offset for PWM 1. The settings of this register are described in the Macros.
-
unsigned int
divisor
¶ The divisor which is passed to the Clock Manager.
-
uint32_t
range
¶ The range to which the PWM counter counts before it starts over.
-
uint32_t
Functions¶
-
uint32_t *
pwm_map
(void)¶ This function maps the PWM registers. It calls
peripheral_map()
with the valuesPWM_OFFSET
andPWM_SIZE
.
-
void
pwm_unmap
(void)¶ This function unmaps the PWM registers.
-
void
pwm_configure
(pwm_channel_t channel, pwm_channel_config_t *config)¶ This function configures
pwm_channel_t
channel
with apwm_channel_config_t
pointed to byconfig
.
-
void
pwm_enable
(pwm_channel_t channel)¶ This function enables
pwm_channel_t
channel
.
-
void
pwm_disable
(pwm_channel_t channel)¶ This function disables
pwm_channel_t
channnel
.