1 /**
2   ******************************************************************************
3   * @file    stm32h7rsxx_ll_i3c.c
4   * @author  MCD Application Team
5   * @brief   I3C LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2022 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32h7rsxx_ll_i3c.h"
22 #include "stm32h7rsxx_ll_bus.h"
23 #ifdef  USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif /* USE_FULL_ASSERT */
28 
29 /** @addtogroup STM32H7RSxx_LL_Driver
30   * @{
31   */
32 
33 #if defined (I3C1)
34 
35 /** @defgroup I3C_LL I3C
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 /** @addtogroup I3C_LL_Private_Macros
44   * @{
45   */
46 #define IS_LL_I3C_SDAHOLDTIME_VALUE(VALUE) (((VALUE) == LL_I3C_SDA_HOLD_TIME_0_5) || \
47                                             ((VALUE) == LL_I3C_SDA_HOLD_TIME_1_5))
48 
49 #define IS_LL_I3C_WAITTIME_VALUE(VALUE) (((VALUE) == LL_I3C_OWN_ACTIVITY_STATE_0) || \
50                                          ((VALUE) == LL_I3C_OWN_ACTIVITY_STATE_1) || \
51                                          ((VALUE) == LL_I3C_OWN_ACTIVITY_STATE_2) || \
52                                          ((VALUE) == LL_I3C_OWN_ACTIVITY_STATE_3))
53 /**
54   * @}
55   */
56 
57 /* Private function prototypes -----------------------------------------------*/
58 
59 /* Exported functions --------------------------------------------------------*/
60 /** @addtogroup I3C_LL_Exported_Functions
61   * @{
62   */
63 
64 /** @addtogroup I3C_LL_EF_Init
65   * @{
66   */
67 
68 /**
69   * @brief  De-initialize the I3C registers to their default reset values.
70   * @param  I3Cx I3C Instance.
71   * @retval An ErrorStatus enumeration value:
72   *          - SUCCESS: I3C registers are de-initialized
73   *          - ERROR: I3C registers are not de-initialized
74   */
LL_I3C_DeInit(const I3C_TypeDef * I3Cx)75 ErrorStatus LL_I3C_DeInit(const I3C_TypeDef *I3Cx)
76 {
77   ErrorStatus status = SUCCESS;
78 
79   /* Check the I3C Instance I3Cx */
80   assert_param(IS_I3C_ALL_INSTANCE(I3Cx));
81 
82   /* Prevent unused argument(s) compilation warning */
83   UNUSED(I3Cx);
84 
85   /* Force reset of I3C clock */
86   LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I3C1);
87 
88   /* Release reset of I3C clock */
89   LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I3C1);
90 
91   return status;
92 }
93 
94 /**
95   * @brief  Initialize the I3C registers according to the specified parameters in I3C_InitStruct.
96   * @param  I3Cx I3C Instance.
97   * @param  I3C_InitStruct pointer to a @ref LL_I3C_InitTypeDef structure.
98   * @param  Mode I3C peripheral mode.
99   *              This parameter can be a value of @ref I3C_LL_EC_MODE.
100   * @retval An ErrorStatus enumeration value:
101   *          - SUCCESS: I3C registers are initialized
102   *          - ERROR: Not applicable
103   */
LL_I3C_Init(I3C_TypeDef * I3Cx,LL_I3C_InitTypeDef * I3C_InitStruct,uint32_t Mode)104 ErrorStatus LL_I3C_Init(I3C_TypeDef *I3Cx, LL_I3C_InitTypeDef *I3C_InitStruct, uint32_t Mode)
105 {
106   uint32_t waveform_value;
107   uint32_t timing_value;
108 
109   /* Check the I3C Instance I3Cx */
110   assert_param(IS_I3C_ALL_INSTANCE(I3Cx));
111 
112   /* Disable the selected I3C peripheral */
113   LL_I3C_Disable(I3Cx);
114 
115   /* Check on the I3C mode: initialization depends on the mode */
116   if (Mode == LL_I3C_MODE_CONTROLLER)
117   {
118     /* Check the parameters */
119     assert_param(IS_LL_I3C_SDAHOLDTIME_VALUE(I3C_InitStruct->CtrlBusCharacteristic.SDAHoldTime));
120     assert_param(IS_LL_I3C_WAITTIME_VALUE(I3C_InitStruct->CtrlBusCharacteristic.WaitTime));
121 
122     /* Set Controller mode */
123     LL_I3C_SetMode(I3Cx, LL_I3C_MODE_CONTROLLER);
124 
125     /*------------------ SCL signal waveform configuration : I3C timing register 0 (I3C_TIMINGR0) ------------------- */
126     /* Set the controller SCL waveform */
127     waveform_value =
128       ((uint32_t)(I3C_InitStruct->CtrlBusCharacteristic.SCLPPLowDuration)                                |
129        ((uint32_t)I3C_InitStruct->CtrlBusCharacteristic.SCLI3CHighDuration << I3C_TIMINGR0_SCLH_I3C_Pos) |
130        ((uint32_t)I3C_InitStruct->CtrlBusCharacteristic.SCLODLowDuration << I3C_TIMINGR0_SCLL_OD_Pos)    |
131        ((uint32_t)I3C_InitStruct->CtrlBusCharacteristic.SCLI2CHighDuration << I3C_TIMINGR0_SCLH_I2C_Pos));
132 
133     LL_I3C_ConfigClockWaveForm(I3Cx, waveform_value);
134 
135     /*------------------- Timing configuration : I3C timing register 1 (I3C_TIMINGR1) ------------------------------- */
136     /* Set SDA hold time, activity state, bus free duration and bus available duration */
137     timing_value = ((uint32_t)(I3C_InitStruct->CtrlBusCharacteristic.SDAHoldTime)                               |
138                     (uint32_t)(I3C_InitStruct->CtrlBusCharacteristic.WaitTime)                                  |
139                     ((uint32_t)I3C_InitStruct->CtrlBusCharacteristic.BusFreeDuration <<  I3C_TIMINGR1_FREE_Pos) |
140                     (uint32_t)(I3C_InitStruct->CtrlBusCharacteristic.BusIdleDuration));
141 
142     LL_I3C_SetCtrlBusCharacteristic(I3Cx, timing_value);
143   }
144   else
145   {
146     /* Set target mode */
147     LL_I3C_SetMode(I3Cx, LL_I3C_MODE_TARGET);
148 
149     /*------------------- Timing configuration : I3C timing register 1 (I3C_TIMINGR1) ------------------------------- */
150     /* Set the number of kernel clocks cycles for the bus available condition time */
151     LL_I3C_SetTgtBusCharacteristic(I3Cx, I3C_InitStruct->TgtBusCharacteristic.BusAvailableDuration);
152   }
153 
154   /* Enable the selected I3C peripheral */
155   LL_I3C_Enable(I3Cx);
156 
157   return SUCCESS;
158 }
159 
160 /**
161   * @brief  Set each @ref LL_I3C_InitTypeDef field to default value.
162   * @param  I3C_InitStruct Pointer to a @ref LL_I3C_InitTypeDef structure.
163   * @retval None
164   */
LL_I3C_StructInit(LL_I3C_InitTypeDef * I3C_InitStruct)165 void LL_I3C_StructInit(LL_I3C_InitTypeDef *I3C_InitStruct)
166 {
167   /* Set I3C_InitStruct fields to default values */
168   I3C_InitStruct->CtrlBusCharacteristic.SDAHoldTime         = LL_I3C_SDA_HOLD_TIME_0_5;
169   I3C_InitStruct->CtrlBusCharacteristic.WaitTime            = LL_I3C_OWN_ACTIVITY_STATE_0;
170   I3C_InitStruct->CtrlBusCharacteristic.SCLPPLowDuration    = 0U;
171   I3C_InitStruct->CtrlBusCharacteristic.SCLI3CHighDuration  = 0U;
172   I3C_InitStruct->CtrlBusCharacteristic.SCLODLowDuration    = 0U;
173   I3C_InitStruct->CtrlBusCharacteristic.SCLI2CHighDuration  = 0U;
174   I3C_InitStruct->CtrlBusCharacteristic.BusFreeDuration     = 0U;
175   I3C_InitStruct->CtrlBusCharacteristic.BusIdleDuration     = 0U;
176   I3C_InitStruct->TgtBusCharacteristic.BusAvailableDuration = 0U;
177 }
178 
179 /**
180   * @}
181   */
182 
183 /**
184   * @}
185   */
186 
187 /**
188   * @}
189   */
190 
191 #endif /* I3C1 */
192 
193 /**
194   * @}
195   */
196 
197 #endif /* USE_FULL_LL_DRIVER */
198