Skip to content

Commit

Permalink
feature: added machine.PWM class
Browse files Browse the repository at this point in the history
  • Loading branch information
boochow committed Oct 12, 2018
1 parent 3447044 commit bca5cd8
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 0 deletions.
23 changes: 23 additions & 0 deletions raspberrypi/bcm283x_clockmgr.c
@@ -0,0 +1,23 @@
#include <stdint.h>
#include "bcm283x_clockmgr.h"

void clockmgr_pause(clockmgr_t *clk) {
if (clk->CTL & CM_CTL_ENAB) {
clockmgr_set_ctl(clk, (clk->CTL & ~CM_CTL_ENAB));
while(clk->CTL & CM_CTL_BUSY) {};
}
}

void clockmgr_config_ctl(clockmgr_t *clk, int32_t flags) {
clockmgr_pause(clk);
clk->CTL = CM_PASSWORD | flags;
}

void clockmgr_config_div(clockmgr_t *clk, int32_t divi, int32_t divf) {
uint32_t save = clk->CTL;
clockmgr_pause(clk);
clockmgr_set_div(clk, divi, divf);
if (save & CM_CTL_ENAB) {
clockmgr_set_ctl(clk, save);
}
}
53 changes: 53 additions & 0 deletions raspberrypi/bcm283x_clockmgr.h
@@ -0,0 +1,53 @@
#ifndef MICROPY_INCLUDED_RPI_BCM283X_CLOCKMGR_H
#define MICROPY_INCLUDED_RPI_BCM283X_CLOCKMGR_H

#define CM_GP0 (IO_BASE + 0x101070)
#define CM_GP1 (IO_BASE + 0x101078)
#define CM_GP2 (IO_BASE + 0x101080)
#define CM_PCM (IO_BASE + 0x101098)
#define CM_PWM (IO_BASE + 0x1010a0)

typedef volatile struct _clockmgr_t {
uint32_t CTL;
uint32_t DIV;
} clockmgr_t;


#define CM_PASSWORD (0x5a000000)

#define CM_CTL_MASH_MASK (3U<<9)
#define CM_CTL_MASH_IDIV (0U<<9)
#define CM_CTL_MASH_1STG (1U<<9)
#define CM_CTL_MASH_2STG (2U<<9)
#define CM_CTL_MASH_3STG (3U<<9)

#define CM_CTL_FLIP (1U<<8)
#define CM_CTL_BUSY (1U<<7)
#define CM_CTL_KILL (1U<<5)
#define CM_CTL_ENAB (1U<<4)

#define CM_CTL_SRC_MASK (0xfU)
#define CM_CTL_SRC_GND (0U)
#define CM_CTL_SRC_OSC (1U)
#define CM_CTL_SRC_PLLA (4U)
#define CM_CTL_SRC_PLLC (5U)
#define CM_CTL_SRC_PLLD (6U)
#define CM_CTL_SRC_HDMI (7U)

#define CM_DIV_MASK (0x00ffffffU)

__attribute__(( always_inline )) inline void clockmgr_set_ctl(clockmgr_t *clk, uint32_t ctl) {
clk->CTL = CM_PASSWORD | (ctl & 0xffffff);
}

__attribute__(( always_inline )) inline void clockmgr_set_div(clockmgr_t *clk, uint32_t divi, uint32_t divf) {
clk->DIV = CM_PASSWORD | (divi & 0xfff) << 12 | (divf & 0xfff);
}

void clockmgr_pause(clockmgr_t *clk);

void clockmgr_config_ctl(clockmgr_t *clk, int32_t flags);

void clockmgr_config_div(clockmgr_t *clk, int32_t divi, int32_t divf);

#endif // MICROPY_INCLUDED_RPI_BCM283X_CLOCKMGR_H
53 changes: 53 additions & 0 deletions raspberrypi/bcm283x_pwm.h
@@ -0,0 +1,53 @@
#ifndef MICROPY_INCLUDED_RPI_BCM283X_PWM_H
#define MICROPY_INCLUDED_RPI_BCM283X_PWM_H

#define PWM (IO_BASE + 0x20C000)

typedef volatile struct _pwm_t {
uint32_t CTL;
uint32_t STA;
uint32_t DMAC;
uint32_t undef1;
uint32_t RNG1;
uint32_t DAT1;
uint32_t FIF1;
uint32_t undef2;
uint32_t RNG2;
uint32_t DAT2;
} pwm_t;

#define CTL_MSEN2 (1<<15)
#define CTL_USEF2 (1<<13)
#define CTL_POLA2 (1<<12)
#define CTL_SBIT2 (1<<11)
#define CTL_RPTL2 (1<<10)
#define CTL_MODE2 (1<<9)
#define CTL_PWEN2 (1<<8)
#define CTL_MSEN1 (1<<7)
#define CTL_CLRF1 (1<<6)
#define CTL_USEF1 (1<<5)
#define CTL_POLA1 (1<<4)
#define CTL_SBIT1 (1<<3)
#define CTL_RPTL1 (1<<2)
#define CTL_MODE1 (1<<1)
#define CTL_PWEN1 (1)

#define STA_STA4 (1<<12)
#define STA_STA3 (1<<11)
#define STA_STA2 (1<<10)
#define STA_STA1 (1<<9)
#define STA_BERR (1<<8)
#define STA_GAPO4 (1<<7)
#define STA_GAPO3 (1<<6)
#define STA_GAPO2 (1<<5)
#define STA_GAPO1 (1<<4)
#define STA_RERR1 (1<<3)
#define STA_WERR1 (1<<2)
#define STA_EMPT1 (1<<1)
#define STA_FULL1 (1)

#define DMAC_ENAB (1<<31)
#define DMAC_PANIC (255<<8)
#define DMAC_DREQ (255)

#endif // MICROPY_INCLUDED_RPI_BCM283X_PWM_H

0 comments on commit bca5cd8

Please sign in to comment.