Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
rit.h File Reference
#include "allocator.h"
#include "rit_types.h"
#include "types.h"
#include <stdbool.h>

Go to the source code of this file.

Functions

RIT_Error RIT_Init (MEM_Allocator *const alloc, u32 ival_ms, u16 int_priority)
 Initialize the RIT.
 
void RIT_Deinit (void)
 De-initializes the RIT, by removing every job and disconnecting power.
 
void RIT_Enable (void)
 Enables the RIT counting.
 
void RIT_Disable (void)
 Disables the RIT entirely (stops counting).
 
bool RIT_IsEnabled (void)
 Returns whether the RIT is counting or not.
 
u32 RIT_GetIntervalMs (void)
 Returns the interval in millis that RIT uses for its interrupts.
 
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 multiplier_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.
 

Function Documentation

◆ RIT_AddJob()

RIT_Error RIT_AddJob ( RIT_Job  job,
u8  multiplier_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
_PRIVATE CL_List * jobs
Array of jobs to execute in the RIT interrupt handler.
Definition rit_job.c:26
_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_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_Deinit()

void RIT_Deinit ( void  )

De-initializes the RIT, by removing every job and disconnecting power.

Definition at line 43 of file rit.c.

44{
46 NVIC_DisableIRQ(RIT_IRQn);
48}
void POWER_TurnOffPeripheral(u8 bit)
Turns off a peripheral.
Definition power.c:30
@ POW_PCRIT
Definition power_types.h:21
_DECL_EXTERNALLY void free_jobs_list(void)
Definition rit_job.c:45

◆ RIT_Disable()

void RIT_Disable ( void  )

Disables the RIT entirely (stops counting).

Definition at line 55 of file rit.c.

56{
57 CLR_BIT(LPC_RIT->RICTRL, 3);
58}
#define CLR_BIT(reg, bit)
Definition types.h:27

◆ 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}
#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
@ RIT_ERR_JOB_NOT_FOUND
The job specified was not found in the handler queue.
Definition rit_types.h:27

◆ RIT_Enable()

void RIT_Enable ( void  )

Enables the RIT counting.

Definition at line 50 of file rit.c.

51{
52 SET_BIT(LPC_RIT->RICTRL, 3);
53}
#define SET_BIT(reg, bit)
Definition types.h:26

◆ 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_GetIntervalMs()

u32 RIT_GetIntervalMs ( void  )

Returns the interval in millis that RIT uses for its interrupts.

Definition at line 65 of file rit.c.

66{
67 return base_ival;
68}
_USED_EXTERNALLY u32 base_ival
Definition rit.c:12

◆ 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_Init()

RIT_Error RIT_Init ( MEM_Allocator *const  alloc,
u32  ival_ms,
u16  int_priority 
)

Initialize the RIT.

Parameters
allocThe memory allocator to use for the RIT job list.
ival_msInterval in millis between each RIT interrupt.
int_priorityDebouncer interrupt priority (0 (highest), 15 (lowest)). If set to INT_PRIO_DEF, the default priority will be used.
Note
The RIT has a fixed number of jobs that can be added to the job queue, defined by RIT_JOBS_COUNT (20 by default) in the peripherals.h file.
Returns
RIT_Error The error.

Definition at line 15 of file rit.c.

16{
17 // Powering up the RIT
19
20 CLR_BITS(LPC_SC->PCLKSEL1, 3, 26); // Clear
21 SET_BIT(LPC_SC->PCLKSEL1, 26); // Set PCLK_RIT to CCLK
22 SET_BIT(LPC_SC->PCONP, 16); // Enable power to RIT
23
24 LPC_RIT->RICOMPVAL = (rit_clk_mhz * ival_ms * 1000);
25 LPC_RIT->RICTRL = 6; // Clear on match + Timer enable for debug
26 LPC_RIT->RICOUNTER = 0;
27
28 base_ival = ival_ms;
29 counter_reset_value = ival_ms;
30
31 allocate_jobs_list(alloc);
32
33 // Enabling interrupts coming from RIT
34 NVIC_EnableIRQ(RIT_IRQn);
35
36 if (!IS_DEF_PRIORITY(int_priority) && IS_BETWEEN_EQ(int_priority, 0, 15))
38
39 NVIC_SetPriority(RIT_IRQn, int_priority);
40 return RIT_ERR_OK;
41}
void POWER_TurnOnPeripheral(u8 bit)
Turns on a peripheral.
Definition power.c:25
_DECL_EXTERNALLY u32 counter_reset_value
Definition rit.c:13
_PRIVATE const u32 rit_clk_mhz
RIT_CLK frequency in MHz. Can be checked in the RIT GUI in Keil.
Definition rit.c:7
_DECL_EXTERNALLY void allocate_jobs_list(MEM_Allocator *const)
Definition rit_job.c:37
@ RIT_ERR_INT_PRIO_INVALID
An invalid priority was specified (must be 0 <= prio <= 15). This is not necessarily an error,...
Definition rit_types.h:21
#define CLR_BITS(reg, value, bit)
Definition types.h:30
#define IS_DEF_PRIORITY(prio)
Definition types.h:42
#define IS_BETWEEN_EQ(value, low, hi)
Definition types.h:23

◆ RIT_IsEnabled()

bool RIT_IsEnabled ( void  )

Returns whether the RIT is counting or not.

Definition at line 60 of file rit.c.

61{
62 return LPC_RIT->RICTRL & (1 << 3);
63}

◆ 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