Freemaster Usage: Serial Driver Implementation
Freemaster Usage: Serial Driver Implementation
Rev. 0, 5/2013
Application Note
FreeMASTER Usage
Serial driver implementation
Contents
Radomir Kozub 1 What are the FreeMASTER serial driver and FreeMASTER
Roznov, Czech Republic PC application? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 I want it — where do I get the FreeMASTER
serial driver? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The FreeMASTER serial driver is a piece of code that 1.2 What can I find on the hard drive?. . . . . . . . . . . . . . 4
1.3 What features does the serial driver offer? . . . . . . . 5
enables an embedded application to communicate with 2 How do I create my first application with FreeMASTER
the FreeMASTER PC application. This application note in CodeWarrior 10.3? . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
shows how to add the FreeMASTER serial driver to your 2.1 How to create an empty bareboard project
stationery using CodeWarrior 10.3 . . . . . . . . . . . . . 5
embedded code. 2.2 How to add the FreeMASTER communication driver
files to the project . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 How to configure the FreeMASTER serial driver. . 10
1 What are the FreeMASTER 2.4 FreeMASTER API short description . . . . . . . . . . . 11
2.5 What FreeMASTER API functions do I have to
serial driver and 3
handle in my application, and where? . . . . . . . . . . 12
FreeMASTER PC application configuration . . . . . . . . . 14
FreeMASTER PC 3.1 How to set up the communication channel . . . . . . 14
3.2 MAP file selection . . . . . . . . . . . . . . . . . . . . . . . . . 15
application? 3.3 How to choose the observed variable . . . . . . . . . . 16
3.4 How to set up the Scope . . . . . . . . . . . . . . . . . . . . 18
FreeMASTER is a PC-based development tool serving 3.5 How to set up the Recorder. . . . . . . . . . . . . . . . . . 20
3.6 How to configure the Recorder in the FreeMASTER
as a real-time monitor, visualization tool, and graphical PC application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
control panel of embedded applications based on 4 Is there any easier way to integrate FreeMASTER into
Freescale Semiconductor processing units. my project? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.1 How to set up a new project with FreeMASTER
drivers integrated by the Processor Expert . . . . . . 21
The FreeMASTER PC application repetitively sends a 4.2 How to add FreeMASTER serial drivers to the
request for the immediate values of chosen variables project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
used in the embedded application (the PC application 4.3 How to set up the FreeMASTER functionality . . . . 23
4.4 How to set up the UART parameters. . . . . . . . . . . 24
acts as the master in this peer-to-peer communication). 4.5 How to modify code in the application . . . . . . . . . . 25
5 Final words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
The embedded application replies with the actual value of the variable (the embedded application acts as
a slave in the communication). The FreeMASTER PC Application visualizes the variable value. The piece
of embedded code that takes care of responding to a request is called the FreeMASTER serial driver. This
driver carries out protocol parsing, prepares responses, and handles the communication periphery. The
serial driver covers the UART SCI and CAN communication for all supported devices, and the
EOnCE/JTAG communication for the 56F8xxx family of hybrid microcontrollers.
This document describes how to add the FreeMASTER serial driver software to your embedded
application and configure it.
You may either choose only those drivers for the platform you are planning to use (e.g. Kxx Kinetis ARM
Processors) or choose a complete installation. Then, go through the installation wizard and finish it.
Then choose the New Project Wizard, choose a project name, and pick the device to be used. This example
uses the MK60DN512 processor on the TWR-K60D100M tower board:
Then check the chosen connection board. This example doesn’t use the Processor Expert Rapid
Development tool, so select None in the Rapid Application Development window. The final step is to click
on Finish and the empty project is created.
Right mouse-click to the Freemaster folder, choose Add files, and add all the files from the src_common
directory along with the platform dependent files from the src_platform directory. In the Kinetis ARM
processor case, we add files from the Kxx subdirectory.
Choose the Copy Files into the project option. The previous operation will also create a Freemaster folder
in your workspace folder and copy the FreeMASTER serial driver files into the folder.
Exactly one of the three macros must be defined non-zero. The others must be defined zero or left
undefined. The non-zero-defined constant selects the interrupt mode of the driver.
FMSTR_LONG_INTR = 1
Serial communication and the FreeMASTER protocol decoding and execution is done in the FMSTR_Isr()
interrupt service routine. As the protocol execution may be a lengthy task, it is recommended to use this
mode only if the interrupt prioritization scheme is possible in the application, and if the FreeMASTER
interrupt is assigned to a lower (the lowest) priority.
FMSTR_SHORT_INTR = 1
The raw serial communication is handled by the FMSTR_Isr() interrupt service routine, while the protocol
decoding and execution is handled in the FMSTR_Poll() routine. You typically call the FMSTR_Poll()
during the idle time in the application ‘main loop’.
FMSTR_POLL_DRIVEN = 1
Both the serial (SCI/CAN) communication and the FreeMASTER protocol execution are done in the
FMSTR_Poll() routine. No interrupts are needed: the FMSTR_Isr() code compiles to an empty function.
When using this mode, you must ensure the FMSTR_Poll() function is called by an application at least
once per ‘SCI character time’, which is the time needed to transmit or receive a single character.
In our example, we will use the short interrupt mode as this is the most versatile.
/*****************************************************************************
* Select communication interface (SCI, CAN, USB CDC or Packet Driven BDM)
******************************************************************************/
#define FMSTR_SCI_BASE 0x4006D000 /* UART3 registers base address on K60 */
#define FMSTR_SCI_INTERRUPT 67 /* UART3 interrupt vector on K60 */
Here we have to show to the FreeMASTER serial driver which SCI peripheral will be used for
communication and its interrupt vector. The K60 Tower Board has UART3 mapped to the Tower Serial
board (TWR-SER board). You have to modify the values if another platform / SCI channel is used.
#define FMSTR_DISABLE 0 /* Disable all the FreeMASTER functionalities */
We typically use FreeMASTER as a debugging tool, while we don’t want to have it in the release code.
Setting the FMSTR_DISABLE to a non-zero value will remove FreeMASTER from your code.
#define FMSTR_USE_SCI 1 /* To select SCI communication interface */
#define FMSTR_USE_FLEXCAN 0 /* To select FlexCAN communication interface */
#define FMSTR_USE_USB_CDC 0 /* To select USB CDC communication interface */
#define FMSTR_USE_PDBDM 0 /* To select Packet Driven BDM comm. interface (optional) */
We will use UART communication in this example, so set FMSTR_USE_SCI to one while clearing the
other to zero. We also have to enable the most important serial driver features
#define FMSTR_USE_READMEM 1 /* enable/disable memory read / write*/
#define FMSTR_USE_SCOPE 1 /* enable/disable scope support */
#define FMSTR_USE_RECORDER 1 /* enable/disable recorder support */
#define FMSTR_USE_APPCMD 1 /* enable/disable App.Commands support */
There are other options in the file not mentioned here for simplicity. Refer to the FMSTRSCIDRVUG.pgf
document for more details.
FMSTR_BOOL FMSTR_Init(void)
This function initializes internal variables of the FreeMASTER driver and enables the communication
interface (SCI, JTAG or CAN). This function does not change the configuration of the selected
communication module; the module must be initialized before the FMSTR_Init() function is called.
The FMSTR_Init() function must be called before any other FreeMASTER driver API function.
void FMSTR_Poll(void)
In the poll-driven or short interrupt modes, this function handles the protocol decoding and execution. In
the poll-driven mode, this function also handles the interface communication with the PC. Typically, you
call the FMSTR_Poll() during the 'idle' time in the main application loop.
void FMSTR_Isr(void)
This is the interface to the interrupt service routine of the FreeMASTER serial driver. In the long or short
interrupt modes, this function must be set as the interrupt vector calling address. On platforms where
interface processing is split into multiple interrupts, this function should be set as a vector for each such
interrupt.
For the recorder functionality, we need to use the following functions
/* Recorder API */
void FMSTR_Recorder(void)
void FMSTR_TriggerRec(void);
FMSTR_Recorder()
This function takes one sample of the variables being recorded using the FreeMASTER recorder. If the
recorder is not active at the moment when FMSTR_Recorder is called, the function returns immediately.
When the recorder is initialized and active, the values of the variables being recorded are copied to the
recorder buffer and the trigger condition is evaluated.
FMSTR_TriggerRec()
This function forces the recorder trigger condition to happen, which causes the recorder to be
automatically de-activated after post-trigger samples are sampled. This function can be used in the
application when it needs to have the trigger occurrence under its control. This function is optional; the
recorder can also be triggered by the PC tool or when the selected variable exceeds a threshold value.
in each file where the FreeMASTER API function is called: in this case, the main.c and kinetis_sysinit.c
files.
1. Call FMSTR_Init(void) just once at the code start, typically after the start-up code, at the beginning
of the main function.
2. Call FMSTR_Poll(void) periodically in your code. A typical place is in the main loop:
int main(void)
{
ConfigureMCU();
/* FreeMASTER internal variables initialization */
FMSTR_Init();
/* main loop */
for(;;) {
/* call function periodically*/
FMSTR_Poll();
}
}
3. FMSTR_Isr() must be assigned to the UART3 interrupt vector. The kinetis_sysinit.c file contains
the vector table definition. Here, we can define the called FreeMASTER:
4. The last thing is to configure all the necessary peripheries used in the example. The clock gates are
enabled, and the interrupt controller and UART have to be configured. After this fourth step, the
embedded code is ready to be compiled and run. Just build it, load it, and run the code. Now we
have to run and configure the FreeMASTER PC Application. The counter variable is here in the
code and will be visualized in FreeMASTER.
/* UART3 baudrate */
#define brate 9600
/* Actual BUS CLOCK value */
#define bclk 21e6
/* Macro to calculate BAUDRATE register value */
#define CALC_SBR(brate,bclk) (unsigned short)((double)bclk/(16.0*(double)brate))
int counter = 0;
int main(void)
{
This was the last step in the embedded application. We are ready to read out variable values from our
embedded application.
The period value shows how often the variable’s value is refreshed in milliseconds. The 16-bit variable
counter is incremented in the main loop, so the variable overflows faster than the 400 ms refresh rate, and
the value shown in the grid seems to be chaotic. In fact, this is due to undersampling — Nyquist theorem
is not fulfilled. The same situation can be observed for the Scope.
If we use a 400 ms refresh time, we have only three samples before counter overflows. Even if we have
the fastest communication possible, we can see only a few values out of the 216 states the counter variable
can get into. If we want to see all the counter increments, we need to use the Recorder feature.
In each cycle after the counter variable is incremented, the FMSTR_Recorder() function is called and the
counter value is buffered to the Recorder buffer. It depends on the FreeMASTER PC application setting
when the buffer will be transferred to the PC. This may happen after the trigger conditions are fulfilled, or
by calling the FMSTR_TriggerRec() function.
Using the Recorder, we can see all values of the counter variable from 0 to a defined buffer length, as
opposed to the Scope view. This is the way to visualize fast actions. The buffer length is limited by the
amount of RAM available in the microprocessor used.
Then, the application is prepared and we can run it and again observe the counter variable in the
FreeMASTER PC application.
5 Final words
There are situations when developing embedded applications where we need to see the application internal
states in real time or where a graphical visualization helps with the debugging. FreeMASTER offers both
of these in a very appropriate way and is easy to use. Adding serial drivers to the application requires only
a few steps, and then we can enjoy the user friendly FreeMASTER application to observe those internal
states.