Software Platform Builder Tutorial - Simplifying the Application Code
Tutorial - Getting Started with the Software Platform Builder
First Steps
Create the Software Platform
Write the Application Code
Build and Download
Simplify the Application Code
In our example keyboard echo design, the C application has source lines to open and close the input and output devices. This is common for many situations. However, since we use only one input device and one output device, we can simplify the application even more by defining the keyboard as stdin
and the Terminal instrument as stdout
.
For this we just need to configure the standard devices in the Software Platform document and then modify the application.
Configure the Generic Device I/O Service
- In the Projects panel, double-click on the PS2_Echo_SP.SwPlatform document to open the Software Platform document.
- Click on the Generic Device I/O in the Software Services list. The configuration options for the Generic Device I/O service are displayed in the Configuration region of the document. Make the following changes to the service's configuration:
- Enable the option Standard Input.
- Click on stdin device name, then on the down-arrow next to it and select
KEYBOARD_1
- Leave the stdin buffering type set to
None
(remember that we did not want input and output buffering for our application).
- Click on stdin device name, then on the down-arrow next to it and select
- Enable the option Standard Output
- Click on stdout device name, then on the down-arrow next to it and select
SERIAL_1
- Set the stdout buffering type to
None
.
- Click on stdout device name, then on the down-arrow next to it and select
- Enable the option Standard Input.
The Configuration for the Generic Device I/O service should now appear as shown in Figure 1.
Rewrite and Rebuild the Application
Because we changed the configuration of the Generic Device I/O service, the library implementation will differ from the first build. This means that in our application we now can access the keyboard and Terminal Instrument as stdin and stdout. Instead of opening and closing our devices, we can simply use the printf()
, getc()
and putc()
functions.
Check out the code below and compare the differences with the previous version:
#include <stdio.h>
int main(void)
{
int ch;
printf("SIMPLIFIED APPLICATION\n\n");
printf("Type your message and press ESC to exit...\n\n");
while ((ch = getc(stdin)) != 27) /* 27 = ESC */
{
if (ch != EOF)
{
putc(ch, stdout);
}
}
printf("\n\nThat's it!\n");
return 0;
}
The following differences can be observed:
- The calls to
fopen()
andfclose()
are no longer necessary,stdin
andstdout
are used instead.
- The calls to
setbuf()
are no longer necessary because we configured the buffering type in the Generic Device I/O service.
- The
fprintf()
,fgetc()
andfputc()
functions are replaced respectively byprintf()
,getc()
andputc()
.
- Double click on the C source document in the embedded project to open it, and enter the code above. Save your project.
- In the Devices view, click on the Compile button just below the TSK3000A symbol. Both the library and the application are rebuilt.
- Click on the Download button to load and start the application. The Terminal instrument's display should appear as shown in Figure 2.
Characters you type on the PS/2 keyboard connected to the NanoBoard are echoed on the Terminal instrument's display.
Now compare the final application to the software requirements we defined previously (see Write the Application Code). It's that simple with the Software Platform Builder to write software that accesses input and output devices!