Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f0xx_hal_pcd_ex.c
4
  * @author  MCD Application Team
5
  * @brief   PCD Extended HAL module driver.
6
  *          This file provides firmware functions to manage the following
7
  *          functionalities of the USB Peripheral Controller:
8
  *           + Extended features functions
9
  *
10
  ******************************************************************************
11
  * @attention
12
  *
13
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
14
  * All rights reserved.</center></h2>
15
  *
16
  * This software component is licensed by ST under BSD 3-Clause license,
17
  * the "License"; You may not use this file except in compliance with the
18
  * License. You may obtain a copy of the License at:
19
  *                        opensource.org/licenses/BSD-3-Clause
20
  *
21
  ******************************************************************************
22
  */
23
 
24
/* Includes ------------------------------------------------------------------*/
25
#include "stm32f0xx_hal.h"
26
 
27
/** @addtogroup STM32F0xx_HAL_Driver
28
  * @{
29
  */
30
 
31
/** @defgroup PCDEx PCDEx
32
  * @brief PCD Extended HAL module driver
33
  * @{
34
  */
35
 
36
#ifdef HAL_PCD_MODULE_ENABLED
37
 
38
#if defined (USB)
39
/* Private types -------------------------------------------------------------*/
40
/* Private variables ---------------------------------------------------------*/
41
/* Private constants ---------------------------------------------------------*/
42
/* Private macros ------------------------------------------------------------*/
43
/* Private functions ---------------------------------------------------------*/
44
/* Exported functions --------------------------------------------------------*/
45
 
46
/** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
47
  * @{
48
  */
49
 
50
/** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
51
  * @brief    PCDEx control functions
52
 *
53
@verbatim
54
 ===============================================================================
55
                 ##### Extended features functions #####
56
 ===============================================================================
57
    [..]  This section provides functions allowing to:
58
      (+) Update FIFO configuration
59
 
60
@endverbatim
61
  * @{
62
  */
63
 
64
/**
65
  * @brief  Configure PMA for EP
66
  * @param  hpcd  Device instance
67
  * @param  ep_addr endpoint address
68
  * @param  ep_kind endpoint Kind
69
  *                  USB_SNG_BUF: Single Buffer used
70
  *                  USB_DBL_BUF: Double Buffer used
71
  * @param  pmaadress: EP address in The PMA: In case of single buffer endpoint
72
  *                   this parameter is 16-bit value providing the address
73
  *                   in PMA allocated to endpoint.
74
  *                   In case of double buffer endpoint this parameter
75
  *                   is a 32-bit value providing the endpoint buffer 0 address
76
  *                   in the LSB part of 32-bit value and endpoint buffer 1 address
77
  *                   in the MSB part of 32-bit value.
78
  * @retval HAL status
79
  */
80
 
81
HAL_StatusTypeDef  HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd,
82
                                       uint16_t ep_addr,
83
                                       uint16_t ep_kind,
84
                                       uint32_t pmaadress)
85
{
86
  PCD_EPTypeDef *ep;
87
 
88
  /* initialize ep structure*/
89
  if ((0x80U & ep_addr) == 0x80U)
90
  {
91
    ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
92
  }
93
  else
94
  {
95
    ep = &hpcd->OUT_ep[ep_addr];
96
  }
97
 
98
  /* Here we check if the endpoint is single or double Buffer*/
99
  if (ep_kind == PCD_SNG_BUF)
100
  {
101
    /* Single Buffer */
102
    ep->doublebuffer = 0U;
103
    /* Configure the PMA */
104
    ep->pmaadress = (uint16_t)pmaadress;
105
  }
106
  else /* USB_DBL_BUF */
107
  {
108
    /* Double Buffer Endpoint */
109
    ep->doublebuffer = 1U;
110
    /* Configure the PMA */
111
    ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU);
112
    ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16);
113
  }
114
 
115
  return HAL_OK;
116
}
117
 
118
/**
119
  * @brief  Activate BatteryCharging feature.
120
  * @param  hpcd PCD handle
121
  * @retval HAL status
122
  */
123
HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
124
{
125
  USB_TypeDef *USBx = hpcd->Instance;
126
  hpcd->battery_charging_active = 1U;
127
 
128
  /* Enable BCD feature */
129
  USBx->BCDR |= USB_BCDR_BCDEN;
130
 
131
  /* Enable DCD : Data Contact Detect */
132
  USBx->BCDR &= ~(USB_BCDR_PDEN);
133
  USBx->BCDR &= ~(USB_BCDR_SDEN);
134
  USBx->BCDR |= USB_BCDR_DCDEN;
135
 
136
  return HAL_OK;
137
}
138
 
139
/**
140
  * @brief  Deactivate BatteryCharging feature.
141
  * @param  hpcd PCD handle
142
  * @retval HAL status
143
  */
144
HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
145
{
146
  USB_TypeDef *USBx = hpcd->Instance;
147
  hpcd->battery_charging_active = 0U;
148
 
149
  /* Disable BCD feature */
150
  USBx->BCDR &= ~(USB_BCDR_BCDEN);
151
 
152
  return HAL_OK;
153
}
154
 
155
/**
156
  * @brief  Handle BatteryCharging Process.
157
  * @param  hpcd PCD handle
158
  * @retval HAL status
159
  */
160
void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
161
{
162
  USB_TypeDef *USBx = hpcd->Instance;
163
  uint32_t tickstart = HAL_GetTick();
164
 
165
  /* Wait Detect flag or a timeout is happen*/
166
  while ((USBx->BCDR & USB_BCDR_DCDET) == 0U)
167
  {
168
    /* Check for the Timeout */
169
    if ((HAL_GetTick() - tickstart) > 1000U)
170
    {
171
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
172
      hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
173
#else
174
      HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
175
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
176
 
177
      return;
178
    }
179
  }
180
 
181
  HAL_Delay(200U);
182
 
183
  /* Data Pin Contact ? Check Detect flag */
184
  if ((USBx->BCDR & USB_BCDR_DCDET) == USB_BCDR_DCDET)
185
  {
186
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
187
    hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
188
#else
189
    HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
190
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
191
  }
192
  /* Primary detection: checks if connected to Standard Downstream Port
193
  (without charging capability) */
194
  USBx->BCDR &= ~(USB_BCDR_DCDEN);
195
  HAL_Delay(50U);
196
  USBx->BCDR |= (USB_BCDR_PDEN);
197
  HAL_Delay(50U);
198
 
199
  /* If Charger detect ? */
200
  if ((USBx->BCDR & USB_BCDR_PDET) == USB_BCDR_PDET)
201
  {
202
    /* Start secondary detection to check connection to Charging Downstream
203
    Port or Dedicated Charging Port */
204
    USBx->BCDR &= ~(USB_BCDR_PDEN);
205
    HAL_Delay(50U);
206
    USBx->BCDR |= (USB_BCDR_SDEN);
207
    HAL_Delay(50U);
208
 
209
    /* If CDP ? */
210
    if ((USBx->BCDR & USB_BCDR_SDET) == USB_BCDR_SDET)
211
    {
212
      /* Dedicated Downstream Port DCP */
213
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
214
      hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
215
#else
216
      HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
217
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
218
    }
219
    else
220
    {
221
      /* Charging Downstream Port CDP */
222
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
223
      hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
224
#else
225
      HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
226
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
227
    }
228
  }
229
  else /* NO */
230
  {
231
    /* Standard Downstream Port */
232
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
233
    hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
234
#else
235
    HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
236
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
237
  }
238
 
239
  /* Battery Charging capability discovery finished Start Enumeration */
240
  (void)HAL_PCDEx_DeActivateBCD(hpcd);
241
#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
242
  hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
243
#else
244
  HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
245
#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
246
}
247
 
248
 
249
/**
250
  * @brief  Activate LPM feature.
251
  * @param  hpcd PCD handle
252
  * @retval HAL status
253
  */
254
HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
255
{
256
 
257
  USB_TypeDef *USBx = hpcd->Instance;
258
  hpcd->lpm_active = 1U;
259
  hpcd->LPM_State = LPM_L0;
260
 
261
  USBx->LPMCSR |= USB_LPMCSR_LMPEN;
262
  USBx->LPMCSR |= USB_LPMCSR_LPMACK;
263
 
264
  return HAL_OK;
265
}
266
 
267
/**
268
  * @brief  Deactivate LPM feature.
269
  * @param  hpcd PCD handle
270
  * @retval HAL status
271
  */
272
HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
273
{
274
  USB_TypeDef *USBx = hpcd->Instance;
275
 
276
  hpcd->lpm_active = 0U;
277
 
278
  USBx->LPMCSR &= ~(USB_LPMCSR_LMPEN);
279
  USBx->LPMCSR &= ~(USB_LPMCSR_LPMACK);
280
 
281
  return HAL_OK;
282
}
283
 
284
 
285
 
286
/**
287
  * @brief  Send LPM message to user layer callback.
288
  * @param  hpcd PCD handle
289
  * @param  msg LPM message
290
  * @retval HAL status
291
  */
292
__weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
293
{
294
  /* Prevent unused argument(s) compilation warning */
295
  UNUSED(hpcd);
296
  UNUSED(msg);
297
 
298
  /* NOTE : This function should not be modified, when the callback is needed,
299
            the HAL_PCDEx_LPM_Callback could be implemented in the user file
300
   */
301
}
302
 
303
/**
304
  * @brief  Send BatteryCharging message to user layer callback.
305
  * @param  hpcd PCD handle
306
  * @param  msg LPM message
307
  * @retval HAL status
308
  */
309
__weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
310
{
311
  /* Prevent unused argument(s) compilation warning */
312
  UNUSED(hpcd);
313
  UNUSED(msg);
314
 
315
  /* NOTE : This function should not be modified, when the callback is needed,
316
            the HAL_PCDEx_BCD_Callback could be implemented in the user file
317
   */
318
}
319
 
320
/**
321
  * @}
322
  */
323
 
324
/**
325
  * @}
326
  */
327
#endif /* defined (USB) */
328
#endif /* HAL_PCD_MODULE_ENABLED */
329
 
330
/**
331
  * @}
332
  */
333
 
334
/**
335
  * @}
336
  */
337
 
338
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/