mcu-an-390096-e-v12

Fujitsu Microelectronics Europe
Application Note
MCU-AN-390096-E-V12
F²MC-16LX FAMILY
16-BIT MICROCONTROLLER
ALL 16LX SERIES
FULL DUPLEX SOFTWARE UART
APPLICATION NOTE
DUPLEX SOFTWARE UART
Revision History
Revision History
Date
2003-07-24
2003-08-18
2006-07-19
Issue
V1.0; First version; MWi
V1.1; Resource picture added
V1.2; New Software Package; Source code updated; MWi
This document contains 17 pages.
MCU-AN-390096-E-V12
-2-
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Warranty and Disclaimer
Warranty and Disclaimer
To the maximum extent permitted by applicable law, Fujitsu Microelectronics Europe GmbH restricts
its warranties and its liability for all products delivered free of charge (eg. software include or
header files, application examples, target boards, evaluation boards, engineering samples of IC’s
etc.), its performance and any consequential damages, on the use of the Product in accordance with
(i) the terms of the License Agreement and the Sale and Purchase Agreement under which
agreements the Product has been delivered, (ii) the technical descriptions and (iii) all accompanying
written materials. In addition, to the maximum extent permitted by applicable law, Fujitsu
Microelectronics Europe GmbH disclaims all warranties and liabilities for the performance of the
Product and any consequential damages in cases of unauthorised decompiling and/or reverse
engineering and/or disassembling. Note, all these products are intended and must only be used
in an evaluation laboratory environment.
1.
Fujitsu Microelectronics Europe GmbH warrants that the Product will perform substantially in
accordance with the accompanying written materials for a period of 90 days form the date of
receipt by the customer. Concerning the hardware components of the Product, Fujitsu
Microelectronics Europe GmbH warrants that the Product will be free from defects in material
and workmanship under use and service as specified in the accompanying written materials
for a duration of 1 year from the date of receipt by the customer.
2.
Should a Product turn out to be defect, Fujitsu Microelectronics Europe GmbH´s entire liability
and the customer´s exclusive remedy shall be, at Fujitsu Microelectronics Europe GmbH´s
sole discretion, either return of the purchase price and the license fee, or replacement of the
Product or parts thereof, if the Product is returned to Fujitsu Microelectronics Europe GmbH in
original packing and without further defects resulting from the customer´s use or the transport.
However, this warranty is excluded if the defect has resulted from an accident not attributable
to Fujitsu Microelectronics Europe GmbH, or abuse or misapplication attributable to the
customer or any other third party not relating to Fujitsu Microelectronics Europe GmbH.
3.
To the maximum extent permitted by applicable law Fujitsu Microelectronics Europe GmbH
disclaims all other warranties, whether expressed or implied, in particular, but not limited to,
warranties of merchantability and fitness for a particular purpose for which the Product is not
designated.
4.
To the maximum extent permitted by applicable law, Fujitsu Microelectronics Europe GmbH´s
and its suppliers´ liability is restricted to intention and gross negligence.
NO LIABILITY FOR CONSEQUENTIAL DAMAGES
To the maximum extent permitted by applicable law, in no event shall Fujitsu
Microelectronics Europe GmbH and its suppliers be liable for any damages whatsoever
(including but without limitation, consequential and/or indirect damages for personal
injury, assets of substantial value, loss of profits, interruption of business operation,
loss of information, or any other monetary or pecuniary loss) arising from the use of
the Product.
Should one of the above stipulations be or become invalid and/or unenforceable, the remaining
stipulations shall stay in full effect
© Fujitsu Microelectronics Europe GmbH
-3-
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Contents
Contents
REVISION HISTORY ............................................................................................................ 2
WARRANTY AND DISCLAIMER ......................................................................................... 3
CONTENTS .......................................................................................................................... 4
0 INTRODUCTION.............................................................................................................. 5
1 FULL DUPLEX ASYNCHRONOUS SOFTWARE UART ................................................. 6
1.1
Overview ................................................................................................................. 6
1.2
Frame Protocol........................................................................................................ 6
1.3
Used Algorithm........................................................................................................ 6
1.4
Uses Resources...................................................................................................... 7
2 EXAMPLE PROGRAM .................................................................................................... 8
2.1
Global variables....................................................................................................... 8
2.2
Initialization ............................................................................................................. 8
2.3
Clear Errors............................................................................................................. 9
2.4
Transmit Data........................................................................................................ 10
2.5
Transmission Interrupt Handler ............................................................................. 10
2.6
Reception Interrupt Handler (1) ............................................................................. 12
2.7
Reception Interrupt Handler (2) ............................................................................. 12
2.8
Main Program example ......................................................................................... 14
3 BAUD RATES................................................................................................................ 16
3.1
Consideration ........................................................................................................ 16
3.2
Baud Rate Table ................................................................................................... 16
4 APPENDIX A ................................................................................................................. 17
4.1
Source Code ......................................................................................................... 17
MCU-AN-390096-E-V12
-4-
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Introduction
0 Introduction
This application note describes how to implement a full duplex asynchronous software UART
with a 16LX-MCU.
© Fujitsu Microelectronics Europe GmbH
-5-
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Chapter 1 Full duplex asynchronous Software UART
1 Full duplex asynchronous Software UART
SHORT DESCRIPTION
1.1 Overview
Sometimes a asynchronous serial frame format is needed, which has more than 8 data bits.
Not all Fujitsu MCU-UARTs allow this format.
A workaround is to implement a software UART which is configurable by the user. This
document gives an example for this application. For implementing an asynchronous
software UART two reload timers and an external interrupt is used.
1.2 Frame Protocol
The asynchronous frame format has no own clock signal. Both receiver and transmitter must
operate at the same bit frequency. This frequency is called baud rate. The reciprocal value
of this frequency is the bit time.
In the usual non-return-to-zero format the frame begins with a zero-bit, which is held by the
transmitter for 1 bit time. Then the data bits are following, usually there exist the following
number of bits: 6, 7, 8, and 9. These bits are also 1 bit time long. Optionally a so-called
parity bit can follow. The frame ends with one or two stop bits, which are a “1”-bits.
The following graphic illustrates an asynchronous serial frame:
ST
D0
D1
Dn
Dn+1/P
SP
Bit time
Frame
ST = Start Bit, D = Data Bit, P = Parity Bit, SP = Stop Bit
The receiver has to synchronize to the falling edge of the start bit. Because the receiver has
the same serial frequency, the receiver knows when a next bit is to be expected. The value
of a bit has to sampled in the middle of the bit time. This method allows a frequency
deviation of about 5% (using 8 data bits, no parity, 1 stop bit = 8N1).
In a full duplex operation transmission and reception at the same time is possible.
1.3 Used Algorithm
The algorithm in this application note uses two reload timers for the baud rate, one for
transmission and one for reception. This is necessary to ensure a full duplex operation, i. e.
transmission and reception timing must be independent.
For transmission the corresponding reload timer’s interrupt has to be enabled and the
transmission starts with the next “counter underflow”.
The reception uses an external interrupt, which is configured for falling edge detection.
Therefore the “Serial Input Pin” has to be an “External interrupt pin”. After detecting this edge
(start bit) the reception reload timer gets the half reload value, because the sampling has to
MCU-AN-390096-E-V12
-6-
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Chapter 1 Full duplex asynchronous Software UART
be done at the middle of the bit time. After the start bit is sampled the reload timer gets the
“normal” value, because it is now synchronized to the incoming serial stream.
1.4 Uses Resources
The following picture shows an overview of the used resources (MB90540 series):
Transmission
Reload Timer 0
Port P87
“SOT”
Reception
Reload Timer 1
Port P90 / INT0
“SIN”
External Interrupt 0
It is also possible to use a different pin for SIN, by connecting this pin to an external interrupt
input pin.
© Fujitsu Microelectronics Europe GmbH
-7-
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Chapter 2 Example Program
2 Example Program
THIS CHAPETER DESCRIBES THE C CODE OF THE SOFTWARE UART
2.1 Global variables
The following variables are defined globally so that they are accessible for the interrupt
handler routines. They are similar to the standard register flags and control bits of Fujitsu
UARTs. Note, that the code is optimized for devices of the MB90540 series. For other
devices, please refer to the hardware manual for pin settings.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
#define
#define
#define
#define
SIN
SOT
D_SIN
D_SOT
PDR8_P90
PDR8_P87
DDR8_D90
DDR8_D87
*/
*/
*/
*/
// Port Data
// Port Direction
// GLOBAL VARIABLES
// internal
unsigned char TXState;
unsigned char RXState;
// State of Transmission
// State of Reception
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
TDcounter;
RDcounter;
Tparcnt;
Rparcnt;
TScounter;
RScounter;
//
//
//
//
//
//
Transmission data counter
Reception data counter
Transmission parity counter
Reception parity counter
Transmission stop bit counter
Reception stop bit counter
// control
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
unsigned char
CL;
SBL;
PEN;
PAR;
TXE;
RXE;
//
//
//
//
//
//
Character length
Stop bit length
Parity enable (0, 1)
Parity (0 = even, 1 = odd)
Transmission enable
Reception enable
// flags
unsigned
unsigned
unsigned
unsigned
unsigned
char
char
char
char
char
TDRE;
RDRF;
PE;
FRE;
ORE;
//
//
//
//
//
Transmission Data Register empty
Reception Data Register full
Parity error
Framing error
Overrun error
// data
unsigned
unsigned
unsigned
unsigned
int
int
int
int
TXReg;
RXReg;
TXShift;
RXShift;
//
//
//
//
Transmission Register
Reception Register
Transmission "Shift" Register
Reception "Shift" Register
char
char
char
char
char
char
unsigned int Baudrate;
// Baudrate
2.2 Initialization
The following function initializes the software UART to the 8N1 format:
MCU-AN-390096-E-V12
-8-
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Chapter 2 Example Program
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
void init_UART(void)
{
TXState = 0;
RXState = 0;
CL = 8;
SBL = 1;
PEN = 0;
PAR = 0;
TXE = 0;
RXE = 0;
TDRE = 1;
RDRF = 0;
PE = 0;
FRE = 0;
ORE = 0;
TXReg = 0;
RXReg = 0;
TXShift = 0;
RXShift = 0;
*/
*/
*/
*/
// "Reset UART" to 8N1
// 8 Data bits
// 1 Stop Bit
// No Parity
// TXReg empty preset
// Set no errors
// Data registers empty
TMRLR0 = Baudrate;
TMCSR0 = 0x0013;
// Reload Value for transmission
TMRLR1 = (Baudrate / 2);
TMCSR1 = 0x0013;
// Reload Value for reception
//
(sample time to synchronize: TBit/2)
ENIR = 0x00;
ELVR_LB0 = 1;
ELVR_LA0 = 1;
// disable external interrupts
// falling edge detection for external
//
interrupt (start bit edge)
D_SOT = 1;
SOT = 1;
SIN = 0;
// PDR8_7 = 1 (mark level) *** MB90540 ***
// DDR8_7 = output (tx)
*** MB90540 ***
// DDR9_0 = input (rx)
*** MB90540 ***
}
The pin on port P90 is the external interrupt input pin. This is the “serial input” in this
example.
2.3 Clear Errors
The functionality of the “CRE” or “REC” control bit in Fujitsu UARTs is implemented in the
following function.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
*/
*/
*/
*/
void clrerr(void)
{
FRE = 0;
PE = 0;
ORE = 0;
}
© Fujitsu Microelectronics Europe GmbH
-9-
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Chapter 2 Example Program
2.4 Transmit Data
In Fujitsu standard UARTs the transmission starts if data is written to the transmission data
register. For the software UART the interrupt of the transmission reload counter has to be
enabled like in the following function.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
void transmit(unsigned int data)
{
TXReg = data;
if (TXE == 1)
{
TXState = 1;
TMCSR0 = 0x001A;
}
}
*/
*/
*/
*/
// leave wait state
// enable RT interrupt
2.5 Transmission Interrupt Handler
The transmission takes place with the interrupt handler of the reload timer. The variable
TXState shows the handler what to do next. At the end the interrupt is disabled to stop the
transmission.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
//
//
//
//
//
//
//
//
*/
*/
*/
*/
Transmission States:
0
1
2
3
4
waiting
start bit
data bits
parity bit
stop bit(s)
__interrupt void TX_RT_IRQ_Handler (void)
{
TMCSR0_UF = 0;
// clear IRQ cause
if (TXState == 1)
{
SOT = 0;
// Start bit
TDcounter = CL;
TScounter = SBL;
TXState++;
TXShift = TXReg;
Tparcnt = 0;
TDRE = 0;
// busy ...
}
else if (TXState == 2)
// Data bits
{
if ((TXShift & 0x01) == 0x01)
{
K
MCU-AN-390096-E-V12
- 10 -
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Chapter 2 Example Program
SOT = 1;
K
Tparcnt = Tparcnt ^ 0x01;
}
else
SOT = 0;
TXShift = (TXShift >> 1);
TDcounter--;
if ((TDcounter == 0) & (PEN == 1))
TXState++;
if ((TDcounter == 0) & (PEN == 0))
TXState = 4;
}
else if (TXState == 3)
{
SOT = (Tparcnt ^ PAR);
TXState++;
}
else if (TXState == 4)
{
SOT = 1;
// Parity
// Stop bit(s)
TScounter--;
if (TScounter == 0)
{
TXState = 0;
TDRE = 1;
TMCSR0 = 0x0013;
}
}
}
The state diagram of this interrupt handler is shown in the graphic below.
TScounter == 0
Interrupt disable
Interrupt enabled
Waiting
0
Stop
Bit(s)
4
Start
Bit
1
TScounter --
TDcounter == 0 &
PEN = 0
Parity
Bit
3
Data
Bits
2
TDcounter--
TDcounter == 0 &
PEN = 1
© Fujitsu Microelectronics Europe GmbH
- 11 -
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Chapter 2 Example Program
2.6 Reception Interrupt Handler (1)
The start bit detection is performed via the external interrupt. Thus the corresponding
interrupt is the first which takes place for the reception.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
*/
*/
*/
*/
__interrupt void RX_EI_IRQ_Handler (void)
{
if ((RXE == 1) & (EIRR_ER0) & (RXState == 0))
{
TMRLR1 = ((Baudrate / 2) - 180);
TMCSR1 = 0x001B;
// enable RT interrupt and re-trigger
RXState++;
// Start bit State
ENIR_EN0 = 0;
// disable ext int #0
}
EIRR_ER0 = 0;
// Clear Ext Int #0
}
Note that the “offset” 180 has to be subtracted from the half reload value. This is for the
delay time between the falling edge of the start bit and the interrupt handler execution.
2.7 Reception Interrupt Handler (2)
In the second interrupt handler the intrinsic reception happens. The handler routine is similar
to the transmission handler routine.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
/* ELIGIBILITY FOR ANY PURPOSES.
/*
(C) Fujitsu Microelectronics Europe GmbH
//
//
//
//
//
//
//
//
*/
*/
*/
*/
Reception States:
0
1
2
3
4
waiting (end of reception)
start bit
data bits
parity bit
stop bit(s)
__interrupt void RX_RT_IRQ_Handler(void)
{
TMCSR1_UF = 0;
// clear IRQ cause
if (RXState == 1)
{
TMRLR1 = Baudrate;
TMCSR1 = 0x001B;
if (SIN != 0)
FRE = 1;
// Start bit
// now set "correct" baudrate
// Framing error if start bit != 0
RDcounter = CL;
RScounter = SBL;
MCU-AN-390096-E-V12
K
- 12 -
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Chapter 2 Example Program
RXState++;
RXShift = 0;
Rparcnt = 0;
}
else if (RXState == 2)
{
RXShift = (RXShift >> 1);
K
// Data bits
if (SIN == 1)
{
RXShift = RXShift | 0x8000;
Rparcnt = Rparcnt ^ 0x01;
}
RDcounter--;
if ((RDcounter == 0) & (PEN == 1))
RXState++;
if ((RDcounter == 0) & (PEN == 0))
RXState = 4;
}
else if (RXState == 3)
// Parity
{
if (SIN != (Rparcnt ^ PAR))
PE = 1;
// Parity error
RXState++;
}
else if (RXState == 4)
{
if (SIN != 1)
FRE = 1;
// Stop bit(s)
// Framing error
RScounter--;
if (RScounter == 0)
{
RXState = 0;
// Move bits to correct position
RXReg = (RXShift >> (16 - CL));
TMCSR1 = 0x0013;
if (RDRF == 1)
ORE = 1;
RDRF = 1;
// Overrun error
if (RXE == 1)
{
EIRR_ER0 = 0;
ENIR_EN0 = 1; // enable ext int #0
}
}
}
}
© Fujitsu Microelectronics Europe GmbH
- 13 -
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Chapter 2 Example Program
The state diagram is shown in the illustration below:
RScounter == 0
Interrupt disable
Waiting
End Rx
0
Interrupt enabled
from External
Interrupt Handler
Stop
Bit(s)
4
Start
Bit
1
RScounter --
RDcounter == 0 &
PEN = 0
Parity
Bit
3
Data
Bits
2
RDcounter--
RDcounter == 0 &
PEN = 1
2.8 Main Program example
A common example is a test program which echoes all received frames. A usual terminal
program can be used for testing. The Fujitsu SK-Wizard tool is predestinated for this
purpose. Be aware that you need a RS232 level shifter (e. g. MAX232) when you connect
the MCU ports to the PC-COM port.
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES.
*/
/*
(C) Fujitsu Microelectronics Europe GmbH
*/
/*---------------------------------------------------------------------------*/
// GLOBAL VARIABLE DEFINITIONS HERE!
#include "mb90540.h"
void main(void)
{
InitIrqLevels();
__set_il(7);
__EI();
// allow all levels
// globaly enable interrupts
Baudrate = 416;
init_UART();
// 19200 bits/s
TXE = 1;
RXE = 1;
ENIR_EN0 = 1;
// enable transmission
// enable reception
//
... and ext int for start bit detection
while (1)
{
while (RDRF == 0);
// wait for reception
clrerr();
// clear possible errors (ignore)
K
MCU-AN-390096-E-V12
- 14 -
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Chapter 2 Example Program
RDRF = 0;
transmit(RXReg);
K
// echo reception
}
}
Note, that normally the transmission data register empty “flag” (TDRE) has to be checked
before using the function transmit(unsigned int data). In the example above it is
assumed that the time between two frames is much larger than the time of the frame itself,
so that there are no transmission conflicts are expected.
Don’t forget to set the interrupt levels and handler function names in the declaration file
vectors.c.
© Fujitsu Microelectronics Europe GmbH
- 15 -
MCU-AN-390096-E-V12
DUPLEX SOFTWARE UART
Chapter 3 Baud Rates
3 Baud Rates
THIS CHAPETER GIVES AN OVERVIEW ABOUT USABLE BAUDRATES
3.1 Consideration
There are three points which are limiting the usable baud rate:
•
MCU main clock speed
•
MCU Performance decrease
•
Delay time between external interrupt event and interrupt execution (15 Ms)
3.2 Baud Rate Table
Therefore the following baud rates and corresponding reload timer values are recommended
for different MCU speeds:
4 MHz
8MHz
16MHz
24 MHz
Val.
Dev.
Val.
Dev.
Val.
Dev.
Val.
Dev.
2400 Bits/s
833
0.4%
1667
<0.1%
3333
<0.1%
5000
0%
4800 Bits/s
416
1.6%
833
0.4%
1667
<0.1%
2500
0%
9600 Bits/s
-
-
416
1.6%
833
0.4%
1250
0%
14400 Bits/s
-
-
-
-
556
-0.7%
833
0.4%
19200 Bits/s
-
-
-
-
416
1.6%
525
0%
28800 Bits/s
-
-
-
-
-
-
416
1.6%
MCU-AN-390096-E-V12
- 16 -
© Fujitsu Microelectronics Europe GmbH
DUPLEX SOFTWARE UART
Chapter 4 Appendix A
4 Appendix A
APPENDIX A
4.1 Source Code
Please find the source codes of this application note adapted for MB90340 series in the zip
file: mcu-an-390096-e-v12-software_uart.zip
© Fujitsu Microelectronics Europe GmbH
- 17 -
MCU-AN-390096-E-V12