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

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.

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;
};
extern volatile struct pwm_register_map *PWM
PWM = (volatile struct pwm_register_map *)pwm_base_ptr;

By using this variable, the registers of the PWM can be accessed like this PWM->RNG1.

Enums

PWM channel number

This enum holds the values distinguishing PWM channel 0 and 1:

enum {
    PWM_CHANNEL0, PWM_CHANNEL1
};

Structs

pwm_channel_config

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;
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.

Functions

int pwm_map(void)

This function maps the PWM registers. It calls peripheral_map() with the values PWM_OFFSET and PWM_SIZE. On error -1 is returned.

void pwm_unmap(void)

This function unmaps the PWM registers.

void pwm_configure(int channel, pwm_channel_config *config)

This function configures channel with a pwm_channel_config pointed to by config.

void pwm_enable(int channel)

This function enables channel.

void pwm_disable(int channel)

This function disables channnel.

Configuration Values

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