view Small_CPU/Src/adc.c @ 224:ceecabfddb57 div-fixes-3

Bugfix, deco: fix 2 (small) problems with calculated ceiling This fixes 1 trivial, and 1 not really trivial bug in the calculation of the ceiling. When simulating a bounce dive to 80m, things become clear (tried this on a CCR dive, fixed setpoint 1.2bar, about 15 minutes of bottom time). Closely watch the behavior of the ceiling data. At some point during the ascent, the ceiling begins to decrease in 10cm steps. Then suddenly (while still ascending), the ceiling increases again with 1m, does not change for some time, and then suddenly steps 1.1m less deep. While not very relevant to real deco diving, it is simply wrong. The reason for this is subtle. The algorithm used to find the ceiling is a sort of linear search, stepping down a meter, overshoot the depth, and search back in 10cm steps. It seems some numerical instability. Fixing this, was a bit more computational intensive search by stepping up down in equal steps of 10cm. But, I'm pretty sure that things can be speeded up here, as a ceiling does not change fast, so it should be not that difficult to limit the search space, or use a binary search algorithm instead. The trivial second problem fixed, is that the ceiling ends at the surface and not at 1m depth. This small issue became visible after changing the step down size above. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Sun, 31 Mar 2019 19:35:51 +0200
parents 5f11787b4f42
children
line wrap: on
line source

/**
  ******************************************************************************
  * @file    adc.c 
  * @author  heinrichs weikamp gmbh
  * @version V0.0.1
  * @date    11-Dec-2014
  * @brief   ADC for ambient light sensor
  *           
  @verbatim                 
  ============================================================================== 
                        ##### How to use #####
  ============================================================================== 
  @endverbatim
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/

#include "stm32f4xx_hal.h"
#include "adc.h"

/* Exported variables --------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
	ADC_HandleTypeDef    AdcHandle;
__IO uint16_t uhADCxConvertedValue = 0;

/* Private types -------------------------------------------------------------*/
#define ADCx                            ADC1
#define ADCx_CLK_ENABLE()               __ADC1_CLK_ENABLE();
#define ADCx_CHANNEL_GPIO_CLK_ENABLE()  __GPIOF_CLK_ENABLE()
     
#define ADCx_FORCE_RESET()              __ADC_FORCE_RESET()
#define ADCx_RELEASE_RESET()            __ADC_RELEASE_RESET()

/* Definition for ADCx Channel Pin */
#define ADCx_GPIO_PIN                		GPIO_PIN_0
#define ADCx_GPIO_PORT         					GPIOB 
#define ADCx_GPIO_CLK_ENABLE()          __GPIOB_CLK_ENABLE()
#define ADCx_GPIO_CLK_DISABLE()         __GPIOB_CLK_DISABLE()

/* Definition for ADCx's Channel */
#define ADCx_CHANNEL                    ADC_CHANNEL_8

/* Definition for ADCx's NVIC */
//#define ADCx_IRQn                      ADC_IRQn

/* Private function prototypes -----------------------------------------------*/
void ADC_Start_single_IT_Conversion(void);

/* Exported functions --------------------------------------------------------*/

uint16_t get_ambient_light_level(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return 800;
	#endif
	
	return uhADCxConvertedValue;
}


static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif
	
  GPIO_InitTypeDef  GPIO_InitStruct;

  ADCx_GPIO_CLK_ENABLE();
  
  GPIO_InitStruct.Pin = ADCx_GPIO_PIN ;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADCx_GPIO_PORT, &GPIO_InitStruct);

  ADCx_CLK_ENABLE(); 

//  HAL_NVIC_SetPriority(ADCx_IRQn, 2, 0);
//  HAL_NVIC_EnableIRQ(ADCx_IRQn);
}


void ADCx_DeInit(void)
{
//	HAL_ADC_Stop_IT(&AdcHandle);
  HAL_ADC_DeInit(&AdcHandle);
}


void ADCx_Init(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif
	
	ADC_ChannelConfTypeDef sConfig;
	
  AdcHandle.Instance          = ADCx;
  
  AdcHandle.Init.ClockPrescaler				 = ADC_CLOCKPRESCALER_PCLK_DIV4;
	AdcHandle.Init.Resolution            = ADC_RESOLUTION12b;
	AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
	AdcHandle.Init.ContinuousConvMode    = DISABLE;
	AdcHandle.Init.DiscontinuousConvMode = DISABLE;
	AdcHandle.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;
	AdcHandle.Init.EOCSelection          = EOC_SINGLE_CONV;
	AdcHandle.Init.NbrOfConversion       = 1;
	AdcHandle.Init.DMAContinuousRequests = DISABLE;    
      
	ADCx_MspInit(&AdcHandle);
	HAL_ADC_Init(&AdcHandle);

	sConfig.Channel = ADCx_CHANNEL;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  sConfig.Rank = 1;
  sConfig.Offset = 0;

	HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
	
//	ADC_Start_single_IT_Conversion();
}


uint32_t adc_debug_status = 0;

void adc_ambient_light_sensor_get_data(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif
	
	HAL_ADC_Start(&AdcHandle);
  HAL_ADC_PollForConversion(&AdcHandle, 10);
	adc_debug_status = HAL_ADC_GetState(&AdcHandle);
  if(adc_debug_status == HAL_ADC_STATE_EOC_REG + HAL_ADC_STATE_READY) // new HAL_ADC_STATE_READY 160613
//  if(HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG)
  uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);
	HAL_ADC_Stop(&AdcHandle);
}


/* Private functions ---------------------------------------------------------*/
/*
void ADC_Start_single_IT_Conversion(void)
{
	HAL_ADC_Start_IT(&AdcHandle);
}


void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
	extern void copyAmbientLightData(void);

  uhADCxConvertedValue = HAL_ADC_GetValue(AdcHandle);
	copyAmbientLightData();
	ADC_Start_single_IT_Conversion();
}
*/

/************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/