Skip to content

Commit

Permalink
Added setPWMReg.py
Browse files Browse the repository at this point in the history
  • Loading branch information
clausqr committed Jun 14, 2012
1 parent 2921539 commit d8b59f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Binary file removed Debug/cPWM
Binary file not shown.
2 changes: 2 additions & 0 deletions cPWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace cPWM {
//Constructor, n is the id of the PWMSS
cPWM::cPWM(int id)
{
///TODO: Add clock selection (mmap). By now you must use setPWMReg.py method
///FIXME: pin mux settings should be done here? or at a highet level?
std::cout << "called cPWM constructor" << std::endl;
cPWM::id = id;

Expand Down
32 changes: 32 additions & 0 deletions setPWMReg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/python
# Enable PWM Timer on Beaglebone
from mmap import mmap
import struct
MMAP_OFFSET = 0x44c00000 # base address of registers
MMAP_SIZE = 0x48ffffff-MMAP_OFFSET # size of the register memory space
CM_PER_BASE = 0x44e00000 - MMAP_OFFSET
CM_PER_EPWMSS1_CLKCTRL = CM_PER_BASE + 0xcc
CM_PER_EPWMSS0_CLKCTRL = CM_PER_BASE + 0xd4
CM_PER_EPWMSS2_CLKCTRL = CM_PER_BASE + 0xd8
with open("/dev/mem", "r+b") as f:
mem = mmap(f.fileno(), MMAP_SIZE, offset=MMAP_OFFSET)
def _andReg(address, mask):
""" Sets 32-bit Register at address to its current value AND mask. """
_setReg(address, _getReg(address)&mask)
def _orReg(address, mask):
""" Sets 32-bit Register at address to its current value OR mask. """
_setReg(address, _getReg(address)|mask)
def _xorReg(address, mask):
""" Sets 32-bit Register at address to its current value XOR mask. """
_setReg(address, _getReg(address)^mask)
def _getReg(address):
""" Returns unpacked 32 bit register value starting from address. """
return struct.unpack("<L", mem[address:address+4])[0]
def _setReg(address, new_value):
""" Sets 32 bits at given address to given value. """
mem[address:address+4] = struct.pack("<L", new_value)
val = _getReg(CM_PER_EPWMSS1_CLKCTRL)
print "Register CM_PER_EPWMSS1_CLKCTRL was " + hex(val)
_setReg(CM_PER_EPWMSS1_CLKCTRL, 0x2)
val = _getReg(CM_PER_EPWMSS1_CLKCTRL)
print "Register CM_PER_EPWMSS1_CLKCTRL changed to " + hex(val)

0 comments on commit d8b59f2

Please sign in to comment.