AnsweredAssumed Answered

STM32L151 and ADC woes

Question asked by Schocke.Sebastian on Jan 23, 2016
Hi guys

I'm hoping someone will be able to assist me here. I have been struggling for most of the day with simply trying to read the value of the internal temperature sensor on my STM32L151. I have been through tons of samples of the internet, and playing around myself to see what I can uncover. To no avail. As such, I have created the smallest possible sample code to show what I have, and any advice on why it's not working is welcome... I'll try anything.

ADC_HandleTypeDef hadc;
DMA_HandleTypeDef hdma_adc;
uint32_t ADC1ConvertedValues[32];
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_DMA_Init();
  MX_ADC_Init();
 
  if( HAL_ADC_Start(&hadc) != HAL_OK)
      return 0;
  // -- Enables ADC DMA request
  if (HAL_ADC_Start_DMA(&hadc, (uint32_t*)ADC1ConvertedValues, 64) != HAL_OK)
      return 0;
 
  while (1)
  {
      HAL_Delay(100);
  }
}
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
  __PWR_CLK_ENABLE();
 
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
 
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
 
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
 
/* ADC init function */
void MX_ADC_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;
 
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc.Instance = ADC1;
  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION12b;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc.Init.EOCSelection = EOC_SINGLE_CONV;
  hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
  hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.NbrOfConversion = 2;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.DMAContinuousRequests = ENABLE;
  HAL_ADC_Init(&hadc);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_VREFINT;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  sConfig.Rank = 2;
  HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
 
/**
  * Enable DMA controller clock
  */
void MX_DMA_Init(void)
{
  /* DMA controller clock enable */
  __DMA1_CLK_ENABLE();
 
  /* DMA interrupt init */
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}

I haven't touched the files generated by CubeMX, but I will provide them if anyone needs to see something in there.

The fact is that nothing is happening in the memory buffer... If I set breakpoints inside the while loop, the values never change. If I set a breakpoint inside the IRQ handlers for the DMA or the ADC conversion, they never get hit. I realize there is a "duplicate" ADC_Start call, but that was what I found in an online sample that seems to have worked for many others. I tried to remove the first non-DMA one anyway, and it makes no difference.

In my other project (where I actually need the ADC values, but can't get it to work) I have somehow managed to get the DMA transfer to work, but the values are all rubbish (lots of zeroes, with 128, 256, 1396, and 764 thrown in). I feel like I must be missing something, as I can't find anybody else struggling with something so simple, but I don't know what.

Thanks in advance,
   Sebastian

Outcomes