/******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
* File Name          : usb_endp.c
* Author             : MCD Application Team
* Version            : V3.0.1
* Date               : 04/27/2009
* Description        : Endpoint routines
********************************************************************************
* 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 "platform_config.h"
#include "stm32f10x.h"
#include "usb_lib.h"
#include "usb_istr.h"
#include "stm32f10x_tim.h"
#include "my_defs.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t Receive_Buffer[Rx_Buffer_Length];
uint8_t Send_Buffer2[Tx_Buffer_Length];
uint16_t i = 0;
uint32_t AOUT1_StartAddress;
uint32_t AOUT1_StopAddress;
uint32_t AOUT2_StartAddress;
uint32_t AOUT2_StopAddress;
uint32_t DOUT_StartAddress;
uint32_t DOUT_StopAddress;
extern uint32_t ReadAddress1;
extern uint32_t ReadAddress2;
extern uint32_t ReadAddress3;
BitAction SettingBit;
FunctionalState EnableBit;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : EP1_OUT_Callback.
* Description    : EP1 OUT Callback Routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/

void EP1_OUT_Callback(void)
{
  uint16_t temp = 0x0000;
  uint32_t ReceivedAddress = 0x00000000;
  uint16_t ReceivedData = 0x0000;
  uint16_t DA_data = 0x0000;
  
  PMAToUserBufferCopy(Receive_Buffer, ENDP1_RXADDR, Rx_Buffer_Length);
  
  switch (Receive_Buffer[0])
  { 
/******************************** OUTREPORT ID 0x01 ***************************/
	case 0x01:
		switch (Receive_Buffer[1])
		{
		//*********************** control lines setups *************************
		  case 0x01:						// setting OE1 
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_RESET;
			}
			else
			{	
				SettingBit = Bit_SET;
			}
		    GPIO_WriteBit(OE1_port,OE1_pin,SettingBit);
		  break;
		  case 0x02:						// setting OE2 
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_RESET;
			}
			else
			{	
				SettingBit = Bit_SET;
			}
		    GPIO_WriteBit(OE2_port,OE2_pin,SettingBit);
		  break;
		  case 0x03:						// settnig DOUT1 OE
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_SET;
			}
			else
			{	
				SettingBit = Bit_RESET;
			}
		    GPIO_WriteBit(D1_OE_port,D1_OE_pin,SettingBit);
		  break;
		  case 0x04:						// setting DOUT2 OE 
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_SET;
			}
			else
			{	
				SettingBit = Bit_RESET;
			}
		    GPIO_WriteBit(D2_OE_port,D2_OE_pin,SettingBit);
		  break;
		  case 0x07:						// setting VCCB
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_SET;
			}
			else
			{	
				SettingBit = Bit_RESET;
			}
		    GPIO_WriteBit(D_VCCB_port,D_VCCB_pin,SettingBit);
		  break;
		  case 0x08:						// Timer start/stop
		    if(Receive_Buffer[2] == 0x01)
			{
				EnableBit = ENABLE;
			}
			else
			{	
				EnableBit = DISABLE;
			}

  			TIM_Cmd(TIM4, EnableBit);

		  break;
		  case 0x09:						// Sisconnect
			
			GPIO_WriteBit(USB_dis_port,USB_dis_pin, Bit_SET);

		  break;
		  case 0x0A:						// setting D1DIR
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_SET;
			}
			else
			{	
				SettingBit = Bit_RESET;
			}
		    GPIO_WriteBit(D1_DIR_port, D1_DIR_pin, SettingBit);
		  break;
		  case 0x0B:						// setting D2DIR
		    if(Receive_Buffer[2] == 0x01)
			{
				SettingBit = Bit_SET;
			}
			else
			{	
				SettingBit = Bit_RESET;
			}
		    GPIO_WriteBit(D2_DIR_port, D2_DIR_pin, SettingBit);
		  break;
		}

    break;
/******************************** OUTREPORT ID 0x03 - 64B **********************/
	case 0x03:
		switch (Receive_Buffer[1])
		{
		  case 0x01:				// Receive data, load to memory
			ReceivedAddress |= Receive_Buffer[4]; 
	   		ReceivedAddress = (ReceivedAddress << 8);
			ReceivedAddress |= Receive_Buffer[3];
			ReceivedAddress = (ReceivedAddress << 8);
			ReceivedAddress |= Receive_Buffer[2];  

			for(i = 0;i < 58 ; (i = i + 2))
			{
				ReceivedData = 0x0000;
				ReceivedData |= Receive_Buffer[i+6];
				ReceivedData = (ReceivedData << 8);
				ReceivedData |= Receive_Buffer[i+5];  
		    	*(u16 *) (0x68000000 + ReceivedAddress + i) = ReceivedData;
			}
			// data received ack
			Send_Buffer2[0] = 0x02;
			Send_Buffer2[1] = 0x01;
		    UserToPMABufferCopy(Send_Buffer2, ENDP1_TXADDR,64);
		    SetEPTxCount(ENDP1, 64);
		    SetEPTxValid(ENDP1);
		  break;
			
		  case 0x02:				// Read 2*29B from memory from starting address
			ReceivedAddress |= Receive_Buffer[4]; 
	   		ReceivedAddress = (ReceivedAddress << 8);
			ReceivedAddress |= Receive_Buffer[3];
			ReceivedAddress = (ReceivedAddress << 8);
			ReceivedAddress |= Receive_Buffer[2];  
		
			Send_Buffer2[0] = 0x04;
			for(i=0; i < 29;i++)
			{
				temp = *(u16 *) (0x68000000 + ReceivedAddress + 2*i);
				Send_Buffer2[2*i+1] = temp;
				temp = (temp >> 8);
				Send_Buffer2[2*i+2] = temp;
			}
		    UserToPMABufferCopy(Send_Buffer2, ENDP1_TXADDR,Tx_Buffer_Length);
		    SetEPTxCount(ENDP1, Tx_Buffer_Length);
		    SetEPTxValid(ENDP1);
		  break;

		  case 0x03:				// Timer update
			temp = 0;
		  	temp |= Receive_Buffer[3];
			temp = (temp << 8);
			temp |= Receive_Buffer[2];  
			TIM_SetAutoreload(TIM4,temp); 
		    temp = 0;
			temp |= Receive_Buffer[5];
			temp = (temp << 8);
			temp |= Receive_Buffer[4];
			
			TIM_PrescalerConfig(TIM4, temp, TIM_PSCReloadMode_Update);
		   	TIM_UpdateRequestConfig(TIM4, TIM_UpdateSource_Regular);
			TIM_GenerateEvent(TIM4, TIM_EventSource_Update);
			// data received ack
			Send_Buffer2[0] = 0x02;
			Send_Buffer2[1] = 0x03;
		    UserToPMABufferCopy(Send_Buffer2, ENDP1_TXADDR,64);
		    SetEPTxCount(ENDP1, 64);
		    SetEPTxValid(ENDP1);
		  break;

		  case 0x04:				// get starting and stop addreses for generation
			AOUT1_StartAddress = 0x00000000;
			AOUT1_StartAddress |= Receive_Buffer[4]; 
	   		AOUT1_StartAddress = (AOUT1_StartAddress << 8);
			AOUT1_StartAddress |= Receive_Buffer[3];
			AOUT1_StartAddress = (AOUT1_StartAddress << 8);
			AOUT1_StartAddress |= Receive_Buffer[2];  
			AOUT1_StopAddress = 0x00000000;
			AOUT1_StopAddress |= Receive_Buffer[7]; 
	   		AOUT1_StopAddress = (AOUT1_StopAddress << 8);
			AOUT1_StopAddress |= Receive_Buffer[6];
			AOUT1_StopAddress = (AOUT1_StopAddress << 8);
			AOUT1_StopAddress |= Receive_Buffer[5]; 
			AOUT1_StopAddress = AOUT1_StopAddress -2;
			ReadAddress1 = AOUT1_StartAddress;
	
 			AOUT2_StartAddress = 0x00000000;
			AOUT2_StartAddress |= Receive_Buffer[10]; 
	   		AOUT2_StartAddress = (AOUT2_StartAddress << 8);
			AOUT2_StartAddress |= Receive_Buffer[9];
			AOUT2_StartAddress = (AOUT2_StartAddress << 8);
			AOUT2_StartAddress |= Receive_Buffer[8]; 
			AOUT2_StopAddress = 0x00000000;
			AOUT2_StopAddress |= Receive_Buffer[13]; 
	   		AOUT2_StopAddress = (AOUT2_StopAddress << 8);
			AOUT2_StopAddress |= Receive_Buffer[12];
			AOUT2_StopAddress = (AOUT2_StopAddress << 8);
			AOUT2_StopAddress |= Receive_Buffer[11]; 
			AOUT2_StopAddress = AOUT1_StopAddress -2;
			ReadAddress2 = AOUT2_StartAddress;
	
			DOUT_StartAddress = 0x00000000;
			DOUT_StartAddress |= Receive_Buffer[16]; 
	   		DOUT_StartAddress = (DOUT_StartAddress << 8);
			DOUT_StartAddress |= Receive_Buffer[15];
			DOUT_StartAddress = (DOUT_StartAddress << 8);
			DOUT_StartAddress |= Receive_Buffer[14]; 
			DOUT_StopAddress = 0x00000000;
			DOUT_StopAddress |= Receive_Buffer[19]; 
	   		DOUT_StopAddress = (DOUT_StopAddress << 8);
			DOUT_StopAddress |= Receive_Buffer[18];
			DOUT_StopAddress = (DOUT_StopAddress << 8);
			DOUT_StopAddress |= Receive_Buffer[17]; 
			DOUT_StopAddress = DOUT_StopAddress-2;
			ReadAddress3 = DOUT_StartAddress;

			// Addresses received ack
			Send_Buffer2[0] = 0x02;
			Send_Buffer2[1] = 0x02;
		    UserToPMABufferCopy(Send_Buffer2, ENDP1_TXADDR,64);
		    SetEPTxCount(ENDP1, 64);
		    SetEPTxValid(ENDP1);
		  break;

		  case 0x05:			 	// Direct setting of Digital Data lines
			DA_data |= Receive_Buffer[3];
			DA_data = (DA_data << 8);
			DA_data |= Receive_Buffer[2];  

			GPIO_Write(Digital_data_port, DA_data);
			// Addresses received ack
			Send_Buffer2[0] = 0x02;
			Send_Buffer2[1] = 0xEA;
		    UserToPMABufferCopy(Send_Buffer2, ENDP1_TXADDR,64);
		    SetEPTxCount(ENDP1, 64);
		    SetEPTxValid(ENDP1);
		  break;
		}
 	break;
/******************************************************************************/
    default:
    break;
  }
  SetEPRxStatus(ENDP1, EP_RX_VALID);
}

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

