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 */ \