Subversion Repositories DashDisplay

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file           : usbd_conf.c
  4.   * @version        : v1.0_Cube
  5.   * @brief          : This file implements the board support package for the USB device library
  6.   ******************************************************************************
  7.   *
  8.   * COPYRIGHT(c) 2016 STMicroelectronics
  9.   *
  10.   * Redistribution and use in source and binary forms, with or without modification,
  11.   * are permitted provided that the following conditions are met:
  12.   * 1. Redistributions of source code must retain the above copyright notice,
  13.   * this list of conditions and the following disclaimer.
  14.   * 2. Redistributions in binary form must reproduce the above copyright notice,
  15.   * this list of conditions and the following disclaimer in the documentation
  16.   * and/or other materials provided with the distribution.
  17.   * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18.   * may be used to endorse or promote products derived from this software
  19.   * without specific prior written permission.
  20.   *
  21.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.   *
  32.   ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f1xx.h"
  36. #include "stm32f1xx_hal.h"
  37. #include "usbd_def.h"
  38. #include "usbd_core.h"
  39. #include "usbd_cdc.h"
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /* Private macro -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44. PCD_HandleTypeDef hpcd_USB_FS;
  45.  
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48.  
  49. /* Private function prototypes -----------------------------------------------*/
  50. /* Private functions ---------------------------------------------------------*/
  51. /* USER CODE BEGIN 1 */
  52. /* USER CODE END 1 */
  53. void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state);
  54.  
  55. /*******************************************************************************
  56.                        LL Driver Callbacks (PCD -> USB Device Library)
  57. *******************************************************************************/
  58. /* MSP Init */
  59.  
  60. void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd)
  61. {
  62.   if(hpcd->Instance==USB)
  63.   {
  64.   /* USER CODE BEGIN USB_MspInit 0 */
  65.  
  66.   /* USER CODE END USB_MspInit 0 */
  67.     /* Peripheral clock enable */
  68.     __HAL_RCC_USB_CLK_ENABLE();
  69.  
  70.     /* Peripheral interrupt init */
  71.     HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  72.     HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  73.   /* USER CODE BEGIN USB_MspInit 1 */
  74.  
  75.   /* USER CODE END USB_MspInit 1 */
  76.   }
  77. }
  78.  
  79. void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd)
  80. {
  81.   if(hpcd->Instance==USB)
  82.   {
  83.   /* USER CODE BEGIN USB_MspDeInit 0 */
  84.  
  85.   /* USER CODE END USB_MspDeInit 0 */
  86.     /* Peripheral clock disable */
  87.     __HAL_RCC_USB_CLK_DISABLE();
  88.  
  89.     /* Peripheral interrupt Deinit*/
  90.     HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  91.  
  92.   /* USER CODE BEGIN USB_MspDeInit 1 */
  93.  
  94.   /* USER CODE END USB_MspDeInit 1 */
  95.   }
  96. }
  97.  
  98. /**
  99.   * @brief  Setup Stage callback
  100.   * @param  hpcd: PCD handle
  101.   * @retval None
  102.   */
  103. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  104. {
  105.   USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
  106. }
  107.  
  108. /**
  109.   * @brief  Data Out Stage callback.
  110.   * @param  hpcd: PCD handle
  111.   * @param  epnum: Endpoint Number
  112.   * @retval None
  113.   */
  114. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  115. {
  116.   USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
  117. }
  118.  
  119. /**
  120.   * @brief  Data In Stage callback..
  121.   * @param  hpcd: PCD handle
  122.   * @param  epnum: Endpoint Number
  123.   * @retval None
  124.   */
  125. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  126. {
  127.   USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
  128. }
  129.  
  130. /**
  131.   * @brief  SOF callback.
  132.   * @param  hpcd: PCD handle
  133.   * @retval None
  134.   */
  135. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  136. {
  137.   USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
  138. }
  139.  
  140. /**
  141.   * @brief  Reset callback.
  142.   * @param  hpcd: PCD handle
  143.   * @retval None
  144.   */
  145. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  146. {
  147.   USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
  148.  
  149.   /*Set USB Current Speed*/
  150.   switch (hpcd->Init.speed)
  151.   {
  152.   case PCD_SPEED_FULL:
  153.     speed = USBD_SPEED_FULL;    
  154.     break;
  155.        
  156.   default:
  157.     speed = USBD_SPEED_FULL;    
  158.     break;    
  159.   }
  160.   USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);  
  161.  
  162.   /*Reset Device*/
  163.   USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
  164. }
  165.  
  166. /**
  167.   * @brief  Suspend callback.
  168.   * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  169.   * @param  hpcd: PCD handle
  170.   * @retval None
  171.   */
  172. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  173. {
  174.   /* Inform USB library that core enters in suspend Mode */
  175.   USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
  176.   /*Enter in STOP mode */
  177.   /* USER CODE BEGIN 2 */  
  178.   if (hpcd->Init.low_power_enable)
  179.   {
  180.     /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */
  181.     SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
  182.   }
  183.   /* USER CODE END 2 */
  184. }
  185.  
  186. /**
  187.   * @brief  Resume callback.
  188.   * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
  189.   * @param  hpcd: PCD handle
  190.   * @retval None
  191.   */
  192. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  193. {
  194.   /* USER CODE BEGIN 3 */
  195.   /* USER CODE END 3 */
  196.   USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
  197.  
  198. }
  199.  
  200. /**
  201.   * @brief  ISOOUTIncomplete callback.
  202.   * @param  hpcd: PCD handle
  203.   * @param  epnum: Endpoint Number
  204.   * @retval None
  205.   */
  206. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  207. {
  208.   USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
  209. }
  210.  
  211. /**
  212.   * @brief  ISOINIncomplete callback.
  213.   * @param  hpcd: PCD handle
  214.   * @param  epnum: Endpoint Number
  215.   * @retval None
  216.   */
  217. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  218. {
  219.   USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
  220. }
  221.  
  222. /**
  223.   * @brief  ConnectCallback callback.
  224.   * @param  hpcd: PCD handle
  225.   * @retval None
  226.   */
  227. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  228. {
  229.   USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
  230. }
  231.  
  232. /**
  233.   * @brief  Disconnect callback.
  234.   * @param  hpcd: PCD handle
  235.   * @retval None
  236.   */
  237. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  238. {
  239.   USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
  240. }
  241.  
  242. /*******************************************************************************
  243.                        LL Driver Interface (USB Device Library --> PCD)
  244. *******************************************************************************/
  245. /**
  246.   * @brief  Initializes the Low Level portion of the Device driver.
  247.   * @param  pdev: Device handle
  248.   * @retval USBD Status
  249.   */
  250. USBD_StatusTypeDef  USBD_LL_Init (USBD_HandleTypeDef *pdev)
  251. {
  252.   /* Init USB_IP */
  253.   /* Link The driver to the stack */
  254.   hpcd_USB_FS.pData = pdev;
  255.   pdev->pData = &hpcd_USB_FS;
  256.  
  257.   hpcd_USB_FS.Instance = USB;
  258.   hpcd_USB_FS.Init.dev_endpoints = 8;
  259.   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
  260.   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_8;
  261.   hpcd_USB_FS.Init.low_power_enable = DISABLE;
  262.   hpcd_USB_FS.Init.lpm_enable = DISABLE;
  263.   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
  264.   HAL_PCD_Init(&hpcd_USB_FS);
  265.  
  266.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
  267.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
  268.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0);  
  269.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x110);
  270.   HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);  
  271.   return USBD_OK;
  272. }
  273.  
  274. /**
  275.   * @brief  De-Initializes the Low Level portion of the Device driver.
  276.   * @param  pdev: Device handle
  277.   * @retval USBD Status
  278.   */
  279. USBD_StatusTypeDef  USBD_LL_DeInit (USBD_HandleTypeDef *pdev)
  280. {
  281.   HAL_PCD_DeInit((PCD_HandleTypeDef*)pdev->pData);
  282.   return USBD_OK;
  283. }
  284.  
  285. /**
  286.   * @brief  Starts the Low Level portion of the Device driver.
  287.   * @param  pdev: Device handle
  288.   * @retval USBD Status
  289.   */
  290. USBD_StatusTypeDef  USBD_LL_Start(USBD_HandleTypeDef *pdev)
  291. {
  292.   HAL_PCD_Start((PCD_HandleTypeDef*)pdev->pData);
  293.   return USBD_OK;
  294. }
  295.  
  296. /**
  297.   * @brief  Stops the Low Level portion of the Device driver.
  298.   * @param  pdev: Device handle
  299.   * @retval USBD Status
  300.   */
  301. USBD_StatusTypeDef  USBD_LL_Stop (USBD_HandleTypeDef *pdev)
  302. {
  303.   HAL_PCD_Stop((PCD_HandleTypeDef*) pdev->pData);
  304.   return USBD_OK;
  305. }
  306.  
  307. /**
  308.   * @brief  Opens an endpoint of the Low Level Driver.
  309.   * @param  pdev: Device handle
  310.   * @param  ep_addr: Endpoint Number
  311.   * @param  ep_type: Endpoint Type
  312.   * @param  ep_mps: Endpoint Max Packet Size
  313.   * @retval USBD Status
  314.   */
  315. USBD_StatusTypeDef  USBD_LL_OpenEP  (USBD_HandleTypeDef *pdev,
  316.                                       uint8_t  ep_addr,                                      
  317.                                       uint8_t  ep_type,
  318.                                       uint16_t ep_mps)
  319. {
  320.   HAL_PCD_EP_Open((PCD_HandleTypeDef*) pdev->pData,
  321.                   ep_addr,
  322.                   ep_mps,
  323.                   ep_type);
  324.  
  325.   return USBD_OK;
  326. }
  327.  
  328. /**
  329.   * @brief  Closes an endpoint of the Low Level Driver.
  330.   * @param  pdev: Device handle
  331.   * @param  ep_addr: Endpoint Number
  332.   * @retval USBD Status
  333.   */
  334. USBD_StatusTypeDef  USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)  
  335. {
  336.   HAL_PCD_EP_Close((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  337.   return USBD_OK;
  338. }
  339.  
  340. /**
  341.   * @brief  Flushes an endpoint of the Low Level Driver.
  342.   * @param  pdev: Device handle
  343.   * @param  ep_addr: Endpoint Number
  344.   * @retval USBD Status
  345.   */
  346. USBD_StatusTypeDef  USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)  
  347. {
  348.   HAL_PCD_EP_Flush((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  349.   return USBD_OK;
  350. }
  351.  
  352. /**
  353.   * @brief  Sets a Stall condition on an endpoint of the Low Level Driver.
  354.   * @param  pdev: Device handle
  355.   * @param  ep_addr: Endpoint Number
  356.   * @retval USBD Status
  357.   */
  358. USBD_StatusTypeDef  USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)  
  359. {
  360.   HAL_PCD_EP_SetStall((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  361.   return USBD_OK;
  362. }
  363.  
  364. /**
  365.   * @brief  Clears a Stall condition on an endpoint of the Low Level Driver.
  366.   * @param  pdev: Device handle
  367.   * @param  ep_addr: Endpoint Number
  368.   * @retval USBD Status
  369.   */
  370. USBD_StatusTypeDef  USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)  
  371. {
  372.   HAL_PCD_EP_ClrStall((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  373.   return USBD_OK;
  374. }
  375.  
  376. /**
  377.   * @brief  Returns Stall condition.
  378.   * @param  pdev: Device handle
  379.   * @param  ep_addr: Endpoint Number
  380.   * @retval Stall (1: Yes, 0: No)
  381.   */
  382. uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr)  
  383. {
  384.   PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData;
  385.  
  386.   if((ep_addr & 0x80) == 0x80)
  387.   {
  388.     return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  389.   }
  390.   else
  391.   {
  392.     return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  393.   }
  394. }
  395. /**
  396.   * @brief  Assigns a USB address to the device.
  397.   * @param  pdev: Device handle
  398.   * @param  ep_addr: Endpoint Number
  399.   * @retval USBD Status
  400.   */
  401. USBD_StatusTypeDef  USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr)  
  402. {
  403.   HAL_PCD_SetAddress((PCD_HandleTypeDef*) pdev->pData, dev_addr);
  404.   return USBD_OK;
  405. }
  406.  
  407. /**
  408.   * @brief  Transmits data over an endpoint.
  409.   * @param  pdev: Device handle
  410.   * @param  ep_addr: Endpoint Number
  411.   * @param  pbuf: Pointer to data to be sent
  412.   * @param  size: Data size    
  413.   * @retval USBD Status
  414.   */
  415. USBD_StatusTypeDef  USBD_LL_Transmit (USBD_HandleTypeDef *pdev,
  416.                                       uint8_t  ep_addr,                                      
  417.                                       uint8_t  *pbuf,
  418.                                       uint16_t  size)
  419. {
  420.   HAL_PCD_EP_Transmit((PCD_HandleTypeDef*) pdev->pData, ep_addr, pbuf, size);
  421.   return USBD_OK;
  422. }
  423.  
  424. /**
  425.   * @brief  Prepares an endpoint for reception.
  426.   * @param  pdev: Device handle
  427.   * @param  ep_addr: Endpoint Number
  428.   * @param  pbuf: Pointer to data to be received
  429.   * @param  size: Data size
  430.   * @retval USBD Status
  431.   */
  432. USBD_StatusTypeDef  USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
  433.                                            uint8_t  ep_addr,                                      
  434.                                            uint8_t  *pbuf,
  435.                                            uint16_t  size)
  436. {
  437.   HAL_PCD_EP_Receive((PCD_HandleTypeDef*) pdev->pData, ep_addr, pbuf, size);
  438.   return USBD_OK;
  439. }
  440.  
  441. /**
  442.   * @brief  Returns the last transfered packet size.
  443.   * @param  pdev: Device handle
  444.   * @param  ep_addr: Endpoint Number
  445.   * @retval Recived Data Size
  446.   */
  447. uint32_t USBD_LL_GetRxDataSize  (USBD_HandleTypeDef *pdev, uint8_t  ep_addr)  
  448. {
  449.   return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr);
  450. }
  451.  
  452. /**
  453.   * @brief  Delays routine for the USB Device Library.
  454.   * @param  Delay: Delay in ms
  455.   * @retval None
  456.   */
  457. void  USBD_LL_Delay (uint32_t Delay)
  458. {
  459.   HAL_Delay(Delay);  
  460. }
  461.  
  462. /**
  463.   * @brief  static single allocation.
  464.   * @param  size: size of allocated memory
  465.   * @retval None
  466.   */
  467. void *USBD_static_malloc(uint32_t size)
  468. {
  469.   static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];//On 32-bit boundary
  470.   return mem;
  471. }
  472.  
  473. /**
  474.   * @brief  Dummy memory free
  475.   * @param  *p pointer to allocated  memory address
  476.   * @retval None
  477.   */
  478. void USBD_static_free(void *p)
  479. {
  480.   free(p);
  481. }
  482.  
  483. /**
  484. * @brief Software Device Connection
  485. * @param hpcd: PCD handle
  486. * @param state: connection state (0 : disconnected / 1: connected)
  487. * @retval None
  488. */
  489. void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
  490. {
  491. /* USER CODE BEGIN 5 */
  492.   if (state == 1)
  493.   {
  494.     /* Configure Low Connection State */
  495.        
  496.   }
  497.   else
  498.   {
  499.     /* Configure High Connection State */
  500.    
  501.   }
  502. /* USER CODE END 5 */
  503. }
  504.  
  505. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  506.