Skip to content

Commit

Permalink
Update PWM support
Browse files Browse the repository at this point in the history
* Should fix #156 with updated kernel to provide the needed udev events
* Eliminates some overly specific logic
* Still needs some bone.js updates to address 'univeralName' requirements to discover pinmuxes or some alternate discovery mechanism
  • Loading branch information
Jason Kridner committed Dec 20, 2017
1 parent 1ea979f commit 49f1495
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ var pinIndex = [
"addr": "48302200"
},
"key": ["P9_14", "P2_1"],
"universalName": ["ocp:P9_14_pinmux", "ocp:P2_01_pinmux", "ocp:PWM_pinmux"],
"muxRegOffset": "0x048",
"options": [
"gpmc_a2",
Expand Down Expand Up @@ -1748,7 +1749,7 @@ var pinIndex = [
"muxmode": 4,
"path": "ecap.2",
"name": "ECAPPWM2",

"universalMode": "pwm2",
"chip": "48304000",
"addr": "48304100"
},
Expand Down
53 changes: 29 additions & 24 deletions src/hw_mainline.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,46 @@ exports.setPinMode = function(pin, pinData, template, resp, callback) {
}
var pinmux = my.find_sysfsFile(p, my.is_ocp(), pin.universalName);
gpioFile[pin.key] = '/sys/class/gpio/gpio' + pin.gpio + '/value';
if(pinmux) { // This is a hack for the new pins on the Blue that appear not to have a pinmux
if(pinmux) {
if((pinData & 7) == 7) {
fs.writeFileSync(pinmux+"/state", 'gpio');
} else if(template == 'bspwm') {
// at least P9_28 uses pwm2
var state = pin.key.indexOf("P9_28") == -1 ? "pwm" : "pwm2";
var state = "pwm";
if(pin.pwm.universalMode) state = pin.pwm.universalMode;
fs.writeFileSync(pinmux+"/state", state);
// Buld a path like: /sys/devices/platform/ocp/48304000.epwmss/48304200.ehrpwm/pwm/pwmchip5
// pin.pwm.chip looks up the first address and pin.pwm.addr looks up the second
// file_find figures which pwmchip to use
// pin.pwm.index tells with half of the PWM to use (0 or 1)
// prefix is .ecap for P9_28 and P9_42 and .pwm or .ehrpwm for the rest
var prefix = pin.pwm.module.indexOf("ecap") == -1 ? ".pwm" : ".ecap";
var pwmPath = my.file_find('/sys/devices/platform/ocp/'+pin.pwm.chip
+ '.epwmss/'+pin.pwm.addr+prefix+'/pwm', 'pwmchip', 1);
// Some versions of kernel (4.4.15-bone11 for instance) still use
// .ehrpwm for address
if (pwmPath == null) {
pwmPath = my.file_find('/sys/devices/platform/ocp/'+pin.pwm.chip
+ '.epwmss/'+pin.pwm.addr+'.ehrpwm/pwm', 'pwmchip', 1)
}
pwmPrefix[pin.pwm.name] = pwmPath + '/pwm' + pin.pwm.index;
if(debug) winston.debug("pwmPrefix[pin.pwm.name] = " + pwmPrefix[pin.pwm.name]);
if(debug) winston.debug("pin.pwm.sysfs = " + pin.pwm.sysfs);
if(!my.file_existsSync(pwmPrefix[pin.pwm.name])) {
fs.appendFileSync(pwmPath+'/export', pin.pwm.index);
}
fs.appendFileSync(pwmPrefix[pin.pwm.name]+'/enable', 1);
} else {
resp.err = 'Unknown pin mode template';
winston.error(resp.err);
}
} else {
resp.err = 'No pinmux for ' + pin.key;
winston.error(resp.err);
}

if(template == 'bspwm') {
// Buld a path like: /sys/devices/platform/ocp/48304000.epwmss/48304200.ehrpwm/pwm/pwmchip5/pwm5-:0
// pin.pwm.chip looks up the first address and pin.pwm.addr looks up the second
// file_find figures which pwmchip to use
// pin.pwm.index tells with half of the PWM to use (0 or 1)
var chipPath = my.file_find('/sys/devices/platform/ocp', pin.pwm.chip, 1);
if(debug) winston.debug("chipPath = " + chipPath);
var addrPath = my.file_find(chipPath, pin.pwm.addr, 1);
if(debug) winston.debug("addrPath = " + addrPath);
var pwmchipPath = my.file_find(addrPath+'/pwm', 'pwmchip', 1);
if(debug) winston.debug("pwmchipPath = " + pwmchipPath);
var pwmPath = my.file_find(pwmchipPath, "pwm.*"+pin.pwm.index+"$", 1);
if(debug) winston.debug("pwmPath = " + pwmPath);
if(pwmchipPath && !pwmPath) {
fs.appendFileSync(pwmchipPath+'/export', pin.pwm.index);
pwmPath = my.file_find(pwmchipPath, "pwm.*"+pin.pwm.index+"$", 1);
if(debug) winston.debug("pwmPath = " + pwmPath);
}
if(pwmPath) {
pwmPrefix[pin.pwm.name] = pwmPath;
}
fs.appendFileSync(pwmPrefix[pin.pwm.name]+'/enable', 1);
}

if(callback) callback(resp);
return(resp);
};
Expand Down
4 changes: 2 additions & 2 deletions src/my.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ exports.file_find = function(path, prefix, attempts, callback) {
for(var j in files) {
if(Array.isArray(prefix)) {
for(var k=0; k<prefix.length; k++) {
if(files[j].indexOf(prefix[k]) === 0) {
if(files[j].search(prefix[k]) == 0) {
resp.path = path + '/' + files[j];
if(callback) callback(resp);
return;
}
}
} else {
if(files[j].indexOf(prefix) === 0) {
if(files[j].search(prefix) === 0) {
resp.path = path + '/' + files[j];
if(callback) callback(resp);
return;
Expand Down

0 comments on commit 49f1495

Please sign in to comment.