/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name          : hw_config.c
* Author             : MCD Application Team
* Version            : V3.0.1
* Date               : 04/27/2009
* Description        : Hardware Configuration & Setup
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "platform_config.h"
#include "hw_config.h"
#include "usb_lib.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "stm32f10x_fsmc.h"
#include "stm32f10x_tim.h"
#include "my_defs.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : Set_System
* Description    : Configures Main system clocks & power.
* Input          : None.
* Return         : None.
*******************************************************************************/
void Set_System(void)
{
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface,  
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
  
  /* Configure the used GPIOs*/
  GPIO_Configuration();

  /* HW initialization*/
  HW_init();

  /* Configure FSMC SRAM - cs3 */ 
  External_SRAM_config(); 
  
    
}

/*******************************************************************************
* Function Name  : Set_USBClock
* Description    : Configures USB Clock input (48MHz).
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Set_USBClock(void)
{
  /* Select USBCLK source */
  RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  /* Enable USB clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
}

/*******************************************************************************
* Function Name  : Enter_LowPowerMode.
* Description    : Power-off system clocks and power while entering suspend mode.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Enter_LowPowerMode(void)
{
  /* Set the device state to suspend */
  bDeviceState = SUSPENDED;
}

/*******************************************************************************
* Function Name  : Leave_LowPowerMode.
* Description    : Restores system clocks and power while exiting suspend mode.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Leave_LowPowerMode(void)
{
  DEVICE_INFO *pInfo = &Device_Info;
  
  /* Set the device state to the correct state */
  if (pInfo->Current_Configuration != 0)
  {
    /* Device configured */
    bDeviceState = CONFIGURED;
  }
  else 
  {
    bDeviceState = ATTACHED;
  }
}

/*******************************************************************************
* Function Name  : USB_Interrupts_Config.
* Description    : Configures the USB interrupts.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_Interrupts_Config(void)
{
  NVIC_InitTypeDef NVIC_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);
  									
  /* Enable the TIM4 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

}

/*******************************************************************************
* Function Name  : USB_Cable_Config.
* Description    : Software Connection/Disconnection of USB Cable.
* Input          : NewState: new state.
* Output         : None.
* Return         : None
*******************************************************************************/
void USB_Cable_Config (FunctionalState NewState)
{ 
  if (NewState != DISABLE)
  {
    GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  }
  else
  {
    GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  }
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB  | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
  						 | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);

  // LEDs
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOG, &GPIO_InitStructure);

  /* USB_DISCONNECT used as USB pull-up */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;	   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  GPIO_Init(GPIOC, &GPIO_InitStructure); 

  /* Output Voltage pin - OE1*/
  GPIO_InitStructure.GPIO_Pin = OE1_pin;	   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(OE1_port, &GPIO_InitStructure);
  /* Output Voltage pin - OE1*/
  GPIO_InitStructure.GPIO_Pin = OE2_pin;	   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(OE2_port, &GPIO_InitStructure);

  /* Digital data output */
  /* data port */
  GPIO_InitStructure.GPIO_Pin = PORTB_DD;	   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure); 	   
  /* control lines - D1/2 OE and DIR*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;	   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);   
  /* Output Voltage pin - DVCCB*/
  GPIO_InitStructure.GPIO_Pin = D_VCCB_pin;	   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(D_VCCB_port, &GPIO_InitStructure);
    
  /* Configure the FSMC alternative function pins */
  /* SRAM Data lines configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_9 |
                                GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure); 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
                                GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | 
                                GPIO_Pin_15;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  
  /* SRAM Address lines configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | 
                                GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_12 | GPIO_Pin_13 | 
                                GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | 
                                GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_Init(GPIOG, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; 
  GPIO_Init(GPIOD, &GPIO_InitStructure);
   
  /* NOE and NWE configuration */  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 |GPIO_Pin_5;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  
  /* NE3 configuration - SRAM*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
  GPIO_Init(GPIOG, &GPIO_InitStructure);
  
  /* NBL0, NBL1 configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; 
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  /* NE1 configuration - DA1 convertor*/
  GPIO_InitStructure.GPIO_Pin = CS_DA1_pin;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
  GPIO_Init(CS_DA1_port, &GPIO_InitStructure);
  /* NE2 configuration - DA2 convertor*/
  GPIO_InitStructure.GPIO_Pin = CS_DA2_pin;
  GPIO_Init(CS_DA2_port, &GPIO_InitStructure);
  
}

/*******************************************************************************
* Function Name  : Get_SerialNum.
* Description    : Create the serial number string descriptor.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Get_SerialNum(void)
{
  uint32_t Device_Serial0, Device_Serial1, Device_Serial2;
  
  Device_Serial0 = *(uint32_t*)(0x1FFFF7E8);
  Device_Serial1 = *(uint32_t*)(0x1FFFF7EC);
  Device_Serial2 = *(uint32_t*)(0x1FFFF7F0);
  
  if(Device_Serial0 != 0)
  {
     CustomHID_StringSerial[2] = (uint8_t)(Device_Serial0 & 0x000000FF);  
     CustomHID_StringSerial[4] = (uint8_t)((Device_Serial0 & 0x0000FF00) >> 8);
     CustomHID_StringSerial[6] = (uint8_t)((Device_Serial0 & 0x00FF0000) >> 16);
     CustomHID_StringSerial[8] = (uint8_t)((Device_Serial0 & 0xFF000000) >> 24);  
     
     CustomHID_StringSerial[10] = (uint8_t)(Device_Serial1 & 0x000000FF);  
     CustomHID_StringSerial[12] = (uint8_t)((Device_Serial1 & 0x0000FF00) >> 8);
     CustomHID_StringSerial[14] = (uint8_t)((Device_Serial1 & 0x00FF0000) >> 16);
     CustomHID_StringSerial[16] = (uint8_t)((Device_Serial1 & 0xFF000000) >> 24); 
     
     CustomHID_StringSerial[18] = (uint8_t)(Device_Serial2 & 0x000000FF);  
     CustomHID_StringSerial[20] = (uint8_t)((Device_Serial2 & 0x0000FF00) >> 8);
     CustomHID_StringSerial[22] = (uint8_t)((Device_Serial2 & 0x00FF0000) >> 16);
     CustomHID_StringSerial[24] = (uint8_t)((Device_Serial2 & 0xFF000000) >> 24); 
  }   
}
/*******************************************************************************
* Function Name  : External_SRAM_config
* Description    : Configuration of FSMC bus - bank 1, SRAM on cs3
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void External_SRAM_config(void)
{
  FSMC_NORSRAMInitTypeDef		FSMC_control;
  FSMC_NORSRAMTimingInitTypeDef FSMC_timing;
// enable AHB peripheral clock:
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
// timing config:
  FSMC_timing.FSMC_AddressSetupTime = 3;
  FSMC_timing.FSMC_AddressHoldTime = 3;
  FSMC_timing.FSMC_DataSetupTime = 2;
  FSMC_timing.FSMC_BusTurnAroundDuration = 0;
  FSMC_timing.FSMC_CLKDivision = 0;
  FSMC_timing.FSMC_DataLatency = 0;
  FSMC_timing.FSMC_AccessMode = FSMC_AccessMode_A;
// bus control config
  FSMC_control.FSMC_Bank = FSMC_Bank1_NORSRAM3;
  FSMC_control.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
  FSMC_control.FSMC_MemoryType = FSMC_MemoryType_SRAM;
  FSMC_control.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
  FSMC_control.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  FSMC_control.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  FSMC_control.FSMC_WrapMode = FSMC_WrapMode_Disable;
  FSMC_control.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  FSMC_control.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  FSMC_control.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
  FSMC_control.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  FSMC_control.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  FSMC_control.FSMC_ReadWriteTimingStruct = &FSMC_timing;
  FSMC_control.FSMC_WriteTimingStruct = &FSMC_timing;
// memory initialization:
  FSMC_NORSRAMInit(&FSMC_control); 
// memory bus enable
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE); 
}

/*******************************************************************************
* Function Name  : Timer configuration
* Description    : Configuration of timer - 
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Timer_config(void)
{ 
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
  TIM_TimeBaseStructure.TIM_Prescaler = 0x04FF;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  
  TIM_UpdateRequestConfig(TIM4, TIM_UpdateSource_Regular);
  /* TIM IT enable */
  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
  /* TIM2 enable counter */
  TIM_Cmd(TIM4, ENABLE);

}
/*******************************************************************************
* Function Name  : Startup hardware configuration
* Description    : Configure hw
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void HW_init(void)
{
	// set level of digital channes to 3.3V  
  GPIO_WriteBit(D_VCCB_port, D_VCCB_pin, Bit_SET);
    // set direction to OUTPUT
  GPIO_WriteBit(D1_DIR_port, D1_DIR_pin, Bit_SET);
  GPIO_WriteBit(D2_DIR_port, D2_DIR_pin, Bit_SET);
    // set Digital output to HIGH Z
  GPIO_WriteBit(D1_OE_port, D1_OE_pin, Bit_RESET);
  GPIO_WriteBit(D2_OE_port, D2_OE_pin, Bit_RESET);
    // Disconect Analog ouputs
  GPIO_WriteBit(OE1_port, OE1_pin, Bit_SET);
  GPIO_WriteBit(OE2_port, OE2_pin, Bit_SET);
      // DA1,2 CS vypnout
  GPIO_WriteBit(CS_DA1_port, CS_DA1_pin, Bit_RESET);
  GPIO_WriteBit(CS_DA2_port, CS_DA2_pin, Bit_RESET);
    // Turn of LEDS 1-3
  GPIO_WriteBit(LED1_port, LED1_pin, Bit_RESET);
  GPIO_WriteBit(LED2_port, LED2_pin, Bit_RESET);
  GPIO_WriteBit(LED3_port, LED3_pin, Bit_RESET);
  


}
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
