Skip to content

Commit

Permalink
hw/pixtend: Add support for PWM1
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Buesch <[email protected]>
  • Loading branch information
mbuesch committed Sep 8, 2018
1 parent f279b53 commit f43a7c4
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 51 deletions.
55 changes: 53 additions & 2 deletions awlsimhw_pixtend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,44 @@ def __build(self):
# Build all PWM() objects for PWM1
self.__PWM1s = []
if self.__isV2:
pass#TODO
for i in range(self.NR_PWM1):
pwmName = "AB"[i]
oper = self.getParamValueByName("pwm1%s_addr" % pwmName)
if oper is None:
continue
bitOffset = oper.offset.toLongBitOffset()
pwm = PWM1(self.__pixtend, self.__isV2, i, bitOffset,
not self.isInProcessImage(oper.offset, 16, True))
self.__PWM1s.append(pwm)
pwm.enabled = True
mode = self.getParamValueByName("pwm1_mode")
pwm.servoMode = (mode == HwParamDesc_pwmMode.MODE_SERVO)
# Handle pwm1_period parameter.
try:
maxPeriod = 255
oper = self.getParamValueByName("pwm1_period")
if not oper:
# Use default constant pwm1_period
PWM1Period(self.__pixtend, self.__isV2, 0, 0).setPWMPeriod(maxPeriod)
else:
if oper.operType == AwlOperatorTypes.IMM:
# Use custom constant pwm1_period
period = oper.immediate
if period < 0 or period > maxPeriod:
raise ValueError
PWM1Period(self.__pixtend, self.__isV2, 0, 0).setPWMPeriod(period)
elif oper.operType == AwlOperatorTypes.MEM_A and\
oper.width == 16:
# Use custom dynamic pwm1_period
bitOffset = oper.offset.toLongBitOffset()
pwm = PWM1Period(self.__pixtend, self.__isV2, 0, bitOffset,
not self.isInProcessImage(oper.offset, 16, True))
self.__PWM1s.append(pwm)
pwm.setPWMPeriod(0)
else:
raise ValueError
except ValueError as e:
self.raiseException("Unsupported 'pwm1_period' parameter value.")

# Build a list of all outputs
self.__allOutputs = []
Expand Down Expand Up @@ -687,7 +724,21 @@ def calcFirstLastByte(IOs):

if self.__isV2:
# Configure global values of PWM1.
pass#TODO
try:
if self.__PWM1s:
PWM1.setServoMode(self.__pixtend,
self.__PWM1s[0].servoMode)
except ValueError as e:
self.raiseException("Unsupported 'pwm1_mode' parameter value.")
try:
freqHz = self.getParamValueByName("pwm1_baseFreqHz")
if not self.__PWM1s:
freqHz = 0
PWM1Period.setBaseFreq(self.__pixtend, freqHz)
except ValueError as e:
self.raiseException("Unsupported 'pwm1_baseFreqHz' parameter value. "
"Supported values are: 16000000, 2000000, "
"500000, 250000, 125000, 62500, 15625, 0.")

def __tryConnect(self, boardType, timeout=5.0):
"""Try to connect to the PiXtend board.
Expand Down
28 changes: 11 additions & 17 deletions awlsimhw_pixtend/ppl_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,12 @@ def setBaseFreq(pixtend, freqHz):
cs2, cs1, cs0 = csMap[freqHz]
except KeyError as e:
raise ValueError
if isV2:
ctrl0 = pixtend.pwm1_ctrl0
ctrl0 &= ~((1 << 5) | (1 << 6) | (1 << 7))
ctrl0 |= (cs0 << 5) | (cs1 << 6) | (cs2 << 7)
if freqHz == 0:
ctrl0 &= ~((1 << 3) | (1 << 4)) # Disable A and B
pixtend.pwm1_ctrl0 = ctrl0 & 0xFF
else:
assert(0)
ctrl0 = pixtend.pwm1_ctrl0
ctrl0 &= ~((1 << 5) | (1 << 6) | (1 << 7))
ctrl0 |= (cs0 << 5) | (cs1 << 6) | (cs2 << 7)
if freqHz == 0:
ctrl0 &= ~((1 << 3) | (1 << 4)) # Disable A and B
pixtend.pwm1_ctrl0 = ctrl0 & 0xFF

class PWM1(AbstractWordIO): #+cdef
"""PiXtend PWM1 output I/O handler. (v2.x only)
Expand Down Expand Up @@ -939,14 +936,11 @@ def __setPWMB(self, value):

@staticmethod
def setServoMode(pixtend, enServoMode):
if isV2:
ctrl0 = pixtend.pwm1_ctrl0
ctrl0 &= ~((1 << 0) | (1 << 1))
if not enServoMode:
ctrl0 |= 1 << 0
pixtend.pwm1_ctrl0 = ctrl0
else:
assert(0)
ctrl0 = pixtend.pwm1_ctrl0
ctrl0 &= ~((1 << 0) | (1 << 1))
if not enServoMode:
ctrl0 |= 1 << 0
pixtend.pwm1_ctrl0 = ctrl0

def __doSetEnabled(self, enabled, bitNr):
self.enabled = enabled
Expand Down
Loading

0 comments on commit f43a7c4

Please sign in to comment.