• heroistired
  • C/C++
  • 5.6MB
  • zip
  • 0
  • 1 积分
  • 7
  • 2020-02-26 19:35
/** @page I2C_TwoBoards_RestartAdvComIT I2C example @verbatim ******************** (C) COPYRIGHT 2017 STMicroelectronics ******************* * @file I2C/I2C_TwoBoards_RestartAdvComIT/readme.txt * @author MCD Application Team * @brief Description of the I2C_TwoBoards_RestartAdvComIT I2C example. ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** @endverbatim @par Example Description How to perform multiple I2C data buffer transmission/reception between two boards, in interrupt mode and with restart condition. _________________________ _________________________ | ______________| ___VDD____ |______________ | | | I2C1 | | | | I2C1| | | | | R (4.7K) R | | | | | SCL(PB6)|_____|________|_______|(PB6)SCL | | | | | | | | | | | | | | | | | | | | | | | | | SDA(PB7)|______________|_______|(PB7)SDA | | | | | | | | | |______________| |______________| | | __ | | __ | | |__| | | |__| | | USER GND|______________________|GND USER | | | | | |_STM32F103RB_NUCLEO______| |_STM32F103RB_NUCLEO______| This example shows how to configure GPIO, DMA and I2C peripherals using two STM32F103RB_NUCLEO boards for exchanging some datas between an I2C Master device using Interrupt mode and an I2C Slave device using Interrupt mode through the STM32F1xx I2C HAL API. At the beginning of the main program the HAL_Init() function is called to reset all the peripherals, initialize the Flash interface and the systick. Then the SystemClock_Config() function is used to configure the system clock (SYSCLK) to run at 64 MHz. The I2C peripheral configuration is ensured by the HAL_I2C_Init() function. This later is calling the HAL_I2C_MspInit()function which core is implementing the configuration of the needed I2C resources according to the used hardware (CLOCK, GPIO and NVIC). You may update this function to change I2C configuration. The User push-button is used to initiate a communication between Master device to Slave. User can initiate a new communication after each previous transfer completed. The I2C communication is then initiated. The project is splitted in two parts the Master Board and the Slave Board - Master Board The HAL_I2C_Master_Sequential_Transmit_IT() and the HAL_I2C_Master_Sequential_Receive_IT() functions allow respectively the transmission and the reception of a predefined data buffer in Master mode. - Slave Board The HAL_I2C_EnableListen_IT(), HAL_I2C_Slave_Sequential_Receive_IT() and the HAL_I2C_Slave_Sequential_Transmit_IT() functions allow respectively the "Listen" the I2C bus for address match code event, reception and the transmission of a predefined data buffer in Slave mode. The user can choose between Master and Slave through "#define MASTER_BOARD" in the "main.c" file. If the Master board is used, the "#define MASTER_BOARD" must be uncommented. If the Slave board is used the "#define MASTER_BOARD" must be commented. Example execution: On Master board side: - Wait User push-button to be pressed. This action initiate a write request by Master through HAL_I2C_Master_Sequential_Transmit_IT() or a write then read request through HAL_I2C_Master_Sequential_Transmit_IT() then HAL_I2C_Master_Sequential_Receive_IT() routine depends on Command Code type. Initialy at power on Slave device through Interrupt "Listen" the I2C bus to perform an acknowledge of Match Address when necessary. This "Listen" action is initiated by calling HAL_I2C_EnableListen_IT(). Command code type is decomposed in two categories : 1- Action Command code a. Type of command which need an internal action from Slave Device without sending any specific answer to Master. b. I2C sequence is composed like that : _____________________________________________________________________________________ |_START_|_Slave_Address_|_Wr_|_A_|_Command_Code_BYTE_1_|_A_|_Command_Code_BYTE_2_|_A_|.... ________________________________ |_Command_Code_BYTE_M_|_A_|_STOP_| First of all, through HAL_I2C_Master_Sequential_Transmit_IT() routine, Master device generate an I2C start condition with the Slave address and a write bit condition. In Slave side, when address Slave match code is received on I2C1, an event interrupt (ADDR) occurs. I2C1 IRQ Handler routine is then calling HAL_I2C_AddrCallback() which check Address Match Code and direction Write (Transmit) to call the correct HAL_I2C_Slave_Sequential_Receive_IT() function. This will allow Slave to enter in receiver mode and then acknowledge Master to send the Command code bytes through Interrupt. The Command code data is received and treated byte per byte through HAL_I2C_SlaveRxCpltCallback() in Slave side until a STOP condition. And so in Master side, each time the Slave acknowledge the byte received, Master transfer the next data from flash memory buffer to I2C1 TXDR register until "Action Command code" Transfer completed. Master auto-generate a Stop condition when transfer is achieved. The STOP condition generate a STOP interrupt and initiate the end of reception on Slave side. Thanks to HAL_I2C_ListenCpltCallback(), Slave is informed of the end of Communication with Master and "Listen" mode is also terminated. STM32F103RB_NUCLEO board's LEDs can be used to monitor the transfer status : Slave board side only : - LED2 is turned ON when the reception process is completed. Master board side only : - LED2 is turned ON when the transmission process is completed. Both side - LED2 is slowly blinking (1 sec. period) when there is an error in communication process.(communication is stopped if any, using infinite loop) These LEDs status are keeped at same value during 1 Second and then clear, this will allow to monitor a next transfer status. Also only on Master board side, Terminal I/O can be used to watch the Action Command Code sent by Master and associated Slave action with IDE in debug mode. Depending of IDE, to watch content of Terminal I/O note that - When resorting to EWARM IAR IDE: Command Code is displayed on debugger as follows: View --> Terminal I/O - When resorting to MDK-ARM KEIL IDE: Command Code is displayed on debugger as follows: View --> Serial Viewer --> Debug (printf) Viewer - When resorting to AC6 SW4STM32 IDE: In Debug configuration window\ Startup, in addition to "monitor reset halt" add the command "monitor arm semihosting enable" Command Code is displayed on debugger as follows: Window--> Show View--> Console. - When resorting to Atollic TrueSTUIDIO IDE: debug --> view--> others-->SWV console 2- Request Command code : a. Type of command which need a specific data answer from Slave Device. b. I2C sequence is composed like that : ___________________________________________________