STM32L4xx_HAL_Driver  1.14.0

Functions

HAL_StatusTypeDef HAL_SMBUS_IsDeviceReady (SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
 Check if target device is ready for communication. More...
 

Detailed Description

Function Documentation

◆ HAL_SMBUS_IsDeviceReady()

HAL_StatusTypeDef HAL_SMBUS_IsDeviceReady ( SMBUS_HandleTypeDef hsmbus,
uint16_t  DevAddress,
uint32_t  Trials,
uint32_t  Timeout 
)

Check if target device is ready for communication.

Parameters
hsmbusPointer to a SMBUS_HandleTypeDef structure that contains the configuration information for the specified SMBUS.
DevAddressTarget device address: The device 7 bits address value in datasheet must be shifted to the left before calling the interface
TrialsNumber of trials
TimeoutTimeout duration
Return values
HALstatus

Definition at line 1420 of file stm32l4xx_hal_smbus.c.

1421 {
1422  uint32_t tickstart;
1423 
1424  __IO uint32_t SMBUS_Trials = 0UL;
1425 
1426  FlagStatus tmp1;
1427  FlagStatus tmp2;
1428 
1429  if (hsmbus->State == HAL_SMBUS_STATE_READY)
1430  {
1431  if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_BUSY) != RESET)
1432  {
1433  return HAL_BUSY;
1434  }
1435 
1436  /* Process Locked */
1437  __HAL_LOCK(hsmbus);
1438 
1439  hsmbus->State = HAL_SMBUS_STATE_BUSY;
1440  hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
1441 
1442  do
1443  {
1444  /* Generate Start */
1445  hsmbus->Instance->CR2 = SMBUS_GENERATE_START(hsmbus->Init.AddressingMode, DevAddress);
1446 
1447  /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
1448  /* Wait until STOPF flag is set or a NACK flag is set*/
1449  tickstart = HAL_GetTick();
1450 
1451  tmp1 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF);
1452  tmp2 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF);
1453 
1454  while ((tmp1 == RESET) && (tmp2 == RESET))
1455  {
1456  if (Timeout != HAL_MAX_DELAY)
1457  {
1458  if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1459  {
1460  /* Device is ready */
1461  hsmbus->State = HAL_SMBUS_STATE_READY;
1462 
1463  /* Update SMBUS error code */
1464  hsmbus->ErrorCode |= HAL_SMBUS_ERROR_HALTIMEOUT;
1465 
1466  /* Process Unlocked */
1467  __HAL_UNLOCK(hsmbus);
1468  return HAL_ERROR;
1469  }
1470  }
1471 
1472  tmp1 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF);
1473  tmp2 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF);
1474  }
1475 
1476  /* Check if the NACKF flag has not been set */
1477  if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) == RESET)
1478  {
1479  /* Wait until STOPF flag is reset */
1480  if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
1481  {
1482  return HAL_ERROR;
1483  }
1484 
1485  /* Clear STOP Flag */
1486  __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
1487 
1488  /* Device is ready */
1489  hsmbus->State = HAL_SMBUS_STATE_READY;
1490 
1491  /* Process Unlocked */
1492  __HAL_UNLOCK(hsmbus);
1493 
1494  return HAL_OK;
1495  }
1496  else
1497  {
1498  /* Wait until STOPF flag is reset */
1499  if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
1500  {
1501  return HAL_ERROR;
1502  }
1503 
1504  /* Clear NACK Flag */
1505  __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
1506 
1507  /* Clear STOP Flag, auto generated with autoend*/
1508  __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
1509  }
1510 
1511  /* Check if the maximum allowed number of trials has been reached */
1512  if (SMBUS_Trials == Trials)
1513  {
1514  /* Generate Stop */
1515  hsmbus->Instance->CR2 |= I2C_CR2_STOP;
1516 
1517  /* Wait until STOPF flag is reset */
1518  if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
1519  {
1520  return HAL_ERROR;
1521  }
1522 
1523  /* Clear STOP Flag */
1524  __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
1525  }
1526 
1527  /* Increment Trials */
1528  SMBUS_Trials++;
1529  }
1530  while (SMBUS_Trials < Trials);
1531 
1532  hsmbus->State = HAL_SMBUS_STATE_READY;
1533 
1534  /* Update SMBUS error code */
1535  hsmbus->ErrorCode |= HAL_SMBUS_ERROR_HALTIMEOUT;
1536 
1537  /* Process Unlocked */
1538  __HAL_UNLOCK(hsmbus);
1539 
1540  return HAL_ERROR;
1541  }
1542  else
1543  {
1544  return HAL_BUSY;
1545  }
1546 }
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK
static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(struct __SMBUS_HandleTypeDef *hsmbus, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
Handle SMBUS Communication Timeout.