mbed library sources

Dependents:   Marvino mbot

Fork of mbed-src by mbed official

Committer:
jaerts
Date:
Tue Dec 22 13:22:16 2015 +0000
Revision:
637:ed69428d4850
Parent:
610:813dcc80987e
Add very shady LPC1768 CAN Filter implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 610:813dcc80987e 1 /**
mbed_official 610:813dcc80987e 2 ******************************************************************************
mbed_official 610:813dcc80987e 3 * @file stm32l4xx_hal_smbus.c
mbed_official 610:813dcc80987e 4 * @author MCD Application Team
mbed_official 610:813dcc80987e 5 * @version V1.0.0
mbed_official 610:813dcc80987e 6 * @date 26-June-2015
mbed_official 610:813dcc80987e 7 * @brief SMBUS HAL module driver.
mbed_official 610:813dcc80987e 8 * This file provides firmware functions to manage the following
mbed_official 610:813dcc80987e 9 * functionalities of the System Management Bus (SMBus) peripheral,
mbed_official 610:813dcc80987e 10 * based on I2C principles of operation :
mbed_official 610:813dcc80987e 11 * + Initialization and de-initialization functions
mbed_official 610:813dcc80987e 12 * + IO operation functions
mbed_official 610:813dcc80987e 13 * + Peripheral State and Errors functions
mbed_official 610:813dcc80987e 14 *
mbed_official 610:813dcc80987e 15 @verbatim
mbed_official 610:813dcc80987e 16 ==============================================================================
mbed_official 610:813dcc80987e 17 ##### How to use this driver #####
mbed_official 610:813dcc80987e 18 ==============================================================================
mbed_official 610:813dcc80987e 19 [..]
mbed_official 610:813dcc80987e 20 The SMBUS HAL driver can be used as follows:
mbed_official 610:813dcc80987e 21
mbed_official 610:813dcc80987e 22 (#) Declare a SMBUS_HandleTypeDef handle structure, for example:
mbed_official 610:813dcc80987e 23 SMBUS_HandleTypeDef hsmbus;
mbed_official 610:813dcc80987e 24
mbed_official 610:813dcc80987e 25 (#)Initialize the SMBUS low level resources by implementing the HAL_SMBUS_MspInit() API:
mbed_official 610:813dcc80987e 26 (++) Enable the SMBUSx interface clock with __HAL_RCC_I2Cx_CLK_ENABLE()
mbed_official 610:813dcc80987e 27 (++) SMBUS pins configuration
mbed_official 610:813dcc80987e 28 (+++) Enable the clock for the SMBUS GPIOs
mbed_official 610:813dcc80987e 29 (+++) Configure SMBUS pins as alternate function open-drain
mbed_official 610:813dcc80987e 30 (++) NVIC configuration if you need to use interrupt process
mbed_official 610:813dcc80987e 31 (+++) Configure the SMBUSx interrupt priority
mbed_official 610:813dcc80987e 32 (+++) Enable the NVIC SMBUS IRQ Channel
mbed_official 610:813dcc80987e 33
mbed_official 610:813dcc80987e 34 (#) Configure the Communication Clock Timing, Bus Timeout, Own Address1, Master Addressing Mode,
mbed_official 610:813dcc80987e 35 Dual Addressing mode, Own Address2, Own Address2 Mask, General call, Nostretch mode,
mbed_official 610:813dcc80987e 36 Peripheral mode and Packet Error Check mode in the hsmbus Init structure.
mbed_official 610:813dcc80987e 37
mbed_official 610:813dcc80987e 38 (#) Initialize the SMBUS registers by calling the HAL_SMBUS_Init() API:
mbed_official 610:813dcc80987e 39 (++) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
mbed_official 610:813dcc80987e 40 by calling the customized HAL_SMBUS_MspInit(&hsmbus) API.
mbed_official 610:813dcc80987e 41
mbed_official 610:813dcc80987e 42 (#) To check if target device is ready for communication, use the function HAL_SMBUS_IsDeviceReady()
mbed_official 610:813dcc80987e 43
mbed_official 610:813dcc80987e 44 (#) For SMBUS IO operations, only one mode of operations is available within this driver :
mbed_official 610:813dcc80987e 45
mbed_official 610:813dcc80987e 46 *** Interrupt mode IO operation ***
mbed_official 610:813dcc80987e 47 ===================================
mbed_official 610:813dcc80987e 48 [..]
mbed_official 610:813dcc80987e 49 (+) Transmit in master/host SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Master_Transmit_IT()
mbed_official 610:813dcc80987e 50 (++) At transmission end of transfer HAL_SMBUS_MasterTxCpltCallback() is executed and user can
mbed_official 610:813dcc80987e 51 add his own code by customization of function pointer HAL_SMBUS_MasterTxCpltCallback()
mbed_official 610:813dcc80987e 52 (+) Receive in master/host SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Master_Receive_IT()
mbed_official 610:813dcc80987e 53 (++) At reception end of transfer HAL_SMBUS_MasterRxCpltCallback() is executed and user can
mbed_official 610:813dcc80987e 54 add his own code by customization of function pointer HAL_SMBUS_MasterRxCpltCallback()
mbed_official 610:813dcc80987e 55 (+) Abort a master/host SMBUS process communication with Interrupt using HAL_SMBUS_Master_Abort_IT()
mbed_official 610:813dcc80987e 56 (++) The associated previous transfer callback is called at the end of abort process
mbed_official 610:813dcc80987e 57 (++) mean HAL_SMBUS_MasterTxCpltCallback() in case of previous state was master transmit
mbed_official 610:813dcc80987e 58 (++) mean HAL_SMBUS_MasterRxCpltCallback() in case of previous state was master receive
mbed_official 610:813dcc80987e 59 (+) Enable/disable the Address listen mode in slave/device or host/slave SMBUS mode
mbed_official 610:813dcc80987e 60 using HAL_SMBUS_EnableListen_IT() HAL_SMBUS_DisableListen_IT()
mbed_official 610:813dcc80987e 61 (++) When address slave/device SMBUS match, HAL_SMBUS_AddrCallback() is executed and user can
mbed_official 610:813dcc80987e 62 add his own code to check the Address Match Code and the transmission direction request by master/host (Write/Read).
mbed_official 610:813dcc80987e 63 (++) At Listen mode end HAL_SMBUS_ListenCpltCallback() is executed and user can
mbed_official 610:813dcc80987e 64 add his own code by customization of function pointer HAL_SMBUS_ListenCpltCallback()
mbed_official 610:813dcc80987e 65 (+) Transmit in slave/device SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Slave_Transmit_IT()
mbed_official 610:813dcc80987e 66 (++) At transmission end of transfer HAL_SMBUS_SlaveTxCpltCallback() is executed and user can
mbed_official 610:813dcc80987e 67 add his own code by customization of function pointer HAL_SMBUS_SlaveTxCpltCallback()
mbed_official 610:813dcc80987e 68 (+) Receive in slave/device SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Slave_Receive_IT()
mbed_official 610:813dcc80987e 69 (++) At reception end of transfer HAL_SMBUS_SlaveRxCpltCallback() is executed and user can
mbed_official 610:813dcc80987e 70 add his own code by customization of function pointer HAL_SMBUS_SlaveRxCpltCallback()
mbed_official 610:813dcc80987e 71 (+) Enable/Disable the SMBUS alert mode using HAL_SMBUS_EnableAlert_IT() HAL_SMBUS_DisableAlert_IT()
mbed_official 610:813dcc80987e 72 (++) When SMBUS Alert is generated HAL_SMBUS_ErrorCallback() is executed and user can
mbed_official 610:813dcc80987e 73 add his own code by customization of function pointer HAL_SMBUS_ErrorCallback()
mbed_official 610:813dcc80987e 74 to check the Alert Error Code using function HAL_SMBUS_GetError()
mbed_official 610:813dcc80987e 75 (+) Get HAL state machine or error values using HAL_SMBUS_GetState() or HAL_SMBUS_GetError()
mbed_official 610:813dcc80987e 76 (+) In case of transfer Error, HAL_SMBUS_ErrorCallback() function is executed and user can
mbed_official 610:813dcc80987e 77 add his own code by customization of function pointer HAL_SMBUS_ErrorCallback()
mbed_official 610:813dcc80987e 78 to check the Error Code using function HAL_SMBUS_GetError()
mbed_official 610:813dcc80987e 79
mbed_official 610:813dcc80987e 80 *** SMBUS HAL driver macros list ***
mbed_official 610:813dcc80987e 81 ==================================
mbed_official 610:813dcc80987e 82 [..]
mbed_official 610:813dcc80987e 83 Below the list of most used macros in SMBUS HAL driver.
mbed_official 610:813dcc80987e 84
mbed_official 610:813dcc80987e 85 (+) __HAL_SMBUS_ENABLE: Enable the SMBUS peripheral
mbed_official 610:813dcc80987e 86 (+) __HAL_SMBUS_DISABLE: Disable the SMBUS peripheral
mbed_official 610:813dcc80987e 87 (+) __HAL_SMBUS_GET_FLAG : Checks whether the specified SMBUS flag is set or not
mbed_official 610:813dcc80987e 88 (+) __HAL_SMBUS_CLEAR_FLAG : Clears the specified SMBUS pending flag
mbed_official 610:813dcc80987e 89 (+) __HAL_SMBUS_ENABLE_IT: Enables the specified SMBUS interrupt
mbed_official 610:813dcc80987e 90 (+) __HAL_SMBUS_DISABLE_IT: Disables the specified SMBUS interrupt
mbed_official 610:813dcc80987e 91
mbed_official 610:813dcc80987e 92 [..]
mbed_official 610:813dcc80987e 93 (@) You can refer to the SMBUS HAL driver header file for more useful macros
mbed_official 610:813dcc80987e 94
mbed_official 610:813dcc80987e 95
mbed_official 610:813dcc80987e 96 @endverbatim
mbed_official 610:813dcc80987e 97 ******************************************************************************
mbed_official 610:813dcc80987e 98 * @attention
mbed_official 610:813dcc80987e 99 *
mbed_official 610:813dcc80987e 100 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
mbed_official 610:813dcc80987e 101 *
mbed_official 610:813dcc80987e 102 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 610:813dcc80987e 103 * are permitted provided that the following conditions are met:
mbed_official 610:813dcc80987e 104 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 610:813dcc80987e 105 * this list of conditions and the following disclaimer.
mbed_official 610:813dcc80987e 106 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 610:813dcc80987e 107 * this list of conditions and the following disclaimer in the documentation
mbed_official 610:813dcc80987e 108 * and/or other materials provided with the distribution.
mbed_official 610:813dcc80987e 109 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 610:813dcc80987e 110 * may be used to endorse or promote products derived from this software
mbed_official 610:813dcc80987e 111 * without specific prior written permission.
mbed_official 610:813dcc80987e 112 *
mbed_official 610:813dcc80987e 113 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 610:813dcc80987e 114 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 610:813dcc80987e 115 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 610:813dcc80987e 116 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 610:813dcc80987e 117 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 610:813dcc80987e 118 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 610:813dcc80987e 119 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 610:813dcc80987e 120 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 610:813dcc80987e 121 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 610:813dcc80987e 122 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 610:813dcc80987e 123 *
mbed_official 610:813dcc80987e 124 ******************************************************************************
mbed_official 610:813dcc80987e 125 */
mbed_official 610:813dcc80987e 126
mbed_official 610:813dcc80987e 127 /* Includes ------------------------------------------------------------------*/
mbed_official 610:813dcc80987e 128 #include "stm32l4xx_hal.h"
mbed_official 610:813dcc80987e 129
mbed_official 610:813dcc80987e 130 /** @addtogroup STM32L4xx_HAL_Driver
mbed_official 610:813dcc80987e 131 * @{
mbed_official 610:813dcc80987e 132 */
mbed_official 610:813dcc80987e 133
mbed_official 610:813dcc80987e 134 /** @defgroup SMBUS SMBUS
mbed_official 610:813dcc80987e 135 * @brief SMBUS HAL module driver
mbed_official 610:813dcc80987e 136 * @{
mbed_official 610:813dcc80987e 137 */
mbed_official 610:813dcc80987e 138
mbed_official 610:813dcc80987e 139 #ifdef HAL_SMBUS_MODULE_ENABLED
mbed_official 610:813dcc80987e 140
mbed_official 610:813dcc80987e 141 /* Private typedef -----------------------------------------------------------*/
mbed_official 610:813dcc80987e 142 /* Private constants ---------------------------------------------------------*/
mbed_official 610:813dcc80987e 143 /** @defgroup SMBUS_Private_Define SMBUS Private Constants
mbed_official 610:813dcc80987e 144 * @{
mbed_official 610:813dcc80987e 145 */
mbed_official 610:813dcc80987e 146 #define TIMING_CLEAR_MASK ((uint32_t)0xF0FFFFFF) /*<! SMBUS TIMING clear register Mask */
mbed_official 610:813dcc80987e 147 #define HAL_TIMEOUT_ADDR ((uint32_t)10000) /* 10 s */
mbed_official 610:813dcc80987e 148 #define HAL_TIMEOUT_BUSY ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 149 #define HAL_TIMEOUT_DIR ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 150 #define HAL_TIMEOUT_RXNE ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 151 #define HAL_TIMEOUT_STOPF ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 152 #define HAL_TIMEOUT_TC ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 153 #define HAL_TIMEOUT_TCR ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 154 #define HAL_TIMEOUT_TXIS ((uint32_t)25) /* 25 ms */
mbed_official 610:813dcc80987e 155 #define MAX_NBYTE_SIZE 255
mbed_official 610:813dcc80987e 156 /**
mbed_official 610:813dcc80987e 157 * @}
mbed_official 610:813dcc80987e 158 */
mbed_official 610:813dcc80987e 159
mbed_official 610:813dcc80987e 160 /* Private macro -------------------------------------------------------------*/
mbed_official 610:813dcc80987e 161 /* Private variables ---------------------------------------------------------*/
mbed_official 610:813dcc80987e 162 /* Private function prototypes -----------------------------------------------*/
mbed_official 610:813dcc80987e 163 /** @addtogroup SMBUS_Private_Functions SMBUS Private Functions
mbed_official 610:813dcc80987e 164 * @{
mbed_official 610:813dcc80987e 165 */
mbed_official 610:813dcc80987e 166 static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(SMBUS_HandleTypeDef *hsmbus, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
mbed_official 610:813dcc80987e 167
mbed_official 610:813dcc80987e 168 static HAL_StatusTypeDef SMBUS_Enable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest);
mbed_official 610:813dcc80987e 169 static HAL_StatusTypeDef SMBUS_Disable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest);
mbed_official 610:813dcc80987e 170 static HAL_StatusTypeDef SMBUS_Master_ISR(SMBUS_HandleTypeDef *hsmbus);
mbed_official 610:813dcc80987e 171 static HAL_StatusTypeDef SMBUS_Slave_ISR(SMBUS_HandleTypeDef *hsmbus);
mbed_official 610:813dcc80987e 172
mbed_official 610:813dcc80987e 173 static void SMBUS_TransferConfig(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
mbed_official 610:813dcc80987e 174 /**
mbed_official 610:813dcc80987e 175 * @}
mbed_official 610:813dcc80987e 176 */
mbed_official 610:813dcc80987e 177
mbed_official 610:813dcc80987e 178 /* Exported functions --------------------------------------------------------*/
mbed_official 610:813dcc80987e 179
mbed_official 610:813dcc80987e 180 /** @defgroup SMBUS_Exported_Functions SMBUS Exported Functions
mbed_official 610:813dcc80987e 181 * @{
mbed_official 610:813dcc80987e 182 */
mbed_official 610:813dcc80987e 183
mbed_official 610:813dcc80987e 184 /** @defgroup SMBUS_Exported_Functions_Group1 Initialization and de-initialization functions
mbed_official 610:813dcc80987e 185 * @brief Initialization and Configuration functions
mbed_official 610:813dcc80987e 186 *
mbed_official 610:813dcc80987e 187 @verbatim
mbed_official 610:813dcc80987e 188 ===============================================================================
mbed_official 610:813dcc80987e 189 ##### Initialization and de-initialization functions #####
mbed_official 610:813dcc80987e 190 ===============================================================================
mbed_official 610:813dcc80987e 191 [..] This subsection provides a set of functions allowing to initialize and
mbed_official 610:813dcc80987e 192 de-initialize the SMBUSx peripheral:
mbed_official 610:813dcc80987e 193
mbed_official 610:813dcc80987e 194 (+) User must Implement HAL_SMBUS_MspInit() function in which he configures
mbed_official 610:813dcc80987e 195 all related peripherals resources (CLOCK, GPIO, IT and NVIC ).
mbed_official 610:813dcc80987e 196
mbed_official 610:813dcc80987e 197 (+) Call the function HAL_SMBUS_Init() to configure the selected device with
mbed_official 610:813dcc80987e 198 the selected configuration:
mbed_official 610:813dcc80987e 199 (++) Clock Timing
mbed_official 610:813dcc80987e 200 (++) Bus Timeout
mbed_official 610:813dcc80987e 201 (++) Analog Filer mode
mbed_official 610:813dcc80987e 202 (++) Own Address 1
mbed_official 610:813dcc80987e 203 (++) Addressing mode (Master, Slave)
mbed_official 610:813dcc80987e 204 (++) Dual Addressing mode
mbed_official 610:813dcc80987e 205 (++) Own Address 2
mbed_official 610:813dcc80987e 206 (++) Own Address 2 Mask
mbed_official 610:813dcc80987e 207 (++) General call mode
mbed_official 610:813dcc80987e 208 (++) Nostretch mode
mbed_official 610:813dcc80987e 209 (++) Packet Error Check mode
mbed_official 610:813dcc80987e 210 (++) Peripheral mode
mbed_official 610:813dcc80987e 211
mbed_official 610:813dcc80987e 212
mbed_official 610:813dcc80987e 213 (+) Call the function HAL_SMBUS_DeInit() to restore the default configuration
mbed_official 610:813dcc80987e 214 of the selected SMBUSx peripheral.
mbed_official 610:813dcc80987e 215
mbed_official 610:813dcc80987e 216 @endverbatim
mbed_official 610:813dcc80987e 217 * @{
mbed_official 610:813dcc80987e 218 */
mbed_official 610:813dcc80987e 219
mbed_official 610:813dcc80987e 220 /**
mbed_official 610:813dcc80987e 221 * @brief Initialize the SMBUS according to the specified parameters
mbed_official 610:813dcc80987e 222 * in the SMBUS_InitTypeDef and initialize the associated handle.
mbed_official 610:813dcc80987e 223 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 224 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 225 * @retval HAL status
mbed_official 610:813dcc80987e 226 */
mbed_official 610:813dcc80987e 227 HAL_StatusTypeDef HAL_SMBUS_Init(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 228 {
mbed_official 610:813dcc80987e 229 /* Check the SMBUS handle allocation */
mbed_official 610:813dcc80987e 230 if(hsmbus == NULL)
mbed_official 610:813dcc80987e 231 {
mbed_official 610:813dcc80987e 232 return HAL_ERROR;
mbed_official 610:813dcc80987e 233 }
mbed_official 610:813dcc80987e 234
mbed_official 610:813dcc80987e 235 /* Check the parameters */
mbed_official 610:813dcc80987e 236 assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
mbed_official 610:813dcc80987e 237 assert_param(IS_SMBUS_ANALOG_FILTER(hsmbus->Init.AnalogFilter));
mbed_official 610:813dcc80987e 238 assert_param(IS_SMBUS_OWN_ADDRESS1(hsmbus->Init.OwnAddress1));
mbed_official 610:813dcc80987e 239 assert_param(IS_SMBUS_ADDRESSING_MODE(hsmbus->Init.AddressingMode));
mbed_official 610:813dcc80987e 240 assert_param(IS_SMBUS_DUAL_ADDRESS(hsmbus->Init.DualAddressMode));
mbed_official 610:813dcc80987e 241 assert_param(IS_SMBUS_OWN_ADDRESS2(hsmbus->Init.OwnAddress2));
mbed_official 610:813dcc80987e 242 assert_param(IS_SMBUS_OWN_ADDRESS2_MASK(hsmbus->Init.OwnAddress2Masks));
mbed_official 610:813dcc80987e 243 assert_param(IS_SMBUS_GENERAL_CALL(hsmbus->Init.GeneralCallMode));
mbed_official 610:813dcc80987e 244 assert_param(IS_SMBUS_NO_STRETCH(hsmbus->Init.NoStretchMode));
mbed_official 610:813dcc80987e 245 assert_param(IS_SMBUS_PEC(hsmbus->Init.PacketErrorCheckMode));
mbed_official 610:813dcc80987e 246 assert_param(IS_SMBUS_PERIPHERAL_MODE(hsmbus->Init.PeripheralMode));
mbed_official 610:813dcc80987e 247
mbed_official 610:813dcc80987e 248 if(hsmbus->State == HAL_SMBUS_STATE_RESET)
mbed_official 610:813dcc80987e 249 {
mbed_official 610:813dcc80987e 250 /* Allocate lock resource and initialize it */
mbed_official 610:813dcc80987e 251 hsmbus->Lock = HAL_UNLOCKED;
mbed_official 610:813dcc80987e 252
mbed_official 610:813dcc80987e 253 /* Init the low level hardware : GPIO, CLOCK, NVIC */
mbed_official 610:813dcc80987e 254 HAL_SMBUS_MspInit(hsmbus);
mbed_official 610:813dcc80987e 255 }
mbed_official 610:813dcc80987e 256
mbed_official 610:813dcc80987e 257 hsmbus->State = HAL_SMBUS_STATE_BUSY;
mbed_official 610:813dcc80987e 258
mbed_official 610:813dcc80987e 259 /* Disable the selected SMBUS peripheral */
mbed_official 610:813dcc80987e 260 __HAL_SMBUS_DISABLE(hsmbus);
mbed_official 610:813dcc80987e 261
mbed_official 610:813dcc80987e 262 /*---------------------------- SMBUSx TIMINGR Configuration ------------------------*/
mbed_official 610:813dcc80987e 263 /* Configure SMBUSx: Frequency range */
mbed_official 610:813dcc80987e 264 hsmbus->Instance->TIMINGR = hsmbus->Init.Timing & TIMING_CLEAR_MASK;
mbed_official 610:813dcc80987e 265
mbed_official 610:813dcc80987e 266 /*---------------------------- SMBUSx TIMEOUTR Configuration ------------------------*/
mbed_official 610:813dcc80987e 267 /* Configure SMBUSx: Bus Timeout */
mbed_official 610:813dcc80987e 268 hsmbus->Instance->TIMEOUTR &= ~I2C_TIMEOUTR_TIMOUTEN;
mbed_official 610:813dcc80987e 269 hsmbus->Instance->TIMEOUTR &= ~I2C_TIMEOUTR_TEXTEN;
mbed_official 610:813dcc80987e 270 hsmbus->Instance->TIMEOUTR = hsmbus->Init.SMBusTimeout;
mbed_official 610:813dcc80987e 271
mbed_official 610:813dcc80987e 272 /*---------------------------- SMBUSx OAR1 Configuration -----------------------*/
mbed_official 610:813dcc80987e 273 /* Configure SMBUSx: Own Address1 and ack own address1 mode */
mbed_official 610:813dcc80987e 274 hsmbus->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
mbed_official 610:813dcc80987e 275
mbed_official 610:813dcc80987e 276 if(hsmbus->Init.OwnAddress1 != 0)
mbed_official 610:813dcc80987e 277 {
mbed_official 610:813dcc80987e 278 if(hsmbus->Init.AddressingMode == SMBUS_ADDRESSINGMODE_7BIT)
mbed_official 610:813dcc80987e 279 {
mbed_official 610:813dcc80987e 280 hsmbus->Instance->OAR1 = (I2C_OAR1_OA1EN | hsmbus->Init.OwnAddress1);
mbed_official 610:813dcc80987e 281 }
mbed_official 610:813dcc80987e 282 else /* SMBUS_ADDRESSINGMODE_10BIT */
mbed_official 610:813dcc80987e 283 {
mbed_official 610:813dcc80987e 284 hsmbus->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hsmbus->Init.OwnAddress1);
mbed_official 610:813dcc80987e 285 }
mbed_official 610:813dcc80987e 286 }
mbed_official 610:813dcc80987e 287
mbed_official 610:813dcc80987e 288 /*---------------------------- SMBUSx CR2 Configuration ------------------------*/
mbed_official 610:813dcc80987e 289 /* Configure SMBUSx: Addressing Master mode */
mbed_official 610:813dcc80987e 290 if(hsmbus->Init.AddressingMode == SMBUS_ADDRESSINGMODE_10BIT)
mbed_official 610:813dcc80987e 291 {
mbed_official 610:813dcc80987e 292 hsmbus->Instance->CR2 = (I2C_CR2_ADD10);
mbed_official 610:813dcc80987e 293 }
mbed_official 610:813dcc80987e 294 /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process) */
mbed_official 610:813dcc80987e 295 /* AUTOEND and NACK bit will be manage during Transfer process */
mbed_official 610:813dcc80987e 296 hsmbus->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
mbed_official 610:813dcc80987e 297
mbed_official 610:813dcc80987e 298 /*---------------------------- SMBUSx OAR2 Configuration -----------------------*/
mbed_official 610:813dcc80987e 299 /* Configure SMBUSx: Dual mode and Own Address2 */
mbed_official 610:813dcc80987e 300 hsmbus->Instance->OAR2 = (hsmbus->Init.DualAddressMode | hsmbus->Init.OwnAddress2 | (hsmbus->Init.OwnAddress2Masks << 8));
mbed_official 610:813dcc80987e 301
mbed_official 610:813dcc80987e 302 /*---------------------------- SMBUSx CR1 Configuration ------------------------*/
mbed_official 610:813dcc80987e 303 /* Configure SMBUSx: Generalcall and NoStretch mode */
mbed_official 610:813dcc80987e 304 hsmbus->Instance->CR1 = (hsmbus->Init.GeneralCallMode | hsmbus->Init.NoStretchMode | hsmbus->Init.PacketErrorCheckMode | hsmbus->Init.PeripheralMode | hsmbus->Init.AnalogFilter);
mbed_official 610:813dcc80987e 305
mbed_official 610:813dcc80987e 306 /* Enable Slave Byte Control only in case of Packet Error Check is enabled and SMBUS Peripheral is set in Slave mode */
mbed_official 610:813dcc80987e 307 if( (hsmbus->Init.PacketErrorCheckMode == SMBUS_PEC_ENABLE)
mbed_official 610:813dcc80987e 308 && ( (hsmbus->Init.PeripheralMode == SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE) || (hsmbus->Init.PeripheralMode == SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE_ARP) ) )
mbed_official 610:813dcc80987e 309 {
mbed_official 610:813dcc80987e 310 hsmbus->Instance->CR1 |= I2C_CR1_SBC;
mbed_official 610:813dcc80987e 311 }
mbed_official 610:813dcc80987e 312
mbed_official 610:813dcc80987e 313 /* Enable the selected SMBUS peripheral */
mbed_official 610:813dcc80987e 314 __HAL_SMBUS_ENABLE(hsmbus);
mbed_official 610:813dcc80987e 315
mbed_official 610:813dcc80987e 316 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 317 hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 318 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 319
mbed_official 610:813dcc80987e 320 return HAL_OK;
mbed_official 610:813dcc80987e 321 }
mbed_official 610:813dcc80987e 322
mbed_official 610:813dcc80987e 323 /**
mbed_official 610:813dcc80987e 324 * @brief DeInitialize the SMBUS peripheral.
mbed_official 610:813dcc80987e 325 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 326 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 327 * @retval HAL status
mbed_official 610:813dcc80987e 328 */
mbed_official 610:813dcc80987e 329 HAL_StatusTypeDef HAL_SMBUS_DeInit(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 330 {
mbed_official 610:813dcc80987e 331 /* Check the SMBUS handle allocation */
mbed_official 610:813dcc80987e 332 if(hsmbus == NULL)
mbed_official 610:813dcc80987e 333 {
mbed_official 610:813dcc80987e 334 return HAL_ERROR;
mbed_official 610:813dcc80987e 335 }
mbed_official 610:813dcc80987e 336
mbed_official 610:813dcc80987e 337 /* Check the parameters */
mbed_official 610:813dcc80987e 338 assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
mbed_official 610:813dcc80987e 339
mbed_official 610:813dcc80987e 340 hsmbus->State = HAL_SMBUS_STATE_BUSY;
mbed_official 610:813dcc80987e 341
mbed_official 610:813dcc80987e 342 /* Disable the SMBUS Peripheral Clock */
mbed_official 610:813dcc80987e 343 __HAL_SMBUS_DISABLE(hsmbus);
mbed_official 610:813dcc80987e 344
mbed_official 610:813dcc80987e 345 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
mbed_official 610:813dcc80987e 346 HAL_SMBUS_MspDeInit(hsmbus);
mbed_official 610:813dcc80987e 347
mbed_official 610:813dcc80987e 348 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 349 hsmbus->PreviousState = HAL_SMBUS_STATE_RESET;
mbed_official 610:813dcc80987e 350 hsmbus->State = HAL_SMBUS_STATE_RESET;
mbed_official 610:813dcc80987e 351
mbed_official 610:813dcc80987e 352 /* Release Lock */
mbed_official 610:813dcc80987e 353 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 354
mbed_official 610:813dcc80987e 355 return HAL_OK;
mbed_official 610:813dcc80987e 356 }
mbed_official 610:813dcc80987e 357
mbed_official 610:813dcc80987e 358 /**
mbed_official 610:813dcc80987e 359 * @brief Initialize the SMBUS MSP.
mbed_official 610:813dcc80987e 360 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 361 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 362 * @retval None
mbed_official 610:813dcc80987e 363 */
mbed_official 610:813dcc80987e 364 __weak void HAL_SMBUS_MspInit(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 365 {
mbed_official 610:813dcc80987e 366 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 367 the HAL_SMBUS_MspInit could be implemented in the user file
mbed_official 610:813dcc80987e 368 */
mbed_official 610:813dcc80987e 369 }
mbed_official 610:813dcc80987e 370
mbed_official 610:813dcc80987e 371 /**
mbed_official 610:813dcc80987e 372 * @brief DeInitialize the SMBUS MSP.
mbed_official 610:813dcc80987e 373 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 374 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 375 * @retval None
mbed_official 610:813dcc80987e 376 */
mbed_official 610:813dcc80987e 377 __weak void HAL_SMBUS_MspDeInit(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 378 {
mbed_official 610:813dcc80987e 379 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 380 the HAL_SMBUS_MspDeInit could be implemented in the user file
mbed_official 610:813dcc80987e 381 */
mbed_official 610:813dcc80987e 382 }
mbed_official 610:813dcc80987e 383
mbed_official 610:813dcc80987e 384 /**
mbed_official 610:813dcc80987e 385 * @}
mbed_official 610:813dcc80987e 386 */
mbed_official 610:813dcc80987e 387
mbed_official 610:813dcc80987e 388 /** @defgroup SMBUS_Exported_Functions_Group2 Input and Output operation functions
mbed_official 610:813dcc80987e 389 * @brief Data transfers functions
mbed_official 610:813dcc80987e 390 *
mbed_official 610:813dcc80987e 391 @verbatim
mbed_official 610:813dcc80987e 392 ===============================================================================
mbed_official 610:813dcc80987e 393 ##### IO operation functions #####
mbed_official 610:813dcc80987e 394 ===============================================================================
mbed_official 610:813dcc80987e 395 [..]
mbed_official 610:813dcc80987e 396 This subsection provides a set of functions allowing to manage the SMBUS data
mbed_official 610:813dcc80987e 397 transfers.
mbed_official 610:813dcc80987e 398
mbed_official 610:813dcc80987e 399 (#) Blocking mode function to check if device is ready for usage is :
mbed_official 610:813dcc80987e 400 (++) HAL_SMBUS_IsDeviceReady()
mbed_official 610:813dcc80987e 401
mbed_official 610:813dcc80987e 402 (#) There is only one mode of transfer:
mbed_official 610:813dcc80987e 403 (++) No-Blocking mode : The communication is performed using Interrupts.
mbed_official 610:813dcc80987e 404 These functions return the status of the transfer startup.
mbed_official 610:813dcc80987e 405 The end of the data processing will be indicated through the
mbed_official 610:813dcc80987e 406 dedicated SMBUS IRQ when using Interrupt mode.
mbed_official 610:813dcc80987e 407
mbed_official 610:813dcc80987e 408 (#) No-Blocking mode functions with Interrupt are :
mbed_official 610:813dcc80987e 409 (++) HAL_SMBUS_Master_Transmit_IT()
mbed_official 610:813dcc80987e 410 (++) HAL_SMBUS_Master_Receive_IT()
mbed_official 610:813dcc80987e 411 (++) HAL_SMBUS_Slave_Transmit_IT()
mbed_official 610:813dcc80987e 412 (++) HAL_SMBUS_Slave_Receive_IT()
mbed_official 610:813dcc80987e 413 (++) HAL_SMBUS_EnableListen_IT() or alias HAL_SMBUS_EnableListen_IT()
mbed_official 610:813dcc80987e 414 (++) HAL_SMBUS_DisableListen_IT()
mbed_official 610:813dcc80987e 415 (++) HAL_SMBUS_EnableAlert_IT()
mbed_official 610:813dcc80987e 416 (++) HAL_SMBUS_DisableAlert_IT()
mbed_official 610:813dcc80987e 417
mbed_official 610:813dcc80987e 418 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
mbed_official 610:813dcc80987e 419 (++) HAL_SMBUS_MasterTxCpltCallback()
mbed_official 610:813dcc80987e 420 (++) HAL_SMBUS_MasterRxCpltCallback()
mbed_official 610:813dcc80987e 421 (++) HAL_SMBUS_SlaveTxCpltCallback()
mbed_official 610:813dcc80987e 422 (++) HAL_SMBUS_SlaveRxCpltCallback()
mbed_official 610:813dcc80987e 423 (++) HAL_SMBUS_AddrCallback()
mbed_official 610:813dcc80987e 424 (++) HAL_SMBUS_ListenCpltCallback()
mbed_official 610:813dcc80987e 425 (++) HAL_SMBUS_ErrorCallback()
mbed_official 610:813dcc80987e 426
mbed_official 610:813dcc80987e 427 @endverbatim
mbed_official 610:813dcc80987e 428 * @{
mbed_official 610:813dcc80987e 429 */
mbed_official 610:813dcc80987e 430
mbed_official 610:813dcc80987e 431 /**
mbed_official 610:813dcc80987e 432 * @brief Transmit in master/host SMBUS mode an amount of data in non-blocking mode with Interrupt.
mbed_official 610:813dcc80987e 433 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 434 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 435 * @param DevAddress: Target device address
mbed_official 610:813dcc80987e 436 * @param pData: Pointer to data buffer
mbed_official 610:813dcc80987e 437 * @param Size: Amount of data to be sent
mbed_official 610:813dcc80987e 438 * @param XferOptions: Options of Transfer, value of @ref SMBUS_XferOptions_definition
mbed_official 610:813dcc80987e 439 * @retval HAL status
mbed_official 610:813dcc80987e 440 */
mbed_official 610:813dcc80987e 441 HAL_StatusTypeDef HAL_SMBUS_Master_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
mbed_official 610:813dcc80987e 442 {
mbed_official 610:813dcc80987e 443 /* Check the parameters */
mbed_official 610:813dcc80987e 444 assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
mbed_official 610:813dcc80987e 445
mbed_official 610:813dcc80987e 446 if(hsmbus->State == HAL_SMBUS_STATE_READY)
mbed_official 610:813dcc80987e 447 {
mbed_official 610:813dcc80987e 448 /* Process Locked */
mbed_official 610:813dcc80987e 449 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 450
mbed_official 610:813dcc80987e 451 hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_TX;
mbed_official 610:813dcc80987e 452 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 453 /* Prepare transfer parameters */
mbed_official 610:813dcc80987e 454 hsmbus->pBuffPtr = pData;
mbed_official 610:813dcc80987e 455 hsmbus->XferCount = Size;
mbed_official 610:813dcc80987e 456 hsmbus->XferOptions = XferOptions;
mbed_official 610:813dcc80987e 457
mbed_official 610:813dcc80987e 458 /* In case of Quick command, remove autoend mode */
mbed_official 610:813dcc80987e 459 /* Manage the stop generation by software */
mbed_official 610:813dcc80987e 460 if(hsmbus->pBuffPtr == NULL)
mbed_official 610:813dcc80987e 461 {
mbed_official 610:813dcc80987e 462 hsmbus->XferOptions &= ~SMBUS_AUTOEND_MODE;
mbed_official 610:813dcc80987e 463 }
mbed_official 610:813dcc80987e 464
mbed_official 610:813dcc80987e 465 if(Size > MAX_NBYTE_SIZE)
mbed_official 610:813dcc80987e 466 {
mbed_official 610:813dcc80987e 467 hsmbus->XferSize = MAX_NBYTE_SIZE;
mbed_official 610:813dcc80987e 468 }
mbed_official 610:813dcc80987e 469 else
mbed_official 610:813dcc80987e 470 {
mbed_official 610:813dcc80987e 471 hsmbus->XferSize = Size;
mbed_official 610:813dcc80987e 472 }
mbed_official 610:813dcc80987e 473
mbed_official 610:813dcc80987e 474 /* Send Slave Address */
mbed_official 610:813dcc80987e 475 /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
mbed_official 610:813dcc80987e 476 if( (hsmbus->XferSize == MAX_NBYTE_SIZE) && (hsmbus->XferSize < hsmbus->XferCount) )
mbed_official 610:813dcc80987e 477 {
mbed_official 610:813dcc80987e 478 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_GENERATE_START_WRITE);
mbed_official 610:813dcc80987e 479 }
mbed_official 610:813dcc80987e 480 else
mbed_official 610:813dcc80987e 481 {
mbed_official 610:813dcc80987e 482 /* If transfer direction not change, do not generate Restart Condition */
mbed_official 610:813dcc80987e 483 /* Mean Previous state is same as current state */
mbed_official 610:813dcc80987e 484 if(hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 485 {
mbed_official 610:813dcc80987e 486 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 487 }
mbed_official 610:813dcc80987e 488 /* Else transfer direction change, so generate Restart with new transfer direction */
mbed_official 610:813dcc80987e 489 else
mbed_official 610:813dcc80987e 490 {
mbed_official 610:813dcc80987e 491 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_GENERATE_START_WRITE);
mbed_official 610:813dcc80987e 492 }
mbed_official 610:813dcc80987e 493
mbed_official 610:813dcc80987e 494 /* If PEC mode is enable, size to transmit manage by SW part should be Size-1 byte, corresponding to PEC byte */
mbed_official 610:813dcc80987e 495 /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
mbed_official 610:813dcc80987e 496 if(SMBUS_GET_PEC_MODE(hsmbus) != RESET)
mbed_official 610:813dcc80987e 497 {
mbed_official 610:813dcc80987e 498 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 499 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 500 }
mbed_official 610:813dcc80987e 501 }
mbed_official 610:813dcc80987e 502
mbed_official 610:813dcc80987e 503 /* Process Unlocked */
mbed_official 610:813dcc80987e 504 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 505
mbed_official 610:813dcc80987e 506 /* Note : The SMBUS interrupts must be enabled after unlocking current process
mbed_official 610:813dcc80987e 507 to avoid the risk of SMBUS interrupt handle execution before current
mbed_official 610:813dcc80987e 508 process unlock */
mbed_official 610:813dcc80987e 509 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX);
mbed_official 610:813dcc80987e 510
mbed_official 610:813dcc80987e 511 return HAL_OK;
mbed_official 610:813dcc80987e 512 }
mbed_official 610:813dcc80987e 513 else
mbed_official 610:813dcc80987e 514 {
mbed_official 610:813dcc80987e 515 return HAL_BUSY;
mbed_official 610:813dcc80987e 516 }
mbed_official 610:813dcc80987e 517 }
mbed_official 610:813dcc80987e 518
mbed_official 610:813dcc80987e 519 /**
mbed_official 610:813dcc80987e 520 * @brief Receive in master/host SMBUS mode an amount of data in non-blocking mode with Interrupt.
mbed_official 610:813dcc80987e 521 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 522 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 523 * @param DevAddress: Target device address
mbed_official 610:813dcc80987e 524 * @param pData: Pointer to data buffer
mbed_official 610:813dcc80987e 525 * @param Size: Amount of data to be sent
mbed_official 610:813dcc80987e 526 * @param XferOptions: Options of Transfer, value of @ref SMBUS_XferOptions_definition
mbed_official 610:813dcc80987e 527 * @retval HAL status
mbed_official 610:813dcc80987e 528 */
mbed_official 610:813dcc80987e 529 HAL_StatusTypeDef HAL_SMBUS_Master_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
mbed_official 610:813dcc80987e 530 {
mbed_official 610:813dcc80987e 531 /* Check the parameters */
mbed_official 610:813dcc80987e 532 assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
mbed_official 610:813dcc80987e 533
mbed_official 610:813dcc80987e 534 if(hsmbus->State == HAL_SMBUS_STATE_READY)
mbed_official 610:813dcc80987e 535 {
mbed_official 610:813dcc80987e 536 /* Process Locked */
mbed_official 610:813dcc80987e 537 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 538
mbed_official 610:813dcc80987e 539 hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_RX;
mbed_official 610:813dcc80987e 540 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 541
mbed_official 610:813dcc80987e 542 /* Prepare transfer parameters */
mbed_official 610:813dcc80987e 543 hsmbus->pBuffPtr = pData;
mbed_official 610:813dcc80987e 544 hsmbus->XferCount = Size;
mbed_official 610:813dcc80987e 545 hsmbus->XferOptions = XferOptions;
mbed_official 610:813dcc80987e 546
mbed_official 610:813dcc80987e 547 /* In case of Quick command, remove autoend mode */
mbed_official 610:813dcc80987e 548 /* Manage the stop generation by software */
mbed_official 610:813dcc80987e 549 if(hsmbus->pBuffPtr == NULL)
mbed_official 610:813dcc80987e 550 {
mbed_official 610:813dcc80987e 551 hsmbus->XferOptions &= ~SMBUS_AUTOEND_MODE;
mbed_official 610:813dcc80987e 552 }
mbed_official 610:813dcc80987e 553
mbed_official 610:813dcc80987e 554 if(Size > MAX_NBYTE_SIZE)
mbed_official 610:813dcc80987e 555 {
mbed_official 610:813dcc80987e 556 hsmbus->XferSize = MAX_NBYTE_SIZE;
mbed_official 610:813dcc80987e 557 }
mbed_official 610:813dcc80987e 558 else
mbed_official 610:813dcc80987e 559 {
mbed_official 610:813dcc80987e 560 hsmbus->XferSize = Size;
mbed_official 610:813dcc80987e 561 }
mbed_official 610:813dcc80987e 562
mbed_official 610:813dcc80987e 563 /* Send Slave Address */
mbed_official 610:813dcc80987e 564 /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
mbed_official 610:813dcc80987e 565 if( (hsmbus->XferSize == MAX_NBYTE_SIZE) && (hsmbus->XferSize < hsmbus->XferCount) )
mbed_official 610:813dcc80987e 566 {
mbed_official 610:813dcc80987e 567 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_GENERATE_START_READ);
mbed_official 610:813dcc80987e 568 }
mbed_official 610:813dcc80987e 569 else
mbed_official 610:813dcc80987e 570 {
mbed_official 610:813dcc80987e 571 /* If transfer direction not change, do not generate Restart Condition */
mbed_official 610:813dcc80987e 572 /* Mean Previous state is same as current state */
mbed_official 610:813dcc80987e 573 if(hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 574 {
mbed_official 610:813dcc80987e 575 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 576 }
mbed_official 610:813dcc80987e 577 /* Else transfer direction change, so generate Restart with new transfer direction */
mbed_official 610:813dcc80987e 578 else
mbed_official 610:813dcc80987e 579 {
mbed_official 610:813dcc80987e 580 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_GENERATE_START_READ);
mbed_official 610:813dcc80987e 581 }
mbed_official 610:813dcc80987e 582 }
mbed_official 610:813dcc80987e 583
mbed_official 610:813dcc80987e 584 /* Process Unlocked */
mbed_official 610:813dcc80987e 585 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 586
mbed_official 610:813dcc80987e 587 /* Note : The SMBUS interrupts must be enabled after unlocking current process
mbed_official 610:813dcc80987e 588 to avoid the risk of SMBUS interrupt handle execution before current
mbed_official 610:813dcc80987e 589 process unlock */
mbed_official 610:813dcc80987e 590 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX);
mbed_official 610:813dcc80987e 591
mbed_official 610:813dcc80987e 592 return HAL_OK;
mbed_official 610:813dcc80987e 593 }
mbed_official 610:813dcc80987e 594 else
mbed_official 610:813dcc80987e 595 {
mbed_official 610:813dcc80987e 596 return HAL_BUSY;
mbed_official 610:813dcc80987e 597 }
mbed_official 610:813dcc80987e 598 }
mbed_official 610:813dcc80987e 599
mbed_official 610:813dcc80987e 600 /**
mbed_official 610:813dcc80987e 601 * @brief Abort a master/host SMBUS process communication with Interrupt.
mbed_official 610:813dcc80987e 602 * @note This abort can be called only if state is ready
mbed_official 610:813dcc80987e 603 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 604 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 605 * @param DevAddress: Target device address
mbed_official 610:813dcc80987e 606 * @retval HAL status
mbed_official 610:813dcc80987e 607 */
mbed_official 610:813dcc80987e 608 HAL_StatusTypeDef HAL_SMBUS_Master_Abort_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress)
mbed_official 610:813dcc80987e 609 {
mbed_official 610:813dcc80987e 610 if(hsmbus->State == HAL_SMBUS_STATE_READY)
mbed_official 610:813dcc80987e 611 {
mbed_official 610:813dcc80987e 612 /* Process Locked */
mbed_official 610:813dcc80987e 613 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 614
mbed_official 610:813dcc80987e 615 /* Keep the same state as previous */
mbed_official 610:813dcc80987e 616 /* to perform as well the call of the corresponding end of transfer callback */
mbed_official 610:813dcc80987e 617 if(hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 618 {
mbed_official 610:813dcc80987e 619 hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_TX;
mbed_official 610:813dcc80987e 620 }
mbed_official 610:813dcc80987e 621 else if(hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 622 {
mbed_official 610:813dcc80987e 623 hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_RX;
mbed_official 610:813dcc80987e 624 }
mbed_official 610:813dcc80987e 625 else
mbed_official 610:813dcc80987e 626 {
mbed_official 610:813dcc80987e 627 /* Wrong usage of abort function */
mbed_official 610:813dcc80987e 628 /* This function should be used only in case of abort monitored by master device */
mbed_official 610:813dcc80987e 629 return HAL_ERROR;
mbed_official 610:813dcc80987e 630 }
mbed_official 610:813dcc80987e 631 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 632
mbed_official 610:813dcc80987e 633 /* Set NBYTES to 1 to generate a dummy read on SMBUS peripheral */
mbed_official 610:813dcc80987e 634 /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
mbed_official 610:813dcc80987e 635 SMBUS_TransferConfig(hsmbus, DevAddress, 1, SMBUS_AUTOEND_MODE, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 636
mbed_official 610:813dcc80987e 637 /* Process Unlocked */
mbed_official 610:813dcc80987e 638 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 639
mbed_official 610:813dcc80987e 640 /* Note : The SMBUS interrupts must be enabled after unlocking current process
mbed_official 610:813dcc80987e 641 to avoid the risk of SMBUS interrupt handle execution before current
mbed_official 610:813dcc80987e 642 process unlock */
mbed_official 610:813dcc80987e 643 if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 644 {
mbed_official 610:813dcc80987e 645 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX);
mbed_official 610:813dcc80987e 646 }
mbed_official 610:813dcc80987e 647 else if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 648 {
mbed_official 610:813dcc80987e 649 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX);
mbed_official 610:813dcc80987e 650 }
mbed_official 610:813dcc80987e 651
mbed_official 610:813dcc80987e 652 return HAL_OK;
mbed_official 610:813dcc80987e 653 }
mbed_official 610:813dcc80987e 654 else
mbed_official 610:813dcc80987e 655 {
mbed_official 610:813dcc80987e 656 return HAL_BUSY;
mbed_official 610:813dcc80987e 657 }
mbed_official 610:813dcc80987e 658 }
mbed_official 610:813dcc80987e 659
mbed_official 610:813dcc80987e 660 /**
mbed_official 610:813dcc80987e 661 * @brief Transmit in slave/device SMBUS mode an amount of data in non-blocking mode with Interrupt.
mbed_official 610:813dcc80987e 662 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 663 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 664 * @param pData: Pointer to data buffer
mbed_official 610:813dcc80987e 665 * @param Size: Amount of data to be sent
mbed_official 610:813dcc80987e 666 * @param XferOptions: Options of Transfer, value of @ref SMBUS_XferOptions_definition
mbed_official 610:813dcc80987e 667 * @retval HAL status
mbed_official 610:813dcc80987e 668 */
mbed_official 610:813dcc80987e 669 HAL_StatusTypeDef HAL_SMBUS_Slave_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
mbed_official 610:813dcc80987e 670 {
mbed_official 610:813dcc80987e 671 /* Check the parameters */
mbed_official 610:813dcc80987e 672 assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
mbed_official 610:813dcc80987e 673
mbed_official 610:813dcc80987e 674 if(hsmbus->State == HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 675 {
mbed_official 610:813dcc80987e 676 if((pData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 677 {
mbed_official 610:813dcc80987e 678 return HAL_ERROR;
mbed_official 610:813dcc80987e 679 }
mbed_official 610:813dcc80987e 680
mbed_official 610:813dcc80987e 681 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
mbed_official 610:813dcc80987e 682 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR | SMBUS_IT_TX);
mbed_official 610:813dcc80987e 683
mbed_official 610:813dcc80987e 684 /* Process Locked */
mbed_official 610:813dcc80987e 685 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 686
mbed_official 610:813dcc80987e 687 hsmbus->State |= HAL_SMBUS_STATE_SLAVE_BUSY_TX;
mbed_official 610:813dcc80987e 688 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 689
mbed_official 610:813dcc80987e 690 /* Set SBC bit to manage Acknowledge at each bit */
mbed_official 610:813dcc80987e 691 hsmbus->Instance->CR1 |= I2C_CR1_SBC;
mbed_official 610:813dcc80987e 692
mbed_official 610:813dcc80987e 693 /* Enable Address Acknowledge */
mbed_official 610:813dcc80987e 694 hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
mbed_official 610:813dcc80987e 695
mbed_official 610:813dcc80987e 696 /* Prepare transfer parameters */
mbed_official 610:813dcc80987e 697 hsmbus->pBuffPtr = pData;
mbed_official 610:813dcc80987e 698 hsmbus->XferSize = Size;
mbed_official 610:813dcc80987e 699 hsmbus->XferCount = Size;
mbed_official 610:813dcc80987e 700 hsmbus->XferOptions = XferOptions;
mbed_official 610:813dcc80987e 701
mbed_official 610:813dcc80987e 702 if(Size > MAX_NBYTE_SIZE)
mbed_official 610:813dcc80987e 703 {
mbed_official 610:813dcc80987e 704 hsmbus->XferSize = MAX_NBYTE_SIZE;
mbed_official 610:813dcc80987e 705 }
mbed_official 610:813dcc80987e 706 else
mbed_official 610:813dcc80987e 707 {
mbed_official 610:813dcc80987e 708 hsmbus->XferSize = Size;
mbed_official 610:813dcc80987e 709 }
mbed_official 610:813dcc80987e 710
mbed_official 610:813dcc80987e 711 /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
mbed_official 610:813dcc80987e 712 if( (hsmbus->XferSize == MAX_NBYTE_SIZE) && (hsmbus->XferSize < hsmbus->XferCount) )
mbed_official 610:813dcc80987e 713 {
mbed_official 610:813dcc80987e 714 SMBUS_TransferConfig(hsmbus,0,hsmbus->XferSize, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 715 }
mbed_official 610:813dcc80987e 716 else
mbed_official 610:813dcc80987e 717 {
mbed_official 610:813dcc80987e 718 /* Set NBYTE to transmit */
mbed_official 610:813dcc80987e 719 SMBUS_TransferConfig(hsmbus,0,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 720
mbed_official 610:813dcc80987e 721 /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
mbed_official 610:813dcc80987e 722 /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
mbed_official 610:813dcc80987e 723 if(SMBUS_GET_PEC_MODE(hsmbus) != RESET)
mbed_official 610:813dcc80987e 724 {
mbed_official 610:813dcc80987e 725 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 726 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 727 }
mbed_official 610:813dcc80987e 728 }
mbed_official 610:813dcc80987e 729
mbed_official 610:813dcc80987e 730 /* Clear ADDR flag after prepare the transfer parameters */
mbed_official 610:813dcc80987e 731 /* This action will generate an acknowledge to the HOST */
mbed_official 610:813dcc80987e 732 __HAL_SMBUS_CLEAR_FLAG(hsmbus,SMBUS_FLAG_ADDR);
mbed_official 610:813dcc80987e 733
mbed_official 610:813dcc80987e 734 /* Process Unlocked */
mbed_official 610:813dcc80987e 735 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 736
mbed_official 610:813dcc80987e 737 /* Note : The SMBUS interrupts must be enabled after unlocking current process
mbed_official 610:813dcc80987e 738 to avoid the risk of SMBUS interrupt handle execution before current
mbed_official 610:813dcc80987e 739 process unlock */
mbed_official 610:813dcc80987e 740 /* REnable ADDR interrupt */
mbed_official 610:813dcc80987e 741 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX | SMBUS_IT_ADDR);
mbed_official 610:813dcc80987e 742
mbed_official 610:813dcc80987e 743 return HAL_OK;
mbed_official 610:813dcc80987e 744 }
mbed_official 610:813dcc80987e 745 else
mbed_official 610:813dcc80987e 746 {
mbed_official 610:813dcc80987e 747 return HAL_ERROR;
mbed_official 610:813dcc80987e 748 }
mbed_official 610:813dcc80987e 749 }
mbed_official 610:813dcc80987e 750
mbed_official 610:813dcc80987e 751 /**
mbed_official 610:813dcc80987e 752 * @brief Receive in slave/device SMBUS mode an amount of data in non-blocking mode with Interrupt.
mbed_official 610:813dcc80987e 753 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 754 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 755 * @param pData: Pointer to data buffer
mbed_official 610:813dcc80987e 756 * @param Size: Amount of data to be sent
mbed_official 610:813dcc80987e 757 * @param XferOptions: Options of Transfer, value of @ref SMBUS_XferOptions_definition
mbed_official 610:813dcc80987e 758 * @retval HAL status
mbed_official 610:813dcc80987e 759 */
mbed_official 610:813dcc80987e 760 HAL_StatusTypeDef HAL_SMBUS_Slave_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
mbed_official 610:813dcc80987e 761 {
mbed_official 610:813dcc80987e 762 /* Check the parameters */
mbed_official 610:813dcc80987e 763 assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
mbed_official 610:813dcc80987e 764
mbed_official 610:813dcc80987e 765 if(hsmbus->State == HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 766 {
mbed_official 610:813dcc80987e 767 if((pData == NULL) || (Size == 0))
mbed_official 610:813dcc80987e 768 {
mbed_official 610:813dcc80987e 769 return HAL_ERROR;
mbed_official 610:813dcc80987e 770 }
mbed_official 610:813dcc80987e 771
mbed_official 610:813dcc80987e 772 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
mbed_official 610:813dcc80987e 773 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR | SMBUS_IT_RX);
mbed_official 610:813dcc80987e 774
mbed_official 610:813dcc80987e 775 /* Process Locked */
mbed_official 610:813dcc80987e 776 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 777
mbed_official 610:813dcc80987e 778 hsmbus->State |= HAL_SMBUS_STATE_SLAVE_BUSY_RX;
mbed_official 610:813dcc80987e 779 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 780
mbed_official 610:813dcc80987e 781 /* Set SBC bit to manage Acknowledge at each bit */
mbed_official 610:813dcc80987e 782 hsmbus->Instance->CR1 |= I2C_CR1_SBC;
mbed_official 610:813dcc80987e 783
mbed_official 610:813dcc80987e 784 /* Enable Address Acknowledge */
mbed_official 610:813dcc80987e 785 hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
mbed_official 610:813dcc80987e 786
mbed_official 610:813dcc80987e 787 /* Prepare transfer parameters */
mbed_official 610:813dcc80987e 788 hsmbus->pBuffPtr = pData;
mbed_official 610:813dcc80987e 789 hsmbus->XferSize = Size;
mbed_official 610:813dcc80987e 790 hsmbus->XferCount = Size;
mbed_official 610:813dcc80987e 791 hsmbus->XferOptions = XferOptions;
mbed_official 610:813dcc80987e 792
mbed_official 610:813dcc80987e 793 /* Set NBYTE to receive */
mbed_official 610:813dcc80987e 794 /* If XferSize equal "1", or XferSize equal "2" with PEC requested (mean 1 data byte + 1 PEC byte */
mbed_official 610:813dcc80987e 795 /* no need to set RELOAD bit mode, a ACK will be automatically generated in that case */
mbed_official 610:813dcc80987e 796 /* else need to set RELOAD bit mode to generate an automatic ACK at each byte Received */
mbed_official 610:813dcc80987e 797 /* This RELOAD bit will be reset for last BYTE to be receive in SMBUS_Slave_ISR */
mbed_official 610:813dcc80987e 798 if((hsmbus->XferSize == 1) || ((hsmbus->XferSize == 2) && (SMBUS_GET_PEC_MODE(hsmbus) != RESET)))
mbed_official 610:813dcc80987e 799 {
mbed_official 610:813dcc80987e 800 SMBUS_TransferConfig(hsmbus,0,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 801 }
mbed_official 610:813dcc80987e 802 else
mbed_official 610:813dcc80987e 803 {
mbed_official 610:813dcc80987e 804 SMBUS_TransferConfig(hsmbus,0, 1, hsmbus->XferOptions | SMBUS_RELOAD_MODE, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 805 }
mbed_official 610:813dcc80987e 806
mbed_official 610:813dcc80987e 807 /* Clear ADDR flag after prepare the transfer parameters */
mbed_official 610:813dcc80987e 808 /* This action will generate an acknowledge to the HOST */
mbed_official 610:813dcc80987e 809 __HAL_SMBUS_CLEAR_FLAG(hsmbus,SMBUS_FLAG_ADDR);
mbed_official 610:813dcc80987e 810
mbed_official 610:813dcc80987e 811 /* Process Unlocked */
mbed_official 610:813dcc80987e 812 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 813
mbed_official 610:813dcc80987e 814 /* Note : The SMBUS interrupts must be enabled after unlocking current process
mbed_official 610:813dcc80987e 815 to avoid the risk of SMBUS interrupt handle execution before current
mbed_official 610:813dcc80987e 816 process unlock */
mbed_official 610:813dcc80987e 817 /* REnable ADDR interrupt */
mbed_official 610:813dcc80987e 818 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_ADDR);
mbed_official 610:813dcc80987e 819
mbed_official 610:813dcc80987e 820 return HAL_OK;
mbed_official 610:813dcc80987e 821 }
mbed_official 610:813dcc80987e 822 else
mbed_official 610:813dcc80987e 823 {
mbed_official 610:813dcc80987e 824 return HAL_ERROR;
mbed_official 610:813dcc80987e 825 }
mbed_official 610:813dcc80987e 826 }
mbed_official 610:813dcc80987e 827
mbed_official 610:813dcc80987e 828 /**
mbed_official 610:813dcc80987e 829 * @brief Enable the Address listen mode with Interrupt.
mbed_official 610:813dcc80987e 830 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 831 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 832 * @retval HAL status
mbed_official 610:813dcc80987e 833 */
mbed_official 610:813dcc80987e 834 HAL_StatusTypeDef HAL_SMBUS_EnableListen_IT(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 835 {
mbed_official 610:813dcc80987e 836 hsmbus->State = HAL_SMBUS_STATE_LISTEN;
mbed_official 610:813dcc80987e 837
mbed_official 610:813dcc80987e 838 /* Enable the Address Match interrupt */
mbed_official 610:813dcc80987e 839 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_ADDR);
mbed_official 610:813dcc80987e 840
mbed_official 610:813dcc80987e 841 return HAL_OK;
mbed_official 610:813dcc80987e 842 }
mbed_official 610:813dcc80987e 843
mbed_official 610:813dcc80987e 844 /**
mbed_official 610:813dcc80987e 845 * @brief Disable the Address listen mode with Interrupt.
mbed_official 610:813dcc80987e 846 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 847 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 848 * @retval HAL status
mbed_official 610:813dcc80987e 849 */
mbed_official 610:813dcc80987e 850 HAL_StatusTypeDef HAL_SMBUS_DisableListen_IT(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 851 {
mbed_official 610:813dcc80987e 852 /* Disable Address listen mode only if a transfer is not ongoing */
mbed_official 610:813dcc80987e 853 if(hsmbus->State == HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 854 {
mbed_official 610:813dcc80987e 855 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 856
mbed_official 610:813dcc80987e 857 /* Disable the Address Match interrupt */
mbed_official 610:813dcc80987e 858 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR);
mbed_official 610:813dcc80987e 859
mbed_official 610:813dcc80987e 860 return HAL_OK;
mbed_official 610:813dcc80987e 861 }
mbed_official 610:813dcc80987e 862 else
mbed_official 610:813dcc80987e 863 {
mbed_official 610:813dcc80987e 864 return HAL_BUSY;
mbed_official 610:813dcc80987e 865 }
mbed_official 610:813dcc80987e 866 }
mbed_official 610:813dcc80987e 867
mbed_official 610:813dcc80987e 868 /**
mbed_official 610:813dcc80987e 869 * @brief Enable the SMBUS alert mode with Interrupt.
mbed_official 610:813dcc80987e 870 * @param hsmbus : pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 871 * the configuration information for the specified SMBUSx peripheral.
mbed_official 610:813dcc80987e 872 * @retval HAL status
mbed_official 610:813dcc80987e 873 */
mbed_official 610:813dcc80987e 874 HAL_StatusTypeDef HAL_SMBUS_EnableAlert_IT(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 875 {
mbed_official 610:813dcc80987e 876 /* Enable SMBus alert */
mbed_official 610:813dcc80987e 877 hsmbus->Instance->CR1 |= I2C_CR1_ALERTEN;
mbed_official 610:813dcc80987e 878
mbed_official 610:813dcc80987e 879 /* Clear ALERT flag */
mbed_official 610:813dcc80987e 880 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ALERT);
mbed_official 610:813dcc80987e 881
mbed_official 610:813dcc80987e 882 /* Enable Alert Interrupt */
mbed_official 610:813dcc80987e 883 SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_ALERT);
mbed_official 610:813dcc80987e 884
mbed_official 610:813dcc80987e 885 return HAL_OK;
mbed_official 610:813dcc80987e 886 }
mbed_official 610:813dcc80987e 887 /**
mbed_official 610:813dcc80987e 888 * @brief Disable the SMBUS alert mode with Interrupt.
mbed_official 610:813dcc80987e 889 * @param hsmbus : pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 890 * the configuration information for the specified SMBUSx peripheral.
mbed_official 610:813dcc80987e 891 * @retval HAL status
mbed_official 610:813dcc80987e 892 */
mbed_official 610:813dcc80987e 893 HAL_StatusTypeDef HAL_SMBUS_DisableAlert_IT(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 894 {
mbed_official 610:813dcc80987e 895 /* Enable SMBus alert */
mbed_official 610:813dcc80987e 896 hsmbus->Instance->CR1 &= ~I2C_CR1_ALERTEN;
mbed_official 610:813dcc80987e 897
mbed_official 610:813dcc80987e 898 /* Disable Alert Interrupt */
mbed_official 610:813dcc80987e 899 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ALERT);
mbed_official 610:813dcc80987e 900
mbed_official 610:813dcc80987e 901 return HAL_OK;
mbed_official 610:813dcc80987e 902 }
mbed_official 610:813dcc80987e 903
mbed_official 610:813dcc80987e 904 /**
mbed_official 610:813dcc80987e 905 * @brief Check if target device is ready for communication.
mbed_official 610:813dcc80987e 906 * @note This function is used with Memory devices
mbed_official 610:813dcc80987e 907 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 908 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 909 * @param DevAddress: Target device address
mbed_official 610:813dcc80987e 910 * @param Trials: Number of trials
mbed_official 610:813dcc80987e 911 * @param Timeout: Timeout duration
mbed_official 610:813dcc80987e 912 * @retval HAL status
mbed_official 610:813dcc80987e 913 */
mbed_official 610:813dcc80987e 914 HAL_StatusTypeDef HAL_SMBUS_IsDeviceReady(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
mbed_official 610:813dcc80987e 915 {
mbed_official 610:813dcc80987e 916 uint32_t tickstart = 0;
mbed_official 610:813dcc80987e 917
mbed_official 610:813dcc80987e 918 __IO uint32_t SMBUS_Trials = 0;
mbed_official 610:813dcc80987e 919
mbed_official 610:813dcc80987e 920 if(hsmbus->State == HAL_SMBUS_STATE_READY)
mbed_official 610:813dcc80987e 921 {
mbed_official 610:813dcc80987e 922 if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_BUSY) != RESET)
mbed_official 610:813dcc80987e 923 {
mbed_official 610:813dcc80987e 924 return HAL_BUSY;
mbed_official 610:813dcc80987e 925 }
mbed_official 610:813dcc80987e 926
mbed_official 610:813dcc80987e 927 /* Process Locked */
mbed_official 610:813dcc80987e 928 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 929
mbed_official 610:813dcc80987e 930 hsmbus->State = HAL_SMBUS_STATE_BUSY;
mbed_official 610:813dcc80987e 931 hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
mbed_official 610:813dcc80987e 932
mbed_official 610:813dcc80987e 933 do
mbed_official 610:813dcc80987e 934 {
mbed_official 610:813dcc80987e 935 /* Generate Start */
mbed_official 610:813dcc80987e 936 hsmbus->Instance->CR2 = SMBUS_GENERATE_START(hsmbus->Init.AddressingMode,DevAddress);
mbed_official 610:813dcc80987e 937
mbed_official 610:813dcc80987e 938 /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
mbed_official 610:813dcc80987e 939 /* Wait until STOPF flag is set or a NACK flag is set*/
mbed_official 610:813dcc80987e 940 tickstart = HAL_GetTick();
mbed_official 610:813dcc80987e 941 while((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF) == RESET) && (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) == RESET) && (hsmbus->State != HAL_SMBUS_STATE_TIMEOUT))
mbed_official 610:813dcc80987e 942 {
mbed_official 610:813dcc80987e 943 if(Timeout != HAL_MAX_DELAY)
mbed_official 610:813dcc80987e 944 {
mbed_official 610:813dcc80987e 945 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 610:813dcc80987e 946 {
mbed_official 610:813dcc80987e 947 /* Device is ready */
mbed_official 610:813dcc80987e 948 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 949
mbed_official 610:813dcc80987e 950 /* Process Unlocked */
mbed_official 610:813dcc80987e 951 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 952 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 953 }
mbed_official 610:813dcc80987e 954 }
mbed_official 610:813dcc80987e 955 }
mbed_official 610:813dcc80987e 956
mbed_official 610:813dcc80987e 957 /* Check if the NACKF flag has not been set */
mbed_official 610:813dcc80987e 958 if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) == RESET)
mbed_official 610:813dcc80987e 959 {
mbed_official 610:813dcc80987e 960 /* Wait until STOPF flag is reset */
mbed_official 610:813dcc80987e 961 if(SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 962 {
mbed_official 610:813dcc80987e 963 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 964 }
mbed_official 610:813dcc80987e 965
mbed_official 610:813dcc80987e 966 /* Clear STOP Flag */
mbed_official 610:813dcc80987e 967 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
mbed_official 610:813dcc80987e 968
mbed_official 610:813dcc80987e 969 /* Device is ready */
mbed_official 610:813dcc80987e 970 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 971
mbed_official 610:813dcc80987e 972 /* Process Unlocked */
mbed_official 610:813dcc80987e 973 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 974
mbed_official 610:813dcc80987e 975 return HAL_OK;
mbed_official 610:813dcc80987e 976 }
mbed_official 610:813dcc80987e 977 else
mbed_official 610:813dcc80987e 978 {
mbed_official 610:813dcc80987e 979 /* Wait until STOPF flag is reset */
mbed_official 610:813dcc80987e 980 if(SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 981 {
mbed_official 610:813dcc80987e 982 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 983 }
mbed_official 610:813dcc80987e 984
mbed_official 610:813dcc80987e 985 /* Clear NACK Flag */
mbed_official 610:813dcc80987e 986 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
mbed_official 610:813dcc80987e 987
mbed_official 610:813dcc80987e 988 /* Clear STOP Flag, auto generated with autoend*/
mbed_official 610:813dcc80987e 989 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
mbed_official 610:813dcc80987e 990 }
mbed_official 610:813dcc80987e 991
mbed_official 610:813dcc80987e 992 /* Check if the maximum allowed number of trials has been reached */
mbed_official 610:813dcc80987e 993 if (SMBUS_Trials++ == Trials)
mbed_official 610:813dcc80987e 994 {
mbed_official 610:813dcc80987e 995 /* Generate Stop */
mbed_official 610:813dcc80987e 996 hsmbus->Instance->CR2 |= I2C_CR2_STOP;
mbed_official 610:813dcc80987e 997
mbed_official 610:813dcc80987e 998 /* Wait until STOPF flag is reset */
mbed_official 610:813dcc80987e 999 if(SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
mbed_official 610:813dcc80987e 1000 {
mbed_official 610:813dcc80987e 1001 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1002 }
mbed_official 610:813dcc80987e 1003
mbed_official 610:813dcc80987e 1004 /* Clear STOP Flag */
mbed_official 610:813dcc80987e 1005 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
mbed_official 610:813dcc80987e 1006 }
mbed_official 610:813dcc80987e 1007 }while(SMBUS_Trials < Trials);
mbed_official 610:813dcc80987e 1008
mbed_official 610:813dcc80987e 1009 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1010
mbed_official 610:813dcc80987e 1011 /* Process Unlocked */
mbed_official 610:813dcc80987e 1012 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1013
mbed_official 610:813dcc80987e 1014 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1015 }
mbed_official 610:813dcc80987e 1016 else
mbed_official 610:813dcc80987e 1017 {
mbed_official 610:813dcc80987e 1018 return HAL_BUSY;
mbed_official 610:813dcc80987e 1019 }
mbed_official 610:813dcc80987e 1020 }
mbed_official 610:813dcc80987e 1021 /**
mbed_official 610:813dcc80987e 1022 * @}
mbed_official 610:813dcc80987e 1023 */
mbed_official 610:813dcc80987e 1024
mbed_official 610:813dcc80987e 1025 /** @defgroup SMBUS_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
mbed_official 610:813dcc80987e 1026 * @{
mbed_official 610:813dcc80987e 1027 */
mbed_official 610:813dcc80987e 1028
mbed_official 610:813dcc80987e 1029 /**
mbed_official 610:813dcc80987e 1030 * @brief Handle SMBUS event interrupt request.
mbed_official 610:813dcc80987e 1031 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1032 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1033 * @retval None
mbed_official 610:813dcc80987e 1034 */
mbed_official 610:813dcc80987e 1035 void HAL_SMBUS_EV_IRQHandler(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1036 {
mbed_official 610:813dcc80987e 1037 uint32_t tmpisrvalue = 0;
mbed_official 610:813dcc80987e 1038
mbed_official 610:813dcc80987e 1039 /* Use a local variable to store the current ISR flags */
mbed_official 610:813dcc80987e 1040 /* This action will avoid a wrong treatment due to ISR flags change during interrupt handler */
mbed_official 610:813dcc80987e 1041 tmpisrvalue = SMBUS_GET_ISR_REG(hsmbus);
mbed_official 610:813dcc80987e 1042
mbed_official 610:813dcc80987e 1043 /* SMBUS in mode Transmitter ---------------------------------------------------*/
mbed_official 610:813dcc80987e 1044 if (((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TXIS) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TCR) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TC) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, (SMBUS_IT_TCI| SMBUS_IT_STOPI| SMBUS_IT_NACKI | SMBUS_IT_TXI)) != RESET))
mbed_official 610:813dcc80987e 1045 {
mbed_official 610:813dcc80987e 1046 /* Slave mode selected */
mbed_official 610:813dcc80987e 1047 if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
mbed_official 610:813dcc80987e 1048 {
mbed_official 610:813dcc80987e 1049 SMBUS_Slave_ISR(hsmbus);
mbed_official 610:813dcc80987e 1050 }
mbed_official 610:813dcc80987e 1051 /* Master mode selected */
mbed_official 610:813dcc80987e 1052 else if((hsmbus->State & HAL_SMBUS_STATE_MASTER_BUSY_TX) == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 1053 {
mbed_official 610:813dcc80987e 1054 SMBUS_Master_ISR(hsmbus);
mbed_official 610:813dcc80987e 1055 }
mbed_official 610:813dcc80987e 1056 }
mbed_official 610:813dcc80987e 1057
mbed_official 610:813dcc80987e 1058 /* SMBUS in mode Receiver ----------------------------------------------------*/
mbed_official 610:813dcc80987e 1059 if (((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_RXNE) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TCR) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TC) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, (SMBUS_IT_TCI| SMBUS_IT_STOPI| SMBUS_IT_NACKI | SMBUS_IT_RXI)) != RESET))
mbed_official 610:813dcc80987e 1060 {
mbed_official 610:813dcc80987e 1061 /* Slave mode selected */
mbed_official 610:813dcc80987e 1062 if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX)
mbed_official 610:813dcc80987e 1063 {
mbed_official 610:813dcc80987e 1064 SMBUS_Slave_ISR(hsmbus);
mbed_official 610:813dcc80987e 1065 }
mbed_official 610:813dcc80987e 1066 /* Master mode selected */
mbed_official 610:813dcc80987e 1067 else if((hsmbus->State & HAL_SMBUS_STATE_MASTER_BUSY_RX) == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 1068 {
mbed_official 610:813dcc80987e 1069 SMBUS_Master_ISR(hsmbus);
mbed_official 610:813dcc80987e 1070 }
mbed_official 610:813dcc80987e 1071 }
mbed_official 610:813dcc80987e 1072
mbed_official 610:813dcc80987e 1073 /* SMBUS in mode Listener Only --------------------------------------------------*/
mbed_official 610:813dcc80987e 1074 if (((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_ADDR) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET))
mbed_official 610:813dcc80987e 1075 && ((__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ADDRI) != RESET) || (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_STOPI) != RESET) || (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_NACKI) != RESET)))
mbed_official 610:813dcc80987e 1076 {
mbed_official 610:813dcc80987e 1077 if (hsmbus->State == HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 1078 {
mbed_official 610:813dcc80987e 1079 SMBUS_Slave_ISR(hsmbus);
mbed_official 610:813dcc80987e 1080 }
mbed_official 610:813dcc80987e 1081 }
mbed_official 610:813dcc80987e 1082 }
mbed_official 610:813dcc80987e 1083
mbed_official 610:813dcc80987e 1084 /**
mbed_official 610:813dcc80987e 1085 * @brief Handle SMBUS error interrupt request.
mbed_official 610:813dcc80987e 1086 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1087 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1088 * @retval None
mbed_official 610:813dcc80987e 1089 */
mbed_official 610:813dcc80987e 1090 void HAL_SMBUS_ER_IRQHandler(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1091 {
mbed_official 610:813dcc80987e 1092 /* SMBUS Bus error interrupt occurred ------------------------------------*/
mbed_official 610:813dcc80987e 1093 if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_BERR) != RESET) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ERRI) != RESET))
mbed_official 610:813dcc80987e 1094 {
mbed_official 610:813dcc80987e 1095 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_BERR;
mbed_official 610:813dcc80987e 1096
mbed_official 610:813dcc80987e 1097 /* Clear BERR flag */
mbed_official 610:813dcc80987e 1098 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_BERR);
mbed_official 610:813dcc80987e 1099 }
mbed_official 610:813dcc80987e 1100
mbed_official 610:813dcc80987e 1101 /* SMBUS Over-Run/Under-Run interrupt occurred ----------------------------------------*/
mbed_official 610:813dcc80987e 1102 if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_OVR) != RESET) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ERRI) != RESET))
mbed_official 610:813dcc80987e 1103 {
mbed_official 610:813dcc80987e 1104 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_OVR;
mbed_official 610:813dcc80987e 1105
mbed_official 610:813dcc80987e 1106 /* Clear OVR flag */
mbed_official 610:813dcc80987e 1107 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_OVR);
mbed_official 610:813dcc80987e 1108 }
mbed_official 610:813dcc80987e 1109
mbed_official 610:813dcc80987e 1110 /* SMBUS Arbitration Loss error interrupt occurred ------------------------------------*/
mbed_official 610:813dcc80987e 1111 if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_ARLO) != RESET) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ERRI) != RESET))
mbed_official 610:813dcc80987e 1112 {
mbed_official 610:813dcc80987e 1113 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ARLO;
mbed_official 610:813dcc80987e 1114
mbed_official 610:813dcc80987e 1115 /* Clear ARLO flag */
mbed_official 610:813dcc80987e 1116 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ARLO);
mbed_official 610:813dcc80987e 1117 }
mbed_official 610:813dcc80987e 1118
mbed_official 610:813dcc80987e 1119 /* SMBUS Timeout error interrupt occurred ---------------------------------------------*/
mbed_official 610:813dcc80987e 1120 if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TIMEOUT) != RESET) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ERRI) != RESET))
mbed_official 610:813dcc80987e 1121 {
mbed_official 610:813dcc80987e 1122 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_BUSTIMEOUT;
mbed_official 610:813dcc80987e 1123
mbed_official 610:813dcc80987e 1124 /* Clear TIMEOUT flag */
mbed_official 610:813dcc80987e 1125 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_TIMEOUT);
mbed_official 610:813dcc80987e 1126 }
mbed_official 610:813dcc80987e 1127
mbed_official 610:813dcc80987e 1128 /* SMBUS Alert error interrupt occurred -----------------------------------------------*/
mbed_official 610:813dcc80987e 1129 if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_ALERT) != RESET) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ERRI) != RESET))
mbed_official 610:813dcc80987e 1130 {
mbed_official 610:813dcc80987e 1131 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ALERT;
mbed_official 610:813dcc80987e 1132
mbed_official 610:813dcc80987e 1133 /* Clear ALERT flag */
mbed_official 610:813dcc80987e 1134 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ALERT);
mbed_official 610:813dcc80987e 1135 }
mbed_official 610:813dcc80987e 1136
mbed_official 610:813dcc80987e 1137 /* SMBUS Packet Error Check error interrupt occurred ----------------------------------*/
mbed_official 610:813dcc80987e 1138 if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_PECERR) != RESET) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ERRI) != RESET))
mbed_official 610:813dcc80987e 1139 {
mbed_official 610:813dcc80987e 1140 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_PECERR;
mbed_official 610:813dcc80987e 1141
mbed_official 610:813dcc80987e 1142 /* Clear PEC error flag */
mbed_official 610:813dcc80987e 1143 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_PECERR);
mbed_official 610:813dcc80987e 1144 }
mbed_official 610:813dcc80987e 1145
mbed_official 610:813dcc80987e 1146 /* Call the Error Callback() in case of Error detected */
mbed_official 610:813dcc80987e 1147 if((hsmbus->ErrorCode != HAL_SMBUS_ERROR_NONE)&&(hsmbus->ErrorCode != HAL_SMBUS_ERROR_ACKF))
mbed_official 610:813dcc80987e 1148 {
mbed_official 610:813dcc80987e 1149 /* Do not Reset the HAL state in case of ALERT error */
mbed_official 610:813dcc80987e 1150 if((hsmbus->ErrorCode & HAL_SMBUS_ERROR_ALERT) != HAL_SMBUS_ERROR_ALERT)
mbed_official 610:813dcc80987e 1151 {
mbed_official 610:813dcc80987e 1152 if(((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
mbed_official 610:813dcc80987e 1153 || ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX))
mbed_official 610:813dcc80987e 1154 {
mbed_official 610:813dcc80987e 1155 /* Reset only HAL_SMBUS_STATE_SLAVE_BUSY_XX */
mbed_official 610:813dcc80987e 1156 /* keep HAL_SMBUS_STATE_LISTEN if set */
mbed_official 610:813dcc80987e 1157 hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1158 hsmbus->State = HAL_SMBUS_STATE_LISTEN;
mbed_official 610:813dcc80987e 1159 }
mbed_official 610:813dcc80987e 1160 }
mbed_official 610:813dcc80987e 1161
mbed_official 610:813dcc80987e 1162 /* Call the Error callback to prevent upper layer */
mbed_official 610:813dcc80987e 1163 HAL_SMBUS_ErrorCallback(hsmbus);
mbed_official 610:813dcc80987e 1164 }
mbed_official 610:813dcc80987e 1165 }
mbed_official 610:813dcc80987e 1166
mbed_official 610:813dcc80987e 1167 /**
mbed_official 610:813dcc80987e 1168 * @brief Master Tx Transfer completed callback.
mbed_official 610:813dcc80987e 1169 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1170 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1171 * @retval None
mbed_official 610:813dcc80987e 1172 */
mbed_official 610:813dcc80987e 1173 __weak void HAL_SMBUS_MasterTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1174 {
mbed_official 610:813dcc80987e 1175 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1176 the HAL_SMBUS_TxCpltCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1177 */
mbed_official 610:813dcc80987e 1178 }
mbed_official 610:813dcc80987e 1179
mbed_official 610:813dcc80987e 1180 /**
mbed_official 610:813dcc80987e 1181 * @brief Master Rx Transfer completed callback.
mbed_official 610:813dcc80987e 1182 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1183 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1184 * @retval None
mbed_official 610:813dcc80987e 1185 */
mbed_official 610:813dcc80987e 1186 __weak void HAL_SMBUS_MasterRxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1187 {
mbed_official 610:813dcc80987e 1188 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1189 the HAL_SMBUS_TxCpltCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1190 */
mbed_official 610:813dcc80987e 1191 }
mbed_official 610:813dcc80987e 1192
mbed_official 610:813dcc80987e 1193 /** @brief Slave Tx Transfer completed callback.
mbed_official 610:813dcc80987e 1194 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1195 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1196 * @retval None
mbed_official 610:813dcc80987e 1197 */
mbed_official 610:813dcc80987e 1198 __weak void HAL_SMBUS_SlaveTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1199 {
mbed_official 610:813dcc80987e 1200 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1201 the HAL_SMBUS_TxCpltCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1202 */
mbed_official 610:813dcc80987e 1203 }
mbed_official 610:813dcc80987e 1204
mbed_official 610:813dcc80987e 1205 /**
mbed_official 610:813dcc80987e 1206 * @brief Slave Rx Transfer completed callback.
mbed_official 610:813dcc80987e 1207 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1208 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1209 * @retval None
mbed_official 610:813dcc80987e 1210 */
mbed_official 610:813dcc80987e 1211 __weak void HAL_SMBUS_SlaveRxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1212 {
mbed_official 610:813dcc80987e 1213 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1214 the HAL_SMBUS_TxCpltCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1215 */
mbed_official 610:813dcc80987e 1216 }
mbed_official 610:813dcc80987e 1217
mbed_official 610:813dcc80987e 1218 /**
mbed_official 610:813dcc80987e 1219 * @brief Slave Address Match callback.
mbed_official 610:813dcc80987e 1220 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1221 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1222 * @param TransferDirection: Master request Transfer Direction (Write/Read)
mbed_official 610:813dcc80987e 1223 * @param AddrMatchCode: Address Match Code
mbed_official 610:813dcc80987e 1224 * @retval None
mbed_official 610:813dcc80987e 1225 */
mbed_official 610:813dcc80987e 1226 __weak void HAL_SMBUS_AddrCallback(SMBUS_HandleTypeDef *hsmbus, uint8_t TransferDirection, uint16_t AddrMatchCode)
mbed_official 610:813dcc80987e 1227 {
mbed_official 610:813dcc80987e 1228 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1229 the HAL_SMBUS_AddrCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1230 */
mbed_official 610:813dcc80987e 1231 }
mbed_official 610:813dcc80987e 1232
mbed_official 610:813dcc80987e 1233 /**
mbed_official 610:813dcc80987e 1234 * @brief Listen Complete callback.
mbed_official 610:813dcc80987e 1235 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1236 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1237 * @retval None
mbed_official 610:813dcc80987e 1238 */
mbed_official 610:813dcc80987e 1239 __weak void HAL_SMBUS_ListenCpltCallback(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1240 {
mbed_official 610:813dcc80987e 1241 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1242 the HAL_SMBUS_ListenCpltCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1243 */
mbed_official 610:813dcc80987e 1244 }
mbed_official 610:813dcc80987e 1245
mbed_official 610:813dcc80987e 1246 /**
mbed_official 610:813dcc80987e 1247 * @brief SMBUS error callback.
mbed_official 610:813dcc80987e 1248 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1249 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1250 * @retval None
mbed_official 610:813dcc80987e 1251 */
mbed_official 610:813dcc80987e 1252 __weak void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1253 {
mbed_official 610:813dcc80987e 1254 /* NOTE : This function should not be modified, when the callback is needed,
mbed_official 610:813dcc80987e 1255 the HAL_SMBUS_ErrorCallback() could be implemented in the user file
mbed_official 610:813dcc80987e 1256 */
mbed_official 610:813dcc80987e 1257 }
mbed_official 610:813dcc80987e 1258
mbed_official 610:813dcc80987e 1259 /**
mbed_official 610:813dcc80987e 1260 * @}
mbed_official 610:813dcc80987e 1261 */
mbed_official 610:813dcc80987e 1262
mbed_official 610:813dcc80987e 1263 /** @defgroup SMBUS_Exported_Functions_Group3 Peripheral State and Errors functions
mbed_official 610:813dcc80987e 1264 * @brief Peripheral State and Errors functions
mbed_official 610:813dcc80987e 1265 *
mbed_official 610:813dcc80987e 1266 @verbatim
mbed_official 610:813dcc80987e 1267 ===============================================================================
mbed_official 610:813dcc80987e 1268 ##### Peripheral State and Errors functions #####
mbed_official 610:813dcc80987e 1269 ===============================================================================
mbed_official 610:813dcc80987e 1270 [..]
mbed_official 610:813dcc80987e 1271 This subsection permits to get in run-time the status of the peripheral
mbed_official 610:813dcc80987e 1272 and the data flow.
mbed_official 610:813dcc80987e 1273
mbed_official 610:813dcc80987e 1274 @endverbatim
mbed_official 610:813dcc80987e 1275 * @{
mbed_official 610:813dcc80987e 1276 */
mbed_official 610:813dcc80987e 1277
mbed_official 610:813dcc80987e 1278 /**
mbed_official 610:813dcc80987e 1279 * @brief Return the SMBUS handle state.
mbed_official 610:813dcc80987e 1280 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1281 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1282 * @retval HAL state
mbed_official 610:813dcc80987e 1283 */
mbed_official 610:813dcc80987e 1284 uint32_t HAL_SMBUS_GetState(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1285 {
mbed_official 610:813dcc80987e 1286 /* Return SMBUS handle state */
mbed_official 610:813dcc80987e 1287 return hsmbus->State;
mbed_official 610:813dcc80987e 1288 }
mbed_official 610:813dcc80987e 1289
mbed_official 610:813dcc80987e 1290 /**
mbed_official 610:813dcc80987e 1291 * @brief Return the SMBUS error code.
mbed_official 610:813dcc80987e 1292 * @param hsmbus : pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1293 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1294 * @retval SMBUS Error Code
mbed_official 610:813dcc80987e 1295 */
mbed_official 610:813dcc80987e 1296 uint32_t HAL_SMBUS_GetError(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1297 {
mbed_official 610:813dcc80987e 1298 return hsmbus->ErrorCode;
mbed_official 610:813dcc80987e 1299 }
mbed_official 610:813dcc80987e 1300
mbed_official 610:813dcc80987e 1301 /**
mbed_official 610:813dcc80987e 1302 * @}
mbed_official 610:813dcc80987e 1303 */
mbed_official 610:813dcc80987e 1304
mbed_official 610:813dcc80987e 1305 /**
mbed_official 610:813dcc80987e 1306 * @}
mbed_official 610:813dcc80987e 1307 */
mbed_official 610:813dcc80987e 1308
mbed_official 610:813dcc80987e 1309 /** @addtogroup SMBUS_Private_Functions SMBUS Private Functions
mbed_official 610:813dcc80987e 1310 * @brief Data transfers Private functions
mbed_official 610:813dcc80987e 1311 * @{
mbed_official 610:813dcc80987e 1312 */
mbed_official 610:813dcc80987e 1313
mbed_official 610:813dcc80987e 1314 /**
mbed_official 610:813dcc80987e 1315 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode.
mbed_official 610:813dcc80987e 1316 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1317 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1318 * @retval HAL status
mbed_official 610:813dcc80987e 1319 */
mbed_official 610:813dcc80987e 1320 static HAL_StatusTypeDef SMBUS_Master_ISR(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1321 {
mbed_official 610:813dcc80987e 1322 uint16_t DevAddress;
mbed_official 610:813dcc80987e 1323
mbed_official 610:813dcc80987e 1324 /* Process Locked */
mbed_official 610:813dcc80987e 1325 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 1326
mbed_official 610:813dcc80987e 1327 if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) != RESET)
mbed_official 610:813dcc80987e 1328 {
mbed_official 610:813dcc80987e 1329 /* Clear NACK Flag */
mbed_official 610:813dcc80987e 1330 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
mbed_official 610:813dcc80987e 1331
mbed_official 610:813dcc80987e 1332 /* Set corresponding Error Code */
mbed_official 610:813dcc80987e 1333 /* No need to generate STOP, it is automatically done */
mbed_official 610:813dcc80987e 1334 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ACKF;
mbed_official 610:813dcc80987e 1335
mbed_official 610:813dcc80987e 1336 /* Process Unlocked */
mbed_official 610:813dcc80987e 1337 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1338
mbed_official 610:813dcc80987e 1339 /* Call the Error callback to prevent upper layer */
mbed_official 610:813dcc80987e 1340 HAL_SMBUS_ErrorCallback(hsmbus);
mbed_official 610:813dcc80987e 1341 }
mbed_official 610:813dcc80987e 1342 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF) != RESET)
mbed_official 610:813dcc80987e 1343 {
mbed_official 610:813dcc80987e 1344
mbed_official 610:813dcc80987e 1345 /* Call the corresponding callback to inform upper layer of End of Transfer */
mbed_official 610:813dcc80987e 1346 if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 1347 {
mbed_official 610:813dcc80987e 1348 /* Disable Interrupt */
mbed_official 610:813dcc80987e 1349 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
mbed_official 610:813dcc80987e 1350
mbed_official 610:813dcc80987e 1351 /* Clear STOP Flag */
mbed_official 610:813dcc80987e 1352 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
mbed_official 610:813dcc80987e 1353
mbed_official 610:813dcc80987e 1354 /* Clear Configuration Register 2 */
mbed_official 610:813dcc80987e 1355 SMBUS_RESET_CR2(hsmbus);
mbed_official 610:813dcc80987e 1356
mbed_official 610:813dcc80987e 1357 /* Flush remaining data in Fifo register in case of error occurs before TXEmpty */
mbed_official 610:813dcc80987e 1358 /* Disable the selected SMBUS peripheral */
mbed_official 610:813dcc80987e 1359 __HAL_SMBUS_DISABLE(hsmbus);
mbed_official 610:813dcc80987e 1360
mbed_official 610:813dcc80987e 1361 hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1362 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1363
mbed_official 610:813dcc80987e 1364 /* Process Unlocked */
mbed_official 610:813dcc80987e 1365 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1366
mbed_official 610:813dcc80987e 1367 /* REenable the selected SMBUS peripheral */
mbed_official 610:813dcc80987e 1368 __HAL_SMBUS_ENABLE(hsmbus);
mbed_official 610:813dcc80987e 1369
mbed_official 610:813dcc80987e 1370 HAL_SMBUS_MasterTxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1371 }
mbed_official 610:813dcc80987e 1372 else if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 1373 {
mbed_official 610:813dcc80987e 1374 /* Disable Interrupt */
mbed_official 610:813dcc80987e 1375 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
mbed_official 610:813dcc80987e 1376
mbed_official 610:813dcc80987e 1377 /* Clear STOP Flag */
mbed_official 610:813dcc80987e 1378 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
mbed_official 610:813dcc80987e 1379
mbed_official 610:813dcc80987e 1380 /* Clear Configuration Register 2 */
mbed_official 610:813dcc80987e 1381 SMBUS_RESET_CR2(hsmbus);
mbed_official 610:813dcc80987e 1382
mbed_official 610:813dcc80987e 1383 hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1384 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1385
mbed_official 610:813dcc80987e 1386 /* Process Unlocked */
mbed_official 610:813dcc80987e 1387 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1388
mbed_official 610:813dcc80987e 1389 HAL_SMBUS_MasterRxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1390 }
mbed_official 610:813dcc80987e 1391 }
mbed_official 610:813dcc80987e 1392 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET)
mbed_official 610:813dcc80987e 1393 {
mbed_official 610:813dcc80987e 1394 /* Read data from RXDR */
mbed_official 610:813dcc80987e 1395 (*hsmbus->pBuffPtr++) = hsmbus->Instance->RXDR;
mbed_official 610:813dcc80987e 1396 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 1397 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 1398 }
mbed_official 610:813dcc80987e 1399 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TXIS) != RESET)
mbed_official 610:813dcc80987e 1400 {
mbed_official 610:813dcc80987e 1401 /* Write data to TXDR */
mbed_official 610:813dcc80987e 1402 hsmbus->Instance->TXDR = (*hsmbus->pBuffPtr++);
mbed_official 610:813dcc80987e 1403 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 1404 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 1405 }
mbed_official 610:813dcc80987e 1406 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TCR) != RESET)
mbed_official 610:813dcc80987e 1407 {
mbed_official 610:813dcc80987e 1408 if((hsmbus->XferSize == 0)&&(hsmbus->XferCount!=0))
mbed_official 610:813dcc80987e 1409 {
mbed_official 610:813dcc80987e 1410 DevAddress = (hsmbus->Instance->CR2 & I2C_CR2_SADD);
mbed_official 610:813dcc80987e 1411
mbed_official 610:813dcc80987e 1412 if(hsmbus->XferCount > MAX_NBYTE_SIZE)
mbed_official 610:813dcc80987e 1413 {
mbed_official 610:813dcc80987e 1414 SMBUS_TransferConfig(hsmbus, DevAddress, MAX_NBYTE_SIZE, (SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE)), SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 1415 hsmbus->XferSize = MAX_NBYTE_SIZE;
mbed_official 610:813dcc80987e 1416 }
mbed_official 610:813dcc80987e 1417 else
mbed_official 610:813dcc80987e 1418 {
mbed_official 610:813dcc80987e 1419 hsmbus->XferSize = hsmbus->XferCount;
mbed_official 610:813dcc80987e 1420 SMBUS_TransferConfig(hsmbus,DevAddress,hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 1421 /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
mbed_official 610:813dcc80987e 1422 /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
mbed_official 610:813dcc80987e 1423 if(SMBUS_GET_PEC_MODE(hsmbus) != RESET)
mbed_official 610:813dcc80987e 1424 {
mbed_official 610:813dcc80987e 1425 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 1426 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 1427 }
mbed_official 610:813dcc80987e 1428 }
mbed_official 610:813dcc80987e 1429 }
mbed_official 610:813dcc80987e 1430 else if((hsmbus->XferSize == 0)&&(hsmbus->XferCount==0))
mbed_official 610:813dcc80987e 1431 {
mbed_official 610:813dcc80987e 1432 /* Call TxCpltCallback() if no stop mode is set */
mbed_official 610:813dcc80987e 1433 if(SMBUS_GET_STOP_MODE(hsmbus) != SMBUS_AUTOEND_MODE)
mbed_official 610:813dcc80987e 1434 {
mbed_official 610:813dcc80987e 1435 /* Call the corresponding callback to inform upper layer of End of Transfer */
mbed_official 610:813dcc80987e 1436 if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 1437 {
mbed_official 610:813dcc80987e 1438 /* Disable Interrupt */
mbed_official 610:813dcc80987e 1439 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
mbed_official 610:813dcc80987e 1440 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1441 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1442
mbed_official 610:813dcc80987e 1443 /* Process Unlocked */
mbed_official 610:813dcc80987e 1444 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1445
mbed_official 610:813dcc80987e 1446 HAL_SMBUS_MasterTxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1447 }
mbed_official 610:813dcc80987e 1448 else if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 1449 {
mbed_official 610:813dcc80987e 1450 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
mbed_official 610:813dcc80987e 1451 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1452 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1453
mbed_official 610:813dcc80987e 1454 /* Process Unlocked */
mbed_official 610:813dcc80987e 1455 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1456
mbed_official 610:813dcc80987e 1457 HAL_SMBUS_MasterRxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1458 }
mbed_official 610:813dcc80987e 1459 }
mbed_official 610:813dcc80987e 1460 }
mbed_official 610:813dcc80987e 1461 }
mbed_official 610:813dcc80987e 1462 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TC) != RESET)
mbed_official 610:813dcc80987e 1463 {
mbed_official 610:813dcc80987e 1464 if(hsmbus->XferCount == 0)
mbed_official 610:813dcc80987e 1465 {
mbed_official 610:813dcc80987e 1466 /* Specific use case for Quick command */
mbed_official 610:813dcc80987e 1467 if(hsmbus->pBuffPtr == NULL)
mbed_official 610:813dcc80987e 1468 {
mbed_official 610:813dcc80987e 1469 /* Generate a Stop command */
mbed_official 610:813dcc80987e 1470 hsmbus->Instance->CR2 |= I2C_CR2_STOP;
mbed_official 610:813dcc80987e 1471 }
mbed_official 610:813dcc80987e 1472 /* Call TxCpltCallback() if no stop mode is set */
mbed_official 610:813dcc80987e 1473 else if(SMBUS_GET_STOP_MODE(hsmbus) != SMBUS_AUTOEND_MODE)
mbed_official 610:813dcc80987e 1474 {
mbed_official 610:813dcc80987e 1475 /* No Generate Stop, to permit restart mode */
mbed_official 610:813dcc80987e 1476 /* The stop will be done at the end of transfer, when SMBUS_AUTOEND_MODE enable */
mbed_official 610:813dcc80987e 1477
mbed_official 610:813dcc80987e 1478 /* Call the corresponding callback to inform upper layer of End of Transfer */
mbed_official 610:813dcc80987e 1479 if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
mbed_official 610:813dcc80987e 1480 {
mbed_official 610:813dcc80987e 1481 /* Disable Interrupt */
mbed_official 610:813dcc80987e 1482 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
mbed_official 610:813dcc80987e 1483 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1484 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1485
mbed_official 610:813dcc80987e 1486 /* Process Unlocked */
mbed_official 610:813dcc80987e 1487 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1488
mbed_official 610:813dcc80987e 1489 HAL_SMBUS_MasterTxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1490 }
mbed_official 610:813dcc80987e 1491 else if(hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
mbed_official 610:813dcc80987e 1492 {
mbed_official 610:813dcc80987e 1493 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
mbed_official 610:813dcc80987e 1494 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1495 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1496
mbed_official 610:813dcc80987e 1497 /* Process Unlocked */
mbed_official 610:813dcc80987e 1498 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1499
mbed_official 610:813dcc80987e 1500 HAL_SMBUS_MasterRxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1501 }
mbed_official 610:813dcc80987e 1502 }
mbed_official 610:813dcc80987e 1503 }
mbed_official 610:813dcc80987e 1504 }
mbed_official 610:813dcc80987e 1505
mbed_official 610:813dcc80987e 1506 /* Process Unlocked */
mbed_official 610:813dcc80987e 1507 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1508
mbed_official 610:813dcc80987e 1509 return HAL_OK;
mbed_official 610:813dcc80987e 1510 }
mbed_official 610:813dcc80987e 1511 /**
mbed_official 610:813dcc80987e 1512 * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode.
mbed_official 610:813dcc80987e 1513 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1514 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1515 * @retval HAL status
mbed_official 610:813dcc80987e 1516 */
mbed_official 610:813dcc80987e 1517 static HAL_StatusTypeDef SMBUS_Slave_ISR(SMBUS_HandleTypeDef *hsmbus)
mbed_official 610:813dcc80987e 1518 {
mbed_official 610:813dcc80987e 1519 uint8_t TransferDirection = 0;
mbed_official 610:813dcc80987e 1520 uint16_t SlaveAddrCode = 0;
mbed_official 610:813dcc80987e 1521
mbed_official 610:813dcc80987e 1522 /* Process Locked */
mbed_official 610:813dcc80987e 1523 __HAL_LOCK(hsmbus);
mbed_official 610:813dcc80987e 1524
mbed_official 610:813dcc80987e 1525 if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) != RESET)
mbed_official 610:813dcc80987e 1526 {
mbed_official 610:813dcc80987e 1527 /* Check that SMBUS transfer finished */
mbed_official 610:813dcc80987e 1528 /* if yes, normal use case, a NACK is sent by the HOST when Transfer is finished */
mbed_official 610:813dcc80987e 1529 /* Mean XferCount == 0*/
mbed_official 610:813dcc80987e 1530 /* So clear Flag NACKF only */
mbed_official 610:813dcc80987e 1531 if(hsmbus->XferCount == 0)
mbed_official 610:813dcc80987e 1532 {
mbed_official 610:813dcc80987e 1533 /* Clear NACK Flag */
mbed_official 610:813dcc80987e 1534 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
mbed_official 610:813dcc80987e 1535
mbed_official 610:813dcc80987e 1536 /* Process Unlocked */
mbed_official 610:813dcc80987e 1537 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1538 }
mbed_official 610:813dcc80987e 1539 else
mbed_official 610:813dcc80987e 1540 {
mbed_official 610:813dcc80987e 1541 /* if no, error use case, a Non-Acknowledge of last Data is generated by the HOST*/
mbed_official 610:813dcc80987e 1542 /* Clear NACK Flag */
mbed_official 610:813dcc80987e 1543 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
mbed_official 610:813dcc80987e 1544
mbed_official 610:813dcc80987e 1545 /* Set HAL State to "Idle" State, mean to LISTEN state */
mbed_official 610:813dcc80987e 1546 /* So reset Slave Busy state */
mbed_official 610:813dcc80987e 1547 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1548 hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_TX);
mbed_official 610:813dcc80987e 1549 hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_RX);
mbed_official 610:813dcc80987e 1550
mbed_official 610:813dcc80987e 1551 /* Disable RX/TX Interrupts, keep only ADDR Interrupt */
mbed_official 610:813dcc80987e 1552 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_TX);
mbed_official 610:813dcc80987e 1553
mbed_official 610:813dcc80987e 1554 /* Set ErrorCode corresponding to a Non-Acknowledge */
mbed_official 610:813dcc80987e 1555 hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ACKF;
mbed_official 610:813dcc80987e 1556
mbed_official 610:813dcc80987e 1557 /* Process Unlocked */
mbed_official 610:813dcc80987e 1558 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1559
mbed_official 610:813dcc80987e 1560 /* Call the Error callback to prevent upper layer */
mbed_official 610:813dcc80987e 1561 HAL_SMBUS_ErrorCallback(hsmbus);
mbed_official 610:813dcc80987e 1562 }
mbed_official 610:813dcc80987e 1563 }
mbed_official 610:813dcc80987e 1564 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_ADDR) != RESET)
mbed_official 610:813dcc80987e 1565 {
mbed_official 610:813dcc80987e 1566 TransferDirection = SMBUS_GET_DIR(hsmbus);
mbed_official 610:813dcc80987e 1567 SlaveAddrCode = SMBUS_GET_ADDR_MATCH(hsmbus);
mbed_official 610:813dcc80987e 1568
mbed_official 610:813dcc80987e 1569 /* Disable ADDR interrupt to prevent multiple ADDRInterrupt*/
mbed_official 610:813dcc80987e 1570 /* Other ADDRInterrupt will be treat in next Listen use case */
mbed_official 610:813dcc80987e 1571 __HAL_SMBUS_DISABLE_IT(hsmbus, SMBUS_IT_ADDRI);
mbed_official 610:813dcc80987e 1572
mbed_official 610:813dcc80987e 1573 /* Process Unlocked */
mbed_official 610:813dcc80987e 1574 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1575
mbed_official 610:813dcc80987e 1576 /* Call Slave Addr callback */
mbed_official 610:813dcc80987e 1577 HAL_SMBUS_AddrCallback(hsmbus, TransferDirection, SlaveAddrCode);
mbed_official 610:813dcc80987e 1578 }
mbed_official 610:813dcc80987e 1579 else if((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET) || (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TCR) != RESET))
mbed_official 610:813dcc80987e 1580 {
mbed_official 610:813dcc80987e 1581 if( (hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX)
mbed_official 610:813dcc80987e 1582 {
mbed_official 610:813dcc80987e 1583 /* Read data from RXDR */
mbed_official 610:813dcc80987e 1584 (*hsmbus->pBuffPtr++) = hsmbus->Instance->RXDR;
mbed_official 610:813dcc80987e 1585 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 1586 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 1587
mbed_official 610:813dcc80987e 1588 if(hsmbus->XferCount == 1)
mbed_official 610:813dcc80987e 1589 {
mbed_official 610:813dcc80987e 1590 /* Receive last Byte, can be PEC byte in case of PEC BYTE enabled */
mbed_official 610:813dcc80987e 1591 /* or only the last Byte of Transfer */
mbed_official 610:813dcc80987e 1592 /* So reset the RELOAD bit mode */
mbed_official 610:813dcc80987e 1593 hsmbus->XferOptions &= ~SMBUS_RELOAD_MODE;
mbed_official 610:813dcc80987e 1594 SMBUS_TransferConfig(hsmbus,0 ,1 , hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 1595 }
mbed_official 610:813dcc80987e 1596 else if(hsmbus->XferCount == 0)
mbed_official 610:813dcc80987e 1597 {
mbed_official 610:813dcc80987e 1598 /* Last Byte is received, disable Interrupt */
mbed_official 610:813dcc80987e 1599 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
mbed_official 610:813dcc80987e 1600
mbed_official 610:813dcc80987e 1601 /* Remove HAL_SMBUS_STATE_SLAVE_BUSY_RX, keep only HAL_SMBUS_STATE_LISTEN */
mbed_official 610:813dcc80987e 1602 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1603 hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_RX);
mbed_official 610:813dcc80987e 1604
mbed_official 610:813dcc80987e 1605 /* Process Unlocked */
mbed_official 610:813dcc80987e 1606 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1607
mbed_official 610:813dcc80987e 1608 /* Call the Rx complete callback to inform upper layer of the end of receive process */
mbed_official 610:813dcc80987e 1609 HAL_SMBUS_SlaveRxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1610 }
mbed_official 610:813dcc80987e 1611 else
mbed_official 610:813dcc80987e 1612 {
mbed_official 610:813dcc80987e 1613 /* Set Reload for next Bytes */
mbed_official 610:813dcc80987e 1614 SMBUS_TransferConfig(hsmbus,0, 1, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 1615
mbed_official 610:813dcc80987e 1616 /* Ack last Byte Read */
mbed_official 610:813dcc80987e 1617 hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
mbed_official 610:813dcc80987e 1618 }
mbed_official 610:813dcc80987e 1619 }
mbed_official 610:813dcc80987e 1620 else if( (hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
mbed_official 610:813dcc80987e 1621 {
mbed_official 610:813dcc80987e 1622 if((hsmbus->XferSize == 0)&&(hsmbus->XferCount!=0))
mbed_official 610:813dcc80987e 1623 {
mbed_official 610:813dcc80987e 1624 if(hsmbus->XferCount > MAX_NBYTE_SIZE)
mbed_official 610:813dcc80987e 1625 {
mbed_official 610:813dcc80987e 1626 SMBUS_TransferConfig(hsmbus, 0, MAX_NBYTE_SIZE, (SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE)), SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 1627 hsmbus->XferSize = MAX_NBYTE_SIZE;
mbed_official 610:813dcc80987e 1628 }
mbed_official 610:813dcc80987e 1629 else
mbed_official 610:813dcc80987e 1630 {
mbed_official 610:813dcc80987e 1631 hsmbus->XferSize = hsmbus->XferCount;
mbed_official 610:813dcc80987e 1632 SMBUS_TransferConfig(hsmbus, 0, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
mbed_official 610:813dcc80987e 1633 /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
mbed_official 610:813dcc80987e 1634 /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
mbed_official 610:813dcc80987e 1635 if(SMBUS_GET_PEC_MODE(hsmbus) != RESET)
mbed_official 610:813dcc80987e 1636 {
mbed_official 610:813dcc80987e 1637 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 1638 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 1639 }
mbed_official 610:813dcc80987e 1640 }
mbed_official 610:813dcc80987e 1641 }
mbed_official 610:813dcc80987e 1642 }
mbed_official 610:813dcc80987e 1643 }
mbed_official 610:813dcc80987e 1644 else if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TXIS) != RESET)
mbed_official 610:813dcc80987e 1645 {
mbed_official 610:813dcc80987e 1646 /* Write data to TXDR only if XferCount not reach "0" */
mbed_official 610:813dcc80987e 1647 /* A TXIS flag can be set, during STOP treatment */
mbed_official 610:813dcc80987e 1648 /* Check if all Data have already been sent */
mbed_official 610:813dcc80987e 1649 /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
mbed_official 610:813dcc80987e 1650 if(hsmbus->XferCount > 0)
mbed_official 610:813dcc80987e 1651 {
mbed_official 610:813dcc80987e 1652 /* Write data to TXDR */
mbed_official 610:813dcc80987e 1653 hsmbus->Instance->TXDR = (*hsmbus->pBuffPtr++);
mbed_official 610:813dcc80987e 1654 hsmbus->XferCount--;
mbed_official 610:813dcc80987e 1655 hsmbus->XferSize--;
mbed_official 610:813dcc80987e 1656 }
mbed_official 610:813dcc80987e 1657
mbed_official 610:813dcc80987e 1658 if(hsmbus->XferCount == 0)
mbed_official 610:813dcc80987e 1659 {
mbed_official 610:813dcc80987e 1660 /* Last Byte is Transmitted */
mbed_official 610:813dcc80987e 1661 /* Remove HAL_SMBUS_STATE_SLAVE_BUSY_TX, keep only HAL_SMBUS_STATE_LISTEN */
mbed_official 610:813dcc80987e 1662 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
mbed_official 610:813dcc80987e 1663 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1664 hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_TX);
mbed_official 610:813dcc80987e 1665
mbed_official 610:813dcc80987e 1666 /* Process Unlocked */
mbed_official 610:813dcc80987e 1667 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1668
mbed_official 610:813dcc80987e 1669 /* Call the Tx complete callback to inform upper layer of the end of transmit process */
mbed_official 610:813dcc80987e 1670 HAL_SMBUS_SlaveTxCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1671 }
mbed_official 610:813dcc80987e 1672 }
mbed_official 610:813dcc80987e 1673
mbed_official 610:813dcc80987e 1674 /* Check if STOPF is set */
mbed_official 610:813dcc80987e 1675 if(__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF) != RESET)
mbed_official 610:813dcc80987e 1676 {
mbed_official 610:813dcc80987e 1677 if((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 1678 {
mbed_official 610:813dcc80987e 1679 /* Disable RX and TX Interrupts */
mbed_official 610:813dcc80987e 1680 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_TX);
mbed_official 610:813dcc80987e 1681
mbed_official 610:813dcc80987e 1682 /* Disable ADDR Interrupt */
mbed_official 610:813dcc80987e 1683 SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR);
mbed_official 610:813dcc80987e 1684
mbed_official 610:813dcc80987e 1685 /* Disable Address Acknowledge */
mbed_official 610:813dcc80987e 1686 hsmbus->Instance->CR2 |= I2C_CR2_NACK;
mbed_official 610:813dcc80987e 1687
mbed_official 610:813dcc80987e 1688 /* Clear Configuration Register 2 */
mbed_official 610:813dcc80987e 1689 SMBUS_RESET_CR2(hsmbus);
mbed_official 610:813dcc80987e 1690
mbed_official 610:813dcc80987e 1691 /* Clear STOP Flag */
mbed_official 610:813dcc80987e 1692 __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
mbed_official 610:813dcc80987e 1693
mbed_official 610:813dcc80987e 1694 /* Clear ADDR flag */
mbed_official 610:813dcc80987e 1695 __HAL_SMBUS_CLEAR_FLAG(hsmbus,SMBUS_FLAG_ADDR);
mbed_official 610:813dcc80987e 1696
mbed_official 610:813dcc80987e 1697 hsmbus->XferOptions = 0;
mbed_official 610:813dcc80987e 1698 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1699 hsmbus->State = HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1700
mbed_official 610:813dcc80987e 1701 /* Process Unlocked */
mbed_official 610:813dcc80987e 1702 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1703
mbed_official 610:813dcc80987e 1704 /* Call the Listen Complete callback, to prevent upper layer of the end of Listen use case */
mbed_official 610:813dcc80987e 1705 HAL_SMBUS_ListenCpltCallback(hsmbus);
mbed_official 610:813dcc80987e 1706 }
mbed_official 610:813dcc80987e 1707 }
mbed_official 610:813dcc80987e 1708
mbed_official 610:813dcc80987e 1709 /* Process Unlocked */
mbed_official 610:813dcc80987e 1710 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1711
mbed_official 610:813dcc80987e 1712 return HAL_OK;
mbed_official 610:813dcc80987e 1713 }
mbed_official 610:813dcc80987e 1714 /**
mbed_official 610:813dcc80987e 1715 * @brief Manage the enabling of Interrupts.
mbed_official 610:813dcc80987e 1716 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1717 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1718 * @param InterruptRequest : Value of @ref SMBUS_Interrupt_configuration_definition.
mbed_official 610:813dcc80987e 1719 * @retval HAL status
mbed_official 610:813dcc80987e 1720 */
mbed_official 610:813dcc80987e 1721 static HAL_StatusTypeDef SMBUS_Enable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest)
mbed_official 610:813dcc80987e 1722 {
mbed_official 610:813dcc80987e 1723 uint32_t tmpisr = 0;
mbed_official 610:813dcc80987e 1724
mbed_official 610:813dcc80987e 1725 if((InterruptRequest & SMBUS_IT_ALERT) == SMBUS_IT_ALERT)
mbed_official 610:813dcc80987e 1726 {
mbed_official 610:813dcc80987e 1727 /* Enable ERR interrupt */
mbed_official 610:813dcc80987e 1728 tmpisr |= SMBUS_IT_ERRI;
mbed_official 610:813dcc80987e 1729 }
mbed_official 610:813dcc80987e 1730
mbed_official 610:813dcc80987e 1731 if((InterruptRequest & SMBUS_IT_ADDR) == SMBUS_IT_ADDR)
mbed_official 610:813dcc80987e 1732 {
mbed_official 610:813dcc80987e 1733 /* Enable ADDR, STOP interrupt */
mbed_official 610:813dcc80987e 1734 tmpisr |= SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_ERRI;
mbed_official 610:813dcc80987e 1735 }
mbed_official 610:813dcc80987e 1736
mbed_official 610:813dcc80987e 1737 if((InterruptRequest & SMBUS_IT_TX) == SMBUS_IT_TX)
mbed_official 610:813dcc80987e 1738 {
mbed_official 610:813dcc80987e 1739 /* Enable ERR, TC, STOP, NACK, RXI interrupt */
mbed_official 610:813dcc80987e 1740 tmpisr |= SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_TXI;
mbed_official 610:813dcc80987e 1741 }
mbed_official 610:813dcc80987e 1742
mbed_official 610:813dcc80987e 1743 if((InterruptRequest & SMBUS_IT_RX) == SMBUS_IT_RX)
mbed_official 610:813dcc80987e 1744 {
mbed_official 610:813dcc80987e 1745 /* Enable ERR, TC, STOP, NACK, TXI interrupt */
mbed_official 610:813dcc80987e 1746 tmpisr |= SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_RXI;
mbed_official 610:813dcc80987e 1747 }
mbed_official 610:813dcc80987e 1748
mbed_official 610:813dcc80987e 1749 /* Enable interrupts only at the end */
mbed_official 610:813dcc80987e 1750 /* to avoid the risk of SMBUS interrupt handle execution before */
mbed_official 610:813dcc80987e 1751 /* all interrupts requested done */
mbed_official 610:813dcc80987e 1752 __HAL_SMBUS_ENABLE_IT(hsmbus, tmpisr);
mbed_official 610:813dcc80987e 1753
mbed_official 610:813dcc80987e 1754 return HAL_OK;
mbed_official 610:813dcc80987e 1755 }
mbed_official 610:813dcc80987e 1756 /**
mbed_official 610:813dcc80987e 1757 * @brief Manage the disabling of Interrupts.
mbed_official 610:813dcc80987e 1758 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1759 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1760 * @param InterruptRequest : Value of @ref SMBUS_Interrupt_configuration_definition.
mbed_official 610:813dcc80987e 1761 * @retval HAL status
mbed_official 610:813dcc80987e 1762 */
mbed_official 610:813dcc80987e 1763 static HAL_StatusTypeDef SMBUS_Disable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest)
mbed_official 610:813dcc80987e 1764 {
mbed_official 610:813dcc80987e 1765 uint32_t tmpisr = 0;
mbed_official 610:813dcc80987e 1766
mbed_official 610:813dcc80987e 1767 if( ((InterruptRequest & SMBUS_IT_ALERT) == SMBUS_IT_ALERT) && (hsmbus->State == HAL_SMBUS_STATE_READY) )
mbed_official 610:813dcc80987e 1768 {
mbed_official 610:813dcc80987e 1769 /* Disable ERR interrupt */
mbed_official 610:813dcc80987e 1770 tmpisr |= SMBUS_IT_ERRI;
mbed_official 610:813dcc80987e 1771 }
mbed_official 610:813dcc80987e 1772
mbed_official 610:813dcc80987e 1773 if((InterruptRequest & SMBUS_IT_TX) == SMBUS_IT_TX)
mbed_official 610:813dcc80987e 1774 {
mbed_official 610:813dcc80987e 1775 /* Disable TC, STOP, NACK, TXI interrupt */
mbed_official 610:813dcc80987e 1776 tmpisr |= SMBUS_IT_TCI | SMBUS_IT_TXI;
mbed_official 610:813dcc80987e 1777
mbed_official 610:813dcc80987e 1778 if((SMBUS_GET_ALERT_ENABLED(hsmbus) == RESET)
mbed_official 610:813dcc80987e 1779 && ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN))
mbed_official 610:813dcc80987e 1780 {
mbed_official 610:813dcc80987e 1781 /* Disable ERR interrupt */
mbed_official 610:813dcc80987e 1782 tmpisr |= SMBUS_IT_ERRI;
mbed_official 610:813dcc80987e 1783 }
mbed_official 610:813dcc80987e 1784
mbed_official 610:813dcc80987e 1785 if((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 1786 {
mbed_official 610:813dcc80987e 1787 /* Disable STOPI, NACKI */
mbed_official 610:813dcc80987e 1788 tmpisr |= SMBUS_IT_STOPI | SMBUS_IT_NACKI;
mbed_official 610:813dcc80987e 1789 }
mbed_official 610:813dcc80987e 1790 }
mbed_official 610:813dcc80987e 1791
mbed_official 610:813dcc80987e 1792 if((InterruptRequest & SMBUS_IT_RX) == SMBUS_IT_RX)
mbed_official 610:813dcc80987e 1793 {
mbed_official 610:813dcc80987e 1794 /* Disable TC, STOP, NACK, RXI interrupt */
mbed_official 610:813dcc80987e 1795 tmpisr |= SMBUS_IT_TCI | SMBUS_IT_RXI;
mbed_official 610:813dcc80987e 1796
mbed_official 610:813dcc80987e 1797 if((SMBUS_GET_ALERT_ENABLED(hsmbus) == RESET)
mbed_official 610:813dcc80987e 1798 && ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN))
mbed_official 610:813dcc80987e 1799 {
mbed_official 610:813dcc80987e 1800 /* Disable ERR interrupt */
mbed_official 610:813dcc80987e 1801 tmpisr |= SMBUS_IT_ERRI;
mbed_official 610:813dcc80987e 1802 }
mbed_official 610:813dcc80987e 1803
mbed_official 610:813dcc80987e 1804 if((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN)
mbed_official 610:813dcc80987e 1805 {
mbed_official 610:813dcc80987e 1806 /* Disable STOPI, NACKI */
mbed_official 610:813dcc80987e 1807 tmpisr |= SMBUS_IT_STOPI | SMBUS_IT_NACKI;
mbed_official 610:813dcc80987e 1808 }
mbed_official 610:813dcc80987e 1809 }
mbed_official 610:813dcc80987e 1810
mbed_official 610:813dcc80987e 1811 if((InterruptRequest & SMBUS_IT_ADDR) == SMBUS_IT_ADDR)
mbed_official 610:813dcc80987e 1812 {
mbed_official 610:813dcc80987e 1813 /* Enable ADDR, STOP interrupt */
mbed_official 610:813dcc80987e 1814 tmpisr |= SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI;
mbed_official 610:813dcc80987e 1815
mbed_official 610:813dcc80987e 1816 if(SMBUS_GET_ALERT_ENABLED(hsmbus) == RESET)
mbed_official 610:813dcc80987e 1817 {
mbed_official 610:813dcc80987e 1818 /* Disable ERR interrupt */
mbed_official 610:813dcc80987e 1819 tmpisr |= SMBUS_IT_ERRI;
mbed_official 610:813dcc80987e 1820 }
mbed_official 610:813dcc80987e 1821 }
mbed_official 610:813dcc80987e 1822
mbed_official 610:813dcc80987e 1823 /* Disable interrupts only at the end */
mbed_official 610:813dcc80987e 1824 /* to avoid a breaking situation like at "t" time */
mbed_official 610:813dcc80987e 1825 /* all disable interrupts request are not done */
mbed_official 610:813dcc80987e 1826 __HAL_SMBUS_DISABLE_IT(hsmbus, tmpisr);
mbed_official 610:813dcc80987e 1827
mbed_official 610:813dcc80987e 1828 return HAL_OK;
mbed_official 610:813dcc80987e 1829 }
mbed_official 610:813dcc80987e 1830 /**
mbed_official 610:813dcc80987e 1831 * @brief Handle SMBUS Communication Timeout.
mbed_official 610:813dcc80987e 1832 * @param hsmbus : Pointer to a SMBUS_HandleTypeDef structure that contains
mbed_official 610:813dcc80987e 1833 * the configuration information for the specified SMBUS.
mbed_official 610:813dcc80987e 1834 * @param Flag: specifies the SMBUS flag to check.
mbed_official 610:813dcc80987e 1835 * @param Status: The new Flag status (SET or RESET).
mbed_official 610:813dcc80987e 1836 * @param Timeout: Timeout duration
mbed_official 610:813dcc80987e 1837 * @retval HAL status
mbed_official 610:813dcc80987e 1838 */
mbed_official 610:813dcc80987e 1839 static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(SMBUS_HandleTypeDef *hsmbus, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
mbed_official 610:813dcc80987e 1840 {
mbed_official 610:813dcc80987e 1841 uint32_t tickstart = HAL_GetTick();
mbed_official 610:813dcc80987e 1842
mbed_official 610:813dcc80987e 1843 /* Wait until flag is set */
mbed_official 610:813dcc80987e 1844 if(Status == RESET)
mbed_official 610:813dcc80987e 1845 {
mbed_official 610:813dcc80987e 1846 while(__HAL_SMBUS_GET_FLAG(hsmbus, Flag) == RESET)
mbed_official 610:813dcc80987e 1847 {
mbed_official 610:813dcc80987e 1848 /* Check for the Timeout */
mbed_official 610:813dcc80987e 1849 if(Timeout != HAL_MAX_DELAY)
mbed_official 610:813dcc80987e 1850 {
mbed_official 610:813dcc80987e 1851 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 610:813dcc80987e 1852 {
mbed_official 610:813dcc80987e 1853 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1854 hsmbus->State= HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1855
mbed_official 610:813dcc80987e 1856 /* Process Unlocked */
mbed_official 610:813dcc80987e 1857 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1858
mbed_official 610:813dcc80987e 1859 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1860 }
mbed_official 610:813dcc80987e 1861 }
mbed_official 610:813dcc80987e 1862 }
mbed_official 610:813dcc80987e 1863 }
mbed_official 610:813dcc80987e 1864 else
mbed_official 610:813dcc80987e 1865 {
mbed_official 610:813dcc80987e 1866 while(__HAL_SMBUS_GET_FLAG(hsmbus, Flag) != RESET)
mbed_official 610:813dcc80987e 1867 {
mbed_official 610:813dcc80987e 1868 /* Check for the Timeout */
mbed_official 610:813dcc80987e 1869 if(Timeout != HAL_MAX_DELAY)
mbed_official 610:813dcc80987e 1870 {
mbed_official 610:813dcc80987e 1871 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 610:813dcc80987e 1872 {
mbed_official 610:813dcc80987e 1873 hsmbus->PreviousState = hsmbus->State;
mbed_official 610:813dcc80987e 1874 hsmbus->State= HAL_SMBUS_STATE_READY;
mbed_official 610:813dcc80987e 1875
mbed_official 610:813dcc80987e 1876 /* Process Unlocked */
mbed_official 610:813dcc80987e 1877 __HAL_UNLOCK(hsmbus);
mbed_official 610:813dcc80987e 1878
mbed_official 610:813dcc80987e 1879 return HAL_TIMEOUT;
mbed_official 610:813dcc80987e 1880 }
mbed_official 610:813dcc80987e 1881 }
mbed_official 610:813dcc80987e 1882 }
mbed_official 610:813dcc80987e 1883 }
mbed_official 610:813dcc80987e 1884 return HAL_OK;
mbed_official 610:813dcc80987e 1885 }
mbed_official 610:813dcc80987e 1886
mbed_official 610:813dcc80987e 1887 /**
mbed_official 610:813dcc80987e 1888 * @brief Handle SMBUSx communication when starting transfer or during transfer (TC or TCR flag are set).
mbed_official 610:813dcc80987e 1889 * @param hsmbus: SMBUS handle.
mbed_official 610:813dcc80987e 1890 * @param DevAddress: specifies the slave address to be programmed.
mbed_official 610:813dcc80987e 1891 * @param Size: specifies the number of bytes to be programmed.
mbed_official 610:813dcc80987e 1892 * This parameter must be a value between 0 and 255.
mbed_official 610:813dcc80987e 1893 * @param Mode: new state of the SMBUS START condition generation.
mbed_official 610:813dcc80987e 1894 * This parameter can be one or a combination of the following values:
mbed_official 610:813dcc80987e 1895 * @arg SMBUS_NO_MODE: No specific mode enabled.
mbed_official 610:813dcc80987e 1896 * @arg SMBUS_RELOAD_MODE: Enable Reload mode.
mbed_official 610:813dcc80987e 1897 * @arg SMBUS_AUTOEND_MODE: Enable Automatic end mode.
mbed_official 610:813dcc80987e 1898 * @arg SMBUS_SOFTEND_MODE: Enable Software end mode and Reload mode.
mbed_official 610:813dcc80987e 1899 * @param Request: new state of the SMBUS START condition generation.
mbed_official 610:813dcc80987e 1900 * This parameter can be one of the following values:
mbed_official 610:813dcc80987e 1901 * @arg SMBUS_NO_STARTSTOP: Don't Generate stop and start condition.
mbed_official 610:813dcc80987e 1902 * @arg SMBUS_GENERATE_STOP: Generate stop condition (Size should be set to 0).
mbed_official 610:813dcc80987e 1903 * @arg SMBUS_GENERATE_START_READ: Generate Restart for read request.
mbed_official 610:813dcc80987e 1904 * @arg SMBUS_GENERATE_START_WRITE: Generate Restart for write request.
mbed_official 610:813dcc80987e 1905 * @retval None
mbed_official 610:813dcc80987e 1906 */
mbed_official 610:813dcc80987e 1907 static void SMBUS_TransferConfig(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
mbed_official 610:813dcc80987e 1908 {
mbed_official 610:813dcc80987e 1909 uint32_t tmpreg = 0;
mbed_official 610:813dcc80987e 1910
mbed_official 610:813dcc80987e 1911 /* Check the parameters */
mbed_official 610:813dcc80987e 1912 assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
mbed_official 610:813dcc80987e 1913 assert_param(IS_SMBUS_TRANSFER_MODE(Mode));
mbed_official 610:813dcc80987e 1914 assert_param(IS_SMBUS_TRANSFER_REQUEST(Request));
mbed_official 610:813dcc80987e 1915
mbed_official 610:813dcc80987e 1916 /* Get the CR2 register value */
mbed_official 610:813dcc80987e 1917 tmpreg = hsmbus->Instance->CR2;
mbed_official 610:813dcc80987e 1918
mbed_official 610:813dcc80987e 1919 /* clear tmpreg specific bits */
mbed_official 610:813dcc80987e 1920 tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP | I2C_CR2_PECBYTE));
mbed_official 610:813dcc80987e 1921
mbed_official 610:813dcc80987e 1922 /* update tmpreg */
mbed_official 610:813dcc80987e 1923 tmpreg |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << 16 ) & I2C_CR2_NBYTES) | \
mbed_official 610:813dcc80987e 1924 (uint32_t)Mode | (uint32_t)Request);
mbed_official 610:813dcc80987e 1925
mbed_official 610:813dcc80987e 1926 /* update CR2 register */
mbed_official 610:813dcc80987e 1927 hsmbus->Instance->CR2 = tmpreg;
mbed_official 610:813dcc80987e 1928 }
mbed_official 610:813dcc80987e 1929 /**
mbed_official 610:813dcc80987e 1930 * @}
mbed_official 610:813dcc80987e 1931 */
mbed_official 610:813dcc80987e 1932
mbed_official 610:813dcc80987e 1933 #endif /* HAL_SMBUS_MODULE_ENABLED */
mbed_official 610:813dcc80987e 1934 /**
mbed_official 610:813dcc80987e 1935 * @}
mbed_official 610:813dcc80987e 1936 */
mbed_official 610:813dcc80987e 1937
mbed_official 610:813dcc80987e 1938 /**
mbed_official 610:813dcc80987e 1939 * @}
mbed_official 610:813dcc80987e 1940 */
mbed_official 610:813dcc80987e 1941
mbed_official 610:813dcc80987e 1942 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/