STM32F4xx HAL Driver master
STM32CubeF4 HAL / LL Drivers API Reference
Loading...
Searching...
No Matches
stm32f4xx_hal_i2c.c
Go to the documentation of this file.
1
299
300/* Includes ------------------------------------------------------------------*/
301#include "stm32f4xx_hal.h"
302
306
311
312#ifdef HAL_I2C_MODULE_ENABLED
313
314/* Private typedef -----------------------------------------------------------*/
315/* Private define ------------------------------------------------------------*/
319#define I2C_TIMEOUT_FLAG 35U
320#define I2C_TIMEOUT_BUSY_FLAG 25U
321#define I2C_TIMEOUT_STOP_FLAG 5U
322#define I2C_NO_OPTION_FRAME 0xFFFF0000U
323
324/* Private define for @ref PreviousState usage */
325#define I2C_STATE_MSK ((uint32_t)((uint32_t)((uint32_t)HAL_I2C_STATE_BUSY_TX | (uint32_t)HAL_I2C_STATE_BUSY_RX) & (uint32_t)(~((uint32_t)HAL_I2C_STATE_READY))))
326#define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE))
327#define I2C_STATE_MASTER_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER))
328#define I2C_STATE_MASTER_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_MASTER))
329#define I2C_STATE_SLAVE_BUSY_TX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_TX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE))
330#define I2C_STATE_SLAVE_BUSY_RX ((uint32_t)(((uint32_t)HAL_I2C_STATE_BUSY_RX & I2C_STATE_MSK) | (uint32_t)HAL_I2C_MODE_SLAVE))
331
335
336/* Private macro -------------------------------------------------------------*/
340/* Macro to get remaining data to transfer on DMA side */
341#define I2C_GET_DMA_REMAIN_DATA(__HANDLE__) __HAL_DMA_GET_COUNTER(__HANDLE__)
345/* Private variables ---------------------------------------------------------*/
346/* Private function prototypes -----------------------------------------------*/
347
351/* Private functions to handle DMA transfer */
352static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma);
353static void I2C_DMAError(DMA_HandleTypeDef *hdma);
354static void I2C_DMAAbort(DMA_HandleTypeDef *hdma);
355
356static void I2C_ITError(I2C_HandleTypeDef *hi2c);
357
358static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
359static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart);
360static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
361static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart);
362
363/* Private functions to handle flags during polling transfer */
364static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart);
365static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart);
366static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
367static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
368static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
369static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart);
370static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c);
371static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c);
372
373/* Private functions for I2C transfer IRQ handler */
374static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c);
375static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c);
376static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c);
377static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c);
378static void I2C_Master_SB(I2C_HandleTypeDef *hi2c);
379static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c);
380static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c);
381
382static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c);
383static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c);
384static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c);
385static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c);
386static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags);
387static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c);
388static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c);
389
390static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c);
391
392/* Private function to Convert Specific options */
393static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c);
394
395/* Private function to flush DR register */
396static void I2C_Flush_DR(I2C_HandleTypeDef *hi2c);
400
401/* Exported functions --------------------------------------------------------*/
402
406
437
446{
447 uint32_t freqrange;
448 uint32_t pclk1;
449
450 /* Check the I2C handle allocation */
451 if (hi2c == NULL)
452 {
453 return HAL_ERROR;
454 }
455
456 /* Check the parameters */
457 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
466
467 if (hi2c->State == HAL_I2C_STATE_RESET)
468 {
469 /* Allocate lock resource and initialize it */
470 hi2c->Lock = HAL_UNLOCKED;
471
472#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
473 /* Init the I2C Callback settings */
474 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
475 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
476 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
477 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
478 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
479 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
480 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
481 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
482 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
483 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
484
485 if (hi2c->MspInitCallback == NULL)
486 {
487 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
488 }
489
490 /* Init the low level hardware : GPIO, CLOCK, NVIC */
491 hi2c->MspInitCallback(hi2c);
492#else
493 /* Init the low level hardware : GPIO, CLOCK, NVIC */
494 HAL_I2C_MspInit(hi2c);
495#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
496 }
497
499
500 /* Disable the selected I2C peripheral */
501 __HAL_I2C_DISABLE(hi2c);
502
503 /*Reset I2C*/
504 hi2c->Instance->CR1 |= I2C_CR1_SWRST;
505 hi2c->Instance->CR1 &= ~I2C_CR1_SWRST;
506
507 /* Get PCLK1 frequency */
508 pclk1 = HAL_RCC_GetPCLK1Freq();
509
510 /* Check the minimum allowed PCLK1 frequency */
511 if (I2C_MIN_PCLK_FREQ(pclk1, hi2c->Init.ClockSpeed) == 1U)
512 {
513 return HAL_ERROR;
514 }
515
516 /* Calculate frequency range */
517 freqrange = I2C_FREQRANGE(pclk1);
518
519 /*---------------------------- I2Cx CR2 Configuration ----------------------*/
520 /* Configure I2Cx: Frequency range */
521 MODIFY_REG(hi2c->Instance->CR2, I2C_CR2_FREQ, freqrange);
522
523 /*---------------------------- I2Cx TRISE Configuration --------------------*/
524 /* Configure I2Cx: Rise Time */
525 MODIFY_REG(hi2c->Instance->TRISE, I2C_TRISE_TRISE, I2C_RISE_TIME(freqrange, hi2c->Init.ClockSpeed));
526
527 /*---------------------------- I2Cx CCR Configuration ----------------------*/
528 /* Configure I2Cx: Speed */
529 MODIFY_REG(hi2c->Instance->CCR, (I2C_CCR_FS | I2C_CCR_DUTY | I2C_CCR_CCR), I2C_SPEED(pclk1, hi2c->Init.ClockSpeed, hi2c->Init.DutyCycle));
530
531 /*---------------------------- I2Cx CR1 Configuration ----------------------*/
532 /* Configure I2Cx: Generalcall and NoStretch mode */
533 MODIFY_REG(hi2c->Instance->CR1, (I2C_CR1_ENGC | I2C_CR1_NOSTRETCH), (hi2c->Init.GeneralCallMode | hi2c->Init.NoStretchMode));
534
535 /*---------------------------- I2Cx OAR1 Configuration ---------------------*/
536 /* Configure I2Cx: Own Address1 and addressing mode */
537 MODIFY_REG(hi2c->Instance->OAR1, (I2C_OAR1_ADDMODE | I2C_OAR1_ADD8_9 | I2C_OAR1_ADD1_7 | I2C_OAR1_ADD0), (hi2c->Init.AddressingMode | hi2c->Init.OwnAddress1));
538
539 /*---------------------------- I2Cx OAR2 Configuration ---------------------*/
540 /* Configure I2Cx: Dual mode and Own Address2 */
541 MODIFY_REG(hi2c->Instance->OAR2, (I2C_OAR2_ENDUAL | I2C_OAR2_ADD2), (hi2c->Init.DualAddressMode | hi2c->Init.OwnAddress2));
542
543 /* Enable the selected I2C peripheral */
544 __HAL_I2C_ENABLE(hi2c);
545
548 hi2c->PreviousState = I2C_STATE_NONE;
549 hi2c->Mode = HAL_I2C_MODE_NONE;
550
551 return HAL_OK;
552}
553
561{
562 /* Check the I2C handle allocation */
563 if (hi2c == NULL)
564 {
565 return HAL_ERROR;
566 }
567
568 /* Check the parameters */
569 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
570
572
573 /* Disable the I2C Peripheral Clock */
574 __HAL_I2C_DISABLE(hi2c);
575
576#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
577 if (hi2c->MspDeInitCallback == NULL)
578 {
579 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
580 }
581
582 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
583 hi2c->MspDeInitCallback(hi2c);
584#else
585 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
586 HAL_I2C_MspDeInit(hi2c);
587#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
588
591 hi2c->PreviousState = I2C_STATE_NONE;
592 hi2c->Mode = HAL_I2C_MODE_NONE;
593
594 /* Release Lock */
595 __HAL_UNLOCK(hi2c);
596
597 return HAL_OK;
598}
599
606__weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
607{
608 /* Prevent unused argument(s) compilation warning */
609 UNUSED(hi2c);
610
611 /* NOTE : This function should not be modified, when the callback is needed,
612 the HAL_I2C_MspInit could be implemented in the user file
613 */
614}
615
622__weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
623{
624 /* Prevent unused argument(s) compilation warning */
625 UNUSED(hi2c);
626
627 /* NOTE : This function should not be modified, when the callback is needed,
628 the HAL_I2C_MspDeInit could be implemented in the user file
629 */
630}
631
632#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
654HAL_StatusTypeDef HAL_I2C_RegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID, pI2C_CallbackTypeDef pCallback)
655{
656 HAL_StatusTypeDef status = HAL_OK;
657
658 if (pCallback == NULL)
659 {
660 /* Update the error code */
661 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
662
663 return HAL_ERROR;
664 }
665 /* Process locked */
666 __HAL_LOCK(hi2c);
667
668 if (HAL_I2C_STATE_READY == hi2c->State)
669 {
670 switch (CallbackID)
671 {
672 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
673 hi2c->MasterTxCpltCallback = pCallback;
674 break;
675
676 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
677 hi2c->MasterRxCpltCallback = pCallback;
678 break;
679
680 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
681 hi2c->SlaveTxCpltCallback = pCallback;
682 break;
683
684 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
685 hi2c->SlaveRxCpltCallback = pCallback;
686 break;
687
688 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
689 hi2c->ListenCpltCallback = pCallback;
690 break;
691
692 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
693 hi2c->MemTxCpltCallback = pCallback;
694 break;
695
696 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
697 hi2c->MemRxCpltCallback = pCallback;
698 break;
699
700 case HAL_I2C_ERROR_CB_ID :
701 hi2c->ErrorCallback = pCallback;
702 break;
703
704 case HAL_I2C_ABORT_CB_ID :
705 hi2c->AbortCpltCallback = pCallback;
706 break;
707
708 case HAL_I2C_MSPINIT_CB_ID :
709 hi2c->MspInitCallback = pCallback;
710 break;
711
712 case HAL_I2C_MSPDEINIT_CB_ID :
713 hi2c->MspDeInitCallback = pCallback;
714 break;
715
716 default :
717 /* Update the error code */
718 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
719
720 /* Return error status */
721 status = HAL_ERROR;
722 break;
723 }
724 }
725 else if (HAL_I2C_STATE_RESET == hi2c->State)
726 {
727 switch (CallbackID)
728 {
729 case HAL_I2C_MSPINIT_CB_ID :
730 hi2c->MspInitCallback = pCallback;
731 break;
732
733 case HAL_I2C_MSPDEINIT_CB_ID :
734 hi2c->MspDeInitCallback = pCallback;
735 break;
736
737 default :
738 /* Update the error code */
739 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
740
741 /* Return error status */
742 status = HAL_ERROR;
743 break;
744 }
745 }
746 else
747 {
748 /* Update the error code */
749 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
750
751 /* Return error status */
752 status = HAL_ERROR;
753 }
754
755 /* Release Lock */
756 __HAL_UNLOCK(hi2c);
757 return status;
758}
759
781HAL_StatusTypeDef HAL_I2C_UnRegisterCallback(I2C_HandleTypeDef *hi2c, HAL_I2C_CallbackIDTypeDef CallbackID)
782{
783 HAL_StatusTypeDef status = HAL_OK;
784
785 /* Process locked */
786 __HAL_LOCK(hi2c);
787
788 if (HAL_I2C_STATE_READY == hi2c->State)
789 {
790 switch (CallbackID)
791 {
792 case HAL_I2C_MASTER_TX_COMPLETE_CB_ID :
793 hi2c->MasterTxCpltCallback = HAL_I2C_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
794 break;
795
796 case HAL_I2C_MASTER_RX_COMPLETE_CB_ID :
797 hi2c->MasterRxCpltCallback = HAL_I2C_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
798 break;
799
800 case HAL_I2C_SLAVE_TX_COMPLETE_CB_ID :
801 hi2c->SlaveTxCpltCallback = HAL_I2C_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
802 break;
803
804 case HAL_I2C_SLAVE_RX_COMPLETE_CB_ID :
805 hi2c->SlaveRxCpltCallback = HAL_I2C_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
806 break;
807
808 case HAL_I2C_LISTEN_COMPLETE_CB_ID :
809 hi2c->ListenCpltCallback = HAL_I2C_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
810 break;
811
812 case HAL_I2C_MEM_TX_COMPLETE_CB_ID :
813 hi2c->MemTxCpltCallback = HAL_I2C_MemTxCpltCallback; /* Legacy weak MemTxCpltCallback */
814 break;
815
816 case HAL_I2C_MEM_RX_COMPLETE_CB_ID :
817 hi2c->MemRxCpltCallback = HAL_I2C_MemRxCpltCallback; /* Legacy weak MemRxCpltCallback */
818 break;
819
820 case HAL_I2C_ERROR_CB_ID :
821 hi2c->ErrorCallback = HAL_I2C_ErrorCallback; /* Legacy weak ErrorCallback */
822 break;
823
824 case HAL_I2C_ABORT_CB_ID :
825 hi2c->AbortCpltCallback = HAL_I2C_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
826 break;
827
828 case HAL_I2C_MSPINIT_CB_ID :
829 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
830 break;
831
832 case HAL_I2C_MSPDEINIT_CB_ID :
833 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
834 break;
835
836 default :
837 /* Update the error code */
838 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
839
840 /* Return error status */
841 status = HAL_ERROR;
842 break;
843 }
844 }
845 else if (HAL_I2C_STATE_RESET == hi2c->State)
846 {
847 switch (CallbackID)
848 {
849 case HAL_I2C_MSPINIT_CB_ID :
850 hi2c->MspInitCallback = HAL_I2C_MspInit; /* Legacy weak MspInit */
851 break;
852
853 case HAL_I2C_MSPDEINIT_CB_ID :
854 hi2c->MspDeInitCallback = HAL_I2C_MspDeInit; /* Legacy weak MspDeInit */
855 break;
856
857 default :
858 /* Update the error code */
859 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
860
861 /* Return error status */
862 status = HAL_ERROR;
863 break;
864 }
865 }
866 else
867 {
868 /* Update the error code */
869 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
870
871 /* Return error status */
872 status = HAL_ERROR;
873 }
874
875 /* Release Lock */
876 __HAL_UNLOCK(hi2c);
877 return status;
878}
879
888HAL_StatusTypeDef HAL_I2C_RegisterAddrCallback(I2C_HandleTypeDef *hi2c, pI2C_AddrCallbackTypeDef pCallback)
889{
890 HAL_StatusTypeDef status = HAL_OK;
891
892 if (pCallback == NULL)
893 {
894 /* Update the error code */
895 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
896
897 return HAL_ERROR;
898 }
899 /* Process locked */
900 __HAL_LOCK(hi2c);
901
902 if (HAL_I2C_STATE_READY == hi2c->State)
903 {
904 hi2c->AddrCallback = pCallback;
905 }
906 else
907 {
908 /* Update the error code */
909 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
910
911 /* Return error status */
912 status = HAL_ERROR;
913 }
914
915 /* Release Lock */
916 __HAL_UNLOCK(hi2c);
917 return status;
918}
919
927HAL_StatusTypeDef HAL_I2C_UnRegisterAddrCallback(I2C_HandleTypeDef *hi2c)
928{
929 HAL_StatusTypeDef status = HAL_OK;
930
931 /* Process locked */
932 __HAL_LOCK(hi2c);
933
934 if (HAL_I2C_STATE_READY == hi2c->State)
935 {
936 hi2c->AddrCallback = HAL_I2C_AddrCallback; /* Legacy weak AddrCallback */
937 }
938 else
939 {
940 /* Update the error code */
941 hi2c->ErrorCode |= HAL_I2C_ERROR_INVALID_CALLBACK;
942
943 /* Return error status */
944 status = HAL_ERROR;
945 }
946
947 /* Release Lock */
948 __HAL_UNLOCK(hi2c);
949 return status;
950}
951
952#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
953
959static void I2C_Flush_DR(I2C_HandleTypeDef *hi2c)
960{
961 /* Write a dummy data in DR to clear TXE flag */
962 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) != RESET)
963 {
964 hi2c->Instance->DR = 0x00U;
965 }
966}
967
971
1044
1056HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1057{
1058 /* Init tickstart for timeout management*/
1059 uint32_t tickstart = HAL_GetTick();
1060
1061 if (hi2c->State == HAL_I2C_STATE_READY)
1062 {
1063 /* Wait until BUSY flag is reset */
1064 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
1065 {
1066 return HAL_BUSY;
1067 }
1068
1069 /* Process Locked */
1070 __HAL_LOCK(hi2c);
1071
1072 /* Check if the I2C is already enabled */
1073 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1074 {
1075 /* Enable I2C peripheral */
1076 __HAL_I2C_ENABLE(hi2c);
1077 }
1078
1079 /* Disable Pos */
1080 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1081
1083 hi2c->Mode = HAL_I2C_MODE_MASTER;
1085
1086 /* Prepare transfer parameters */
1087 hi2c->pBuffPtr = pData;
1088 hi2c->XferCount = Size;
1089 hi2c->XferSize = hi2c->XferCount;
1090 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1091
1092 /* Send Slave Address */
1093 if (I2C_MasterRequestWrite(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
1094 {
1095 return HAL_ERROR;
1096 }
1097
1098 /* Clear ADDR flag */
1100
1101 while (hi2c->XferSize > 0U)
1102 {
1103 /* Wait until TXE flag is set */
1104 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1105 {
1106 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1107 {
1108 /* Generate Stop */
1109 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1110 }
1111 return HAL_ERROR;
1112 }
1113
1114 /* Write data to DR */
1115 hi2c->Instance->DR = *hi2c->pBuffPtr;
1116
1117 /* Increment Buffer pointer */
1118 hi2c->pBuffPtr++;
1119
1120 /* Update counter */
1121 hi2c->XferCount--;
1122 hi2c->XferSize--;
1123
1124 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
1125 {
1126 /* Write data to DR */
1127 hi2c->Instance->DR = *hi2c->pBuffPtr;
1128
1129 /* Increment Buffer pointer */
1130 hi2c->pBuffPtr++;
1131
1132 /* Update counter */
1133 hi2c->XferCount--;
1134 hi2c->XferSize--;
1135 }
1136
1137 /* Wait until BTF flag is set */
1138 if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1139 {
1140 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
1141 {
1142 /* Generate Stop */
1143 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1144 }
1145 return HAL_ERROR;
1146 }
1147 }
1148
1149 /* Generate Stop */
1150 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1151
1152 hi2c->State = HAL_I2C_STATE_READY;
1153 hi2c->Mode = HAL_I2C_MODE_NONE;
1154
1155 /* Process Unlocked */
1156 __HAL_UNLOCK(hi2c);
1157
1158 return HAL_OK;
1159 }
1160 else
1161 {
1162 return HAL_BUSY;
1163 }
1164}
1165
1177HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1178{
1179 /* Init tickstart for timeout management*/
1180 uint32_t tickstart = HAL_GetTick();
1181
1182 if (hi2c->State == HAL_I2C_STATE_READY)
1183 {
1184 /* Wait until BUSY flag is reset */
1185 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
1186 {
1187 return HAL_BUSY;
1188 }
1189
1190 /* Process Locked */
1191 __HAL_LOCK(hi2c);
1192
1193 /* Check if the I2C is already enabled */
1194 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1195 {
1196 /* Enable I2C peripheral */
1197 __HAL_I2C_ENABLE(hi2c);
1198 }
1199
1200 /* Disable Pos */
1201 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1202
1204 hi2c->Mode = HAL_I2C_MODE_MASTER;
1206
1207 /* Prepare transfer parameters */
1208 hi2c->pBuffPtr = pData;
1209 hi2c->XferCount = Size;
1210 hi2c->XferSize = hi2c->XferCount;
1211 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1212
1213 /* Send Slave Address */
1214 if (I2C_MasterRequestRead(hi2c, DevAddress, Timeout, tickstart) != HAL_OK)
1215 {
1216 return HAL_ERROR;
1217 }
1218
1219 if (hi2c->XferSize == 0U)
1220 {
1221 /* Clear ADDR flag */
1223
1224 /* Generate Stop */
1225 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1226 }
1227 else if (hi2c->XferSize == 1U)
1228 {
1229 /* Disable Acknowledge */
1230 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1231
1232 /* Clear ADDR flag */
1234
1235 /* Generate Stop */
1236 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1237 }
1238 else if (hi2c->XferSize == 2U)
1239 {
1240 /* Disable Acknowledge */
1241 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1242
1243 /* Enable Pos */
1244 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1245
1246 /* Clear ADDR flag */
1248 }
1249 else
1250 {
1251 /* Enable Acknowledge */
1252 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1253
1254 /* Clear ADDR flag */
1256 }
1257
1258 while (hi2c->XferSize > 0U)
1259 {
1260 if (hi2c->XferSize <= 3U)
1261 {
1262 /* One byte */
1263 if (hi2c->XferSize == 1U)
1264 {
1265 /* Wait until RXNE flag is set */
1266 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1267 {
1268 return HAL_ERROR;
1269 }
1270
1271 /* Read data from DR */
1272 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1273
1274 /* Increment Buffer pointer */
1275 hi2c->pBuffPtr++;
1276
1277 /* Update counter */
1278 hi2c->XferSize--;
1279 hi2c->XferCount--;
1280 }
1281 /* Two bytes */
1282 else if (hi2c->XferSize == 2U)
1283 {
1284 /* Wait until BTF flag is set */
1285 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
1286 {
1287 return HAL_ERROR;
1288 }
1289
1290 /* Generate Stop */
1291 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1292
1293 /* Read data from DR */
1294 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1295
1296 /* Increment Buffer pointer */
1297 hi2c->pBuffPtr++;
1298
1299 /* Update counter */
1300 hi2c->XferSize--;
1301 hi2c->XferCount--;
1302
1303 /* Read data from DR */
1304 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1305
1306 /* Increment Buffer pointer */
1307 hi2c->pBuffPtr++;
1308
1309 /* Update counter */
1310 hi2c->XferSize--;
1311 hi2c->XferCount--;
1312 }
1313 /* 3 Last bytes */
1314 else
1315 {
1316 /* Wait until BTF flag is set */
1317 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
1318 {
1319 return HAL_ERROR;
1320 }
1321
1322 /* Disable Acknowledge */
1323 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1324
1325 /* Read data from DR */
1326 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1327
1328 /* Increment Buffer pointer */
1329 hi2c->pBuffPtr++;
1330
1331 /* Update counter */
1332 hi2c->XferSize--;
1333 hi2c->XferCount--;
1334
1335 /* Wait until BTF flag is set */
1336 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
1337 {
1338 return HAL_ERROR;
1339 }
1340
1341 /* Generate Stop */
1342 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
1343
1344 /* Read data from DR */
1345 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1346
1347 /* Increment Buffer pointer */
1348 hi2c->pBuffPtr++;
1349
1350 /* Update counter */
1351 hi2c->XferSize--;
1352 hi2c->XferCount--;
1353
1354 /* Read data from DR */
1355 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1356
1357 /* Increment Buffer pointer */
1358 hi2c->pBuffPtr++;
1359
1360 /* Update counter */
1361 hi2c->XferSize--;
1362 hi2c->XferCount--;
1363 }
1364 }
1365 else
1366 {
1367 /* Wait until RXNE flag is set */
1368 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1369 {
1370 return HAL_ERROR;
1371 }
1372
1373 /* Read data from DR */
1374 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1375
1376 /* Increment Buffer pointer */
1377 hi2c->pBuffPtr++;
1378
1379 /* Update counter */
1380 hi2c->XferSize--;
1381 hi2c->XferCount--;
1382
1383 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
1384 {
1385
1386 if (hi2c->XferSize == 3U)
1387 {
1388 /* Disable Acknowledge */
1389 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1390 }
1391
1392 /* Read data from DR */
1393 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1394
1395 /* Increment Buffer pointer */
1396 hi2c->pBuffPtr++;
1397
1398 /* Update counter */
1399 hi2c->XferSize--;
1400 hi2c->XferCount--;
1401 }
1402 }
1403 }
1404
1405 hi2c->State = HAL_I2C_STATE_READY;
1406 hi2c->Mode = HAL_I2C_MODE_NONE;
1407
1408 /* Process Unlocked */
1409 __HAL_UNLOCK(hi2c);
1410
1411 return HAL_OK;
1412 }
1413 else
1414 {
1415 return HAL_BUSY;
1416 }
1417}
1418
1428HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1429{
1430 /* Init tickstart for timeout management*/
1431 uint32_t tickstart = HAL_GetTick();
1432
1433 if (hi2c->State == HAL_I2C_STATE_READY)
1434 {
1435 if ((pData == NULL) || (Size == 0U))
1436 {
1437 return HAL_ERROR;
1438 }
1439
1440 /* Process Locked */
1441 __HAL_LOCK(hi2c);
1442
1443 /* Check if the I2C is already enabled */
1444 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1445 {
1446 /* Enable I2C peripheral */
1447 __HAL_I2C_ENABLE(hi2c);
1448 }
1449
1450 /* Disable Pos */
1451 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1452
1454 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1456
1457 /* Prepare transfer parameters */
1458 hi2c->pBuffPtr = pData;
1459 hi2c->XferCount = Size;
1460 hi2c->XferSize = hi2c->XferCount;
1461 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1462
1463 /* Enable Address Acknowledge */
1464 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1465
1466 /* Wait until ADDR flag is set */
1467 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1468 {
1469 return HAL_ERROR;
1470 }
1471
1472 /* Clear ADDR flag */
1474
1475 /* If 10bit addressing mode is selected */
1477 {
1478 /* Wait until ADDR flag is set */
1479 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1480 {
1481 return HAL_ERROR;
1482 }
1483
1484 /* Clear ADDR flag */
1486 }
1487
1488 while (hi2c->XferSize > 0U)
1489 {
1490 /* Wait until TXE flag is set */
1491 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1492 {
1493 /* Disable Address Acknowledge */
1494 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1495
1496 return HAL_ERROR;
1497 }
1498
1499 /* Write data to DR */
1500 hi2c->Instance->DR = *hi2c->pBuffPtr;
1501
1502 /* Increment Buffer pointer */
1503 hi2c->pBuffPtr++;
1504
1505 /* Update counter */
1506 hi2c->XferCount--;
1507 hi2c->XferSize--;
1508
1509 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
1510 {
1511 /* Write data to DR */
1512 hi2c->Instance->DR = *hi2c->pBuffPtr;
1513
1514 /* Increment Buffer pointer */
1515 hi2c->pBuffPtr++;
1516
1517 /* Update counter */
1518 hi2c->XferCount--;
1519 hi2c->XferSize--;
1520 }
1521 }
1522
1523 /* Wait until AF flag is set */
1524 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_AF, RESET, Timeout, tickstart) != HAL_OK)
1525 {
1526 return HAL_ERROR;
1527 }
1528
1529 /* Clear AF flag */
1531
1532 /* Disable Address Acknowledge */
1533 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1534
1535 hi2c->State = HAL_I2C_STATE_READY;
1536 hi2c->Mode = HAL_I2C_MODE_NONE;
1537
1538 /* Process Unlocked */
1539 __HAL_UNLOCK(hi2c);
1540
1541 return HAL_OK;
1542 }
1543 else
1544 {
1545 return HAL_BUSY;
1546 }
1547}
1548
1558HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1559{
1560 /* Init tickstart for timeout management*/
1561 uint32_t tickstart = HAL_GetTick();
1562
1563 if (hi2c->State == HAL_I2C_STATE_READY)
1564 {
1565 if ((pData == NULL) || (Size == (uint16_t)0))
1566 {
1567 return HAL_ERROR;
1568 }
1569
1570 /* Process Locked */
1571 __HAL_LOCK(hi2c);
1572
1573 /* Check if the I2C is already enabled */
1574 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1575 {
1576 /* Enable I2C peripheral */
1577 __HAL_I2C_ENABLE(hi2c);
1578 }
1579
1580 /* Disable Pos */
1581 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1582
1584 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1586
1587 /* Prepare transfer parameters */
1588 hi2c->pBuffPtr = pData;
1589 hi2c->XferCount = Size;
1590 hi2c->XferSize = hi2c->XferCount;
1591 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1592
1593 /* Enable Address Acknowledge */
1594 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1595
1596 /* Wait until ADDR flag is set */
1597 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, RESET, Timeout, tickstart) != HAL_OK)
1598 {
1599 return HAL_ERROR;
1600 }
1601
1602 /* Clear ADDR flag */
1604
1605 while (hi2c->XferSize > 0U)
1606 {
1607 /* Wait until RXNE flag is set */
1608 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1609 {
1610 /* Disable Address Acknowledge */
1611 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1612
1613 return HAL_ERROR;
1614 }
1615
1616 /* Read data from DR */
1617 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1618
1619 /* Increment Buffer pointer */
1620 hi2c->pBuffPtr++;
1621
1622 /* Update counter */
1623 hi2c->XferSize--;
1624 hi2c->XferCount--;
1625
1626 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
1627 {
1628 /* Read data from DR */
1629 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
1630
1631 /* Increment Buffer pointer */
1632 hi2c->pBuffPtr++;
1633
1634 /* Update counter */
1635 hi2c->XferSize--;
1636 hi2c->XferCount--;
1637 }
1638 }
1639
1640 /* Wait until STOP flag is set */
1641 if (I2C_WaitOnSTOPFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
1642 {
1643 /* Disable Address Acknowledge */
1644 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1645
1646 return HAL_ERROR;
1647 }
1648
1649 /* Clear STOP flag */
1651
1652 /* Disable Address Acknowledge */
1653 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1654
1655 hi2c->State = HAL_I2C_STATE_READY;
1656 hi2c->Mode = HAL_I2C_MODE_NONE;
1657
1658 /* Process Unlocked */
1659 __HAL_UNLOCK(hi2c);
1660
1661 return HAL_OK;
1662 }
1663 else
1664 {
1665 return HAL_BUSY;
1666 }
1667}
1668
1679HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1680{
1681 __IO uint32_t count = 0U;
1682
1683 if (hi2c->State == HAL_I2C_STATE_READY)
1684 {
1685 /* Wait until BUSY flag is reset */
1686 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
1687 do
1688 {
1689 count--;
1690 if (count == 0U)
1691 {
1692 hi2c->PreviousState = I2C_STATE_NONE;
1693 hi2c->State = HAL_I2C_STATE_READY;
1694 hi2c->Mode = HAL_I2C_MODE_NONE;
1696
1697 return HAL_BUSY;
1698 }
1699 }
1700 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1701
1702 /* Process Locked */
1703 __HAL_LOCK(hi2c);
1704
1705 /* Check if the I2C is already enabled */
1706 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1707 {
1708 /* Enable I2C peripheral */
1709 __HAL_I2C_ENABLE(hi2c);
1710 }
1711
1712 /* Disable Pos */
1713 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1714
1716 hi2c->Mode = HAL_I2C_MODE_MASTER;
1718
1719 /* Prepare transfer parameters */
1720 hi2c->pBuffPtr = pData;
1721 hi2c->XferCount = Size;
1722 hi2c->XferSize = hi2c->XferCount;
1723 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1724 hi2c->Devaddress = DevAddress;
1725
1726 /* Process Unlocked */
1727 __HAL_UNLOCK(hi2c);
1728
1729 /* Note : The I2C interrupts must be enabled after unlocking current process
1730 to avoid the risk of I2C interrupt handle execution before current
1731 process unlock */
1732 /* Enable EVT, BUF and ERR interrupt */
1734
1735 /* Generate Start */
1736 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
1737
1738 return HAL_OK;
1739 }
1740 else
1741 {
1742 return HAL_BUSY;
1743 }
1744}
1745
1756HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1757{
1758 __IO uint32_t count = 0U;
1759
1760 if (hi2c->State == HAL_I2C_STATE_READY)
1761 {
1762 /* Wait until BUSY flag is reset */
1763 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
1764 do
1765 {
1766 count--;
1767 if (count == 0U)
1768 {
1769 hi2c->PreviousState = I2C_STATE_NONE;
1770 hi2c->State = HAL_I2C_STATE_READY;
1771 hi2c->Mode = HAL_I2C_MODE_NONE;
1773
1774 return HAL_BUSY;
1775 }
1776 }
1777 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1778
1779 /* Process Locked */
1780 __HAL_LOCK(hi2c);
1781
1782 /* Check if the I2C is already enabled */
1783 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1784 {
1785 /* Enable I2C peripheral */
1786 __HAL_I2C_ENABLE(hi2c);
1787 }
1788
1789 /* Disable Pos */
1790 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1791
1793 hi2c->Mode = HAL_I2C_MODE_MASTER;
1795
1796 /* Prepare transfer parameters */
1797 hi2c->pBuffPtr = pData;
1798 hi2c->XferCount = Size;
1799 hi2c->XferSize = hi2c->XferCount;
1800 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1801 hi2c->Devaddress = DevAddress;
1802
1803
1804 /* Process Unlocked */
1805 __HAL_UNLOCK(hi2c);
1806
1807 /* Note : The I2C interrupts must be enabled after unlocking current process
1808 to avoid the risk of I2C interrupt handle execution before current
1809 process unlock */
1810
1811 /* Enable EVT, BUF and ERR interrupt */
1813
1814 /* Enable Acknowledge */
1815 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1816
1817 /* Generate Start */
1818 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
1819
1820 return HAL_OK;
1821 }
1822 else
1823 {
1824 return HAL_BUSY;
1825 }
1826}
1827
1836HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1837{
1838
1839 if (hi2c->State == HAL_I2C_STATE_READY)
1840 {
1841 if ((pData == NULL) || (Size == 0U))
1842 {
1843 return HAL_ERROR;
1844 }
1845
1846 /* Process Locked */
1847 __HAL_LOCK(hi2c);
1848
1849 /* Check if the I2C is already enabled */
1850 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1851 {
1852 /* Enable I2C peripheral */
1853 __HAL_I2C_ENABLE(hi2c);
1854 }
1855
1856 /* Disable Pos */
1857 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1858
1860 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1862
1863 /* Prepare transfer parameters */
1864 hi2c->pBuffPtr = pData;
1865 hi2c->XferCount = Size;
1866 hi2c->XferSize = hi2c->XferCount;
1867 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1868
1869 /* Enable Address Acknowledge */
1870 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1871
1872 /* Process Unlocked */
1873 __HAL_UNLOCK(hi2c);
1874
1875 /* Note : The I2C interrupts must be enabled after unlocking current process
1876 to avoid the risk of I2C interrupt handle execution before current
1877 process unlock */
1878
1879 /* Enable EVT, BUF and ERR interrupt */
1881
1882 return HAL_OK;
1883 }
1884 else
1885 {
1886 return HAL_BUSY;
1887 }
1888}
1889
1898HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
1899{
1900
1901 if (hi2c->State == HAL_I2C_STATE_READY)
1902 {
1903 if ((pData == NULL) || (Size == 0U))
1904 {
1905 return HAL_ERROR;
1906 }
1907
1908 /* Process Locked */
1909 __HAL_LOCK(hi2c);
1910
1911 /* Check if the I2C is already enabled */
1912 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1913 {
1914 /* Enable I2C peripheral */
1915 __HAL_I2C_ENABLE(hi2c);
1916 }
1917
1918 /* Disable Pos */
1919 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1920
1922 hi2c->Mode = HAL_I2C_MODE_SLAVE;
1924
1925 /* Prepare transfer parameters */
1926 hi2c->pBuffPtr = pData;
1927 hi2c->XferCount = Size;
1928 hi2c->XferSize = hi2c->XferCount;
1929 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
1930
1931 /* Enable Address Acknowledge */
1932 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
1933
1934 /* Process Unlocked */
1935 __HAL_UNLOCK(hi2c);
1936
1937 /* Note : The I2C interrupts must be enabled after unlocking current process
1938 to avoid the risk of I2C interrupt handle execution before current
1939 process unlock */
1940
1941 /* Enable EVT, BUF and ERR interrupt */
1943
1944 return HAL_OK;
1945 }
1946 else
1947 {
1948 return HAL_BUSY;
1949 }
1950}
1951
1962HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
1963{
1964 __IO uint32_t count = 0U;
1965 HAL_StatusTypeDef dmaxferstatus;
1966
1967 if (hi2c->State == HAL_I2C_STATE_READY)
1968 {
1969 /* Wait until BUSY flag is reset */
1970 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
1971 do
1972 {
1973 count--;
1974 if (count == 0U)
1975 {
1976 hi2c->PreviousState = I2C_STATE_NONE;
1977 hi2c->State = HAL_I2C_STATE_READY;
1978 hi2c->Mode = HAL_I2C_MODE_NONE;
1980
1981 return HAL_BUSY;
1982 }
1983 }
1984 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
1985
1986 /* Process Locked */
1987 __HAL_LOCK(hi2c);
1988
1989 /* Check if the I2C is already enabled */
1990 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
1991 {
1992 /* Enable I2C peripheral */
1993 __HAL_I2C_ENABLE(hi2c);
1994 }
1995
1996 /* Disable Pos */
1997 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
1998
2000 hi2c->Mode = HAL_I2C_MODE_MASTER;
2002
2003 /* Prepare transfer parameters */
2004 hi2c->pBuffPtr = pData;
2005 hi2c->XferCount = Size;
2006 hi2c->XferSize = hi2c->XferCount;
2007 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2008 hi2c->Devaddress = DevAddress;
2009
2010 if (hi2c->XferSize > 0U)
2011 {
2012 if (hi2c->hdmatx != NULL)
2013 {
2014 /* Set the I2C DMA transfer complete callback */
2015 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
2016
2017 /* Set the DMA error callback */
2018 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2019
2020 /* Set the unused DMA callbacks to NULL */
2021 hi2c->hdmatx->XferHalfCpltCallback = NULL;
2022 hi2c->hdmatx->XferM1CpltCallback = NULL;
2023 hi2c->hdmatx->XferM1HalfCpltCallback = NULL;
2024 hi2c->hdmatx->XferAbortCallback = NULL;
2025
2026 /* Enable the DMA stream */
2027 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
2028 }
2029 else
2030 {
2031 /* Update I2C state */
2032 hi2c->State = HAL_I2C_STATE_READY;
2033 hi2c->Mode = HAL_I2C_MODE_NONE;
2034
2035 /* Update I2C error code */
2037
2038 /* Process Unlocked */
2039 __HAL_UNLOCK(hi2c);
2040
2041 return HAL_ERROR;
2042 }
2043
2044 if (dmaxferstatus == HAL_OK)
2045 {
2046 /* Process Unlocked */
2047 __HAL_UNLOCK(hi2c);
2048
2049 /* Note : The I2C interrupts must be enabled after unlocking current process
2050 to avoid the risk of I2C interrupt handle execution before current
2051 process unlock */
2052
2053 /* Enable EVT and ERR interrupt */
2055
2056 /* Enable DMA Request */
2057 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2058
2059 /* Enable Acknowledge */
2060 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2061
2062 /* Generate Start */
2063 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2064 }
2065 else
2066 {
2067 /* Update I2C state */
2068 hi2c->State = HAL_I2C_STATE_READY;
2069 hi2c->Mode = HAL_I2C_MODE_NONE;
2070
2071 /* Update I2C error code */
2073
2074 /* Process Unlocked */
2075 __HAL_UNLOCK(hi2c);
2076
2077 return HAL_ERROR;
2078 }
2079 }
2080 else
2081 {
2082 /* Enable Acknowledge */
2083 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2084
2085 /* Generate Start */
2086 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2087
2088 /* Process Unlocked */
2089 __HAL_UNLOCK(hi2c);
2090
2091 /* Note : The I2C interrupts must be enabled after unlocking current process
2092 to avoid the risk of I2C interrupt handle execution before current
2093 process unlock */
2094
2095 /* Enable EVT, BUF and ERR interrupt */
2097 }
2098
2099 return HAL_OK;
2100 }
2101 else
2102 {
2103 return HAL_BUSY;
2104 }
2105}
2106
2117HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
2118{
2119 __IO uint32_t count = 0U;
2120 HAL_StatusTypeDef dmaxferstatus;
2121
2122 if (hi2c->State == HAL_I2C_STATE_READY)
2123 {
2124 /* Wait until BUSY flag is reset */
2125 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
2126 do
2127 {
2128 count--;
2129 if (count == 0U)
2130 {
2131 hi2c->PreviousState = I2C_STATE_NONE;
2132 hi2c->State = HAL_I2C_STATE_READY;
2133 hi2c->Mode = HAL_I2C_MODE_NONE;
2135
2136 return HAL_BUSY;
2137 }
2138 }
2139 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2140
2141 /* Process Locked */
2142 __HAL_LOCK(hi2c);
2143
2144 /* Check if the I2C is already enabled */
2145 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2146 {
2147 /* Enable I2C peripheral */
2148 __HAL_I2C_ENABLE(hi2c);
2149 }
2150
2151 /* Disable Pos */
2152 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2153
2155 hi2c->Mode = HAL_I2C_MODE_MASTER;
2157
2158 /* Prepare transfer parameters */
2159 hi2c->pBuffPtr = pData;
2160 hi2c->XferCount = Size;
2161 hi2c->XferSize = hi2c->XferCount;
2162 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2163 hi2c->Devaddress = DevAddress;
2164
2165 if (hi2c->XferSize > 0U)
2166 {
2167 if (hi2c->hdmarx != NULL)
2168 {
2169 /* Set the I2C DMA transfer complete callback */
2170 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
2171
2172 /* Set the DMA error callback */
2173 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2174
2175 /* Set the unused DMA callbacks to NULL */
2176 hi2c->hdmarx->XferHalfCpltCallback = NULL;
2177 hi2c->hdmarx->XferM1CpltCallback = NULL;
2178 hi2c->hdmarx->XferM1HalfCpltCallback = NULL;
2179 hi2c->hdmarx->XferAbortCallback = NULL;
2180
2181 /* Enable the DMA stream */
2182 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
2183 }
2184 else
2185 {
2186 /* Update I2C state */
2187 hi2c->State = HAL_I2C_STATE_READY;
2188 hi2c->Mode = HAL_I2C_MODE_NONE;
2189
2190 /* Update I2C error code */
2192
2193 /* Process Unlocked */
2194 __HAL_UNLOCK(hi2c);
2195
2196 return HAL_ERROR;
2197 }
2198
2199 if (dmaxferstatus == HAL_OK)
2200 {
2201 /* Enable Acknowledge */
2202 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2203
2204 /* Generate Start */
2205 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2206
2207 /* Process Unlocked */
2208 __HAL_UNLOCK(hi2c);
2209
2210 /* Note : The I2C interrupts must be enabled after unlocking current process
2211 to avoid the risk of I2C interrupt handle execution before current
2212 process unlock */
2213
2214 /* Enable EVT and ERR interrupt */
2216
2217 /* Enable DMA Request */
2218 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2219 }
2220 else
2221 {
2222 /* Update I2C state */
2223 hi2c->State = HAL_I2C_STATE_READY;
2224 hi2c->Mode = HAL_I2C_MODE_NONE;
2225
2226 /* Update I2C error code */
2228
2229 /* Process Unlocked */
2230 __HAL_UNLOCK(hi2c);
2231
2232 return HAL_ERROR;
2233 }
2234 }
2235 else
2236 {
2237 /* Process Unlocked */
2238 __HAL_UNLOCK(hi2c);
2239
2240 /* Note : The I2C interrupts must be enabled after unlocking current process
2241 to avoid the risk of I2C interrupt handle execution before current
2242 process unlock */
2243
2244 /* Enable EVT, BUF and ERR interrupt */
2246
2247 /* Enable Acknowledge */
2248 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2249
2250 /* Generate Start */
2251 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2252 }
2253
2254 return HAL_OK;
2255 }
2256 else
2257 {
2258 return HAL_BUSY;
2259 }
2260}
2261
2270HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
2271{
2272 HAL_StatusTypeDef dmaxferstatus;
2273
2274 if (hi2c->State == HAL_I2C_STATE_READY)
2275 {
2276 if ((pData == NULL) || (Size == 0U))
2277 {
2278 return HAL_ERROR;
2279 }
2280
2281 /* Process Locked */
2282 __HAL_LOCK(hi2c);
2283
2284 /* Check if the I2C is already enabled */
2285 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2286 {
2287 /* Enable I2C peripheral */
2288 __HAL_I2C_ENABLE(hi2c);
2289 }
2290
2291 /* Disable Pos */
2292 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2293
2295 hi2c->Mode = HAL_I2C_MODE_SLAVE;
2297
2298 /* Prepare transfer parameters */
2299 hi2c->pBuffPtr = pData;
2300 hi2c->XferCount = Size;
2301 hi2c->XferSize = hi2c->XferCount;
2302 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2303
2304 if (hi2c->hdmatx != NULL)
2305 {
2306 /* Set the I2C DMA transfer complete callback */
2307 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
2308
2309 /* Set the DMA error callback */
2310 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2311
2312 /* Set the unused DMA callbacks to NULL */
2313 hi2c->hdmatx->XferHalfCpltCallback = NULL;
2314 hi2c->hdmatx->XferM1CpltCallback = NULL;
2315 hi2c->hdmatx->XferM1HalfCpltCallback = NULL;
2316 hi2c->hdmatx->XferAbortCallback = NULL;
2317
2318 /* Enable the DMA stream */
2319 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
2320 }
2321 else
2322 {
2323 /* Update I2C state */
2325 hi2c->Mode = HAL_I2C_MODE_NONE;
2326
2327 /* Update I2C error code */
2329
2330 /* Process Unlocked */
2331 __HAL_UNLOCK(hi2c);
2332
2333 return HAL_ERROR;
2334 }
2335
2336 if (dmaxferstatus == HAL_OK)
2337 {
2338 /* Enable Address Acknowledge */
2339 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2340
2341 /* Process Unlocked */
2342 __HAL_UNLOCK(hi2c);
2343
2344 /* Note : The I2C interrupts must be enabled after unlocking current process
2345 to avoid the risk of I2C interrupt handle execution before current
2346 process unlock */
2347 /* Enable EVT and ERR interrupt */
2349
2350 /* Enable DMA Request */
2351 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
2352
2353 return HAL_OK;
2354 }
2355 else
2356 {
2357 /* Update I2C state */
2358 hi2c->State = HAL_I2C_STATE_READY;
2359 hi2c->Mode = HAL_I2C_MODE_NONE;
2360
2361 /* Update I2C error code */
2363
2364 /* Process Unlocked */
2365 __HAL_UNLOCK(hi2c);
2366
2367 return HAL_ERROR;
2368 }
2369 }
2370 else
2371 {
2372 return HAL_BUSY;
2373 }
2374}
2375
2384HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
2385{
2386 HAL_StatusTypeDef dmaxferstatus;
2387
2388 if (hi2c->State == HAL_I2C_STATE_READY)
2389 {
2390 if ((pData == NULL) || (Size == 0U))
2391 {
2392 return HAL_ERROR;
2393 }
2394
2395 /* Process Locked */
2396 __HAL_LOCK(hi2c);
2397
2398 /* Check if the I2C is already enabled */
2399 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2400 {
2401 /* Enable I2C peripheral */
2402 __HAL_I2C_ENABLE(hi2c);
2403 }
2404
2405 /* Disable Pos */
2406 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2407
2409 hi2c->Mode = HAL_I2C_MODE_SLAVE;
2411
2412 /* Prepare transfer parameters */
2413 hi2c->pBuffPtr = pData;
2414 hi2c->XferCount = Size;
2415 hi2c->XferSize = hi2c->XferCount;
2416 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2417
2418 if (hi2c->hdmarx != NULL)
2419 {
2420 /* Set the I2C DMA transfer complete callback */
2421 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
2422
2423 /* Set the DMA error callback */
2424 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2425
2426 /* Set the unused DMA callbacks to NULL */
2427 hi2c->hdmarx->XferHalfCpltCallback = NULL;
2428 hi2c->hdmarx->XferM1CpltCallback = NULL;
2429 hi2c->hdmarx->XferM1HalfCpltCallback = NULL;
2430 hi2c->hdmarx->XferAbortCallback = NULL;
2431
2432 /* Enable the DMA stream */
2433 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
2434 }
2435 else
2436 {
2437 /* Update I2C state */
2439 hi2c->Mode = HAL_I2C_MODE_NONE;
2440
2441 /* Update I2C error code */
2443
2444 /* Process Unlocked */
2445 __HAL_UNLOCK(hi2c);
2446
2447 return HAL_ERROR;
2448 }
2449
2450 if (dmaxferstatus == HAL_OK)
2451 {
2452 /* Enable Address Acknowledge */
2453 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2454
2455 /* Process Unlocked */
2456 __HAL_UNLOCK(hi2c);
2457
2458 /* Note : The I2C interrupts must be enabled after unlocking current process
2459 to avoid the risk of I2C interrupt handle execution before current
2460 process unlock */
2461 /* Enable EVT and ERR interrupt */
2463
2464 /* Enable DMA Request */
2465 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
2466
2467 return HAL_OK;
2468 }
2469 else
2470 {
2471 /* Update I2C state */
2472 hi2c->State = HAL_I2C_STATE_READY;
2473 hi2c->Mode = HAL_I2C_MODE_NONE;
2474
2475 /* Update I2C error code */
2477
2478 /* Process Unlocked */
2479 __HAL_UNLOCK(hi2c);
2480
2481 return HAL_ERROR;
2482 }
2483 }
2484 else
2485 {
2486 return HAL_BUSY;
2487 }
2488}
2489
2503HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
2504{
2505 /* Init tickstart for timeout management*/
2506 uint32_t tickstart = HAL_GetTick();
2507
2508 /* Check the parameters */
2509 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2510
2511 if (hi2c->State == HAL_I2C_STATE_READY)
2512 {
2513 /* Wait until BUSY flag is reset */
2514 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
2515 {
2516 return HAL_BUSY;
2517 }
2518
2519 /* Process Locked */
2520 __HAL_LOCK(hi2c);
2521
2522 /* Check if the I2C is already enabled */
2523 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2524 {
2525 /* Enable I2C peripheral */
2526 __HAL_I2C_ENABLE(hi2c);
2527 }
2528
2529 /* Disable Pos */
2530 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2531
2533 hi2c->Mode = HAL_I2C_MODE_MEM;
2535
2536 /* Prepare transfer parameters */
2537 hi2c->pBuffPtr = pData;
2538 hi2c->XferCount = Size;
2539 hi2c->XferSize = hi2c->XferCount;
2540 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2541
2542 /* Send Slave Address and Memory Address */
2543 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
2544 {
2545 return HAL_ERROR;
2546 }
2547
2548 while (hi2c->XferSize > 0U)
2549 {
2550 /* Wait until TXE flag is set */
2551 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2552 {
2553 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2554 {
2555 /* Generate Stop */
2556 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2557 }
2558 return HAL_ERROR;
2559 }
2560
2561 /* Write data to DR */
2562 hi2c->Instance->DR = *hi2c->pBuffPtr;
2563
2564 /* Increment Buffer pointer */
2565 hi2c->pBuffPtr++;
2566
2567 /* Update counter */
2568 hi2c->XferSize--;
2569 hi2c->XferCount--;
2570
2571 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET) && (hi2c->XferSize != 0U))
2572 {
2573 /* Write data to DR */
2574 hi2c->Instance->DR = *hi2c->pBuffPtr;
2575
2576 /* Increment Buffer pointer */
2577 hi2c->pBuffPtr++;
2578
2579 /* Update counter */
2580 hi2c->XferSize--;
2581 hi2c->XferCount--;
2582 }
2583 }
2584
2585 /* Wait until BTF flag is set */
2586 if (I2C_WaitOnBTFFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2587 {
2588 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
2589 {
2590 /* Generate Stop */
2591 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2592 }
2593 return HAL_ERROR;
2594 }
2595
2596 /* Generate Stop */
2597 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2598
2599 hi2c->State = HAL_I2C_STATE_READY;
2600 hi2c->Mode = HAL_I2C_MODE_NONE;
2601
2602 /* Process Unlocked */
2603 __HAL_UNLOCK(hi2c);
2604
2605 return HAL_OK;
2606 }
2607 else
2608 {
2609 return HAL_BUSY;
2610 }
2611}
2612
2626HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
2627{
2628 /* Init tickstart for timeout management*/
2629 uint32_t tickstart = HAL_GetTick();
2630
2631 /* Check the parameters */
2632 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2633
2634 if (hi2c->State == HAL_I2C_STATE_READY)
2635 {
2636 /* Wait until BUSY flag is reset */
2637 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
2638 {
2639 return HAL_BUSY;
2640 }
2641
2642 /* Process Locked */
2643 __HAL_LOCK(hi2c);
2644
2645 /* Check if the I2C is already enabled */
2646 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2647 {
2648 /* Enable I2C peripheral */
2649 __HAL_I2C_ENABLE(hi2c);
2650 }
2651
2652 /* Disable Pos */
2653 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2654
2656 hi2c->Mode = HAL_I2C_MODE_MEM;
2658
2659 /* Prepare transfer parameters */
2660 hi2c->pBuffPtr = pData;
2661 hi2c->XferCount = Size;
2662 hi2c->XferSize = hi2c->XferCount;
2663 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2664
2665 /* Send Slave Address and Memory Address */
2666 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, Timeout, tickstart) != HAL_OK)
2667 {
2668 return HAL_ERROR;
2669 }
2670
2671 if (hi2c->XferSize == 0U)
2672 {
2673 /* Clear ADDR flag */
2675
2676 /* Generate Stop */
2677 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2678 }
2679 else if (hi2c->XferSize == 1U)
2680 {
2681 /* Disable Acknowledge */
2682 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2683
2684 /* Clear ADDR flag */
2686
2687 /* Generate Stop */
2688 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2689 }
2690 else if (hi2c->XferSize == 2U)
2691 {
2692 /* Disable Acknowledge */
2693 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2694
2695 /* Enable Pos */
2696 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2697
2698 /* Clear ADDR flag */
2700 }
2701 else
2702 {
2703 /* Clear ADDR flag */
2705 }
2706
2707 while (hi2c->XferSize > 0U)
2708 {
2709 if (hi2c->XferSize <= 3U)
2710 {
2711 /* One byte */
2712 if (hi2c->XferSize == 1U)
2713 {
2714 /* Wait until RXNE flag is set */
2715 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2716 {
2717 return HAL_ERROR;
2718 }
2719
2720 /* Read data from DR */
2721 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2722
2723 /* Increment Buffer pointer */
2724 hi2c->pBuffPtr++;
2725
2726 /* Update counter */
2727 hi2c->XferSize--;
2728 hi2c->XferCount--;
2729 }
2730 /* Two bytes */
2731 else if (hi2c->XferSize == 2U)
2732 {
2733 /* Wait until BTF flag is set */
2734 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
2735 {
2736 return HAL_ERROR;
2737 }
2738
2739 /* Generate Stop */
2740 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2741
2742 /* Read data from DR */
2743 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2744
2745 /* Increment Buffer pointer */
2746 hi2c->pBuffPtr++;
2747
2748 /* Update counter */
2749 hi2c->XferSize--;
2750 hi2c->XferCount--;
2751
2752 /* Read data from DR */
2753 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2754
2755 /* Increment Buffer pointer */
2756 hi2c->pBuffPtr++;
2757
2758 /* Update counter */
2759 hi2c->XferSize--;
2760 hi2c->XferCount--;
2761 }
2762 /* 3 Last bytes */
2763 else
2764 {
2765 /* Wait until BTF flag is set */
2766 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
2767 {
2768 return HAL_ERROR;
2769 }
2770
2771 /* Disable Acknowledge */
2772 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2773
2774 /* Read data from DR */
2775 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2776
2777 /* Increment Buffer pointer */
2778 hi2c->pBuffPtr++;
2779
2780 /* Update counter */
2781 hi2c->XferSize--;
2782 hi2c->XferCount--;
2783
2784 /* Wait until BTF flag is set */
2785 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BTF, RESET, Timeout, tickstart) != HAL_OK)
2786 {
2787 return HAL_ERROR;
2788 }
2789
2790 /* Generate Stop */
2791 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
2792
2793 /* Read data from DR */
2794 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2795
2796 /* Increment Buffer pointer */
2797 hi2c->pBuffPtr++;
2798
2799 /* Update counter */
2800 hi2c->XferSize--;
2801 hi2c->XferCount--;
2802
2803 /* Read data from DR */
2804 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2805
2806 /* Increment Buffer pointer */
2807 hi2c->pBuffPtr++;
2808
2809 /* Update counter */
2810 hi2c->XferSize--;
2811 hi2c->XferCount--;
2812 }
2813 }
2814 else
2815 {
2816 /* Wait until RXNE flag is set */
2817 if (I2C_WaitOnRXNEFlagUntilTimeout(hi2c, Timeout, tickstart) != HAL_OK)
2818 {
2819 return HAL_ERROR;
2820 }
2821
2822 /* Read data from DR */
2823 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2824
2825 /* Increment Buffer pointer */
2826 hi2c->pBuffPtr++;
2827
2828 /* Update counter */
2829 hi2c->XferSize--;
2830 hi2c->XferCount--;
2831
2832 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
2833 {
2834 if (hi2c->XferSize == 3U)
2835 {
2836 /* Disable Acknowledge */
2837 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
2838 }
2839 /* Read data from DR */
2840 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
2841
2842 /* Increment Buffer pointer */
2843 hi2c->pBuffPtr++;
2844
2845 /* Update counter */
2846 hi2c->XferSize--;
2847 hi2c->XferCount--;
2848 }
2849 }
2850 }
2851
2852 hi2c->State = HAL_I2C_STATE_READY;
2853 hi2c->Mode = HAL_I2C_MODE_NONE;
2854
2855 /* Process Unlocked */
2856 __HAL_UNLOCK(hi2c);
2857
2858 return HAL_OK;
2859 }
2860 else
2861 {
2862 return HAL_BUSY;
2863 }
2864}
2865
2878HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2879{
2880 __IO uint32_t count = 0U;
2881
2882 /* Check the parameters */
2883 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2884
2885 if (hi2c->State == HAL_I2C_STATE_READY)
2886 {
2887 /* Wait until BUSY flag is reset */
2888 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
2889 do
2890 {
2891 count--;
2892 if (count == 0U)
2893 {
2894 hi2c->PreviousState = I2C_STATE_NONE;
2895 hi2c->State = HAL_I2C_STATE_READY;
2896 hi2c->Mode = HAL_I2C_MODE_NONE;
2898
2899 return HAL_BUSY;
2900 }
2901 }
2902 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2903
2904 /* Process Locked */
2905 __HAL_LOCK(hi2c);
2906
2907 /* Check if the I2C is already enabled */
2908 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2909 {
2910 /* Enable I2C peripheral */
2911 __HAL_I2C_ENABLE(hi2c);
2912 }
2913
2914 /* Disable Pos */
2915 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
2916
2918 hi2c->Mode = HAL_I2C_MODE_MEM;
2920
2921 /* Prepare transfer parameters */
2922 hi2c->pBuffPtr = pData;
2923 hi2c->XferCount = Size;
2924 hi2c->XferSize = hi2c->XferCount;
2925 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
2926 hi2c->Devaddress = DevAddress;
2927 hi2c->Memaddress = MemAddress;
2928 hi2c->MemaddSize = MemAddSize;
2929 hi2c->EventCount = 0U;
2930
2931 /* Generate Start */
2932 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
2933
2934 /* Process Unlocked */
2935 __HAL_UNLOCK(hi2c);
2936
2937 /* Note : The I2C interrupts must be enabled after unlocking current process
2938 to avoid the risk of I2C interrupt handle execution before current
2939 process unlock */
2940
2941 /* Enable EVT, BUF and ERR interrupt */
2943
2944 return HAL_OK;
2945 }
2946 else
2947 {
2948 return HAL_BUSY;
2949 }
2950}
2951
2963HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
2964{
2965 __IO uint32_t count = 0U;
2966
2967 /* Check the parameters */
2968 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
2969
2970 if (hi2c->State == HAL_I2C_STATE_READY)
2971 {
2972 /* Wait until BUSY flag is reset */
2973 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
2974 do
2975 {
2976 count--;
2977 if (count == 0U)
2978 {
2979 hi2c->PreviousState = I2C_STATE_NONE;
2980 hi2c->State = HAL_I2C_STATE_READY;
2981 hi2c->Mode = HAL_I2C_MODE_NONE;
2983
2984 return HAL_BUSY;
2985 }
2986 }
2987 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
2988
2989 /* Process Locked */
2990 __HAL_LOCK(hi2c);
2991
2992 /* Check if the I2C is already enabled */
2993 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
2994 {
2995 /* Enable I2C peripheral */
2996 __HAL_I2C_ENABLE(hi2c);
2997 }
2998
2999 /* Disable Pos */
3000 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3001
3003 hi2c->Mode = HAL_I2C_MODE_MEM;
3005
3006 /* Prepare transfer parameters */
3007 hi2c->pBuffPtr = pData;
3008 hi2c->XferCount = Size;
3009 hi2c->XferSize = hi2c->XferCount;
3010 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3011 hi2c->Devaddress = DevAddress;
3012 hi2c->Memaddress = MemAddress;
3013 hi2c->MemaddSize = MemAddSize;
3014 hi2c->EventCount = 0U;
3015
3016 /* Enable Acknowledge */
3017 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3018
3019 /* Generate Start */
3020 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3021
3022 /* Process Unlocked */
3023 __HAL_UNLOCK(hi2c);
3024
3025 if (hi2c->XferSize > 0U)
3026 {
3027 /* Note : The I2C interrupts must be enabled after unlocking current process
3028 to avoid the risk of I2C interrupt handle execution before current
3029 process unlock */
3030
3031 /* Enable EVT, BUF and ERR interrupt */
3033 }
3034 return HAL_OK;
3035 }
3036 else
3037 {
3038 return HAL_BUSY;
3039 }
3040}
3041
3054HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
3055{
3056 __IO uint32_t count = 0U;
3057 HAL_StatusTypeDef dmaxferstatus;
3058
3059 /* Init tickstart for timeout management*/
3060 uint32_t tickstart = HAL_GetTick();
3061
3062 /* Check the parameters */
3063 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
3064
3065 if (hi2c->State == HAL_I2C_STATE_READY)
3066 {
3067 /* Wait until BUSY flag is reset */
3068 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3069 do
3070 {
3071 count--;
3072 if (count == 0U)
3073 {
3074 hi2c->PreviousState = I2C_STATE_NONE;
3075 hi2c->State = HAL_I2C_STATE_READY;
3076 hi2c->Mode = HAL_I2C_MODE_NONE;
3078
3079 return HAL_BUSY;
3080 }
3081 }
3082 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3083
3084 /* Process Locked */
3085 __HAL_LOCK(hi2c);
3086
3087 /* Check if the I2C is already enabled */
3088 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3089 {
3090 /* Enable I2C peripheral */
3091 __HAL_I2C_ENABLE(hi2c);
3092 }
3093
3094 /* Disable Pos */
3095 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3096
3098 hi2c->Mode = HAL_I2C_MODE_MEM;
3100
3101 /* Prepare transfer parameters */
3102 hi2c->pBuffPtr = pData;
3103 hi2c->XferCount = Size;
3104 hi2c->XferSize = hi2c->XferCount;
3105 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3106 hi2c->Devaddress = DevAddress;
3107 hi2c->Memaddress = MemAddress;
3108 hi2c->MemaddSize = MemAddSize;
3109 hi2c->EventCount = 0U;
3110
3111 if (hi2c->XferSize > 0U)
3112 {
3113 if (hi2c->hdmatx != NULL)
3114 {
3115 /* Set the I2C DMA transfer complete callback */
3116 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
3117
3118 /* Set the DMA error callback */
3119 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
3120
3121 /* Set the unused DMA callbacks to NULL */
3122 hi2c->hdmatx->XferHalfCpltCallback = NULL;
3123 hi2c->hdmatx->XferM1CpltCallback = NULL;
3124 hi2c->hdmatx->XferM1HalfCpltCallback = NULL;
3125 hi2c->hdmatx->XferAbortCallback = NULL;
3126
3127 /* Enable the DMA stream */
3128 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
3129 }
3130 else
3131 {
3132 /* Update I2C state */
3133 hi2c->State = HAL_I2C_STATE_READY;
3134 hi2c->Mode = HAL_I2C_MODE_NONE;
3135
3136 /* Update I2C error code */
3138
3139 /* Process Unlocked */
3140 __HAL_UNLOCK(hi2c);
3141
3142 return HAL_ERROR;
3143 }
3144
3145 if (dmaxferstatus == HAL_OK)
3146 {
3147 /* Send Slave Address and Memory Address */
3148 if (I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
3149 {
3150 /* Abort the ongoing DMA */
3151 dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmatx);
3152
3153 /* Prevent unused argument(s) compilation and MISRA warning */
3154 UNUSED(dmaxferstatus);
3155
3156 /* Set the unused I2C DMA transfer complete callback to NULL */
3157 hi2c->hdmatx->XferCpltCallback = NULL;
3158
3159 /* Disable Acknowledge */
3160 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3161
3162 hi2c->XferSize = 0U;
3163 hi2c->XferCount = 0U;
3164
3165 /* Disable I2C peripheral to prevent dummy data in buffer */
3166 __HAL_I2C_DISABLE(hi2c);
3167
3168 return HAL_ERROR;
3169 }
3170
3171 /* Clear ADDR flag */
3173
3174 /* Process Unlocked */
3175 __HAL_UNLOCK(hi2c);
3176
3177 /* Note : The I2C interrupts must be enabled after unlocking current process
3178 to avoid the risk of I2C interrupt handle execution before current
3179 process unlock */
3180 /* Enable ERR interrupt */
3182
3183 /* Enable DMA Request */
3184 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3185
3186 return HAL_OK;
3187 }
3188 else
3189 {
3190 /* Update I2C state */
3191 hi2c->State = HAL_I2C_STATE_READY;
3192 hi2c->Mode = HAL_I2C_MODE_NONE;
3193
3194 /* Update I2C error code */
3196
3197 /* Process Unlocked */
3198 __HAL_UNLOCK(hi2c);
3199
3200 return HAL_ERROR;
3201 }
3202 }
3203 else
3204 {
3205 /* Update I2C state */
3206 hi2c->State = HAL_I2C_STATE_READY;
3207 hi2c->Mode = HAL_I2C_MODE_NONE;
3208
3209 /* Update I2C error code */
3211
3212 /* Process Unlocked */
3213 __HAL_UNLOCK(hi2c);
3214
3215 return HAL_ERROR;
3216 }
3217 }
3218 else
3219 {
3220 return HAL_BUSY;
3221 }
3222}
3223
3236HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
3237{
3238 /* Init tickstart for timeout management*/
3239 uint32_t tickstart = HAL_GetTick();
3240 __IO uint32_t count = 0U;
3241 HAL_StatusTypeDef dmaxferstatus;
3242
3243 /* Check the parameters */
3244 assert_param(IS_I2C_MEMADD_SIZE(MemAddSize));
3245
3246 if (hi2c->State == HAL_I2C_STATE_READY)
3247 {
3248 /* Wait until BUSY flag is reset */
3249 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3250 do
3251 {
3252 count--;
3253 if (count == 0U)
3254 {
3255 hi2c->PreviousState = I2C_STATE_NONE;
3256 hi2c->State = HAL_I2C_STATE_READY;
3257 hi2c->Mode = HAL_I2C_MODE_NONE;
3259
3260 return HAL_BUSY;
3261 }
3262 }
3263 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3264
3265 /* Process Locked */
3266 __HAL_LOCK(hi2c);
3267
3268 /* Check if the I2C is already enabled */
3269 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3270 {
3271 /* Enable I2C peripheral */
3272 __HAL_I2C_ENABLE(hi2c);
3273 }
3274
3275 /* Disable Pos */
3276 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3277
3279 hi2c->Mode = HAL_I2C_MODE_MEM;
3281
3282 /* Prepare transfer parameters */
3283 hi2c->pBuffPtr = pData;
3284 hi2c->XferCount = Size;
3285 hi2c->XferSize = hi2c->XferCount;
3286 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3287 hi2c->Devaddress = DevAddress;
3288 hi2c->Memaddress = MemAddress;
3289 hi2c->MemaddSize = MemAddSize;
3290 hi2c->EventCount = 0U;
3291
3292 if (hi2c->XferSize > 0U)
3293 {
3294 if (hi2c->hdmarx != NULL)
3295 {
3296 /* Set the I2C DMA transfer complete callback */
3297 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
3298
3299 /* Set the DMA error callback */
3300 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
3301
3302 /* Set the unused DMA callbacks to NULL */
3303 hi2c->hdmarx->XferHalfCpltCallback = NULL;
3304 hi2c->hdmarx->XferM1CpltCallback = NULL;
3305 hi2c->hdmarx->XferM1HalfCpltCallback = NULL;
3306 hi2c->hdmarx->XferAbortCallback = NULL;
3307
3308 /* Enable the DMA stream */
3309 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
3310 }
3311 else
3312 {
3313 /* Update I2C state */
3314 hi2c->State = HAL_I2C_STATE_READY;
3315 hi2c->Mode = HAL_I2C_MODE_NONE;
3316
3317 /* Update I2C error code */
3319
3320 /* Process Unlocked */
3321 __HAL_UNLOCK(hi2c);
3322
3323 return HAL_ERROR;
3324 }
3325
3326 if (dmaxferstatus == HAL_OK)
3327 {
3328 /* Send Slave Address and Memory Address */
3329 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
3330 {
3331 /* Abort the ongoing DMA */
3332 dmaxferstatus = HAL_DMA_Abort_IT(hi2c->hdmarx);
3333
3334 /* Prevent unused argument(s) compilation and MISRA warning */
3335 UNUSED(dmaxferstatus);
3336
3337 /* Set the unused I2C DMA transfer complete callback to NULL */
3338 hi2c->hdmarx->XferCpltCallback = NULL;
3339
3340 /* Disable Acknowledge */
3341 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3342
3343 hi2c->XferSize = 0U;
3344 hi2c->XferCount = 0U;
3345
3346 /* Disable I2C peripheral to prevent dummy data in buffer */
3347 __HAL_I2C_DISABLE(hi2c);
3348
3349 return HAL_ERROR;
3350 }
3351
3352 if (hi2c->XferSize == 1U)
3353 {
3354 /* Disable Acknowledge */
3355 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3356 }
3357 else
3358 {
3359 /* Enable Last DMA bit */
3360 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
3361 }
3362
3363 /* Clear ADDR flag */
3365
3366 /* Process Unlocked */
3367 __HAL_UNLOCK(hi2c);
3368
3369 /* Note : The I2C interrupts must be enabled after unlocking current process
3370 to avoid the risk of I2C interrupt handle execution before current
3371 process unlock */
3372 /* Enable ERR interrupt */
3374
3375 /* Enable DMA Request */
3376 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
3377 }
3378 else
3379 {
3380 /* Update I2C state */
3381 hi2c->State = HAL_I2C_STATE_READY;
3382 hi2c->Mode = HAL_I2C_MODE_NONE;
3383
3384 /* Update I2C error code */
3386
3387 /* Process Unlocked */
3388 __HAL_UNLOCK(hi2c);
3389
3390 return HAL_ERROR;
3391 }
3392 }
3393 else
3394 {
3395 /* Send Slave Address and Memory Address */
3396 if (I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG, tickstart) != HAL_OK)
3397 {
3398 return HAL_ERROR;
3399 }
3400
3401 /* Clear ADDR flag */
3403
3404 /* Generate Stop */
3405 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3406
3407 hi2c->State = HAL_I2C_STATE_READY;
3408
3409 /* Process Unlocked */
3410 __HAL_UNLOCK(hi2c);
3411 }
3412
3413 return HAL_OK;
3414 }
3415 else
3416 {
3417 return HAL_BUSY;
3418 }
3419}
3420
3432HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
3433{
3434 /* Get tick */
3435 uint32_t tickstart = HAL_GetTick();
3436 uint32_t I2C_Trials = 0U;
3437 FlagStatus tmp1;
3438 FlagStatus tmp2;
3439
3440 if (hi2c->State == HAL_I2C_STATE_READY)
3441 {
3442 /* Wait until BUSY flag is reset */
3443 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
3444 {
3445 return HAL_BUSY;
3446 }
3447
3448 /* Process Locked */
3449 __HAL_LOCK(hi2c);
3450
3451 /* Check if the I2C is already enabled */
3452 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3453 {
3454 /* Enable I2C peripheral */
3455 __HAL_I2C_ENABLE(hi2c);
3456 }
3457
3458 /* Disable Pos */
3459 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3460
3461 hi2c->State = HAL_I2C_STATE_BUSY;
3463 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
3464
3465 do
3466 {
3467 /* Generate Start */
3468 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3469
3470 /* Wait until SB flag is set */
3471 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, tickstart) != HAL_OK)
3472 {
3473 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
3474 {
3476 }
3477 return HAL_TIMEOUT;
3478 }
3479
3480 /* Send slave address */
3481 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
3482
3483 /* Wait until ADDR or AF flag are set */
3484 /* Get tick */
3485 tickstart = HAL_GetTick();
3486
3487 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
3488 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
3489 while ((hi2c->State != HAL_I2C_STATE_TIMEOUT) && (tmp1 == RESET) && (tmp2 == RESET))
3490 {
3491 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
3492 {
3494 }
3495 tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR);
3496 tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
3497 }
3498
3499 hi2c->State = HAL_I2C_STATE_READY;
3500
3501 /* Check if the ADDR flag has been set */
3502 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_ADDR) == SET)
3503 {
3504 /* Generate Stop */
3505 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3506
3507 /* Clear ADDR Flag */
3509
3510 /* Wait until BUSY flag is reset */
3511 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
3512 {
3513 return HAL_ERROR;
3514 }
3515
3516 hi2c->State = HAL_I2C_STATE_READY;
3517
3518 /* Process Unlocked */
3519 __HAL_UNLOCK(hi2c);
3520
3521 return HAL_OK;
3522 }
3523 else
3524 {
3525 /* Generate Stop */
3526 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
3527
3528 /* Clear AF Flag */
3530
3531 /* Wait until BUSY flag is reset */
3532 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, I2C_TIMEOUT_BUSY_FLAG, tickstart) != HAL_OK)
3533 {
3534 return HAL_ERROR;
3535 }
3536 }
3537
3538 /* Increment Trials */
3539 I2C_Trials++;
3540 }
3541 while (I2C_Trials < Trials);
3542
3543 hi2c->State = HAL_I2C_STATE_READY;
3544
3545 /* Process Unlocked */
3546 __HAL_UNLOCK(hi2c);
3547
3548 return HAL_ERROR;
3549 }
3550 else
3551 {
3552 return HAL_BUSY;
3553 }
3554}
3555
3568HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
3569{
3570 __IO uint32_t Prev_State = 0x00U;
3571 __IO uint32_t count = 0x00U;
3572
3573 /* Check the parameters */
3575
3576 if (hi2c->State == HAL_I2C_STATE_READY)
3577 {
3578 /* Check Busy Flag only if FIRST call of Master interface */
3579 if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
3580 {
3581 /* Wait until BUSY flag is reset */
3582 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3583 do
3584 {
3585 count--;
3586 if (count == 0U)
3587 {
3588 hi2c->PreviousState = I2C_STATE_NONE;
3589 hi2c->State = HAL_I2C_STATE_READY;
3590 hi2c->Mode = HAL_I2C_MODE_NONE;
3592
3593 return HAL_BUSY;
3594 }
3595 }
3596 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3597 }
3598
3599 /* Process Locked */
3600 __HAL_LOCK(hi2c);
3601
3602 /* Check if the I2C is already enabled */
3603 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3604 {
3605 /* Enable I2C peripheral */
3606 __HAL_I2C_ENABLE(hi2c);
3607 }
3608
3609 /* Disable Pos */
3610 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3611
3613 hi2c->Mode = HAL_I2C_MODE_MASTER;
3615
3616 /* Prepare transfer parameters */
3617 hi2c->pBuffPtr = pData;
3618 hi2c->XferCount = Size;
3619 hi2c->XferSize = hi2c->XferCount;
3620 hi2c->XferOptions = XferOptions;
3621 hi2c->Devaddress = DevAddress;
3622
3623 Prev_State = hi2c->PreviousState;
3624
3625 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
3626 /* Mean Previous state is same as current state */
3627 if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
3628 {
3629 /* Generate Start */
3630 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3631 }
3632
3633 /* Process Unlocked */
3634 __HAL_UNLOCK(hi2c);
3635
3636 /* Note : The I2C interrupts must be enabled after unlocking current process
3637 to avoid the risk of I2C interrupt handle execution before current
3638 process unlock */
3639
3640 /* Enable EVT, BUF and ERR interrupt */
3642
3643 return HAL_OK;
3644 }
3645 else
3646 {
3647 return HAL_BUSY;
3648 }
3649}
3650
3663HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
3664{
3665 __IO uint32_t Prev_State = 0x00U;
3666 __IO uint32_t count = 0x00U;
3667 HAL_StatusTypeDef dmaxferstatus;
3668
3669 /* Check the parameters */
3671
3672 if (hi2c->State == HAL_I2C_STATE_READY)
3673 {
3674 /* Check Busy Flag only if FIRST call of Master interface */
3675 if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
3676 {
3677 /* Wait until BUSY flag is reset */
3678 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3679 do
3680 {
3681 count--;
3682 if (count == 0U)
3683 {
3684 hi2c->PreviousState = I2C_STATE_NONE;
3685 hi2c->State = HAL_I2C_STATE_READY;
3686 hi2c->Mode = HAL_I2C_MODE_NONE;
3688
3689 return HAL_BUSY;
3690 }
3691 }
3692 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3693 }
3694
3695 /* Process Locked */
3696 __HAL_LOCK(hi2c);
3697
3698 /* Check if the I2C is already enabled */
3699 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3700 {
3701 /* Enable I2C peripheral */
3702 __HAL_I2C_ENABLE(hi2c);
3703 }
3704
3705 /* Disable Pos */
3706 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3707
3709 hi2c->Mode = HAL_I2C_MODE_MASTER;
3711
3712 /* Prepare transfer parameters */
3713 hi2c->pBuffPtr = pData;
3714 hi2c->XferCount = Size;
3715 hi2c->XferSize = hi2c->XferCount;
3716 hi2c->XferOptions = XferOptions;
3717 hi2c->Devaddress = DevAddress;
3718
3719 Prev_State = hi2c->PreviousState;
3720
3721 if (hi2c->XferSize > 0U)
3722 {
3723 if (hi2c->hdmatx != NULL)
3724 {
3725 /* Set the I2C DMA transfer complete callback */
3726 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
3727
3728 /* Set the DMA error callback */
3729 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
3730
3731 /* Set the unused DMA callbacks to NULL */
3732 hi2c->hdmatx->XferHalfCpltCallback = NULL;
3733 hi2c->hdmatx->XferAbortCallback = NULL;
3734
3735 /* Enable the DMA stream */
3736 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
3737 }
3738 else
3739 {
3740 /* Update I2C state */
3741 hi2c->State = HAL_I2C_STATE_READY;
3742 hi2c->Mode = HAL_I2C_MODE_NONE;
3743
3744 /* Update I2C error code */
3746
3747 /* Process Unlocked */
3748 __HAL_UNLOCK(hi2c);
3749
3750 return HAL_ERROR;
3751 }
3752
3753 if (dmaxferstatus == HAL_OK)
3754 {
3755 /* Enable Acknowledge */
3756 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3757
3758 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
3759 /* Mean Previous state is same as current state */
3760 if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
3761 {
3762 /* Generate Start */
3763 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3764 }
3765
3766 /* Process Unlocked */
3767 __HAL_UNLOCK(hi2c);
3768
3769 /* Note : The I2C interrupts must be enabled after unlocking current process
3770 to avoid the risk of I2C interrupt handle execution before current
3771 process unlock */
3772
3773 /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
3774 /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
3775 if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
3776 {
3777 /* Enable DMA Request */
3778 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
3779 }
3780
3781 /* Enable EVT and ERR interrupt */
3783 }
3784 else
3785 {
3786 /* Update I2C state */
3787 hi2c->State = HAL_I2C_STATE_READY;
3788 hi2c->Mode = HAL_I2C_MODE_NONE;
3789
3790 /* Update I2C error code */
3792
3793 /* Process Unlocked */
3794 __HAL_UNLOCK(hi2c);
3795
3796 return HAL_ERROR;
3797 }
3798 }
3799 else
3800 {
3801 /* Enable Acknowledge */
3802 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3803
3804 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
3805 /* Mean Previous state is same as current state */
3806 if ((Prev_State != I2C_STATE_MASTER_BUSY_TX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
3807 {
3808 /* Generate Start */
3809 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3810 }
3811
3812 /* Process Unlocked */
3813 __HAL_UNLOCK(hi2c);
3814
3815 /* Note : The I2C interrupts must be enabled after unlocking current process
3816 to avoid the risk of I2C interrupt handle execution before current
3817 process unlock */
3818
3819 /* Enable EVT, BUF and ERR interrupt */
3821 }
3822
3823 return HAL_OK;
3824 }
3825 else
3826 {
3827 return HAL_BUSY;
3828 }
3829}
3830
3843HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
3844{
3845 __IO uint32_t Prev_State = 0x00U;
3846 __IO uint32_t count = 0U;
3847 uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3848
3849 /* Check the parameters */
3851
3852 if (hi2c->State == HAL_I2C_STATE_READY)
3853 {
3854 /* Check Busy Flag only if FIRST call of Master interface */
3855 if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
3856 {
3857 /* Wait until BUSY flag is reset */
3858 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3859 do
3860 {
3861 count--;
3862 if (count == 0U)
3863 {
3864 hi2c->PreviousState = I2C_STATE_NONE;
3865 hi2c->State = HAL_I2C_STATE_READY;
3866 hi2c->Mode = HAL_I2C_MODE_NONE;
3868
3869 return HAL_BUSY;
3870 }
3871 }
3872 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3873 }
3874
3875 /* Process Locked */
3876 __HAL_LOCK(hi2c);
3877
3878 /* Check if the I2C is already enabled */
3879 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
3880 {
3881 /* Enable I2C peripheral */
3882 __HAL_I2C_ENABLE(hi2c);
3883 }
3884
3885 /* Disable Pos */
3886 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3887
3889 hi2c->Mode = HAL_I2C_MODE_MASTER;
3891
3892 /* Prepare transfer parameters */
3893 hi2c->pBuffPtr = pData;
3894 hi2c->XferCount = Size;
3895 hi2c->XferSize = hi2c->XferCount;
3896 hi2c->XferOptions = XferOptions;
3897 hi2c->Devaddress = DevAddress;
3898
3899 Prev_State = hi2c->PreviousState;
3900
3901 if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
3902 {
3903 if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
3904 {
3905 /* Disable Acknowledge */
3906 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3907
3908 /* Enable Pos */
3909 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
3910
3911 /* Remove Enabling of IT_BUF, mean RXNE treatment, treat the 2 bytes through BTF */
3912 enableIT &= ~I2C_IT_BUF;
3913 }
3914 else
3915 {
3916 /* Enable Acknowledge */
3917 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3918 }
3919 }
3920 else
3921 {
3922 /* Enable Acknowledge */
3923 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
3924 }
3925
3926 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
3927 /* Mean Previous state is same as current state */
3928 if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
3929 {
3930 /* Generate Start */
3931 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
3932 }
3933
3934 /* Process Unlocked */
3935 __HAL_UNLOCK(hi2c);
3936
3937 /* Note : The I2C interrupts must be enabled after unlocking current process
3938 to avoid the risk of I2C interrupt handle execution before current
3939 process unlock */
3940
3941 /* Enable interrupts */
3942 __HAL_I2C_ENABLE_IT(hi2c, enableIT);
3943
3944 return HAL_OK;
3945 }
3946 else
3947 {
3948 return HAL_BUSY;
3949 }
3950}
3951
3964HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
3965{
3966 __IO uint32_t Prev_State = 0x00U;
3967 __IO uint32_t count = 0U;
3968 uint32_t enableIT = (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
3969 HAL_StatusTypeDef dmaxferstatus;
3970
3971 /* Check the parameters */
3973
3974 if (hi2c->State == HAL_I2C_STATE_READY)
3975 {
3976 /* Check Busy Flag only if FIRST call of Master interface */
3977 if ((READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP) || (XferOptions == I2C_FIRST_AND_LAST_FRAME) || (XferOptions == I2C_FIRST_FRAME))
3978 {
3979 /* Wait until BUSY flag is reset */
3980 count = I2C_TIMEOUT_BUSY_FLAG * (SystemCoreClock / 25U / 1000U);
3981 do
3982 {
3983 count--;
3984 if (count == 0U)
3985 {
3986 hi2c->PreviousState = I2C_STATE_NONE;
3987 hi2c->State = HAL_I2C_STATE_READY;
3988 hi2c->Mode = HAL_I2C_MODE_NONE;
3990
3991 return HAL_BUSY;
3992 }
3993 }
3994 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET);
3995 }
3996
3997 /* Process Locked */
3998 __HAL_LOCK(hi2c);
3999
4000 /* Check if the I2C is already enabled */
4001 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
4002 {
4003 /* Enable I2C peripheral */
4004 __HAL_I2C_ENABLE(hi2c);
4005 }
4006
4007 /* Disable Pos */
4008 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
4009
4010 /* Clear Last DMA bit */
4011 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
4012
4014 hi2c->Mode = HAL_I2C_MODE_MASTER;
4016
4017 /* Prepare transfer parameters */
4018 hi2c->pBuffPtr = pData;
4019 hi2c->XferCount = Size;
4020 hi2c->XferSize = hi2c->XferCount;
4021 hi2c->XferOptions = XferOptions;
4022 hi2c->Devaddress = DevAddress;
4023
4024 Prev_State = hi2c->PreviousState;
4025
4026 if (hi2c->XferSize > 0U)
4027 {
4028 if ((hi2c->XferCount == 2U) && ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP)))
4029 {
4030 if (Prev_State == I2C_STATE_MASTER_BUSY_RX)
4031 {
4032 /* Disable Acknowledge */
4033 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4034
4035 /* Enable Pos */
4036 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
4037
4038 /* Enable Last DMA bit */
4039 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
4040 }
4041 else
4042 {
4043 /* Enable Acknowledge */
4044 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4045 }
4046 }
4047 else
4048 {
4049 /* Enable Acknowledge */
4050 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4051
4052 if ((XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_OTHER_AND_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
4053 {
4054 /* Enable Last DMA bit */
4055 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
4056 }
4057 }
4058 if (hi2c->hdmarx != NULL)
4059 {
4060 /* Set the I2C DMA transfer complete callback */
4061 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
4062
4063 /* Set the DMA error callback */
4064 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
4065
4066 /* Set the unused DMA callbacks to NULL */
4067 hi2c->hdmarx->XferHalfCpltCallback = NULL;
4068 hi2c->hdmarx->XferAbortCallback = NULL;
4069
4070 /* Enable the DMA stream */
4071 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
4072 }
4073 else
4074 {
4075 /* Update I2C state */
4076 hi2c->State = HAL_I2C_STATE_READY;
4077 hi2c->Mode = HAL_I2C_MODE_NONE;
4078
4079 /* Update I2C error code */
4081
4082 /* Process Unlocked */
4083 __HAL_UNLOCK(hi2c);
4084
4085 return HAL_ERROR;
4086 }
4087 if (dmaxferstatus == HAL_OK)
4088 {
4089 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
4090 /* Mean Previous state is same as current state */
4091 if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
4092 {
4093 /* Generate Start */
4094 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
4095
4096 /* Update interrupt for only EVT and ERR */
4097 enableIT = (I2C_IT_EVT | I2C_IT_ERR);
4098 }
4099 else
4100 {
4101 /* Update interrupt for only ERR */
4102 enableIT = I2C_IT_ERR;
4103 }
4104
4105 /* Process Unlocked */
4106 __HAL_UNLOCK(hi2c);
4107
4108 /* Note : The I2C interrupts must be enabled after unlocking current process
4109 to avoid the risk of I2C interrupt handle execution before current
4110 process unlock */
4111
4112 /* If XferOptions is not associated to a new frame, mean no start bit is request, enable directly the DMA request */
4113 /* In other cases, DMA request is enabled after Slave address treatment in IRQHandler */
4114 if ((XferOptions == I2C_NEXT_FRAME) || (XferOptions == I2C_LAST_FRAME) || (XferOptions == I2C_LAST_FRAME_NO_STOP))
4115 {
4116 /* Enable DMA Request */
4117 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
4118 }
4119
4120 /* Enable EVT and ERR interrupt */
4121 __HAL_I2C_ENABLE_IT(hi2c, enableIT);
4122 }
4123 else
4124 {
4125 /* Update I2C state */
4126 hi2c->State = HAL_I2C_STATE_READY;
4127 hi2c->Mode = HAL_I2C_MODE_NONE;
4128
4129 /* Update I2C error code */
4131
4132 /* Process Unlocked */
4133 __HAL_UNLOCK(hi2c);
4134
4135 return HAL_ERROR;
4136 }
4137 }
4138 else
4139 {
4140 /* Enable Acknowledge */
4141 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4142
4143 /* If transfer direction not change and there is no request to start another frame, do not generate Restart Condition */
4144 /* Mean Previous state is same as current state */
4145 if ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(XferOptions) == 1))
4146 {
4147 /* Generate Start */
4148 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
4149 }
4150
4151 /* Process Unlocked */
4152 __HAL_UNLOCK(hi2c);
4153
4154 /* Note : The I2C interrupts must be enabled after unlocking current process
4155 to avoid the risk of I2C interrupt handle execution before current
4156 process unlock */
4157
4158 /* Enable interrupts */
4159 __HAL_I2C_ENABLE_IT(hi2c, enableIT);
4160 }
4161 return HAL_OK;
4162 }
4163 else
4164 {
4165 return HAL_BUSY;
4166 }
4167}
4168
4179HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
4180{
4181 /* Check the parameters */
4183
4184 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4185 {
4186 if ((pData == NULL) || (Size == 0U))
4187 {
4188 return HAL_ERROR;
4189 }
4190
4191 /* Process Locked */
4192 __HAL_LOCK(hi2c);
4193
4194 /* Check if the I2C is already enabled */
4195 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
4196 {
4197 /* Enable I2C peripheral */
4198 __HAL_I2C_ENABLE(hi2c);
4199 }
4200
4201 /* Disable Pos */
4202 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
4203
4205 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4207
4208 /* Prepare transfer parameters */
4209 hi2c->pBuffPtr = pData;
4210 hi2c->XferCount = Size;
4211 hi2c->XferSize = hi2c->XferCount;
4212 hi2c->XferOptions = XferOptions;
4213
4214 /* Clear ADDR flag */
4216
4217 /* Process Unlocked */
4218 __HAL_UNLOCK(hi2c);
4219
4220 /* Note : The I2C interrupts must be enabled after unlocking current process
4221 to avoid the risk of I2C interrupt handle execution before current
4222 process unlock */
4223
4224 /* Enable EVT, BUF and ERR interrupt */
4226
4227 return HAL_OK;
4228 }
4229 else
4230 {
4231 return HAL_BUSY;
4232 }
4233}
4234
4245HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
4246{
4247 HAL_StatusTypeDef dmaxferstatus;
4248
4249 /* Check the parameters */
4251
4252 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4253 {
4254 if ((pData == NULL) || (Size == 0U))
4255 {
4256 return HAL_ERROR;
4257 }
4258
4259 /* Process Locked */
4260 __HAL_LOCK(hi2c);
4261
4262 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
4264
4265 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
4266 /* and then toggle the HAL slave RX state to TX state */
4268 {
4269 if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4270 {
4271 /* Abort DMA Xfer if any */
4272 if (hi2c->hdmarx != NULL)
4273 {
4274 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
4275
4276 /* Set the I2C DMA Abort callback :
4277 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4278 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
4279
4280 /* Abort DMA RX */
4281 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
4282 {
4283 /* Call Directly XferAbortCallback function in case of error */
4284 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
4285 }
4286 }
4287 }
4288 }
4289 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
4290 {
4291 if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4292 {
4293 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
4294
4295 /* Abort DMA Xfer if any */
4296 if (hi2c->hdmatx != NULL)
4297 {
4298 /* Set the I2C DMA Abort callback :
4299 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4300 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
4301
4302 /* Abort DMA TX */
4303 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
4304 {
4305 /* Call Directly XferAbortCallback function in case of error */
4306 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
4307 }
4308 }
4309 }
4310 }
4311 else
4312 {
4313 /* Nothing to do */
4314 }
4315
4316 /* Check if the I2C is already enabled */
4317 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
4318 {
4319 /* Enable I2C peripheral */
4320 __HAL_I2C_ENABLE(hi2c);
4321 }
4322
4323 /* Disable Pos */
4324 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
4325
4327 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4329
4330 /* Prepare transfer parameters */
4331 hi2c->pBuffPtr = pData;
4332 hi2c->XferCount = Size;
4333 hi2c->XferSize = hi2c->XferCount;
4334 hi2c->XferOptions = XferOptions;
4335
4336 if (hi2c->hdmatx != NULL)
4337 {
4338 /* Set the I2C DMA transfer complete callback */
4339 hi2c->hdmatx->XferCpltCallback = I2C_DMAXferCplt;
4340
4341 /* Set the DMA error callback */
4342 hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
4343
4344 /* Set the unused DMA callbacks to NULL */
4345 hi2c->hdmatx->XferHalfCpltCallback = NULL;
4346 hi2c->hdmatx->XferAbortCallback = NULL;
4347
4348 /* Enable the DMA stream */
4349 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)hi2c->pBuffPtr, (uint32_t)&hi2c->Instance->DR, hi2c->XferSize);
4350 }
4351 else
4352 {
4353 /* Update I2C state */
4355 hi2c->Mode = HAL_I2C_MODE_NONE;
4356
4357 /* Update I2C error code */
4359
4360 /* Process Unlocked */
4361 __HAL_UNLOCK(hi2c);
4362
4363 return HAL_ERROR;
4364 }
4365
4366 if (dmaxferstatus == HAL_OK)
4367 {
4368 /* Enable Address Acknowledge */
4369 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4370
4371 /* Clear ADDR flag */
4373
4374 /* Process Unlocked */
4375 __HAL_UNLOCK(hi2c);
4376
4377 /* Note : The I2C interrupts must be enabled after unlocking current process
4378 to avoid the risk of I2C interrupt handle execution before current
4379 process unlock */
4380 /* Enable EVT and ERR interrupt */
4382
4383 /* Enable DMA Request */
4384 hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
4385
4386 return HAL_OK;
4387 }
4388 else
4389 {
4390 /* Update I2C state */
4391 hi2c->State = HAL_I2C_STATE_READY;
4392 hi2c->Mode = HAL_I2C_MODE_NONE;
4393
4394 /* Update I2C error code */
4396
4397 /* Process Unlocked */
4398 __HAL_UNLOCK(hi2c);
4399
4400 return HAL_ERROR;
4401 }
4402 }
4403 else
4404 {
4405 return HAL_BUSY;
4406 }
4407}
4408
4419HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
4420{
4421 /* Check the parameters */
4423
4424 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4425 {
4426 if ((pData == NULL) || (Size == 0U))
4427 {
4428 return HAL_ERROR;
4429 }
4430
4431 /* Process Locked */
4432 __HAL_LOCK(hi2c);
4433
4434 /* Check if the I2C is already enabled */
4435 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
4436 {
4437 /* Enable I2C peripheral */
4438 __HAL_I2C_ENABLE(hi2c);
4439 }
4440
4441 /* Disable Pos */
4442 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
4443
4445 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4447
4448 /* Prepare transfer parameters */
4449 hi2c->pBuffPtr = pData;
4450 hi2c->XferCount = Size;
4451 hi2c->XferSize = hi2c->XferCount;
4452 hi2c->XferOptions = XferOptions;
4453
4454 /* Clear ADDR flag */
4456
4457 /* Process Unlocked */
4458 __HAL_UNLOCK(hi2c);
4459
4460 /* Note : The I2C interrupts must be enabled after unlocking current process
4461 to avoid the risk of I2C interrupt handle execution before current
4462 process unlock */
4463
4464 /* Enable EVT, BUF and ERR interrupt */
4466
4467 return HAL_OK;
4468 }
4469 else
4470 {
4471 return HAL_BUSY;
4472 }
4473}
4474
4485HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
4486{
4487 HAL_StatusTypeDef dmaxferstatus;
4488
4489 /* Check the parameters */
4491
4492 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
4493 {
4494 if ((pData == NULL) || (Size == 0U))
4495 {
4496 return HAL_ERROR;
4497 }
4498
4499 /* Process Locked */
4500 __HAL_LOCK(hi2c);
4501
4502 /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
4504
4505 /* I2C cannot manage full duplex exchange so disable previous IT enabled if any */
4506 /* and then toggle the HAL slave RX state to TX state */
4508 {
4509 if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4510 {
4511 /* Abort DMA Xfer if any */
4512 if (hi2c->hdmarx != NULL)
4513 {
4514 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
4515
4516 /* Set the I2C DMA Abort callback :
4517 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4518 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
4519
4520 /* Abort DMA RX */
4521 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
4522 {
4523 /* Call Directly XferAbortCallback function in case of error */
4524 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
4525 }
4526 }
4527 }
4528 }
4529 else if (hi2c->State == HAL_I2C_STATE_BUSY_TX_LISTEN)
4530 {
4531 if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
4532 {
4533 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
4534
4535 /* Abort DMA Xfer if any */
4536 if (hi2c->hdmatx != NULL)
4537 {
4538 /* Set the I2C DMA Abort callback :
4539 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
4540 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
4541
4542 /* Abort DMA TX */
4543 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
4544 {
4545 /* Call Directly XferAbortCallback function in case of error */
4546 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
4547 }
4548 }
4549 }
4550 }
4551 else
4552 {
4553 /* Nothing to do */
4554 }
4555
4556 /* Check if the I2C is already enabled */
4557 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
4558 {
4559 /* Enable I2C peripheral */
4560 __HAL_I2C_ENABLE(hi2c);
4561 }
4562
4563 /* Disable Pos */
4564 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
4565
4567 hi2c->Mode = HAL_I2C_MODE_SLAVE;
4569
4570 /* Prepare transfer parameters */
4571 hi2c->pBuffPtr = pData;
4572 hi2c->XferCount = Size;
4573 hi2c->XferSize = hi2c->XferCount;
4574 hi2c->XferOptions = XferOptions;
4575
4576 if (hi2c->hdmarx != NULL)
4577 {
4578 /* Set the I2C DMA transfer complete callback */
4579 hi2c->hdmarx->XferCpltCallback = I2C_DMAXferCplt;
4580
4581 /* Set the DMA error callback */
4582 hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
4583
4584 /* Set the unused DMA callbacks to NULL */
4585 hi2c->hdmarx->XferHalfCpltCallback = NULL;
4586 hi2c->hdmarx->XferAbortCallback = NULL;
4587
4588 /* Enable the DMA stream */
4589 dmaxferstatus = HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->pBuffPtr, hi2c->XferSize);
4590 }
4591 else
4592 {
4593 /* Update I2C state */
4595 hi2c->Mode = HAL_I2C_MODE_NONE;
4596
4597 /* Update I2C error code */
4599
4600 /* Process Unlocked */
4601 __HAL_UNLOCK(hi2c);
4602
4603 return HAL_ERROR;
4604 }
4605
4606 if (dmaxferstatus == HAL_OK)
4607 {
4608 /* Enable Address Acknowledge */
4609 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4610
4611 /* Clear ADDR flag */
4613
4614 /* Process Unlocked */
4615 __HAL_UNLOCK(hi2c);
4616
4617 /* Enable DMA Request */
4618 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
4619
4620 /* Note : The I2C interrupts must be enabled after unlocking current process
4621 to avoid the risk of I2C interrupt handle execution before current
4622 process unlock */
4623 /* Enable EVT and ERR interrupt */
4625
4626 return HAL_OK;
4627 }
4628 else
4629 {
4630 /* Update I2C state */
4631 hi2c->State = HAL_I2C_STATE_READY;
4632 hi2c->Mode = HAL_I2C_MODE_NONE;
4633
4634 /* Update I2C error code */
4636
4637 /* Process Unlocked */
4638 __HAL_UNLOCK(hi2c);
4639
4640 return HAL_ERROR;
4641 }
4642 }
4643 else
4644 {
4645 return HAL_BUSY;
4646 }
4647}
4648
4656{
4657 if (hi2c->State == HAL_I2C_STATE_READY)
4658 {
4660
4661 /* Check if the I2C is already enabled */
4662 if ((hi2c->Instance->CR1 & I2C_CR1_PE) != I2C_CR1_PE)
4663 {
4664 /* Enable I2C peripheral */
4665 __HAL_I2C_ENABLE(hi2c);
4666 }
4667
4668 /* Enable Address Acknowledge */
4669 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4670
4671 /* Enable EVT and ERR interrupt */
4673
4674 return HAL_OK;
4675 }
4676 else
4677 {
4678 return HAL_BUSY;
4679 }
4680}
4681
4689{
4690 /* Declaration of tmp to prevent undefined behavior of volatile usage */
4691 uint32_t tmp;
4692
4693 /* Disable Address listen mode only if a transfer is not ongoing */
4694 if (hi2c->State == HAL_I2C_STATE_LISTEN)
4695 {
4696 tmp = (uint32_t)(hi2c->State) & I2C_STATE_MSK;
4697 hi2c->PreviousState = tmp | (uint32_t)(hi2c->Mode);
4698 hi2c->State = HAL_I2C_STATE_READY;
4699 hi2c->Mode = HAL_I2C_MODE_NONE;
4700
4701 /* Disable Address Acknowledge */
4702 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4703
4704 /* Disable EVT and ERR interrupt */
4706
4707 return HAL_OK;
4708 }
4709 else
4710 {
4711 return HAL_BUSY;
4712 }
4713}
4714
4724{
4725 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
4726 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
4727
4728 /* Prevent unused argument(s) compilation warning */
4729 UNUSED(DevAddress);
4730
4731 /* Abort Master transfer during Receive or Transmit process */
4732 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && ((CurrentMode == HAL_I2C_MODE_MASTER) ||
4733 (CurrentMode == HAL_I2C_MODE_MEM)))
4734 {
4735 /* Process Locked */
4736 __HAL_LOCK(hi2c);
4737
4738 hi2c->PreviousState = I2C_STATE_NONE;
4739 hi2c->State = HAL_I2C_STATE_ABORT;
4740
4741 /* Disable Acknowledge */
4742 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
4743
4744 /* Generate Stop */
4745 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
4746
4747 hi2c->XferCount = 0U;
4748
4749 /* Disable EVT, BUF and ERR interrupt */
4751
4752 /* Process Unlocked */
4753 __HAL_UNLOCK(hi2c);
4754
4755 /* Call the corresponding callback to inform upper layer of End of Transfer */
4756 I2C_ITError(hi2c);
4757
4758 return HAL_OK;
4759 }
4760 else
4761 {
4762 /* Wrong usage of abort function */
4763 /* This function should be used only in case of abort monitored by master device */
4764 /* Or periphal is not in busy state, mean there is no active sequence to be abort */
4765 return HAL_ERROR;
4766 }
4767}
4768
4772
4776
4784{
4785 uint32_t sr1itflags;
4786 uint32_t sr2itflags = 0U;
4787 uint32_t itsources = READ_REG(hi2c->Instance->CR2);
4788 uint32_t CurrentXferOptions = hi2c->XferOptions;
4789 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
4790 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
4791
4792 /* Master or Memory mode selected */
4793 if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
4794 {
4795 sr2itflags = READ_REG(hi2c->Instance->SR2);
4796 sr1itflags = READ_REG(hi2c->Instance->SR1);
4797
4798 /* Exit IRQ event until Start Bit detected in case of Other frame requested */
4799 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) == RESET) && (IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(CurrentXferOptions) == 1U))
4800 {
4801 return;
4802 }
4803
4804 /* SB Set ----------------------------------------------------------------*/
4805 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_SB) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4806 {
4807 /* Convert OTHER_xxx XferOptions if any */
4808 I2C_ConvertOtherXferOptions(hi2c);
4809
4810 I2C_Master_SB(hi2c);
4811 }
4812 /* ADD10 Set -------------------------------------------------------------*/
4813 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADD10) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4814 {
4815 I2C_Master_ADD10(hi2c);
4816 }
4817 /* ADDR Set --------------------------------------------------------------*/
4818 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4819 {
4820 I2C_Master_ADDR(hi2c);
4821 }
4822 /* I2C in mode Transmitter -----------------------------------------------*/
4823 else if (I2C_CHECK_FLAG(sr2itflags, I2C_FLAG_TRA) != RESET)
4824 {
4825 /* Do not check buffer and BTF flag if a Xfer DMA is on going */
4826 if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
4827 {
4828 /* TXE set and BTF reset -----------------------------------------------*/
4829 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
4830 {
4831 I2C_MasterTransmit_TXE(hi2c);
4832 }
4833 /* BTF set -------------------------------------------------------------*/
4834 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4835 {
4836 if (CurrentState == HAL_I2C_STATE_BUSY_TX)
4837 {
4838 I2C_MasterTransmit_BTF(hi2c);
4839 }
4840 else /* HAL_I2C_MODE_MEM */
4841 {
4842 if (CurrentMode == HAL_I2C_MODE_MEM)
4843 {
4844 I2C_MemoryTransmit_TXE_BTF(hi2c);
4845 }
4846 }
4847 }
4848 else
4849 {
4850 /* Do nothing */
4851 }
4852 }
4853 }
4854 /* I2C in mode Receiver --------------------------------------------------*/
4855 else
4856 {
4857 /* Do not check buffer and BTF flag if a Xfer DMA is on going */
4858 if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN)
4859 {
4860 /* RXNE set and BTF reset -----------------------------------------------*/
4861 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
4862 {
4863 I2C_MasterReceive_RXNE(hi2c);
4864 }
4865 /* BTF set -------------------------------------------------------------*/
4866 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4867 {
4868 I2C_MasterReceive_BTF(hi2c);
4869 }
4870 else
4871 {
4872 /* Do nothing */
4873 }
4874 }
4875 }
4876 }
4877 /* Slave mode selected */
4878 else
4879 {
4880 /* If an error is detected, read only SR1 register to prevent */
4881 /* a clear of ADDR flags by reading SR2 after reading SR1 in Error treatment */
4882 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
4883 {
4884 sr1itflags = READ_REG(hi2c->Instance->SR1);
4885 }
4886 else
4887 {
4888 sr2itflags = READ_REG(hi2c->Instance->SR2);
4889 sr1itflags = READ_REG(hi2c->Instance->SR1);
4890 }
4891
4892 /* ADDR set --------------------------------------------------------------*/
4893 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ADDR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4894 {
4895 /* Now time to read SR2, this will clear ADDR flag automatically */
4896 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
4897 {
4898 sr2itflags = READ_REG(hi2c->Instance->SR2);
4899 }
4900 I2C_Slave_ADDR(hi2c, sr2itflags);
4901 }
4902 /* STOPF set --------------------------------------------------------------*/
4903 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_STOPF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4904 {
4905 I2C_Slave_STOPF(hi2c);
4906 }
4907 /* I2C in mode Transmitter -----------------------------------------------*/
4908 else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
4909 {
4910 /* TXE set and BTF reset -----------------------------------------------*/
4911 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_TXE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
4912 {
4913 I2C_SlaveTransmit_TXE(hi2c);
4914 }
4915 /* BTF set -------------------------------------------------------------*/
4916 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4917 {
4918 I2C_SlaveTransmit_BTF(hi2c);
4919 }
4920 else
4921 {
4922 /* Do nothing */
4923 }
4924 }
4925 /* I2C in mode Receiver --------------------------------------------------*/
4926 else
4927 {
4928 /* RXNE set and BTF reset ----------------------------------------------*/
4929 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_RXNE) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_BUF) != RESET) && (I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) == RESET))
4930 {
4931 I2C_SlaveReceive_RXNE(hi2c);
4932 }
4933 /* BTF set -------------------------------------------------------------*/
4934 else if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BTF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_EVT) != RESET))
4935 {
4936 I2C_SlaveReceive_BTF(hi2c);
4937 }
4938 else
4939 {
4940 /* Do nothing */
4941 }
4942 }
4943 }
4944}
4945
4953{
4955 uint32_t tmp2;
4957 uint32_t tmp4;
4958 uint32_t sr1itflags = READ_REG(hi2c->Instance->SR1);
4959 uint32_t itsources = READ_REG(hi2c->Instance->CR2);
4960 uint32_t error = HAL_I2C_ERROR_NONE;
4961 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
4962
4963 /* I2C Bus error interrupt occurred ----------------------------------------*/
4964 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_BERR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
4965 {
4966 error |= HAL_I2C_ERROR_BERR;
4967
4968 /* Clear BERR flag */
4970 }
4971
4972 /* I2C Arbitration Lost error interrupt occurred ---------------------------*/
4973 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_ARLO) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
4974 {
4975 error |= HAL_I2C_ERROR_ARLO;
4976
4977 /* Clear ARLO flag */
4979 }
4980
4981 /* I2C Acknowledge failure error interrupt occurred ------------------------*/
4982 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_AF) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
4983 {
4984 tmp1 = CurrentMode;
4985 tmp2 = hi2c->XferCount;
4986 tmp3 = hi2c->State;
4987 tmp4 = hi2c->PreviousState;
4988 if ((tmp1 == HAL_I2C_MODE_SLAVE) && (tmp2 == 0U) && \
4989 ((tmp3 == HAL_I2C_STATE_BUSY_TX) || (tmp3 == HAL_I2C_STATE_BUSY_TX_LISTEN) || \
4990 ((tmp3 == HAL_I2C_STATE_LISTEN) && (tmp4 == I2C_STATE_SLAVE_BUSY_TX))))
4991 {
4992 I2C_Slave_AF(hi2c);
4993 }
4994 else
4995 {
4996 /* Clear AF flag */
4998
4999 error |= HAL_I2C_ERROR_AF;
5000
5001 /* Do not generate a STOP in case of Slave receive non acknowledge during transfer (mean not at the end of transfer) */
5002 if ((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM))
5003 {
5004 /* Generate Stop */
5005 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5006 }
5007 }
5008 }
5009
5010 /* I2C Over-Run/Under-Run interrupt occurred -------------------------------*/
5011 if ((I2C_CHECK_FLAG(sr1itflags, I2C_FLAG_OVR) != RESET) && (I2C_CHECK_IT_SOURCE(itsources, I2C_IT_ERR) != RESET))
5012 {
5013 error |= HAL_I2C_ERROR_OVR;
5014 /* Clear OVR flag */
5016 }
5017
5018 /* Call the Error Callback in case of Error detected -----------------------*/
5019 if (error != HAL_I2C_ERROR_NONE)
5020 {
5021 hi2c->ErrorCode |= error;
5022 I2C_ITError(hi2c);
5023 }
5024}
5025
5033{
5034 /* Prevent unused argument(s) compilation warning */
5035 UNUSED(hi2c);
5036
5037 /* NOTE : This function should not be modified, when the callback is needed,
5038 the HAL_I2C_MasterTxCpltCallback could be implemented in the user file
5039 */
5040}
5041
5049{
5050 /* Prevent unused argument(s) compilation warning */
5051 UNUSED(hi2c);
5052
5053 /* NOTE : This function should not be modified, when the callback is needed,
5054 the HAL_I2C_MasterRxCpltCallback could be implemented in the user file
5055 */
5056}
5057
5064{
5065 /* Prevent unused argument(s) compilation warning */
5066 UNUSED(hi2c);
5067
5068 /* NOTE : This function should not be modified, when the callback is needed,
5069 the HAL_I2C_SlaveTxCpltCallback could be implemented in the user file
5070 */
5071}
5072
5080{
5081 /* Prevent unused argument(s) compilation warning */
5082 UNUSED(hi2c);
5083
5084 /* NOTE : This function should not be modified, when the callback is needed,
5085 the HAL_I2C_SlaveRxCpltCallback could be implemented in the user file
5086 */
5087}
5088
5097__weak void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
5098{
5099 /* Prevent unused argument(s) compilation warning */
5100 UNUSED(hi2c);
5101 UNUSED(TransferDirection);
5102 UNUSED(AddrMatchCode);
5103
5104 /* NOTE : This function should not be modified, when the callback is needed,
5105 the HAL_I2C_AddrCallback() could be implemented in the user file
5106 */
5107}
5108
5116{
5117 /* Prevent unused argument(s) compilation warning */
5118 UNUSED(hi2c);
5119
5120 /* NOTE : This function should not be modified, when the callback is needed,
5121 the HAL_I2C_ListenCpltCallback() could be implemented in the user file
5122 */
5123}
5124
5132{
5133 /* Prevent unused argument(s) compilation warning */
5134 UNUSED(hi2c);
5135
5136 /* NOTE : This function should not be modified, when the callback is needed,
5137 the HAL_I2C_MemTxCpltCallback could be implemented in the user file
5138 */
5139}
5140
5148{
5149 /* Prevent unused argument(s) compilation warning */
5150 UNUSED(hi2c);
5151
5152 /* NOTE : This function should not be modified, when the callback is needed,
5153 the HAL_I2C_MemRxCpltCallback could be implemented in the user file
5154 */
5155}
5156
5164{
5165 /* Prevent unused argument(s) compilation warning */
5166 UNUSED(hi2c);
5167
5168 /* NOTE : This function should not be modified, when the callback is needed,
5169 the HAL_I2C_ErrorCallback could be implemented in the user file
5170 */
5171}
5172
5180{
5181 /* Prevent unused argument(s) compilation warning */
5182 UNUSED(hi2c);
5183
5184 /* NOTE : This function should not be modified, when the callback is needed,
5185 the HAL_I2C_AbortCpltCallback could be implemented in the user file
5186 */
5187}
5188
5192
5207
5215{
5216 /* Return I2C handle state */
5217 return hi2c->State;
5218}
5219
5227{
5228 return hi2c->Mode;
5229}
5230
5237uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
5238{
5239 return hi2c->ErrorCode;
5240}
5241
5245
5249
5253
5260static void I2C_MasterTransmit_TXE(I2C_HandleTypeDef *hi2c)
5261{
5262 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
5263 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
5264 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
5265 uint32_t CurrentXferOptions = hi2c->XferOptions;
5266
5267 if ((hi2c->XferSize == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
5268 {
5269 /* Call TxCpltCallback() directly if no stop mode is set */
5270 if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
5271 {
5273
5274 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
5275 hi2c->Mode = HAL_I2C_MODE_NONE;
5276 hi2c->State = HAL_I2C_STATE_READY;
5277
5278#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5279 hi2c->MasterTxCpltCallback(hi2c);
5280#else
5282#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5283 }
5284 else /* Generate Stop condition then Call TxCpltCallback() */
5285 {
5286 /* Disable EVT, BUF and ERR interrupt */
5288
5289 /* Generate Stop */
5290 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5291
5292 hi2c->PreviousState = I2C_STATE_NONE;
5293 hi2c->State = HAL_I2C_STATE_READY;
5294
5295 if (hi2c->Mode == HAL_I2C_MODE_MEM)
5296 {
5297 hi2c->Mode = HAL_I2C_MODE_NONE;
5298#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5299 hi2c->MemTxCpltCallback(hi2c);
5300#else
5302#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5303 }
5304 else
5305 {
5306 hi2c->Mode = HAL_I2C_MODE_NONE;
5307#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5308 hi2c->MasterTxCpltCallback(hi2c);
5309#else
5311#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5312 }
5313 }
5314 }
5315 else if ((CurrentState == HAL_I2C_STATE_BUSY_TX) || \
5316 ((CurrentMode == HAL_I2C_MODE_MEM) && (CurrentState == HAL_I2C_STATE_BUSY_RX)))
5317 {
5318 if (hi2c->XferCount == 0U)
5319 {
5320 /* Disable BUF interrupt */
5322 }
5323 else
5324 {
5325 if (hi2c->Mode == HAL_I2C_MODE_MEM)
5326 {
5327 I2C_MemoryTransmit_TXE_BTF(hi2c);
5328 }
5329 else
5330 {
5331 /* Write data to DR */
5332 hi2c->Instance->DR = *hi2c->pBuffPtr;
5333
5334 /* Increment Buffer pointer */
5335 hi2c->pBuffPtr++;
5336
5337 /* Update counter */
5338 hi2c->XferCount--;
5339 }
5340 }
5341 }
5342 else
5343 {
5344 /* Do nothing */
5345 }
5346}
5347
5354static void I2C_MasterTransmit_BTF(I2C_HandleTypeDef *hi2c)
5355{
5356 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
5357 uint32_t CurrentXferOptions = hi2c->XferOptions;
5358
5359 if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
5360 {
5361 if (hi2c->XferCount != 0U)
5362 {
5363 /* Write data to DR */
5364 hi2c->Instance->DR = *hi2c->pBuffPtr;
5365
5366 /* Increment Buffer pointer */
5367 hi2c->pBuffPtr++;
5368
5369 /* Update counter */
5370 hi2c->XferCount--;
5371 }
5372 else
5373 {
5374 /* Call TxCpltCallback() directly if no stop mode is set */
5375 if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) && (CurrentXferOptions != I2C_NO_OPTION_FRAME))
5376 {
5378
5379 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_TX;
5380 hi2c->Mode = HAL_I2C_MODE_NONE;
5381 hi2c->State = HAL_I2C_STATE_READY;
5382
5383#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5384 hi2c->MasterTxCpltCallback(hi2c);
5385#else
5387#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5388 }
5389 else /* Generate Stop condition then Call TxCpltCallback() */
5390 {
5391 /* Disable EVT, BUF and ERR interrupt */
5393
5394 /* Generate Stop */
5395 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5396
5397 hi2c->PreviousState = I2C_STATE_NONE;
5398 hi2c->State = HAL_I2C_STATE_READY;
5399 if (hi2c->Mode == HAL_I2C_MODE_MEM)
5400 {
5401 hi2c->Mode = HAL_I2C_MODE_NONE;
5402#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5403 hi2c->MemTxCpltCallback(hi2c);
5404#else
5406#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5407 }
5408 else
5409 {
5410 hi2c->Mode = HAL_I2C_MODE_NONE;
5411
5412#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5413 hi2c->MasterTxCpltCallback(hi2c);
5414#else
5416#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5417 }
5418 }
5419 }
5420 }
5421 else
5422 {
5423 /* Do nothing */
5424 }
5425}
5426
5433static void I2C_MemoryTransmit_TXE_BTF(I2C_HandleTypeDef *hi2c)
5434{
5435 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
5436 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
5437
5438 if (hi2c->EventCount == 0U)
5439 {
5440 /* If Memory address size is 8Bit */
5441 if (hi2c->MemaddSize == I2C_MEMADD_SIZE_8BIT)
5442 {
5443 /* Send Memory Address */
5444 hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
5445
5446 hi2c->EventCount += 2U;
5447 }
5448 /* If Memory address size is 16Bit */
5449 else
5450 {
5451 /* Send MSB of Memory Address */
5452 hi2c->Instance->DR = I2C_MEM_ADD_MSB(hi2c->Memaddress);
5453
5454 hi2c->EventCount++;
5455 }
5456 }
5457 else if (hi2c->EventCount == 1U)
5458 {
5459 /* Send LSB of Memory Address */
5460 hi2c->Instance->DR = I2C_MEM_ADD_LSB(hi2c->Memaddress);
5461
5462 hi2c->EventCount++;
5463 }
5464 else if (hi2c->EventCount == 2U)
5465 {
5466 if (CurrentState == HAL_I2C_STATE_BUSY_RX)
5467 {
5468 /* Generate Restart */
5469 hi2c->Instance->CR1 |= I2C_CR1_START;
5470
5471 hi2c->EventCount++;
5472 }
5473 else if ((hi2c->XferCount > 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
5474 {
5475 /* Write data to DR */
5476 hi2c->Instance->DR = *hi2c->pBuffPtr;
5477
5478 /* Increment Buffer pointer */
5479 hi2c->pBuffPtr++;
5480
5481 /* Update counter */
5482 hi2c->XferCount--;
5483 }
5484 else if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX))
5485 {
5486 /* Generate Stop condition then Call TxCpltCallback() */
5487 /* Disable EVT, BUF and ERR interrupt */
5489
5490 /* Generate Stop */
5491 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5492
5493 hi2c->PreviousState = I2C_STATE_NONE;
5494 hi2c->State = HAL_I2C_STATE_READY;
5495 hi2c->Mode = HAL_I2C_MODE_NONE;
5496#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5497 hi2c->MemTxCpltCallback(hi2c);
5498#else
5500#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5501 }
5502 else
5503 {
5504 /* Do nothing */
5505 }
5506 }
5507 else
5508 {
5509 /* Clear TXE and BTF flags */
5510 I2C_Flush_DR(hi2c);
5511 }
5512}
5513
5520static void I2C_MasterReceive_RXNE(I2C_HandleTypeDef *hi2c)
5521{
5522 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5523 {
5524 uint32_t tmp;
5525 uint32_t CurrentXferOptions;
5526
5527 CurrentXferOptions = hi2c->XferOptions;
5528 tmp = hi2c->XferCount;
5529 if (tmp > 3U)
5530 {
5531 /* Read data from DR */
5532 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5533
5534 /* Increment Buffer pointer */
5535 hi2c->pBuffPtr++;
5536
5537 /* Update counter */
5538 hi2c->XferCount--;
5539
5540 if (hi2c->XferCount == (uint16_t)3)
5541 {
5542 /* Disable BUF interrupt, this help to treat correctly the last 4 bytes
5543 on BTF subroutine */
5544 /* Disable BUF interrupt */
5546 }
5547 }
5548 else if ((hi2c->XferOptions != I2C_FIRST_AND_NEXT_FRAME) && ((tmp == 1U) || (tmp == 0U)))
5549 {
5550 if (I2C_WaitOnSTOPRequestThroughIT(hi2c) == HAL_OK)
5551 {
5552 /* Disable Acknowledge */
5553 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5554
5555 /* Disable EVT, BUF and ERR interrupt */
5557
5558 /* Read data from DR */
5559 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5560
5561 /* Increment Buffer pointer */
5562 hi2c->pBuffPtr++;
5563
5564 /* Update counter */
5565 hi2c->XferCount--;
5566
5567 hi2c->State = HAL_I2C_STATE_READY;
5568
5569 if (hi2c->Mode == HAL_I2C_MODE_MEM)
5570 {
5571 hi2c->Mode = HAL_I2C_MODE_NONE;
5572 hi2c->PreviousState = I2C_STATE_NONE;
5573
5574#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5575 hi2c->MemRxCpltCallback(hi2c);
5576#else
5578#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5579 }
5580 else
5581 {
5582 hi2c->Mode = HAL_I2C_MODE_NONE;
5583 if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
5584 {
5585 hi2c->PreviousState = I2C_STATE_NONE;
5586 }
5587 else
5588 {
5589 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
5590 }
5591
5592#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5593 hi2c->MasterRxCpltCallback(hi2c);
5594#else
5596#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5597 }
5598 }
5599 else
5600 {
5601 /* Disable EVT, BUF and ERR interrupt */
5603
5604 /* Read data from DR */
5605 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5606
5607 /* Increment Buffer pointer */
5608 hi2c->pBuffPtr++;
5609
5610 /* Update counter */
5611 hi2c->XferCount--;
5612
5613 hi2c->State = HAL_I2C_STATE_READY;
5614 hi2c->Mode = HAL_I2C_MODE_NONE;
5615
5616 /* Call user error callback */
5617#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5618 hi2c->ErrorCallback(hi2c);
5619#else
5621#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5622 }
5623 }
5624 else
5625 {
5626 /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
5627 on BTF subroutine if there is a reception delay between N-1 and N byte */
5629 }
5630 }
5631}
5632
5639static void I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
5640{
5641 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
5642 uint32_t CurrentXferOptions = hi2c->XferOptions;
5643
5644 if (hi2c->XferCount == 4U)
5645 {
5646 /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
5647 on BTF subroutine if there is a reception delay between N-1 and N byte */
5649
5650 /* Read data from DR */
5651 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5652
5653 /* Increment Buffer pointer */
5654 hi2c->pBuffPtr++;
5655
5656 /* Update counter */
5657 hi2c->XferCount--;
5658 }
5659 else if (hi2c->XferCount == 3U)
5660 {
5661 /* Disable BUF interrupt, this help to treat correctly the last 2 bytes
5662 on BTF subroutine if there is a reception delay between N-1 and N byte */
5664
5665 if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME))
5666 {
5667 /* Disable Acknowledge */
5668 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5669 }
5670
5671 /* Read data from DR */
5672 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5673
5674 /* Increment Buffer pointer */
5675 hi2c->pBuffPtr++;
5676
5677 /* Update counter */
5678 hi2c->XferCount--;
5679 }
5680 else if (hi2c->XferCount == 2U)
5681 {
5682 /* Prepare next transfer or stop current transfer */
5683 if ((CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP))
5684 {
5685 /* Disable Acknowledge */
5686 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5687 }
5688 else if ((CurrentXferOptions == I2C_NEXT_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_NEXT_FRAME))
5689 {
5690 /* Enable Acknowledge */
5691 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5692 }
5693 else
5694 {
5695 /* Generate Stop */
5696 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5697 }
5698
5699 /* Read data from DR */
5700 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5701
5702 /* Increment Buffer pointer */
5703 hi2c->pBuffPtr++;
5704
5705 /* Update counter */
5706 hi2c->XferCount--;
5707
5708 /* Read data from DR */
5709 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5710
5711 /* Increment Buffer pointer */
5712 hi2c->pBuffPtr++;
5713
5714 /* Update counter */
5715 hi2c->XferCount--;
5716
5717 /* Disable EVT and ERR interrupt */
5719
5720 hi2c->State = HAL_I2C_STATE_READY;
5721 if (hi2c->Mode == HAL_I2C_MODE_MEM)
5722 {
5723 hi2c->Mode = HAL_I2C_MODE_NONE;
5724 hi2c->PreviousState = I2C_STATE_NONE;
5725#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5726 hi2c->MemRxCpltCallback(hi2c);
5727#else
5729#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5730 }
5731 else
5732 {
5733 hi2c->Mode = HAL_I2C_MODE_NONE;
5734 if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
5735 {
5736 hi2c->PreviousState = I2C_STATE_NONE;
5737 }
5738 else
5739 {
5740 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
5741 }
5742#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
5743 hi2c->MasterRxCpltCallback(hi2c);
5744#else
5746#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
5747 }
5748 }
5749 else
5750 {
5751 /* Read data from DR */
5752 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
5753
5754 /* Increment Buffer pointer */
5755 hi2c->pBuffPtr++;
5756
5757 /* Update counter */
5758 hi2c->XferCount--;
5759 }
5760}
5761
5768static void I2C_Master_SB(I2C_HandleTypeDef *hi2c)
5769{
5770 if (hi2c->Mode == HAL_I2C_MODE_MEM)
5771 {
5772 if (hi2c->EventCount == 0U)
5773 {
5774 /* Send slave address */
5775 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
5776 }
5777 else
5778 {
5779 hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
5780 }
5781 }
5782 else
5783 {
5785 {
5786 /* Send slave 7 Bits address */
5787 if (hi2c->State == HAL_I2C_STATE_BUSY_TX)
5788 {
5789 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(hi2c->Devaddress);
5790 }
5791 else
5792 {
5793 hi2c->Instance->DR = I2C_7BIT_ADD_READ(hi2c->Devaddress);
5794 }
5795
5796 if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
5797 || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
5798 {
5799 /* Enable DMA Request */
5800 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
5801 }
5802 }
5803 else
5804 {
5805 if (hi2c->EventCount == 0U)
5806 {
5807 /* Send header of slave address */
5808 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(hi2c->Devaddress);
5809 }
5810 else if (hi2c->EventCount == 1U)
5811 {
5812 /* Send header of slave address */
5813 hi2c->Instance->DR = I2C_10BIT_HEADER_READ(hi2c->Devaddress);
5814 }
5815 else
5816 {
5817 /* Do nothing */
5818 }
5819 }
5820 }
5821}
5822
5829static void I2C_Master_ADD10(I2C_HandleTypeDef *hi2c)
5830{
5831 /* Send slave address */
5832 hi2c->Instance->DR = I2C_10BIT_ADDRESS(hi2c->Devaddress);
5833
5834 if (((hi2c->hdmatx != NULL) && (hi2c->hdmatx->XferCpltCallback != NULL))
5835 || ((hi2c->hdmarx != NULL) && (hi2c->hdmarx->XferCpltCallback != NULL)))
5836 {
5837 /* Enable DMA Request */
5838 SET_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
5839 }
5840}
5841
5848static void I2C_Master_ADDR(I2C_HandleTypeDef *hi2c)
5849{
5850 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
5851 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
5852 uint32_t CurrentXferOptions = hi2c->XferOptions;
5853 uint32_t Prev_State = hi2c->PreviousState;
5854
5855 if (hi2c->State == HAL_I2C_STATE_BUSY_RX)
5856 {
5857 if ((hi2c->EventCount == 0U) && (CurrentMode == HAL_I2C_MODE_MEM))
5858 {
5859 /* Clear ADDR flag */
5861 }
5862 else if ((hi2c->EventCount == 0U) && (hi2c->Init.AddressingMode == I2C_ADDRESSINGMODE_10BIT))
5863 {
5864 /* Clear ADDR flag */
5866
5867 /* Generate Restart */
5868 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
5869
5870 hi2c->EventCount++;
5871 }
5872 else
5873 {
5874 if (hi2c->XferCount == 0U)
5875 {
5876 /* Clear ADDR flag */
5878
5879 /* Generate Stop */
5880 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5881 }
5882 else if (hi2c->XferCount == 1U)
5883 {
5884 if (CurrentXferOptions == I2C_NO_OPTION_FRAME)
5885 {
5886 /* Disable Acknowledge */
5887 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5888
5889 if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
5890 {
5891 /* Disable Acknowledge */
5892 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5893
5894 /* Clear ADDR flag */
5896 }
5897 else
5898 {
5899 /* Clear ADDR flag */
5901
5902 /* Generate Stop */
5903 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5904 }
5905 }
5906 /* Prepare next transfer or stop current transfer */
5907 else if ((CurrentXferOptions != I2C_FIRST_AND_LAST_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME) \
5908 && ((Prev_State != I2C_STATE_MASTER_BUSY_RX) || (CurrentXferOptions == I2C_FIRST_FRAME)))
5909 {
5910 if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
5911 {
5912 /* Disable Acknowledge */
5913 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5914 }
5915 else
5916 {
5917 /* Enable Acknowledge */
5918 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5919 }
5920
5921 /* Clear ADDR flag */
5923 }
5924 else
5925 {
5926 /* Disable Acknowledge */
5927 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5928
5929 /* Clear ADDR flag */
5931
5932 /* Generate Stop */
5933 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
5934 }
5935 }
5936 else if (hi2c->XferCount == 2U)
5937 {
5938 if ((CurrentXferOptions != I2C_NEXT_FRAME) && (CurrentXferOptions != I2C_FIRST_AND_NEXT_FRAME) && (CurrentXferOptions != I2C_LAST_FRAME_NO_STOP))
5939 {
5940 /* Disable Acknowledge */
5941 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5942
5943 /* Enable Pos */
5944 SET_BIT(hi2c->Instance->CR1, I2C_CR1_POS);
5945 }
5946 else
5947 {
5948 /* Enable Acknowledge */
5949 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5950 }
5951
5952 if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME)))
5953 {
5954 /* Enable Last DMA bit */
5955 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
5956 }
5957
5958 /* Clear ADDR flag */
5960 }
5961 else
5962 {
5963 /* Enable Acknowledge */
5964 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
5965
5966 if (((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN) && ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME_NO_STOP) || (CurrentXferOptions == I2C_LAST_FRAME)))
5967 {
5968 /* Enable Last DMA bit */
5969 SET_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
5970 }
5971
5972 /* Clear ADDR flag */
5974 }
5975
5976 /* Reset Event counter */
5977 hi2c->EventCount = 0U;
5978 }
5979 }
5980 else
5981 {
5982 /* Clear ADDR flag */
5984 }
5985}
5986
5993static void I2C_SlaveTransmit_TXE(I2C_HandleTypeDef *hi2c)
5994{
5995 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
5996 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
5997
5998 if (hi2c->XferCount != 0U)
5999 {
6000 /* Write data to DR */
6001 hi2c->Instance->DR = *hi2c->pBuffPtr;
6002
6003 /* Increment Buffer pointer */
6004 hi2c->pBuffPtr++;
6005
6006 /* Update counter */
6007 hi2c->XferCount--;
6008
6009 if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN))
6010 {
6011 /* Last Byte is received, disable Interrupt */
6013
6014 /* Set state at HAL_I2C_STATE_LISTEN */
6015 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
6017
6018 /* Call the corresponding callback to inform upper layer of End of Transfer */
6019#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6020 hi2c->SlaveTxCpltCallback(hi2c);
6021#else
6023#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6024 }
6025 }
6026}
6027
6034static void I2C_SlaveTransmit_BTF(I2C_HandleTypeDef *hi2c)
6035{
6036 if (hi2c->XferCount != 0U)
6037 {
6038 /* Write data to DR */
6039 hi2c->Instance->DR = *hi2c->pBuffPtr;
6040
6041 /* Increment Buffer pointer */
6042 hi2c->pBuffPtr++;
6043
6044 /* Update counter */
6045 hi2c->XferCount--;
6046 }
6047}
6048
6055static void I2C_SlaveReceive_RXNE(I2C_HandleTypeDef *hi2c)
6056{
6057 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
6058 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
6059
6060 if (hi2c->XferCount != 0U)
6061 {
6062 /* Read data from DR */
6063 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6064
6065 /* Increment Buffer pointer */
6066 hi2c->pBuffPtr++;
6067
6068 /* Update counter */
6069 hi2c->XferCount--;
6070
6071 if ((hi2c->XferCount == 0U) && (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
6072 {
6073 /* Last Byte is received, disable Interrupt */
6075
6076 /* Set state at HAL_I2C_STATE_LISTEN */
6077 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
6079
6080 /* Call the corresponding callback to inform upper layer of End of Transfer */
6081#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6082 hi2c->SlaveRxCpltCallback(hi2c);
6083#else
6085#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6086 }
6087 }
6088}
6089
6096static void I2C_SlaveReceive_BTF(I2C_HandleTypeDef *hi2c)
6097{
6098 if (hi2c->XferCount != 0U)
6099 {
6100 /* Read data from DR */
6101 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6102
6103 /* Increment Buffer pointer */
6104 hi2c->pBuffPtr++;
6105
6106 /* Update counter */
6107 hi2c->XferCount--;
6108 }
6109}
6110
6118static void I2C_Slave_ADDR(I2C_HandleTypeDef *hi2c, uint32_t IT2Flags)
6119{
6120 uint8_t TransferDirection = I2C_DIRECTION_RECEIVE;
6121 uint16_t SlaveAddrCode;
6122
6123 if (((uint32_t)hi2c->State & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
6124 {
6125 /* Disable BUF interrupt, BUF enabling is manage through slave specific interface */
6127
6128 /* Transfer Direction requested by Master */
6129 if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_TRA) == RESET)
6130 {
6131 TransferDirection = I2C_DIRECTION_TRANSMIT;
6132 }
6133
6134 if (I2C_CHECK_FLAG(IT2Flags, I2C_FLAG_DUALF) == RESET)
6135 {
6136 SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress1;
6137 }
6138 else
6139 {
6140 SlaveAddrCode = (uint16_t)hi2c->Init.OwnAddress2;
6141 }
6142
6143 /* Process Unlocked */
6144 __HAL_UNLOCK(hi2c);
6145
6146 /* Call Slave Addr callback */
6147#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6148 hi2c->AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
6149#else
6150 HAL_I2C_AddrCallback(hi2c, TransferDirection, SlaveAddrCode);
6151#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6152 }
6153 else
6154 {
6155 /* Clear ADDR flag */
6157
6158 /* Process Unlocked */
6159 __HAL_UNLOCK(hi2c);
6160 }
6161}
6162
6169static void I2C_Slave_STOPF(I2C_HandleTypeDef *hi2c)
6170{
6171 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
6172 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
6173
6174 /* Disable EVT, BUF and ERR interrupt */
6176
6177 /* Clear STOPF flag */
6179
6180 /* Disable Acknowledge */
6181 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
6182
6183 /* If a DMA is ongoing, Update handle size context */
6184 if ((hi2c->Instance->CR2 & I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
6185 {
6186 if ((CurrentState == HAL_I2C_STATE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN))
6187 {
6188 hi2c->XferCount = (uint16_t)(I2C_GET_DMA_REMAIN_DATA(hi2c->hdmarx));
6189
6190 if (hi2c->XferCount != 0U)
6191 {
6192 /* Set ErrorCode corresponding to a Non-Acknowledge */
6193 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6194 }
6195
6196 /* Disable, stop the current DMA */
6197 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
6198
6199 /* Abort DMA Xfer if any */
6201 {
6202 /* Set the I2C DMA Abort callback :
6203 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
6204 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
6205
6206 /* Abort DMA RX */
6207 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
6208 {
6209 /* Call Directly XferAbortCallback function in case of error */
6210 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
6211 }
6212 }
6213 }
6214 else
6215 {
6216 hi2c->XferCount = (uint16_t)(I2C_GET_DMA_REMAIN_DATA(hi2c->hdmatx));
6217
6218 if (hi2c->XferCount != 0U)
6219 {
6220 /* Set ErrorCode corresponding to a Non-Acknowledge */
6221 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6222 }
6223
6224 /* Disable, stop the current DMA */
6225 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
6226
6227 /* Abort DMA Xfer if any */
6229 {
6230 /* Set the I2C DMA Abort callback :
6231 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
6232 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
6233
6234 /* Abort DMA TX */
6235 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
6236 {
6237 /* Call Directly XferAbortCallback function in case of error */
6238 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
6239 }
6240 }
6241 }
6242 }
6243
6244 /* All data are not transferred, so set error code accordingly */
6245 if (hi2c->XferCount != 0U)
6246 {
6247 /* Store Last receive data if any */
6248 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == SET)
6249 {
6250 /* Read data from DR */
6251 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6252
6253 /* Increment Buffer pointer */
6254 hi2c->pBuffPtr++;
6255
6256 /* Update counter */
6257 hi2c->XferCount--;
6258 }
6259
6260 /* Store Last receive data if any */
6261 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
6262 {
6263 /* Read data from DR */
6264 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6265
6266 /* Increment Buffer pointer */
6267 hi2c->pBuffPtr++;
6268
6269 /* Update counter */
6270 hi2c->XferCount--;
6271 }
6272
6273 if (hi2c->XferCount != 0U)
6274 {
6275 /* Set ErrorCode corresponding to a Non-Acknowledge */
6276 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
6277 }
6278 }
6279
6280 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
6281 {
6282 /* Call the corresponding callback to inform upper layer of End of Transfer */
6283 I2C_ITError(hi2c);
6284 }
6285 else
6286 {
6287 if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
6288 {
6289 /* Set state at HAL_I2C_STATE_LISTEN */
6290 hi2c->PreviousState = I2C_STATE_NONE;
6292
6293 /* Call the corresponding callback to inform upper layer of End of Transfer */
6294#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6295 hi2c->SlaveRxCpltCallback(hi2c);
6296#else
6298#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6299 }
6300
6301 if (hi2c->State == HAL_I2C_STATE_LISTEN)
6302 {
6303 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6304 hi2c->PreviousState = I2C_STATE_NONE;
6305 hi2c->State = HAL_I2C_STATE_READY;
6306 hi2c->Mode = HAL_I2C_MODE_NONE;
6307
6308 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
6309#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6310 hi2c->ListenCpltCallback(hi2c);
6311#else
6313#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6314 }
6315 else
6316 {
6317 if ((hi2c->PreviousState == I2C_STATE_SLAVE_BUSY_RX) || (CurrentState == HAL_I2C_STATE_BUSY_RX))
6318 {
6319 hi2c->PreviousState = I2C_STATE_NONE;
6320 hi2c->State = HAL_I2C_STATE_READY;
6321 hi2c->Mode = HAL_I2C_MODE_NONE;
6322
6323#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6324 hi2c->SlaveRxCpltCallback(hi2c);
6325#else
6327#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6328 }
6329 }
6330 }
6331}
6332
6338static void I2C_Slave_AF(I2C_HandleTypeDef *hi2c)
6339{
6340 /* Declaration of temporary variables to prevent undefined behavior of volatile usage */
6341 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
6342 uint32_t CurrentXferOptions = hi2c->XferOptions;
6343
6344 if (((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME)) && \
6345 (CurrentState == HAL_I2C_STATE_LISTEN))
6346 {
6347 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6348
6349 /* Disable EVT, BUF and ERR interrupt */
6351
6352 /* Clear AF flag */
6354
6355 /* Disable Acknowledge */
6356 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
6357
6358 hi2c->PreviousState = I2C_STATE_NONE;
6359 hi2c->State = HAL_I2C_STATE_READY;
6360 hi2c->Mode = HAL_I2C_MODE_NONE;
6361
6362 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
6363#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6364 hi2c->ListenCpltCallback(hi2c);
6365#else
6367#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6368 }
6369 else if (CurrentState == HAL_I2C_STATE_BUSY_TX)
6370 {
6371 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6372 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
6373 hi2c->State = HAL_I2C_STATE_READY;
6374 hi2c->Mode = HAL_I2C_MODE_NONE;
6375
6376 /* Disable EVT, BUF and ERR interrupt */
6378
6379 /* Clear AF flag */
6381
6382 /* Disable Acknowledge */
6383 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
6384
6385 /* Clear TXE flag */
6386 I2C_Flush_DR(hi2c);
6387
6388#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6389 hi2c->SlaveTxCpltCallback(hi2c);
6390#else
6392#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6393 }
6394 else
6395 {
6396 /* Clear AF flag only */
6397 /* State Listen, but XferOptions == FIRST or NEXT */
6399 }
6400}
6401
6407static void I2C_ITError(I2C_HandleTypeDef *hi2c)
6408{
6409 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
6410 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
6411 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
6412 uint32_t CurrentError;
6413
6414 if (((CurrentMode == HAL_I2C_MODE_MASTER) || (CurrentMode == HAL_I2C_MODE_MEM)) && (CurrentState == HAL_I2C_STATE_BUSY_RX))
6415 {
6416 /* Disable Pos bit in I2C CR1 when error occurred in Master/Mem Receive IT Process */
6417 hi2c->Instance->CR1 &= ~I2C_CR1_POS;
6418 }
6419
6420 if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
6421 {
6422 /* keep HAL_I2C_STATE_LISTEN */
6423 hi2c->PreviousState = I2C_STATE_NONE;
6425 }
6426 else
6427 {
6428 /* If state is an abort treatment on going, don't change state */
6429 /* This change will be do later */
6430 if ((READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) != I2C_CR2_DMAEN) && (CurrentState != HAL_I2C_STATE_ABORT))
6431 {
6432 hi2c->State = HAL_I2C_STATE_READY;
6433 hi2c->Mode = HAL_I2C_MODE_NONE;
6434 }
6435 hi2c->PreviousState = I2C_STATE_NONE;
6436 }
6437
6438 /* Abort DMA transfer */
6439 if (READ_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN) == I2C_CR2_DMAEN)
6440 {
6441 hi2c->Instance->CR2 &= ~I2C_CR2_DMAEN;
6442
6443 if (hi2c->hdmatx->State != HAL_DMA_STATE_READY)
6444 {
6445 /* Set the DMA Abort callback :
6446 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
6447 hi2c->hdmatx->XferAbortCallback = I2C_DMAAbort;
6448
6449 if (HAL_DMA_Abort_IT(hi2c->hdmatx) != HAL_OK)
6450 {
6451 /* Disable I2C peripheral to prevent dummy data in buffer */
6452 __HAL_I2C_DISABLE(hi2c);
6453
6454 hi2c->State = HAL_I2C_STATE_READY;
6455
6456 /* Call Directly XferAbortCallback function in case of error */
6457 hi2c->hdmatx->XferAbortCallback(hi2c->hdmatx);
6458 }
6459 }
6460 else
6461 {
6462 /* Set the DMA Abort callback :
6463 will lead to call HAL_I2C_ErrorCallback() at end of DMA abort procedure */
6464 hi2c->hdmarx->XferAbortCallback = I2C_DMAAbort;
6465
6466 if (HAL_DMA_Abort_IT(hi2c->hdmarx) != HAL_OK)
6467 {
6468 /* Store Last receive data if any */
6469 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
6470 {
6471 /* Read data from DR */
6472 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6473
6474 /* Increment Buffer pointer */
6475 hi2c->pBuffPtr++;
6476 }
6477
6478 /* Disable I2C peripheral to prevent dummy data in buffer */
6479 __HAL_I2C_DISABLE(hi2c);
6480
6481 hi2c->State = HAL_I2C_STATE_READY;
6482
6483 /* Call Directly hi2c->hdmarx->XferAbortCallback function in case of error */
6484 hi2c->hdmarx->XferAbortCallback(hi2c->hdmarx);
6485 }
6486 }
6487 }
6488 else if (hi2c->State == HAL_I2C_STATE_ABORT)
6489 {
6490 hi2c->State = HAL_I2C_STATE_READY;
6492
6493 /* Store Last receive data if any */
6494 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
6495 {
6496 /* Read data from DR */
6497 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6498
6499 /* Increment Buffer pointer */
6500 hi2c->pBuffPtr++;
6501 }
6502
6503 /* Disable I2C peripheral to prevent dummy data in buffer */
6504 __HAL_I2C_DISABLE(hi2c);
6505
6506 /* Call the corresponding callback to inform upper layer of End of Transfer */
6507#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6508 hi2c->AbortCpltCallback(hi2c);
6509#else
6511#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6512 }
6513 else
6514 {
6515 /* Store Last receive data if any */
6516 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == SET)
6517 {
6518 /* Read data from DR */
6519 *hi2c->pBuffPtr = (uint8_t)hi2c->Instance->DR;
6520
6521 /* Increment Buffer pointer */
6522 hi2c->pBuffPtr++;
6523 }
6524
6525 /* Call user error callback */
6526#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6527 hi2c->ErrorCallback(hi2c);
6528#else
6530#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6531 }
6532
6533 /* STOP Flag is not set after a NACK reception, BusError, ArbitrationLost, OverRun */
6534 CurrentError = hi2c->ErrorCode;
6535
6536 if (((CurrentError & HAL_I2C_ERROR_BERR) == HAL_I2C_ERROR_BERR) || \
6537 ((CurrentError & HAL_I2C_ERROR_ARLO) == HAL_I2C_ERROR_ARLO) || \
6538 ((CurrentError & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) || \
6539 ((CurrentError & HAL_I2C_ERROR_OVR) == HAL_I2C_ERROR_OVR))
6540 {
6541 /* Disable EVT, BUF and ERR interrupt */
6543 }
6544
6545 /* So may inform upper layer that listen phase is stopped */
6546 /* during NACK error treatment */
6547 CurrentState = hi2c->State;
6548 if (((hi2c->ErrorCode & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) && (CurrentState == HAL_I2C_STATE_LISTEN))
6549 {
6550 hi2c->XferOptions = I2C_NO_OPTION_FRAME;
6551 hi2c->PreviousState = I2C_STATE_NONE;
6552 hi2c->State = HAL_I2C_STATE_READY;
6553 hi2c->Mode = HAL_I2C_MODE_NONE;
6554
6555 /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
6556#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6557 hi2c->ListenCpltCallback(hi2c);
6558#else
6560#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6561 }
6562}
6563
6573static HAL_StatusTypeDef I2C_MasterRequestWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
6574{
6575 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
6576 uint32_t CurrentXferOptions = hi2c->XferOptions;
6577
6578 /* Generate Start condition if first transfer */
6579 if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
6580 {
6581 /* Generate Start */
6582 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6583 }
6584 else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_RX)
6585 {
6586 /* Generate ReStart */
6587 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6588 }
6589 else
6590 {
6591 /* Do nothing */
6592 }
6593
6594 /* Wait until SB flag is set */
6595 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
6596 {
6597 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
6598 {
6600 }
6601 return HAL_TIMEOUT;
6602 }
6603
6605 {
6606 /* Send slave address */
6607 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
6608 }
6609 else
6610 {
6611 /* Send header of slave address */
6612 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
6613
6614 /* Wait until ADD10 flag is set */
6615 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
6616 {
6617 return HAL_ERROR;
6618 }
6619
6620 /* Send slave address */
6621 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
6622 }
6623
6624 /* Wait until ADDR flag is set */
6625 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
6626 {
6627 return HAL_ERROR;
6628 }
6629
6630 return HAL_OK;
6631}
6632
6643static HAL_StatusTypeDef I2C_MasterRequestRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Timeout, uint32_t Tickstart)
6644{
6645 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
6646 uint32_t CurrentXferOptions = hi2c->XferOptions;
6647
6648 /* Enable Acknowledge */
6649 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
6650
6651 /* Generate Start condition if first transfer */
6652 if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_FIRST_FRAME) || (CurrentXferOptions == I2C_NO_OPTION_FRAME))
6653 {
6654 /* Generate Start */
6655 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6656 }
6657 else if (hi2c->PreviousState == I2C_STATE_MASTER_BUSY_TX)
6658 {
6659 /* Generate ReStart */
6660 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6661 }
6662 else
6663 {
6664 /* Do nothing */
6665 }
6666
6667 /* Wait until SB flag is set */
6668 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
6669 {
6670 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
6671 {
6673 }
6674 return HAL_TIMEOUT;
6675 }
6676
6678 {
6679 /* Send slave address */
6680 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
6681 }
6682 else
6683 {
6684 /* Send header of slave address */
6685 hi2c->Instance->DR = I2C_10BIT_HEADER_WRITE(DevAddress);
6686
6687 /* Wait until ADD10 flag is set */
6688 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADD10, Timeout, Tickstart) != HAL_OK)
6689 {
6690 return HAL_ERROR;
6691 }
6692
6693 /* Send slave address */
6694 hi2c->Instance->DR = I2C_10BIT_ADDRESS(DevAddress);
6695
6696 /* Wait until ADDR flag is set */
6697 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
6698 {
6699 return HAL_ERROR;
6700 }
6701
6702 /* Clear ADDR flag */
6704
6705 /* Generate Restart */
6706 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6707
6708 /* Wait until SB flag is set */
6709 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
6710 {
6711 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
6712 {
6714 }
6715 return HAL_TIMEOUT;
6716 }
6717
6718 /* Send header of slave address */
6719 hi2c->Instance->DR = I2C_10BIT_HEADER_READ(DevAddress);
6720 }
6721
6722 /* Wait until ADDR flag is set */
6723 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
6724 {
6725 return HAL_ERROR;
6726 }
6727
6728 return HAL_OK;
6729}
6730
6743static HAL_StatusTypeDef I2C_RequestMemoryWrite(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
6744{
6745 /* Generate Start */
6746 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6747
6748 /* Wait until SB flag is set */
6749 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
6750 {
6751 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
6752 {
6754 }
6755 return HAL_TIMEOUT;
6756 }
6757
6758 /* Send slave address */
6759 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
6760
6761 /* Wait until ADDR flag is set */
6762 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
6763 {
6764 return HAL_ERROR;
6765 }
6766
6767 /* Clear ADDR flag */
6769
6770 /* Wait until TXE flag is set */
6771 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
6772 {
6773 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
6774 {
6775 /* Generate Stop */
6776 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
6777 }
6778 return HAL_ERROR;
6779 }
6780
6781 /* If Memory address size is 8Bit */
6782 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
6783 {
6784 /* Send Memory Address */
6785 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
6786 }
6787 /* If Memory address size is 16Bit */
6788 else
6789 {
6790 /* Send MSB of Memory Address */
6791 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
6792
6793 /* Wait until TXE flag is set */
6794 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
6795 {
6796 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
6797 {
6798 /* Generate Stop */
6799 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
6800 }
6801 return HAL_ERROR;
6802 }
6803
6804 /* Send LSB of Memory Address */
6805 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
6806 }
6807
6808 return HAL_OK;
6809}
6810
6823static HAL_StatusTypeDef I2C_RequestMemoryRead(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint32_t Timeout, uint32_t Tickstart)
6824{
6825 /* Enable Acknowledge */
6826 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
6827
6828 /* Generate Start */
6829 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6830
6831 /* Wait until SB flag is set */
6832 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
6833 {
6834 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
6835 {
6837 }
6838 return HAL_TIMEOUT;
6839 }
6840
6841 /* Send slave address */
6842 hi2c->Instance->DR = I2C_7BIT_ADD_WRITE(DevAddress);
6843
6844 /* Wait until ADDR flag is set */
6845 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
6846 {
6847 return HAL_ERROR;
6848 }
6849
6850 /* Clear ADDR flag */
6852
6853 /* Wait until TXE flag is set */
6854 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
6855 {
6856 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
6857 {
6858 /* Generate Stop */
6859 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
6860 }
6861 return HAL_ERROR;
6862 }
6863
6864 /* If Memory address size is 8Bit */
6865 if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
6866 {
6867 /* Send Memory Address */
6868 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
6869 }
6870 /* If Memory address size is 16Bit */
6871 else
6872 {
6873 /* Send MSB of Memory Address */
6874 hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
6875
6876 /* Wait until TXE flag is set */
6877 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
6878 {
6879 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
6880 {
6881 /* Generate Stop */
6882 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
6883 }
6884 return HAL_ERROR;
6885 }
6886
6887 /* Send LSB of Memory Address */
6888 hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
6889 }
6890
6891 /* Wait until TXE flag is set */
6892 if (I2C_WaitOnTXEFlagUntilTimeout(hi2c, Timeout, Tickstart) != HAL_OK)
6893 {
6894 if (hi2c->ErrorCode == HAL_I2C_ERROR_AF)
6895 {
6896 /* Generate Stop */
6897 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
6898 }
6899 return HAL_ERROR;
6900 }
6901
6902 /* Generate Restart */
6903 SET_BIT(hi2c->Instance->CR1, I2C_CR1_START);
6904
6905 /* Wait until SB flag is set */
6906 if (I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout, Tickstart) != HAL_OK)
6907 {
6908 if (READ_BIT(hi2c->Instance->CR1, I2C_CR1_START) == I2C_CR1_START)
6909 {
6911 }
6912 return HAL_TIMEOUT;
6913 }
6914
6915 /* Send slave address */
6916 hi2c->Instance->DR = I2C_7BIT_ADD_READ(DevAddress);
6917
6918 /* Wait until ADDR flag is set */
6919 if (I2C_WaitOnMasterAddressFlagUntilTimeout(hi2c, I2C_FLAG_ADDR, Timeout, Tickstart) != HAL_OK)
6920 {
6921 return HAL_ERROR;
6922 }
6923
6924 return HAL_OK;
6925}
6926
6932static void I2C_DMAXferCplt(DMA_HandleTypeDef *hdma)
6933{
6934 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
6935
6936 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
6937 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
6938 HAL_I2C_ModeTypeDef CurrentMode = hi2c->Mode;
6939 uint32_t CurrentXferOptions = hi2c->XferOptions;
6940
6941 /* Disable EVT and ERR interrupt */
6943
6944 /* Clear Complete callback */
6945 if (hi2c->hdmatx != NULL)
6946 {
6947 hi2c->hdmatx->XferCpltCallback = NULL;
6948 }
6949 if (hi2c->hdmarx != NULL)
6950 {
6951 hi2c->hdmarx->XferCpltCallback = NULL;
6952 }
6953
6954 if ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_TX) == (uint32_t)HAL_I2C_STATE_BUSY_TX) || ((((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_BUSY_RX) == (uint32_t)HAL_I2C_STATE_BUSY_RX) && (CurrentMode == HAL_I2C_MODE_SLAVE)))
6955 {
6956 /* Disable DMA Request */
6957 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
6958
6959 hi2c->XferCount = 0U;
6960
6961 if (CurrentState == HAL_I2C_STATE_BUSY_TX_LISTEN)
6962 {
6963 /* Set state at HAL_I2C_STATE_LISTEN */
6964 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_TX;
6966
6967 /* Call the corresponding callback to inform upper layer of End of Transfer */
6968#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6969 hi2c->SlaveTxCpltCallback(hi2c);
6970#else
6972#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6973 }
6974 else if (CurrentState == HAL_I2C_STATE_BUSY_RX_LISTEN)
6975 {
6976 /* Set state at HAL_I2C_STATE_LISTEN */
6977 hi2c->PreviousState = I2C_STATE_SLAVE_BUSY_RX;
6979
6980 /* Call the corresponding callback to inform upper layer of End of Transfer */
6981#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
6982 hi2c->SlaveRxCpltCallback(hi2c);
6983#else
6985#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
6986 }
6987 else
6988 {
6989 /* Do nothing */
6990 }
6991
6992 /* Enable EVT and ERR interrupt to treat end of transfer in IRQ handler */
6994 }
6995 /* Check current Mode, in case of treatment DMA handler have been preempted by a prior interrupt */
6996 else if (hi2c->Mode != HAL_I2C_MODE_NONE)
6997 {
6998 if (hi2c->XferCount == (uint16_t)1)
6999 {
7000 /* Disable Acknowledge */
7001 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
7002 }
7003
7004 /* Disable EVT and ERR interrupt */
7006
7007 /* Prepare next transfer or stop current transfer */
7008 if ((CurrentXferOptions == I2C_NO_OPTION_FRAME) || (CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_OTHER_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
7009 {
7010 /* Generate Stop */
7011 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
7012 }
7013
7014 /* Disable Last DMA */
7015 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_LAST);
7016
7017 /* Disable DMA Request */
7018 CLEAR_BIT(hi2c->Instance->CR2, I2C_CR2_DMAEN);
7019
7020 hi2c->XferCount = 0U;
7021
7022 /* Check if Errors has been detected during transfer */
7023 if (hi2c->ErrorCode != HAL_I2C_ERROR_NONE)
7024 {
7025#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
7026 hi2c->ErrorCallback(hi2c);
7027#else
7029#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
7030 }
7031 else
7032 {
7033 hi2c->State = HAL_I2C_STATE_READY;
7034
7035 if (hi2c->Mode == HAL_I2C_MODE_MEM)
7036 {
7037 hi2c->Mode = HAL_I2C_MODE_NONE;
7038 hi2c->PreviousState = I2C_STATE_NONE;
7039
7040#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
7041 hi2c->MemRxCpltCallback(hi2c);
7042#else
7044#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
7045 }
7046 else
7047 {
7048 hi2c->Mode = HAL_I2C_MODE_NONE;
7049 if ((CurrentXferOptions == I2C_FIRST_AND_LAST_FRAME) || (CurrentXferOptions == I2C_LAST_FRAME))
7050 {
7051 hi2c->PreviousState = I2C_STATE_NONE;
7052 }
7053 else
7054 {
7055 hi2c->PreviousState = I2C_STATE_MASTER_BUSY_RX;
7056 }
7057
7058#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
7059 hi2c->MasterRxCpltCallback(hi2c);
7060#else
7062#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
7063 }
7064 }
7065 }
7066 else
7067 {
7068 /* Do nothing */
7069 }
7070}
7071
7077static void I2C_DMAError(DMA_HandleTypeDef *hdma)
7078{
7079 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
7080
7081 /* Clear Complete callback */
7082 if (hi2c->hdmatx != NULL)
7083 {
7084 hi2c->hdmatx->XferCpltCallback = NULL;
7085 }
7086 if (hi2c->hdmarx != NULL)
7087 {
7088 hi2c->hdmarx->XferCpltCallback = NULL;
7089 }
7090
7091 /* Ignore DMA FIFO error */
7093 {
7094 /* Disable Acknowledge */
7095 hi2c->Instance->CR1 &= ~I2C_CR1_ACK;
7096
7097 hi2c->XferCount = 0U;
7098
7099 hi2c->State = HAL_I2C_STATE_READY;
7100 hi2c->Mode = HAL_I2C_MODE_NONE;
7101
7103
7104#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
7105 hi2c->ErrorCallback(hi2c);
7106#else
7108#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
7109 }
7110}
7111
7118static void I2C_DMAAbort(DMA_HandleTypeDef *hdma)
7119{
7120 __IO uint32_t count = 0U;
7121 I2C_HandleTypeDef *hi2c = (I2C_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-11.5 */
7122
7123 /* Declaration of temporary variable to prevent undefined behavior of volatile usage */
7124 HAL_I2C_StateTypeDef CurrentState = hi2c->State;
7125
7126 /* During abort treatment, check that there is no pending STOP request */
7127 /* Wait until STOP flag is reset */
7128 count = I2C_TIMEOUT_FLAG * (SystemCoreClock / 25U / 1000U);
7129 do
7130 {
7131 if (count == 0U)
7132 {
7134 break;
7135 }
7136 count--;
7137 }
7138 while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
7139
7140 /* Clear Complete callback */
7141 if (hi2c->hdmatx != NULL)
7142 {
7143 hi2c->hdmatx->XferCpltCallback = NULL;
7144 }
7145 if (hi2c->hdmarx != NULL)
7146 {
7147 hi2c->hdmarx->XferCpltCallback = NULL;
7148 }
7149
7150 /* Disable Acknowledge */
7151 CLEAR_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
7152
7153 hi2c->XferCount = 0U;
7154
7155 /* Reset XferAbortCallback */
7156 if (hi2c->hdmatx != NULL)
7157 {
7158 hi2c->hdmatx->XferAbortCallback = NULL;
7159 }
7160 if (hi2c->hdmarx != NULL)
7161 {
7162 hi2c->hdmarx->XferAbortCallback = NULL;
7163 }
7164
7165 /* Disable I2C peripheral to prevent dummy data in buffer */
7166 __HAL_I2C_DISABLE(hi2c);
7167
7168 /* Check if come from abort from user */
7169 if (hi2c->State == HAL_I2C_STATE_ABORT)
7170 {
7171 hi2c->State = HAL_I2C_STATE_READY;
7172 hi2c->Mode = HAL_I2C_MODE_NONE;
7174
7175 /* Call the corresponding callback to inform upper layer of End of Transfer */
7176#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
7177 hi2c->AbortCpltCallback(hi2c);
7178#else
7180#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
7181 }
7182 else
7183 {
7184 if (((uint32_t)CurrentState & (uint32_t)HAL_I2C_STATE_LISTEN) == (uint32_t)HAL_I2C_STATE_LISTEN)
7185 {
7186 /* Renable I2C peripheral */
7187 __HAL_I2C_ENABLE(hi2c);
7188
7189 /* Enable Acknowledge */
7190 SET_BIT(hi2c->Instance->CR1, I2C_CR1_ACK);
7191
7192 /* keep HAL_I2C_STATE_LISTEN */
7193 hi2c->PreviousState = I2C_STATE_NONE;
7195 }
7196 else
7197 {
7198 hi2c->State = HAL_I2C_STATE_READY;
7199 hi2c->Mode = HAL_I2C_MODE_NONE;
7200 }
7201
7202 /* Call the corresponding callback to inform upper layer of End of Transfer */
7203#if (USE_HAL_I2C_REGISTER_CALLBACKS == 1)
7204 hi2c->ErrorCallback(hi2c);
7205#else
7207#endif /* USE_HAL_I2C_REGISTER_CALLBACKS */
7208 }
7209}
7210
7221static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout, uint32_t Tickstart)
7222{
7223 /* Wait until flag is set */
7224 while (__HAL_I2C_GET_FLAG(hi2c, Flag) == Status)
7225 {
7226 /* Check for the Timeout */
7227 if (Timeout != HAL_MAX_DELAY)
7228 {
7229 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7230 {
7231 if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == Status))
7232 {
7233 hi2c->PreviousState = I2C_STATE_NONE;
7234 hi2c->State = HAL_I2C_STATE_READY;
7235 hi2c->Mode = HAL_I2C_MODE_NONE;
7237
7238 /* Process Unlocked */
7239 __HAL_UNLOCK(hi2c);
7240
7241 return HAL_ERROR;
7242 }
7243 }
7244 }
7245 }
7246 return HAL_OK;
7247}
7248
7258static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
7259{
7260 while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
7261 {
7262 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
7263 {
7264 /* Generate Stop */
7265 SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);
7266
7267 /* Clear AF Flag */
7269
7270 hi2c->PreviousState = I2C_STATE_NONE;
7271 hi2c->State = HAL_I2C_STATE_READY;
7272 hi2c->Mode = HAL_I2C_MODE_NONE;
7273 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
7274
7275 /* Process Unlocked */
7276 __HAL_UNLOCK(hi2c);
7277
7278 return HAL_ERROR;
7279 }
7280
7281 /* Check for the Timeout */
7282 if (Timeout != HAL_MAX_DELAY)
7283 {
7284 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7285 {
7286 if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET))
7287 {
7288 hi2c->PreviousState = I2C_STATE_NONE;
7289 hi2c->State = HAL_I2C_STATE_READY;
7290 hi2c->Mode = HAL_I2C_MODE_NONE;
7292
7293 /* Process Unlocked */
7294 __HAL_UNLOCK(hi2c);
7295
7296 return HAL_ERROR;
7297 }
7298 }
7299 }
7300 }
7301 return HAL_OK;
7302}
7303
7312static HAL_StatusTypeDef I2C_WaitOnTXEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
7313{
7314 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET)
7315 {
7316 /* Check if a NACK is detected */
7317 if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
7318 {
7319 return HAL_ERROR;
7320 }
7321
7322 /* Check for the Timeout */
7323 if (Timeout != HAL_MAX_DELAY)
7324 {
7325 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7326 {
7327 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_TXE) == RESET))
7328 {
7329 hi2c->PreviousState = I2C_STATE_NONE;
7330 hi2c->State = HAL_I2C_STATE_READY;
7331 hi2c->Mode = HAL_I2C_MODE_NONE;
7333
7334 /* Process Unlocked */
7335 __HAL_UNLOCK(hi2c);
7336
7337 return HAL_ERROR;
7338 }
7339 }
7340 }
7341 }
7342 return HAL_OK;
7343}
7344
7353static HAL_StatusTypeDef I2C_WaitOnBTFFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
7354{
7355 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET)
7356 {
7357 /* Check if a NACK is detected */
7358 if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
7359 {
7360 return HAL_ERROR;
7361 }
7362
7363 /* Check for the Timeout */
7364 if (Timeout != HAL_MAX_DELAY)
7365 {
7366 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7367 {
7368 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BTF) == RESET))
7369 {
7370 hi2c->PreviousState = I2C_STATE_NONE;
7371 hi2c->State = HAL_I2C_STATE_READY;
7372 hi2c->Mode = HAL_I2C_MODE_NONE;
7374
7375 /* Process Unlocked */
7376 __HAL_UNLOCK(hi2c);
7377
7378 return HAL_ERROR;
7379 }
7380 }
7381 }
7382 }
7383 return HAL_OK;
7384}
7385
7394static HAL_StatusTypeDef I2C_WaitOnSTOPFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
7395{
7396 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET)
7397 {
7398 /* Check if a NACK is detected */
7399 if (I2C_IsAcknowledgeFailed(hi2c) != HAL_OK)
7400 {
7401 return HAL_ERROR;
7402 }
7403
7404 /* Check for the Timeout */
7405 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7406 {
7407 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == RESET))
7408 {
7409 hi2c->PreviousState = I2C_STATE_NONE;
7410 hi2c->State = HAL_I2C_STATE_READY;
7411 hi2c->Mode = HAL_I2C_MODE_NONE;
7413
7414 /* Process Unlocked */
7415 __HAL_UNLOCK(hi2c);
7416
7417 return HAL_ERROR;
7418 }
7419 }
7420 }
7421 return HAL_OK;
7422}
7423
7430static HAL_StatusTypeDef I2C_WaitOnSTOPRequestThroughIT(I2C_HandleTypeDef *hi2c)
7431{
7432 __IO uint32_t count = 0U;
7433
7434 /* Wait until STOP flag is reset */
7435 count = I2C_TIMEOUT_STOP_FLAG * (SystemCoreClock / 25U / 1000U);
7436 do
7437 {
7438 count--;
7439 if (count == 0U)
7440 {
7442
7443 return HAL_ERROR;
7444 }
7445 }
7446 while (READ_BIT(hi2c->Instance->CR1, I2C_CR1_STOP) == I2C_CR1_STOP);
7447
7448 return HAL_OK;
7449}
7450
7459static HAL_StatusTypeDef I2C_WaitOnRXNEFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Timeout, uint32_t Tickstart)
7460{
7461
7462 while (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET)
7463 {
7464 /* Check if a STOPF is detected */
7465 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF) == SET)
7466 {
7467 /* Clear STOP Flag */
7469
7470 hi2c->PreviousState = I2C_STATE_NONE;
7471 hi2c->State = HAL_I2C_STATE_READY;
7472 hi2c->Mode = HAL_I2C_MODE_NONE;
7474
7475 /* Process Unlocked */
7476 __HAL_UNLOCK(hi2c);
7477
7478 return HAL_ERROR;
7479 }
7480
7481 /* Check for the Timeout */
7482 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
7483 {
7484 if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_RXNE) == RESET))
7485 {
7486 hi2c->PreviousState = I2C_STATE_NONE;
7487 hi2c->State = HAL_I2C_STATE_READY;
7488 hi2c->Mode = HAL_I2C_MODE_NONE;
7490
7491 /* Process Unlocked */
7492 __HAL_UNLOCK(hi2c);
7493
7494 return HAL_ERROR;
7495 }
7496 }
7497 }
7498 return HAL_OK;
7499}
7500
7507static HAL_StatusTypeDef I2C_IsAcknowledgeFailed(I2C_HandleTypeDef *hi2c)
7508{
7509 if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
7510 {
7511 /* Clear NACKF Flag */
7513
7514 hi2c->PreviousState = I2C_STATE_NONE;
7515 hi2c->State = HAL_I2C_STATE_READY;
7516 hi2c->Mode = HAL_I2C_MODE_NONE;
7517 hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
7518
7519 /* Process Unlocked */
7520 __HAL_UNLOCK(hi2c);
7521
7522 return HAL_ERROR;
7523 }
7524 return HAL_OK;
7525}
7526
7532static void I2C_ConvertOtherXferOptions(I2C_HandleTypeDef *hi2c)
7533{
7534 /* if user set XferOptions to I2C_OTHER_FRAME */
7535 /* it request implicitly to generate a restart condition */
7536 /* set XferOptions to I2C_FIRST_FRAME */
7537 if (hi2c->XferOptions == I2C_OTHER_FRAME)
7538 {
7540 }
7541 /* else if user set XferOptions to I2C_OTHER_AND_LAST_FRAME */
7542 /* it request implicitly to generate a restart condition */
7543 /* then generate a stop condition at the end of transfer */
7544 /* set XferOptions to I2C_FIRST_AND_LAST_FRAME */
7545 else if (hi2c->XferOptions == I2C_OTHER_AND_LAST_FRAME)
7546 {
7548 }
7549 else
7550 {
7551 /* Nothing to do */
7552 }
7553}
7554
7558
7559#endif /* HAL_I2C_MODULE_ENABLED */
7563
7567
#define HAL_DMA_ERROR_FE
HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
struct __DMA_HandleTypeDef DMA_HandleTypeDef
DMA handle Structure definition.
@ HAL_DMA_STATE_READY
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
HAL_I2C_ModeTypeDef
@ HAL_I2C_MODE_MASTER
@ HAL_I2C_MODE_MEM
@ HAL_I2C_MODE_SLAVE
@ HAL_I2C_MODE_NONE
HAL_I2C_StateTypeDef
@ HAL_I2C_STATE_BUSY
@ HAL_I2C_STATE_LISTEN
@ HAL_I2C_STATE_BUSY_TX_LISTEN
@ HAL_I2C_STATE_ABORT
@ HAL_I2C_STATE_TIMEOUT
@ HAL_I2C_STATE_BUSY_RX
@ HAL_I2C_STATE_BUSY_RX_LISTEN
@ HAL_I2C_STATE_RESET
@ HAL_I2C_STATE_BUSY_TX
@ HAL_I2C_STATE_READY
#define HAL_I2C_ERROR_ARLO
#define HAL_I2C_ERROR_NONE
#define HAL_I2C_WRONG_START
#define HAL_I2C_ERROR_OVR
#define HAL_I2C_ERROR_SIZE
#define HAL_I2C_ERROR_BERR
#define HAL_I2C_ERROR_DMA_PARAM
#define HAL_I2C_ERROR_AF
#define HAL_I2C_ERROR_DMA
#define HAL_I2C_ERROR_TIMEOUT
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress)
HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c)
HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size)
HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
HAL_StatusTypeDef HAL_I2C_Slave_Seq_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c)
HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c)
uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
#define __HAL_I2C_CLEAR_ADDRFLAG(__HANDLE__)
Clears the I2C ADDR pending flag.
#define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__)
#define __HAL_I2C_DISABLE(__HANDLE__)
Disable the specified I2C peripheral.
#define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clears the I2C pending flags which are cleared by writing 0 in a specific bit.
#define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable or disable the specified I2C interrupts.
#define __HAL_I2C_ENABLE(__HANDLE__)
Enable the specified I2C peripheral.
#define __HAL_I2C_CLEAR_STOPFLAG(__HANDLE__)
Clears the I2C STOPF pending flag.
#define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__)
Checks whether the specified I2C flag is set or not.
#define I2C_FLAG_TRA
#define I2C_FLAG_BERR
#define I2C_FLAG_AF
#define I2C_FLAG_ADD10
#define I2C_FLAG_DUALF
#define I2C_FLAG_BTF
#define I2C_FLAG_BUSY
#define I2C_FLAG_ADDR
#define I2C_FLAG_OVR
#define I2C_FLAG_STOPF
#define I2C_FLAG_RXNE
#define I2C_FLAG_SB
#define I2C_FLAG_ARLO
#define I2C_FLAG_TXE
void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c)
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c)
void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c)
#define IS_I2C_TRANSFER_OTHER_OPTIONS_REQUEST(REQUEST)
#define I2C_CHECK_FLAG(__ISR__, __FLAG__)
#define IS_I2C_GENERAL_CALL(CALL)
#define IS_I2C_OWN_ADDRESS2(ADDRESS2)
#define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST)
#define IS_I2C_ADDRESSING_MODE(ADDRESS)
#define I2C_CHECK_IT_SOURCE(__CR1__, __IT__)
#define IS_I2C_MEMADD_SIZE(SIZE)
#define IS_I2C_OWN_ADDRESS1(ADDRESS1)
#define IS_I2C_CLOCK_SPEED(SPEED)
#define IS_I2C_DUAL_ADDRESS(ADDRESS)
#define IS_I2C_DUTY_CYCLE(CYCLE)
#define IS_I2C_NO_STRETCH(STRETCH)
#define I2C_IT_BUF
#define I2C_IT_ERR
#define I2C_IT_EVT
#define I2C_MEMADD_SIZE_8BIT
#define I2C_SPEED(__PCLK__, __SPEED__, __DUTYCYCLE__)
#define I2C_10BIT_HEADER_WRITE(__ADDRESS__)
#define I2C_RISE_TIME(__FREQRANGE__, __SPEED__)
#define I2C_MEM_ADD_MSB(__ADDRESS__)
#define I2C_MIN_PCLK_FREQ(__PCLK__, __SPEED__)
#define I2C_10BIT_ADDRESS(__ADDRESS__)
#define I2C_10BIT_HEADER_READ(__ADDRESS__)
#define I2C_FREQRANGE(__PCLK__)
#define I2C_MEM_ADD_LSB(__ADDRESS__)
#define I2C_7BIT_ADD_READ(__ADDRESS__)
#define I2C_7BIT_ADD_WRITE(__ADDRESS__)
#define I2C_DIRECTION_TRANSMIT
#define I2C_DIRECTION_RECEIVE
#define I2C_FIRST_AND_NEXT_FRAME
#define I2C_OTHER_AND_LAST_FRAME
#define I2C_FIRST_AND_LAST_FRAME
#define I2C_NEXT_FRAME
#define I2C_FIRST_FRAME
#define I2C_OTHER_FRAME
#define I2C_LAST_FRAME_NO_STOP
#define I2C_LAST_FRAME
#define I2C_ADDRESSINGMODE_10BIT
#define I2C_ADDRESSINGMODE_7BIT
uint32_t HAL_RCC_GetPCLK1Freq(void)
#define assert_param(expr)
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_TIMEOUT
@ HAL_ERROR
@ HAL_OK
@ HAL_BUSY
#define __HAL_UNLOCK(__HANDLE__)
#define HAL_MAX_DELAY
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)
volatile uint32_t EventCount
volatile uint16_t XferCount
volatile uint32_t Devaddress
volatile HAL_I2C_StateTypeDef State
I2C_InitTypeDef Init
volatile HAL_I2C_ModeTypeDef Mode
volatile uint32_t MemaddSize
volatile uint32_t XferOptions
DMA_HandleTypeDef * hdmarx
DMA_HandleTypeDef * hdmatx
volatile uint32_t Memaddress
HAL_LockTypeDef Lock
volatile uint32_t ErrorCode
volatile uint32_t PreviousState
void(* XferAbortCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferCpltCallback)(struct __DMA_HandleTypeDef *hdma)
volatile HAL_DMA_StateTypeDef State
void(* XferErrorCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferHalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferM1HalfCpltCallback)(struct __DMA_HandleTypeDef *hdma)
void(* XferM1CpltCallback)(struct __DMA_HandleTypeDef *hdma)