From 53d4dc58d9e8e85a6777b418b7d23b6736aca89a Mon Sep 17 00:00:00 2001 From: vaishnav98 Date: Sat, 26 May 2018 06:49:00 +0530 Subject: [PATCH 1/3] add draft pru support(sync only) --- src/index.js | 4 ++ src/pru.js | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/pru.js diff --git a/src/index.js b/src/index.js index 66f41dc6..0d386c38 100644 --- a/src/index.js +++ b/src/index.js @@ -18,6 +18,7 @@ var autorun = require('./autorun'); var server = require('./server'); var socketHandlers = require('./socket_handlers'); var ffi = require('./ffiimp'); +var pru = require('./pru'); //var rc = require('./rc'); var debug = process.env.DEBUG ? true : false; @@ -646,6 +647,9 @@ for (var x in socketHandlers) { for (var x in ffi) { f[x] = ffi[x]; } +for (var x in pru) { + f[x] = pru[x]; +} /*for(var x in rc) { exports[x] = rc[x]; }*/ diff --git a/src/pru.js b/src/pru.js new file mode 100644 index 00000000..bbbc5f4c --- /dev/null +++ b/src/pru.js @@ -0,0 +1,102 @@ +var fs = require('fs'); +var shell = require('shelljs'); +var winston = require('winston'); + +var debug = process.env.DEBUG ? true : false; + +var rproc_path = '/sys/class/remoteproc/remoteproc'; +var fw_path = '/lib/firmware/am335x-pru'; + +//modprobe/unprobe +var modProbe = function (mod, probe) { + mod = mod || 'pru_rproc'; //default pru_rproc + probe = typeof probe !== 'undefined' ? probe : true; + if (probe) + var cmd = 'modprobe ' + mod; //for modprobe + else + var cmd = 'modprobe -r ' + mod; //for modunprobe + if (debug) winston.debug('running modprobe'); + code = shell.exec(cmd).code; + if (code > 0) + winston.error("modprobe/modunprobe failed"); +} +//pru enable/disable +var pruEnable = function (pruno, enable) { + var pruno = typeof pruno !== 'undefined' ? pruno : '0'; + if (pruno != 0 && pruno != 1) { + winston.error('PRU Core No. should be 0 or 1'); + return false; + } else + pruno += 1; + var path = rproc_path + pruno + '/state'; + var enable = typeof enable !== 'undefined' ? enable : true; + var currentState = (fs.readFileSync(path).toString()); + var state; + if (currentState == 'offline\n' && enable) state = 'start\n'; //start only if offline + else if (currentState == 'running\n' && !enable) state = 'stop\n'; //stop only if running + if (typeof state != 'undefined') { + if (debug) winston.debug('Changing PRU' + pruno + 'state from' + currentState); + fs.writeFileSync(path, state); + } +} +//pru reset +var pruReset = function (pruno) { + if (pruno != 0 && pruno != 1) { + winston.error('PRU Core No. should be 0 or 1'); + return false; + } + pruEnable(pruno, false); + pruEnable(pruno, true); +} +// loadFirmware +var loadPRUFirmware = function (filepath, pruno) { + var pruno = typeof pruno !== 'undefined' ? pruno : '0'; + if (pruno != 0 && pruno != 1) { + winston.error('PRU Core No. should be 0 or 1'); + return false; + } + if (!(fs.existsSync(filepath))) { + winston.error('Firmware file not found'); + return false; + } + pruEnable(pruno, false); //disable PRU before loading firmware if not already disabled + var path = fw_path + pruno + ' -fw'; + shell.cp(filepath, path); //used because fs.copyFile() not supported, fast enough + pruEnable(pruno, true); //enable PRU after loading firmware +} +//sendmsg +var sendrpMsg = function (message, pruno) { + var pruno = typeof pruno !== 'undefined' ? pruno : '0'; + if (pruno != 0 && pruno != 1) { + winston.error('PRU Core No. should be 0 or 1'); + return false; + } + pruno += 1; + var channel = 'rpmsg_pru3' + pruno; + var path = '/dev/' + channel; //default is rpmsg_pru31 + fs.writeFileSync(path, message + '\n'); +} +//getmsg +var getrpMsg = function (pruno) { + var pruno = typeof pruno !== 'undefined' ? pruno : '0'; + if (pruno != 0 && pruno != 1) { + winston.error('PRU Core No. should be 0 or 1'); + return false; + } + pruno += 1; + var channel = 'rpmsg_pru3' + pruno; + var path = '/dev/' + channel; + if (!(fs.existsSync(path))) { + winston.error('getMsg failed to fetch message'); + return false; + } else return fs.readFileSync(path).toString(); +} + +module.exports = { + modProbe: modProbe, + pruEnable: pruEnable, + pruReset: pruReset, + loadPRUFirmware: loadPRUFirmware, + sendrpMsg: sendrpMsg, + getrpMsg: getrpMsg +} \ No newline at end of file From 003bb73763904f0d38e05bd7936e9b697d05fa7d Mon Sep 17 00:00:00 2001 From: vaishnav98 Date: Mon, 2 Jul 2018 20:53:03 +0530 Subject: [PATCH 2/3] add compilation support for pru.js compilation support for pru.js implemented using bone101 Makefile --- src/pru.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/pru.js b/src/pru.js index bbbc5f4c..e4484ba2 100644 --- a/src/pru.js +++ b/src/pru.js @@ -1,7 +1,8 @@ var fs = require('fs'); var shell = require('shelljs'); var winston = require('winston'); - +var child_process = require('child_process'); +var node_path = require('path'); var debug = process.env.DEBUG ? true : false; var rproc_path = '/sys/class/remoteproc/remoteproc'; @@ -55,14 +56,37 @@ var loadPRUFirmware = function (filepath, pruno) { winston.error('PRU Core No. should be 0 or 1'); return false; } - if (!(fs.existsSync(filepath))) { - winston.error('Firmware file not found'); - return false; + if ((filepath).indexOf('.out') == -1) { + var filename = node_path.basename(filepath); + var directory = filepath.replace(filename, ''); + if (debug) winston.debug('compiling and loading firmware: ' + filepath + 'to pru' + pruno) //compile the source using bone101 makefile + var make = child_process.spawn('/usr/bin/make', [ + "-f", + "/usr/share/bone101/examples/extras/pru/Makefile", + "TARGET=" + filename.replace(/\.c$/, ''), + "PRUN=" + 'pru' + pruno + ], { + 'cwd': directory + }); + make.stdout.on('data', (data) => { + if (debug) winston.debug(`stdout: ${data}`); + }); + make.stderr.on('data', (data) => { + winston.error(`stderr: ${data}`); + }); + return true; //the makfile handles loading of firmware + } else { + if (!(fs.existsSync(directory + filename))) { + winston.error('Firmware file not found'); + return false; + } + if (debug) winston.debug('loading firmware: ' + filepath + ' to pru' + pruno) + pruEnable(pruno, false); //disable PRU before loading firmware if not already disabled + var path = fw_path + pruno + ' -fw'; + shell.cp(filepath, path); //used because fs.copyFile() not supported, fast enough + pruEnable(pruno, true); //enable PRU after loading firmware + return true; } - pruEnable(pruno, false); //disable PRU before loading firmware if not already disabled - var path = fw_path + pruno + ' -fw'; - shell.cp(filepath, path); //used because fs.copyFile() not supported, fast enough - pruEnable(pruno, true); //enable PRU after loading firmware } //sendmsg var sendrpMsg = function (message, pruno) { From 57d32c40f592b177db8014af4e5ebc20d60e4660 Mon Sep 17 00:00:00 2001 From: vaishnav98 Date: Mon, 2 Jul 2018 21:53:43 +0530 Subject: [PATCH 3/3] Eventemitter for receiving rpmsg added eventemitter based approach to receive rpmsg asynchronously --- src/pru.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pru.js b/src/pru.js index e4484ba2..d6d5bd40 100644 --- a/src/pru.js +++ b/src/pru.js @@ -3,6 +3,7 @@ var shell = require('shelljs'); var winston = require('winston'); var child_process = require('child_process'); var node_path = require('path'); +var events = require('events'); var debug = process.env.DEBUG ? true : false; var rproc_path = '/sys/class/remoteproc/remoteproc'; @@ -113,7 +114,15 @@ var getrpMsg = function (pruno) { if (!(fs.existsSync(path))) { winston.error('getMsg failed to fetch message'); return false; - } else return fs.readFileSync(path).toString(); + } else { + var rpmsgEmitter = new events.EventEmitter(); + fs.watch(path, function (curr, prev) { + fs.readFile(path, 'utf8', function (err, data) { + rpmsgEmitter.emit('rpmsg', data); + }); + }); + return rpmsgEmitter; + } } module.exports = {