/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * 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.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"

/** @addtogroup STM32F10x_StdPeriph_Template
  * @{
  */

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

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

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
//void PendSV_Handler(void)
//{
//}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
volatile uint32_t TickCounter;
volatile uint32_t TickSec = 0;

void SysTick_Handler(void)
{
	OSIntEnter();
	OSTimeTick();
	OSIntExit();
  TickCounter++;
	 if(TickCounter>=0xFFFFFFFF)
        TickSec += 1;
}
uint32_t Get_SysTick(void)
{
	return TickCounter;
}
/******************************************************************************/
/*                 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).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */ 
void DMA1_Channel4_IRQHandler(void)
{
	OSIntEnter();
	
	if( DMA_GetFlagStatus(DMA1_FLAG_TC4) == SET )
	{
		USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE);
		DMA_Cmd (DMA1_Channel4, DISABLE);//USART1 TX
		DMA_ClearFlag(DMA1_FLAG_TC4);
		while(	USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
		{};
		GPIO_ResetBits(GPIOA,GPIO_Pin_8);//ENABLE 485 Receive
	}
	
	
	OSIntExit();
}


void DMA1_Channel7_IRQHandler(void)
{
	OSIntEnter();
	
	
	if( DMA_GetFlagStatus(DMA1_FLAG_TC7) == SET )
	{
		USART_DMACmd(USART2, USART_DMAReq_Tx, DISABLE);
		DMA_Cmd (DMA1_Channel7, DISABLE);//USART2 TX
		DMA_ClearFlag(DMA1_FLAG_TC7);
		while(	USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
		{};
		GPIO_ResetBits(GPIOA,GPIO_Pin_1); //ENABLE 485 Receive
	}
	
	OSIntExit();
}

//void USART1_IRQHandler(void)                 //串口1中断服务程序
//{
//	OS_ERR  err;
//	OSIntEnter();
//	
//	if(USART_GetITStatus(USART1, USART_IT_IDLE) == SET)  //检测到总线空闲
//	{
//		USART1_RxBUF.len = USATR1_RX_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);

//		OSTaskQPost ((OS_TCB   *)&AppTask_OTA_HandleTCB,                 //目标任务的控制块
//						 (void        *)USART1_RxBUF.buff,             //消息内容
//						 (OS_MSG_SIZE  )USART1_RxBUF.len,  //消息长度
//						 (OS_OPT       )OS_OPT_POST_FIFO,        //发布到任务消息队列的入口端
//						 (OS_ERR      *)&err);                   //返回错误类型

//		DMA_Cmd (DMA1_Channel5, DISABLE);
//		DMA_SetCurrDataCounter(DMA1_Channel5, USATR1_RX_SIZE);
//		DMA_Cmd (DMA1_Channel5, ENABLE);

//	USART_ClearFlag(USART1, USART_FLAG_RXNE);
//	USART_GetITStatus(USART1, USART_IT_IDLE);	//Clear IDLE Flag
//	USART_ReceiveData(USART1);								//Clear IDLE Flag
//	}
//		
//	OSIntExit();
//}


//void USART2_IRQHandler(void)
//{
//	OS_ERR  err;
//	OSIntEnter();

//	if(USART_GetITStatus(USART2, USART_IT_IDLE) == SET)
//	{
//		ErrFlag = 0;//计数清零
//		
//		USART2_RxBUF.len = USATR2_RX_SIZE - DMA_GetCurrDataCounter(DMA1_Channel6);
//		
//		OSTaskQPost ((OS_TCB   *)&AppTask_Server_ComTCB,                 //目标任务的控制块
//						 (void        *)USART2_RxBUF.buff,             //消息内容
//						 (OS_MSG_SIZE  )USART2_RxBUF.len,  //消息长度
//						 (OS_OPT       )OS_OPT_POST_FIFO,        //发布到任务消息队列的入口端
//						 (OS_ERR      *)&err);                   //返回错误类型

//		DMA_Cmd (DMA1_Channel6, DISABLE);
//		DMA_SetCurrDataCounter(DMA1_Channel6, USATR2_RX_SIZE);
//		DMA_Cmd (DMA1_Channel6, ENABLE);
//		
//	USART_ClearFlag(USART2, USART_FLAG_RXNE);
//	USART_GetITStatus(USART2, USART_IT_IDLE); //Clear IDLE Flag
//	USART_ReceiveData(USART2);							  //Clear IDLE Flag
//	}
//		
//	OSIntExit();
//}




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