Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
rit_job.c
Go to the documentation of this file.
1#include "cl_list.h"
2#include "rit.h"
3
4#include <LPC17xx.h>
5#include <stdlib.h>
6#include <string.h>
7
8// PRIVATE TYPES
9
10typedef struct
11{
13
14 // Interval in milliseconds between each execution of the job. It is used to
15 // determine if the job should be executed in the interrupt handler, based on
16 // the RIT counter value.
18 bool enabled;
20
24
27
33
34// PRIVATE FUNCTIONS
35
36// Used externally (in rit.c)
38{
39 jobs = CL_ListAlloc(alloc, sizeof(JobWrapper));
40 if (!jobs)
41 return;
42}
43
44// Used externally (in rit.c)
46{
47 if (jobs)
49}
50
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}
62
63// PUBLIC FUNCTIONS
64
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}
84
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}
103
104RIT_Error RIT_AddJob(RIT_Job job, u8 divider_factor)
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}
116
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}
137
139{
140 return CL_ListSize(jobs);
141}
142
144{
146 if (list_job->job == job)
147 return list_job->interval / base_ival;
148 })
149
150 return 0;
151}
152
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}
174
180
181// INTERRUPT HANDLER
182
183extern void RIT_IRQHandler(void)
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}
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
void CL_ListClear(CL_List *const list)
Removes all elements from the list.
Definition cl_list.c:316
u32 CL_ListSize(const CL_List *const list)
Gets the number of elements in the list.
Definition cl_list.c:334
#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
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
void CL_ListFree(CL_List *const list)
Frees the memory previously assigned to the list.
Definition cl_list.c:63
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
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
@ CL_ERR_OK
No error.
Definition cl_types.h:26
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 CL_List * jobs
Array of jobs to execute in the RIT interrupt handler.
Definition rit_job.c:26
void allocate_jobs_list(MEM_Allocator *const alloc)
Definition rit_job.c:37
RIT_Error RIT_DisableJob(RIT_Job job)
Exclude this job from the RIT handler queue.
Definition rit_job.c:85
RIT_Error RIT_EnableJob(RIT_Job job)
Include this job in the RIT handler queue.
Definition rit_job.c:65
u32 RIT_GetJobsCount(void)
Returns the number of jobs in the job queue.
Definition rit_job.c:138
RIT_Error RIT_SetJobMultiplierFactor(RIT_Job job, u8 multiplier_factor)
Sets the multiplier factor for the given job.
Definition rit_job.c:153
void RIT_ClearJobs(void)
Clears the job queue.
Definition rit_job.c:175
RIT_Error RIT_RemoveJob(RIT_Job job)
Removes a job from the RIT interrupt handler job queue.
Definition rit_job.c:117
u8 RIT_GetJobMultiplierFactor(RIT_Job job)
Returns the multiplier factor for the given job.
Definition rit_job.c:143
_PRIVATE void recalculate_reset_value(void)
Definition rit_job.c:51
_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
_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
void RIT_IRQHandler(void)
Definition rit_job.c:183
void free_jobs_list(void)
Definition rit_job.c:45
RIT_Error RIT_AddJob(RIT_Job job, u8 divider_factor)
Definition rit_job.c:104
void(* RIT_Job)(void)
RIT interrupt function pointer for jobs that need to be executed in the RIT interrupt handler.
Definition rit_types.h:38
RIT_Error
Definition rit_types.h:11
@ RIT_ERR_DURING_PUSHBACK
An error occurred during the push back of the job in the list.
Definition rit_types.h:30
@ RIT_ERR_INVALID_MULTIPLIER
An invalid multiplier factor was specified (must be > 0).
Definition rit_types.h:24
@ RIT_ERR_OK
No errors.
Definition rit_types.h:13
@ RIT_ERR_JOB_NOT_FOUND
The job specified was not found in the handler queue.
Definition rit_types.h:27
@ RIT_ERR_DURING_REMOVEAT
An error occurred during the removal of the job from the list.
Definition rit_types.h:33
u32 interval
Definition rit_job.c:17
bool enabled
Definition rit_job.c:18
RIT_Job job
Definition rit_job.c:12
#define SET_BIT(reg, bit)
Definition types.h:26
uint8_t u8
Definition types.h:8
#define _PRIVATE
Definition types.h:37
#define _USED_EXTERNALLY
Definition types.h:35
#define _DECL_EXTERNALLY
Definition types.h:36
uint32_t u32
Definition types.h:6