Programming notes - @CHIP-RTOS V1.04
IPC@Chip Documentation Index
Introduction
- Here are some useful notes for programming applications for the IPC@Chip.
These notes contains some general rules for programming DOS applications for the IPC@CHIP and should prevent
the programmers from fatal errors.
Common notes for building IPC@Chip user applications
- Compiler option settings:
We recommend the usage of large memory model for user applications
The compiler must produce 186 processor instruction code
The data alignment must be set to 8 Bit (Byte alignment), not to 16-Bit (Word alignment)!
-
No "execption handling" at Borland C 4.x or 5.x projects:
Users of Borland C-Compilers (which provides "execption handling libraries" e.g. it's the default setting at 4.5 or 5.02)
should choose for their program project "No exceptions" at the "Target Expert" window.
This saves Flash and RAM memory space.
-
Do not select "Test stack overflow" at Borland C 4.x or 5.x project compiler settings:
If "Test stack overflow" is selected a stackcheck is done at every function call. If stack overflow
is recognized the program halts.
This is not practicable at user applications, which are using several tasks inside of their application,
because every task has its own stack, and the stack function of Borlands runtime library disregard this.
- Turbo Pascal programmers should include the example file SC12.INC
(part of each
IPC@Chip Pascal example program).
At the start of their program, they should install the procedure Terminate_Program
as the default exit procedure of the program.
Usage of the standard unit crt
is not allowed.
- Before starting the first Turbo Pascal program at the IPC@Chip the
memopt 1
command must be executed
(e.g. in the autoexec.bat)
- If programs that depend on one another are started from a batch file (e.g. if a user
program needs a previous driver program) the Batchmode 1
in chip.ini should be set.
- The CLIB library API calls are using the standard int86 and int86x calls e.g. from the Borland C standard
libs. We used these functions for more readable code. If developers need higher performance in their applications,
it will be better to implement often used API calls with inline assembler code. With
inline assembler instructions it is possible to load the processor registers directly with the needed
parameters and directly invoke the appropriate software interrupt int xxh
.
- For some reasons it is not advisable to design an IPC@Chip application as a set of several DOS programs:
1. It is more meaningful to run several tasks (builded with the RTOS API) inside of one DOS program because this requires
much less RAM and Flash memory space, than running several DOS programs as tasks of the @chip-RTOS.
2. It could lead to high memory fragmentation and insufficient memory, if more than one or two DOS programs are running at the IPC@Chip,
which will be often terminated and restarted again.
- Most of the used pointer variables and function parameters at the CLIB library C files are not declared as FAR pointer, because all of our
testprograms are using the memory model Large (at model Large all pointers become automatically FAR pointers).
If you want to use e.g. memory model Small for your application you must rewrite the CLIB
source files by declaring all pointer variables and function parameters explicit as FAR pointers.
-
It is not advisable to use the Borlandc-CLIB call malloc at memory model Large inside of your application.
In that case the DOS program tries to increase its own memory block with int21h 0x4A. If another program is loaded after that
program the malloc call failed, because there is no memory space left between the two programs.
It is possible to define a memory gap between two loaded programs at chip.ini
but then you must know how max. memory is required at your malloc calls. It is better to reserve inside of your application the
memory by declaring an unsigned char array (or the type you require) with the neede memory size.
-
The @Chip-RTOS has its own memory strategy.
The @Chip-RTOS memory always allocates memory in the following way:
DOS programs are always loaded at the first lowest free memory block. For memory blocks allocated inside of
the @Chip-RTOS or e.g. with DOS service 0x48
the @Chip-RTOS always start searching for
a free memory block from the highest possible RAM address. So the largest free memory block of the system is always located in the
middle of the @Chip-RTOS memory area. The shell command mem
shows the state of the internal memory map.
-
To open files on the flash disk drive A: for reading or writing, you can use the standard C functions fopen or open.
If you want to write blocks with a size greater 128 bytes, we strongly recommend to use the open/read/write calls instead of
fopen/fread/fwrite to reduce the flash write cycles, because these cycles are limited.
Description:
fopen internally works with a 128 byte stream buffer. The filesystem sectors have a size of 512 bytes.
If you want to write a block of 512 bytes, with fwrite every sector is read and written four times.
The handle based open/write/read functions call the INT21h functions directly with the given block size.
So only the needed flash write cycles are generated and the call is faster.
With block sizes smaller or equal 128 bytes fopen is better, because it buffers the data
and only writes data to disk, when 128 bytes are complete.
Top of list Index page
Using RTOS API
- Timer procedures must be implemented as short as possible, because they are executed in the Kernel task.
Large timer procedures are blocking the kernel and other tasks of the @Chip-RTOS.
Do not use large Clib functions like printf inside of a timer procedure.
This could cause a fatal stack overflow in the kernel task (stack size 1024 Bytes)
- Declaring of timer procedures with Borland C:
void huge my_timer(void)
- Microsoft C:
void far _saveregs _loadds my_timer(void)
- Turbo Pascal:
procedure Timer1_Proc;interrupt;
begin
[... your code ...]
(************************************************)
(* This is needed at the end of the Timer Proc. *)
asm
POP BP
POP ES
POP DS
POP DI
POP SI
POP DX
POP CX
POP BX
POP AX
RETF
end;
(************************************************)
end;
- DOS applications are running as a task of the @CHIP-RTOS with a default priority of 25.
If the users application doesn't go to sleep
, lower
priority tasks like the FTP server or the webserver will not work.
In major loops within user applications, the programmer should insert
sleep calls
if the FTP server
or webserver should work during the runtime of the user application.
- The stack of a task created
inside of a DOS application
should have a minimum stack size of 1024 Bytes.
Programmers of task functions who are using Microsoft C-Compilers with C-Library functions,
e.g. sprintf
, which requires a lot of stack space should increase this
allocation to 6144 (6 Kbytes). More stack space for the task is also required
if your task function uses a large amount of stack for automatic data (local variables) declared
inside the task function call.
- Declaring of task functions with Borland C:
void huge my_task(void)
- Declaring of task functions with Microsoft C:
void far _saveregs _loadds my_task(void)
- Before exiting an application, every task or timer procedure created inside of this application program
must be removed.
- A sleep call
with parameter 1 millisecond takes
equal or less than one millisecond.
If a user needs a minimal sleep time of 1 millisecond then RTX_SLEEP_TIME must be called with value
2 milliseconds.
- It is possible to create tasks
with a timeslicing value
at the taskdefblock structure.
.
Please note that the kernel executes timeslicing only between tasks which have same priority.
Other priorized tasks are not affected.
RTOS API
Top of list Index page
Programming CGI functions
- Avoid large loops inside of CGI functions. CGI functions must be executed
as fast as possible. Large execution times in CGI functions block
the webserver task preventing response to other http requests.
- Declaring of CGI functions with Borland C:
void huge my_cgi_func(rpCgiPtr CgiRequest)
- Declaring of CGI functions with Microsoft C:
void far _saveregs _loadds my_cgi_func(rpCgiPtr CgiRequest)
- Declaring of CGI functions with Borland Turbo Pascal:
procedure my_cgi_func;interrupt;
- Turbo Pascal users must install their CGI functions with API call Install CGI Pascal function
- C-Programmers must use Install CGI function
- Avoid the declaration of large arrays as local variables inside
of the CGI function to prevent stack overflows
- Users of Microsoft C should set Webserver stacksize
in
chip.ini up to 6144 KBytes,
to prevent stack overflows, if Microsoft CLib functions like sprintf are used.
- Dynamic html or text pages, which are created inside of a user CGI function should be as
small as possible. The webserver inside must allocate
a tempbuffer for storing this page before sending it to the browser. If your application builds
large dynamic html page in RAM, your application should not use all available memory in the IPC@Chip
because the webserver will need some memory for allocating tempbuffers for this
page. How much memory should be left: This depends on the application and the sizes of the created dynamic html
or CGI pages.
- Before exiting an application, every CGI function installed by the application must be removed
with Remove CGI page
CGI API
Top of list Index page
Configure the FTP server
- The default FTP idletimeout is set to 300 seconds. This is a very long time for waiting,
if FTP commands fail.
The idletimeout
can be reduced in chip.ini.
Top of list Index page
Usage of the TCPIP API
- Processing of a socket callback functions (see Register callback
) should be kept at a minimum
to prevent stack overflows.
- Declaring of socket callback functions with Borland C:
void huge my_callback(int socketdescriptor, int eventflagmask)
- Declaring of socket callback functions with Microsoft C:
void far _saveregs _loadds my_callback(int sd, int eventmask)
- The internal TCPIP stack of the IPC@Chip allocates memory for buffers smaller than 4096 bytes
from a preallocated memory block.
Larger buffers are allocated direct from the Chip-RTOS. If user TCPIP network communication sends/receives
packets with a size larger than 4096
bytes, the user application should not use all available memory in the IPC@Chip, because of these
additional allocations. There should be
in that case always a minimum of 30-40 KBytes of free available memory at the IPC@Chip. The mem command
shows the whole memory list of the IPC@Chip at runtime. The maximum amount of TCPIP memory could be configured
in chip.ini (see Set TCPIP memory size
).
The application programmer could reduce or increase this size in chip.ini.
With the API call Get TCPIP memory info
it
is possible to control the TCPIP memory usage at the application runtime.
TCPIP API
Top of list Index page
General notes for the usage of the DOS and @CHIP-RTOS int API calls
- At the start of a user program the Stdio focus should be set to USER.
Before ending the application switch the focus back to SHELL or BOTH (see Set Stdio focus
).
- If more than one user program runs in the IPC@Chip, only one of them should read characters from Stdin
- The functionality of most of the shell commands is also available through calls into the
@CHIP-RTOS Int API
. If not, use
the @CHIP-RTOS int call Execute a shell command
.
This call executes a shell command from inside the user application.
- Install a fatal user error handler, which does a reboot of the IPC@Chip with Install user fatal error handler
- Used software interrupts (all others are free for use):
0x00
- Reserved (@CHIP-RTOS Divide Overflow Handler)
0x01
- Reserved (Debugger Trace Interrupt)
0x02
- Reserved (Hardware Non-Maskable Interrupt (NMI))
0x03
- Reserved (Debugger Breakpoint Interrupt)
0x04
- Reserved (@CHIP-RTOS INTO Overflow Handler)
0x05
- Reserved (@CHIP-RTOS Array Bounds Exception Handler)
0x06
- Reserved (@CHIP-RTOS Invalid Opcode Exception Handler)
0x07
- Reserved (@CHIP-RTOS ESC Opcode Exception Handler)
0x08
- Reserved (Hardware, Timer #0 Handler)
0x0A
- Reserved (Hardware, DMA #0 / INT5 Handler)
0x0B
- Reserved (Hardware, DMA #1 / INT6 Handler)
0x0C
- Reserved (Hardware, INT0 Handler)
0x0D
- Reserved (Hardware, INT1 Ethernet Handler)
0x0E
- Reserved (Hardware, INT2 Handler)
0x0F
- Reserved (Hardware, INT3 Handler)
0x10
- Biosint
0x11
- Biosint
0x12
- Reserved (Hardware, Timer #1 Handler)
0x13
- Reserved (Hardware, Timer #2 Handler)
0x14
- Fossil Interface
0x16
- Biosint
0x1A
- Biosint
0x1C
- Timer Interrupt, see Set timer 1C interval
0x20
- Terminate Program (Only for compatibility, instead use
DOS service 0x4C
)
0x21
- DOSEmu
Interrupt Interface
0xA0
- Several 'chip' related services
0xA1
- Hardware API
(HAL)
0xA2
- Hardware API
(PFE)
0xAA
- I2C Interface
0xAB
- CGI Interface
0xAC
- TCP/IP API
0xAD
- RTOS API
0xAE
- Ethernet Packet Driver
0xAF
- Timer Interrupt, see Set timer AF interval
0xB0
- External Disk API
0xB1
- External Disk Driver
0xBF
- This vector is reserved to start a DOS executable
If you are using a BIOS variant in which some modules are not included, then the interrupts
corresponding to these modules are free for use.
- External hardware interrupts are enabled (STI opcode) during execution of the
following API software interrupts:
DOS ints 0x10, 0x14(Fossil), 0x16, 0x20, 0x21, @CHIP-RTOS int 0xA0, CGI int 0xAB,
I2C int 0xAA, CGI int 0xAB, TCPIP int 0xAC, RTOS int 0xAD
Pkt int 0xAE, Extdisk int 0xB0, Extdisk user int 0xB1
BIOS ints
Int21h
Top of list Index page
Using Hardware API
- The HAL functions keep interrupts disabled, so you can call them inside an
interrupt routine. The PFE functions are only for choosing and initializing a
specific function on the selected pin. They should be called once in your
application for initializing your hardware environment and not at runtime or
inside interrupt routines.
-
Do not use functions of the RTOS API inside of a user isr installed
with Install ISR
- The latency time of the user ISR (from generation of an interrupt until
first line of code inside the user ISR) is about 65 µs.
- Instead of HAL functions Read data bus
and Write data bus
you can call C-functions
inportb
and outportb
from DOS.H for faster data bus accesses.
- Usage of DMA0/INT5 and DMA1/INT6:
DMA0 and INT5 are served inside of the @CHIP-RTOS by one interrupt handler, also DMA1 and INT6.
If an user application has activated e.g. serial EXT DMA mode.
and wants also use INT5 as external interrupt, the DMA interrupt event is higher priorized.
This means: If a DMA0/1 interrupt occurs simultaneous with an external INT5/6 interrupts, the installed user handler
for the external interrupt is not called.
Hardware API
Top of list Index page
Using Fossil API
- The receive and send queue size
can be configured over the CHIP.INI.
- If you want external DMA, you have to disable the
serial DMA mode.
Otherwise the
DMA mode is recommended.
- Since @CHIP-RTOS 1.02B XON/XOFF mode is available, if the serial DMA mode is set.
Please note: Because of the internal functionality of DMA it is not
possible to detect an XON or XOFF character from the connected peer immediately. It is possible that
an overrun situation at the peer (e.g. GSM modem) could occur.
Nevertheless we enable this mode because some GSM modems (any??) supports only XON/XOFF as
serial flow control mechanism.
- The default serial recv queue size is 1024 Byte. If the default DMA receive mode is used, it is advised to increase
the recv queue size at Chip.ini
up to a minimum value of 2048 Bytes to prevent a possible buffer overrun (even if hard handshake is used).
This could only happen with the default queue size of 1024 Bytes, if the user doesn't call the
Fossil API readblock function
fast enough.
If the application programmer will not increase this buffer size up to the recommended value, he should call the
Fossil API readblock function
with the highest possible size
at the CX-Register for flushing the internal buffers and preventing an receive buffer overrun
- For a given serial port the fossil functions are not reentrant. Do not call fossil functions for
the same serial port from different tasks. However for different serial ports, the fossil functions
are reentrant. E.g. task A can operate the COM port using fossil functions concurrently with task
B operating the EXT port using the same fossil functions.
Fossil API
Top of list Index page
Working with Float Data Types
- The IPC@CHIP does not provide a floating point co-processor. So if you want to use floating point data
types you need to enable the math-emulation in your compiler. In Borland C++ 5.02 see the option
"Emulation" under the Target Expert's
(right mouse click on your Exe-file in your project) "Math Support".
- If you want to use float data types in tasks other than your main (DOS) task, you have to reinit the
floating point emulation. Using the Borland C++ 5.02 compiler you do that with the command "_fpreset(void)"
(available in float.h). Do that in your task procedure before using floating point data types.
Top of list Index page
Configure PPP client or server
-
Connected modems should be configured in chip.ini with the modem command ATE0 INITCMD
.
This prevents the modem from echoing characters to the peer in command mode.
The COM and the serial ports of the IPC@Chip SC12 have only 4 lines (TxD/RxD/CTS/RTS) and no line for
detecting a hang-up of the modem. Because of this fact we provided the
Idletimeout
and
the MODEMCTRL
configuration features in chip.ini
or in the PPP client init struct type
.
But the idletimeout and modemctrl detection could fail if a modem has switched its echo mode on.
If the peer modem hangs up without correctly closing a PPP session, the IPC@chip modem also hangs
up and goes into the command mode.
Because of the missing line for detecting a modem hang-up, the PPP server doesn't know anything about
the broken connection and still sends PPP frames to the modem. It could happen that the modem
echoes these characters back to the IPC@Chip due to being in "Echo on" mode. This will cause the
idletimeout to not work.
- We recommend that the PPP server or client and the connected modems should run (if possible)
with RTS/CTS flow control
(see chip.ini flow control mode
or in the
PPP client init struct type
).
Most modems use RTS/CTS flow control, if they get the AT command AT\Q3.
-
The COM and EXT port of the SC12 has only CTS, RTS, RxD and TxD lines,
so you have to configure your modem with DTR always on.
(e.g. AT cmd for a most modem types: AT&D0)
- If the option PPP_IPCP_PROTOCOL (VJ TCP/IP header compression) is set (see PPPCLIENT_SET_OPTIONS
,
it could be possible that the FTP server of the IPC@Chip is not usable via the PPP interface. This was noticed at PPP sessions connected
to a Linux PPP peer.
PPP
Top of list Index page
End of document
|