#include "stm32f1xx_hal.h"

#include "com_usart_hal.h"
#include "herramienta_hal.h"
#include <string.h>

#include "main.h"

UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;

__IO ITStatus UartReady = RESET;
//uint8_t *DataRx;
//uint8_t *DataTx;

uint8_t volatile DataRx[tamRx];
uint8_t DataTx[tamTx];

uint8_t CAPA1=0;
osSemaphoreId Sem2_Rx;
//osPoolId PoolRx,PoolTx;

void  UART_Init(void){
	
	__HAL_RCC_USART1_CLK_ENABLE();//activa periferico usart1
	
  huart1.Instance = USART1;
  huart1.Init.BaudRate = baudioCom;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

//__HAL_UNLOCK(&huart1);
	
	

	//__HAL_DMA_ENABLE_IT(&hdma_usart1_rx, DMA_IT_TC);
	
	//huart1.Lock=HAL_UNLOCKED;
	
	 if (HAL_UART_Receive_DMA(&huart1, (uint8_t *)DataRx, 5) != HAL_OK) {
     Error_Handler();
	 }
	
}

void HAL_UART_MspInit(UART_HandleTypeDef* huart){

  GPIO_InitTypeDef GPIO_InitStruct;
  if(huart->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();
  
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
		
		//PIN RS485 para transminir.....
		
		GPIO_InitStruct.Pin=rs485_PinTx;
		GPIO_InitStruct.Mode =GPIO_MODE_OUTPUT_PP;
		GPIO_InitStruct.Pull =GPIO_PULLDOWN;
		HAL_GPIO_Init(rs485_Port,&GPIO_InitStruct);

    /* Peripheral DMA init*/
  
    hdma_usart1_rx.Instance = DMA1_Channel5;
    hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
    hdma_usart1_rx.Init.Priority = DMA_PRIORITY_HIGH;
    if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
    {
      Error_Handler();
    }
    __HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);

		
		
    hdma_usart1_tx.Instance = DMA1_Channel4;
    hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart1_tx.Init.Mode = DMA_NORMAL;
    hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
    {
      Error_Handler();
    }
    __HAL_LINKDMA(huart,hdmatx,hdma_usart1_tx);
		
		

  /* USER CODE BEGIN USART1_MspInit 1 */
	
/*
	if (HAL_DMA_Start_IT(&hdma_usart1_rx,huart->Instance->DR,(uint32_t)DataRx,5)!=HAL_OK){
		Error_Handler();
	}
	SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
*/
		/*
	 if (HAL_UART_Receive_DMA(&huart1, (uint8_t *)DataRx, 5) != HAL_OK) {
    
    Error_Handler();
  
		*/
//__HAL_DMA_ENABLE_IT(&hdma_usart1_rx, DMA_IT_TC);

  /* USER CODE END USART1_MspInit 1 */
  }

}

void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{

  if(huart->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();
  
    /**USART1 GPIO Configuration    
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX 
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* Peripheral DMA DeInit*/
    HAL_DMA_DeInit(huart->hdmarx);
    HAL_DMA_DeInit(huart->hdmatx);
  }
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */

}

void Escuchar_DMA_RX(uint16_t rx_tam){
	
	//HAL_DMA_DeInit(huart->hdmarx);
	
	
}

void Enviar_DMA_TX(uint8_t *buffer,uint16_t tx_tam){
	
		if (HAL_UART_Transmit_DMA(&huart1,buffer,tx_tam) != HAL_OK) {
        Error_Handler();
		}
		while(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC)!=SET);
	
		huart1.State=HAL_UART_STATE_READY;
	
}

/*
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart){
  Error_Handler();
}
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart){
  
	//__HAL_DMA_CLEAR_FLAG(huart1.hdmatx,DMA_FLAG_TC4);
	
  UartReady = SET;
	//HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_12);
	if(huart->Instance==USART1){
		
	//	__HAL_UART_CLEAR_FLAG(&huart1,UART_FLAG_TC);
		
		
		
	}
	
	
}

/*  la nuevaa  ojojo
void  HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	Cap_Data cap;
	//uint16_t vcon=0;
	DesModularMem(&cap,(uint8_t*)DataRx,sizeof(cap),0);
	
	if(cap.cap.Direccion==DIR && cap.cap.Tamdatas>0){
		//	CAPA1=1;
		huart1.State=HAL_UART_STATE_READY;
		if (HAL_UART_Receive_DMA(&huart1, (uint8_t *)DataRx, Multiplo(5+cap.cap.Tamdatas+5,5)) != HAL_OK) {
			Error_Handler();
		}
	
	}
			
	else if(cap.cap.Direccion==DIR && cap.cap.Tamdatas==0){
			osSemaphoreRelease(Sem2_Rx);
	}
 		
	else{
		//CAPA1=0;
		
		//osSemaphoreRelease(Sem2_Rx);
		return;
		
	}
	
	
}
*/

// esta es la original
void  HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	Cap_Data cap;
	//uint16_t vcon=0;
	if(CAPA1==0){
		//huart1.State=HAL_UART_STATE_READY;
		DesModularMem(&cap,(uint8_t*)DataRx,sizeof(cap),0);
		if(cap.cap.Tamdatas>0){
			CAPA1=1;
			huart1.State=HAL_UART_STATE_READY;
			if (HAL_UART_Receive_DMA(&huart1, (uint8_t *)DataRx, Multiplo(5+cap.cap.Tamdatas+5,5)) != HAL_OK) {
				Error_Handler();
			}
			return;
		}
		else{
			osSemaphoreRelease(Sem2_Rx);
		}
  }
		
	else{
		CAPA1=0;
		
		osSemaphoreRelease(Sem2_Rx);
		
	}
	
	
}

/* version errada 1
void  HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	Cap_Data cap;
	//uint16_t vcon=0;
	if(CAPA1==0){
		//huart1.State=HAL_UART_STATE_READY;
		DesModularMem(&cap,(uint8_t*)DataRx,sizeof(cap),0);
		if(cap.cap.Direccion==DIR&&cap.cap.Tamdatas>0){
			CAPA1=1;
			huart1.State=HAL_UART_STATE_READY;
			if (HAL_UART_Receive_DMA(&huart1, (uint8_t *)DataRx, Multiplo(5+cap.cap.Tamdatas+5,5)) != HAL_OK) {
				Error_Handler();
			}
			return;
		}
		else{
			if(cap.cap.Direccion==DIR)osSemaphoreRelease(Sem2_Rx);
		}
  }
		
	else{
		CAPA1=0;
		
		osSemaphoreRelease(Sem2_Rx);
		
	}
	
	
}

*/


/*
void 	UART_DMAReceiveCplt (DMA_HandleTypeDef *hdma){//interruptor de rx 
	
	
	 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_12);
	
	
	 
	
}
*/




