STM32F0xx Standard Peripherals Firmware Library: I2C Communication Boards Data Exchange using CPAL Library

STM32F0xx Standard Peripherals Library

I2C Communication Boards Data Exchange using CPAL Library
  ******************** (C) COPYRIGHT 2014 STMicroelectronics *******************
  * @file    I2C/I2C_TwoBoards/readme.txt 
  * @author  MCD Application Team
  * @version V1.4.0
  * @date    24-July-2014
  * @brief   Description of the CPAL Two Boards communication example.
  ******************************************************************************
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
   
Example Description

This example shows how to use the CPAL library to control I2C devices and communicate between two different boards.

To use this example, you need to load the same software into two STM32 boards (let's call them Board A and Board B) then connect these two boards through I2C lines and Gnd.

------------------------------------------------------------------------------* | BOARD A BOARD B | | ____________________ ____________________ | | | | | | | | | | | | | | | __________ | | __________ | | | | |I2C Device|____|_______SCL_______|____|I2C Device| | | | | |__________|____|_______SDA_______|____|__________| | | | | | | | | | | | | | | | | Tamper SEL | | Tamper SEL | | | | _ _ | | _ _ | | | | |_| |_| | | |_| |_| | | | | | | | | | | GND O--|-----------------|--O GND | | | |____________________| |____________________| | | | | | ------------------------------------------------------------------------------*

The example software perform the following actions:

  • At startup, Boards A & B are both in slave receiver mode and wait for messages to be received.
  • When a push button is pressed, for example on Board A, the master transmitter mode is activated and a message is sent to Board B containing identification of the action relative to the push button.
  • Once a message is correctly sent, Board A activates the master transmitter mode permanently and disables the slave receiver mode.
  • Once a message is received and correctly recognized, Board B activates the receiver mode and disable the transmitter mode. (push buttons interrupts are still active but message sending is disabled).
  • According to the push button pressed, a value is affected to a Timer which generates periodic high priority interrupts. At each interrupt, a status message is sent to Board B. The period of Timer interrupts is variable depending on the push button pressed.
  • When SEL is pressed Board A send specific message (tSignal1) to Board B and set the period of Timer to 1.5 seconds. When Tamper is pressed specific message (tSignal2) is sent and the period of Timer is set to 0.375 seconds.

For each event (message received, message transmitted, error occur ...) a relative information is displayed on the LCD screen. When the same event is repeated, the line color is switched to highlight the event occurrence.

the Mode of Board (transmitter or receiver) is monitored by LEDs:

  • LED2 is toggling when transmitter mode is activated.
  • LED3 is toggling when receiver mode is activated.

LED1 and LED4 are toggling by an interrupt generated by TIM6 each 50ms.

The programming model of I2C communication can be selected (DMA or Interrupt) in stm32f0xx_i2c_cpal_conf.h file by commenting or uncommenting these defines in Section 2 (Transfer Options Configuration): #define CPAL_I2C_DMA_PROGMODEL #define CPAL_I2C_IT_PROGMODEL Note : Only one define must be selected. If these two defines are uncommented, DMA Programming model is selected and the code size of this example will increase.

UserCallbacks :

  • CPAL_TIMEOUT_UserCallback : This Callback is called to manage timeout error. In this Callback I2C device is reinitialized.
  • CPAL_I2C_ERR_UserCallback : This Callback is called to manage Device error . In this Callback I2C device is reinitialized. Note: If you want to generate an I2C error, you can disconnect I2C_SCL/I2C_SDA connector. In this case the I2C communication will be corrupted and the CPAL I2C error callbacks will be entered. If you fit back the jumper or connector, the communication should be recovered immediately thanks to CPAL error management.
  • CPAL_I2C_TXTC_UserCallback : This Callback is called when Write operation is completed. In this Callback only information relative to communication are displayed on LCD screen.
  • CPAL_I2C_RXTC_UserCallback : This Callback is called when Read operation is completed. In this Callback received data are compared and transfer status is displayed on LCD screen.
Directory contents
Note:
The "system_stm32f0xx.c" is generated by an automatic clock configuration tool and can be easily customized to meet user application requirements. To select different clock setup, use the "STM32F0xx_Clock_Configuration_VX.Y.Z.xls" provided with the AN4055 package available on ST Microcontrollers
Hardware and Software environment
  • This example runs on STM32F0xx devices.
  • This example has been tested with STMicroelectronics STM320518-EVAL and STM32072B-EVAL including respectively STM32F051R8T6 and STM32F072VBT6 devices and can be easily tailored to any other supported device and development board
  • The same example should be loaded in two evaluation boards.
  • STM320518-EVAL Set-up
    • Use two boards should be connected as follows:
      • Connect I2C1 SCL pin (PB6) to I2C1 SCL pin (PB6)
      • Connect I2C1 SDA pin (PB7) to I2C SDA pin (PB7)
      • Connect Gnd pins of two boards.
  • STM32072B-EVAL Set-up
    • Use two boards should be connected as follows:
      • Connect I2C1 SCL pin (PB6) to I2C1 SCL pin (PB6)
      • Connect I2C1 SDA pin (PB7) to I2C SDA pin (PB7)
      • Connect Gnd pins of two boards.
How to use it ?

In order to make the program work, you must do the following :

  • Copy all source files from this example folder to the template folder under Project
  • Open your preferred toolchain
  • If the used device is STM32F051R8T6 choose STM32F051 project
    • Add the following files to the project source list
      • Libraries/STM32F0xx_CPAL_Driver/stm32f0xx_i2c_cpal.c
      • Libraries/STM32F0xx_CPAL_Driver/stm32f0xx_i2c_cpal_hal.c
      • stm32f0xx_i2c_cpal_usercallback.c
      • Utilities/STM32_EVAL/STM320518_EVAL/stm320518_eval.c
      • Utilities/STM32_EVAL/STM320518_EVAL/stm320518_eval_lcd.c
  • If the used device is STM32F072VBT6 choose STM32F072 project
    • Add the following files to the project source list
      • Libraries/STM32F0xx_CPAL_Driver/stm32f0xx_i2c_cpal.c
      • Libraries/STM32F0xx_CPAL_Driver/stm32f0xx_i2c_cpal_hal.c
      • stm32f0xx_i2c_cpal_usercallback.c
      • Utilities/STM32_EVAL/STM32072B_EVAL/stm32072b_eval.c
      • Utilities/STM32_EVAL/STM32072B_EVAL/stm32072b_eval_lcd.c
  • Rebuild all files and load your image into target memory
  • Run the example

© COPYRIGHT STMicroelectronics

STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32