Creating a Real-Time Application that Uses the NAL

An RTSS application can use the NAL API function set (starting with the prefix RtNal). An application that uses the NAL typically runs a Layer 2 protocol for transmitting and receiving data and does not need to use the RT-TCP/IP Stack or socket API.

This topicdescribes the steps required to create a real-time application that uses the NAL.

Sections in this topic:

Creating the Real-Time Application

The RTX64 Application template provides the structure for creating RTX64 applications. Use the following procedure to create an RTX64 project using the RTX64 Application template in Microsoft Visual Studio.

To create a RTX64 application project:

  1. Create a new project in Visual Studio.
  2. Select C or C++ as the template type.
  3. Select the RTX64 Application template. This template is a single-page dialog that displays the default project settings and provides optional application and program settings.

Specify the following options:

  1. Click Finish to create the project. This generates the RTX64 solution and project according to the options you selected.

Set additional project properties:

  1. Right-click the project name and select Properties from the pull-down menu.
  2. Add the following properties:
In the properties page... Add... For these configurations...
Linker /Input RtNal.lib RtssRelease, RtssDebug

Add includes to the header file:

Add the following includes to the application header file, below all existing includes and after Rtapi.h:

Rebuild the application:

NOTE: If encounter problems building the application, compare your project properties with the properties of the NalDataStream sample provided with the NAL.

Writing the Application

The NAL application typically:

The transmit thread might wait on an event flag set from an application timer to send a packet.

NOTE: There is no receive thread that runs in the context of the RTX64NAL process. All packets received on the wire, when there is no application attached to the driver queue, are discarded.

The priority of the receive and transmit threads must be less than the priority of the interrupt thread configured by the driver (the default is 64). The transmit thread priority should be less than the priority of the transmit complete thread (the default is 62).

All API calls that use a queue handle fail with error code ERROR_INVALID_HANDLE if the queue is released from another thread.

NOTE: The main function must wait until all threads complete before exiting.

NOTE: If the RtNal functionality is encapsulated into an RTDLL and not used by the loading RTSS process, you must not free the library before you exit your main process.

See the RTX64 Network Abstraction Layer (NAL) API Reference and NalDataStream sample for the complete information on using the NAL API.

Typical Application Sequence

Below are the steps that make up a typical application sequence.

Steps:

  1. Call RtNalInit.
  2. Call RtNalAcquireQueue for all the queues needed for the application.
  3. Prepare the application context structures for all acquired queues, as well as for the RTNAL_QUEUE_CAPABILITIES structure.
  4. Start receive threads.
  5. Call RtNalConfigureQueue for all acquired queues.
  6. Start transmit threads.
  7. The main thread waits for stop events or for some signal to stop the application and sets stop events.
  8. The main thread waits for all threads to complete.
  9. The main thread releases all queues and other application resources and frees allocated memory.
  10. The main thread returns.

See the RTX64 Network Abstraction Layer (NAL) API Reference and NalDataStream sample for the complete information on using the NAL API.

Typical Receive Thread Function

Below are the steps for a typical receive thread function, which loops until the thread wakes up by the stop event or until one of API calls fails with error code ERROR_INVALID_HANDLE.

Steps:

  1. Wait on receive and stop notification event handles.
  2. Call RtNalGetReceivePacketCount to get the receive count.
  3. Call RtNalReceive count times in a loop.
  4. Break out of this loop and the outer loop if RtNalReceive fails with error code ERROR_INVALID_HANDLE.
  5. If RtNalReceive succeeds, process the receive packet.

Notification Events

Notification events are used to notify an application of received packets, transmitted packets, link change, and the queue being released. The event handles are returned in the structure in RtNalAcquireQueue. Typically, the events are set by the NAL and the application threads wait on event handles. The application might also set the stop event to let its threads know that it wants to terminate.

NOTE: An application must not use the transmit complete event handle directly.

Acquiring a Queue

  1. To get the friendly name for a device, call RtNalGetNumberOfQueues and then iterate through queues by calling RtNalGetQueueInfoByIndex, starting with the index 0 and incrementing the index. This way, you can see information on all of the queues configured for the system.
  2. See the NalDataStream sample for a usage example in DisplayAllConfiguredQueues.
  3. Before calling RtNalAcquireQueue, fill in the structure of RTNAL_QUEUE_CRITERIA type with the device’s friendly name, receive or transmit queue type, search method, and event flag mask.
  4. Search methods allow you to use the device name or PCI Bus location. Use the device queue with the specified queue number, use any available queue, or use the default queue (receiving unfiltered packets) for receive.
  1. Set flags to RTNAL_USE_RX_EVENT_FLAG | RTNAL_USE_LINK_EVENT_FLAG for a receive queue to use receive, link change, and stop events to receive packets in a receive thread.
  2. Set flags to RTNAL_USE_LINK_EVENT_FLAG for a receive queue to use link change and stop events to receive packets in a polling timer.
  3. Set flags to RTNAL_USE_LINK_EVENT_FLAG for a transmit queue to use link change and stop events to send packets with RtNalTransmit (or RtNalTransmitEx without transmit complete callbacks).
  4. Set flags to RTNAL_USE_LINK_EVENT_FLAG | RTNAL_USE_TRANSMIT_COMPLETE_EVENT_FLAG for a transmit queue to use link change, transmit complete and stop events to if you send packets with RtNalTransmitEx and use transmit complete callbacks.
  1. Call RtNalAcquireQueue with the pointers to the prepared structures RTNAL_QUEUE_CRITERIA, RTNAL_QUEUE, and RTNAL_QUEUE_EVENTS.
  2. Upon successful return, the system queue information and the associated device is returned in the RTNAL_QUEUE structure. The notification event handles are returned in the RTNAL_QUEUE_EVENTS structure. The returned information contains:
  3. the correct MAC address
  4. the driver instance index
  5. the driver queue index
  6. the number of receive packet buffers configured by the driver
  7. the number of transmit packet buffers configured by the driver
  8. the maximum packet size used by the driver. Note that the driver might report the maximum packet size without accounting for Ethernet FCS, but still place FCS in the receive packet buffer. An application must add 4 bytes to the buffer size used in RtNalReceive. For example, if the reported maximum packet size is 1514, an application must use 1518-byte buffers for RtNalReceive.

Configuring the Queue to Send or Receive Data

The NAL application should do the following for the queue to properly send and receive data:

The application calls RtNalConfigureQueue with the queue handle, an application context pointer, and the pointer to a RTNAL_QUEUE_CAPABILITIES structure. An application context pointer must not be NULL.

Writing a Receive Thread

Below are rules for writing a receive thread:

The application can now process the received packet.

NOTES:

Using the Receive Packet Callback

Below are rules for using the receive packet callback:

NOTE: The RtNalE1000 driver does not support RtndReceiveWithCallback.

Sending a Packet

Below are rules for sending a packet:

Sending Multiple Packets with RtNalTransmitEx

NOTE: The RtNalE1000 driver does not support RtndTransmitEx.

RtNalTransmitEx transmits multiple Ethernet frames in a single call. The frames must be pre-allocated by RtNalAllocateFrame, which returns a pointer to an RTNAL_FRAME structure, containing a virtual address and a physical address of the frame. The application must not alter the virtual and physical addresses of the frame buffer. The RTNAL_FRAME structure also contains, size of the packet to transmit and other information. RtNalTransmitEx uses an array of pointers to RTNAL_FRAME to send multiple Ethernet packets. An application can also provide callbacks to be called with each or some of the frames from a transmit complete thread. A pointer to a transmit complete callback must be written to a RTNAL_FRAME structure for a packet for which an application wants to get a callback.

To use transmit complete callbacks, the transmit notifications must be enabled for this queue. When the queue is acquired with the transmit notifications, the NAL runs an additional transmit complete thread per queue in the application process context. This thread runs on the ideal processor and with the priority, configured for the interface. Even though the transmit complete event handle is returned in the structure RTNAL_QUEUE_EVENTS from RtNalAcquireQueue, the application must not use it directly.

See Also: