view Small_CPU/Src/rtc.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 f0069f002c55
line wrap: on
line source

/**
  ******************************************************************************
  * @file    rtc.c 
  * @author  heinrichs weikamp gmbh
  * @version V0.0.1
  * @date    10-Oct-2014
  * @brief   Source code for rtc control
  *           
  @verbatim                 
  ============================================================================== 
                        ##### How to use #####
  ============================================================================== 
  @endverbatim
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2015 heinrichs weikamp</center></h2>
  *
  ******************************************************************************
  */ 
/* Includes ------------------------------------------------------------------*/
#include "rtc.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_conf.h"
#include "baseCPU2.h"

RTC_HandleTypeDef RTCHandle;

static void RTC_Error_Handler(void);


void RTC_SetTime(RTC_TimeTypeDef stimestructure)
{

	stimestructure.SubSeconds = 0;
  stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
  stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
  stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;

  if(HAL_RTC_SetTime(&RTCHandle, &stimestructure, FORMAT_BIN) != HAL_OK)
  {
    RTC_Error_Handler(); 
  }
}


void RTC_SetDate(RTC_DateTypeDef sdatestructure)
{
  if(HAL_RTC_SetDate(&RTCHandle, &sdatestructure, FORMAT_BIN) != HAL_OK)
  {
    RTC_Error_Handler(); 
  }
}


/*
static void RTC_CalendarConfig(void)
{
  RTC_DateTypeDef sdatestructure;
  RTC_TimeTypeDef stimestructure;

  //##-1- Configure the Date #################################################
  // Set Date: Monday April 14th 2014 
  sdatestructure.Year = 0;
  sdatestructure.Month = RTC_MONTH_JANUARY;
  sdatestructure.Date = 1;
  sdatestructure.WeekDay = RTC_WEEKDAY_MONDAY;
  
  if(HAL_RTC_SetDate(&RTCHandle,&sdatestructure,FORMAT_BCD) != HAL_OK)
  {
    RTC_Error_Handler(); 
  } 
  
  //##-2- Configure the Time #################################################
  // Set Time: 02:00:00 
  stimestructure.Hours = 0;
  stimestructure.Minutes = 0;
  stimestructure.Seconds = 0;
  stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
  stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
  
  if(HAL_RTC_SetTime(&RTCHandle,&stimestructure,FORMAT_BCD) != HAL_OK)
  {
    RTC_Error_Handler(); 
  }
  
  //##-3- Writes a data in a RTC Backup data Register0 #######################
//  HAL_RTCEx_BKUPWrite(&RTCHandle,RTC_BKP_DR0,0x32F2);  
}
*/


  /* ##-1- Configure the RTC peripheral #######################################
		Configure RTC prescaler and RTC data registers 
		RTC configured as follow:
      - Hour Format    = Format 24
      - Asynch Prediv  = Value according to source clock
      - Synch Prediv   = Value according to source clock
      - OutPut         = Output Disable
      - OutPutPolarity = High Polarity
      - OutPutType     = Open Drain 
*/


void MX_RTC_init(void)
{

  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
//  RTC_AlarmTypeDef sAlarm;

    /**Initialize RTC and set the Time and Date 
    */
  RTCHandle.Instance = RTC;
  RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  RTCHandle.Init.AsynchPrediv = 127;
  RTCHandle.Init.SynchPrediv = 255;
  RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  HAL_RTC_Init(&RTCHandle);

  sTime.Hours = 11;
  sTime.Minutes = 0;
  sTime.Seconds = 0;
  sTime.SubSeconds = 0;
  sTime.TimeFormat = RTC_HOURFORMAT12_AM;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  HAL_RTC_SetTime(&RTCHandle, &sTime, FORMAT_BCD);

  sDate.WeekDay = RTC_WEEKDAY_SUNDAY;
  sDate.Month = RTC_MONTH_FEBRUARY;
  sDate.Date = 15;
  sDate.Year = 17;
  HAL_RTC_SetDate(&RTCHandle, &sDate, FORMAT_BCD);


/*
  RTCHandle.Instance = RTC; 
  RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
  RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
  RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
	
	HAL_RTC_Init(&RTCHandle);
*/	
}


void RTC_StopMode_2seconds(void)
{
    /* Enable Power Control clock */
    __HAL_RCC_PWR_CLK_ENABLE();

  /* Disable Wake-up timer */
  HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);

	/* Enable Wake-up timer */
	HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, (0x1000-1), RTC_WAKEUPCLOCK_RTCCLK_DIV16);

	/* FLASH Deep Power Down Mode enabled */
	HAL_PWREx_EnableFlashPowerDown();

	/*## Enter Stop Mode #######################################################*/
	HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

	/* Configures system clock after wake-up from STOP: enable HSI, PLL and select 
	PLL as system clock source (HSI and PLL are disabled in STOP mode) */
	SYSCLKConfig_STOP();
	
	HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
}


void RTC_Stop_11ms(void)
{
  /* Disable Wake-up timer */
  HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);

	/* Enable Wake-up timer */
	HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, (0x18-1), RTC_WAKEUPCLOCK_RTCCLK_DIV16);

	/* FLASH Deep Power Down Mode enabled */
	HAL_PWREx_DisableFlashPowerDown();

	/*## Enter Stop Mode #######################################################*/
	HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);

	/* Configures system clock after wake-up from STOP: enable HSI, PLL and select 
	PLL as system clock source (HSI and PLL are disabled in STOP mode) */
	SYSCLKConfig_STOP();
	
	HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
}


static void RTC_Error_Handler(void)
{
	while(1);
}	


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