/** * @brief Initializes the PCD according to the specified * parameters in the PCD_InitTypeDef and initialize the associated handle. * @param hpcd: PCD handle * @retval HAL status */ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd) { uint32_t index = 0U; /* Check the PCD handle allocation */ if(hpcd == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance)); if(hpcd->State == HAL_PCD_STATE_RESET) { /* Allocate lock resource and initialize it */ hpcd->Lock = HAL_UNLOCKED; /* Init the low level hardware : GPIO, CLOCK, NVIC... */ HAL_PCD_MspInit(hpcd); } hpcd->State = HAL_PCD_STATE_BUSY; /* Disable the Interrupts */ __HAL_PCD_DISABLE(hpcd); /*Init the Core (common init.) */ USB_CoreInit(hpcd->Instance, hpcd->Init); /* Force Device Mode*/ USB_SetCurrentMode(hpcd->Instance , USB_DEVICE_MODE); /* Init endpoints structures */ for (index = 0; index < hpcd->Init.dev_endpoints ; index++) { /* Init ep structure */ hpcd->IN_ep[index].is_in = 1; hpcd->IN_ep[index].num = index; hpcd->IN_ep[index].tx_fifo_num = index; /* Control until ep is activated */ hpcd->IN_ep[index].type = EP_TYPE_CTRL; hpcd->IN_ep[index].maxpacket = 0; hpcd->IN_ep[index].xfer_buff = 0; hpcd->IN_ep[index].xfer_len = 0; } for (index = 0; index < 15 ; index++) { hpcd->OUT_ep[index].is_in = 0; hpcd->OUT_ep[index].num = index; hpcd->IN_ep[index].tx_fifo_num = index; /* Control until ep is activated */ hpcd->OUT_ep[index].type = EP_TYPE_CTRL; hpcd->OUT_ep[index].maxpacket = 0; hpcd->OUT_ep[index].xfer_buff = 0; hpcd->OUT_ep[index].xfer_len = 0; } /* Init Device */ USB_DevInit(hpcd->Instance, hpcd->Init); hpcd->USB_Address = 0; hpcd->State= HAL_PCD_STATE_READY; /* Activate LPM */ if (hpcd->Init.lpm_enable ==1) { HAL_PCDEx_ActivateLPM(hpcd); } /* Activate Battery charging */ if (hpcd->Init.battery_charging_enable ==1) { HAL_PCDEx_ActivateBCD(hpcd); } USB_DevDisconnect (hpcd->Instance); return HAL_OK; }
/** * @brief Initializes the PCD according to the specified * parameters in the PCD_InitTypeDef and create the associated handle. * @param hpcd: PCD handle * @retval HAL status */ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd) { uint32_t i = 0; /* Check the PCD handle allocation */ if(hpcd == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance)); hpcd->State = HAL_PCD_STATE_BUSY; /* Init the low level hardware : GPIO, CLOCK, NVIC... */ HAL_PCD_MspInit(hpcd); /* Disable the Interrupts */ __HAL_PCD_DISABLE(hpcd); /*Init the Core (common init.) */ USB_CoreInit(hpcd->Instance, hpcd->Init); /* Force Device Mode*/ USB_SetCurrentMode(hpcd->Instance , USB_OTG_DEVICE_MODE); /* Init endpoints structures */ for (i = 0; i < 15 ; i++) { /* Init ep structure */ hpcd->IN_ep[i].is_in = 1; hpcd->IN_ep[i].num = i; hpcd->IN_ep[i].tx_fifo_num = i; /* Control until ep is activated */ hpcd->IN_ep[i].type = EP_TYPE_CTRL; hpcd->IN_ep[i].maxpacket = 0; hpcd->IN_ep[i].xfer_buff = 0; hpcd->IN_ep[i].xfer_len = 0; } for (i = 0; i < 15 ; i++) { hpcd->OUT_ep[i].is_in = 0; hpcd->OUT_ep[i].num = i; hpcd->IN_ep[i].tx_fifo_num = i; /* Control until ep is activated */ hpcd->OUT_ep[i].type = EP_TYPE_CTRL; hpcd->OUT_ep[i].maxpacket = 0; hpcd->OUT_ep[i].xfer_buff = 0; hpcd->OUT_ep[i].xfer_len = 0; hpcd->Instance->DIEPTXF[i] = 0; } /* Init Device */ USB_DevInit(hpcd->Instance, hpcd->Init); hpcd->State= HAL_PCD_STATE_READY; /* Activate LPM */ if (hpcd->Init.lpm_enable == 1) { HAL_PCDEx_ActivateLPM(hpcd); } USB_DevDisconnect (hpcd->Instance); return HAL_OK; }
/** * @brief Initializes the PCD according to the specified * parameters in the PCD_InitTypeDef and create the associated handle. * @param hpcd: PCD handle * @retval HAL status */ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd) { uint32_t i = 0U; uint32_t wInterrupt_Mask = 0U; /* Check the PCD handle allocation */ if(hpcd == NULL) { return HAL_ERROR; } /* Check the parameters */ assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance)); if(hpcd->State == HAL_PCD_STATE_RESET) { /* Allocate lock resource and initialize it */ hpcd->Lock = HAL_UNLOCKED; /* Init the low level hardware : GPIO, CLOCK, NVIC... */ HAL_PCD_MspInit(hpcd); } hpcd->State = HAL_PCD_STATE_BUSY; /* Init endpoints structures */ for (i = 0U; i < hpcd->Init.dev_endpoints ; i++) { /* Init ep structure */ hpcd->IN_ep[i].is_in = 1U; hpcd->IN_ep[i].num = i; /* Control until ep is actvated */ hpcd->IN_ep[i].type = PCD_EP_TYPE_CTRL; hpcd->IN_ep[i].maxpacket = 0U; hpcd->IN_ep[i].xfer_buff = 0U; hpcd->IN_ep[i].xfer_len = 0U; } for (i = 0U; i < hpcd->Init.dev_endpoints ; i++) { hpcd->OUT_ep[i].is_in = 0U; hpcd->OUT_ep[i].num = i; /* Control until ep is activated */ hpcd->OUT_ep[i].type = PCD_EP_TYPE_CTRL; hpcd->OUT_ep[i].maxpacket = 0U; hpcd->OUT_ep[i].xfer_buff = 0U; hpcd->OUT_ep[i].xfer_len = 0U; } /* Init Device */ /*CNTR_FRES = 1*/ hpcd->Instance->CNTR = USB_CNTR_FRES; /*CNTR_FRES = 0*/ hpcd->Instance->CNTR = 0U; /*Clear pending interrupts*/ hpcd->Instance->ISTR = 0U; /*Set Btable Adress*/ hpcd->Instance->BTABLE = BTABLE_ADDRESS; /*set wInterrupt_Mask global variable*/ wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \ | USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_RESETM; /*Set interrupt mask*/ hpcd->Instance->CNTR = wInterrupt_Mask; hpcd->USB_Address = 0U; hpcd->State= HAL_PCD_STATE_READY; /* Activate LPM */ if (hpcd->Init.lpm_enable ==1) { HAL_PCDEx_ActivateLPM(hpcd); } /* Activate Battery charging */ if (hpcd->Init.battery_charging_enable ==1) { HAL_PCDEx_ActivateBCD(hpcd); } return HAL_OK; }