|
STM32G474xx HAL 用户手册
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32g4xx_hal_i2c.c 00004 * @author MCD Application Team 00005 * @brief I2C HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Inter Integrated Circuit (I2C) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral State and Errors functions 00011 * 00012 ****************************************************************************** 00013 * @attention 00014 * 00015 * Copyright (c) 2019 STMicroelectronics. 00016 * All rights reserved. 00017 * 00018 * This software is licensed under terms that can be found in the LICENSE file 00019 * in the root directory of this software component. 00020 * If no LICENSE file comes with this software, it is provided AS-IS. 00021 * 00022 ****************************************************************************** 00023 @verbatim 00024 ============================================================================== 00025 ##### How to use this driver ##### 00026 ============================================================================== 00027 [..] 00028 The I2C HAL driver can be used as follows: 00029 00030 (#) Declare a I2C_HandleTypeDef handle structure, for example: 00031 I2C_HandleTypeDef hi2c; 00032 00033 (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API: 00034 (##) Enable the I2Cx interface clock 00035 (##) I2C pins configuration 00036 (+++) Enable the clock for the I2C GPIOs 00037 (+++) Configure I2C pins as alternate function open-drain 00038 (##) NVIC configuration if you need to use interrupt process 00039 (+++) Configure the I2Cx interrupt priority 00040 (+++) Enable the NVIC I2C IRQ Channel 00041 (##) DMA Configuration if you need to use DMA process 00042 (+++) Declare a DMA_HandleTypeDef handle structure for 00043 the transmit or receive channel 00044 (+++) Enable the DMAx interface clock using 00045 (+++) Configure the DMA handle parameters 00046 (+++) Configure the DMA Tx or Rx channel 00047 (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle 00048 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on 00049 the DMA Tx or Rx channel 00050 00051 (#) Configure the Communication Clock Timing, Own Address1, Master Addressing mode, Dual Addressing mode, 00052 Own Address2, Own Address2 Mask, General call and Nostretch mode in the hi2c Init structure. 00053 00054 (#) Initialize the I2C registers by calling the HAL_I2C_Init(), configures also the low level Hardware 00055 (GPIO, CLOCK, NVIC...etc) by calling the customized HAL_I2C_MspInit(&hi2c) API. 00056 00057 (#) To check if target device is ready for communication, use the function HAL_I2C_IsDeviceReady() 00058 00059 (#) For I2C IO and IO MEM operations, three operation modes are available within this driver : 00060 00061 *** Polling mode IO operation *** 00062 ================================= 00063 [..] 00064 (+) Transmit in master mode an amount of data in blocking mode using HAL_I2C_Master_Transmit() 00065 (+) Receive in master mode an amount of data in blocking mode using HAL_I2C_Master_Receive() 00066 (+) Transmit in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Transmit() 00067 (+) Receive in slave mode an amount of data in blocking mode using HAL_I2C_Slave_Receive() 00068 00069 *** Polling mode IO MEM operation *** 00070 ===================================== 00071 [..] 00072 (+) Write an amount of data in blocking mode to a specific memory address using HAL_I2C_Mem_Write() 00073 (+) Read an amount of data in blocking mode from a specific memory address using HAL_I2C_Mem_Read() 00074 00075 00076 *** Interrupt mode IO operation *** 00077 =================================== 00078 [..] 00079 (+) Transmit in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Transmit_IT() 00080 (+) At transmission end of transfer, HAL_I2C_MasterTxCpltCallback() is executed and users can 00081 add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback() 00082 (+) Receive in master mode an amount of data in non-blocking mode using HAL_I2C_Master_Receive_IT() 00083 (+) At reception end of transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can 00084 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback() 00085 (+) Transmit in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Transmit_IT() 00086 (+) At transmission end of transfer, HAL_I2C_SlaveTxCpltCallback() is executed and users can 00087 add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback() 00088 (+) Receive in slave mode an amount of data in non-blocking mode using HAL_I2C_Slave_Receive_IT() 00089 (+) At reception end of transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can 00090 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback() 00091 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00092 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00093 (+) Abort a master or memory I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() 00094 (+) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can 00095 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback() 00096 (+) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro. 00097 This action will inform Master to generate a Stop condition to discard the communication. 00098 00099 00100 *** Interrupt mode or DMA mode IO sequential operation *** 00101 ========================================================== 00102 [..] 00103 (@) These interfaces allow to manage a sequential transfer with a repeated start condition 00104 when a direction change during transfer 00105 [..] 00106 (+) A specific option field manage the different steps of a sequential transfer 00107 (+) Option field values are defined through I2C_XFEROPTIONS and are listed below: 00108 (++) I2C_FIRST_AND_LAST_FRAME: No sequential usage, functional is same as associated interfaces in 00109 no sequential mode 00110 (++) I2C_FIRST_FRAME: Sequential usage, this option allow to manage a sequence with start condition, address 00111 and data to transfer without a final stop condition 00112 (++) I2C_FIRST_AND_NEXT_FRAME: Sequential usage (Master only), this option allow to manage a sequence with 00113 start condition, address and data to transfer without a final stop condition, 00114 an then permit a call the same master sequential interface several times 00115 (like HAL_I2C_Master_Seq_Transmit_IT() then HAL_I2C_Master_Seq_Transmit_IT() 00116 or HAL_I2C_Master_Seq_Transmit_DMA() then HAL_I2C_Master_Seq_Transmit_DMA()) 00117 (++) I2C_NEXT_FRAME: Sequential usage, this option allow to manage a sequence with a restart condition, address 00118 and with new data to transfer if the direction change or manage only the new data to 00119 transfer 00120 if no direction change and without a final stop condition in both cases 00121 (++) I2C_LAST_FRAME: Sequential usage, this option allow to manage a sequance with a restart condition, address 00122 and with new data to transfer if the direction change or manage only the new data to 00123 transfer 00124 if no direction change and with a final stop condition in both cases 00125 (++) I2C_LAST_FRAME_NO_STOP: Sequential usage (Master only), this option allow to manage a restart condition 00126 after several call of the same master sequential interface several times 00127 (link with option I2C_FIRST_AND_NEXT_FRAME). 00128 Usage can, transfer several bytes one by one using 00129 HAL_I2C_Master_Seq_Transmit_IT 00130 or HAL_I2C_Master_Seq_Receive_IT 00131 or HAL_I2C_Master_Seq_Transmit_DMA 00132 or HAL_I2C_Master_Seq_Receive_DMA 00133 with option I2C_FIRST_AND_NEXT_FRAME then I2C_NEXT_FRAME. 00134 Then usage of this option I2C_LAST_FRAME_NO_STOP at the last Transmit or 00135 Receive sequence permit to call the opposite interface Receive or Transmit 00136 without stopping the communication and so generate a restart condition. 00137 (++) I2C_OTHER_FRAME: Sequential usage (Master only), this option allow to manage a restart condition after 00138 each call of the same master sequential 00139 interface. 00140 Usage can, transfer several bytes one by one with a restart with slave address between 00141 each bytes using 00142 HAL_I2C_Master_Seq_Transmit_IT 00143 or HAL_I2C_Master_Seq_Receive_IT 00144 or HAL_I2C_Master_Seq_Transmit_DMA 00145 or HAL_I2C_Master_Seq_Receive_DMA 00146 with option I2C_FIRST_FRAME then I2C_OTHER_FRAME. 00147 Then usage of this option I2C_OTHER_AND_LAST_FRAME at the last frame to help automatic 00148 generation of STOP condition. 00149 00150 (+) Different sequential I2C interfaces are listed below: 00151 (++) Sequential transmit in master I2C mode an amount of data in non-blocking mode using 00152 HAL_I2C_Master_Seq_Transmit_IT() or using HAL_I2C_Master_Seq_Transmit_DMA() 00153 (+++) At transmission end of current frame transfer, HAL_I2C_MasterTxCpltCallback() is executed and 00154 users can add their own code by customization of function pointer HAL_I2C_MasterTxCpltCallback() 00155 (++) Sequential receive in master I2C mode an amount of data in non-blocking mode using 00156 HAL_I2C_Master_Seq_Receive_IT() or using HAL_I2C_Master_Seq_Receive_DMA() 00157 (+++) At reception end of current frame transfer, HAL_I2C_MasterRxCpltCallback() is executed and users can 00158 add their own code by customization of function pointer HAL_I2C_MasterRxCpltCallback() 00159 (++) Abort a master or memory IT or DMA I2C process communication with Interrupt using HAL_I2C_Master_Abort_IT() 00160 (+++) End of abort process, HAL_I2C_AbortCpltCallback() is executed and users can 00161 add their own code by customization of function pointer HAL_I2C_AbortCpltCallback() 00162 (++) Enable/disable the Address listen mode in slave I2C mode using HAL_I2C_EnableListen_IT() 00163 HAL_I2C_DisableListen_IT() 00164 (+++) When address slave I2C match, HAL_I2C_AddrCallback() is executed and users can 00165 add their own code to check the Address Match Code and the transmission direction request by master 00166 (Write/Read). 00167 (+++) At Listen mode end HAL_I2C_ListenCpltCallback() is executed and users can 00168 add their own code by customization of function pointer HAL_I2C_ListenCpltCallback() 00169 (++) Sequential transmit in slave I2C mode an amount of data in non-blocking mode using 00170 HAL_I2C_Slave_Seq_Transmit_IT() or using HAL_I2C_Slave_Seq_Transmit_DMA() 00171 (+++) At transmission end of current frame transfer, HAL_I2C_SlaveTxCpltCallback() is executed and 00172 users can add their own code by customization of function pointer HAL_I2C_SlaveTxCpltCallback() 00173 (++) Sequential receive in slave I2C mode an amount of data in non-blocking mode using 00174 HAL_I2C_Slave_Seq_Receive_IT() or using HAL_I2C_Slave_Seq_Receive_DMA() 00175 (+++) At reception end of current frame transfer, HAL_I2C_SlaveRxCpltCallback() is executed and users can 00176 add their own code by customization of function pointer HAL_I2C_SlaveRxCpltCallback() 00177 (++) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00178 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00179 (++) Discard a slave I2C process communication using __HAL_I2C_GENERATE_NACK() macro. 00180 This action will inform Master to generate a Stop condition to discard the communication. 00181 00182 *** Interrupt mode IO MEM operation *** 00183 ======================================= 00184 [..] 00185 (+) Write an amount of data in non-blocking mode with Interrupt to a specific memory address using 00186 HAL_I2C_Mem_Write_IT() 00187 (+) At Memory end of write transfer, HAL_I2C_MemTxCpltCallback() is executed and users can 00188 add their own code by customization of function pointer HAL_I2C_MemTxCpltCallback() 00189 (+) Read an amount of data in non-blocking mode with Interrupt from a specific memory address using 00190 HAL_I2C_Mem_Read_IT() 00191 (+) At Memory end of read transfer, HAL_I2C_MemRxCpltCallback() is executed and users can 00192 add their own code by customization of function pointer HAL_I2C_MemRxCpltCallback() 00193 (+) In case of transfer Error, HAL_I2C_ErrorCallback() function is executed and users can 00194 add their own code by customization of function pointer HAL_I2C_ErrorCallback() 00195 00196 *** DMA mode IO operation *** 00197 ============================== 00198 [..] 00199 (+) Transmit in master mode an amount of data in non-blocking mode (DMA) using 00200 HAL_I2C_Master