Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lp_timer.c « Src « Core « BLE_MeshLightingLPN « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 404bb2e3ba89d61eb05ebbe33b7e3b379144fbf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 ***************************************************************************************
  * File Name          : lp_timer.c
  * Description        : Low power timer to be used within Mesh Application.
 ***************************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the 
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/  
#include "app_common.h"

#include "lp_timer.h"

/* Exported variables --------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void LpTimerCb(void);

/* Private typedef -----------------------------------------------------------*/
typedef struct
{
  uint32_t LpTimeLeftOnEntry;
  uint8_t LpTimer_Id;
} LpTimerContext_t;

/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static LpTimerContext_t LpTimerContext;

/* Functions Definition ------------------------------------------------------*/

/**
 * @brief Initialize the low power timer
 *
 * @param  None
 * @retval None
 */
void LpTimerInit(void)
{
  (void) HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(LpTimerContext.LpTimer_Id), hw_ts_SingleShot, LpTimerCb);

  return;
}

/**
 * @brief  Request to start a low power timer ( running is stop mode )
 *
 * @param  time_to_sleep : in ms
 * @retval None
 */
void LpTimerStart(uint32_t time_to_sleep)
{
  /* Converts the number of ms into hw timer tick */
  if(time_to_sleep > 0x400000)
  {
    time_to_sleep = time_to_sleep / (CFG_TS_TICK_VAL);
    time_to_sleep *= 1000;
  }
  else
  {
    time_to_sleep *= 1000;
    time_to_sleep = time_to_sleep / (CFG_TS_TICK_VAL);
  }

  HW_TS_Start(LpTimerContext.LpTimer_Id, time_to_sleep);

  /**
   * There might be other timers already running in the timer server that may elapse
   * before this one.
   * Store how long before the next event so that on wakeup, it will be possible to calculate
   * how long the tick has been suppressed
   */
  LpTimerContext.LpTimeLeftOnEntry = HW_TS_RTC_ReadLeftTicksToCount();

  return;
}

/**
 * @brief  Read how long the timer has run
 *
 * @param  None
 * @retval The time elapsed in ms
 */
uint32_t LpGetElapsedTime(void)
{
  uint32_t return_value;

  return_value = (CFG_TS_TICK_VAL) * (uint32_t)(LpTimerContext.LpTimeLeftOnEntry - HW_TS_RTC_ReadLeftTicksToCount());
  return_value = return_value / 1000;

  /**
   * The system may have been out from another reason than the timer
   * Stop the timer after the elapsed time is calculated other wise, HW_TS_RTC_ReadLeftTicksToCount()
   * may return 0xFFFF ( TIMER LIST EMPTY )
   * It does not hurt stopping a timer that exists but is not running.
   */
  HW_TS_Stop(LpTimerContext.LpTimer_Id);

  return return_value;
}


/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/
/**
 * @brief Low power timer callback
 *
 * @param  None
 * @retval None
 */
static void LpTimerCb( void )
{
  /**
   * Nothing to be done
   */

  return;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/