October 2012 Doc ID 023577 Rev 1 1/66
UM1566
User manual
STM32F0xx and STM32F3xx
I2C communication peripheral application library (CPAL v2)
Introduction
The purpose of this document is to explain the architecture and the implementation of the
I
2
C CPAL v2 (Communication Peripheral Application Library).
The CPAL v2 provides the same high layer API implemented in CPAL v1, thus you can
migrate between these two libraries easily.
The CPAL v1 is a library which provides API for I
2
C peripherals of STM32F1, STM32F2,
STM32F4 and STM32L1 devices. While CPAL v2 provides API for I
2
C peripherals of
STM32F0 and STM32F3 devices.
It provides CPAL drivers for communication peripherals and some examples showing how to
use and customize the CPAL drivers.
The CPAL v2 drivers and examples are supplied within STM32F0xx, STM32F37x and
STM32F30x Standard Peripherals library; the drivers are under
Libraries\STM32xxxx_CPAL_Driver and the examples under
Project\STM32xxxx_StdPeriph_Examples\I2C.
Table 1 lists applicable products concerned by this user manual.
.
Table 1. Applicable products
Type Part numbers
Microcontrollers STM32F0xxxx and STM32F3xxxx
www.st.com
Contents UM1566
2/66 Doc ID 023577 Rev 1
Contents
1 CPAL overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 CPAL architecture description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1 CPAL application hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Communication layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.1 CPAL main structures (stm32xxxx_i2c_cpal.h) . . . . . . . . . . . . . . . . . . . . 9
2.2.2 CPAL communication functions (stm32xxxx_i2c_cpal.c) . . . . . . . . . . . . 16
2.3 User application interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.1 Configuration interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.2 User callback interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4 Low layer interface (hardware abstraction layer HAL) . . . . . . . . . . . . . . . 24
3 CPAL functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.1 CPAL_I2C_Init() functional description . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1.2 CPAL_I2C_DeInit() functional description . . . . . . . . . . . . . . . . . . . . . . . 29
3.1.3 CPAL_I2C_StructInit() functional description . . . . . . . . . . . . . . . . . . . . . 31
3.2 Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2.1 CPAL_I2C_Read() functional description . . . . . . . . . . . . . . . . . . . . . . . 32
3.2.2 CPAL_I2C_Write() functional description . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2.3 CPAL_I2C_Listen () functional description: . . . . . . . . . . . . . . . . . . . . . . 35
3.2.4 CPAL_I2C_IsDeviceReady() functional description . . . . . . . . . . . . . . . . 35
3.2.5 CPAL interrupts and DMA management . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3 Event and error management (user callbacks) . . . . . . . . . . . . . . . . . . . . 38
3.3.1 Timeout management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4 How to use and customize the CPAL library (step by step) . . . . . . . . 42
4.1 Basic configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.1.1 Select peripherals to be used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.1.2 Configure transfer options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.1.3 Select and configure user and error callbacks . . . . . . . . . . . . . . . . . . . . 43
4.1.4 Configure timeout management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.1.5 Set Events, Errors and DMA interrupt priorities . . . . . . . . . . . . . . . . . . . 44
4.1.6 Configure the Log Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44