Advertisement

Pipes: utility services and data structures

November 13, 2018

Colin Walls-November 13, 2018

Data Structures

Pipes utilize six or seven data structures – all in RAM and ROM – which, like other Nucleus SE objects, are a series of tables, included and dimensioned according to the number of pipes configured and options selected.

I strongly recommend that application code does not access these data structures directly but uses the provided API functions. This avoids incompatibility with future versions of Nucleus SE and unwanted side-effects and simplifies porting of an application to Nucleus RTOS. The details of data structures are included here to facilitate easier understanding of the working of the service call code and for debugging.

Kernel RAM Data

These data structures are:

NUSE_Pipe_Head[] – This is an array of type U8, with one entry for each configured pipe, which represents a pointer to the front of the pipe of messages. It is used as an index off of the addresses in NUSE_Pipe_Data[] (see below).
NUSE_Pipe_Tail[] – This is an array of type U8, with one entry for each configured pipe, which represents a pointer to the end of the pipe of messages. It is used as an index off of the addresses in NUSE_Pipe_Data[] (see below).
NUSE_Pipe_Items[] – This is an array of type U8, with one entry for each configured pipe, which represents a count of the current number of messages in the pipe. This data is arguably redundant, as its value can be derived from the head and tail indexes, but storing the count simplifies the code.
NUSE_Pipe_Blocking_Count[] – This type U8 array contains the counts of how many tasks are blocked on each pipe. This array only exists if blocking API call support is enabled.

These data structures are all initialized to zeros by NUSE_Init_Pipe() when Nucleus SE starts up. This is logical, as it renders every pipe as being empty (unused). A future article will provide a full description of Nucleus SE start-up procedures.

Here are the definitions of these data structures in nuse_init.c file:

RAM U8 NUSE_Pipe_Head[NUSE_PIPE_NUMBER];
RAM U8 NUSE_Pipe_Tail[NUSE_PIPE_NUMBER];
RAM U8 NUSE_Pipe_Items[NUSE_PIPE_NUMBER];
#if NUSE_BLOCKING_ENABLE
   RAM U8 NUSE_Pipe_Blocking_Count[NUSE_PIPE_NUMBER];
#endif


User RAM

It is the user’s responsibility to provide an area of RAM for data storage for each configured pipe. The size of this RAM area must accommodate an array of type U8 large enough to accommodate all of the messages in the pipe.

ROM Data

These data structures are:

NUSE_Pipe_Data[] – This is an array of type ADDR, with one entry for each configured pipe, which represents a pointer to the data area (discussed in User RAM above) for each pipe.
NUSE_Pipe_Size[] – This is an array of type U8, with one entry for each configured pipe, which represents the number of messages that may be accommodated by each pipe.
NUSE_Pipe_Message_Size[] – This is an array of type U8, with one entry for each configured pipe, which represents the size of messages (in bytes) that may be accommodated by each pipe.

These data structures are all declared and initialized (statically, of course) in nuse_config.c, thus:

ROM ADDR *NUSE_Pipe_Data[NUSE_PIPE_NUMBER] =
{
   /* addresses of pipe data areas ------ */
};
ROM U8 NUSE_Pipe_Size[NUSE_PIPE_NUMBER] =
{
   /* pipe sizes ------ */
};
ROM U8 NUSE_Pipe_Message_Size[NUSE_PIPE_NUMBER] =
{
   /* pipe message sizes ------ */
};

Pipe Data Footprint

Like all kernel objects in Nucleus SE, the amount of data memory required for pipes is readily predictable.

The ROM data footprint (in bytes) for all the pipes in an application may be computed thus:

NUSE_PIPE_NUMBER * (sizeof(ADDR) + 2)

The kernel RAM data footprint (in bytes) for all the pipes in an application, when blocking API calls is enabled, may be computed thus:

NUSE_PIPE_NUMBER * 4

Otherwise it is:

NUSE_PIPE_NUMBER * 3

The amount of user RAM (in bytes) required for the pipe with index pipe is:

NUSE_Pipe_Size[pipe] * NUSE_Pipe_Message_Size[pipe]

Unimplemented API Calls

Four pipe API calls found in Nucleus RTOS are not implemented in Nucleus SE:

Create Pipe

This API call creates a pipe. It is not needed with Nucleus SE, as pipes are created statically.

Service call prototype:

STATUS NU_Create_Pipe(NU_PIPE *pipe, char *name,
VOID *start_address, UNSIGNED pipe_size, OPTION message_type,
UNSIGNED message_size, OPTION suspend_type);

Parameters:

pipe – pointer to a user-supplied pipe control block; this will be used as a “handle” for the pipe in other API calls
name – pointers to a 7-character, null-terminated name for the pipe
start_address – starting address for the pipe
pipe_size – the total number of bytes in the pipe
message_type – type of message supported by the pipe; may be NU_FIXED_SIZE or NU_VARIABLE_SIZE
message_size – if the pipe supports fixed size messages, this parameter specifies the exact size of each message; otherwise, if the pipe supports variable sized messages, this is the maximum message size
suspend_type – specifies how tasks suspend on the pipe. Valid options for this parameter are NU_FIFO and NU_PRIORITY, which represent First-In-First-Out (FIFO) and priority-order task suspension, respectively

Returns:

NU_SUCCESS – indicates successful completion of the service
NU_INVALID_PIPE – indicates the pipe control block pointer is NULL or already in use
NU_INVALID_MEMORY – indicates the memory area specified by the start_address is invalid
NU_INVALID_MESSAGE – indicates that the message_type parameter is invalid
NU_INVALID_SIZE – indicates that either the message size is greater than the pipe size, or that the pipe size or message size is zero
NU_INVALID_SUSPEND – indicates that the suspend_type parameter is invalid

Delete Pipe

This API call deletes a previously created pipe. It is not needed with Nucleus SE, as pipes are created statically and cannot be deleted.

Service call prototype:

STATUS NU_Delete_Pipe(NU_PIPE *pipe);

Parameters:

pipe - pointer to pipe control block

Returns:

NU_SUCCESS – indicates successful completion of the service
NU_INVALID_PIPE – indicates the pipe pointer is invalid

Pipe Pointers

This API call builds a sequential list of pointers to all pipes in the system. It is not needed with Nucleus SE, as pipes are identified by a simple index, not a pointer, and it would be redundant.

Service call prototype:

UNSIGNED NU_Pipe_Pointers(NU_PIPE **pointer_list,
UNSIGNED maximum_pointers);

Parameters:

pointer_list – pointer to an array of NU_PIPE pointers; this array will be filled with pointers to established pipes in the system
maximum_pointers – the maximum number of pointers to place in the array

Returns:

The number of NU_PIPE pointers placed into the array

Broadcast to Pipe

This API call broadcasts a message to all tasks waiting for a message from the specified pipe. It is not implemented with Nucleus SE, as it would have added excessive complexity.

Service call prototype:

STATUS NU_Broadcast_To_Pipe(NU_PIPE *pipe, VOID *message,
UNSIGNED size, UNSIGNED suspend);

Parameters:

pipe – pointer to pipe control block
message – pointer to the broadcast message
size – the number of UNSIGNED data elements in the message. If the pipe supports variable-length messages, this parameter must be equal to or less than the message size supported by the pipe. If the pipe supports fixed-size messages, this parameter must be exactly the same as the message size supported by the pipe
suspend - specifies whether or not to suspend the calling task if the pipe is already full; valid options for this parameter are NU_NO_SUSPEND, NU_SUSPEND or a timeout value.

Returns:

NU_SUCCESS – indicates successful completion of the service
NU_INVALID_PIPE – indicates the pipe pointer is invalid
NU_INVALID_POINTER – indicates that the message pointer is NULL
NU_INVALID_SIZE – Indicates that the message size specified is not compatible with the size specified when the pipe was created
NU_INVALID_SUSPEND – indicates that suspend attempted from a non-task thread
NU_PIPE_FULL – indicates that there is insufficient space in the pipe for the message
NU_TIMEOUT – indicates the pipe is still full after the timeout has expired
NU_PIPE_DELETED – pipe was deleted while task was suspended
NU_PIPE_RESET – pipe was reset while the task was suspended

Compatibility with Nucleus RTOS

With all aspects of Nucleus SE, it was my goal to maintain as high a level of applications code compatibility with Nucleus RTOS as possible. Pipes are no exception, and, from a user’s perspective, they are implemented in much the same way as in Nucleus RTOS. There are areas of incompatibility, which have come about where I determined that such an incompatibility would be acceptable, given that the resulting code is easier to understand, or, more likely, could be made more memory efficient. Otherwise, Nucleus RTOS API calls may be almost directly mapped onto Nucleus SE calls. A future article will include further information on using Nucleus SE for users of Nucleus RTOS.

Object Identifiers

In Nucleus RTOS, all objects are described by a data structure – a control block – which has a specific data type. A pointer to this control block serves as an identifier for the pipe. In Nucleus SE, I decided that a different approach was needed for memory efficiency, and all kernel objects are described by a number of tables in RAM and/or ROM. The size of these tables is determined by the number of each object type that is configured. The identifier for a specific object is simply an index into those tables. So, I have defined NUSE_PIPE as being equivalent to U8; a variable – not a pointer – of this type then serves as the pipe identifier. This is a small incompatibility, which is easily handled if code is ported to or from Nucleus RTOS. Object identifiers are normally just stored and passed around and not operated upon in any way.

Nucleus RTOS also supports naming of pipes. These names are only used for target-based debug facilities. I omitted them from Nucleus SE to save memory.

Message Size and Variability

In Nucleus RTOS, a pipe may be configured to handle messages which are comprised of an arbitrary number of bytes of data. Likewise, in Nucleus SE. Nucleus RTOS also supports pipes with variable size messages, where only the maximum size is specified at creation time. Variable size messages are not supported by Nucleus SE.

Pipe Size

The number of messages in a pipe in Nucleus SE is limited to 256, as all the index variables and constants are type U8. Nucleus RTOS is not limited in this way.

Unimplemented API Calls

Nucleus RTOS supports ten service calls to work with pipes. Of these, four are not implemented in Nucleus SE. Details of these and of the decision to omit them may be found in Unimplemented API Calls earlier in this article.

The next RTOS Revealed article will look at system time.


Colin Walls has nearly forty years’ experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor, a Siemens business, and is based in the UK. His regular blog is located at: http://blogs.mentor.com/colinwalls. He may be reached by email at colin_walls@mentor.com

< Previous
Page 2 of 2
Next >

Loading comments...