#include "stm32f1xx_hal.h"
#include "cmsis_os.h"

#include "com_usart_hal.h"

#include "lectura_adc1_hal.h"
#include <string.h>
#include "main.h"

ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
uint16_t DATA_ADC[tam_adc];

osSemaphoreId Sem1_Adc1;



void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc){

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hadc->Instance==ADC1)  {
  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();
  
   /**ADC1 GPIO Configuracion   
    PA0-WKUP------> ADC1_IN0
    PA1     ------> ADC1_IN1
    PA2     ------> ADC1_IN2
    PA3     ------> ADC1_IN3 
		PA4			------> ADC1_IN4
		PA5			------>	ADC1_IN5
		PA6			------> ADC1_IN6
		*/
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Peripheral DMA init*/
  
    hdma_adc1.Instance = DMA1_Channel1;
    hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
    hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_adc1.Init.Mode = DMA_CIRCULAR;//DMA_NORMAL;
    hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
		
		
		
		
    if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)  {
      Error_Handler();
    }

    __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);

  /* USER CODE BEGIN ADC1_MspInit 1 */

  /* USER CODE END ADC1_MspInit 1 */
  }

}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{

  if(hadc->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspDeInit 0 */

  /* USER CODE END ADC1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_ADC1_CLK_DISABLE();
  
    /**ADC1 GPIO Configuracion   
    PA0-WKUP------> ADC1_IN0
    PA1     ------> ADC1_IN1
    PA2     ------> ADC1_IN2
    PA3     ------> ADC1_IN3 
		PA4			------> ADC1_IN4
		PA5			------>	ADC1_IN5
		PA6			------> ADC1_IN6
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6 );

    /* Peripheral DMA DeInit*/
    HAL_DMA_DeInit(hadc->DMA_Handle);
  }
  /* USER CODE BEGIN ADC1_MspDeInit 1 */

  /* USER CODE END ADC1_MspDeInit 1 */

}

void DMA_ADC1_Init(void){

	
  ADC_ChannelConfTypeDef sConfig;
	//__HAL_RCC_ADC1_CLK_ENABLE();//activar adc1
	HAL_ADC_MspInit(&hadc1);

    /**Common config 
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = /*ADC_EXTERNALTRIGCONVEDGE_NONE;*/ ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 7;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
	
	
    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;//ADC_SAMPLETIME_1CYCLE_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
	
	sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = 2;
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;//ADC_SAMPLETIME_1CYCLE_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
	sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = 3;
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;//ADC_SAMPLETIME_1CYCLE_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
	
	sConfig.Channel = ADC_CHANNEL_3;
  sConfig.Rank = 4;
  sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;//ADC_SAMPLETIME_1CYCLE_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
	sConfig.Channel = ADC_CHANNEL_4;
	sConfig.Rank=5;
	sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
	if(HAL_ADC_ConfigChannel(&hadc1,&sConfig) != HAL_OK){
		Error_Handler();
	}
	
	sConfig.Channel =	ADC_CHANNEL_5;
	sConfig.Rank=6;
	sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
	if(HAL_ADC_ConfigChannel(&hadc1,&sConfig) != HAL_OK){
		Error_Handler();
	}
	
	sConfig.Channel = ADC_CHANNEL_6;
	sConfig.Rank=7;
	sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
	if(HAL_ADC_ConfigChannel(&hadc1,&sConfig) != HAL_OK){
		Error_Handler();
	}
	//HAL_ADC_Start(&hadc1);
	

	HAL_ADC_Start_DMA(&hadc1,(uint32_t *)DATA_ADC,7);//7
}

void Mostrar_ADC_Texto(void){
	uint8_t buf[80];
	buf[0]=0;
	sprintf((char*)buf,"ROJO=%d ->VERDE=%d -> AZUL=%d -> UV=%d # TEM=%d\n",DATA_ADC[0],DATA_ADC[1],DATA_ADC[2],DATA_ADC[3],DATA_ADC[4]);
	Enviar_DMA_TX(buf,strlen((const char*)buf));
}

void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
		
		osSemaphoreRelease(Sem1_Adc1);

	
	
	
}





