1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_ll_swpmi.c
4   * @author  MCD Application Team
5   * @brief   SWPMI LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2017 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 "stm32h7xx_ll_swpmi.h"
22 #include "stm32h7xx_ll_bus.h"
23 #ifdef  USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif
28 
29 /** @addtogroup STM32H7xx_LL_Driver
30   * @{
31   */
32 
33 
34 /** @addtogroup SWPMI_LL
35   * @{
36   */
37 
38 /* Private types -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 /* Private constants ---------------------------------------------------------*/
41 /* Private macros ------------------------------------------------------------*/
42 /** @addtogroup SWPMI_LL_Private_Macros
43   * @{
44   */
45 
46 #define IS_LL_SWPMI_BITRATE_VALUE(__VALUE__) (((__VALUE__) <= 255U))
47 
48 #define IS_LL_SWPMI_SW_BUFFER_RX(__VALUE__) (((__VALUE__) == LL_SWPMI_SW_BUFFER_RX_SINGLE) \
49                                           || ((__VALUE__) == LL_SWPMI_SW_BUFFER_RX_MULTI))
50 
51 #define IS_LL_SWPMI_SW_BUFFER_TX(__VALUE__) (((__VALUE__) == LL_SWPMI_SW_BUFFER_TX_SINGLE) \
52                                           || ((__VALUE__) == LL_SWPMI_SW_BUFFER_TX_MULTI))
53 
54 #define IS_LL_SWPMI_VOLTAGE_CLASS(__VALUE__) (((__VALUE__) == LL_SWPMI_VOLTAGE_CLASS_C) \
55                                            || ((__VALUE__) == LL_SWPMI_VOLTAGE_CLASS_B))
56 
57 /**
58   * @}
59   */
60 
61 /* Private function prototypes -----------------------------------------------*/
62 
63 /* Exported functions --------------------------------------------------------*/
64 /** @addtogroup SWPMI_LL_Exported_Functions
65   * @{
66   */
67 
68 /** @addtogroup SWPMI_LL_EF_Init
69   * @{
70   */
71 
72 /**
73   * @brief  De-initialize the SWPMI peripheral registers to their default reset values.
74   * @param  SWPMIx SWPMI Instance
75   * @retval An ErrorStatus enumeration value
76   *          - SUCCESS: SWPMI registers are de-initialized
77   *          - ERROR: Not applicable
78   */
LL_SWPMI_DeInit(SWPMI_TypeDef * SWPMIx)79 ErrorStatus LL_SWPMI_DeInit(SWPMI_TypeDef *SWPMIx)
80 {
81   ErrorStatus status = SUCCESS;
82 
83   /* Check the parameter */
84   assert_param(IS_SWPMI_INSTANCE(SWPMIx));
85 
86   if (SWPMIx == SWPMI1)
87   {
88     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_SWPMI1);
89     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_SWPMI1);
90   }
91   else
92   {
93     status = ERROR;
94   }
95 
96   return status;
97 }
98 
99 /**
100   * @brief  Initialize the SWPMI peripheral according to the specified parameters in the SWPMI_InitStruct.
101   * @note   As some bits in SWPMI configuration registers can only be written when the SWPMI is deactivated
102   *         (SWPMI_CR_SWPACT bit = 0), the SWPMI peripheral should be in deactivated state prior calling
103   *         this function. Otherwise, ERROR result will be returned.
104   * @param  SWPMIx           SWPMI Instance
105   * @param  SWPMI_InitStruct pointer to a @ref LL_SWPMI_InitTypeDef structure that contains
106   *                          the configuration information for the SWPMI peripheral.
107   * @retval An ErrorStatus enumeration value
108   *          - SUCCESS: SWPMI registers are initialized
109   *          - ERROR: SWPMI registers are not initialized
110   */
LL_SWPMI_Init(SWPMI_TypeDef * SWPMIx,LL_SWPMI_InitTypeDef * SWPMI_InitStruct)111 ErrorStatus LL_SWPMI_Init(SWPMI_TypeDef *SWPMIx, LL_SWPMI_InitTypeDef *SWPMI_InitStruct)
112 {
113   ErrorStatus status = SUCCESS;
114 
115   /* Check the parameters */
116   assert_param(IS_SWPMI_INSTANCE(SWPMIx));
117   assert_param(IS_LL_SWPMI_BITRATE_VALUE(SWPMI_InitStruct->BitRatePrescaler));
118   assert_param(IS_LL_SWPMI_SW_BUFFER_TX(SWPMI_InitStruct->TxBufferingMode));
119   assert_param(IS_LL_SWPMI_SW_BUFFER_RX(SWPMI_InitStruct->RxBufferingMode));
120   assert_param(IS_LL_SWPMI_VOLTAGE_CLASS(SWPMI_InitStruct->VoltageClass));
121 
122   /* SWPMI needs to be in deactivated state, in order to be able to configure some bits */
123   if (LL_SWPMI_IsActivated(SWPMIx) == 0U)
124   {
125     /* Configure the BRR register (Bitrate) */
126     LL_SWPMI_SetBitRatePrescaler(SWPMIx, SWPMI_InitStruct->BitRatePrescaler);
127 
128     /* Configure the voltage class */
129     LL_SWPMI_SetVoltageClass(SWPMIx, SWPMI_InitStruct->VoltageClass);
130 
131     /* Set the new configuration of the SWPMI peripheral */
132     MODIFY_REG(SWPMIx->CR,
133                (SWPMI_CR_RXMODE | SWPMI_CR_TXMODE),
134                (SWPMI_InitStruct->TxBufferingMode | SWPMI_InitStruct->RxBufferingMode));
135   }
136   /* Else (SWPMI not in deactivated state => return ERROR) */
137   else
138   {
139     status = ERROR;
140   }
141 
142   return status;
143 }
144 
145 /**
146   * @brief  Set each @ref LL_SWPMI_InitTypeDef field to default value.
147   * @param  SWPMI_InitStruct pointer to a @ref LL_SWPMI_InitTypeDef structure that contains
148   *                          the configuration information for the SWPMI peripheral.
149   * @retval None
150   */
LL_SWPMI_StructInit(LL_SWPMI_InitTypeDef * SWPMI_InitStruct)151 void LL_SWPMI_StructInit(LL_SWPMI_InitTypeDef *SWPMI_InitStruct)
152 {
153   /* Set SWPMI_InitStruct fields to default values */
154   SWPMI_InitStruct->VoltageClass     = LL_SWPMI_VOLTAGE_CLASS_C;
155   SWPMI_InitStruct->BitRatePrescaler = (uint32_t)0x00000001;
156   SWPMI_InitStruct->TxBufferingMode  = LL_SWPMI_SW_BUFFER_TX_SINGLE;
157   SWPMI_InitStruct->RxBufferingMode  = LL_SWPMI_SW_BUFFER_RX_SINGLE;
158 }
159 
160 /**
161   * @}
162   */
163 
164 /**
165   * @}
166   */
167 
168 /**
169   * @}
170   */
171 
172 
173 /**
174   * @}
175   */
176 
177 #endif /* USE_FULL_LL_DRIVER */
178