ptolemy.codegen.c.targets.openRTOS.domains.pn.kernel
Class PNDirector

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.codegen.kernel.CodeGeneratorHelper
          extended by ptolemy.codegen.actor.Director
              extended by ptolemy.codegen.c.targets.openRTOS.domains.pn.kernel.PNDirector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, ActorCodeGenerator, ComponentCodeGenerator, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable

public class PNDirector
extends Director

Code generator helper associated with the PNDirector class. This helper generates OpenRTOS specific code. OpenRTOS is a real-time operating system for the Luminary Micro target. It features a small preemptive, priority-based kernel. For further documentation, one can refer to http://www.freertos.org/. This director starts a task for each actor. Each task has a specified name, stack size, priority and function code to execute. User can introduce annotations in an actor to specify these values. In particular, this helper class looks for the "_stackSize" and "_priority" parameters and use their values to create the tasks. If these parameters are not specified, the code generator uses the default value 80 for stack size, and 0 for priority. Each task executes a given function which consists of the actor initialization, fire and wrapup code. The communication between tasks use the OpenRTOS queues. These queues provide synchronized access methods. This director helper generates a queue for each connection which is referenced by its referable port channel. There is one referable port channel for each connection.

Since:
Ptolemy II 8.0
Version:
$Id: PNDirector.java 57044 2010-01-27 22:41:05Z cxh $
Author:
Man-Kit Leung
See Also:
Serialized Form
Accepted Rating:
Yellow (mankit)
Proposed Rating:
Yellow (mankit)

Nested Class Summary
 
Nested classes/interfaces inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
CodeGeneratorHelper.Channel, CodeGeneratorHelper.VariableScope
 
Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj
NamedObj.ContainedObjectsIterator
 
Field Summary
private  java.util.HashSet<CodeGeneratorHelper.Channel> _queues
          The set of referable channels that are associated with a generated queue.
 
Fields inherited from class ptolemy.codegen.actor.Director
_director, _eol, _INDENT1, _INDENT2, _INDENT3, _INDENT4
 
Fields inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_codeGenerator, _codeStream, _parseTreeCodeGenerator, _portConversions, _referencedParameters
 
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
 
Constructor Summary
PNDirector(PNDirector pnDirector)
          Construct the code generator helper associated with the given PNDirector.
 
Method Summary
protected  java.lang.String _createDynamicOffsetVariables(TypedIOPort port)
          Create the offset variables for the given port.
private static java.lang.String _generateQueueReference(IOPort port, int channelNumber)
          Generate the reference of the queue generated for the specified port and channel number.
private  void _generateTaskFunctionCode(java.lang.StringBuffer code)
          Generate the task functions.
private  java.lang.String _getActorTaskLabel(Actor actor)
          Generate the label of the task generated for the specified actor.
private  java.lang.String _getMaxDelay(CodeGeneratorHelper.Channel channel)
          Return the maximum wait time for getting and sending data on the specified port channel.
private  java.lang.String _getPriority(Actor actor)
          Return the task priority associated with the specified actor.
private  int _getQueueSize(IOPort port, int channelNumber)
          Return the size of the queue to be generated for the specified port channel.
private  java.lang.String _getStackSize(Actor actor)
          Return the expression of the stack size value for the specified actor task.
 java.lang.String generateCodeForGet(IOPort port, int channelNumber)
          Generate code for getting data from the specified channel.
 java.lang.String generateCodeForSend(IOPort port, int channelNumber, java.lang.String dataToken)
          Generate code for sending data to the specified channel.
 java.lang.String generateFireCode()
          Generate the director fire code.
 java.lang.String generateInitializeCode()
          Generate the initialize code.
 java.lang.String generateMainLoop()
          Generate the main loop code.
 java.lang.String generatePreinitializeCode()
          Generate the preinitialize code for the associated PN director.
 void generateTransferInputsCode(IOPort inputPort, java.lang.StringBuffer code)
          Generate code for transferring tokens into a composite.
 void generateTransferOutputsCode(IOPort port, java.lang.StringBuffer code)
          Generate code for transferring tokens outside of a composite.
 java.lang.String generateVariableInitialization()
          Generate variable initialization for the referenced parameters.
 int getBufferSize(IOPort port, int channelNumber)
          Return the buffer size to generate the variable for the specified port channel.
 java.util.Set<java.lang.String> getHeaderFiles()
          Get the files needed by the code generated from this helper class.
 
Methods inherited from class ptolemy.codegen.actor.Director
_ceilToPowerOfTwo, _getIndentPrefix, _updateConnectedPortsOffset, _updatePortOffset, createOffsetVariablesIfNeeded, generateFireFunctionCode, generateModeTransitionCode, generateOffset, generatePostfireCode, generateVariableDeclaration, generateWrapupCode, getComponent, getIncludeDirectories, getLibraries, getLibraryDirectories, getModifiedVariables, getReference, getReference, getReferenceChannels, getWCET
 
Methods inherited from class ptolemy.codegen.kernel.CodeGeneratorHelper
_createBufferSizeAndOffsetMap, _createInputBufferSizeAndOffsetMap, _findClosedParen, _generateBlockCode, _generateBlockCode, _generateFireCode, _generateTypeConvertMethod, _generateTypeConvertStatement, _generateTypeConvertStatements, _getCastType, _getChannelAndOffset, _getFireFunctionArguments, _getHelper, _getHelper, _getReference, _getReferenceChannels, _getTypeConvertChannels, _getTypeConvertReference, _indexOf, _replaceMacro, addFunctionUsed, addNewTypeUsed, analyzeTypeConvert, checkLocal, checkRemote, codeGenType, copyFilesToCodeDirectory, generateChannelOffset, generateFireFunctionCode2, generateName, generateOffset, generatePortReference, generatePrefireCode, generateSimpleName, generateTypeConvertFireCode, generateTypeConvertFireCode, generateVariableName, getBufferSize, getCodeGenerator, getDefaultBlocks, getDirector, getDirectorHelper, getFunctionInvocation, getNewInvocation, getObject, getParameterValue, getParseTreeCodeGenerator, getPort, getRates, getReadOffset, getReference, getReference, getReference, getReference, getSharedCode, getSinkChannels, getSize, getSourceChannel, getWriteOffset, isPrimitive, isPrimitive, parseList, processCode, resetInputPortsOffset, setBufferSize, setCodeGenerator, setReadOffset, setWriteOffset, targetType, toString
 
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _debug, _debug, _debug, _debug, _debug, _description, _exportMoMLContents, _getContainedObject, _isMoMLSuppressed, _markContentsDerived, _propagateExistence, _propagateValue, _recordDecoratedAttributes, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, attributeChanged, attributeList, attributeList, attributeTypeChanged, clone, clone, containedObjectsIterator, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getContainer, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, propagateExistence, propagateValue, propagateValues, removeChangeListener, removeDebugListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setName, setPersistent, setSource, sortContainedObjects, toplevel, uniqueName, validateSettables, workspace
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_queues

private java.util.HashSet<CodeGeneratorHelper.Channel> _queues
The set of referable channels that are associated with a generated queue.

Constructor Detail

PNDirector

public PNDirector(PNDirector pnDirector)
Construct the code generator helper associated with the given PNDirector.

Parameters:
pnDirector - The associated ptolemy.domains.pn.kernel.PNDirector
Method Detail

generateCodeForGet

public java.lang.String generateCodeForGet(IOPort port,
                                           int channelNumber)
                                    throws IllegalActionException
Generate code for getting data from the specified channel. The generated code waits forever until the receipt of data. Upon return the data can be access through using the $ref(port#channalNumber) macro.

Overrides:
generateCodeForGet in class Director
Parameters:
port - The specified port.
channelNumber - The specified channel.
Returns:
The code for getting data from specified channel.
Throws:
IllegalActionException - If the specified port channel has more than one referable queues.
IllegalActionException - If an error occurs when getting the helper for the actor containing the given port, or reading the width of the referenced port, or Director.getReferenceChannels(IOPort, int) or CodeGeneratorHelper.processCode(String) throw it.

generateCodeForSend

public java.lang.String generateCodeForSend(IOPort port,
                                            int channelNumber,
                                            java.lang.String dataToken)
                                     throws IllegalActionException
Generate code for sending data to the specified channel. The generated code waits forever until the data is successfully sent.

Overrides:
generateCodeForSend in class Director
Parameters:
port - The specified port.
channelNumber - The specified channel.
dataToken - The specified expression for the data being send.
Returns:
The code for sending data to the specified channel.
Throws:
IllegalActionException - If an error occurs when getting the helper for the actor containing the given port, or Director.getReferenceChannels(IOPort, int) or CodeGeneratorHelper.processCode(String) throw it.

generateFireCode

public java.lang.String generateFireCode()
                                  throws IllegalActionException
Generate the director fire code. The code creates a new task for each actor according to their specified parameters (e.g. stack depth, priority, and etc.). The code also initiates the task scheduler.

Specified by:
generateFireCode in interface ActorCodeGenerator
Overrides:
generateFireCode in class Director
Returns:
The generated fire code.
Throws:
IllegalActionException - Not thrown in this class.

generateInitializeCode

public java.lang.String generateInitializeCode()
                                        throws IllegalActionException
Generate the initialize code. This generates the hardware initialization code and creates the queues for all referable port channels.

Specified by:
generateInitializeCode in interface ComponentCodeGenerator
Overrides:
generateInitializeCode in class Director
Returns:
The generated initialize code.
Throws:
IllegalActionException - If the helper associated with an actor throws it while generating initialize code for the actor.

generateMainLoop

public java.lang.String generateMainLoop()
                                  throws IllegalActionException
Generate the main loop code. It delegates to the helper of the container, and invokes its generateFireCode() method.

Overrides:
generateMainLoop in class Director
Returns:
The main loop code.
Throws:
IllegalActionException - Thrown if an error occurs when getting the helper or generating the fire code from it.

generatePreinitializeCode

public java.lang.String generatePreinitializeCode()
                                           throws IllegalActionException
Generate the preinitialize code for the associated PN director.

Specified by:
generatePreinitializeCode in interface ActorCodeGenerator
Overrides:
generatePreinitializeCode in class Director
Returns:
The generated preinitialize code.
Throws:
IllegalActionException - If the helper associated with an actor throws it while generating preinitialize code for the actor.

generateTransferInputsCode

public void generateTransferInputsCode(IOPort inputPort,
                                       java.lang.StringBuffer code)
                                throws IllegalActionException
Generate code for transferring tokens into a composite.

Overrides:
generateTransferInputsCode in class Director
Parameters:
inputPort - The port to transfer tokens.
code - The string buffer that the generated code is appended to.
Throws:
IllegalActionException - If thrown while transferring tokens.

generateTransferOutputsCode

public void generateTransferOutputsCode(IOPort port,
                                        java.lang.StringBuffer code)
                                 throws IllegalActionException
Generate code for transferring tokens outside of a composite.

Overrides:
generateTransferOutputsCode in class Director
Parameters:
port - The specified port.
code - The given code buffer.
Throws:
IllegalActionException - Not thrown in this class.

generateVariableInitialization

public java.lang.String generateVariableInitialization()
                                                throws IllegalActionException
Generate variable initialization for the referenced parameters. This overrides the super class method and returns an empty string. It avoids generating any offset variables.

Specified by:
generateVariableInitialization in interface ActorCodeGenerator
Overrides:
generateVariableInitialization in class Director
Returns:
code The empty string.
Throws:
IllegalActionException - Not thrown in this class.

getBufferSize

public int getBufferSize(IOPort port,
                         int channelNumber)
                  throws IllegalActionException
Return the buffer size to generate the variable for the specified port channel. This number dictates the size of the array generated for a variable associated with the port channel. This returns 1, since queuing is done using a separate structure.

Overrides:
getBufferSize in class Director
Parameters:
port - The specified port
channelNumber - The specified channel number.
Returns:
The buffer size to generate the variable for the specified port channel. In this case, it's 1.
Throws:
IllegalActionException - Not thrown in this class.
See Also:
CodeGeneratorHelper.setBufferSize(IOPort, int, int)

getHeaderFiles

public java.util.Set<java.lang.String> getHeaderFiles()
                                               throws IllegalActionException
Get the files needed by the code generated from this helper class. The header files required are "FreeRTOS.h", "task.h", "queue.h", "lcd_message.h", and "semphr.h". Because of dependencies between these header files, they are included in the order specified.

Specified by:
getHeaderFiles in interface ActorCodeGenerator
Overrides:
getHeaderFiles in class Director
Returns:
A set of strings that are header files needed by the code generated from this helper class.
Throws:
IllegalActionException - Not thrown in this class.

_createDynamicOffsetVariables

protected java.lang.String _createDynamicOffsetVariables(TypedIOPort port)
                                                  throws IllegalActionException
Create the offset variables for the given port. Since built-in queue structure is used, it eliminates the need for offset variables. Instead, this method generates the declaration of the queues if the specified port has any referable port channels. There is one referable port channel for each connection. All input port channels are considered referable.

Parameters:
port - The specified port.
Returns:
Code that declares the read and write offset variables.
Throws:
IllegalActionException - If getting the code block throws it.

_generateQueueReference

private static java.lang.String _generateQueueReference(IOPort port,
                                                        int channelNumber)
Generate the reference of the queue generated for the specified port and channel number. The specified port channel is assumed to be referable.

Parameters:
port - The specified port.
channelNumber - The specified channel number.
Returns:
The reference for the queue.

_generateTaskFunctionCode

private void _generateTaskFunctionCode(java.lang.StringBuffer code)
                                throws IllegalActionException
Generate the task functions. A task function is generated for each actor that is visible to this director helper. A task function consists of the actor's initialize, fire and wrapup code. In particular, a loop is generated to iterate the actor's fire code. If the actor has a firing count limit, a finite for loop is generated. Otherwise, the fire code is wrapped inside an infinite loop.

Parameters:
code - The given code buffer.
Throws:
IllegalActionException - If getting the helper or generating the actor initialize, fire, or wrapup code throws it.

_getActorTaskLabel

private java.lang.String _getActorTaskLabel(Actor actor)
Generate the label of the task generated for the specified actor.

Parameters:
actor - The specified actor.
Returns:
The task label for the specified actor.

_getMaxDelay

private java.lang.String _getMaxDelay(CodeGeneratorHelper.Channel channel)
Return the maximum wait time for getting and sending data on the specified port channel. The wait time specifies the duration the queue access functions block before returning. In this case, it is set to the maximum, portMAX_DELAY, which is a symbolic constant defined in the OpenRTOS header file.

Parameters:
channel - The specified port channel.
Returns:
The maximum wait time for getting and sending data.

_getPriority

private java.lang.String _getPriority(Actor actor)
Return the task priority associated with the specified actor. If the actor has a parameter named "_priority", its expression is returned. Otherwise, zero is the default priority value.

Parameters:
actor - The specified actor.
Returns:
The task priority associated with the specified actor.

_getQueueSize

private int _getQueueSize(IOPort port,
                          int channelNumber)
                   throws IllegalActionException
Return the size of the queue to be generated for the specified port channel.

Parameters:
port - The specified port.
channelNumber - The specified channel number.
Returns:
The size of the queue to be generated for the given port channel.
Throws:
IllegalActionException

_getStackSize

private java.lang.String _getStackSize(Actor actor)
Return the expression of the stack size value for the specified actor task. If the specified actor has a parameter named "_stackSize", its expression is return. Otherwise, 80 is default return value.

Parameters:
actor - The specified actor.
Returns:
The expression of the stack size value.