STM32 RTC Scheduler
RTC-based scheduler for ultra-low power applications
scheduler.c
Go to the documentation of this file.
1 
14 /* Includes ------------------------------------------------------------------*/
15 #include "scheduler.h"
16 #include "rtc.h"
17 
18 /* Private variables ---------------------------------------------------------*/
21 
22 /* Private function prototypes -----------------------------------------------*/
23 void Scheduler_ProcessRemainingTime(const uint32_t elapsedTime);
24 
28 void SchedulerInit(void)
29 {
30  scheduler.startTime = 0U;
31  scheduler.isRunning = 0U;
32  scheduler.numOfJobs = 0U;
33 }
34 
43 uint8_t SchedulerAddJob(const uint32_t period, const Callback_t callback)
44 {
45  uint8_t result = 0U;
46 
47  assert_param(period > 0U);
48  assert_param(callback != NULL);
49 
50  if(scheduler.isRunning == 0U)
51  {
53  {
59  }
60  else
61  {
62  result = 0U;
63  }
64  }
65  else
66  {
67  result = 0U;
68  }
69 
70  return result;
71 }
72 
85 void SchedulerProcess(void)
86 {
87  uint8_t isScheduleNextJob = 0U;
88 
89  if(scheduler.isRunning != 0U)
90  {
91  const uint32_t elapsedTime = RtcGetEpoch() - scheduler.startTime;
92  if(elapsedTime > 0U)
93  {
94  /* Process the remaining time of the jobs */
95  Scheduler_ProcessRemainingTime(elapsedTime);
96 
97  /* Schedule next job */
98  isScheduleNextJob = 1U;
99  }
100  else
101  {
102  /* Elapsed time is zero: no need to process and schedule jobs */
103  isScheduleNextJob = 0U;
104  }
105  }
106  else
107  {
108  /* Scheduler is not running: start the scheduler */
109  isScheduleNextJob = 1U;
110  }
111 
112  /* Schedule next job */
113  if(isScheduleNextJob != 0U)
114  {
115  /* Search for the next job with the lowest remaining time */
116  uint8_t indexOfNextJob = 0;
117  for(uint_fast8_t i = 0U; i < scheduler.numOfJobs; ++i)
118  {
120  scheduler.jobs[indexOfNextJob].remainingTime)
121  {
122  indexOfNextJob = i;
123  }
124  }
125 
126  /* Set RTC alarm for next job */
127  if(scheduler.jobs[indexOfNextJob].remainingTime > 0U)
128  {
132  scheduler.jobs[indexOfNextJob].remainingTime) != 0U)
133  {
134  scheduler.isRunning = 1U;
135  }
136  }
137  }
138 }
139 
160 {
161  for(uint_fast8_t i = 0U; i < scheduler.numOfJobs; ++i)
162  {
163  if(scheduler.jobs[i].isPending != 0U)
164  {
165  /* Execute job callback */
166  if(scheduler.jobs[i].callback != 0U)
167  {
168  scheduler.jobs[i].callback();
169  }
170 
171  /* Reset pending flag */
172  scheduler.jobs[i].isPending = 0U;
173  }
174  }
175 }
176 
188 void SchedulerStop(void)
189 {
190  if(scheduler.isRunning != 0U)
191  {
192  /* Deactivate RTC alarm */
194 
195  const uint32_t elapsedTime = RtcGetEpoch() - scheduler.startTime;
196  if(elapsedTime > 0U)
197  {
198  /* Process the remaining time of the jobs */
199  Scheduler_ProcessRemainingTime(elapsedTime);
200  }
201  else
202  {
203  /* Elapsed time is zero: no need to process and schedule jobs */
204  }
205 
206  /* Stop the scheduler */
207  scheduler.isRunning = 0U;
208  }
209  else
210  {
211  /* Scheduler is already stopped: do nothing */
212  }
213 }
214 
220 void Scheduler_ProcessRemainingTime(const uint32_t elapsedTime)
221 {
222  for(uint_fast8_t i = 0U; i < scheduler.numOfJobs; ++i)
223  {
224  if(elapsedTime >= scheduler.jobs[i].remainingTime)
225  {
226  /* Job is ready: reset remaining time and set pending flag */
228  scheduler.jobs[i].isPending = 1U;
229  }
230  else
231  {
232  /* Decrease remaining time */
233  scheduler.jobs[i].remainingTime -= elapsedTime;
234  }
235  }
236 }
Scheduler_t
Definition: scheduler.h:48
Job_t::period
uint32_t period
Definition: scheduler.h:38
Scheduler_t::startTime
uint32_t startTime
Definition: scheduler.h:52
Scheduler_t::numOfJobs
uint8_t numOfJobs
Definition: scheduler.h:56
SchedulerStop
void SchedulerStop(void)
Stop the scheduler.
Definition: scheduler.c:188
Scheduler_t::jobs
Job_t jobs[MAX_NUM_OF_JOBS]
Definition: scheduler.h:58
SchedulerExecutePendingJobs
void SchedulerExecutePendingJobs(void)
Execute the pending jobs.
Definition: scheduler.c:159
Scheduler_t::isRunning
uint8_t isRunning
Definition: scheduler.h:54
scheduler.h
This file contains the RTC-based scheduler definitions, structures and function prototypes.
RtcSetAlarmFromEpoch
uint8_t RtcSetAlarmFromEpoch(const uint32_t epoch)
Set an RTC alarm at a given time specified by an epoch.
Definition: rtc.c:149
Job_t::isPending
uint8_t isPending
Definition: scheduler.h:42
SchedulerAddJob
uint8_t SchedulerAddJob(const uint32_t period, const Callback_t callback)
Add a new job to the scheduler.
Definition: scheduler.c:43
RtcDeactivateAlarm
void RtcDeactivateAlarm(void)
Deactivate a previously set RTC alarm.
Definition: rtc.c:189
RtcGetEpoch
uint32_t RtcGetEpoch(void)
Get the current epoch.
Definition: rtc.c:84
Job_t::remainingTime
uint32_t remainingTime
Definition: scheduler.h:40
scheduler
Scheduler_t scheduler
Definition: scheduler.c:20
Job_t::callback
Callback_t callback
Definition: scheduler.h:44
Scheduler_ProcessRemainingTime
void Scheduler_ProcessRemainingTime(const uint32_t elapsedTime)
This function calculates the remaining time for each job.
Definition: scheduler.c:220
SchedulerProcess
void SchedulerProcess(void)
Process the scheduler.
Definition: scheduler.c:85
rtc.h
This file contains the RTC-specific function prototypes.
MAX_NUM_OF_JOBS
#define MAX_NUM_OF_JOBS
Definition: scheduler.h:27
Callback_t
void(* Callback_t)(void)
Definition: scheduler.h:31
SchedulerInit
void SchedulerInit(void)
Initialize the scheduler by setting its structure values to zero.
Definition: scheduler.c:28