ST7 Software Library
ST7 Software Library
ST7 Software Library
USER MANUAL
November 2005
Ref: DOC-ST7SOFT-LIB
USE IN LIFE SUPPORT DEVICES OR SYSTEMS MUST BE EXPRESSLY AUTHORIZED.
STMicroelectronics PRODUCTS ARE NOT AUTHORIZED FOR USE AS CRITICAL COMPONENTS IN
LIFE SUPPORT DEVICES OR SYSTEMS WITHOUT THE EXPRESS WRITTEN APPROVAL OF STMi-
croelectronics. As used herein:
1.Life support devices or systems are those which 2. A critical component is any component of a life
(a) are intended for surgical implant into the body, support device or system whose failure to perform
or (b) support or sustain life, and whose failure to can reasonably be expected to cause the failure
perform, when properly used in accordance with of the life support device or system, or to affect its
instructions for use provided with the product, can safety or effectiveness.
be reasonably expected to result in significant in-
jury to the user.
1
USER MANUAL
ST7 FAMILY
ST7 SOFTWARE LIBRARY
1 INTRODUCTION
This document describes the features, the files structure, examples, module drivers and
guidelines for using the ST7 software library package.
Rev. 4.0
November 2005
Table of Contents
1 INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 OVERVIEW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 FEATURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4 LIBRARY STRUCTURE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.1 ST7_LIBX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.2 ST7LIB_CONFIG.H . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.2.1 User part of the ST7lib_config.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2.2 Non-User part of the ST7lib_config.h . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.4 DEVICES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.5 DOCUMENTATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.6 DEMO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5 EXAMPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4/235
1
Table of Contents
5.2.2 STVD7_3x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.2.3 winIDEA (only for ST72F561 and CAN peripheral) . . . . . . . . . . . . . . . . 15
8 RELEASE INFORMATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.1 PERIPHERALS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8.2 DEVICES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
9 FUNCTION DESCRIPTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5/235
Table of Contents
9.1.6 16-bit TIMER (TIMER) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
9.1.7 8-bit TIMER (TIMER8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
9.1.8 LITE TIMER (LT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.1.9 PWMART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9.1.10 LITE AUTO-RELOAD TIMER (LART) . . . . . . . . . . . . . . . . . . . . . . . . . 171
9.1.11 TBU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
9.1.12 WDG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
9.1.13 ITC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
9.1.14 MCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
9.1.15 EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
9.1.16 I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
10 APPENDIX A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
235
6/235
Overview
2 OVERVIEW
2.2 FEATURES
– NEW: Supports new devices ST72325 and ST7232A
– NEW: Provided workspace for both STVD7 version 3.x and 2.x
– The ST7 library package consists of device driver library files, the configuration and setup
files.
– With each peripheral, application example code is provided. This is an application tailored to
a specific ST7 device, which uses the library functions to drive the peripheral. You can use
it without modification in an ST development kit.
– A detailed function reference is provided for each peripheral
– The functional behaviour and input/output parameters of each function are described in de-
tail in the user manual
– The functions are coded in ’C’ and are compatible with Metrowerks & Cosmic compilers
– The ST7 library is MISRA compliant
– Registry Key is added to provide information on installation path and version
7/235
Getting Started with Tools
8/235
1
Getting Started with Tools
9/235
Library Structure
4 LIBRARY STRUCTURE
4.1 ST7_LIBX
Location: \Root directory
Description: The ST7_libx (where x represents the latest numeric version of the library) is in-
stalled by default in the root directory. It is comprised of five main components: the ST7library
configuration file, the Peripherals (Device driver) library folder, the devices configuration files
folder, documentation on the package and the demo folder. The location of these components
is described in this section and shown in the figure given below.
Note: The Example directory is shown in Figure 2 on page 14.
Figure 1. Main Directory structure
ST7_LIBx
Periph 1 Device 1
Periph 2
Device n
Periph14
device_reg.h
device_reg.c
C Folder device_periph.h
Metrowerks
Example Cosmic
periph.c
periph.h
periph_hr.h
4.2 ST7LIB_CONFIG.H
Location: ST7_libx\ST7lib_config.h
Description: ST7lib_config.h is the entry point for the user. You have to include this file in your
application (main.c). This file is used to define specific labels for example, to define the mode
of transmission of communication peripherals, cpu frequency, etc.
10/235
Library Structure
11/235
Library Structure
Description: Each subdirectory contains a ‘C’ sub folder which contains peripheral library files.
4.3.2.1 C directory
Location: ST7_libx\Peripherals\Periph\sources\C
Description: Each subdirectory contains the source files, header files and an example folder
showing the usage of the functions.
Files: Periph.c, Periph.h, Periph_hr.h
Periph.c
Inclusion of periph_hr.h, periph.h, ST7lib_config.h. It contains the Peripheral functions with
some conditional compilation options.
Periph.h:
This contains the (typedef enum) parameters for each peripheral, prototypes of functions de-
fined in Peripheral.c and definition of Peripheral constant definitions.
Periph_hr.h
This file contains the bit mapping of the hardware registers used for the peripherals.
4.4 DEVICES
Location: ST7_LIBx\Devices
Description:
1. Contains the files which define all registers for each device and includes the file which
is used to select peripherals for the application. This register file is included in the
ST7lib_config.h.
The folder ST7_LIBx\Devices\ST7xx contains st7xx_reg.h, st7xx_reg.c and
st7xx_periph.h files.
st7xx_reg.h: This file contains a declaration of the register variables of st7xx for
Metrowerks and definitions of the register variables for the Cosmic compiler.
st7xx_reg.c: This file contains definitions of the register variables of st7xx device.
st7xx_periph.h: This file is used to select which peripherals of st7xx device are used in
the application.
2. Contains the generic configuration files both Metrowerks and Cosmic compilers.
Metrowerks: Contains the mapping file (ST72xxx.prm) for all the hardware registers in de-
vice, Make file (ST72xxx.mak) to build the application and the default.env which defines
all the useful paths and options for the application.
COSMIC: make file to build the application (ST72xxx.mak), link file (ST72xxx.lkf) used to
link the device and the interrupt mapping file (vector_xxx.c) for the target device.
Notes:
1. This software covers 13 main devices and their subsets. You have to include the file from
the main device section in order to support the related subsets.
2. The register files in the ST7 library are different from those provided with the STVD7 ver 3.x.
Take care to include the correct one.
12/235
Library Structure
4.5 DOCUMENTATION
Location: ST7_LIBx\Documentation
Description: This directory contains the global user manual describing each peripheral library
and its use in detail.
Files: user manual.pdf
4.6 DEMO
Location: ST7_LIBx\DEMO
Description: This directory contains an application program which demonstrates the use of the
ST7LIB on the devices ST72F521, ST72F62, ST7FLITE0, ST7FLITE2, ST7SUPERLITE,
ST72F561 and ST72325. The program uses all the peripheral libraries together for a particular
application. The purpose of the demo is to help to develop an application using the software li-
brary.
13/235
Example
5 EXAMPLE
Location: ST7_LIBx\Peripherals\Periph\sources\C\Example
Description: Contains the example application code for each peripheral individually. The code
has been developed using the peripheral library functions exercises the functionality of that
peripheral. The configuration and workspace has been provided for users of both STVD7
ver2.x and 3.x. The example has been compiled and tested using both Metrowerks and
Cosmic compilers and configuration files are provided.
Subdirectories: Sources, workspace
Figure 2. Example directory structure
Example
Sources workspace
main.c
st7lib_config.h
st72xx_periph.h STVD7_2x STVD7_3x
periph_mwerks.stw
Config periph_mwerks.wsp Object periph_mweks.stp
periph_csmc.wsp interrupt_vector.c
vector.c
Metrowerks periph_csmc.stw
Cosmic Metrowerks Cosmic Metrowerks
periph_csmc.stp
14/235
Example
5.2 WORKSPACE
This folder contains configuration and workspace files for both STVD7 ver2.x and 3.x as per
the directory structure shown in figure 2.
Note:
1. For ST72F561 demo and CAN peripheral, winIDEA workspace is also available.
2. For ST72325 and ST7232A demo STVD7_3X workspace is only available.
5.2.1 STVD7_2x
This folder contains relevant configuration files for ST7 Visual Debug ver 2.x. Subdirectories:
Config, Object
Config Folder: Contains the configuration files for both Metrowerks and Cosmic compilers.
metrowerks: Contains the mapping file (ST72xxx.prm) for all the hardware registers in the de-
vice, the Make file (ST72xxx.mak) for building the application and the default.env file which
defines all the useful paths names and options for the application.
COSMIC: Contains the make file for building the application (ST72xxx.mak), the link file
(ST72xxx.lkf) used to link the device and the interrupt mapping file (vector_xxx.c) for the target
device.
Object Folder:
These folders are used for temporary storage of object and executable files generated by the
compiler in respective directories - metrowerks and cosmic.
5.2.2 STVD7_3x
This folder contains relevant Cosmic and Metrowerks workspace for ST7 Visual Debug ver
3.X, as the configuration files are automatically generated.
15/235
How to use the library
16/235
How to use the library
7. Write the application program using the library functions given in the user manual for each
peripheral and compile.
Caution: Only the ST7lib_config.h and the files contained in the configuration subdirectory of
the examples folder are user-modifiable, the rest of the source files are write protected.
Changing peripheral source files and header files may adversely affect the library operations
and this will be complicated to update when there are new library releases.
17/235
How to use the library
6.2.2 SPI
SPI: This part of the user manual contains the detailed description of all the functions for the
SPI. An example C program has been given at the end.
The SPI can be used as Single master (multiple slaves) and multi master systems in full du-
plex mode. This can be configured by using parallel port pins to control the SS pin by software.
The transfer of master or slave control can be implemented using a handshake method
through the I/O ports or by an exchange of code messages through the serial peripheral inter-
face system.
In order to respect the SPI protocol, you must define the configuration setting
SPI_SLAVE_CONFIG in ST7lib_config.h file as shown below, in order to be able to transmit
data in software slave mode. #define SPI_SLAVE_CONFIG To select any of the possible
communication modes described below you need to select the corresponding #define state-
ment inside the ST7lib_config.h file. These modes are applicable for all communication pe-
ripherals (SPI, SCI and I2C).
Polling:
With this mechanism the data can be transmitted or received by polling the status of the cor-
responding flag. Both single and continuous Transmission/Reception is possible. In the case
of continuous Transmission/Reception the function keeps control until all the requested data is
Transmitted/Received and hence the application software has the risk of losing control if there
is a breakdown in communication (the SCI mode is disabled). To avoid the risk, you can use
the single byte transmission with some timeout protection inside this mechanism. This mech-
anism can only be used with the SPI in half duplex mode. To use this mode, you must have se-
lected the following # define labels inside the ST7lib_config.h file:
SPI_POLLING_TX -- For Transmission mode
SPI_POLLING_RX -- For Reception mode
Interrupt driven without communication buffer:
Data can be Transmitted/Received both in single as well as continuous mode through the in-
terrupt driven mechanism. In the continuous mode the user data is directly being read/written
from/to the addresses passed by the user. After each byte of data transfer an interrupt is ac-
knowledged and the control goes to the interrupt subroutine. The main advantage of using in-
terrupts rather than polling is that control does not stay in the function till the last data is Trans-
mitted/Received and hence the SPI can be used in full duplex mode. Here you should take
care not to read/write the user buffer until the Transmission/Reception completion. To use this
mode you must select the following # define labels inside the ST7lib_config.h file:
SPI_ITDRV_WITHOUTBUF_TX -- For Transmission
SPI_ITDRV_WITHOUTBUF_RX -- For Reception
Notes:
1. If both SPI_ ITDRV_WITHOUTBUF_TX and SPI_ITDRV_WITHOUTBUF_RX are defined
in full duplex mode, then the program will perform either transmission or reception (only trans-
mission as per the present structure) since, the peripheral has a single interrupt for both
Transmission and Reception completion. Because of this correct full duplex communication
will be prevented. In order to operate the SPI in Full Duplex Mode, it is required that either the
18/235
How to use the library
Transmission or Reception is performed in Polling Mode and the other in Interrupt Driven
Mode. So, you can use any one of the following combinations in full duplex mode.
SPI_POLLING_TX -- For Transmission mode
SPI_ITDRV_WITHOUTBUF_RX -- For Reception
(or)
SPI_ITDRV_WITHOUTBUF_TX -- For Transmission
SPI_POLLING_RX -- For Reception mode
6.2.3 I2C
This part of the user manual contains the detailed description of all the functions for I2C. An
example C program has been given at the end. You can select either of the two Transmission/
Reception modes implemented in the library. To select any of the possible modes described
below, you need to select the corresponding #define statement inside the ST7lib_config.h file.
Polling:
With this mechanism the data can be transmitted or received by polling the status of the cor-
responding flag. Either single or continuous Transmission/Reception is possible. In contin-
uous Transmission/Reception control stays inside the function until all the requested data is
Transmitted/Received and hence the application software risks losing control if there is a
breakdown in communication (if the I2C mode is disabled). To avoid the risk, the appplication
can use single byte transmission with some timeout protection. This mechanism can only be
used with the I2C in half duplex mode. To use this mode, you must have selected the following
# define labels in the ST7lib_config.h file:
I2C_POLLING_TX -- For Transmission mode
I2C_POLLING_RX -- For Reception mode
Interrupt driven without communication buffer
Data can be Transmitted/Received both in single as well as continuous mode through the in-
terrupt driven mechanism. In continuous mode the user data is directly read/written from/to the
addresses passed by the user. After each byte of data transfer an interrupt is acknowledged
and the control goes to the interrupt subroutine. The advantage of using interrupts rather than
polling is that control does not stay in the function till the last data is Transmitted/Received.
Here, care should be taken not to read/write the user-buffer until the Transmission/Reception
completes. To use this mode, you must select the following # define labels inside the
ST7lib_config.h file:
I2C_ITDRV_WITHOUTBUF_TX -- For Transmission
I2C_ITDRV_WITHOUTBUF_RX -- For Reception
Master Receiver Communication Methodology:
In Master receiver mode, to close the communication the STOP bit must be set to generate a
stop condition, before reading the last byte from the DR register. In order to generate the non-
acknowledge pulse after the last received data, the ACK bit must be cleared just before
reading the second last byte. The following flowchart shows the management of the ACK and
STOP bits, when the master is receiving.
19/235
How to use the library
When‘N-2’ byte
is received Read I2CDR
Wait for BTF = 1
In I2C_GetBuffer, the ACK and STOP bits are automatically managed inside the function. In
I2C_GetByte you must manage the ACK and STOP bits as shown below, in order to receive
only one byte.
20/235
How to use the library
I2C_Load_Address
(send slave address)
Read I2CSR1
Clear ACK in I2CCR register
by using the function I2C_ACK
I2C_GetByte
6.2.4 CAN
This Section gives an overview of the user guidelines for the CAN Library. The library provides
the software routines to use the CAN peripheral for ST72F561 device. The library is based on
the HIS / Vector CAN driver specification. The implemented function list is a subset of the func-
tions described in the HIS / Vector CAN driver specification document.
21/235
How to use the library
6.2.4.1 DESCRIPTION
Files
Can.c - This file contains the CAN driver source code.
Can.h - This file contains the data structure, data type definitions and function prototypes for
the driver functions.
Can_hr.h - This file contains the #define statements for the driver functions.
User.c - This file contains the Global data declaration which is used by both driver as well as
application. For example - Tx & Rx buffers, Tx & Rx Id, Confirmation & Indication flags etc.
User.h - This file contains all user configurable parameters. For example- size of Tx & Rx
buffers, Number of Tx & Rx messages, hardware registers initialization values etc.
Note: User.c and User.h can be configured by the user depending on the application.
6.2.4.2 DATA STRUCTURES
Init Structure
Init structure contains the initialization values for the CAN controller registers. The user appli-
cation may have more than one Init Structures which is configured by the parameter
NO_OF_INIT_HANDLES. The init handle is used as an index for the init structure.
Transmit Structure
The transmit structure contains the information about the transmitted message, for example
tx_Id, tx_dlc, tx_buffer. There is a separate table each for Id, dlc, pointer to tx_buffer. The
number of transmit structures depends on the number of messages to be transmitted in an ap-
plication. It is configured by the parameter NO_OF_TX_HANDLES. The tx_handle is used as
an index for each transmit structure. The tx_identifier has two tables: one table each for
standard and extended identifiers. The tx_id, tx_dlc tables are configured by the user as per
the message to be transmitted. There is a single bit confirmation flag for each transmit mes-
sage.
Receive Structure
The receive structure contains the information about the received message, for example
rx_Id, rx_dlc, rx_buffer. There is separate table each for Id, dlc, pointer to rx_buffer. The
number of receive structures depends up on number of messages to be received in an appli-
cation. It is configured by the parameter NO_OF_RX_HANDLES. The rx_handle is used as an
index for each receive structure. The rx_identifier has two tables: one table each for standard
and ex tended identifiers. The identifier for the received message is stored inside this table by
the driver. The rx_dlc is configured by the user as per the length of the message. There is a
single bit indication flag and overflow flag for each received message.
22/235
How to use the library
This is a transmit message structure referenced for the CAN driver service
CanMsgTransmit().
6.2.4.4 MEMORY USAGE
Constants
This includes the initialization values inside the Init Structure for the CAN controller registers,
transmit message information(tx_id, tx_dlc) inside the transmit structure, and receive mes-
sage information(rx_dlc). These are stored in ROM.
Global Variables
These include the transmit & receive buffers, confirmation & indication flags, pointers to the Tx
& Rx buffers, and receive message id’s(rx_id).
6.2.4.5 PARAMETER CONFIGURATION
There are certain parameters that you have to configure depending on the application. These
parameters are configured inside the files user.c & user.h.
The following are the parameters that must be configured in the file user.h -
1. NO_OF_TX_HANDLES - This parameter defines the number of messages to be trans-
mitted by the application.
2. NO_OF_RX_HANDLES- The number of messages to be received by the application which
depends on the number of messages configured in the filter registers.
3. NO_OF_INIT_HANDLES- The number of initialization structures required in an application.
By default, its value is set to 1.
23/235
How to use the library
4. TX_MSGx_STDID - The standard id part for the MSGx to be transmitted, where x = transmit
message number.
5. TX_MSGx_EXTID - The extended id part for the MSGx to be transmitted, where x =
transmit message number.
6. TX_MSGx_DLC - The length for the MSGx to be transmitted, where x = transmit message
number. The maximum length that can be defined is 8.
7. RX_MSGx_DLC - The length for the MSGx to be received, where x = receive message
number. The maximum length that can be defined is 8.
8. REG_INITx_VALUE - The register initialization value for the CAN controller register, where
REG - register name, x = init structure number.
The following are the parameters that must be configured in the file user.c -
1. MSGx_Tx_Buffer - This declares the buffer for the MSGx to be transmitted. The length of
the buffer is the same as defined by the parameter TX_MSGx_DLC in the file user.h. The
number of such buffers to be declared is the same as defined by the parameter
NO_OF_TX_HANDLES in the file user.h.
2. tx_stdid[ ] - This table stores the standard id value/s for the message/s to be transmitted.
The length of the table is the same as defined by the parameter NO_OF_TX_HANDLES and
the value/s stored inside is/are the same as defined by the parameter TX_MSGx_STDID in
the file user.h. If the message to be transmitted is Extended then above has to logical-ORed
with the EXT_ID_MASK(IDE bit), EXID17 & EXID16 bit values. As a result, the values stored
inside the table are in the same format as of MIDR0 & MIDR1 registers.
3. tx_extid[ ] - This table stores the extended id value/s for the message/s to be transmitted.
The length of the table is the same as defined by the parameter NO_OF_TX_HANDLES and
the value/s stored inside the table is/are the same as defined by the parameter
TX_MSGx_EXTID in the file user.h. It stores the values into the same format as of MIDR2 &
MIDR3 registers.
4. tx_dlc[ ] - This table stores the length for the message/s to be transmitted. The length of the
table is the same as defined by the parameter NO_OF_TX_HANDLES and the value/s stored
inside the table is/are the same as defined by the parameter TX_MSGx_DLC in the file user.h.
5. tx_data_ptr[ ] - This pointer table stores the address of transmit buffer/s (MSGx_Tx_buffer).
The length of the table is the same as defined by the parameter NO_OF_TX_HANDLES in the
file user.h.
6. MSGx_Rx_Buffer - This declares the buffer for the MSGx to be received. The length of the
buffer is the same as defined by the parameter RX_MSGx_DLC in the file user.h. Number of
such buffers to be declared is the same as defined by the parameter NO_OF_RX_HANDLES
in the file user.h.
24/235
How to use the library
7. rx_stdid - This declares the memory for storing the standard id part of the message to be re-
ceived. The length of the table is the same as defined by the parameter
NO_OF_RX_HANDLES in the file user.h.
8. rx_extid - This declares the memory for storing the extended id part of the message to be re-
ceived. The length of the table is the same as defined by the parameter
NO_OF_RX_HANDLES in the file user.h.
9. rx_dlc[ ] - This table stores the length for the message/s to be received. The length of the
table is the same as defined by the parameter NO_OF_RX_HANDLES and the value/s stored
inside the table is/are the same as defined by the parameter TX_MSGx_DLC in the file user.h.
10. rx_data_ptr[ ] - This pointer table stores the address of receive buffer/s
(MSGx_Rx_Buffer). The length of the table is the same as defined by the parameter
NO_OF_RX_HANDLES in the file user.h.
6.2.4.6 Tx & Rx BUFFER USAGE
Data can be accessed through the Tx & Rx buffers using the tx_handle & rx_handle as an
index. For example, data can be written into the MSGx_Tx_Buffer using the pointer
tx_data_ptr[x], where x = tx_handle for the message. Similarly, data can be received from
MSGx_Rx_Buffer using the pointer rx_data_ptr[x], where x = rx_handle for the message.
6.2.4.7 IMPLEMENTATION HINTS
– CanSleep( ) service must not be called when message transmission is in progress otherwise
sleep mode is not entered and service returns KCANFAILED. Also CanTransmit( ) service
shall not be called while the CAN driver is in sleep mode.
– Confirmation flag is set by the driver after the successful transmission of a message and flag
has to be cleared by the application. Application must call CanInterruptDisable( ) and Can-
InterruptRestore( ) services when clearing the confirmation flag in order to avoid CAN inter-
rupt.
– Indication flag is set by the driver for a message received and this flag has to be cleared by
the application. Application must call CanInterruptDisable( ) and CanInterruptRestore( )
services when clearing the indication flag in order to avoid CAN interrupt.
– Overflow flag is set by the driver if the indication flag is not cleared by the application or mes-
sage is not copied by the application from the global buffer into the application buffer.
If the overflow is set it means that the new message has been overwritten over the previous
message. Overflow flag has to be cleared by the application. Application must call CanInter-
ruptDisable( ) and CanInterruptRestore( ) services when clearing the overflow flag in order to
avoid CAN interrupt.
– While copying data from receive buffer, application must call CanInterruptDisable( ) and
CanInterruptRestore( ) services in order to avoid a CAN interrupt. Similarly, while copying
25/235
How to use the library
data into the transmit buffer, application must call CanInterruptDisable( ) and
CanInterruptRestore( ) services to avoid an interrupt.
26/235
Presentation of library functions
27/235
Release Information
8 RELEASE INFORMATION
This release supports the following peripherals and devices.
8.1 PERIPHERALS
– ADC (8-bit and 10bit): The on-chip Analog to Digital Converter (ADC) peripheral is a 10-bit,
successive approximation converter with internal sample and hold circuitry.
– SCI (with/without extended Baud Rate Pre scalar): The Serial Communications Interface
(SCI) offers a flexible means of full-duplex data exchange with external equipment requiring
an industry standard NRZ asynchronous serial data format.
– SPI: The Serial Peripheral Interface (SPI) allows fullduplex, synchronous, serial communi-
cation with external devices.
– I2C single/multi master: The Inter-Integrated Circuit Bus Interface serves as an interface be-
tween the microcontroller and the serial I2C bus.
– I2C Slave
– 16-bit Timer: The timer consists of a 16-bit free-running counter driven by a programmable
prescaler.
– 8-bit Timer: The timer consists of a 8-bit free-running counter driven by a programmable
prescaler.
– 8-bit Lite timer: The Lite Timer can be used for general-purpose timing functions.
– PWM ART 8-bit: The Pulse Width Modulated Auto-Reload Timer on-chip peripheral consists
of an 8-bit auto reload counter with compare/capture capabilities and of a 7-bit prescaler
clock source.
– AR 12-bit timer: The 12-bit Autoreload Timer can be used for general-purpose timing func-
tions.
– TBU: The Timebase unit (TBU) can be used to generate periodic interrupts.
– WDG: The Watchdog timer is used to detect the occurrence of a software fault.
– ITC: The Interrupt Controller manages the hardware and software interrupts with flexible in-
terrupt priority and level configuration.
– MCC: The Main Clock Controller consists of a programmable CPU clock prescaler, a clock-
out signal to supply external devices and a real time clock timer with interrupt capability.
– EEPROM: The Electrically Erasable Programmable Read Only Memory can be used as a
non volatile backup for storing data.
– I/Os: An I/O port contains up to 8 pins. Each pin can be programmed independently as digital
input (with or without interrupt generation) or digital output.
– CAN: The Controller area Network peripheral allows communication over a CAN network.
28/235
Release Information
8.2 DEVICES
– ST72F62
– ST72F63B
– ST72F65
– ST72F521
– ST7FLITE0
– ST7FLITE1
– ST7FLITE2
– ST7FLITE3
– ST72F264
– ST72F561
– ST7SUPERLITE
– ST72325
– ST7232A
29/235
Function Descriptions
9 FUNCTION DESCRIPTIONS
30/235
Function Descriptions
Table 2. ADC_Select_Channel
Function Name ADC_Select_Channel
Function Prototype Void ADC_Select_Channel (unsigned char ADC_AIN)
Selects the conversion channel by passing the channel
Behaviour Description
number as input parameter
ADC_AIN
ADC_AIN is in the range [0:15]
Input Parameters
The channel number depends on the device, please refer
to the corresponding datasheet.
Output Parameters None
1. ‘ADC_Init’ must have been called.
Required Preconditions 2. The selected channel must be configured as floating in-
put.
Functions called None
Postconditions ADC channel selected
See also ADC_Enable
Table 3. ADC_Enable
Function Name ADC_Enable
Function Prototype Void ADC_Enable (void)
Switches on the ADC to start conversion on the selected
Behaviour Description
channel.
Input Parameters None
Output Parameters None
Required Preconditions ADC_Select_Channel must have been called.
Functions called None
Postconditions ADC conversion started
See also ADC_Disable
31/235
Function Descriptions
Table 4. ADC_Test_Conversn_Complete
Function Name ADC_Test_Conversn_Complete
Function Prototype BOOL ADC_Test_Conversn_Complete (void)
Behaviour Description Returns the latest status of conversion
Input Parameters None
TRUE: conversion completed
Output Parameters
FALSE: conversion not completed
Required Preconditions ADC_Enable must have been called.
Functions called None
If TRUE, ADC conversion is complete and you can call the
ADC_Conversn _Read.
Postconditions If FALSE, ADC conversion not completed.
This function can be looped until the conversion is com-
plete.
See also ADC_Disable, ADC_Conversn_Read
Table 5. ADC_Conversn_Read
Function Name ADC_Conversn_Read
For 10-bit ADC,
unsigned int ADC_Conversn_Read (void)
Function Prototype
For 8-bit ADC,
unsigned char ADC_Conversn_Read (void)
Behaviour Description Reads the converted digital value from the data register.
Input Parameters None
Data Register value (it depends upon the device selected,
Output Parameters
please refer to the corresponding data sheet).
Required Preconditions ADC_Test_Conversn_Complete must have been called.
Functions called None
1. EOC flag is cleared.
Postconditions 2. Equivalent digital value available in the data register is
returned.
See also None
Note: The EOC flag may be set again during the execution of this function, this depends on
the conversion time.
32/235
Function Descriptions
Table 6. ADC_Disable
Function Name ADC_Disable
Function Prototype Void ADC_Disable (void)
Behaviour Description Stops ADC conversion on the selected channel.
Input Parameters None
Output Parameters None
Required Preconditions ADC is switched on
Functions called None
Postconditions ADC switched off
See also ADC_Enable
Example:
The following C program shows the use of the ADC functions.
Program description:
This program converts the analog value on channel 5 of the ST72F62 device to a digital value.
/* Program start */
#include “st7lib_config.h” /* Select st72F62 device */
void main (void);
void main(void)
{
unsigned int Conv_Data1; /* Variable to get the converted digital value */
unsigned char channel = 5;
33/235
Function Descriptions
9.1.2 SCI
This Library supports 2 SCI of ST72F561 device and 1 SCI on all other devices.
For devices with only one SCI no suffix “x” is used in the function names.
For 2nd SCI of ST72F561 you must replace suffix “x” in the function names with 2.
Function Name SCIx_Init
Void SCIx_Init (SCI_Type_Param1 Init_Value1,
Function Prototype
SCI_Type_Param2 Init_Value2)
Initialization of SCI, sets by default receiver sleep off, no break
character will be transmitted, wakeup from sleep by idle frame
detection, Parity disabled, 8-bit transmission mode and Receiver
Behaviour Description
in active mode. You can select the parity, 9-bit transmission
mode, Receiver wakeup, Receiver Mute and Break Enable fea-
ture through properly selecting the Init_Value1 and Init_Value2.
SCI_ODPARITY_SELECT
Select odd Parity
SCI_EVPARITY_SELECT
Select even parity
SCI_WAKEUP_ADDR
Input Parameter 1 Receiver wake up from mute mode while address mark is detect-
ed(i.e MSBit of the data transmitted should be 1)
SCI_WORDLENGTH_9
Select 9-bit transmission
SCI_DEFAULT_PARAM1
Load the register with the default value(0x00)
SCI_MUTE_ENABLE
Receiver in mute mode
SCI_BREAK_ENABLE
Input Parameter 2
Transmit break characters
SCI_DEFAULT_PARAM2
Load the register with the default value(0x00)
Output Parameter None
Required Preconditions SCI port pin should be configured properly.
34/235
Function Descriptions
Table 7. SCIx_Compute_Baudrate
Function Name SCIx_Compute_Baudrate
Void SCIx_Compute_Baudrate(unsigned int
Function Prototype
BaudRate_Tx, unsigned int BaudRate_Rx)
Selects Transmitter/ Receiver baudrate for the SCI without
Behaviour Description
extended prescaler.
BaudRate_Tx*
Input Parameter 1
You can select any possible baudrate for transmission.
BaudRate_Rx*
Input Parameter 2
You can select any possible baudrate for reception.
Output Parameter None
1. SCIx_Init, must have been called.
Required Preconditions
2. fcpu must have been defined in ST7lib_Config.h
Functions called None
Postconditions None
Note:
– This function takes a large ROM area as calculations for TR, RR and PR are done inside the
function. However, you can choose to pass the baudrate directly.
– If the selected baudrate speed is not possible, the closest possible value will be used.
– If there is no common prescalar factor for receiver and transmitter baudrates, then you will
get the nearest possible receiver baudrate, at the prescalar division factor selected for the
transmitter.
– In half Duplex mode you can pass the same transmitter and receiver baudrates to get the
exact Tx/Rx baudrate (whichever mode you are using)
35/235
Function Descriptions
Table 8. SCIx_Select_Baudrate
Function Name SCIx_Select_Baudrate
Void SCIx_Select_Baudrate (SCI_Baudrate_Type
Function Prototype
Baudrate_Prescaler)
Selects Transmit/Receive baudrate for SCI without ex-
tended baudrate prescaler. You have to define all the
Behaviour Description prescaler parameters corresponding to the desired
baudrate speed. You have to pass the three input param-
eters by logically ORing them to select the baudrate.
SCI_PR_X
X=1,3, 4,13
SCI_TR_Y
Input Parameters
Y=1,2,4,8,16,32,64,128
SCI_RR_Z
Z=1,2,4,8,16,32,64,128
Output Parameter None
1.SCIx_Init must have been called
Required Preconditions 2. You have to specify the PR, RR and TR values for the
desired baudrate
Functions called None
Postconditions Refer to the table below.
Note: This function saves the ROM area but you have to pass the values for the TR,PR RR.
36/235
Function Descriptions
Table 9. SCIx_Extend_Baudrate
Function Name SCIx_Extend_Baudrate
Void SCIx_Extend_Baudrate (SCI_Baudrate_Type
Function Prototype Baudrate_Prescaler, unsigned char EPTR, unsigned char
EPRR)
Selects Transmit/Receive baudrate for SCI with extended
Behaviour Description baudrate prescaler. You have to define all the prescaler
parameters corresponding to the desired baudrate speed.
SCI_PR_X
X=1,3, 4,13
SCI_TR_Y
Input Parameter 1
Y=1,2,4,8,16,32,64,128
SCI_RR_Z
Z=1,2,4,8,16,32,64,128
EPTR
Input Parameter 2
Select any value from 0 to 255
EPRR
Input Parameter 3
Select any value of EPRR from 0 to 255
Output Parameter None
1.SCIx_Init must have been called
Required Preconditions 2. You have to specify the PR, RR and TR values for the
desired baudrate
Functions called None
Postconditions Refer to the table below.
Receiver
Transmitter
SCI_PR_X TR_Y RR_Z EPTR EPRR baudrate
baudrate speed
Speed
SCI_T SCI_RR
SCI_PR_13 1 1 38400 38400
R_1 _1
SCI_T SCI_RR
SCI_PR_13 2 2 9600 9600
R_2 _2
SCI_T SCI_RR
SCI_PR_13 3 3 3200 3200
R_4 _4
SCI_T SCI_RR
SCI_PR_13 4 4 1200 1200
R_8 _8
SCI_T SCI_RR
SCI_PR_13 5 5 480 480
R_16 _16
SCI_T SCI_RR
SCI_PR_13 6 6 200 200
R_32 _32
37/235
Function Descriptions
38/235
Function Descriptions
39/235
Function Descriptions
Notes:
– You can use some timeout protection while using this function.
– This function is for Polling or Interrupt driven mode.
40/235
Function Descriptions
41/235
Function Descriptions
42/235
Function Descriptions
Notes:
– In transmission, the return value of the function is ignored. In reception, the input parameter
is not significant.
– You must call this function while using 9 bit mode.
– The Status of the 9th bit remains same during the complete buffer/string transmission.
– You can change the status of 9th bit in the next request.
43/235
Function Descriptions
Notes:
– You can use some timeout protection while using this function.
– This function can be used in Polling or Interrupt driven mode.
44/235
Function Descriptions
45/235
Function Descriptions
46/235
Function Descriptions
47/235
Function Descriptions
48/235
Function Descriptions
49/235
Function Descriptions
– If a reception request for a set of data is over, this function will return the error status of that
request only once. If this function is called again (before making next reception request), then
the function will check for single byte reception.
– In Polling mode, this function is used in conjunction with SCIx_GetByte only.
Note: You can call this function whenever you want to force the error and status flags be
cleared.
Caution: Do not call this function if a reception request is ongoing as it will corrupt the recep-
tion status by clearing all the flags and you will not receive any error status.
50/235
Function Descriptions
51/235
Function Descriptions
52/235
Function Descriptions
EXAMPLE:
The following C program shows how the SCI functions are used.
This program runs the following sequence for an SCI without extended baudrate prescaler for
polling or interrupt driven mode:
– transmits a single byte passed by the user
– transmits the 20 bytes of data at the baudrate 9600
– receives 20 bytes of data at a baud rate of 9600.
Note: You must define the communication mode and cpu speed (Fcpu) in the ST7lib_config.h
file.
You can select any of the communication speeds from the following list :
SCI_POLLING_TX -- For Transmission mode
SCI_POLLING_RX -- For Reception mode
SCI_ITDRV_WITHOUTBUF_TX -- For Transmission
SCI_ITDRV_WITHOUTBUF_RX -- For Reception
/* You can use a timeout function to handle the fault in which the control will get stuck in side
a loop.This function should have the Boolean return, i.e it should return TRUE if the expected
wait Time is not elapsed and FALSE if it is elapsed.*/
Software Control
#endif
/*============================================================*/
53/235
Function Descriptions
/* Program start */
#include "ST7lib_Config.h" /*List of all ST7 devices and communication mode*/
#include "main.h" /*Declaration of all the functions used in main.c*/
void main(void);
static unsigned int Timeoutcount;
unsigned char Buff_Rcv[20]= {" "} ;
#endif
/*-----------------------------------------------------------------------------
Reception Through Polling mechanism
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_RX /*Selects polling mode for reception*/
SCI_Mode(SCI_RX_ENABLE); /* Enable the receiver mode of SCI*/
54/235
Function Descriptions
/*----------------------------Buffer Reception-------------------------------*/
55/235
Function Descriptions
Userfunction ();
}
if((unsigned char)Err & SCI_PARITY_ERR)
{
Userfunction ();
}
}
#endif
/*-----------------------------------------------------------------------------
Transmission through Interrupt Driven without Buffer mode
-----------------------------------------------------------------------------*/
#ifdef SCI_ITDRV_WITHOUTBUF_TX /*Selects interrupt mode for transmission*/
/*Interrupt enable when TDRE flag is set */
SCI_Mode(SCI_TX_ENABLE);
if(SCI_IsTransmitCompleted())
{
SCI_PutByte((unsigned char)55);
while (!(SCI_IsTransmitCompleted()));
SCI_PutString(Buff); /*user pointer is copied to the global pointer*/
/* Here,user can perform other tasks or operations except transmission till the
time transmission is complete,after which user can perform transmission again*/
while (!(SCI_IsTransmitCompleted()));
}
#endif
/*-----------------------------------------------------------------------------
Reception through Interrupt driven without Buffer mechanism
-----------------------------------------------------------------------------*/
#ifdef SCI_ITDRV_WITHOUTBUF_RX /* Selects interrupt mode for transmission */
/*Interrupt enable when RDR register is ready to read*/
SCI_IT_Enable((unsigned char)SCI_RECEIVE_OVERRUN);
SCI_Mode(SCI_RX_ENABLE);
56/235
Function Descriptions
Userfunction ();
}
if((unsigned char)Err & SCI_FRAMING_ERR)
{
Userfunction ();
}
if((unsigned char)Err & SCI_PARITY_ERR)
{
Userfunction ();
}
Rx_Data = SCI_GetByte(); /*User will receive the corrupted data */
}
}
else
{
while(1); /*Transmitter or Receiver having problem */
}
/*------------------------------Buffer Reception-----------------------------*/
SCI_GetBuffer(Buff_Rcv,(unsigned char)19);
/* Any data received before calling this function is ignored*/
/* Here, user can perform other tasks or operations except reception till the
time the function SCI_IsReceptionCompleted() returns RECEIVE_OK,
after which user can perform reception again */
do
{
Err = SCI_IsReceptionCompleted();
}while (Err == SCI_BUFFER_ONGOING);
/* To be sure that the communication by this point has been completed */
if(Err == SCI_RECEIVE_OK)
{
Nop /*Reception OK */
}
else
{
if((unsigned char)Err & SCI_NOISE_ERR)
{
Userfunction ();
}
if((unsigned char)Err & SCI_OVERRUN_ERR)
{
Userfunction ();
}
if((unsigned char)Err & SCI_FRAMING_ERR)
{
Userfunction ();
}
if((unsigned char)Err & SCI_PARITY_ERR)
{
Userfunction ();
}
}
57/235
Function Descriptions
#endif
Nop
}
/******************************************************************************
Interrupt Subroutine
******************************************************************************/
/*******************************End OF ISR************************************/
/*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**/
void Userfunction(void)
{
SCI_Forced_Clear_Flag();
}
BOOL User_Timeout_Function(void)
{
while(Timeoutcount < 50000)
{
Timeoutcount++ ;
return (TRUE);
}
return (FALSE) ;
}
58/235
Function Descriptions
9.1.3 SPI
Following are the functions related to SPI:
Function Name SPI_Init
Void SPI_Init ( SPI_Init_Parameter1 Init_Value1,
Function Prototype
SPI_Init_Parameter2 Init_Value2)
Initialization of the SPI. By default the SPI is put in slave
mode (hardware selected), baudrate Fcpu/8, the SPI pe-
ripheral is not connected to the external pins (SPE=0), al-
Behaviour Description
ternate function of SPI output enabled (SOD=0) and
interrupts are disabled. You can change the default config-
uration by selecting input parameters given below.
You can pass one or more parameters by ‘OR’ing them.
SPI_DEFAULT
Reset Value
SPI_ENABLE
Enables the serial peripheral output (SPI alternate func-
tions connected to pins).
SPI_ENABLE_IT
Enables the Interrupt
59/235
Function Descriptions
Note: If you want to select the Interrupt driven communication mode, you must enable inter-
rupts during initialization.
60/235
Function Descriptions
Notes:
– The above function is only for SPI Polling or SPI Interrupt driven modes.
– It is recommended to add timeout protection when using this function.
– For transmission in software slave mode, you must define SPI_SLAVE_CONFIG in
ST7lib_config.h.
61/235
Function Descriptions
Notes:
– The above function is only for SPI Polling mode.
– For transmission in software slave mode, you must define SPI_SLAVE_CONFIG in
ST7lib_config.h.
Caution: The application can lose control if the SPI is disabled while using this function in poll-
ing mode.
62/235
Function Descriptions
Notes:
– The above function is only for SPI Polling mode.
– For transmission in software slave mode, you must define SPI_SLAVE_CONFIG in
ST7lib_config.h.
Caution: The application can lose control if the SPI is disabled while using this function in poll-
ing mode.
63/235
Function Descriptions
Notes:
– The above function is only for SPI Interrupt driven mode.
– For software slave mode transmission, you must define SPI_SLAVE_CONFIG in
ST7lib_config.h.
Caution:
– Do not access the string until transmission is completed
– Data transmission will stop if any error occurs during transmission.
64/235
Function Descriptions
Notes:
– The above function is only for SPI Interrupt driven mode.
– For transmission in software slave mode, you must define SPI_SLAVE_CONFIG in
ST7lib_config.h.
Caution:
– Do not access the string until transmission is completed.
– Data transmission will stop if any error occurs during transmission.
65/235
Function Descriptions
66/235
Function Descriptions
Notes:
– The above function is for SPI Polling or SPI Interrupt driven mode.
– It is recommended to add a timeout protection when using this function.
67/235
Function Descriptions
68/235
Function Descriptions
69/235
Function Descriptions
Note: The above function is only for SPI Interrupt driven mode.
Caution:
– Take care not to access the string until reception is complete.
– Any data received before calling this function is ignored.
– The data reception will stop if any error occurs during reception.
70/235
Function Descriptions
Note: The above function is only for SPI Interrupt driven mode.
Caution:
– Take care not to access the string until reception is complete.
– Any data received before calling this function is ignored
– The data reception will stop if any error occurs during reception.
71/235
Function Descriptions
72/235
Function Descriptions
Note: You must use this function only in the Interrupt service routine.
Caution: Special care must be taken, while you write your own code along with this function
inside the interrupt service routine. Otherwise, data transfer synchronisation will be affected,
which may lead to data loss or overrun error.
Note: You can call this function whenever you want to force the error and status flags to be
cleared.
Caution: Do not call this function if a reception request is ongoing as it will corrupt the recep-
tion status by clearing all the flags and you will not receive any error status.
.
73/235
Function Descriptions
EXAMPLE:
The following C program shows the use of SPI functions.
Program Description:
This program runs the following sequence for the SPI for SPI Polling and SPI Interrupt
driven modes:
1. Transmits a single byte passed by the user and receives single byte of data,
2. Transmits and receives the 10 bytes of data,
You can select one pair of the following communication modes, for transmission and reception
respectively:
SPI_POLLING_TX -- For Transmission mode
SPI_POLLING_RX -- For Reception mode
SPI_ITDRV_WITHOUTBUF_TX - For Transmission mode
SPI_ITDRV_WITHOUTBUF_RX -- For Reception mode
/* User can use a time out function to handle the fault in which the control will get stuck in
side a loop.This function should have the Boolean return, i.e it should return TRUE if the
expected wait Time is not elapsed and FALSE if it is elapsed.*/
Software Control
/*============================================================*/
The following variables are declared in main.h file.
main.h:
#ifndef MAIN
#define MAIN
#endif
/*============================================================*/
74/235
Function Descriptions
/* Program Start */
void main(void);
void main(void)
{
unsigned char NbOfBytes_get = 10;
unsigned char NbOfBytes_put = 10;
unsigned char Rx_Data;
unsigned char Buff_Test[buf_size];
unsigned char Buff[buf_size] ={0x00,0x55,0xAA,0xFF,0x00,0x55,0xAA,0xFF,0x00,0x55};
SPI_TxErrCode_t Temp1= 0x00 ;
SPI_RxErrCode_t Temp2= 0x00 ;
/*=========================================================================
Transmission through ‘Polling’ mode
=========================================================================*/
#ifdef SPI_POLLING_TX
/* Single byte data transmission */
SPI_PutByte (My_Data) ;
Temp1 = SPI_IsTransmitCompleted();
while ((!(User_Timeout_Function())) && (Temp1 != SPI_TRANSMIT_OK) )
{
Temp1 = SPI_IsTransmitCompleted() ;
}
if (!(User_Timeout_Function()))
{
switch (Temp1)
{
case (SPI_TX_MODF + SPI_TX_WCOL):
case (SPI_TX_MODF):
case (SPI_TX_WCOL):
User_Function(); /* Error Management */
break;
default: /* If none of the above condition is met */
User_Function();
break;
}
}
/* Transmission of 10 data bytes from user buffer */
switch(SPI_PutBuffer(Buff, (unsigned char)10))
{
case (SPI_TX_MODF + SPI_TX_WCOL):
75/235
Function Descriptions
case (SPI_TX_MODF):
case (SPI_TX_WCOL):
User_Function(); /* Error Management */
break;
case SPI_TRANSMIT_OK: /*Transmission is successful */
break;
default: /* If none of the above condition is met */
User_Function();
break;
}
#endif
/*=========================================================================
Reception through ‘Polling’ mode
=========================================================================*/
#ifdef SPI_POLLING_RX
/* Single byte data reception */
Temp2 = SPI_IsReceptionCompleted() ;
while ((User_Timeout_Function()) && (Temp2 == SPI_RX_DATA_EMPTY))
{ /* Waits for data byte reception */
Temp2 = SPI_IsReceptionCompleted() ;
}
/* Waiting for data byte reception */
if (User_Timeout_Function())
{
switch (Temp2)
{
case (SPI_RX_MODF + SPI_RX_OVR):
case (SPI_RX_MODF):
case (SPI_RX_OVR):
User_Function(); /* Error Management */
Rx_Data = SPI_GetByte () ; /* Corrupted data byte received */
break;
case SPI_RECEIVE_OK: /* Reception successful */
Rx_Data = SPI_GetByte () ;
break;
default: /* If none of the above condition is met */
User_Function();
break;
}
}
else
{
while (1) ;
/* Handle time out as Transmitter/Receiver is having some problem */
}
/* Reception of set of data */
switch ((SPI_GetBuffer(Buff_Test,(unsigned char)10)))
{
case (SPI_RX_MODF + SPI_RX_OVR):
case (SPI_RX_MODF):
case (SPI_RX_OVR):
User_Function(); /* Error Management */
break;
case SPI_RECEIVE_OK: /* Reception is successful */
break;
76/235
Function Descriptions
Temp1 = SPI_IsTransmitCompleted() ;
while((Temp1 == SPI_TX_BUFFER_ONGOING) && (Temp1 == SPI_TX_STRING_ONGOING))
/* Wait for transmission completion */
{
Temp1 = SPI_IsTransmitCompleted() ;
}
switch (Temp1)
{
case (SPI_TX_MODF + SPI_TX_WCOL):
case (SPI_TX_MODF):
case (SPI_TX_WCOL):
User_Function(); /* Error Management */
break;
case SPI_TRANSMIT_OK: /* Transmission is successful */
break;
default: /* If none of the above condition is met */
User_Function();
break;
}
/* Transmission of 10 data bytes from user buffer */
/* User pointer is copied to the global pointer */
SPI_PutBuffer(Buff, NbOfBytes_put);
Temp1 = SPI_IsTransmitCompleted() ;
while ((User_Timeout_Function())&&(Temp1 == SPI_TX_BUFFER_ONGOING))
{
Temp1 = SPI_IsTransmitCompleted() ;
} /* To be sure that the communication by this point has been completed */
if (User_Timeout_Function())
{
switch (Temp1)
{
case (SPI_TX_MODF + SPI_TX_WCOL):
case (SPI_TX_MODF):
case (SPI_TX_WCOL):
77/235
Function Descriptions
78/235
Function Descriptions
/* Here, user can perform other tasks or operations except reception till
the time re-ception is complete, after which user can perform reception
again */
Temp2 = SPI_IsReceptionCompleted();
while ((User_Timeout_Function()) && (Temp2 == SPI_RX_BUFFER_ONGOING))
{
Temp2 = SPI_IsReceptionCompleted();
} /* To be sure that the communication by this point has been completed */
if (User_Timeout_Function())
{
switch (Temp2)
{
case (SPI_RX_MODF + SPI_RX_OVR):
case (SPI_RX_MODF):
case (SPI_RX_OVR):
User_Function(); /* Error Management */
break;
case SPI_RECEIVE_OK: /* Reception successful */
break;
default: /* If none of the above condition is met */
User_Function();
break;
}
}
else
{
while (1) ;
/* Time-Out elapsed without reception completion. Error in
communication and user should handle the case */
}
#endif
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SPI_User_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Control comes into this routine when an interrupt is generated.
User can use the SPI interrupt service routine function or he
can write his own code inside this routine at his own risk. The
data transfer syncronisation may be affected if user includes
his own code along with SPI ISR function.
COMMENTS : None
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
#endif
79/235
Function Descriptions
#endif
BOOL User_Timeout_Function(void)
{
while(Timeoutcount < 50000)
{
Timeoutcount++ ;
return (TRUE); /* Time-out not elapsed */
}
return (FALSE) ; /* Time-out elapsed */
}
void User_Function(void)
{
SPI_Clear_Flags (); /* Clears error and status flags */
}
80/235
Function Descriptions
Note: If you select I2C Interrupt driven communication mode, interrupts must be enabled dur-
ing initialization.
81/235
Function Descriptions
82/235
Function Descriptions
Note: I2C speed is strongly dependant on the R and C wired on the lines, Fcpu and Vdd values,
and not solely on the value programmed in I2CCCR register. So you must take account of
the R and C values. You must not pass the speed value less than the minimum speed limit.
(For ex., minimum speed limit for Fcpu = 8MHz is 16 kHz).
83/235
Function Descriptions
Notes:
– A start condition is not generated unless I2C_Init or I2C_Generate_Stop is called before this
function.
– When start condition is generated for ST72F65/ST72F521/ ST72F63B/ ST72325 devices,
the I2C switches over from Idle/Slave modes to Master mode.
84/235
Function Descriptions
85/235
Function Descriptions
Notes:
– The above function is for I2C Polling or I2C Interrupt driven mode.
– It is recommended to add a timeout protection when using this function.
86/235
Function Descriptions
87/235
Function Descriptions
88/235
Function Descriptions
Note: The above function is only for I2C Interrupt driven mode.
Caution:
– Take care not to access the string until transmission is complete.
– The data transmission will stop if any error occurs during transmission.
89/235
Function Descriptions
Note: The above function is only for I2C Interrupt driven mode.
Caution:
– Take care not to access the string until transmission is complete.
– The data transmission will stop if any error occurs during transmission.
90/235
Function Descriptions
91/235
Function Descriptions
Notes:
– The above function is for I2C Polling or I2C Interrupt driven mode.
– It is recommended to use a timeout protection when using this function.
– To terminate communication after receiving one byte using I2C_GetByte, you have to man-
age ACK bit and STOP generation as shown in the introduction.
92/235
Function Descriptions
93/235
Function Descriptions
Notes:
– The above function is only for I2C Interrupt driven mode.
– ACK bit is managed automatically inside this routine.
– STOP bit is set automatically inside this routine, before the last byte is read. So there is no
need to call I2C_Generate_Stop after this.
Caution:
– Take care not to access the string until reception completion.
– Any data received before calling this function is ignored.
– The data reception will stop if any error occurs during reception.
– In String reception, the NULL character must be taken into account.
94/235
Function Descriptions
95/235
Function Descriptions
Note: You must use this function only inside the Interrupt service routine.
Caution: Special care must be taken, when you write your own code along with this function
inside the interrupt service routine. As all I2C interrupt driven functions rely on this function,
you are advised to call only this function in Interrupt service routine. Otherwise, data transfer
synchronisation will be affected, which may lead to data loss or errors.
Note: You can call this function whenever the error flags are required to be cleared forcibly.
Caution: Do not call this function if a reception request is ongoing as it will corrupt the recep-
tion status by clearing all the flags and you will not receive any error status.
96/235
Function Descriptions
Note: When the peripheral is disabled, all I2C register bits except the stop bit and speed se-
lection bits are cleared.
97/235
Function Descriptions
Note: In master mode, you must call this function to end data transfer.
Caution: In order to generate the non-acknowledge pulse after the last received data byte, the
ACK bit must be cleared just before reading the second last byte. You must call
I2C_GetBuffer before calling the I2C_Generate_Stop function or you have to manage non-
acknowledge pulse.
Note: This function is used only as a recovery measure. If the I2C slaves are powered sepa-
rately or the MCU was reset during an I2C transmission, the I2C bus can be in an unknown
state. In this case, this function can be used to re-synchronise the I2C. This routine must be
called only one time after the MCU was reset.
Caution: The port register values of SDA and SCL pins changes if user calls this function. So,
you must take the port register values into account.
98/235
Function Descriptions
EXAMPLE:
The following C program shows the uses of the I2C functions.
Program Description:
This program runs the following sequence for multimaster I2C (ST72F521 device) for I2C
Polling and I2C Interrupt driven communication modes:
1. Transmits a single byte passed by the user and receives single byte of data in 7 bit ad-
dressing mode,
2. Transmits and receives the10 bytes of data in 7 bit addressing mode.
You can select one pair of the following communication modes, for transmission
and reception respectively:
I2C_POLLING_TX -- For Transmission mode
I2C_POLLING_RX -- For Reception mode
I2C_ITDRV_WITHOUTBUF_TX -- For Transmission mode
I2C_ITDRV_WITHOUTBUF_RX -- For Reception mode
/* You can use a timeout function to handle the fault in which the control will get
stuck inside a loop.This function should have the Boolean return, i.e it should return
TRUE if the expected wait Time is not elapsed and FALSE if it is elapsed.*/
Software Control
/*============================================================*/
The following variables are declared in main.h file.
main.h:
#ifndef MAIN
#define MAIN
void main(void);
void I2C_User_IT_Routine (void);
99/235
Function Descriptions
#endif
/*============================================================*/
/* Program Start */
#include "ST7lib_config.h" /* List of all ST7 devices and communication mode */
#include "main.h"
/* Declaration of prototypes of user defined functions used in main.c */
void main(void)
{
unsigned char Rx_Data;
unsigned char Buff_Test[size_buff];
unsigned char Buff[size_buff] = {0, 1, 2, 5, 50, 10, 100, 200, 225, 255};
I2C_TxErrCode_t Temp1 ;
I2C_RxErrCode_t Temp2 ;
Temp1 = Temp2 = 0x00 ;
I2C_Init ((unsigned char)I2C_ENABLE_ACK | (unsigned char)I2C_IT_ENABLE);
/* Enable acknowledge and interrupts */
100/235
Function Descriptions
101/235
Function Descriptions
break;
default:
break;
}
Timeoutcount = 0 ;
Temp2 = I2C_IsReceptionCompleted() ;
while ((User_Timeout_Function()) && (Temp2 == I2C_RX_DATA_EMPTY))
{
Temp2 = I2C_IsReceptionCompleted() ;
}
/* Waits for data byte reception */
if (User_Timeout_Function())
{
switch (Temp2)
{
case I2C_RX_ARLO:
case I2C_RX_BERR:
Rx_Data = I2C_GetByte(); /* Corrupted data byte received */
User_Function(); /* Error Management */
break;
case I2C_DATA_RX_OK: /* Reception successful */
Rx_Data = I2C_GetByte();
break;
default: /* None of the above condition is true */
User_Function ();
break;
}
}
else
{
/* Handle time out as Transmitter/Receiver is having some problem */
}
/* Reception of a set of data */
switch(I2C_GetBuffer (Buff_Test,(unsigned char)10))
{
case I2C_RX_ARLO:
case I2C_RX_BERR:
User_Function(); /* Error Management */
break;
case I2C_DATA_RX_OK:
break;
default: /* None of the above condition is met */
User_Function ();
break;
}
#endif
/*==========================================================
Transmission through ‘Interrupt driven without buffer’ mode
==========================================================*/
/* Communication mode defined as ITDRV_WITHOUTBUF_TX in
ST7lib_config.h */
/* Single byte transmission */
#ifdef I2C_ITDRV_WITHOUTBUF_TX
if ((I2C_IsTransmitCompleted()) == I2C_DATA_TX_OK)
102/235
Function Descriptions
103/235
Function Descriptions
/*==========================================================
Reception through ‘Interrupt driven without buffer’ mode
==========================================================*/
/* Communication mode defined as ITDRV_WITHOUTBUF_RX in
ST7lib_config.h */
/* Single byte reception */
#ifdef I2C_ITDRV_WITHOUTBUF_RX
I2C_Generate_Start ();
while(!((I2C_IsTransmitCompleted())== I2C_START_OK));
I2C_Load_Address (Addr_Byte_Tx, I2C_RX_MODE);
Timeoutcount = 0 ;
Temp1 = I2C_IsTransmitCompleted() ;
while ((User_Timeout_Function()) && ( Temp1 != I2C_ADD_TX_OK))
{
Temp1 = I2C_IsTransmitCompleted() ;
}
switch (Temp1) /* To check address transmission status */
{
case I2C_TX_AF:
User_Function();
break;
default:
break;
}
Timeoutcount = 0 ;
Temp2 = I2C_IsReceptionCompleted() ;
while ((User_Timeout_Function()) && ((Temp2) == I2C_RX_DATA_EMPTY) )
{
Temp2 = I2C_IsReceptionCompleted() ;
}
/* Waits for data byte reception */
if (User_Timeout_Function())
{
switch (Temp2)
{
case I2C_RX_ARLO:
case I2C_RX_BERR:
Rx_Data =I2C_GetByte ();
User_Function();
break;
case I2C_DATA_RX_OK:
104/235
Function Descriptions
/* Here, user can perform other tasks or operations except reception till
the time reception is complete, after which user can perform reception
again */
Timeoutcount = 0 ;
Temp2 = I2C_IsReceptionCompleted() ;
while ((User_Timeout_Function()) && ((Temp2) == I2C_RX_BUFFER_ONGOING))
{
Temp2 = I2C_IsReceptionCompleted() ;
}
/* To be sure that the communication by this point has been completed */
if (User_Timeout_Function())
{
switch (Temp1)
{
case I2C_RX_BERR:
case I2C_RX_BUFFER_ONGOING:
User_Function(); /* Error Management */
break;
case I2C_DATA_RX_OK: /* Reception successful */
break;
default: /* If none of the above condition is true */
break;
}
}
#endif
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : I2C_User_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Control comes into this routine when an interrupt is generated.
User can use the I2C interrupt service routine function or he
can write his own code inside this routine at his own risk. The
data transfer syncronisation may be affected if user includes
his own code along with I2C ISR function.
COMMENTS : None
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
105/235
Function Descriptions
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
/*-----------------------------------------------------------------------------
USER FUNCTIONS
-----------------------------------------------------------------------------*/
BOOL User_Timeout_Function(void)
{
while(Timeoutcount < 50000)
{
Timeoutcount++ ;
return (TRUE); /* Time-out not elapsed */
}
return (FALSE) ; /* Time-out elapsed */
}
void User_Function(void)
{
I2C_Error_Clear () ; /* Clears error and status flags */
}
106/235
Function Descriptions
START
Initialize I2C
1 Transmitter routine
TRA with or without
buffer
0
Stop
Condition
Receiver routine
with or without
buffer
End
Stop Condition
End
Note: This is a general flow part. Error management is not shown here for the purpose of sim-
plicity.
107/235
Function Descriptions
Notes:
– For ST72F63B device there is only one address register I2COAR (It does not support 10 bit
addressing). Here I2COAR will get the value of I2Cs_OAR1Value and the value of
I2Cs_OAR2Value will be neglected.
– If single master I2C device is selected, I2C will remain idle as in this case I2C can not behave
as slave.
– When slave is in the interrupt mode. That is,
if #ifdef I2C_ITDRV_WITHOUTBUF_TX Or,
# ifdef I2C_ITDRV_WITHOUTBUF_RX are defined then ITE bit is also set in control register
automatically. There is no separate parameter for enabling the ITE bit.
Caution:
– If ITE bit is forcibly modified in the User routine using the hardware register then the behav-
iour of the I2C slave library is unpredictable.
– If you are using the I2C as both transmitter and receiver, then both should be configured in
the same mode (Polling/ Interrupt driven).
108/235
Function Descriptions
Notes:
1. In I2Cs_DEFAULT mode, you should use I2Cs_IsReceptionCompleted to detect the start
condition. As by default the slave is considered as a receiver.
2. You can directly call this function to get the mode of communication after getting Address
matched condition irrespective of whether it is 7-bit or 10-bit address detection.
3. If you are calling this function in a loop for detecting the mode of communication then control
will not come out of this loop till the first BTF condition occurs. So if BTF is never set then the
program will lose control in the above loop. In this case you can use note (1) to tackle this
problem to detect the error condition inside the loop.
109/235
Function Descriptions
110/235
Function Descriptions
111/235
Function Descriptions
Byte Specific:
Output Parameters
I2Cs_TX_DATA_OK
Transmission of current byte has taken place successfully
I2Cs_STOP_DETECTED
If the stop condition is detected
Buffer Specific:
I2Cs_BUFF_TX_ONGOING
If the transmission of the buffer is ongoing successfully
I2Cs_TX_DATA_OK
Transmission has taken place successfully and Stop con-
dition is detected
I2Cs_OVERFLOW_TX
Transmission has taken place successfully and stop is de-
tected correctly. But overflow condition has occurred
Check through I2Cs_GetCommMode If I2Cs_TX_MODE
Required Preconditions
is selected or not
Functions called None
Postconditions None
See also I2Cs_IsReceptionCompleted
Notes:
– The above function is for both polling and Interrupt driven mode. But in I2C_POLLING_TX
mode, this function is used in conjunction with I2Cs_PutByte only.
112/235
Function Descriptions
– SCL and SDA lines are released in this function in case of Acknowledge failure and commu-
nication proceeds according to the I2C protocol. This function should be called again to de-
tect the next event (START / STOP).
Notes:
– The above function is only for I2C_POLLING_RX mode.
– Control from this function comes only after receiving a STOP or any Error condition.
Caution: You can lose the control if I2C is disabled while using this function.
113/235
Function Descriptions
114/235
Function Descriptions
Byte Specific
Output Parameters
I2Cs_RX_DATA_OK
Reception of the current byte has taken place successfully
I2Cs_STOP_DETECTED
If the stop condition is detected
BufferSpecific
I2Cs_BUFF_RX_ONGOING
If the reception is ongoing successfully
I2Cs_RX_DATA_OK
Reception has taken place successfully and stop is detect-
ed correctly
I2Cs_OVERFLOW_RX
Reception has taken place successfully and stop is detect-
ed correctly. But overflow condition has occurred.
Required Preconditions None
Functions called None
Postconditions None
See also I2Cs_IsTransmitCompleted
Notes:
– The above function is for both Polling and Interrupt driven Mode. But in I2C_POLLING_RX
it is used only with I2Cs_GetByte.
115/235
Function Descriptions
– If this function is called before any reception request is made then it will return I2C_EMPTY
until the first data byte is received, and returns the reception status thereafter. If a reception
request is over, this function will return the error status of that request only once.
Note: You can call this function, whenever the error flags are required to be cleared forcibly.
Caution: Do not call this function if a reception request i0s ongoing as it will corrupt the recep-
tion status by clearing all the flags and you will not receive the error status.
Note: When you disable the peripheral, all registers are also cleared except the Stop bit and
address registers. So to reinitiate the communication, the I2C peripheral needs to be initial-
ized again.
116/235
Function Descriptions
Note: You must use this function only inside the Interrupt service routine.
Caution: Special care must be taken when you write code with this function inside the interrupt
service routine. You are advised to call only this function inside Interrupt service routine. Oth-
er wise, data transfer synchronisation will be affected, which may lead to data loss or errors.
EXAMPLE:
The following C program shows the uses of the I2C functions.
Program Description:
This program runs the following sequence for the I2C slave (ST72F63B device) for I2C
Polling and I2C Interrupt driven communication modes:
1. Transmits a single byte passed by the user and receives single byte of data in 7-bit ad-
dressing mode,
2. Transmits and receives the10 bytes of data in 7-bit addressing mode.
You can select any one pair of the following communication modes.
/* You can use a time out function to handle the fault in which the control will get
stuck inside a loop.This function should have the Boolean return, i.e it should return
TRUE if the expected wait Time is not elapsed and FALSE if it is elapsed.*/
117/235
Function Descriptions
Software Control
void I2Cs_User_IT_Routine(void);
void main(void);
BOOL Time_Out(void) ; /* Prototypes of user function */
void User_Function(void);
unsigned int count=0;
#endif
/*============================================================*/
/* Program Start */
/* This example code explains the usage of I2C slave in the transmitter
and receiver mode.First the slave is configured as a transmitter and it
transmits 10 bytes of data from the user buffer.Then it is configured as a
receiver and it reads the same 10 bytes of data and they are compared.
In case of any mismatch control gets struck in a loop*/
#include "ST7lib_config.h"
#include "main.h"
unsigned char Buff_In[9]= {0x00};
unsigned char Buff_Out[]={1,2,3,4,5,6,7,8,9};
EnableInterrupts
118/235
Function Descriptions
Error_Status=I2Cs_IsReceptionCompleted();
while((!Time_Out()) && (Error_Status != I2Cs_ADDRESS_DETECTED))
/* Time_out() is bring before to remove side-efffect error */
{
Error_Status=I2Cs_IsReceptionCompleted();
}
Comm_Mode=I2Cs_GetCommMode();
while(Comm_Mode == I2Cs_DEFAULT)
{
Comm_Mode=I2Cs_GetCommMode(); /* checking for communication mode */
}
case I2Cs_BERR:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
default:
User_Function();
119/235
Function Descriptions
break;
}
#endif
I2Cs_PutBuffer(Buff_Out,maxsize);
Error_Status=I2Cs_IsTransmitCompleted();
switch( Error_Status)
{
case I2Cs_BUFF_TX_ONGOING:
Error_Status=I2Cs_IsTransmitCompleted();
while((Error_Status != I2Cs_TX_DATA_OK) && (Error_Status !=
I2Cs_OVERFLOW_TX))
{
Error_Status=I2Cs_IsTransmitCompleted();
}
break;
case I2Cs_TX_DATA_OK:
case I2Cs_OVERFLOW_TX:
break;
case I2Cs_TX_AF:
case I2Cs_BERR:
case I2Cs_GENERAL_CALL:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_DEFAULT_STATUS:
User_Function();
break;
default:
User_Function();
break;
}
#endif
/****************END OF INTERRUPT DRIVEN MODE *******************/
}
/***********************************************************/
/******* RECEIVER ROUTINE ******************/
Error_Status=I2Cs_IsReceptionCompleted();
while((!(Time_Out()))&& ((Error_Status != I2Cs_ADDRESS_DETECTED) && (Error_Status
!= I2Cs_GENERAL_CALL)))
{
Error_Status=I2Cs_IsReceptionCompleted();
120/235
Function Descriptions
}
Comm_Mode=I2Cs_GetCommMode();
while((Comm_Mode == I2Cs_DEFAULT))
{
Comm_Mode=I2Cs_GetCommMode();
}
if(Comm_Mode == I2Cs_RX_MODE) /* POLLING AND INTERRUPT DRIVEN RECEIVER */
{
Error_Status=I2Cs_IsReceptionCompleted();
/* ONE BYTE RECEPTION */
121/235
Function Descriptions
#ifdef I2C_ITDRV_WITHOUTBUF_RX
Error_Status=I2Cs_IsReceptionCompleted();
while((!(Time_Out())) && (Error_Status != I2Cs_RX_DATA_OK ))
{
Error_Status=I2Cs_IsReceptionCompleted();
}
switch(Error_Status)
{
case I2Cs_BUFF_RX_ONGOING:
case I2Cs_OVERFLOW_RX:
case I2Cs_BERR:
case I2Cs_EMPTY:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
case I2Cs_RX_DATA_OK:
break;
default:
User_Function();
break;
}
I2Cs_GetBuffer(Buff_In,maxsize);
Error_Status=I2Cs_IsReceptionCompleted();
while((!(Time_Out())) && (Error_Status != I2Cs_RX_DATA_OK ))
{
Error_Status=I2Cs_IsReceptionCompleted();
}
switch(Error_Status)
{
case I2Cs_BUFF_RX_ONGOING:
case I2Cs_OVERFLOW_RX:
case I2Cs_BERR:
case I2Cs_EMPTY:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
case I2Cs_RX_DATA_OK:
break;
default:
User_Function();
break;
}
#endif
122/235
Function Descriptions
/****************************************************************/
/**** COMPARE THE TRANSMITTED AND RECEIVED BYTES ******/
/*------------------------------------------------------------------------------
ROUTINE NAME : User_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Control comes into this routine when an interrupt is generated.
User can use the I2C interrupt service routine function for
slave or he can write his own code inside this routine at his
own risk.The data transfer syncronisation may be affected if
user includes his own code along with I2C ISR function.
COMMENTS : None
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
void User_Function(void)
{
I2Cs_ErrorClear();
/* user can include his code here */
}
123/235
Function Descriptions
124/235
Function Descriptions
125/235
Function Descriptions
Note: If you select Forced compare mode, the input parameter 2 will not affect the output
waveform and hence you can pass any value between 0x0000 to 0xFFFF.
126/235
Function Descriptions
127/235
Function Descriptions
128/235
Function Descriptions
– The flag due to capture1 is set by hardware when counter reaches the output compare 2
register value and can produce the timer interrupt if the input capture interrupt is enabled and
the instruction ‘rim’ is used to clear the ‘I’ bit in CC register.
– By enabling the Forced compare mode or OPM mode while PWM mode is enabled,the po-
larity of the PWM Output waveform may change.
129/235
Function Descriptions
130/235
Function Descriptions
– To perform the input capture only the ICAP2 pin can be used, not the ICAP1 pin. Take care
that the counter is reset each time a valid edge occurs on ICAP1 pin and that the capture 1
flag can also generate interrupts if the input capture interrupt is enabled and the ‘rim’ instruc-
tion has been used to clear the I-bit.
– When OPM is used, the input capture1 register is dedicated to this mode. Similarly output
compare 2 cannot be used as level OLVL2 is dedicated to OPM.
– By enabling the Forced compare mode while OPM mode is enabled, the polarity of the OPM
Output waveform may change.
131/235
Function Descriptions
132/235
Function Descriptions
133/235
Function Descriptions
134/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the TIMERx functions. Here, x=A as TIMERA is
used. You must define TIMERA in ST7lib_config.h.
Program description:
It compares the output compare2 register value with the free running counter, checks the oc-
currence of (event) leading edge at ICAP2 pin. It generates PWM signal with a frequency of
10KHz and a Duty cycle of 33% on the OCMP1 pin, if _Enable_PWM_ is defined in
ST7lib_confi.h or it generates a 5ms pulse at OCMP1 pin, if _Enable_OPM_ is defined in
ST7lib_config.h (Fcpu = 8 MHz).
/* Program start */
#include "st7lib_config.h"
//prototype declaration
void main(void);
void TIMERA_IT_Routine(void);
void main(void)
{
unsigned int OCR_VALUE = 0x2050;
IO_Input (IO_FLOATING,IO_PORT_F,((unsigned char)IO_PIN_5 | (unsigned char)
IO_PIN_6));
/* ICAP1,ICAP2 pins asloating input */
TIMERA_Init (TIMER_FCPU_8); /* Timer Clock to Fcpu/8 and reset counter */
EnableInterrupts /* Clear I bit in CC reg */
/* Timer compares 0x2050 with free running counter */
TIMERA_OCMP_Mode(TIMER_OCMP_2,OCR_VALUE,TIMER_OUTPUT_R );
while(!(TIMERA_Status_Flag(TIMER_FLAG_OCF2)== TRUE));
TIMERA_Clear_Flag(TIMER_FLAG_OCF2); /* Clear output compare2 flag */
TIMERA_IT_Enable(TIMER_ICAP_IT_ENABLE); /* Enable capture interrupt */
TIMERA_ICAP_Mode(TIMER_ICAP_2,TIMER_EDGE_1); /*Detect rising edge at ICAP2 pin*/
/* ----------------------------------------------------------------------------
ROUTINE NAME : TIMERA_IT_Routine
INPUT : None
OUTPUT : None
135/235
Function Descriptions
136/235
Function Descriptions
137/235
Function Descriptions
138/235
Function Descriptions
Note: When the Forced compare mode is selected, the input parameter 2 will not affect the
output waveform and hence you can pass any value between 0x00 to 0xFC.
139/235
Function Descriptions
140/235
Function Descriptions
Notes:
– Flags for compare1 & compare2 can not be set by hardware in PWM mode, therefore the
output compare interrupt is inhibited.
141/235
Function Descriptions
– The flag due to capture1 is set by hardware when the counter reaches the output compare2
register value and can produce the timer interrupt if the interrupt for input capture is enabled
and the instruction ‘rim’ is used to clear the ‘I’ bit in CC register.
– By enabling the Forced compare mode or OPM mode while PWM mode is enabled,the po-
larity of the PWM Output waveform may change.
142/235
Function Descriptions
143/235
Function Descriptions
– Only the ICAP2 pin can be used to perform input capture, not the ICAP1 pin. Take care that
the counter is reset each time a valid edge occurs on the ICAP1 pin and capture 1 flag can
also generate an interrupt if input capture interrupt is enabled and the instruction ‘rim’ is used
to clear the I bit.
– When OPM is used, input capture1 register is dedicated to this mode. Similarly output
compare2 cannot be used as level OLVL2 is dedicated to OPM.
– By enabling the Forced compare mode while OPM mode is enabled, the polarity of the OPM
Output waveform may change.
Note: The input capture flag gets cleared if this function is called after the
TIMER8_Status_Flag.
144/235
Function Descriptions
145/235
Function Descriptions
146/235
Function Descriptions
147/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the TIMER8 functions.
Program description:
It compares the output compare2 register value with the free running counter, checks the oc-
currence of (event) leading edge at ICAP2 pin. It generates PWM signal with 10KHz frequency
and with 33% Duty cycle at the OCMP1 pin if _Enable_PWM_ is defined in main.c or it gen-
erates the 0.1ms pulse at OCMP1 pin, if _Enable_OPM_ is defined in main.c (Fcpu = 8 MHz).
/* Program start */
//prototype declaration
void main(void);
void TIMER8_IT_Routine(void);
void main(void)
{
unsigned char TIMER8_OCR_VALUE = 0x50;
/* Configuring the Port B pin 0 & 2 as floating for ICAP1 and ICAP2 */
IO_Input(IO_FLOATING,IO_PORT_B,((unsigned char)IO_PIN_0 | (unsigned char)
IO_PIN_2));
TIMER8_Init (TIMER8_FCPU_8); /* Timer8 Clock to Fcpu/8 and reset counter */
EnableInterrupts /* Clear I bit in CC reg */
/* Timer8 compares 0x50 with free running counter */
TIMER8_OCMP_Mode(TIMER8_OCMP_2,TIMER8_OCR_VALUE,TIMER8_OUTPUT_R );
while(!(TIMER8_Status_Flag(TIMER8_FLAG_OCF2)== TRUE));
TIMER8_Clear_Flag(TIMER8_FLAG_OCF2); /* Clear output compare2 flag */
TIMER8_IT_Enable(TIMER8_ICAP_IT_ENABLE); /* Enable capture interrupt */
/*Detect rising edge at ICAP2 pin*/
TIMER8_ICAP_Mode(TIMER8_ICAP_2,TIMER8_EDGE_1);
148/235
Function Descriptions
/* ----------------------------------------------------------------------------
ROUTINE NAME : TIMER8_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Interrupt service routine
COMMENTS : This gets automatically executed when any of the timer8
interrupt is enabled. If the same functions are called in the
main Tree and the interrupt Tree, the function Re-entrant error
occurs in case COSMIC compiler is used with models other than
stack models.For configuring the port pins,I/O library is used.
----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt
#else
#error "Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
void TIMER8_IT_Routine(void)
{
unsigned int CAP2_Value; /* Define local variables */
unsigned char i,Temp;
149/235
Function Descriptions
150/235
Function Descriptions
151/235
Function Descriptions
152/235
Function Descriptions
153/235
Function Descriptions
1. tWDG = 2ms @ 8 MHz fosc, therefore You have to use this option at regular intervals to pre-
vent a watchdog reset occurring.
Note: The function can be used for ST7FLite0, ST7SUPERLITE and ST7FDALI.
154/235
Function Descriptions
155/235
Function Descriptions
EXAMPLE:
The following C program example shows the use of the LT functions for the ST7FLite0 device.
Program description:
This program detects an event (rising edge & trailing edge) at the LTIC pin and toggles a LED
every 5 seconds.
/* Program start */
//prototype declaration
void LT_ICAP_IT_Routine(void);
void LT_TB_IT_Routine(void);
156/235
Function Descriptions
void main(void);
/******************************************************************************
Use of Input capture Interrupt service routine
- User has to write this function and map the interrupt vector in .prm file in
case of HIWARE or in vector_xxx.c in case of COSMIC.
- An example of LED toggles at port PB1 is given, which will be executed when
capture occurs.
- This gets automatically executed if the ICAP interrupt of the LT is enabled.
If the same functions are called in the main Tree and the interrupt Tree, the
function Reentrant error occurs in case COSMIC compiler is used with models
other than stack models.
Functions description
- The lt_hr.h is to be included when the actual hardware register are read to
clear the flags.For configuring the port pins, I/O library is used.
******************************************************************************/
157/235
Function Descriptions
}
IO_Write (IO_PORT_B,IO_PIN_1,IO_DATA_LOW); /* Turn OFF LED at PB1 */
}
/******************************************************************************
Use of TimebaseInterrupt service routine
- User has to write this function and map the interrupt vector in .prm file in
case of HIWARE or in vector_xxx.c in case of COSMIC.
- An example of LED toggles after every 5 seconds is given.This routine is
executed when overflow occurs (TBF=1).
- This gets automatically executed when TBF interrupt of the LT is enabled. If
the same functions are called in the main Tree and the interrupt Tree, the
function Reentrant error occurs in case COSMIC compiler is used with models
other than stack models.
Functions description
- The lt_hr.h is to be included when the actual hardware register are read to
clear the flags.For configuring the port pins, I/O library is used.
******************************************************************************/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt @nostack
#else
#error "Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
void LT_TB_IT_Routine(void)
{
unsigned char Temp;
/* i = LTCSR; Clear ICF */
LT_Clear_Flag(LT_FLAG_TBF); /* Call only to clear TBF */
/* Routine up to the user */
count++;
if(count == 5000)
{
Temp = IO_Read (IO_PORT_B ); /* TO Toggle PB3 */
if (Temp & 0x08)
{
IO_Write (IO_PORT_B,IO_PIN_3,IO_DATA_LOW); /* Turn OFF LED at PB3 */
}
else
{
IO_Write (IO_PORT_B,IO_PIN_3,IO_DATA_HIGH); /* Turn ON LED at PB3 */
}
count = 0;
}
}
158/235
Function Descriptions
9.1.9 PWMART
This software library for PWMART consists of the following functions:
Function Name PWMART_Init
Void PWMART_Init(Typ_Pwmart_InitParameter InitVal-
Function Prototype
ue)
Initialization of the PWMART, by default sets counter clock
as Fcpu, counter stopped and interrupt disabled. You can
Behaviour Description
select external clock. One or more input parameters can
be passed by logically ORing them together.
PWMART_DEFAULT
Reset value (00h)
PWMART_EXCLK
Input Parameter
Enable the external clock source
PWMART_OVF_IT_ENABLE
Enable counter overflow interrupt
Output Parameters None
Required Preconditions None
Functions called None
Postconditions PWMART is configured as desired
See also PWMART_Counter_Enable
159/235
Function Descriptions
160/235
Function Descriptions
161/235
Function Descriptions
Notes: Here are some time base ranges corresponding to the various counter clock frequen-
cies (fcounter):
fcounter=8MHz gives a time base range of [125ns to 31.875µs],
from 1 step to 255 steps (ARR=254 to ARR=0).
counter=4MHz gives a time base range of [250ns to 63.75µs]
fcounter=2MHz gives a time base range of [500ns to 127.5µs]
fcounter=1MHz gives a time base range of [1µs to 255µs]
fcounter=500kHz gives a time base range of [2µs to 510µs]
fcounter=250kHz gives a time base range of [4µs to 1.02ms]
162/235
Function Descriptions
Note: The table given below shows data to be loaded in ARR register for different PWM signal
163/235
Function Descriptions
164/235
Function Descriptions
165/235
Function Descriptions
166/235
Function Descriptions
167/235
Function Descriptions
168/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the PWMART functions.
Program description:
This program detects the event (rising edge) at ARTIC1 pin and generates the PWM signal of
frequency 50KHz with duty cycle 33% on the PWM1 pin for ST72F521 device with a 4MHz ex-
ternal clock. The output compare signal is obtained on the PWM2 pin.
/* Program start */
//prototype declaration
void PWMART_IT_Routine(void);
void main(void);
void main(void)
{
unsigned char Counter_Data = 0xB0;
unsigned char Compare_Data = 0xCA;
unsigned char DutyCycle_Data = 0xCA;
/* ----------------------------------------------------------------------------
ROUTINE NAME : PWMART_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Interrupt service routine
COMMENTS : This gets automatically executed when any of the PWMART
interrupt is enabled. If the same functions are called in the
169/235
Function Descriptions
main Tree and the interrupt Tree, the function Re-entrant error
occurs in case COSMIC compiler is used with models other than
stack models.
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* additional registers will be saved */
#else
#ifdef _COSMIC_ /* test for Cosmic Compiler */
@interrupt
#else
#error "Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
void PWMART_IT_Routine(void)
{
unsigned char CAP1_Value;
unsigned char i,Temp;
if(PWMART_Status_Flag(PWMART_FLAG_OVF) == TRUE)
{
PWMART_Clear_Flag(PWMART_FLAG_OVF); /* call only to clear OVF */
}
170/235
Function Descriptions
171/235
Function Descriptions
1) Feature available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI devices.
2) Feature available only on ST7FLite3 device.
Notes: If PWM output is disabled, the Output compare mode is enabled as the OE bit is
cleared.
172/235
Function Descriptions
Notes:
– This function can be used only with ST7FLite0 devices.
– This function is used to keep backward compatibility with previous library. For new develop-
ment LART_ConfigurePWM function should be used with the first parameter LART_PWM0
in place of this function.
173/235
Function Descriptions
174/235
Function Descriptions
Notes:
– This function can be used only with the ST7FLite0 device.
– This function is added for backward compatibility with previous library. For new develop-
ments LART_ConfigurePWM should be used with LART_OCMP0 as the first parameter.
175/235
Function Descriptions
1) Feature available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI devices
Note: Take care of the ATR value while using this function.
176/235
Function Descriptions
1) Feature available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI devices.
2) Feature available only on ST7FLite3 device.
Note: All flags except LART_FLAG_ICF also get cleared by calling this function so need to
call LART_Clear_Flag function again to clear the flag.
177/235
Function Descriptions
1) Feature available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI devices.
2) Feature available only on ST7FLite3 device.
178/235
Function Descriptions
Note: This function is available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI de-
vices.
Note: This function is available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI de-
vices.
179/235
Function Descriptions
Note: This function is available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI de-
vices.
180/235
Function Descriptions
Note: This function is available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI de-
vices.
Note: This function is available only for ST7FLite1, ST7FLite2, ST7FLite3 and ST7DALI de-
vices.
181/235
Function Descriptions
182/235
Function Descriptions
Notes:
– This function is available only for the ST7FLite3 device.
– This function may affect other functions as it changes the clock source which will also affect
the LiteTimer Input capture functionality because this is used along with LART Input Capture
for this mode.
183/235
Function Descriptions
184/235
Function Descriptions
Program description:
This program generates a PWM signal with a 10KHz frequency and with a 30% Duty cycle and
toggles an LED every second (Fcpu=8MHz ).
/* Program start */
/* prototype declaration */
void LART_OVF_IT_Routine(void);
void main(void);
void main(void)
{
while(1);
}
/* ----------------------------------------------------------------------------
ROUTINE NAME : LART_OVF_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Interrupt service routine for Overflow interrupt
COMMENTS : This gets automatically executed when OVF interrupt of the
LART is enabled. If the same functions are called in the
main Tree and the interrupt Tree, the function Re-entrant error
occurs in case COSMIC compiler is used with models other than
stack models.
For configuring the port pins,I/O library is used.
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* Test for Metrowerks Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt @nostack
#else
#error "Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
185/235
Function Descriptions
void LART_OVF_IT_Routine(void)
{
unsigned char Temp;
186/235
Function Descriptions
9.1.11 TBU
This software library consists of the following functions for TBU.
Function Name TBU_Init
Function Prototype Void TBU_Init (TBU_Init_Param init_value)
Initialization of the TBU, sets by default TBU counter fro-
zen, cascading disabled, interrupt disabled and prescaling
Behaviour Description
factor to 2. You can change the default configuration by
selecting one or more of the following input parameters.
TBU_DEFAULT
Sets TBU to reset value
TBU_ART_CASCADE
Input Parameters Cascade the TBU and the PWMART timer counters to-
gether
TBU_IT_ENABLE
TBU overflow interrupt enable
Output Parameters None
Required Preconditions None
Functions called None
Postconditions None
See also None
187/235
Function Descriptions
Notes:
– The actual delay obtained using this function will be approximately equal to the calculated
delay.
– For best accuracy and smaller delays, it is recommended to use the TBU_SetPrescCount8
function.
188/235
Function Descriptions
189/235
Function Descriptions
Example
The following C program shows the use of the TBU functions.
Program description:
This program is for an ST2F62 device. It generates an interrupt after 1ms if the
TBU_Standalone label is selected in ST7lib_config.h file or generates an interrupt after 1s if
TBU_Cascade label is selected in ST7lib_config.h file. Also an interrupt subroutine is written
which clears the interrupt flag.
.
/************************************ Program Start ******************************/
190/235
Function Descriptions
void TBU_IT_Routine(void);
void main(void);
/*------------------------------------------------------------------------------
For Cascade Mode
------------------------------------------------------------------------------*/
#else
#ifdef TBU_Cascade /*Selects Cascade mode*/
TBU_Init (TBU_IT_ENABLE+TBU_ART_CASCADE);
/*Enable interrupt and select Cascade mode*/
TBU_SetPrescCount16(TBU_CasPrescvalue_128,Counter_Value16);
/*Generate an interrupt after 1 second */
#endif
#endif
TBU_Enable(); /*Enable the TBU counter*/
while(1);
}
/***********************************Interrupt Subroutine************************/
void TBU_IT_Routine(void)
{
TBU_ClearOverflow(); /*Clears the Overflow flag*/
}
191/235
Function Descriptions
9.1.12 WDG
Table 140. WDG_Refresh
Function Name WDG_Refresh
Function Prototype Void WDG_Refresh (unsigned char Counter_Data)
Loads the value of Watchdog counter and activates the
Behaviour Description
Watchdog.
Counter_Data
Value which is loaded in the Watchdog counter. This value
must be between 0x40 and 0x7f to avoid an immediate re-
Input Parameter
set.
To get an immediate reset this value can be between 0x00
to 0x3f.
Output Parameter None
You must precalculate the value of Counter_Data for the
Required Preconditions
desired Watchdog timeout.
Functions called None
1. The Watchdog is activated after this function and can
not be disabled, except by a reset.
2. A Reset is generated after the desired Timeout, when
Watchdog counter rolls over from 0x40 to 0x3f (See Table
Postconditions 141 for some Counter_Data values and corresponding
Watchdog Timeout).
3. For window watchdog, a reset is also generated if this
routine is called when the Watchdog counter value is
greater than the Window register value 1).
192/235
Function Descriptions
Table 141. Watchdog Timeout for some Counter_Data values at fosc2 = 8MHz for
ST72F521 device
Counter_Data Watchdog Timeout (ms)
0x3f to 0x00 0.0
0x40 1.5
0x50 34
0x60 65
0x70 98
0x7f 128
193/235
Function Descriptions
Caution: This function is valid only for Window Watchdog, if used for normal Watchdog a
compilation error is generated.
Caution: This function is valid only for Window Watchdog, if used for normal Watchdog a com-
pilation error is generated.
194/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the WDG library functions.
Program description:
This program generates the watchdog reset for ST72F521 device. The reset timeout period is
configured as 34ms with fosc2 = 8MHz, through function WDG_Refresh with input parameter
“Counter_Data” as 0x50 (See Table 141).
In Window Watchdog, the reset can be generated by reloading the WDG counter register out-
side the Window. In the example it is generated after 1ms
void main(void);
void main (void)
{
unsigned int i;
/*-----------------------------------------------------------------------------
WDG_Refresh function to generate a reset after 34ms at fosc2 = 8MHz
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
WDG_ComputeTimeout function to generate a reset after 20ms (MCC timebase:
4ms, fosc2 = 8MHz)
-----------------------------------------------------------------------------*/
/*WDG_ComputeTimeout (50000); */ /*Generates reset after 20ms*/
/*----------------------------------------------------------------------------
Window Watchdog for setting a Refresh Period of 34ms and a Window Size of
18.432ms at fosc2 = 8MHz. Reset is generated after 1ms by reloading the WDG
counter register outside the window.
-----------------------------------------------------------------------------*/
#ifdef WDG_72F561
WDG_Refresh(0x50) ; /* Set a Refresh period of 34ms */
WDG_WriteWindow(0x48)
for (i = 0; i < 532; i++) ; /*1ms delay */
WDG_Refresh(0x50); /* Generates Reset on execution of this routine */
#endif
while (1);
195/235
Function Descriptions
9.1.13 ITC
Table 146. ITC_Init
Function Name ITC_Init
Function Prototype Void ITC_Init (void)
Initializes all the Interrupt software priority registers and
Behaviour Description External interrupt controller/status register to their default
values.
Input Parameters None
Output Parameters None
Required Preconditions None
Functions called None
Postconditions None
See also None
196/235
Function Descriptions
IT_AVD
Sets the software priority of Auxiliary Voltage detector in-
terrupt
IT_I2C 2)
Sets the software priority of I2C peripheral interrupt
IT_PWMART
Sets the software priority of PWMART peripheral interrupt
IT_DTC 2)
Sets the software priority of DTC peripheral interrupt
IT_PLG
Sets the software priority of Power Management USB
plug/unplug interrupt
Input Parameter 1 IT_ESUSP
Sets the software priority of USB Endsuspend interrupt
IT_USB 2)
Sets the software priority of USB peripheral interrupt
IT_TIM
Sets the software priority of TIMER interrupt
IT_ADC
Sets the software priority of A/D End of Conversion Inter-
rupt
IT_CSS
Sets the software priority of Clock Filter Interrupt
IT_SCI1,IT_SCI2
Sets the software priority of SCI interrupts
IT_LEVEL_1
Sets the priority of the selected interrupt as Level1
IT_LEVEL_2
Input Parameter 2 1)
Sets the priority of the selected interrupt as Level2
IT_LEVEL_3
Sets the priority of the selected interrupt as Level3
Output Parameter None
Required Preconditions None
Functions called None
Postconditions None
See also None
1) IT_LEVEL_0 can not be written.
2) These Interrupts do not have an Exit from HALT mode capability.
Notes:
– This function is for ST72F521,ST72F561,ST72F65,ST72F62,ST72F264, ST72325 and
ST7232A devices.
197/235
Function Descriptions
– For selecting different priorities for different interrupts you must call the function more than
once. If it is required to set two or more interrupts to same priority level, then you can pass
them together by logically ORing.
– This function is for Nested Interrupts only.
Caution:
– If you select an interrupt which is not present in the Peripheral then you will get an error mes-
sage during compilation.
– If the Priority of the Interrupt is changed while it is being executed then the following behav-
iour has to be considered:
If that interrupt is still pending, and the new software priority is higher than the previous one,
the interrupt is re-entered. Otherwise the software priority stays unchanged until the next in-
terrupt request.
198/235
Function Descriptions
199/235
Function Descriptions
IT_I2C
Returns the software priority of I2C peripheral interrupt
IT_PWMART
Returns the software priority of PWMART peripheral inter-
rupt
IT_DTC
Returns the software priority of DTC interrupt
IT_PLG
Returns the software priority of Power Management USB
plug/unplug interrupt
IT_ESUSP
Input Parameter 1 Returns the software priority of USB Endsuspend interrupt
IT_USB
Returns the software priority of USB peripheral interrupt
IT_TIM
Returns the software priority of TIMER interrupt
IT_ADC
Returns the software priority of A/D End of Conversion in-
terrupt
IT_CSS
Returns the software priority of Clock Filter interrupt
IT_SCI1,IT_SCI2
Returns the software priority of SCI interrupts
IT_LEVEL_0
Priority of the selected interrupt is at Level0
IT_LEVEL_1
Priority of the selected interrupt is at Level1
Output Parameter
IT_LEVEL_2
Priority of the selected interrupt is at Level2
IT_LEVEL_3
Priority of the selected interrupt is at Level3
Required Preconditions None
Functions called None
Postconditions None
See also None
Notes:
– This function is for ST72F521,ST72F561,ST72F65,ST72F62,ST72F264, ST72325 and
ST7232A devices.
– This function is for Nested Interrupts only.
Caution: If you select an interrupt which is not present in the Peripheral then he will get an
error message during compilation.
200/235
Function Descriptions
Note: This is a Non Maskable Software Interrupt and can interrupt a Level3 program.
201/235
Function Descriptions
202/235
Function Descriptions
– For devices which have pins with fixed sensitivity the option IT_DEFAULT has to be used.
Any other option used also has no effect and the default value is only configured.
– For ST72F264, PortC can be configured as EI0 or EI1 using option bytes. For this the con-
stant EXTIT_VALUE in the device_hr.h file has to be changed as 0 or 1 to configure PortC
as EI0 or EI1 respectively.
203/235
Function Descriptions
Notes:
– This function is for ST72F521, ST72F561 and ST72325 devices.
– This is a Non Maskable Interrupt source and can interrupt a Level3 program.
Notes:
– This function is for ST72F521, ST72F561 and ST72325 devices.
– A parasitic interrupt can be generated when disabling the TLI, depending upon the status of
the TLI pin.
204/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the ITC library functions.
Program description:
This program is written for the ST72F521 device. It sets the Software priority level for EI0 Ex-
ternal interrupt & TIMERA peripheral interrupt to Level2. Then it sets the software priority of
EI2 to Level1. The sensitivity of PA0 (EI0 interrupt pin in ST72F521) and PF0 (EI2) interrupt is
set to falling edge. The TLI interrupt is enabled and the sensitivity of the TLI pin is set to Falling
edge. A falling edge is applied on the PF0 pin (configured as input interrupt) to generate an
EI2 interrupt. Immediately after this, a falling edge is applied on the PA0 pin. As the priority of
the EI0 interrupt is higher than EI2, the EI2 interrupt is interrupted and control goes to EI0.
Then TLI interrupt is generated by applying a falling edge on the TLI pin. The LEDs connected
to the Port D pins are toggled by the interrupt subroutines.
//prototype declarations
void TLI_IT_Routine (void);
void EI0_IT_Routine(void);
void EI2_IT_Routine(void);
void main(void);
205/235
Function Descriptions
/*******************************************************************************
Interrupt Subroutine for TLI
*******************************************************************************/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error "Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
/*******************************************************************************
Interrupt Subroutine for EI0
*******************************************************************************/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
206/235
Function Descriptions
#endif
#endif
/*******************************************************************************
Interrupt Subroutine for EI2
*******************************************************************************/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
207/235
Function Descriptions
9.1.14 MCC
Function Name MCC_Init
Function Prototype Void MCC_Init (MCC_Init Init_Value)
Initialization of MCC. By default, main clock out (MCO) al-
ternate function, beep mode and oscillator interrupt are
Behaviour Description disabled. You can change the default configuration, by se-
lecting input parameters given below. You can pass one or
more parameters by ‘OR’ing them.
MCC_DEFAULT
Load MCC registers with reset value (00h).
MCO_ENABLE
Input Parameters
Enables main clock out alternate function.
MCC_IT_ENABLE
Enables oscillator interrupt.
Output Parameters None
Required Preconditions None
Functions called None
Postconditions None
See also None
Note: If you want to enable interrupts, parameter MCC_IT_ENABLE has to be passed in the
MCC_Init function. You must then use the “EnableInterrupts” macro to reset the Interrupt
mask.
Caution: The MCO function is stopped during Active-Halt mode.
208/235
Function Descriptions
1) This parameter is applicable for ST72F521, ST72325 and ST7232A devices, when slow
mode is enabled.
2) This parameter is applicable for ST7FLite0 device, when slow mode is enabled.
209/235
Function Descriptions
Notes:
– The MCC/ RTC interrupt wakes up the MCU from ACTIVE-HALT mode, not from HALT
mode.
– A modification of time base is taken into account at the end of the current period (previously
set), to avoid an unwanted time shift.
Notes:
The timebase for other fOSC2 can be reached by calculation, as shown below.
For MCC_RTC_0, Timebase = 32000/(2*fOSC2).
For MCC_RTC_1, Timebase = 64000/(2*fOSC2).
For MCC_RTC_2, Timebase = 160000/(2*fOSC2).
For MCC_RTC_3, Timebase = 400000/(2*fOSC2).
For example, if fOSC2 = 5MHz, for the MCC_RTC_X input values, the timebase values are as
follows.
For MCC_RTC_0, Timebase = 3.2 ms,
For MCC_RTC_1, Timebase = 6.4 ms,
For MCC_RTC_2, Timebase =16 ms,
For MCC_RTC_3, Timebase = 40 ms.
210/235
Function Descriptions
Note: The beep signal is available in ACTIVE-HALT mode, but has to be disabled to reduce
the consumption.
211/235
Function Descriptions
212/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the MCC functions.
Program Description:
This program configures fCPU main clock frequency as 2 MHz, oscillator interrupt timebase as
4ms and generates 1 KHz beep signal.
/*============================================================*/
/* Program Start */
#include "ST7lib_config.h"
/* File for user to select device as ST72F521, Fosc2 as 8MHz */
//protoytpe declaration
void MCC_IT_Routine (void);
void main(void);
/******************************************************************************
Interrupt Service Routine:
An example of 1kHz beep signal generation in BEEP pin, is given, which will
be executed when RTC interrupt is generated.
******************************************************************************/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
void MCC_IT_Routine (void)
{
MCC_Clear_IT (); /* Clears the interrupt */
MCC_Beep (MCC_BEEP_2); /* Generates 1 KHz beep signal */
}
213/235
Function Descriptions
9.1.15 EEPROM
Following are the functions related to EEPROM.
Function Name EEPROM_Init
Function Prototype Void EEPROM_Init (void)
Initialization of EEPROM. Loads the EEPROM register
Behaviour Description
with reset value (00h).
Input Parameters None
Output Parameters None
Selection of the right EEPROM device in the file
Required Preconditions
“ST7lib_config.h”.
Functions called None
Postconditions None
See also None
Notes:
– The EEPROM can enter WAIT mode, on execution of the WFI instruction of the microcon-
troller. If programming is in progress, then EEPROM will finish the current cycle and then en-
ter WAIT mode.
– The EEPROM immediately enters HALT mode if the microcontroller executes the HALT in-
struction. Therefore, EEPROM will stop the function in progress and the data may be cor-
rupted.
214/235
Function Descriptions
Notes:
– The value of *PtrToE2Buffer can be from 0x1000h to 0x107Fh for LITE0/1/2/3,
ST7SUPERLITE and ST7DALI device.
– You have to type-cast parameter PtrToE2Buffer to unsigned char * in the function
EEPROM_Read as shown in example (page 218).
– Because of the limitation of ST7FLite0/1/2/3, ST7SUPERLITE and ST7DALI ZRAM and
RAM size (which is 64 bytes each), you must take care while declaring the size of user buffer.
215/235
Function Descriptions
Notes:
– The value of *PtrToE2Buffer can be from 0x1000h to 0x107Fh for ST7FLite0/1/2/3,
ST7SUPERLITE and ST7DALI device.
– You have to type-cast parameter PtrToE2Buffer to unsigned char * in the function
EEPROM_Write as shown in example (page 218).
– To avoid incorrect programming, take care that all the bytes written between the two pro-
gramming sequences have the same high address: only the four Least Significant Bits of the
address can change.
– Because of the ST7FLite0/1/2/3, ST7SUPERLITE and ST7DALI ZRAM and RAM size limi-
tation (which is 64 bytes each), you must take care while declaring the size of user buffer.
216/235
Function Descriptions
Notes:
– Care should be taken during programming cycle. Writing to the same memory location will
over-program the memory. If a programming cycle is interrupted (by software or a reset ac-
tion), the integrity of the data in memory is not guaranteed.
– Reading the EEPROM memory is not possible when the data writing is in progress.
217/235
Function Descriptions
Example
The following C program shows the uses of the EEPROM functions for the Lite0 device.
Program Description:
This program writes 5 data bytes in EEPROM memory from one user buffer, Temp1. Then
data bytes are read from the same EEPROM memory and stored in another buffer, Temp2.
The written data and data read from the two buffers are then compared.
/*============================================================*/
/* Program Start */
void main(void);
void main (void)
{
int i;
unsigned char NoofBytes = 5;
unsigned char Temp1[5] = {0x55,0xAA,0x7F,0x18,0x4C};
unsigned char Temp2[5] = {0x00,0x00,0x00,0x00,0x00};
/*@far unsigned char * ptr_write;
@far unsigned char * ptr_read;
ptr_read = &ptr_address;
ptr_write = &ptr_address;*/
EEPROM_Read (Temp2,NoofBytes,&ptr_address);
/* Reads data from EEPROM address 1000h and stores it in buffer Temp2 */
/* EEPROM_Read (Temp2,5,(unsigned char * far) 0x1000); */ /* Read function
is called in this way for hiware compiler, when small memory model is used */
for (i = 0; i<5;i++)
{ /* Comparison of written data and data read */
if ((*(Temp1+i)) != (*(Temp2+i)))
{ /* Mismatch between written data and data read */
while (1);
}
}
}
/*Program Stop */
/*============================================================*/
218/235
Function Descriptions
9.1.16 I/O
The following are the functions related to Input/Output ports.
Function Name IO_Init
Function Prototype Void IO_Init (void)
Initialization of IO. Loads IO registers with reset value
Behaviour Description
(00h).
Input Parameters None
Output Parameters None
Required Preconditions Selection of the right device in the file “ST7lib_config.h”.
Functions called None
Postconditions None
See also None
Note: The bits associated with unavailable pins must always keep their reset value.
219/235
Function Descriptions
Notes:
– You can use this function to configure pins as floating input, when the pins are used as ADC
input.
– If you want to select external interrupt, you should use the “EnableInterrupts” macro after this
function.
Caution:
– Alternate function must not be activated, while the pin is configured as input with interrupt,
in order to avoid generating spurious interrupts.
– Input pull-up configuration can cause an unexpected value at the input of the alternate pe-
ripheral.
220/235
Function Descriptions
221/235
Function Descriptions
Note: When the IO port is in input configuration and associated alternate function is enabled
as an output, reading the port (DR) register will read the alternate function output status.
Caution: When you write data in a port register in this function, the previous data in the port
is modified.
222/235
Function Descriptions
Notes:
– When the I/O port is in output configuration and associated alternate function is enabled as
an input, the alternate function reads the pin status given by the port (DR) register content.
– This function reads DR register, performs the bit operations and writes back DR. This could
give different results in some situations, to avoid this use IO_ByteWrite with shadow register
variables.
223/235
Function Descriptions
EXAMPLE:
The following C program shows the use of the I/O functions.
Program Description:
This program, written for the ST72F521 device, configures all Port D pins in push-pull output
mode. The D5 and D7 port pins are put into logic high state. The port register is read and the
data is compared with the written data. If there is any mismatch between the data read and
data written, the control goes into a ‘while’ loop.
It then configures Port C (C3 & C4) in floating input mode. The C3 and C4 port pins are put into
logic high state by the external input. Then, port C is read. The read value is compared with
expected value, i.e.,0x18. If there is any mismatch between the data read and expected data,
the control goes into a ‘while’ loop.
/*============================================================*/
/* Program Start */
void main(void);
void main(void)
{
unsigned char Temp = 0x00;
224/235
Function Descriptions
225/235
Function Descriptions
IO_Output(IO_OPEN_DRAIN,IO_PORT_C,((unsigned char)IO_PIN_0|
((unsigned char)IO_PIN_1|((unsigned char)IO_PIN_2|((unsigned char)
IO_PIN_4|((unsigned char)IO_PIN_5|((unsigned char)IO_PIN_6|
((unsigned char)IO_PIN_7))))))));
226/235
Function Descriptions
227/235
Function Descriptions
9.2.1.2 Transmit-Services
Table 173. CanTransmit
Function Name CanTransmit
canuint8 CanTransmit (CanTransmitHandle
Function Prototype
<transmitObject>)
Input Parameters transmitObject - Selected transmit Handle
KCANTXOK - If the transmit request is accepted
by the CAN driver.
Output Parameters
KCANTXFAILED- If the transmit request is not
accepted by the CAN driver.
This service initiates the transmission within the
CAN controller for the CAN message referenced
by <transmitObject>. If any transmit mailbox is
empty, the transmit process is initiated and
KCANTXOK is returned. The message informa-
tion (message ID, DLC, data) is taken from the
transmit message table referenced by transmit
Behaviour Description handle <transmitObject> and it is copied into the
transmit registers. If message is transmitted suc-
cessfully, the confirmation flag is set for this mes-
sage inside the transmit interrupt routine
(CanTx_ISR). Transmit process is initiated and If
none of the mailbox is empty or the <transmitOb-
ject> is out of range then transmit process is not
initiated and KCANTXFAILED is returned.
This service shall not be called when the CAN
Required Preconditions
driver is in stop or sleep mode.
228/235
Function Descriptions
229/235
Function Descriptions
230/235
Function Descriptions
231/235
Function Descriptions
232/235
Appendix A
10 APPENDIX A
ST7SUPERLITE
ST7FLITE0
ST7FLITE1
ST7FLITE2
ST7FLITE3
ST72F63B
ST72F32A
ST72F521
ST72F325
ST72F264
ST72F561
ST72F62
ST72F65
Device
Family
ST72F325(AR/C/J/K)6/7/9
Supported
ST72F325(C/J/K)4
ST72F561(R/J/K)6
ST72F561(R/J/K)9
P/Ns
ST72F324B/L/BL
ST72F63BK2
ST72F63BK3
ST72F32AK2
ST72F260G1
ST72F262G1
ST72F262G2
ST72F264G1
ST72F264G2
ST72F63BK1
ST7FLITES2
ST7FLITES5
ST7FLITE1B
ST7FLITE02
ST7FLITE05
ST7FLITE09
ST7FLITE10
ST7FLITE15
ST7FLITE19
ST7FLITE20
ST7FLITE25
ST7FLITE29
ST7FLITE30
ST7FLITE35
ST7FLITE39
ST72F521B
ST72F321B
ST72F621
ST72F622
ST72F623
ST72F611
ST72F521
ST72F321
ST72F324
ST72F65
ADC 10-bit 8-bit 8-bit 10-bit 10-bit 10-bit 8-bit 10-bit 10-bit 10-bit 10-bit 10-bit 8-bit
SCI x x x x x x x x
SPI x x x x x x x x x x x x
I2C
(Master & x x x x x
Slave)
Timer (16-bit
timer)
x x x x x x x
Timer8 (8-bit
Timer)
x
LT (8-bit
LiteTimer)
x x x x x
PWMART x x x x
LART (12-bit
ART)
x x x x x
TBU x
WDG x x x x x x x x x x x
ITC x x x x x x x x x x x x x
MCC x x x x x x x x x x
EEPROM x x x x
I/O x x x x x x x x x x x x x
CAN 1) x
Note:
1. This software library supports only beCAN (basic extended 2.0b active CAN cell) which is
found, for example, in the ST72F561 devices. The ST72F521 device has a pCAN peripheral
(2.0b passive CAN cell) and therefore is not supported.
233/235
Revision History
11 REVISION HISTORY
234/235
Revision History
THE PRESENT MANUAL WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMA-
TION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELEC-
TRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RE-
SPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A MANUAL AND/OR THE USE MADE BY CUS-
TOMERS OF THE INFORMATION CONTAINED HEREIN IN CONNEXION WITH THEIR PRODUCTS.
Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences
of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted
by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject
to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not
authorized for use as critical components in life support devices or systems without express written approval of STMicroelectronics.
235/235