Subversion Repositories FuelGauge

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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, uint16_t ep_addr,
  82.                                        uint16_t ep_kind, uint32_t pmaadress)
  83. {
  84.   PCD_EPTypeDef *ep;
  85.  
  86.   /* initialize ep structure*/
  87.   if ((0x80U & ep_addr) == 0x80U)
  88.   {
  89.     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  90.   }
  91.   else
  92.   {
  93.     ep = &hpcd->OUT_ep[ep_addr];
  94.   }
  95.  
  96.   /* Here we check if the endpoint is single or double Buffer*/
  97.   if (ep_kind == PCD_SNG_BUF)
  98.   {
  99.     /* Single Buffer */
  100.     ep->doublebuffer = 0U;
  101.     /* Configure the PMA */
  102.     ep->pmaadress = (uint16_t)pmaadress;
  103.   }
  104.   else /* USB_DBL_BUF */
  105.   {
  106.     /* Double Buffer Endpoint */
  107.     ep->doublebuffer = 1U;
  108.     /* Configure the PMA */
  109.     ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU);
  110.     ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16);
  111.   }
  112.  
  113.   return HAL_OK;
  114. }
  115.  
  116. /**
  117.   * @brief  Activate BatteryCharging feature.
  118.   * @param  hpcd PCD handle
  119.   * @retval HAL status
  120.   */
  121. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  122. {
  123.   USB_TypeDef *USBx = hpcd->Instance;
  124.   hpcd->battery_charging_active = 1U;
  125.  
  126.   /* Enable BCD feature */
  127.   USBx->BCDR |= USB_BCDR_BCDEN;
  128.  
  129.   /* Enable DCD : Data Contact Detect */
  130.   USBx->BCDR &= ~(USB_BCDR_PDEN);
  131.   USBx->BCDR &= ~(USB_BCDR_SDEN);
  132.   USBx->BCDR |= USB_BCDR_DCDEN;
  133.  
  134.   return HAL_OK;
  135. }
  136.  
  137. /**
  138.   * @brief  Deactivate BatteryCharging feature.
  139.   * @param  hpcd PCD handle
  140.   * @retval HAL status
  141.   */
  142. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  143. {
  144.   USB_TypeDef *USBx = hpcd->Instance;
  145.   hpcd->battery_charging_active = 0U;
  146.  
  147.   /* Disable BCD feature */
  148.   USBx->BCDR &= ~(USB_BCDR_BCDEN);
  149.  
  150.   return HAL_OK;
  151. }
  152.  
  153. /**
  154.   * @brief  Handle BatteryCharging Process.
  155.   * @param  hpcd PCD handle
  156.   * @retval HAL status
  157.   */
  158. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  159. {
  160.   USB_TypeDef *USBx = hpcd->Instance;
  161.   uint32_t tickstart = HAL_GetTick();
  162.  
  163.   /* Wait Detect flag or a timeout is happen */
  164.   while ((USBx->BCDR & USB_BCDR_DCDET) == 0U)
  165.   {
  166.     /* Check for the Timeout */
  167.     if ((HAL_GetTick() - tickstart) > 1000U)
  168.     {
  169. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  170.       hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
  171. #else
  172.       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  173. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  174.  
  175.       return;
  176.     }
  177.   }
  178.  
  179.   HAL_Delay(200U);
  180.  
  181.   /* Data Pin Contact ? Check Detect flag */
  182.   if ((USBx->BCDR & USB_BCDR_DCDET) == USB_BCDR_DCDET)
  183.   {
  184. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  185.     hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
  186. #else
  187.     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  188. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  189.   }
  190.   /* Primary detection: checks if connected to Standard Downstream Port
  191.   (without charging capability) */
  192.   USBx->BCDR &= ~(USB_BCDR_DCDEN);
  193.   HAL_Delay(50U);
  194.   USBx->BCDR |= (USB_BCDR_PDEN);
  195.   HAL_Delay(50U);
  196.  
  197.   /* If Charger detect ? */
  198.   if ((USBx->BCDR & USB_BCDR_PDET) == USB_BCDR_PDET)
  199.   {
  200.     /* Start secondary detection to check connection to Charging Downstream
  201.     Port or Dedicated Charging Port */
  202.     USBx->BCDR &= ~(USB_BCDR_PDEN);
  203.     HAL_Delay(50U);
  204.     USBx->BCDR |= (USB_BCDR_SDEN);
  205.     HAL_Delay(50U);
  206.  
  207.     /* If CDP ? */
  208.     if ((USBx->BCDR & USB_BCDR_SDET) == USB_BCDR_SDET)
  209.     {
  210.       /* Dedicated Downstream Port DCP */
  211. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  212.       hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  213. #else
  214.       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  215. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  216.     }
  217.     else
  218.     {
  219.       /* Charging Downstream Port CDP */
  220. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  221.       hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  222. #else
  223.       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  224. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  225.     }
  226.   }
  227.   else /* NO */
  228.   {
  229.     /* Standard Downstream Port */
  230. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  231.     hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  232. #else
  233.     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  234. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  235.   }
  236.  
  237.   /* Battery Charging capability discovery finished Start Enumeration */
  238.   (void)HAL_PCDEx_DeActivateBCD(hpcd);
  239. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  240.   hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  241. #else
  242.   HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  243. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  244. }
  245.  
  246.  
  247. /**
  248.   * @brief  Activate LPM feature.
  249.   * @param  hpcd PCD handle
  250.   * @retval HAL status
  251.   */
  252. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  253. {
  254.  
  255.   USB_TypeDef *USBx = hpcd->Instance;
  256.   hpcd->lpm_active = 1U;
  257.   hpcd->LPM_State = LPM_L0;
  258.  
  259.   USBx->LPMCSR |= USB_LPMCSR_LMPEN;
  260.   USBx->LPMCSR |= USB_LPMCSR_LPMACK;
  261.  
  262.   return HAL_OK;
  263. }
  264.  
  265. /**
  266.   * @brief  Deactivate LPM feature.
  267.   * @param  hpcd PCD handle
  268.   * @retval HAL status
  269.   */
  270. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  271. {
  272.   USB_TypeDef *USBx = hpcd->Instance;
  273.  
  274.   hpcd->lpm_active = 0U;
  275.  
  276.   USBx->LPMCSR &= ~(USB_LPMCSR_LMPEN);
  277.   USBx->LPMCSR &= ~(USB_LPMCSR_LPMACK);
  278.  
  279.   return HAL_OK;
  280. }
  281.  
  282.  
  283.  
  284. /**
  285.   * @brief  Send LPM message to user layer callback.
  286.   * @param  hpcd PCD handle
  287.   * @param  msg LPM message
  288.   * @retval HAL status
  289.   */
  290. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  291. {
  292.   /* Prevent unused argument(s) compilation warning */
  293.   UNUSED(hpcd);
  294.   UNUSED(msg);
  295.  
  296.   /* NOTE : This function should not be modified, when the callback is needed,
  297.             the HAL_PCDEx_LPM_Callback could be implemented in the user file
  298.    */
  299. }
  300.  
  301. /**
  302.   * @brief  Send BatteryCharging message to user layer callback.
  303.   * @param  hpcd PCD handle
  304.   * @param  msg LPM message
  305.   * @retval HAL status
  306.   */
  307. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  308. {
  309.   /* Prevent unused argument(s) compilation warning */
  310.   UNUSED(hpcd);
  311.   UNUSED(msg);
  312.  
  313.   /* NOTE : This function should not be modified, when the callback is needed,
  314.             the HAL_PCDEx_BCD_Callback could be implemented in the user file
  315.    */
  316. }
  317.  
  318. /**
  319.   * @}
  320.   */
  321.  
  322. /**
  323.   * @}
  324.   */
  325. #endif /* defined (USB) */
  326. #endif /* HAL_PCD_MODULE_ENABLED */
  327.  
  328. /**
  329.   * @}
  330.   */
  331.  
  332. /**
  333.   * @}
  334.   */
  335.  
  336. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  337.