forked from jadonk/bonescript
-
Notifications
You must be signed in to change notification settings - Fork 0
add draft pru support #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vaishnavachath
wants to merge
3
commits into
master
Choose a base branch
from
pru-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| var fs = require('fs'); | ||
| 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'; | ||
| 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 ((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; | ||
| } | ||
| } | ||
| //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 { | ||
| 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 = { | ||
| modProbe: modProbe, | ||
| pruEnable: pruEnable, | ||
| pruReset: pruReset, | ||
| loadPRUFirmware: loadPRUFirmware, | ||
| sendrpMsg: sendrpMsg, | ||
| getrpMsg: getrpMsg | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be some way to get events via an EventEmitter asynchronously.