/*
 * Copyright (c) 2010 仪星电子
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 * 著作权所有(C) 2010 仪星电子
 *
 * 本程序为自由软件；您可依据自由软件基金会所发表的GNU通用公共授权条款规定，
 * 就本程序再为发布与／或修改；无论您依据的是本授权的第二版或（您自行选择的）
 * 任一日后发行的版本。

 * 本程序是基于使用目的而加以发布，然而不负任何担保责任；亦无对适售性或特定
 * 目的适用性所为的默示性担保。详情请参照GNU通用公共授权。

 * 您应已收到附随于本程序的GNU通用公共授权的副本；如果没有，请写信至自由软件
 * 基金会：59 Temple Place - Suite 330, Boston, Ma 02111-1307, USA。

 * Email yixingdianzi@126.com
 * Email/MSN yixingdianzi@hotmail.com
 * QQ 648887464
 */

/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name          : stm32f10x_it.c
* Author             : MCD Application Team
* Version            : V3.1.0
* Date               : 10/30/2009
* Description        : Main Interrupt Service Routines.
*                      This file provides template for all exceptions handler
*                      and peripherals interrupt service routine.
********************************************************************************
* 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_it.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "usb_desc.h"						
#include "usb_pwr.h"
#include "qdmx_adc.h"

extern __IO uint32_t count_in;


uint32_t count;

uint8_t Controla_a=1;
uint8_t Controla_b=1;

extern uint8_t ADCConvertedValue[16384];

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

/******************************************************************************/
/*            Cortex-M3 Processor Exceptions Handlers                         */
/******************************************************************************/

/*******************************************************************************
* Function Name  : NMI_Handler
* Description    : This function handles NMI exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NMI_Handler(void)
{
}

/*******************************************************************************
* Function Name  : HardFault_Handler
* Description    : This function handles Hard Fault exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : MemManage_Handler
* Description    : This function handles Memory Manage exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : BusFault_Handler
* Description    : This function handles Bus Fault exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : UsageFault_Handler
* Description    : This function handles Usage Fault exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/*******************************************************************************
* Function Name  : SVC_Handler
* Description    : This function handles SVCall exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SVC_Handler(void)
{
}

/*******************************************************************************
* Function Name  : DebugMon_Handler
* Description    : This function handles Debug Monitor exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void DebugMon_Handler(void)
{
}

/*******************************************************************************
* Function Name  : PendSV_Handler
* Description    : This function handles PendSVC exception.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void PendSV_Handler(void)
{
}

/*******************************************************************************
* Function Name  : SysTick_Handler
* Description    : This function handles SysTick Handler.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SysTick_Handler(void)
{
}

/******************************************************************************/
/*            STM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/*******************************************************************************
* Function Name  : DMA1_Channel1_IRQHandler
* Description    : This function handles DMA1 Channel 1 interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void DMA1_Channel1_IRQHandler(void)
{  
	u8 i;	   
	u32 j;

	uint32_t Data;
	uint32_t Data1;
	
	uint8_t max_a,min_a;
	uint8_t max_b,min_b;

	i=64;

	max_a=0;
	min_a=0xff;
	max_b=0;
	min_b=0xff;

	ADC_Cmd(ADC1, DISABLE);
	SetEPDblBuffCount(ENDP1 , EP_DBUF_IN , i);
	if((bDeviceState == CONFIGURED))
    {	
		/*算出最大值和最小值计算放大倍数*/
		for(j=0;j<8191;j++)
		{
			if(ADCConvertedValue[j]>max_a)			max_a=ADCConvertedValue[j];
			if(ADCConvertedValue[j+1]>max_b)		max_b=ADCConvertedValue[j+1];
			if(ADCConvertedValue[j]<min_a)			min_a=ADCConvertedValue[j];
			if(ADCConvertedValue[j+1]<min_b)		min_b=ADCConvertedValue[j+1];
		}

		ADCConvertedValue[0]=0x02;
		ADCConvertedValue[1]=0x01;
		ADCConvertedValue[2]=0x02;

		switch(Controla_a)
		{
			case 1 :  
				Data=100000*(3.307/255+0.000005)/1.15;
				Data1=100000*1.5/1.15;	
			break;
			case 2 :  
				Data=100000*(3.307/255+0.000005)/2.04;
				Data1=100000*1.5/2.04;	
			break;
			case 5 :  
				Data=100000*(3.307/255+0.000005)/4.9;
				Data1=100000*1.5/4.9;
			break;
			case 10 :  
				Data=100000*(3.307/255+0.000005)/10;
				Data1=100000*1.5/10;	
			break;
			case 20 :  
				Data=100000*(3.307/255+0.000005)/20;
				Data1=100000*1.5/20;	
			break;
			default : break;
		}
	
	//	Data=100000*(3.307/255+0.000005)/Controla_a;		  					//A通道放大倍数*基准
		ADCConvertedValue[3]=(u8)(Data/16777216);
		ADCConvertedValue[4]=(u8)(Data/65536);
		ADCConvertedValue[5]=(u8)(Data/256);
		ADCConvertedValue[6]=(u8)(Data%256);
	//	Data=100000*115/Controla_a;		  					//A通道放大倍数*偏置    
		ADCConvertedValue[11]=(u8)(Data1/16777216);	 
	    ADCConvertedValue[12]=(u8)(Data1/65536);	 
	    ADCConvertedValue[13]=(u8)(Data1/256);
	    ADCConvertedValue[14]=(u8)(Data1%256);

		switch(Controla_b)
		{
			case 1 :  
				Data=100000*(3.307/255+0.000005)/1.15;
				Data1=100000*1.5/1.15;	
			break;
			case 2 :  
				Data=100000*(3.307/255+0.000005)/2.04;
				Data1=100000*1.5/2.04;	
			break;
			case 5 :  
				Data=100000*(3.307/255+0.000005)/4.9;
				Data1=100000*1.5/4.9;
			break;
			case 10 :  
				Data=100000*(3.307/255+0.000005)/10;
				Data1=100000*1.5/10;	
			break;
			case 20 :  
				Data=100000*(3.307/255+0.000005)/20;
				Data1=100000*1.5/20;	
			break;
			default : break;
		}
		
	//	Data=100000*(3.307/255+0.000005)/Controla_b;		  					//B通道放大倍数*基准
		ADCConvertedValue[7]=(u8)(Data/16777216);	 
	    ADCConvertedValue[8]=(u8)(Data/65536);	 
	    ADCConvertedValue[9]=(u8)(Data/256);
	    ADCConvertedValue[10]=(u8)(Data%256);	
		
	//	Data=100000*115/Controla_b;			  					//B通道放大倍数*偏置
		ADCConvertedValue[15]=(u8)(Data1/16777216);	 
	    ADCConvertedValue[16]=(u8)(Data1/65536);	 
	    ADCConvertedValue[17]=(u8)(Data1/256);
	    ADCConvertedValue[18]=(u8)(Data1%256);
	
	    count=16361;
		ADCConvertedValue[19]=(u8)(count/16777216);
	    ADCConvertedValue[20]=(u8)(count/65536);
	    ADCConvertedValue[21]=(u8)(count/256);
	    ADCConvertedValue[22]=(u8)(count%256);

		Control_a(max_a,min_a);
		Control_b(max_b,min_b);


	  	if (GetENDPOINT(ENDP1) & EP_DTOG_RX)
	  	{
		  FreeUserBuffer(ENDP1, EP_DBUF_IN); 
	      UserToPMABufferCopy(ADCConvertedValue, ENDP1_TX1ADDR, i);
	      SetEPDblBuf1Count(ENDP1,EP_DBUF_IN,i);
	  	}
	  	else
	  	{
	   	  FreeUserBuffer(ENDP1, EP_DBUF_IN); 
	      UserToPMABufferCopy(ADCConvertedValue, ENDP1_TX0ADDR, i);
	      SetEPDblBuf0Count(ENDP1,EP_DBUF_IN,i);
	  	}
		count_in=1;
	}  
	DMA_ClearFlag(DMA1_FLAG_TC1);
}


#ifndef STM32F10X_CL
/*******************************************************************************
* Function Name  : USB_HP_CAN1_TX_IRQHandler
* Description    : This function handles USB High Priority or CAN TX interrupts requests
*                  requests.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USB_HP_CAN1_TX_IRQHandler(void)
{
  if(bDeviceState==CONFIGURED)
      CTR_HP();
}

/*******************************************************************************
* Function Name  : USB_LP_CAN1_RX0_IRQHandler
* Description    : This function handles USB Low Priority or CAN RX0 interrupts
*                  requests.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
  USB_Istr();
}
#endif /* STM32F10X_CL */

/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
}

/*******************************************************************************
* Function Name  : USART2_IRQHandler
* Description    : This function handles USART2 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART2_IRQHandler(void)
{
}

#ifdef STM32F10X_CL
/*******************************************************************************
* Function Name  : OTG_FS_IRQHandler
* Description    : This function handles USB-On-The-Go FS global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void OTG_FS_IRQHandler(void)
{
  STM32_PCD_OTG_ISR_Handler(); 
}
#endif /* STM32F10X_CL */

/******************************************************************************/
/*                 STM32F10x Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f10x_xx.s).                                            */
/******************************************************************************/

/*******************************************************************************
* Function Name  : PPP_IRQHandler
* Description    : This function handles PPP interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
/*void PPP_IRQHandler(void)
{
}*/

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

