1 intime technical support what’s intime ? --- introduction of realtime extension for windows...

188
INtime technical support 1 What’s INtime? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash Ohkubo Copyright reserved by Micronet.Co, 2006

Upload: michael-patterson

Post on 26-Dec-2015

274 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support1

What’s INtime?

--- Introduction of realtime extension for Windows System ---

Micronet.Co,

INtime Technical support group

Ash Ohkubo

Copyright reserved by Micronet.Co, 2006

Page 2: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support2

INtime Architecture

Page 3: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support3

Platform Requirement

IBM PC/AT Compatible platform. Intel CPU 32-bit architecture (486-Pentium4) Advantage for using PC platform

– Reduction of hardware cost• It doesn’t need to be dedicational (doesn’t need to prepare any dedicated computers or

equipments).

– Easy to provide• Maintainability for provision for long term.

Page 4: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support4

TERMS SPECIFICATION

CPU Intel 32-bit CPU 486-Pentium series

Celeron

32-bit CPU compatible to Pentium

RAM 64MB-4GB

HDD For INtime application development

applox. 30MB

For INtime kernel to run

applox. 10MB

Details

Page 5: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support5

Installation of INtime

All of the INtime software components are provided as files, which are accessible from Windows. That means that NTFS/FAT/FAT32 formatted disk is used for installing INtime,

SCSI HARD DISK Hi-speed Reducing CPU loads

IDE HARD DISK Large amount. Low cost

IDE FLASH DISK Resolves vibration problems

Page 6: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support6

Installation of INtime #2

The following device would be needed for the installation: – CRT– Mouse device– Keyboard device– CD-ROM drive

INtime software is provided as CD-ROM. Inserting the CD-ROM, navigation screen shows up how to install INtime SDK.

You’ll need to input license key provided with a license card to install INtime.

To install INtime software, it is required to log on with “Administrator” privilege.

After the installation, the system should be required to restart.

Page 7: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support7

Software Requirement

For installation of INtime software, one of these following OSes should be installed in advance:

– Microsoft Windows 2000 /Professional /Server– Microsoft Windows XP /Home /Professional/Embedded– Microsoft Windows 2003 Server

For development of the INtime application program or Windows’, one of these following development tools should be required:

– Microsoft Visual Studio 5.0– Microsoft Visual Studio 6.0– Microsoft Visual Studio .NET 2002(7.0)– Microsoft Visual Studio .NET 2003(7.1) – Microsoft Visual Studio 2005

Page 8: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support8

Starting/Stopping INtime

INtime can be started/stopped by taking the following steps:

– Clicking “Start kernel/Stop kernel” from the INtime icon in a task tray

Page 9: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support9

You can also start and stop INtime kernel from [Start INtime Kernel/Stop INtime Kernel] button on the “Local Kernel” applet, which is invoked from INtime Control Panel.

INtime Kernel is runningStarting INtime Kernel

Starting/Stopping INtime #2

Page 10: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support10

If you set “Start INtime Kernel Automatically” checkbox on the “Local kernel” applet, on the INtime Control Panel, INtime kernel starts to run when Windows starts.

Starting/Stopping INtime #3

Page 11: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support11

Shutdown

There is no other method to shutdown except Windows standard shutdown method.

Shutdown process for Windows environment is needed for even when on the application system. Especially sudden power off while file operation may cause the destruction of files.

Page 12: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support12

INtime Utility Software

INtime Configuration (INtimeConfiguration.EXE) – to configure INtime kernel and INtime environment

INtime Explorer (Intex.EXE) – to observe INtime objects

INtime Wizard – to generate an application source code accessed from Microsoft Visual Studio environment.

RT Application Loader (LDRTA.EXE) – Loading INtime application program

Spider/Visual Studio Debugger – Source code debuggers.

INscope – Thread tracing

INtime 3.0 SDK has powerful utilities to help you develop application.

Page 13: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support13

INtime Utility software: INtime Configuration You can access to INtime conf

iguration tool from:– INtime icon in a task tray.– [Start]-[Control Panel]-[Performa

nce and maintenance]-INtime control panel

You can configure the INtime kernel setting and INtime environment such as RT Network, application autoloading function, development tool, INtime device manager and so on.

INtime Configuration

Page 14: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support14

INtime Utility software: INtime Explorer(INtex)

INtime Explorer is a powerful tool to observe INtime objects that are existed on the INtime kernel.

You can access to INtime Explorer from:

– INtime icon in the task tray– [Start]-[Program]-[INtime]-

[INtime Explorer]

INtime Explorer

Page 15: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support15

INtime Utility software: INtime Wizard

INtime Wizard is a wizard tool which is installed to Microsoft Visual Studio.

INtime wizard helps user to develop an application program.

The output source code illustrates how to develop INtime application using INtime Objects

INtime Wizard(Visual Studio .NET 2003)

Page 16: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support16

INtime Utility software: RT Application Loader(LDRTA.EXE)

Use this utility when you start INtime application program. RT Application loader is associated to INtime icon so that you can start

application with double clicking the icon.

RT Application Loader

Page 17: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support17

INtime Utility software: Spider/Visual Studio Debugger

Source code debuggers Spider debugger allows you debug multi-thread. INtime source code

debugger is integrated in Visual Studio .NET/2005. (Single thread)

Spider Debugger

Page 18: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support18

INtime Utility software: INscope Task Analyzer

Traces all threads running transition state for a certain period of time and exposes how those threads are switched by INtime kernel.

INscope

Page 19: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support19

INtime Memory Physical memory, which PC mounts, should be divided into the memor

y for Windows use and also for INtime use. This configuration is done by Local Kernel applet from INtime Configuration panel.

Realtime (RT) application program is usually not so large. Normally, it wouldn’t be required to allocate a large amount of memory for INtime. However, if the INtime application program allocates a large structured memory, INtime memory should be enlarged depending on that size.

It should be better to leave enough memory for Windows environment to work properly.

Page 20: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support20

How to configure INtime Memory? Configuring INtime memory setting, access to Local Kernel applet on

the INtime configuration panel ([Start]-[Control panel]-[Performance and maintenance]-INtime).

Allocates certain amount of memory for INtime(default:16MB) using the slider or set value to the box

Page 21: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support21

INtime kernel tick INtime uses an interval timer or APIC timer (depending on PC hardware

architecture) to get “time”. These hardware components generate events periodically and are implemented in PC hardware.

Default kernel tick is configured as 500us. INtime kernel counts these ticks and calculates the elapsed time.

Kernel tick is configurable using INtime Configuration tool. The enable range are:

– 100us, 125us, 200us, 250us, 500us, 1ms, 2ms, 5ms, 10ms.

Kernel tick setting is not always good to make it as fast rate. The faster kernel tick that is configured, the more events to process and overheads for kernel according to those events.

Page 22: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support22

How to configure INtime kernel tick? Configuring INtime kernel tick setting, access to Local Kernel applet on

the INtime configuration panel ([Start]-[Control panel]-[Performance and maintenance]-INtime).

Double clicking “Kernel Clock rate(us) ” and select the value you would like to set(default: 500us) from the box.

Page 23: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support23

Preparation for INtime Wizard

Developing INtime application program, Visual C++ is used. Installing INtime wizard to the Visual Studio, it will improve development.

When installing INtime software, the installer searches those installed Visual Studio and installs suitable INtime Wizard.

You can manually install and uninstall from Development Tool applet on the INtime configuration panel.

Development Tools applet

Page 24: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support24

Communication between Windows application program

Windows application program communicates with INtime application program (RT program) via NTX API (Windows extension library). This library is provided when INtime is installed. Calling this library call, Windows application can access to RT memory or can get events from RT program.

Page 25: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support25

Communication between Windows application program #2

1. RT Kernel: Abbreviation for Real Time kernel, mea

ns INtime kernel, which provides deterministic real time scheduling.

2. RT process, RT thread, and RT library: RT (Real-time) process is by RT applic

ation program, which is currently running that includes RT threads. Real-time process can use ANSI C library and the system call (APIs).

3. NTX library: provides RT interface extensions for th

e Win32 program. This library allows Windows threads to communicate with RT threads.

Page 26: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support26

Communication between Windows application program #3

4. Transport driver: A driver that converts information to the protocol needed by the specified transport

mechanism.

5. Transport mechanism: The communication protocol or method used by NTX to communicate between

Windows and RT thread. Whether the various portions of your INtime application resides on a single PC or multiple computers accessed via serial or Ethernet cable.

6. HAL: When INtime is started, it patches Windows HAL on memory so that INtime

software intercepts some HAL calls to ensure realtime performance and even if Windows crashes INtime can keep running.

Page 27: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support27

Transport

mechanism

Transport driver Description

OSEM RTIF.SYS Used when the Windows host and RT client co-exist on a single PC.

Ethernet UDP/IP Used for Windows hosts and RT nodes connected via a local area network cable. UDP/IP provides a high-speed communication. This method is used to transport data without using several routers.

RS232C Serial I/O Used for Windows hosts and RT nodes connected by serial cables.

Transport mechanisms

Page 28: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support28

About the OSEM OSEM: OS Encupsulize Mechani

sm The mechanism encapsulates enti

re Windows as one of INtime RT threads.

OSEM manages the simultaneous operation of Windows and RT kernel on a single PC.

Once it encapsulates Windows, all its processes and threads execute as a single, low prioritized RT thread in the context of the RT root process.

Page 29: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support29

Development

Page 30: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support30

Environment

To develop INtime application program, the following items are required:

– INtime development kit( INtime 3.0 SDK )– Microsoft Visual Studio (Visual C/C++)

Page 31: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support31

Application Platform

RT Application (INtime Application) Application program(s), which has/have priority level between 0 to 255 and run(s) on INtime kerne

l. Thread(s), which require(s) realtime would be created as RT Application.

Application program(s), which require(s) to handle interruption or hardware I/O operation would be created as RT Application.

NTX Application( Windows Application) Windows application program, which includes realtime extension system calls (NTX) would be crea

ted as NTX Application.

Windows User interface program, which includes Windows GUI, would be created as NTX Application.

Page 32: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support32

Procedures for the development of application Use Microsoft Visual Studio to develop both RT application program and NTX

application program

Starting development

Create a Project

Generates souce code for RTprogram

Generates souce code forWindows program

INtime WizardWindows Wizard

Edit souce code

Build

Preparation for release

Debug with Windows tool Debug with RT tool

Completion

Page 33: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support33

Using INtime Wizard The Wizard, which is usable from Microsoft Visual Studio(R) supports a user to

develop RT application. Wizard has a dialogue with a user and generates a basement of source code accordingly.

Generates the following code: – Mailbox or semaphore server thread

– Thread that operates at a regular interval.

– Interrupt handling (detects PCI device)

– Shared memory allocation

– Client thread

Page 34: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support34

Compile and Link (RT application) Necessary files to develop RT application:

<INtime folder>\RT\INCLUDE

rt.h is needed to include for the RT application to call INtime System Calls. Definition will be needed like as “#include <rt.h>”.

<INtime folder>\RT\LIB

ciff3m.lib/ciff3mf.lib C library function interface library.

netf3m.lib Socket library(Network)

pcibus.lib PCI Library

rt.lib INtime API Interface library

rmxiff3m.lib iRMX system call library

rtserv.lib INtime port interface library

ecpp.lib Embedded C++ library

rtpp.lib Embedded C++ library

rtppd.lib Embedded C++ debug library

Page 35: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support35

Compile and Link (NTX application) Necessary files to develop NTX application:

<INtime folder>\NT\INCLUDE

ntx.h is needed to include for Windows application to call NTX System Calls. Definition will be needed like as “#include <rt.h>”.

<INtime folder>\NT\LIB

ntx.lib INtime NTX-API interface library

ntxext.lib INtime NTX-API interface library(extension)

Page 36: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support36

Exceptional Interrupt

Page 37: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support37

Exception RT application operates at 32 bits and exceptional interrupt would be occurred by

CPU, when any application program operates invalidly such as accessing invalid memory area.

You may witness the following exceptions occur:

General Protection Interrupt 13

-Referring more than specified indexes for array. - Accessing non-existed memory area.

Stack Protection Interrupt 12

- Overflow the stack memory occurs.

Page Protection Interrupt 14

- Referring no-allocated physical memory for corresponding virtual memory

Page 38: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support38

Exception Handling

Calling SetRtExceptionHandler() allows a user to decide how it behaves when exceptional error occurs. On the contrary, calling GetRtExceptionHandler() lets a user know it.

As default threads, which causes exceptional error, would be suspended.

Page 39: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support39

Debug

Page 40: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support40

INtime Debug tools INtime software provides the following debugging tools:

1. Spider Debugger (source code debugger)

2. Visual Studio add on debugger: Deburta (source code debugger )

3. INtime Explorer

4. INscope task analyzer

5. SDM/SDB(static system debug monitor)

Page 41: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support41

INtime Debug tools: Source code debugger

Source code debugger allows user to debug program with source code without stopping system.

The target executable needs to be built with debug option.

Visual Studio add-on debugger is integrated in Visual Studio 2003/2005. Just like Windows programming, a user can make break points, referring memory, changing memory value and etc.

For the Visual Studio debugger is not able to debug multi-threads, Spider debugger is provided. A user can switch more than one thread contexts by making breakpoints at the top of the thread loop.

Debugging via network using NTX technology.

Page 42: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support42

INtime Debug tools: INscope Task Analyzer

INscope task analyzer is a great tool to see how multiple threads are switched by INtime kernel.

This tool allows a user to measure threads running time approximately.

INscope shows when INtime APIs call, RT C library call and interruption are occurred on a target thread.

INscope

Page 43: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support43

INtime Debug tools: INtime Explorer – crash log

INtime Explorer can output crash log. This function is useful to investigate application error.

Enabling to watch hardware fault makes this function work. When any exceptional interruption occurs, INtime explorer outputs crash log for the process information such as type of error, suspended code address, register information and etc.

Crash log file might includes those name of the process, thread, source code module, function, local variables and line number where the problem is occurred.

•*********************************************************Crash from file C:\Program Files\INtime\Crash\Local.rtc dd Wed Feb 22 21:48:56 2006

Exception 0x810e (EH_PAGE_FAULT)

in process 77d8

in thread 78f0

at 7873:00416340

•*********************************************************Information on process 77d8

Object type = 1: Process

Current Threads 3 Current objects 27

Container proc 0258 Max Priority 129

Pool min 256K Pool max 6144K

Used 242K Free 13K

Borrowed 232K

Exception mode 0 Exception handler 0280:3f7c

Object directory has 64 entries, 6 in use

KANRI 78f0

R?C_INFO 78a8

R?ENV 8168

R?EXIT_MBOX 78c0

R?H$C$NAME 78b0

R?LRS 1610

……………

Crash log sample

Page 44: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support44

INtime Debug tools: INtime Explorer – crash log #2

Access from file menu [View] of INtime Explorer. [Option] menu will show the INtime Explorer’s option setting.

Select [Watch] tab and check the “Watch hardware fault”.INtime Explorer option setting

Page 45: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support45

INtime Debug tools: SDM/SDB

SDM is built in INtime system as static debug monitor. This debugger stops whole system including Windows to debug an RT application program by outputting information to an serial port, showing with the code with assembler code, and inputting command from the serial terminal connected to the serial port.

SDB is a static debugger, which co-works with SDM. SDB is separated from SDM. If you debug an RT application with using SDB function, you’ll need to run SDB.rta in advance.

SDM/SDB command can be accessed from INtime Help.

Page 46: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support46

Objects

Page 47: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support47

Management of Objects

INtime system is constructed based on a concept of “OBJECTS”.

INtime kernel manages INtime objects with “RTHANDLE”, which is 16 bits (WORD) identical value. Creating INtime objects always returns RTHANDLE. Besides, when creating NTX objects returns NTXHANDLE.

Application program creates, manipulates or delete INtime objects.

There is a limitation for the objects to be existed on system. This limit is configured by GDT(Global Descriptor Table) slot number, which a user can configure using INtime Configuration. The max value is 8190.

Page 48: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support48

Objects Types

PROCESS AN APPLICATION PROGRAM

THREAD PROGRAM CODE

MAILBOX DATA/OBJECTS INTERFACE

SEMAPHORE SOFTWARE EVENTS, COUNTER

SEGMENT CONTINUOUS MEMORY

Typical INtime objects are the followings. These objects are defined those behaviour and the use for each:

Page 49: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support49

Creating Objects Application program can create any INtime object by calling System call(APIs) at any ti

me.

System calls for the creation of objects returns 16-bit valid handle value(RTHANLDE) when the call is successful. There are system calls for each object to create according to objects

All System calls for the creation of objects are defined like as following:

RTHANDLE = CreateRtObjectname(Parameters)

Page 50: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support50

Deleting Objects Application program can delete any INtime object by calling System call(APIs) at any ti

me.

When deleting a object, RTHANDLE value is passed to System call as a parameter. There are system calls for each object to delete according to objects.

All System calls for the deletion of objects are defined like as following:

DeleteRtObjectname(RTHANDLE)

Page 51: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support51

Discriminating Objects System call GetRtHandle() is used for distinguishing type of objects

Type =GetRtHandle(RTHANDLE)

Objects Type

Processes PROCESS_TYPE

Threads THREAD_TYPE

Mailboxes MAILBOX_TYPE

Semaphores SEMAPHORE_TYPE

Memory SEGMENT_TYPE

Page 52: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support52

Naming Objects

RTHANDLE is enough for INtime kernel to manage objects but it is not for users. System call CatalogRtHandle() will make it clear and easier to manage objects so that a user can name objects as he likes according to the rule.

Object name should be up to 12 English one byte characters.

Object Directory is a table for managing handle value and object name.

Page 53: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support53

System calls

Page 54: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support54

RT System calls

RT System calls are used to program RT side of application.

There are two levels for RT calls(High level calls/ Low level calls).

Normally high level calls are mainly used.

There are subset RT System calls(APIs), which mostly are compatible to Windows System calls called as iWin32.

Page 55: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support55

High-Level calls

High-level calls provide lower performance, plus higher protection and validation features.

High-level system calls validate a call's parameters; a condition code returned by the call indicates whether you used invalid parameters.

Usually high level calls are enough to develop RT applications.

Page 56: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support56

Low-Level calls

Low-level calls provide higher performance, but lower protection and validation features. Low-level objects provide functionality beyond that of high-level objects.

You must allocate memory for low-level objects and may allocate memory beyond low-level object needs.

Low-level objects are not protected against unexpected deletion and do not validate parameters.

If you need to validate parameter, use hi-level system calls. Or use low-level objects in these situations:

– Well-tested code for where performance is critical.

Page 57: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support57

NTX calls

Installing INtime, Windows application program can call Windows extensional System call “NTX”.

NTX calls allow Windows applications to operate on objects created by, stored in, and controlled by the INtime kernel. This allows Windows and INtime applications to communicate and coordinate their activities.

All of the NTX System calls include “ntx” as prefix.

Page 58: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support58

iWin32 Subset system calls

iWin32 subset APIs are provided for users who is used to program Windows application.

This subset is suitable when porting from Windows source code to RT side.

Page 59: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support59

System calls references

Following documents are useful to study details of INtime:

– INtime Help( online help files )• Installed in <INtime folder>\help\intime.hlp

– .NET help• For Visual Studio .NET 2003/2005, the help document is integrat

ed in the development tool. API reference is also included.

Page 60: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support60

Processes

Page 61: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support61

What are processes? An RT process is an RT kernel object that contains threads and all their needed resour

ces.

Windows application program should be called as Windows process so as to distinguish.

When you delete processes, the objects associated with them also are deleted.

INtime Application Loader creates RT process when an INtime application program is loaded.

There is no priority for processes.

Page 62: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support62

What is process? #2

RT processes are arranged in a hierarchy like this:

– The root process is always at topmost.

– The parent process creates child processes.

Parent( root )

Thread

Child Child

Page 63: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support63

RT process’s resources

Memory area for program code and data area

Object directory table

Thread objects including initial thread( main() ).

Other objects.

Memory pool.

Page 64: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support64

Object directory table

Object directory table lists object name(cataloged name) and its corresponding handles.

Every RT process has object directory table. When cataloging or looking up handle specify process handle where to catalog or look up.

When process terminates, all of the objects are deleted and so as the object directory table is. However if there is an object, which is cataloged to the object directory, which belongs to the other process, even though the process is deleted, the cataloged name is remained as invalid name, unless the process is deleted. In this case, Lookup call is successful but the handle you get is invalid. You should better to call GetRtHandleType to confirm if the handle is valid, every time looking up an object.

Page 65: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support65

Why catalog objects? For instance, you’ll need to get handle somehow to send a data to a certain mailbo

x. In this case you’ll need a mailbox handle. Among a process, thread can share the object handle by declaring the variable as global, but it is not possible to share the value between different processes.

This is resolved by using the object directory. A thread in process A, which creates a mailbox object catalogs the handle as defined name, the other thread included in process B looks up the handle with the name.

To catalog an object, use the system call CatalogRtHandle() for RT application program and use ntxCatalogNtxHandle() for NTX application program.

To look up an object, use the system call LookupRtHandle for Rt application program and use ntxLookupNtxHandle for NTX application program.

Page 66: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support66

How to create RT processes Use RT application loader Utility to load RT application program(Real time executa

ble: RTA[Real Time Application]) and create RT process.

Call CreateRtProcess() for RT program. It will create RT process dynamically.

Call ntxCreateRtProcess() for NTX program. It will create RT process dynamically.

RT application loader

Page 67: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support67

Threads

Page 68: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support68

What are threads?

Threads, or threads of execution, are the active, code-executing objects in a system.

We call a thread which runs on Windows as “Windows threads” to distinguish from RT threads.

Page 69: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support69

RT thread structure RT threads are completely different from Windows threads. RT thread should be made

as an infinite-loop structured program:

void SampleRT_Thread( void ){ // Local variables WORDstatus;

// Rt thread what to do in advance

while( 1 ){ ; // what to do RtSleep(); }}

RT threads should never exit or return.

Page 70: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support70

Threads Attributes

priority

an instruction pointer

current execution state

suspension depth

ordinary/interrupt (thread type)

process which it belongs to

Register information

address for Exception handler

Page 71: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support71

Manipulating threads

Creating a thread System call CreateRtThread creates thread. CreateRtThread needs the following para

meter to call: – Thread priority

– A pointer to the thread’s start address

– Stack size

– One 32-bit parameter passed to the thread(optionally)

Deleting a thread System call DeleteRtThread makes the thread disassociated from its process, and any

stack space created for it is freed and thread resources are returned to the parent process.

Page 72: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support72

Manipulating threads #2Suspending a thread System call SuspendRtThread makes a thread, corresponding to the handle specified,

suspended. SuspendRtThread is able to use to suspend the caller thread itself(specifying NULL_R

THANDLE) .

Each time you call SuspendRtThread, the suspension depth increases by one. The INtime kernel keeps track of the thread's suspension depth, up to 255.

Resuming a thread System call ResumeRtThread() brings the thread out of suspension.

Each time you call ResumeRtThread, the suspension depth decreases by one. It will be resumed, when the suspension depth is 0.

Page 73: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support73

Thread execution state transitions Any of RT thread exists in one of these execution state:

READY

ASLEEP

ASLEEP-

SUSPENDED

SUSPENDED RUNNING

Create

Resume

Suspend

Wakeup Pre-empt

Dispatch

Sleep

Resume

Resume

Resume

Suspend

Suspend

Suspend Suspend

Wakeup

Delete

未登録

Page 74: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support74

Available thread states

RUNNING – The thread’s instructions execute(CPU is used for the thread).

– Only one thread can execute at a time.

READY – When the thread is able to be running(not running at this moment).

– Creating a thread instantly makes READY state.

ASLEEP – The thread voluntarily waits for something to wake it up.

– A thread goes to sleep because: • It puts itself to sleep for a specified period of time(RtSleep). • It makes a request that cannot run at once and it waits, while the request is not demanded:

– Requests for units to get from a semaphore by calling WaitForRtSemaphore(), or for messages to receive from a mailbox by calling ReceiveRtHandle()/ReceiveRtData().

A thread should be somewhere in the following states:

Page 75: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support75

Available thread states #2

SUSPENDED – One of the followings make a thread suspended:

• The thread suspends itself • The thread is suspended by another thread.

– The number as between 0 to 255 expresses the suspension depth.

– The larger the number is, the greater the depth is.

– When the suspension depth is greater than 0, you must make a corresponding number of ResumeRtThread() calls to bring the thread out of suspension.

ASLEEP-SUSPENDED – When the sleeping thread is suspended by another thread.

– While the thread at this state, INtime kernel continuously checks if the specified time to sleep is expired or not.

– When the specified time to sleep comes to expire, the state moves to SUSPENDED.

– When another thread calls ResumeRtThread() for this thread, the state moves to ASLEEP.

Page 76: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support76

Thread transitional events

“Create” event(READY) – When the thread is created, the thread is in “READY” state.

“Delete” event(NON) – System call DeleteRtThread or ResetRtInterruptHandler makes the thread out of these states.

“Dispatch” event(READY to RUNNING) – The thread becomes running when the thread has one of these characteristics:

• Highest priority of all ready thread. • A higher priority than the running thread. • The running thread is suspended, put in the sleep state or deleted. • The running thread’s round-robin time quota expires, and the ready thread is next in the queue.

Page 77: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support77

Thread transitional events #2

“Pre-empt” event(RUNNING to READY) – The running thread moves to READY state for one of the following reasons:

• A higher priority thread becomes ready. • The thread uses all its time quota in round-robin scheduling.

“Sleep” event(RUNNING to ASLEEP) – The running thread moves to ASLEEP state for one of the following reasons:

• The thread puts itself to sleep for a specified time(RtSleep). • The thread requests something that cannot be done immediately and it waits.(ReceiveRtHandle, WaitForR

tSemaphore and so on).

Page 78: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support78

Thread transitional events #3

“Suspend” event(RUNNING/READY to SUSPENDED) – The thread moves to SUSPENDED state for one of the following reasons:

• The running thread suspends itself. • The thread is suspended by another thread. The suspension depth increases by one each time the thread

suspends.

“Resume” event – When the suspended thread whose suspension depth is 1, is resumed by another thread(Resu

meRtThread()), the thread moves from SUSPENDED to READY, or from ASLEEP-SUSPENDED to ASLEEP.

– If the thread’s suspension depth more than 2, unless ResumeRtThread calls for the same number to the suspension depth are called, the thread still keeps to be SUSPENDED or ASLEEP-SUSPENDED.

Page 79: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support79

Priority

The priority is an integer value from 0 through 254, with 0 as the highest priority. The lower the number is, the higher the thread priority is.

Priority value is specified when the thread is created.

System call SetRtThreadPriority() allows you to change the thread priority dynamically.

Process has the max priority level. You cannot create a thread whose priority exceeds the limitation. The limitation of the process max priority can be changed by calling System call SetRtProcessMaxPriority().

Entire Windows acts as the realtime thread whose priority is 254.

- The priority level is divided into 256 from 0 to 255. While Windows thread is active, the priority 255 thread actually doesn’t work at all.

Page 80: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support80

Initial thread

When creating RT process, the main() function is called as process entry point. This means process always should have at least one thread. This thread is called as “Initial thread” for this thread is created by OS at beginning.

Initial thread( main() ) can “return”. This means that process is deleted at this point.

Page 81: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support81

Round-Robin Scheduling

If you assign the same priority to more than one thread, the first thread that runs may continue running indefinitely. Using round-robin scheduling makes ensure that each thread gets an opportunity to run.

When the thread works at the round-robin, Windows looks like freezing. The important thing is to design threads not to work at round-robin in the first place.

At the round-robin scheduling, all the same priority threads uses CPU equally.

To set up round-robin scheduling, set the threshold priority level and the amount of time the thread runs(time slices) via INtime Configuration panel (default threshold priority is set as 140, the time is 50ms).

while( 1 ){ i++;}

Page 82: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support82

RtSleep

For the specified parameter for RtSleep would be rounded by the high-level tick value(10ms).

High-level tick is a period of 10 milliseconds, which is created based on INtime kernel tick configured at INtime Configuration panel.

If you need to sleep less than 10 ms, call RtSleepEx() or low-level call knRtSleep(). RtSleepEx() enables a thread to sleep by the unit 1ms unit. knRtSleep() enables a thread to sleep less than 1ms. However, the parameter for the knRtSleep() is the count of the Kernel tick. That means depending on the configuration of the kernel tick, the sleep time changes.

Page 83: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support83

How does RtSleep work?

The following diagram illustrates how a thread behaves when calling RtSleep(20). Actual sleep time is not 20ms but the period of time until it counts up to the two high-level ticks.

Ammount of time

0ms 10ms 20ms 30ms 40ms

thread to workt High-Level tick times

10ms

RtSleep ( 20 )Two High-level ticks

RtSleep ( 20 )Two High-level ticks

Page 84: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support84

Stack memory Stack memory is constructed as FIFO structured memory.

When you create a new thread, you’ll need to specify the stack memory size for the thread.

Every thread has a stack memory. And this memory area is usually used for:– local variables of a thread.

– the return value of function calls that are called in a thread.

– the return address for function calls that are called in a thread.

– the work area for interrupt handler.

Stack memory size would be needed to change depending on how much size of local variables(arrays) have and how deep the nesting of function calls

This size should be specified in 4096 Byte units.

Page 85: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support85

How the stack memory is used Local variables are existed on the stack memory so that they wouldn’t be tampered

between multiple of threads. However global variables are existed in the common data area where all the threads that are included in a process can access and if there are multiple threads in a process, that access global variables, they might tamper them each other.

Thread's localvariable

Thread's localvariable

Thread's localvariable

Argument for afunction call

Return address for afunction call

Function's localvariables

MyFunction( &byA ,&byB ) returnBYTE byA ,byB

How the stack memory is used

Page 86: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support86

STEP1 “Hello INtime”

Page 87: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support87

Using INtime Wizard

Here we are going to create a thread that operates:

– at priority 170 – to output “Hello INtime” to console periodicall

y at 500ms interval.

Start up Visual Studio, select [New] –> [New Projects] from [File] menu.

INtime Wizard is already installed. Select “INtime RT Process” to create common INtime application program in general and name it “Hello” as the project name.

As INtime Wizard starts, answering questions to some dialogues, it will generate a frame of source code and project workspace setting accordingly.

Page 88: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support88

Using INtime Wizard #2 Follow the steps like this:

1. Selection of the wizard type Select “A full-featured application”.

2. Choosing INtime objects Select “Thread operates at a regular

interval”

Selection of INtime objects to add-in

Page 89: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support89

Using INtime Wizard #33. Setting of the objects

Select “A full-featured application”.- Click [Add element] to configure object:- [Polling thread1]

Method for waitingSleep

Number of

milliseconds to wait500

- [Thread priorities]

Thread priority: 170

Stack size in kilobytes: 4

4. Editing global setting for a project Select -global- from a list box for “Elements in real

time process”, then click [Edit element]. This allows you to configure global setting of the projects. Leave them as default here and click [OK].

Clicking [Generate process] will bring up the last dialog to show how the project would be created. Click [OK] to generate a project

Editing of thread objects

Editing global setting

Page 90: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support90

Editing source code As the next step, we are going to add function for the project. The function

we’re going to put is ‘to output “Hello INtime”’ to console. Find PollThread() from the file Poll1.c. This is the code for INtime thread object. We’re going to put the function where it shows “TODO:”

printf(“Hello INtime\n”);

That’s it. Editing source code is completed.

Page 91: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support91

Build

After the editing source code, we’re going to build them to create an executable.Select [Build] -> [Active setting] to change the active setting from “debug” to “release”. Select “Hello- Win32 Release” to build an executable as “Release ” configuration and click . This will create Hello.rta in “Release” folder where locates under the project directory.

If you select “Debug” to build, the application program will show some debug messages.

Page 92: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support92

Starting an RT application program

To start an RT application program, use RT Application Loader. You can invoke this utility from short cut menu [Start]-[Program]-[INtime]-RT Application Loader.

Select INtime node as “Local” by clicking [Node]. Then find the application using the directory selection function and click [OK].

Or you can invoke RT Application only with double click the file icon for the extension .RTA is associated with RT Application Loader.

RT application loader

Page 93: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support93

INtime RT Console

The application we created at STEP1 outputs “Hello INtime” to console every 500ms. If you try to kill the console, it automatically opens again, as long as the application is alive. This console is called RT Console, which is provided for console I/O for RT Application program.

RT console continues to show itself until the process is deleted.

Page 94: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support94

Stopping an RT application program

INtime Explorer(INtex) INtime Explorer is powerful utility to pr

ovide users to manipulate INtime objects. INtime Explorer usually is used for browsing INtime objects that are included in user process, deleting user process and debugging user process.

We’re going to delete “Hello.rta” process using INtime Explorer here.

INtime Explorer

Page 95: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support95

Dynamic memory( shared memory )

Page 96: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support96

What is dynamic memory?

Dynamic memory supports many uses, including communicating and storing data.

INtime software includes system calls that allocate memory(AllocateRtMemory) and free memory(FreeRtMemory) and create handles for allocated memory to share with other processes(CreateRtMemoryHandle).

INtime kernel manages memory in 4KB unit (page) for 32-bit virtual memory space structure.

Page 97: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support97

Manipulating dynamic memoryHow to create shared memory System call AllocateRtMemory() allocates continuous memory in 4KB unit and returns

its address. Memory size to allocate should be passed as a parameter. To share memory, it is efficient to allocate memory in 4KB unit. System call CreateRtMemoryHandle() returns memory handle, passing allocated mem

ory address and its size. The memory is allocated from process memory pool. The allocated memory pointer(address) is valid in a process. By declaring the pointer v

ariable as global, threads can access the memory.

How to delete the shared memory System call DeleteRtMemoryHandle only deletes handle and it doesn’t free the allocat

ed memory. System call FreeRtMemory will free and the allocated memory, which is pointed to the

address that is passed as parameter. All memory that belong to a process will be returned to memory pool when the process

terminates.

Page 98: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support98

Manipulating dynamic memory #2

How to get size of shared memory System call GetRtSize() allows you to know the allocated memory size from its handle

in BYTE unit.

Page 99: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support99

Memory Fragmentation

Frequent allocating and freeing memory might cause memory fragmentation.

Fragmentation is referred to the condition of RAM that have small, unused holes scattered throughout it. Fragmentation occurs naturally when you allocate and free memory frequently. At some point, application fails allocating certain size of memory as continuous, even if there is enough space for the size in total.

The larger the memory size is and more time for allocating and deleting, the more frequent the fragmentation occurs.

Assuming the fragmentation, application would be better to be designed to allocate necessary memory in advance and not to delete it as possible.

Page 100: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support100

Memory Fragmentation #2

Memory fragmentation

NOT USED

NOT USED

NOT USED

44KB

4KB

8KB

16KB

20KB

24KB

28KB

32KB

36KB

40KB

0KB

USED

USED

USED

Page 101: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support101

Virtual Memory

RT application program is built as 32-bit flat model.

For the flat model, application program behaves as if it owns 4GB of continuous memory space and the code for the program and process itself are located in that memory space.

This logical memory address space is called as virtual memory space. The memory pointed by the address doesn’t point actual physical memory address.

Assume there are two process(A and B). The valid address 0x4521000 for memory address to the process A would never be valid to the process B. The valid memory address is closed within the process’ virtual address space.

Page 102: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support102

Shared memory and Mapping

Creating memory handle by calling CreateRtMemoryHandle(), passing the memory handle to another process, then the process that gets the memory handle is able to access to the memory.

Memory handle usually exchanged via mailboxes.

You can share the memory between NTX applications.

To get the memory address to access from the memory handle, which is given, system call MapRtSharedMemory() is called. This call maps the memory area indicated by the handle to process’ virtual memory space and returns its mapped address. For NTX application, use ntxMapRtSharedMemory() call.

Page 103: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support103

Shared memory and Mapping #2

When a process doesn’t need to access to the mapped memory space, unmapping should be needed. For RT application program, FreeRtMemory() by passing the mapped address as parameter. For NTX application program, ntxUnmapRtSharedMemory() by passing the memory handle.

For physical memory access, you can call MapRtPhysicalMemory() by passing the address to map and its size.

Page 104: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support104

STEP2 How to use dynamic memory

Page 105: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support105

Using memory allocated dynamically

The following code illustrates how to access memory which is allocated by AllocateRtMemory() call. The return value of the AllocateRtMemory() is defined as void address so that you’ll need to cast as defined type.

struct   MYMEM{char mbMessage[64];WORD mwCount;

};//--------struct MYMEM *pp = (struct MYMEM *)AllocateRtMemory( sizeof(struct MYMEM) );p-> mwCount = 100 ;

Page 106: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support106

Semaphores

Page 107: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support107

What are semaphores?

A semaphore is a counter that takes positive integer values called “units”. Thread release units to and wait for units from the semaphore. A semaphore can:

– Synchronize a thread’s actions with other threads. – Provide mutual exclusion for data or a resource.

There are system calls for creating/deleting a semaphore and also releasing/waiting for units to/from a semaphore.

When a thread releases n units to a semaphore, the units are counted n up.

When a thread waits for n units and gets them, the units are counted n down.

Page 108: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support108

Manipulating a semaphoreCreating a semaphore System call CreateRtSemaphore() is used. Specify the initial number of units in the custody of the new semaphore. Specify whether the thread queue is FIFO-base or priority-base.

Deleting a semaphore System call DeleteRtSemaphore is used. INtime kernel awakens any threads waiting for units at the semaphore with an E_EXIS

T condition code(error).

Page 109: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support109

Manipulating a semaphore #2Waiting for units System call WaitForRtSemaphore() waits until it can get specified count of u

nits from a semaphore. You can specify amount of time to wait(0 to infinite(WAIT_FOREVER)). A thread can be awaken from the waiting state when:

– it can get the specified units from a semaphore– specified amount of time to wait expired. – The semaphore the thread is waiting at is deleted.

Releasing units System call ReleaseRtSemaphore() releases specified count of units to a semaphore.

You cannot release 0 units or more units than the maximum number of units the semaphore is allowed to have.

Page 110: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support110

WaitForRtSemaphore and its timeout

You need to specify how long a thread waits for specified units to get when calling WaitForRtSemaphore(). A thread will wait until when the specified units can be gotten or the specified time to wait is expired.

In case a semaphore already counts as same number of units as a thread waits for, the thread gets units and wake up immediately. At the same time, the semaphore decrements the unit counts.

Assume that you specify 1000 for the time to wait, a thread will be waiting for 1000ms(1s) at maximum until it can get specified count of units. If the semaphore counts the units up to the number that the threads wait for, the thread will wake up immediately without waiting such long.

Page 111: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support111

WaitForRtSemaphore and its timeout #2

Specifying the time to wait as infinite(WAIT_FOREVER), a thread will never wake up from the call, until it can get specified number of units, except the semaphore is deleted.

Specifying the time to wait as 0(NO_WAIT), a thread will figure out whichever it can get the units or it cannot with error immediately.

Page 112: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support112

Thread queue for semaphores By calling System call WaitForRtSemaphore(), a thread is placed in a waiting

queue(thread queue). If multiple threads wait for units from a semaphore at same time, how it work

s is defined by the thread queue. When creating a semaphore, you need to choose a thread queue between FI

FO base and priority base. Usually FIFO base is used.

FIFO base queue A thread comes at queue in FIFO order, no matter thread priority (FIFO: Fast

-In-First-Out). The faster a thread calls WaitForRtSemaphore, the faster it can wake up.

Priority base queue High-priority threads do not wait behind lower-priority threads in the queue. The higher priority thread, in the queue, is, the faster it can wake up.

Page 113: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support113

Synchronizing threads Using a semaphore, you can make multiple threads’ operation synchronized.

Multiple threads(worker threads) should be on a rule that they wait for a unit to get from a semaphore for each.

A thread(master thread), which orders to make them act, release units as the same number of the worker threads. Those worker threads get 1 unit for each so that they all can be active.

In this case, the maximum unit count, the releasing units count and the worker thread would be equal.

Page 114: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support114

Synchronization

Synchlonization

ACTIVE

Master thread

ACTIVE ACTIVE

STOP STOP STOP

Worker threads

Page 115: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support115

Mutual excluding threads Using a semaphore, you can guard a resource from threads’ accessing to it

at same time.

A semaphore is created as 1 unit initially(an official right for dinner).

Every thread(people) should be on a rule that it waits 1 unit and it is not allowed to access the resource until it gets the unit.

When the thread, which could get the unit, completes its task using the resource(dinner), it should release 1 unit(an official right for dinner) to the semaphore.

Page 116: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support116

Mutual exclusion

Mutual exclution

$

VISIO CORPORATION

受付

WAITWAIT GET ACTIVE

1 UNIT

Page 117: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support117

Deadlock The condition that a thread can never wake up.

Deadlock often occurs at the development of application at primary stage or this is caused by design error.

Assuming that two threads(thread A and thread B) access a resource exclusively using a semaphore and thread A is now active and thread B is waiting for the unit released. At this moment, if thread A was suspended by error, or thread A didn’t release the unit to the semaphore by mistake, not only thread B but also thread A cannot active again.

Page 118: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support118

Event flag Some real time OS might have Event flag object.

Event flag is something similar to a semaphore object but it is different at the point that the event flag have several pattern to make a thread to wait(Event flag has AND/OR condition). However the event flag is system global variable and tends to be the factor that makes portability worse.

There is no Event flag and its substitute for INtime.

Page 119: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support119

STEP3 Synchronization using a semaphore

Page 120: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support120

Semaphore method Sample

Here we’re going to learn how to use a semaphore.

The sample Step3 is illustrates the example 2 threads controlled by a threads to synchronize. The semaphore is used as more like software events. Here the event is issued by calling ReleaseRtSemaphore() and the event is waited by calling WaitForRtSemaphore().

Page 121: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support121

Mailboxes

Page 122: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support122

What are mailboxes?

Mailboxes are INtime objects that threads exchange information by sending messages to and receiving message from mailboxes.

Feature of mailboxes A thread can receive messages from multiple threads.

The messages are queued in mailbox queue, and received by a thread in order.

Mailboxes have two different types. One is Object type and the other is Data type. When creating a mailbox, the type is decided:

– Object type : The message is object handle(RTHANDLE) – Data type : The message is a data stream at maximum 128 Bytes.

Page 123: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support123

Manipulating a mailboxCreating a mailbox System call CreateRtMailbox() is used. Specify whichever the mailbox passes data or objects. Specify whether the thread queue is FIFO-base or priority-base.

Deleting a mailbox System call DeleteRtMailbox is used.

Sending messages(object) By calling SendRtHandle, a thread sends an object handle to specified mailb

ox. SendRtHandle is only valid to an object type mailbox. You can specify response mailbox handle so that the thread, which gets the

messages, can send messages back.

Page 124: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support124

Manipulating a mailbox #2Receiving messages(object) By calling ReceiveRtHandle(), a thread waits to receive an object handle fro

m a specified mailbox.

ReceiveRtHandle is only valid to an object type mailbox.

You can specify the time to wait messages between 0ms to infinite(WAIT_FOREVER).

A thread can receive a response mailbox, which it should send message back to, when it receives messages.

A thread wakes up when it receives messages, when the specified time to wait is expired, or when the mailbox, which the thread expects to get message from, is deleted.

Page 125: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support125

Manipulating a mailbox #3Sending messages(Data) By calling SendRtData(), a thread sends stream data(maximum 128 Byte).

SendRtHandle is only valid to a data type mailbox.

Receiving messages(Data) By calling ReceiveRtData(), a thread waits to receive stream data(maximum 128 Byte)

from a specified mailbox.

ReceiveRtData is only valid to a data type mailbox.

You can specify the time to wait messages between 0ms to infinite(WAIT_FOREVER).

A thread wakes up when it receives messages, when the specified time to wait is expired, or when the mailbox, which the thread expects to get message from, is deleted.

Page 126: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support126

Two queues for a mailbox A mailbox has two queues. The one is called as a mailbox queue. The other

one is a thread queue, which is as the same as the semaphore’: – mailbox queue – A thread queue

Page 127: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support127

Mailbox queue Mailbox queue is, in general, considered as “message queue”. All the

messages that are sent to the mailbox comes into the mailbox queue. Those messages are received by threads in FIFO manner. The behaviour is different, depending on those two types(Object type and Data type ):

Page 128: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support128

Mailbox queue #2Mailbox queue for object(Object type mailbox) The queue is created by INtime kernel automatically when creating a mailbox

and the size of queue should be large enough to hold messages. When more objects arrive than the queue can hold, INtime kernel creates a

temporary overflow queue that holds up to four messages. The overflow queue is not deleted until the queue empties.

Mailbox queue for data(Data type mailbox) Data mailboxes have a default queue size of three messages, 128 bytes

each. When more messages arrive than the queue can hold, INtime kernel creates

a temporary overflow queue that holds up to 400 bytes. The overflow queue is not deleted until the queue empties.

Page 129: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support129

Thread queue for mailboxes Basically all the idea about the thread queue is as the same as semaphore’s.

For there is a difference of system calls between those two objects like as the following:

FIFO base queue A thread comes at queue in FIFO order, no matter thread priority (FIFO: Fast

-In-First-Out). The faster a thread calls ReceiveRtData/Handle, the faster it can wake up.

Priority base queue High-priority threads do not wait behind lower-priority threads in the queue. The higher priority thread, in the queue, is, the faster it can wake up.

Page 130: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support130

ReceiveRtData/Handle and its timeout You need to specify how long a thread waits for a message, when calling Receive

RtData/Handle(). A thread will wait until when it can get a message from the mailbox queue or the specified time to wait is expired.

In case there already have messages in the mailbox queue, the thread gets a message and wake up immediately. At the same time, a message is deleted out of the mailbox queue.

Assume that you specify 1000 for the time to wait, a thread will be waiting for 1000ms(1s) at maximum, until it can get a message. If there have messages in the queue, the thread will wake up immediately without waiting such long.

Specifying the time to wait as infinite(WAIT_FOREVER), a thread will never wake up from the call, until it can get a message, except the mailbox is deleted.

Specifying the time to wait as 0(NO_WAIT), a thread will figure out whichever it can get a message or it cannot with error immediately.

Page 131: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support131

Replying messages A message is handled by a thread, which receives it. There might be sometimes yo

u want send a message back. You can do it by using Object type mailbox communication.

For replying messages, all you have to do is to call system call according to the following rules for both sending thread and receiving thread:

1. The sending thread calls SendRtHandle with specifying a response mailbox, which the returned messages would be sent back.

2. The sending thread waits to receive a message, which should be received at the response mailbox.

3. The receiving thread replies after it receives the message. So it checks the response mailbox, if it is a mailbox object or not, and send a replying message back to the response mailbox.

4. Sending thread will receive a message as the response from the response mailbox.

Page 132: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support132

Message buffer queue sample Sending/Receiving a memory handle to/from mailbox, threads almost can communi

cate messages without a size limit. However it possibly may cause fragmentation, resource exhausting or tampering data between multiple thread if a single memory handle is used as the message. To resolve this, the following technique is recommended especially for communication between a Windows thread and an RT thread:

– Allocating memory, creating its handle, which is used for messaging, for multiple times and sending those empty messages to so-called message-buffer-mailbox in advance.

– When sending thread needs to send a message, it receives one of empty messages from the message-buffer-mailbox, maps the handle and edit the memory, and sends a message with specifying the message-buffer-mailbox as a response mailbox.

– After the receiving thread gets a message and completes its work with the memory, it sends the memory handle back to the response mailbox(message-buffer-mailbox), so that the memory handle would be returned to the buffer.

This technique is quite effective for communication, which doesn’t need to synchronize between two threads. E.G.: When NTX application program draws a line graphically, according to the data streams that are sent from RT application program.

Page 133: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support133

Message buffer queue sample #2

///////////////////////////////////////////

Creating message-buffers mailbox

///////////////////////////////////////////

// Creates a mailbox for queuing message-buffer.

hRespMbx = CreateRtMailbox( OBJECT_MAILBOX | FIFO_QUEUING );

// Creates 10 of message buffers to queue.

for( wLp=0 ;wLp<10 ;wLp++ ){

lpBuf = AllocateRtMemory( size );

hMem = CreateRtMemoryHandle( lpBuf ,size );

SendRtHandle( hRespMbx ,hMem ,NULL_RTHANDLE );

}

Page 134: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support134

Message buffer queue sample #3/////////////////////////////////////////// Sending thread///////////////////////////////////////////  while( 1 ){ // Get a message buffer from the message-buffer mailbox. hMem = ReceiveRtHandle( hRespMbx ,NO_WAIT ,NULL ); // Map the memory by handle. lpMapedBuf = MapRtSharedMemory( hMem ); TO DO with the memory // Freeing the mapped memory area FreeRtMemory( lpMapedBuf ); // Send a message to receiving thread.// we specify a response mailbox as the message-buffer-mailbox. SendRtHandle( hTargetMbx ,hMem ,hRespMbx ); }

Page 135: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support135

Message buffer queue sample #3/////////////////////////////////////////// Receiving thread///////////////////////////////////////////  while( 1 ){

// Waits for the message at a mailbox until a message arrives. hObj = ReceiveRtHandle( hTargetMbx ,WAIT_FOREVER ,&hReturnMbx ); // Maps the memory by a handle it has received. lpDataTop = MapRtSharedMemory( hObj ); TO DO with the memory. // Freeing the mapped memory area. FreeRtMemory( lpDataTop ); // For the message is no needed now, send the memory handle back to buffer mailbo

x. SendRtHandle( hReturnMbx ,hObj ,NULL_RTHANDLE ); }

Page 136: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support136

How to design threads For development of realtime multi-thread application, it is very important to define

tasks to make them each thread and those tasks priority for each in advance. It also would be needed to design the connection between all tasks as a diagram or something (how to signal[Interrupt?/semaphore?/mailbox? etc]) .

If you define tasks too detail and divided them into 1000s of pieces, that also brings up OS’ loads(overhead for switching threads). The worse thing that this may possibly make OS’s affectivity down and application’s maintainability down. You’ll need to decide if you make the task as a thread or just a function call .

E.g. Creating threads for an application:

Measuring thread Control Thread

Print –out thread

Data sending thread Log output thread Watching Enter-key thread

Data receiving thread Console output thread

Page 137: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support137

Polling thread(operates at regular interval) Threads operate at regular interval. Use RtSleep() or RtSleepEx() to mak

e thread operate cyclical at the decided interval:

void IntervalThread( void ){ while(1){ RtSleep( 250 ); ToDo( ); }}

Page 138: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support138

Server thread(event driven) A thread waits until they receive a message. When they receive a message, they

operate. : This kind of thread usually waits forever without timeout so it almost is at ASLEEP

state. As you configure the thread as high-priority thread, the thread operates as soon as

it receives the message. On the contrary, if you make the thread as low-priority thread, it would operate while it has to spare.

Using a object mailbox allows you to respond the request(e.g.: status code according to the request or something):

void EventThread( void ){ RTHANDLEhResp , hSeg; MySTRUCT*my; while(1){ hSeg = ReceiveRtHandle( hMail , WAIT_FOREVER ,&hRes ); my = (MySTRUCT *)MapRtSharedMemory( hSeg ); ToDo( ); my->status = SUCCESS; FreeRtMemory( my ); SendRtHandle( hRes ,hSeg , NULL_RTHANDLE ); }}

Page 139: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support139

Client thread A thread, which makes a signal, or sends a message to do to a server thread. Using a object mailbox allows you to get the response from a server thread.

void ClientThread( void ){ RTHANDLEhSeg , hResMail; MySTRUCT*mine; mine = AllocateRtMemory( sizeof(MySTRUCT) ); hSeg = CreateRtMemoryHandle(mine, 4096); hResMail=CreateRtMailbox(OBJECT_MAILBOX|FIFO_QUEUING ); while( 1 ){

mine->data = 1234;SendRtHandle( hMail , hSeg , hResMail );hSeg=ReceiveRtHandle(hResMail , WAIT_FOREVER ,NULL );if ( mine->status != 0 ) ErrorDo( );

}}

Page 140: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support140

STEP4 Communication between threads

Page 141: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support141

Communication between threads#1

Assuming the following application system. The application system:

-   needs to exquisite data from external 4 channels of sensors.

-   needs to output the acquisition data for each channel.

-   Needs to output data to a log file(C:\LOGDATA.TXT) in every 50 times of

the acquisition.

** Here the data is not actually able to be acquired from those sensors. We use pseud-data for each channel.

Tasks TO DO PRIORITYInstrumental Thread (CH0) 180

Instrumental Thread (CH1) 181

Instrumental Thread (CH2) 182

Instrumental Thread (CH3) 183

Log Output Thread Semaphores 190

Console Output Thread 191

Page 142: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support142

Communication between threads#2We’re going to use data mailboxes to communicate this time.

Those instrumental threads generate value from 0 to 260. Each thread has its own

cycle to generate(0ch: 100ms, 1ch 200ms, 2ch 300ms, 3ch 400ms). Here, the data

should be counted 10 up for every cycle.

Define the data structure used for the communication this time.

typedef struct {

BYTE ch ; // Channel number

WORD value ; // value

BYTE reserved[125] ; // reserve

}SCANDATA ;

Page 143: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support143

Communication between threads#3

Console Output Thread

Console Output Thread waits for a request from each Instrumental Thread. When it receives a message, it accesses the member variable “ch” to know which channel of data should be output. Using TAB character depending on the channel and output the “value” to INtime RT console.

Page 144: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support144

Communication between threads#4

Log Output Thread

Log Output Thread waits for a request from each Instrumental Thread. When it receives a message, it accesses the member variable “ch” to know which channel of data should be output. It outputs the data to “C:\LOGDATA.TXT”. To access file, use a standard-C library call(fopen(),fclose(),fprintf() etc)

CH:0 VALUE:240

CH:1 VALUE:240

CH:0 VALUE:230

CH:2 VALUE:240

CH:0 VALUE:220

CH:3 VALUE:240

CH:1 VALUE:230

CH:0 VALUE:210

CH:0 VALUE:200

CH:2 VALUE:230

CH:1 VALUE:220

CH:0 VALUE:190

Page 145: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support145

STEP5 Communication with Windows

application

Page 146: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support146

Communication with Windows application

We’re going to modify the program, which we created at STEP4, to display data as chart on Windows application using Visual C/C++.

We’re not creating the Console Output Thread at STEP4 here and moving the function to Windows thread.

Page 147: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support147

I/O Programming

Page 148: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support148

Selecting devices By installing expansion boards, you can add some hardware functionality

to standard PC.

You’ll need to study which expansion board to take among various products(Which type of board you need, which vendor you choose, and etc).

Page 149: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support149

Intelligent type This type of expansion board in general equips its own CPU and some

commands interfaces are provided to control. And it helps to reduce host CPU loads.

Advantage: ・ Reducing host CPU loads down.

・ It would be easier to program by using the command interface.

・ Interruption is less important because the CPU can operate on its own.

Disadvantage: ・ Protocol with the CPU is defined and fixed.

・ It wouldn’t be easy to tweak the fixed functionalities.

・ Cost much.

Page 150: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support150

Non-intelligent type This type of expansion board in general only has necessary

hardware(controller), so that host CPU should take care of initializing the board or to output/input data.

Advantage: ・ Flexibility.

・ Cost not too much.

Disadvantage: ・ Raising host CPU loads up while accessing.

・ Program tends to be more complex.

Page 151: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support151

I/O interface When a program talks to hardware, it accesses to I/O ports in general.

I/O ports have addresses, just like memory.

A port is configured as 8 bit[1 BYTE], and usually each bit is defined.

There are instructions of CPU to access I/O ports. To write, OUT is used and to read, it is IN.

These instructions(IN/OUT) allow an application program to control hardware.

Page 152: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support152

Dual Port RAM interface Some devices include Dual Port RAM(common memory)

Intelligent devices usually have this.

Host PC allocates unused memory address space for Dual Port RAM so that both host CPU on the PC and device CPU can talk by reading/writing each other accessing to that address.

When accessing a specific physical address space, use MapRtPhysicalMemory().

Page 153: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support153

ISA devices “ISA” is short for Industry Standard Architecture Bus. And this is the bus

architecture used in the IBM PC/XT and new PC/AT. Standard devices such as a keyboard, a mouse, COM(serial) ports, and so on usually are implemented as legacy ISA devices.

Usually an ISA device has static port addresses and their range.

For ISA device interruption signal is edge trigger, an interrupt line is not able to share with multiple devices.

When programming to control ISA device, static addresses(real number) are used for I/O ports or memory, because it should not be changed.

ISA bus is not so fast to transfer data. You’ll need to be careful to access the device on a platform, which has hi-spec CPU. Program sometimes needs to make a delay to access ISA bus.

Page 154: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support154

PCI devices “PCI” is short for Peripheral Component Interconnect. And this is a local

bus standard nowadays.

Devices on PCI bus are dynamically configured by Operation System with Plug and Play technologies, and allocated its own I/O ports address, memory address and interrupt line.

Program needs to get I/O port base address and IRQ to control a PCI device.

INtime PCI library provides APIs that you can easily get PCI information.

Page 155: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support155

I/O functions INtime application program can access I/O port directly.

I/O port access functions are provided as program can access to the port depending on its size:

data8 = inbyte( port address )

data16 = inhword( port address )

data32 = inword( port address )

outbyte( port address , data8 )

outhword( port address , data16 )

outword( port address , data32 )

Page 156: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support156

Handling external signals There are two options to handle external signals by a application

program. The one is to poll and the other is to use hardware interruption.

The application program working in realtime environment would need to poll at certain interval. If it polls without to sleep, it makes the lower priority threads cannot operate at all(in case of INtime, Windows also is included in the lower priority threads).

Generally, hardware interruption should be used for realtime system as possible for the polling tends to use CPU too much for nothing.

Page 157: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support157

To poll external signals Polling external signals reads I/O ports every cycle to act.

Advantage:・ Easy to program

Disadvantage:・ it might not be able to get signal depending on the pulse width.(with interval)

・ No good for multithreading(without any interval)

while( 1 ){

RtSleep(10);

data = inbyte (PORTADR);

if(( data & 0x01 ) == 0x01 )

// TO DO: when it is signaled

}

Page 158: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support158

To poll external signals #2

Ammount of time

Polling external signals

A threadoperation

0ms 1ms 2ms 3ms 4ms

SignalState

Missed the pulse !!!

Time Lag

Could bedetected

ON▼

ON▼

ON▼

Page 159: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support159

To handle a hardware interruption To handle a hardware interruption, it is needed that program register an

interrupt handler for a certain IRQ level to CPU to be signaled in advance. When the hardware signals, a running thread is temporarily interrupted, and the handler is operated.

When all the interrupt handling completed, the running thread can continue to operate from the point it is interrupted.

Advantage:

・ It possibly can detect short width of pulse.

・ Low loads for systems.

Disadvantage: ・ Handling interrupt signals would be rather complex, comparing to polling.

Page 160: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support160

To handle a hardware interruption #2

Ammount of times

Handling interrupt signals

a threadoperation

0ms 1ms 2ms 3ms 4ms

Signalstate

ON▼

ON▼

ON▼

Page 161: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support161

Digital I/O Assume that there is a digital signal board which has got 32 bit

input/output which has 4 ports for input, and also 4 ports for output and each port is 8 bit[1byte].

To input/output a signal from/to a certain port, using the following functions for each.

data8 = inbyte( portaddress )

outbyte( portaddress , data8 )

Page 162: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support162

I/O information for a device Expansion board vendor provides a hardware technical reference, which

includes I/O ports information to control the hardware. There should be explanation about register information to access for the control.

PCI device is dynamically configurable so the technical document may describe by offset address.

Address READ WRITE

Base + 0 Group 0 (Sw) Base + 1 Group 1 (Sw) Base + 2 Group 2 (Sw) Base + 3 Group 3 (Sw) Base + 4 Group 4 (LED)

Base + 5 Group 5 (LED)

Base + 6 Group 6 (LED)

Base + 7 Group 7 (LED)

Page 163: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support163

Connection sample

CN1

Group 0Group 1

Group 2Group 3

Group 4Group 5

Group 6Group 7

0 0 0 1 1 0 1 0

Bit1 Bit0Bit2Bit3Bit4Bit5Bit6Bit7

SW

0

SW

1

SW

2

SW

3

SW

4

SW

5

SW

6

SW

7

Group 2

Page 164: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support164

Detecting PCI devices PCI devices usually have Ids on nonvolatile memory on the board.

Usually you detect a certain PCI device using those Ids as key codes.

Vendor ID

Id number for vendors.

Intel: 0x8086

CONTEC: 0x1221

Device ID

ID numbers for devices. All devices have this own ID. These are defined by vendors who provide devices according to their own rule.

Page 165: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support165

Example for detecting a PCI device BOOL LookupPCItarget(WORD wPciVendorId ,WORD wPciDeviceId ,WORD *lpwIObase)

{

PCIDEV dev;

*lpwIObase = 0;

dev.wVendorId = wPciVendorId;

dev.wDeviceId = wPciDeviceId;

dev.wDeviceIndex = 0; // First device to detect

if (!PciFindDevice( &dev ))

return FALSE; // No device can be found

 

*lpwIObase = (WORD)dev.dwBaseAddr[0] & 0xFFFC;

return TRUE;

}

VENDOR ID : VENDID 0x1221DEVICE ID : DEVID 0x8112

Page 166: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support166

How to use PCI library calls For RT application program, you’ll need to include the header file and to li

nk a library file to use PCI library calls:

Header file

#include <pcibus.h>

Library file

<INtime folder>\rt\include\pcibus.lib

Passing vendor ID, device ID, device index of the device you’re going to control as parameters for PciFindDevice() call allows you to get the device information which includes I/O base address, memory address, IRQ number and etc.

Page 167: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support167

PCIDEV structure typedef struct {

WORD wBusNum;WORD wDeviceNum;WORD wFunction;WORD wVendorId; // Vendor IDWORD wDeviceId; // Device IDWORD wDeviceIndex; // Device IndexWORD wCommand;WORD wClassId;BYTE byInterfaceId;BYTE byRevId;BYTE byCLS;BYTE byLatency;DWORD dwBaseAddr[6]; // Base addressesDWORD dwCIS;WORD wSubSystemVendorId;WORD wSubSystemId;DWORD dwRomBaseAddr;BYTE byIntLine; // IRQ numberBYTE byIntPin;BYTE byMaxLatency;BYTE byMinGrant;

} PCIDEV, *LPPCIDEV;

Page 168: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support168

STEP6 Multitasking and Signal Output

Page 169: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support169

Multitasking and Signal Output Creating 4 threads which output 8 bit data to each port.

 

Initial thread( main() ) creates 4 threads( thread1-thread4)

Thread1 PRI:170 CYCLE:100ms Port: BASE+0 DATA: incremented by 1

Thread2 PRI:171 CYCLE:200ms Port:BASE+1 DATA: decremented by 1

Thread3 PRI:172 CYCLE:300ms Port:BASE+2 DATA: left rotation by 1

Thread3 PRI:173 CYCLE:400ms Port:BASE+3 DATA: right rotation by 1

Page 170: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support170

Handling Interruption

Page 171: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support171

To handle a hardware interruption A hardware interruption is handled with an interrupt handler and an

interrupt thread for INtime application program.

IDT TABLE

Interrupt Handler

Interrupt Thread

While(1){

}

Interruption Signal External signal

Checks what is going on the device?

You can send message to another thread , etc

Page 172: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support172

Interrupt handler This program code is invoked as soon as external signal comes in.

With the macros(__INTERRUPT_PROLOG() and __INTERRUPT_RETURN()),all register values should be pushed/popped.

Interrupt handler runs using stack area of a thread, which was running at the moment interruption comes in.

There is no thread priority for an interrupt handler.

Interrupt handler can invoke an interrupt thread(SignalRtInterruptThread)

You cannot call all the INtime System calls within an interrupt handler.

You cannot call even C library calls(memcpy, strcpy etc)

You’d better not to operate long.

Page 173: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support173

Interrupt handler#2 The followings are some system calls you can call within an interrupt handler:

SignalRtInterruptThread

SignalEndOfRtInterrupt

GetRtInterruptLevel

EnterRtInterrupt

DisableRtInterrupt

EnableRtInterrupt

By invoking an interrupt thread, you can call all INtime system calls in it.

Page 174: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support174

Example: Interrupt handler __INTERRUPT void IntHandler(void)

{

__INTERRUPT_PROLOG();

EnterRtInterrupt(GetRtInterruptLevel());

 

// operate inside an interrupt handler

// All you can do is to handle signals or simple memory access.

// you’re not even allowed t

data = inbyte( PORTADDR )

 

if (/*If I should invoke interrupt thread?*/)

SignalEndOfRtInterrupt(wLevel); // I’m not invoking a thread.

else

SignalRtInterruptThread(wLevel); // I’m invoking a thread

 

__INTERRUPT_RETURN();

}

Page 175: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support175

Interrupt thread You can call any INtime System call in this thread as same as normal threads.

This thread is always invoked by an interrupt handler.

Thread priority when you create a thread would be ignored since the priority usually defined according to interrupt level. INtime kernel changes the thread’s priority automatically.

While a certain IRQ level of thread is running, lower interrupt would be prohibited.

Page 176: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support176

Registering Interrupt handler Interrupt thread is created by calling CreateRtThread as you create a normal thread.

When calling SetRtInterruptHandler(), the thread behaves as an interrupt thread.

When an interrupt thread calls WaitForRtInterrupt(), the registered interrupt level goes to be enabled.

MaxInt parameter for SetRtInterruptHandler decides its behaviour when interrupts occurs:

MaxInt = 0   ----- Interrupt thread is invalid. (Invalid SignalRtInterruptThread())

MaxInt = 1   ----- When a thread returns from WaitForRtInterrupt(), the interrupt level becomes to be prohibited. When a thread calls WaitForRtInterrupt agai

n, the interrupt level becomes enabled again.

MaxInt 2 -----≧   The interrupt level is kept being enabled until MaxInt counts of interruption occurs. Those occurred interruption is queued. Therefore, when it calls

WaitForRtInterrupt, it may return from it soon, if this number is more than 1.

Page 177: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support177

Example: Interrupt thread void int_thread( void )

{

// Initialization of this thread …

// Any preparation for this thread

// Registers an interrupt handler to IDT so that this can be interrupt // thread

SetRtInterruptHandler(wLevel, 1, IntHandler);

while(1){

// Waits to be signaled

WaitForRtInterrupt(wLevel, WAIT_FOREVER);

// TO DO

}

}

Page 178: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support178

PIC and IRQ IRQ: Interrupt Request Line. PC platform can handle multiple hardware ev

ents using IRQs.

PIC: Programmable Interrupt Controller is used to handle IRQ signals from hardware.

PIC works to enable/disable IRQ and to control IRQ Level.

APIC(Advanced Programmable Interrupt Controller) makes more IRQ for PC platforms nowadays.

Page 179: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support179

Interrupt Level rrupt level is calculated with the following formula:

IRQLevel = (IRQ < 8) ? (IRQ *16 + 8) : ((1 + IRQ /8) * 16 + (IRQ % 8));

Static IRQ Level is already defined like as following: IRQ0_LEVEL, IRQ1_LEVEL, RQ2_LEVEL, IRQ3_LEV, IRQ4_LEVEL,

IRQ5_LEVEL, IRQ6_LEVEL, IRQ7_LEVEL, IRQ8_LEVEL, IRQ9_LEVEL,

IRQ10_LEVEL, RQ11_LEVEL, IRQ12_LEVEL, IRQ13_LEVEL, IRQ14_LEVEL, IRQ15_LEVEL

Page 180: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support180

Shared IRQ Multiple PCI devices, which are connected to a same IRQ line, can share

the IRQ, because PCI interrupt is level trigger.

For shared IRQ, call SetRtInterruptHandlerEx instead of SetRtInterruptHandler().

Please notice that the same IRQ is sharable between devices, which are used as real time device. You can never share the same IRQ with Windows device.

Handler #1

Thread #1

Device #1

Handler #2

Thread #2

Device #2

Handler #3

Thread #3

Device #3

PIC

Page 181: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support181

RTDRM(Realtime Device Resource Management) If you need to control a PCI device with RT application program, you need to make the

device to pass to INtime as an RT device. You can never share a device.

Also you can never share the same IRQ level with the other Windows device. E.G: If a network card (Windows) is assigned as IRQ 20, you cannot use IRQ 20. If you need to use the IRQ line, you need to make the Windows device disabled so that INtime device can use the line.

Installing RTDRM driver(Dummy Windows driver) for a device makes Windows OS not to access to the device.

For this device configuration, you can use INtime device configuration tool(INtime Device Manager), which you can access from INtime control panel.

Page 182: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support182

STEP7 Handling a hardware interruption

Page 183: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support183

Handling a hardware interruption We’re going to program an RT application, which would handle a hardware

interruption here. The application program would do the following:

– Initializing a digital board to configure that it occurs interrupt when an input bit transition.

– An application will show the state on an RT console.

PORT yyyyH xxxx xxxx

PORT yyyyH xxxx xxxx

PORT yyyyH xxxx xxxx

PORT yyyyH xxxx xxxx

Page 184: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support184

Handling a hardware interruption #2Using DIO switching accessory board to trigger

CN1

Input 1Input 2

Input 4 Input 3

Output 1

Output 3Output 4

Output 2

Trigger for an interruptionGroup 0 is allocated to the input sig

nal for interruption for this digital board. This configuration would be different according to the board you have.

 

You’ll need to get a technical reference from a board vendor to know the detail device information.

Here, a program will take the input value from the port at an interrupt handler, and it shows the state at an interrupt thread.

Page 185: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support185

The Others

Page 186: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support186

RSL (Realtime Shared Library) RSL is more like a Windows DLL(Dynamic Link Library).

INtime software provides RSL development wizard to configure the workspace.

RSL would be better placed in <INtime Folder>\bin.

RSL allows you to make your program module size small, to make it easier to port functions and so on.

Page 187: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support187

iWIN32 Subset APIs iWin32 subset API is one of option for a user who are used to develop Wi

ndows application program because the all the function which is provided as iWin32 calls are identical to win32 API.

iWin32 subset APIs provides an alternative to the standard INtime APIs.

iWIN32 subset APIs includes a number of objects that are similar to INtime objects and some that are specific to iWin32; INtime also provides some objects for which iWin32 provides no interface and in that case API mixing may be used.

Page 188: 1 INtime technical support What’s INtime ? --- Introduction of realtime extension for Windows System --- Micronet.Co, INtime Technical support group Ash

INtime technical support188

Thank you

Micronet.Co,http://www.mnc.co.jp/INtime

TenAsys.com

http://www.tenasys.com