Guest User

Untitled

a guest
Mar 20th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.95 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    hw_config.c
  4.   * @author  MCD Application Team
  5.   * @version V1.1.0
  6.   * @date    20-September-2012
  7.   * @brief   Hardware Configuration & Setup.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */
  27.  
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "stm32f30x.h"
  30. #include "hw_config.h"
  31. #include "usb_lib.h"
  32. #include "usb_desc.h"
  33. #include "usb_pwr.h"
  34. #include "usb_lib.h"
  35. #include "platform_config.h"
  36. #include "stm32f3_discovery.h"
  37.  
  38. /** @addtogroup STM32F3-Discovery_Demo
  39.   * @{
  40.   */
  41.  
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. /* Private macro -------------------------------------------------------------*/
  45. /* Private variables ---------------------------------------------------------*/
  46. ErrorStatus HSEStartUpStatus;
  47. EXTI_InitTypeDef EXTI_InitStructure;
  48. extern __IO uint32_t packet_sent;
  49. extern __IO uint8_t Send_Buffer[VIRTUAL_COM_PORT_DATA_SIZE] ;
  50. extern __IO  uint32_t packet_receive;
  51. extern __IO uint8_t Receive_length;
  52.  
  53. uint8_t Receive_Buffer[64];
  54. uint32_t Send_length;
  55. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  56. /* Extern variables ----------------------------------------------------------*/
  57. //extern __IO uint8_t PrevXferComplete;
  58.  
  59. /* Private function prototypes -----------------------------------------------*/
  60. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
  61. /* Private functions ---------------------------------------------------------*/
  62.  
  63. /**
  64.   * @brief  Configures Main system clocks & power.
  65.   * @param  None
  66.   * @retval None
  67.   */
  68.  
  69. //#define USB_USE_EXTERNAL_PULLUP
  70. void Set_System(void)
  71. {
  72.   GPIO_InitTypeDef  GPIO_InitStructure;
  73.  
  74.   /*!< At this stage the microcontroller clock setting is already configured,
  75.        this is done through SystemInit() function which is called from startup
  76.        file (startup_stm32f30x.s) before to branch to application main.
  77.        To reconfigure the default setting of SystemInit() function, refer to
  78.        system_stm32f30x.c file
  79.      */
  80.  
  81.  /* Enable the PWR clock */
  82.  // RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  83.  
  84.   /* Enable the SYSCFG module clock (used for the USB disconnect feature) */
  85.  
  86.  // RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  87.  
  88.   /* Enable the USB disconnect GPIO clock */
  89.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIO_DISCONNECT, ENABLE);
  90.  
  91.  /*Set PA11,12 as IN - USB_DM,DP*/
  92.  
  93.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  94.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
  95.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  96.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  97.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  98.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  99.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  100.  
  101.  
  102.   /*SET PA11,12 for USB: USB_DM,DP*/
  103.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_14);
  104.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_14);
  105.  
  106.  
  107. #if defined(USB_USE_EXTERNAL_PULLUP)
  108.   /* Enable the USB disconnect GPIO clock */
  109.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIO_DISCONNECT, ENABLE);
  110.  
  111.   /* USB_DISCONNECT used as USB pull-up */
  112.   GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
  113.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  114.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  115.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  116.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  117.   GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);
  118. #endif /* USB_USE_EXTERNAL_PULLUP */
  119.  
  120.   // Configure the Key button in EXTI mode
  121.   //STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
  122.  
  123.   // Configure the EXTI line 18 connected internally to the USB IP
  124.   EXTI_ClearITPendingBit(EXTI_Line18);
  125.   EXTI_InitStructure.EXTI_Line = EXTI_Line18; /*USB resume from suspend mode*/
  126.   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  127.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  128.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  129.   EXTI_Init(&EXTI_InitStructure);
  130.  
  131.   //EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);
  132. }
  133.  
  134. /**
  135.   * @brief  Configures USB Clock input (48MHz).
  136.   * @param  None
  137.   * @retval None
  138.   */
  139. void Set_USBClock(void)
  140. {
  141.   /* USBCLK = PLLCLK = 48 MHz */
  142.   RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  143.  
  144.   /* Enable USB clock */
  145.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  146. }
  147.  
  148. /**
  149.   * @brief  Configures all IOs as AIN to reduce the power consumption.
  150.   * @param  None
  151.   * @retval None
  152.   */
  153. void GPIO_AINConfig(void)
  154. {
  155.   GPIO_InitTypeDef GPIO_InitStructure;
  156.  
  157.   /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  158.  
  159.   /* Enable all GPIOs Clock*/
  160.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ALLGPIO, ENABLE);
  161.  
  162.  /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  163.  
  164.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  165.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  166.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  167.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  168.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  169.  
  170.   /* Disable all GPIOs Clock*/
  171.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ALLGPIO, DISABLE);
  172.  
  173. }
  174.  
  175. /**
  176.   * @brief  Power-off system clocks and power while entering suspend mode.
  177.   * @param  None
  178.   * @retval None
  179.   */
  180. void Enter_LowPowerMode(void)
  181. {
  182.   /* Set the device state to suspend */
  183.   bDeviceState = SUSPENDED;
  184.  
  185.   /* Clear EXTI Line18 pending bit */
  186.   EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);
  187. }
  188.  
  189. /**
  190.   * @brief  Restores system clocks and power while exiting suspend mode.
  191.   * @param  None
  192.   * @retval None
  193.   */
  194. void Leave_LowPowerMode(void)
  195. {
  196.   DEVICE_INFO *pInfo = &Device_Info;
  197.  
  198.   /* Enable HSE */
  199.   RCC_HSEConfig(RCC_HSE_ON);
  200.  
  201.   /* Wait till HSE is ready */
  202.   HSEStartUpStatus = RCC_WaitForHSEStartUp();
  203.  
  204.  
  205.   /* Wait till HSE is ready */
  206.   while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
  207.   {}
  208.  
  209.   /* Enable PLL1 */
  210.   RCC_PLLCmd(ENABLE);
  211.  
  212.   /* Wait till PLL1 is ready */
  213.   while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  214.   {}
  215.  
  216.   /* Select PLL as system clock source */
  217.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  218.  
  219.   /* Wait till PLL is used as system clock source */
  220.   while (RCC_GetSYSCLKSource() != 0x08)
  221.   {}
  222.  
  223.  /* Set the device state to the correct state */
  224.   if (pInfo->Current_Configuration != 0)
  225.   {
  226.     /* Device configured */
  227.     bDeviceState = CONFIGURED;
  228.   }
  229.   else
  230.   {
  231.     bDeviceState = ATTACHED;
  232.   }
  233. }
  234.  
  235. /**
  236.   * @brief  Configures the USB interrupts.
  237.   * @param  None
  238.   * @retval None
  239.   */
  240.  
  241.   #define USB_INT_DEFAULT
  242. void USB_Interrupts_Config(void)
  243. {
  244.   NVIC_InitTypeDef NVIC_InitStructure;
  245.  
  246.   /* 2 bit for pre-emption priority, 2 bits for subpriority */
  247.   //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  248.   /* Enable the USB interrupt */
  249. #if defined (USB_INT_DEFAULT)
  250.   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  251. #endif
  252. #if defined (USB_INT_REMAP)
  253.   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn;
  254. #endif
  255.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
  256.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  257.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  258.   NVIC_Init(&NVIC_InitStructure);
  259.  
  260.   /* Enable the USB Wake-up interrupt */
  261. #if defined (USB_INT_DEFAULT)
  262.   NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
  263. #endif
  264. #if defined (USB_INT_REMAP)
  265.   NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_RMP_IRQn;
  266. #endif
  267.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
  268.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  269.   NVIC_Init(&NVIC_InitStructure);
  270. /*
  271.   // Enable the Key EXTI line Interrupt
  272.   NVIC_InitStructure.NVIC_IRQChannel = USER_BUTTON_EXTI_IRQn;
  273.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  274.   NVIC_Init(&NVIC_InitStructure);
  275.   */
  276. }
  277.  
  278. /**
  279.   * @brief  Software Connection/Disconnection of USB Cable.
  280.   * @param  None
  281.   * @retval None
  282.   */
  283. void USB_Cable_Config (FunctionalState NewState)
  284. {
  285.   if (NewState != DISABLE)
  286.   {
  287.     GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  288.   }
  289.   else
  290.   {
  291.     GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  292.   }
  293. }
  294.  
  295. /**
  296.   * @brief  Decodes the Joystick direction.
  297.   * @param  None
  298.   * @retval The direction value.
  299.   */
  300. uint8_t JoyState(void)
  301. {
  302.   return 0;
  303.  
  304. }
  305.  
  306. /**
  307.   * @brief  Prepares buffer to be sent containing Joystick event infos.
  308.   * @param  Keys: keys received from terminal.
  309.   * @retval None
  310.   */
  311. void Joystick_Send(uint8_t Keys)
  312. {
  313.  
  314. }
  315.  
  316. /**
  317.   * @brief  Create the serial number string descriptor.
  318.   * @param  None.
  319.   * @retval None
  320.   */
  321. void Get_SerialNum(void)
  322. {
  323.   uint32_t Device_Serial0, Device_Serial1, Device_Serial2;
  324.  
  325.   Device_Serial0 = *(uint32_t*)ID1;
  326.   Device_Serial1 = *(uint32_t*)ID2;
  327.   Device_Serial2 = *(uint32_t*)ID3;
  328.  
  329.   Device_Serial0 += Device_Serial2;
  330.  
  331.   if (Device_Serial0 != 0)
  332.   {
  333.     IntToUnicode (Device_Serial0, &Virtual_Com_Port_StringSerial[2] , 8);
  334.     IntToUnicode (Device_Serial1, &Virtual_Com_Port_StringSerial[18], 4);
  335.   }
  336. }
  337.  
  338. /**
  339.   * @brief  Convert Hex 32Bits value into char.
  340.   * @param  value: Data to be converted.
  341.   * @param  pbuf: pointer to buffer.
  342.   * @param  len: Data length.
  343.   * @retval None
  344.   */
  345. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  346. {
  347.   uint8_t idx = 0;
  348.  
  349.   for( idx = 0 ; idx < len ; idx ++)
  350.   {
  351.     if( ((value >> 28)) < 0xA )
  352.     {
  353.       pbuf[ 2* idx] = (value >> 28) + '0';
  354.     }
  355.     else
  356.     {
  357.       pbuf[2* idx] = (value >> 28) + 'A' - 10;
  358.     }
  359.  
  360.     value = value << 4;
  361.  
  362.     pbuf[ 2* idx + 1] = 0;
  363.   }
  364. }
  365.  
  366. /*******************************************************************************
  367. * Function Name  : Send DATA .
  368. * Description    : send the data received from the STM32 to the PC through USB
  369. * Input          : None.
  370. * Output         : None.
  371. * Return         : None.
  372. *******************************************************************************/
  373. uint32_t CDC_Send_DATA (uint8_t *ptrBuffer, uint8_t Send_length)
  374. {
  375.   /*if max buffer is Not reached*/
  376.   if(Send_length < VIRTUAL_COM_PORT_DATA_SIZE)
  377.   {
  378.     /*Sent flag*/
  379.     packet_sent = 0;
  380.     /* send  packet to PMA*/
  381.     UserToPMABufferCopy((unsigned char*)ptrBuffer, ENDP1_TXADDR, Send_length);
  382.     SetEPTxCount(ENDP1, Send_length);
  383.     SetEPTxValid(ENDP1);
  384.   }
  385.   else
  386.   {
  387.     return 0;
  388.   }
  389.   return 1;
  390. }
  391.  
  392. /*******************************************************************************
  393. * Function Name  : Receive DATA .
  394. * Description    : receive the data from the PC to STM32 and send it through USB
  395. * Input          : None.
  396. * Output         : None.
  397. * Return         : None.
  398. *******************************************************************************/
  399. uint32_t CDC_Receive_DATA(void)
  400. {
  401.   /*Receive flag*/
  402.   packet_receive = 0;
  403.   SetEPRxValid(ENDP3);
  404.   return 1 ;
  405. }
  406.  
  407. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  408.  
  409.  
  410.  
  411.  
  412.  
  413. //  #define USB_INT_DEFAULT
  414. //void USB_Interrupts_Config(void)
  415. //{
  416. //  NVIC_InitTypeDef NVIC_InitStructure;
  417. //
  418. //  /* 2 bit for pre-emption priority, 2 bits for subpriority */
  419. //  //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  420. //
  421. //  /* Enable the USB interrupt */
  422. //#if defined (USB_INT_DEFAULT)
  423. //  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  424. //#endif
  425. //#if defined (USB_INT_REMAP)
  426. //  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQn;
  427. //#endif
  428. //  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
  429. //  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  430. //  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  431. //  NVIC_Init(&NVIC_InitStructure);
  432. //
  433. //  /* Enable the USB Wake-up interrupt */
  434. //#if defined (USB_INT_DEFAULT)
  435. //  NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
  436. //#endif
  437. //#if defined (USB_INT_REMAP)
  438. //  NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_RMP_IRQn;
  439. //#endif
  440. //  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
  441. //  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  442. //  NVIC_Init(&NVIC_InitStructure);
  443. ///*
  444. //  // Enable the Key EXTI line Interrupt
  445. //  NVIC_InitStructure.NVIC_IRQChannel = USER_BUTTON_EXTI_IRQn;
  446. //  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  447. //  NVIC_Init(&NVIC_InitStructure);
  448. //  */
  449. //}
Advertisement
Add Comment
Please, Sign In to add comment