This application note describes the firmware library implementing the STLM75 interface for
the STM32F10xxx (STM32F101x, STM32F103x, STM32F105x, STM32F107x)
microcontroller.
This library is a firmware package which contains a collection of routines, data structures
and macros covering the features of the STLM75 temperature sensor device. The firmware
library allows the STLM75 sensor to be used in the user application without the need for an
in-depth study of STLM75 registers and I
the firmware library saves significant time which would otherwise be spent in coding, while
reducing the application development and integration costs.
The firmware library source code is developed in 'Strict ANSI-C' (relaxed ANSI-C for the
example application). Writing the whole library in 'Strict ANSI-C' makes it independent from
the software toolchain. In addition, the firmware architecture is developed in separate layers
and the HAL (hardware abstraction layer) makes it independent from the microcontroller
used in the final application.
2
C read/write operation steps. As a result, using
Even though the firmware library source code is developed in 'ANSI-C', the code
architecture follows an OOP (object oriented programming) approach.
Section 1 describes document and library rules.
Section 2 highlights the features of the STLM75 sensor and explains its hardware interface
with a master device microcontroller (STM32 in this case).
Section 3 and 4 highlight the features of the firmware library and describe its architecture
and its exported APIs (application programming interfaces) in detail.
Section 5 contains an example application source code describing how to configure and use
This document uses the conventions described in the sections below.
1.1 Acronyms
The following table lists the acronyms used in this document.
Table 1.List of abbreviations
AcronymMeaning
APIApplication programming interface
HALHardware abstraction layer
MCUMicrocontroller unit
I2CInter-integrated circuit
OOPObject oriented programming
Doc ID 16793 Rev 15/44
STLM75 temperature sensorAN3108
2 STLM75 temperature sensor
2.1 Sensor introduction
The STLM75 is a high-precision digital CMOS temperature sensor IC with a sigma-delta
temperature-to-digital converter and an I
2
C-compatible serial digital interface. It is targeted
at general applications such as personal computers, system thermal management,
electronics equipment, and industrial controllers, and is packaged in the industry standard
8-lead TSSOP and SO8 packages. The device contains a band-gap temperature sensor
and 9-bit ADC which monitor and digitize the temperature to a resolution up to 0.5 °C. The
STLM75 is typically accurate to ±3°C - max over the full temperature measurement range of
-55 °C to 125 °C with ±2 °C accuracy in the -25 °C to + 100 °C range (max). The STLM75 is
factory-calibrated and requires no external components to measure temperature.
Refer to the STLM75 (Digital temperature sensor and thermal watchdog) datasheet for more
information.
2.2 Interfacing the sensor with the microcontroller
The STLM75 has a simple 2-wire I2C-compatible digital serial interface which allows the
user to access the data in the temperature register at any time. It communicates via the
serial interface with a master controller which operates at speeds up to 400 kHz. Three pins
(A0, A1, and A2) are available for address selection, and enable the user to connect up to 8
devices on the same bus without address conflicts. In addition, the serial interface gives the
user easy access to all STLM75 registers to customize the operation of the device.
Figure 2 shows how the SMT32F10xxx microcontroller (master device) must be connected
to the STLM75 device.
Refer to the STLM75 datasheet for more information.
The STLM75 firmware library is fully developed in 'Strict ANSI-C' following an OOP
approach. This means the final application using this library uses an instance of a
temperature sensor object (TempSensor), and uses it according to its public methods and
properties. The TempSensor is a structure containing public properties (data fields) and
methods (functions pointers). The OOP encapsulation feature is assured.
The final application can create more than one TempSensor instance, and each instance
can be matched with a different STLM75 temperature sensor assembled on the board.
Therefore, the same library can be used to manage more temperature sensors
simultaneously without communication and data conflict problems.
The library may be included in the final application as a library file (STLM75.a) and used as
a black box through its exported public API, or can be included in the final application as
source files (.c and .h), if the user wants to debug the library itself, or if it’s necessary to
change the HAL functions in order to port the library on an alternative microcontroller to the
STM32F10xxx.
3.2 Library package
The library was developed using the IAR EWARM 5.20 and the related workspace/project
files are included in the delivered package. As all the firmware is written in 'Strict ANSI-C',
the library porting on another toolset doesn't require any change in the library.
The library folder contains all the subdirectories and files that make up the core of the
library:
●The included sub-folder contains the firmware library header files. They don't need to
be modified by the user:
– TempSensorObj.h: API layer file; this contains the Temperature Sensor Object
structure description in terms of methods and properties; The API functions are
declared in this file.
–TempSensorTypes.h: API layer file; this contains all the defined types used by
TempSensorObj.xxx files and related to the STLM75 temperature sensor.
–TempSensorHal.h: HAL layer file; this contains all the Temperature Sensor
functions declaration whose implementation depends on the MCU used (STM32
for this delivery). The final user should change these files in order to reuse this
STLM75 library with other microcontrollers
–TempSensorHalTypes.h HAL layer file; this contains all the Temperature Sensor
types mapped on the used MCU library types (STM32 for this delivery). The final
8/44Doc ID 16793 Rev 1
AN3108STLM75 library
user should change this type mapping in order to reuse this STLM75 library with
other microcontrollers
●The source sub-folder contains the firmware library source files. They don't need to be
modified by the user:
–TempSensorObj.c: API layer file; this contains the exported public API (Application
Programming Interface) and the related private internal functions. No direct
reference to the Hardware and Micro firmware library occur in this file.
–TempSensorHal.c: HAL layer file; this contains all the temperature sensor
functions implementation whose source code depends on the MCU used (STM32
for this delivery). The final user should change these file in order to reuse this
STLM75 library with other microcontrollers
–The STM32_Include sub-folder contains the STM32F10xxxV2.0.3 firmware library
included files. If the final user wants to use another microcontroller library version,
replace this folder and check the HAL types and the microcontroller library function
calls inside the HAL layer files (TempSensorHal.h, TempSensorHalTypes.h,
TempSensorHal.c)
●EWARMv5 sub-folder contains the IAR EWARM 5.20 workspace and project files:
–STLM75_Lib.eww: The IAR workspace file
–STLM75_Lib.ewp: The IAR project file
Figure 3.Firmware library project files
3.3 Library architecture
The library architecture is devised and developed in two separate layers:
●API layer
●HAL layer
This layer architecture improves the code reusability splitting the application programming
interface code (fully portable and reusable) from the hardware abstraction layer code
(hardware dependent and written onto the STM32F10xxx libraries).
3.3.1 API layer
The application programming interface layer allows the final application to use the library as
a black-box. The library firmware encapsulation feature and exported API allow full control of
Doc ID 16793 Rev 19/44
STLM75 libraryAN3108
the STLM75 temperature sensor without the need of an in-depth study of sensor registers
2
and I
C read/write operation steps.
The API layer includes the following files:
●TempSensorObj.h
●TempSensorTypes.h
●TempSensorObj.c;
See Section 4.1 for a more detailed description.
3.3.2 HAL layer
The hardware abstraction layer is directly built on the specific microcontroller firmware
library and allows the built-upon layers, like the API layer, to implement its functions without
the need of an in-depth study of the microcontroller used. This improves the library code
reusability and guarantees easy portability on other microcontrollers.
The HAL layer includes the following files:
●TempSensorHal.h
●TempSensorHalTypes.h
●TempSensorHal.c
See Section 4.2 for a more detailed description.
Figure 4.Firmware library architecture
10/44Doc ID 16793 Rev 1
AN3108STLM75 library firmware
4 STLM75 library firmware
This section describes the API and HAL layer implementation. Each library firmware
function is described in detail. API and HAL layer functions are fully described. An example
of how to use API functions is included. No example for HAL functions is provided because
the final application should manage the STLM75 temperature sensor through the API layer
functions only, without any direct access to the HAL functions.
The functions are described in the following format:
Table 3.Function description format
NameDescription
Function nameThe name of the peripheral function
Function prototypePrototype declaration
Behavior descriptionBrief explanation of how the function is executed
Input parameter {x}Description of the input parameters
Output parameter {x}Description of the output parameters
Return valueValue returned by the function
Required preconditionsRequirements before calling the function
Called functionsOther library functions called
4.1 API layer firmware overview
The application programming interface layer allows the final application to easily use the
STLM75 temperature sensor. An OOP approach is used, making it possible for the
application to create and use one or more instances of a TempSensor object.
The TempSensor structure is seen by the application as an object with encapsulate
properties and methods. All read/write operations on the temperature sensors are executed
through this object. It is an advanced structure containing:
●Properties as data fields
●Methods as functions pointers
In this way, each API function belongs to the related TempSensor object instance and more
instances, and then more temperature sensors, can be managed without any conflicts.
In addition, the library exports two public API global functions in order to create/destroy a
TempSensor structure instance:
●NewTempSensorObj function
●DelTempSensorObj function
Doc ID 16793 Rev 111/44
STLM75 library firmwareAN3108
NewTempSensorObj API global function
Ta bl e 4 describes the NewTempSensorObj function:
Table 4.NewTempSensor API function
NameDescription
Function nameNewTempSensorObj
Function prototypeTempSensorType* NewTempSensorObj (void)
Behavior descriptionCreate and initialize a new TempSensor object (a C structure)
Input parameter {x}None
Output parameter {x}None
Return valueThe created object pointer or null if the object cannot be created
Once a TempSensor object instance is created using the NewTempSensorObj function, the
TempSensor object itself provides all its features through its internal function pointers.
Figure 2 shows the TempSensor object properties and methods in detail, which the final
application can use to interact with the temperature sensor.
12/44Doc ID 16793 Rev 1
AN3108STLM75 library firmware
DelTempSensorObj API global function
Ta bl e 5 describes the DelTempSensorObj function:
Table 5.DelTempSensor API function
NameDescription
Function nameDelTempSensorObj
Function prototypeTS_ErrStatus DelTempSensorObj (TempSensorType** ppTempSensor)
Behavior description
Input parameter {x}ppTempSensor - Object pointer
Output parameter {x}None
Return valueTS_OK if successful, TS_ERROR otherwise
Required preconditionsNewTempSensorObj must have been called before
Called functionsNo API/HAL layers functions;
Destroy the Object internal members and free the memory allocated when
NewTempSensorObj function is called.
Example:
TempSensorType* pObjTempSensor;
TS_ErrStatus errStatus;
...
errStatus = DelTempSensorObj(pTempSensorObj);
Figure 5.Firmware library API and types
Doc ID 16793 Rev 113/44
STLM75 library firmwareAN3108
TempSensorObj API types/properties/functions
This section describes the TempSensor structure, all its exported API functions and all the
defined types. See Figure 2.
Below you can find a description of the defined types used by the TempSensor object
source code and a description of its properties and methods. Except for TempSensorType,
all API layer types are defined in the TempSensorTypes.h file.
TempSensorType type
The TempSensorType is defined in the following TempSensorObj.h file. The final
TempSensor object is an instance of this defined structure.
The following described structure type can be considered as an OOP-class including private
and public properties and public methods.
All the structure fields with a prefix name of 'm_' can be considered as private properties.
This means these structure fields must not be directly accessed by the final application and
Getxxx methods should be used instead. Clearly the final application could access these
private members, but it should NEVER do so.
The final application must interact with this library using the following public properties and
methods only, as described in Figure 2 and in the following sections.
#define TEMP_SENSOR_OBJ /* Temperature Sensor definition */ \
/* */ \
/* PRIVATE PROPERTIES */ \
TS_u8 m_Configuration; \
TS_TemperatureType m_Temperature; \
TS_TemperatureType m_TempHysteresis; \
TS_TemperatureType m_TempOverLimit; \
TS_RegistersType m_Registers; \
TS_I2C_SettingsType m_I2C_Settings; \
/* */ \
/* PUBLIC PROPERTIES */ \
TS_SignalsType Signals; /* The Micro/STLM75 signals */ \
TS_SignType TS_Sign; /* Positive or negative sign */
TS_u8 TS_IntegerValue; /* Integer part of the value */
TS_u8 TS_DecimalValue; /* Decimal part of the value */
} TS_TemperatureType;
TS_SignalsType type
/* The STLM75 Signals */
typedef struct {
TS_SignalType TS_SCL;
TS_SignalType TS_SDA;
TS_SignalType TS_OS_INT;
} TS_SignalsType;
TempSensor:: Signals API property
The TempSensor structure exports the following public property:
TS_SignalsType Signals;
The signals property contains the SCL, SDA, and OS_INT pin configuration. This property is
redundant because the final application can use GetSignals and SetSignals API layer
functions in order to manage signals property. The property has been left as public for utility
scope only. It is recommended that the final application instead uses GetSignals and
SetSignals functions to directly access signals public property.
Doc ID 16793 Rev 117/44
STLM75 library firmwareAN3108
TempSensor::TimeoutTicks API property
The TempSensor structure exports the following public property:
TS_u32 TimeoutTicks;
The TimeoutTick property allows the final application to avoid application block in case of
2
I
C communication problems. TimeoutTick is the number of g_TempSensorTick to wait
during an I
considering that an error has occurred and resetting the I
The following is an example of TimeoutTicks property:
...
2
C communication between the microcontroller and the STLM75 before
2
C peripheral.
Warning:TimeoutTicks must be set to >0 as following ONLY if
g_TempSensorTick is defined as: extern volatile u32 g_TempSensorTick; in the final application source code,
and it is incremented in a scheduler timer IRQ handler. If you
don't want to use timeout feature, don't configure the
TimeoutTicks property or set it to 0.
...
●File: main.c
int main(void)
{
...
/* configure SysTick timer in order to have a tick each ms */
...
/* configure the Temperature Sensor TimeoutTicks */
pObjTempSensor->TimeoutTicks = 2; /* 2ms:we have a g_TempSensorTick
each millisecond */
...
}
●File: stm32f10x_it.c
extern volatile u32 g_TempSensorTick;
...
void SysTickHandler(void)
{
// Increment Temperature Sensor tick for Timeout purpose
g_TempSensorTick ++;
18/44Doc ID 16793 Rev 1
AN3108STLM75 library firmware
}
...
TempSensor:: Init API function
Ta bl e 6 describes the Init function of the TempSensor structure:
Table 6.Init API function
NameDescription
Function nameInit
Function prototype
Behavior descriptionInitialize the TempSensor object.
Input parameter {x}
Output parameter {x}None
Return valueTS_OK if successful, TS_ERROR otherwise
Required preconditionsNewTempSensorObj, must have been called before.
Ta bl e 8 describes the SetI2C_Settings function of the TempSensor structure.
This function is called by the TempSensor:Init function during the initialization phase,
therefore it’s not necessary to directly call it. The final application can specifically call this
function in order to change the I
Table 8.SetI2C_Settings API function
NameDescription
Function nameSetI2C_Settings
Function prototype
Behavior description
Input parameter {x}
Output parameter {x}None
Return valueTS_OK if successful, TS_ERROR otherwise
Required preconditionsNewTempSensorObj function must have been called before.
Called functions
Set the STLM75 I2C peripheral settings copying the right values from the
Ta bl e 9 describes the SetSignals function of the TempSensor structure.
This function is called by the TempSensor:Init function during the initialization phase,
therefore it’s not necessary to directly call it. The final application can specifically call this
function in order to change the microcontroller signal (port, pin, mode) settings after
initialization.
Table 9.SetSignals
NameDescription
Function nameSetSignals
Function prototype
Behavior description
Input parameter {x}
Output parameter {x}None
Return valueTS_OK if successful, TS_ERROR otherwise
Required preconditionsNewTempSensorObj function must have been called before.
Ta bl e 1 1 describes the SetRegister function of the TempSensor structure.
This function can be considered as an advanced function for the final user who knows
STLM75 internal registers. This function call can be avoided by calling one of the following
specific functions:
Note that calling the SetRegister advanced function instead of a specific one, internal
TempSensor object data (m_Configuration, m_TempHysteresis, m_TempOverLimit) is not
assigned.
Table 11.SetRegister
NameDescription
Function nameSetRegister
Function prototype
Behavior description
Input parameter {x}
Output parameter {x}None
Return valueTS_OK if successful, TS_ERROR otherwise
Required preconditions
Called functions
Set the STLM75 register identified by eRegSelection type with the value
uRegValue - The value to be assigned to the register. This is a 16-bit
NewTempSensorObj, TempSensor:: Init functions must have been called
Ta bl e 1 2 describes the GetRegister function of the TempSensor structure.
This function can be considered as an advanced function for the final user who knows
STLM75 internal registers. This function call can be avoided by calling one of the following
specific functions:
This section describes the hardware abstraction layer function used by the upper API layer
described in the previous section. All the library microcontroller hardware dependent
functions and related defined types are described in this section. See Figure 2.
The final application should NEVER directly use these HAL functions, and it should manage
the STLM75 temperature sensor through the API layer functions as described above.
4.2.1 HAL types
Standard types redefinition
/* Standard type redefinition in order
to maintain code portability */
typedef signed long TS_s32;
typedef signed short TS_s16;
typedef signed char TS_s8;
typedef unsigned long TS_u32;
typedef unsigned short TS_u16;
typedef unsigned char TS_u8;
STM32 library types redefinition
/* Redefine micro specific types */
typedef GPIO_TypeDef* TS_PortType;
typedef GPIOMode_TypeDef TS_ModeType;
typedef u32 TS_PinType;
typedef I2C_TypeDef* TS_I2CType;
TS_SignalActionType type
/* Signal state enumeration */
typedef enum
{ TS_SIGNAL_LOW = Bit_RESET,
TS_SIGNAL_HIGH = Bit_SET
} TS_SignalActionType;
TS_StateType type
/* State type */
typedef enum
{
TS_DISABLE = DISABLE,
TS_ENABLE = ENABLE
30/44Doc ID 16793 Rev 1
AN3108STLM75 library firmware
} TS_StateType;
TS_SignalType type
/* The STLM75 Single Signal type */
typedef struct {
TS_PortType Port;
TS_PinType Pin;
TS_ModeType Mode;
} TS_SignalType;
TS_I2C_SettingsType type
/* The STLM75 I2C Peripheral settings */
typedef struct {
TS_I2CType TS_I2C_Ptr;
TS_u32 TS_I2C_Clock;
TS_u16 TS_I2C_Address;
} TS_I2C_SettingsType;
4.2.2 HAL functions
TS_ConfigSignal HAL function
Ta bl e 2 0 describes the TS_ConfigSignal function of the hardware abstraction layer.
Table 20.TS_ConfigSignal
NameDescription
Function nameTS_ConfigSignal
Function prototypevoid TS_ConfigSignal(TS_SignalType* pSignal)
Behavior descriptionConfig the signal pin according to the passed parameters
Input parameter {x}pSignal - Signal structure pointer
Output parameter {x}None
Return valueNone
Required preconditionsNone
Called functionsNone
Doc ID 16793 Rev 131/44
STLM75 library firmwareAN3108
TS_InitI2C_Peripheral HAL function
Ta bl e 2 1 describes the TS_InitI2C_Peripheral function of the hardware abstraction layer.
Table 21.TS_InitI2C_Peripheral
NameDescription
Function nameTS_InitI2C_Peripheral
Function prototypevoid TS_InitI2C_Peripheral(TS_I2C_SettingsType* pI2C_Settings)
eRegLenType - Register byte-length type (Can be 1-Byt or 2-Byte);
uTimeoutTicks - The g_TempSensorTick to wait before returning an error;
uTimeoutTicks must be >=1.
Doc ID 16793 Rev 135/44
Example applicationAN3108
5 Example application
Together with the STLM75 firmware library package, an example application is delivered in
order to provide the final user with a real example of STLM75 library use. See Figure 6.
The delivered example application has been developed using IAR EWARM 5.20 IDE and
can be built for both STM32F10xxx Medium-density and High-density microcontroller
families.
An application project which wants to use the STLM75 library must:
●Include the STLM75.a file in the project generated compiling the STLM75_LIB library
firmware delivered in the final library package. SeeSection 3.2. The 2STLM75 library
source code can be included instead of the STLM75.a object file
●Implement a main function as described in the following section.
Figure 6.Application project files
5.1 main.c
An example of a main application is reported below. The main function contains an example
of the STLM75 library initialization/configuration and implements temperature sensor
registers read/write operations:
/* Includes -----------------------------------------------------*/
/* You can configure timeout to avoid application block in case of
I2C communication problems. TimeoutTick is the number of
g_TempSensorTick to wait during an I2C communication between Micro
and STLM75 before considering an error has occurred and resetting
the I2C peripheral.
WARNING: TimeoutTicks must be set >0 as following ONLY if
g_TempSensorTick is defined as "extern volatile u32
g_TempSensorTick" in this Application,and is incremented in
SysTickHandler or another timer irq handler. */
pObjTempSensor->TimeoutTicks = 2; /* 2ms:we have a
g_TempSensorTick each
millisecond */
/* STLM75 Library use example --------------------------------*/
38/44Doc ID 16793 Rev 1
AN3108Example application
/* TEST: Write the following value for the Configuration
register:
0x00 */
3. STM32F10xFWLib 2.0.3, AN2953; How to migrate from the STM32F10xxx firmware
library V2.0.3 to the STM32F10xxx standard peripheral library V3.0.0, application note
42/44Doc ID 16793 Rev 1
AN3108Revision history
7 Revision history
Table 27.Document revision history
DateRevisionChanges
12-Oct-20101Initial release.
Doc ID 16793 Rev 143/44
AN3108
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.