Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
rit_job.c File Reference
#include "cl_list.h"
#include "rit.h"
#include <LPC17xx.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  JobWrapper
 

Functions

void allocate_jobs_list (MEM_Allocator *const alloc)
 
void free_jobs_list (void)
 
_PRIVATE void recalculate_reset_value (void)
 
RIT_Error RIT_EnableJob (RIT_Job job)
 Include this job in the RIT handler queue.
 
RIT_Error RIT_DisableJob (RIT_Job job)
 Exclude this job from the RIT handler queue.
 
RIT_Error RIT_AddJob (RIT_Job job, u8 divider_factor)
 
RIT_Error RIT_RemoveJob (RIT_Job job)
 Removes a job from the RIT interrupt handler job queue.
 
u32 RIT_GetJobsCount (void)
 Returns the number of jobs in the job queue.
 
u8 RIT_GetJobMultiplierFactor (RIT_Job job)
 Returns the multiplier factor for the given job.
 
RIT_Error RIT_SetJobMultiplierFactor (RIT_Job job, u8 multiplier_factor)
 Sets the multiplier factor for the given job.
 
void RIT_ClearJobs (void)
 Clears the job queue.
 
void RIT_IRQHandler (void)
 

Variables

_DECL_EXTERNALLY u32 base_ival
 External variable that stores the base interval between each RIT interrupt.
 
_PRIVATE CL_Listjobs
 Array of jobs to execute in the RIT interrupt handler.
 
_PRIVATE u32 counter = 0
 Stores the current RIT counter value, and it's incremented by each RIT interrupt. It's used with the counter_reset_value, that's the maximum value the counter can reach before being reset, which corresponds to the interval of the less frequent job.
 
_USED_EXTERNALLY u32 counter_reset_value
 

Function Documentation

◆ allocate_jobs_list()

void allocate_jobs_list ( MEM_Allocator *const  alloc)

Definition at line 37 of file rit_job.c.

38{
39 jobs = CL_ListAlloc(alloc, sizeof(JobWrapper));
40 if (!jobs)
41 return;
42}
CL_List * CL_ListAlloc(MEM_Allocator *const alloc, u32 elem_sz)
Initializes a new doubly-linked list with tail, with the allocated memory provided by the user.
Definition cl_list.c:43
_PRIVATE CL_List * jobs
Array of jobs to execute in the RIT interrupt handler.
Definition rit_job.c:26

◆ free_jobs_list()

void free_jobs_list ( void  )

Definition at line 45 of file rit_job.c.

46{
47 if (jobs)
49}
void CL_ListFree(CL_List *const list)
Frees the memory previously assigned to the list.
Definition cl_list.c:63

◆ recalculate_reset_value()

_PRIVATE void recalculate_reset_value ( void  )

Definition at line 51 of file rit_job.c.

52{
55 if (!job->enabled || !job->job)
56 continue;
57
58 if (job->interval > counter_reset_value)
59 counter_reset_value = job->interval;
60 });
61}
#define CL_LIST_FOREACH_PTR(__type, __name, __list,...)
Macro to iterate over the elements of the list with a pointer to the current element.
Definition cl_list.h:33
_DECL_EXTERNALLY u32 base_ival
External variable that stores the base interval between each RIT interrupt.
Definition rit_job.c:23
_USED_EXTERNALLY u32 counter_reset_value
Definition rit_job.c:32

◆ RIT_AddJob()

RIT_Error RIT_AddJob ( RIT_Job  job,
u8  divider_factor 
)

Definition at line 104 of file rit_job.c.

105{
106 const u32 ival = base_ival * divider_factor;
107 if (CL_ListPushBack(jobs, &(JobWrapper){.job = job, .interval = ival, .enabled = false}) != CL_ERR_OK)
109
110 // Compute the maximum value counter can reach before being reset
111 if (ival > counter_reset_value)
112 counter_reset_value = ival;
113
114 return RIT_ERR_OK;
115}
CL_Error CL_ListPushBack(CL_List *const list, const void *const elem)
Adds an element at the end of the list.
Definition cl_list.c:80
@ CL_ERR_OK
No error.
Definition cl_types.h:26
@ RIT_ERR_DURING_PUSHBACK
An error occurred during the push back of the job in the list.
Definition rit_types.h:30
@ RIT_ERR_OK
No errors.
Definition rit_types.h:13
uint32_t u32
Definition types.h:6

◆ RIT_ClearJobs()

void RIT_ClearJobs ( void  )

Clears the job queue.

Definition at line 175 of file rit_job.c.

176{
179}
void CL_ListClear(CL_List *const list)
Removes all elements from the list.
Definition cl_list.c:316

◆ RIT_DisableJob()

RIT_Error RIT_DisableJob ( RIT_Job  job)

Exclude this job from the RIT handler queue.

Definition at line 85 of file rit_job.c.

86{
88 if (list_job->job == job)
89 {
90 list_job->enabled = false;
91
92 // Need to recompute the counter_reset_value, if this job was
93 // the slowest one.
94 if (list_job->interval == counter_reset_value)
95 recalculate_reset_value();
96
97 return RIT_ERR_OK;
98 }
99 });
100
102}
@ RIT_ERR_JOB_NOT_FOUND
The job specified was not found in the handler queue.
Definition rit_types.h:27

◆ RIT_EnableJob()

RIT_Error RIT_EnableJob ( RIT_Job  job)

Include this job in the RIT handler queue.

Definition at line 65 of file rit_job.c.

66{
68 if (list_job->job == job)
69 {
70 list_job->enabled = true;
71
72 // Need to recompute the counter_reset_value, if the interval
73 // of the enabled job is higher than the current least frequent enabled job.
74 if (list_job->interval > counter_reset_value)
75 counter_reset_value = list_job->interval;
76
77 return RIT_ERR_OK;
78 }
79 });
80
81 // If here, job was not found
83}

◆ RIT_GetJobMultiplierFactor()

u8 RIT_GetJobMultiplierFactor ( RIT_Job  job)

Returns the multiplier factor for the given job.

Parameters
jobThe job to get the multiplier factor from
Returns
u8 The multiplier factor

Definition at line 143 of file rit_job.c.

144{
146 if (list_job->job == job)
147 return list_job->interval / base_ival;
148 })
149
150 return 0;
151}

◆ RIT_GetJobsCount()

u32 RIT_GetJobsCount ( void  )

Returns the number of jobs in the job queue.

Definition at line 138 of file rit_job.c.

139{
140 return CL_ListSize(jobs);
141}
u32 CL_ListSize(const CL_List *const list)
Gets the number of elements in the list.
Definition cl_list.c:334

◆ RIT_IRQHandler()

void RIT_IRQHandler ( void  )
extern

Definition at line 183 of file rit_job.c.

184{
185#ifdef SIMULATOR
186 RIT_Disable();
187#endif
188
189 LPC_RIT->RICOUNTER = 0; // Reset counter
190 SET_BIT(LPC_RIT->RICTRL, 0); // Clear interrupt flag
191
192 if (CL_ListSize(jobs) <= 0)
193 return;
194
197 if (!job->enabled)
198 continue;
199
200 if (counter % job->interval == 0 && job->job)
201 job->job();
202 });
203
204 // Reset the counter if the maximum value is reached
206 counter = 0;
207
208#ifdef SIMULATOR
209 RIT_Enable();
210#endif
211}
void RIT_Disable(void)
Disables the RIT entirely (stops counting).
Definition rit.c:55
void RIT_Enable(void)
Enables the RIT counting.
Definition rit.c:50
_PRIVATE u32 counter
Stores the current RIT counter value, and it's incremented by each RIT interrupt. It's used with the ...
Definition rit_job.c:31
#define SET_BIT(reg, bit)
Definition types.h:26

◆ RIT_RemoveJob()

RIT_Error RIT_RemoveJob ( RIT_Job  job)

Removes a job from the RIT interrupt handler job queue.

Parameters
jobThe job to remove

Definition at line 117 of file rit_job.c.

118{
119 for (u32 i = 0; i < CL_ListSize(jobs); i++)
120 {
121 JobWrapper *const list_job;
122 CL_ListGetPtr(jobs, i, (void **)&(list_job));
123 {
124 if (list_job->job == job)
125 {
126 if (CL_ListRemoveAt(jobs, i, ((void *)0)) != CL_ERR_OK)
128
130 break;
131 }
132 }
133 }
134
135 return RIT_ERR_OK;
136}
CL_Error CL_ListRemoveAt(CL_List *const list, u32 index, void *out_elem)
Removes an element at the specified index.
Definition cl_list.c:260
CL_Error CL_ListGetPtr(const CL_List *const list, u32 index, void **out_elem)
Gets an element at the specified index as a pointer, so the user can modify it.
Definition cl_list.c:186
_PRIVATE void recalculate_reset_value(void)
Definition rit_job.c:51
@ RIT_ERR_DURING_REMOVEAT
An error occurred during the removal of the job from the list.
Definition rit_types.h:33
RIT_Job job
Definition rit_job.c:12

◆ RIT_SetJobMultiplierFactor()

RIT_Error RIT_SetJobMultiplierFactor ( RIT_Job  job,
u8  multiplier_factor 
)

Sets the multiplier factor for the given job.

Parameters
jobThe job to set the multiplier factor for
multiplier_factorThe new multiplier factor
Returns
RIT_Error The error.

Definition at line 153 of file rit_job.c.

154{
155 if (multiplier_factor == 0)
157
159 if (list_job->job == job)
160 {
161 list_job->interval = base_ival * multiplier_factor;
162
163 // Need to recompute the counter_reset_value, if this job was
164 // the slowest one.
165 if (list_job->interval == counter_reset_value)
166 recalculate_reset_value();
167
168 return RIT_ERR_OK;
169 }
170 });
171
173}
@ RIT_ERR_INVALID_MULTIPLIER
An invalid multiplier factor was specified (must be > 0).
Definition rit_types.h:24

Variable Documentation

◆ base_ival

_DECL_EXTERNALLY u32 base_ival

External variable that stores the base interval between each RIT interrupt.

Note
This variable is used to calculate the RIT counter value for each job.

Definition at line 23 of file rit_job.c.

◆ counter

_PRIVATE u32 counter = 0

Stores the current RIT counter value, and it's incremented by each RIT interrupt. It's used with the counter_reset_value, that's the maximum value the counter can reach before being reset, which corresponds to the interval of the less frequent job.

Definition at line 31 of file rit_job.c.

◆ counter_reset_value

_USED_EXTERNALLY u32 counter_reset_value

Definition at line 32 of file rit_job.c.

◆ jobs

Array of jobs to execute in the RIT interrupt handler.

Definition at line 26 of file rit_job.c.