2024-11-06 19:58:23 -08:00
|
|
|
/********************************** (C) COPYRIGHT *******************************
|
|
|
|
* File Name : hw_config.c
|
|
|
|
* Author : WCH
|
|
|
|
* Version : V1.0.0
|
|
|
|
* Date : 2021/08/08
|
|
|
|
* Description : USB configuration file.
|
|
|
|
*********************************************************************************
|
|
|
|
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
|
|
|
* Attention: This software (modified or not) and binary are used for
|
|
|
|
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
|
|
|
*******************************************************************************/
|
|
|
|
#include "hw_config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "../driver/inc/usb_lib.h"
|
|
|
|
|
|
|
|
#include "usb_desc.h"
|
|
|
|
#include "usb_istr.h"
|
|
|
|
#include "usb_prop.h"
|
|
|
|
#include "usb_pwr.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void USBWakeUp_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
|
|
|
void USB_LP_CAN1_RX0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn USBWakeUp_IRQHandler
|
|
|
|
*
|
|
|
|
* @brief This function handles USB wake up exception.
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
void USBWakeUp_IRQHandler(void)
|
|
|
|
{
|
|
|
|
EXTI_ClearITPendingBit(EXTI_Line18);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn USB_LP_CAN1_RX0_IRQHandler
|
|
|
|
*
|
|
|
|
* @brief This function handles USB exception.
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
void USB_LP_CAN1_RX0_IRQHandler(void)
|
|
|
|
{
|
|
|
|
USB_Istr();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn Set_USBConfig
|
|
|
|
*
|
|
|
|
* @brief Set_USBConfig .
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
void usb_conf_clksource(void)
|
|
|
|
{
|
|
|
|
switch (SystemCoreClock) {
|
|
|
|
case 144000000: RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div3 ); break;
|
|
|
|
case 96000000: RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div2 ); break;
|
|
|
|
case 48000000: RCC_USBCLKConfig( RCC_USBCLKSource_PLLCLK_Div1 ); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn Enter_LowPowerMode
|
|
|
|
*
|
|
|
|
* @brief Enter low power mode.
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
2024-11-06 22:04:41 -08:00
|
|
|
void usb_enter_LowPowerMode(void)
|
2024-11-06 19:58:23 -08:00
|
|
|
{
|
2024-11-06 22:04:41 -08:00
|
|
|
printf_usb("usb enter low power mode\r\n");
|
2024-11-06 19:58:23 -08:00
|
|
|
bDeviceState = SUSPENDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn Leave_LowPowerMode
|
|
|
|
*
|
|
|
|
* @brief Leave low power mode.
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
2024-11-06 22:04:41 -08:00
|
|
|
void usb_leave_LowPowerMode(void)
|
2024-11-06 19:58:23 -08:00
|
|
|
{
|
|
|
|
DEVICE_INFO *pInfo = &Device_Info;
|
2024-11-06 22:04:41 -08:00
|
|
|
printf_usb("usb leave low power mode\r\n");
|
2024-11-06 19:58:23 -08:00
|
|
|
if (pInfo->Current_Configuration != 0)
|
|
|
|
bDeviceState = CONFIGURED;
|
|
|
|
else
|
|
|
|
bDeviceState = ATTACHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn USB_Interrupts_Config
|
|
|
|
*
|
|
|
|
* @brief Configrate USB interrupt.
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
|
|
|
void usb_intr_init(void)
|
|
|
|
{
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
EXTI_InitTypeDef EXTI_InitStructure;
|
|
|
|
|
|
|
|
EXTI_ClearITPendingBit(EXTI_Line18);
|
|
|
|
EXTI_InitStructure.EXTI_Line = EXTI_Line18;
|
|
|
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
|
|
|
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
|
|
|
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
|
|
|
EXTI_Init(&EXTI_InitStructure);
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* @fn USB_Port_Set
|
|
|
|
*
|
|
|
|
* @brief Set USB IO port.
|
|
|
|
*
|
|
|
|
* @param NewState: DISABLE or ENABLE.
|
|
|
|
* Pin_In_IPU: Enables or Disables intenal pull-up resistance.
|
|
|
|
*
|
|
|
|
* @return None
|
|
|
|
*/
|
2024-11-06 22:04:41 -08:00
|
|
|
void usb_gpio_init(FunctionalState NewState, FunctionalState Pin_In_IPU)
|
2024-11-06 19:58:23 -08:00
|
|
|
{
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
|
|
|
|
if (NewState) {
|
|
|
|
_SetCNTR(_GetCNTR()&(~(1<<1)));
|
|
|
|
GPIOA->CFGHR&=0XFFF00FFF;
|
|
|
|
GPIOA->OUTDR&=~(3<<11); //PA11/12=0
|
|
|
|
GPIOA->CFGHR|=0X00044000; //float
|
|
|
|
} else {
|
|
|
|
_SetCNTR(_GetCNTR()|(1<<1));
|
|
|
|
GPIOA->CFGHR&=0XFFF00FFF;
|
|
|
|
GPIOA->OUTDR&=~(3<<11); //PA11/12=0
|
|
|
|
GPIOA->CFGHR|=0X00033000; // LOW
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Pin_In_IPU) (EXTEN->EXTEN_CTR) |= EXTEN_USBD_PU_EN;
|
|
|
|
else (EXTEN->EXTEN_CTR) &= ~EXTEN_USBD_PU_EN;
|
|
|
|
}
|