Skip to content

Commit b12c5b6

Browse files
clean up rtc and timer macros
1 parent fae13cc commit b12c5b6

File tree

6 files changed

+477
-319
lines changed

6 files changed

+477
-319
lines changed

docs/doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ INCLUDE_FILE_PATTERNS =
21652165
# recursively expanded use the := operator instead of the = operator.
21662166
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
21672167

2168-
PREDEFINED = FORCE_INTERRUPTS
2168+
PREDEFINED = FORCE_INTERRUPTS=1 tiflags=
21692169

21702170
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
21712171
# tag can be used to specify a list of macro names that should be expanded. The

examples/standalone/real_time_clock/src/main.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,29 @@
33

44
int main(void)
55
{
6-
unsigned int seconds = 0;
6+
uint8_t seconds = 0;
77

88
/* Clear the homescreen */
99
os_ClrHome();
1010

11-
/* Configure the RTC */
12-
rtc_LoadSeconds = rtc_LoadMinutes = rtc_LoadHours = rtc_LoadDays = 0;
13-
rtc_Control = RTC_ENABLE | RTC_LOAD | RTC_SEC_INT_SOURCE;
11+
/* Enable the RTC, and enable second change interrupts */
12+
rtc_Enable(RTC_SEC_INT);
1413

15-
/* Wait for the RTC to load in the new values and acknowledge all the interrupts */
14+
/* Wait for the RTC to not be busy and acknowledge all interrupts */
1615
while (rtc_IsBusy());
17-
rtc_IntAcknowledge = RTC_INT_MASK;
16+
rtc_AckInterrupt(RTC_INT_MASK);
1817

1918
/* Wait until 5 seconds have passed */
2019
while (seconds < 5)
2120
{
22-
/* If a second passed, increment the second counter */
23-
/* Fill the screen with a new color */
24-
if (rtc_IntStatus & RTC_SEC_INT)
21+
/* If a second passed, increment the second counter and then */
22+
/* fill the screen with a fresh color */
23+
if (rtc_ChkInterrupt(RTC_SEC_INT))
2524
{
26-
memset((void*)lcd_Ram, randInt(0,255), LCD_SIZE);
25+
memset((void*)lcd_Ram, randInt(0, 255), LCD_SIZE);
2726
seconds++;
2827

29-
rtc_IntAcknowledge = rtc_IntStatus;
28+
rtc_AckInterrupt(RTC_SEC_INT);
3029
}
3130
}
3231

examples/standalone/second_counter/src/main.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include <tice.h>
22
#include <stdio.h>
33

4-
#define ONE_SECOND (32768/1)
5-
#define HALF_SECOND (32768/2)
6-
#define QUARTER_SECOND (32768/4)
4+
#define TIMER_FREQ 32768 /* Frequency of timer in Hz */
5+
#define ONE_SECOND (TIMER_FREQ / 1)
6+
#define HALF_SECOND (TIMER_FREQ / 2)
7+
#define QUARTER_SECOND (TIMER_FREQ / 4)
78

89
int main(void)
910
{
@@ -13,23 +14,23 @@ int main(void)
1314
/* Clear the homescreen */
1415
os_ClrHome();
1516

16-
/* Disable the timer so it doesn't run when setting the configuration */
17-
timer_Control = TIMER1_DISABLE;
17+
/* Disable timer 1 so it doesn't run when setting the configuration */
18+
timer_Disable(1);
1819

19-
/* By using the 32768 kHz clock, we can count for exactly 1 second */
20-
/* or for a more precise interval of time */
21-
timer_1_ReloadValue = timer_1_Counter = ONE_SECOND;
20+
/* Count for 1 second. The counter will be reloaded once it reaches 0 */
21+
timer_Set(1, ONE_SECOND);
22+
timer_SetReload(1, ONE_SECOND);
2223

23-
/* Enable the timer */
24-
/* Set the timer to use the 32768 kHz clock */
25-
/* Enable an interrupt once the timer reaches 0 */
24+
/* Enable timer 1 */
25+
/* Set the timer to use the 32768 Hz clock */
26+
/* Trigger an interrupt once the timer reaches 0 and reloads */
2627
/* Set the timer to count down */
27-
timer_Control = TIMER1_ENABLE | TIMER1_32K | TIMER1_0INT | TIMER1_DOWN;
28+
timer_Enable(1, TIMER_32K, TIMER_0INT, TIMER_DOWN);
2829

2930
do
3031
{
3132
/* If the timer is reloaded, we reached 0 */
32-
if (timer_IntStatus & TIMER1_RELOADED)
33+
if (timer_ChkInterrupt(1, TIMER_RELOADED))
3334
{
3435
/* Increment number of counts */
3536
count++;
@@ -40,7 +41,7 @@ int main(void)
4041
os_PutStrFull(str);
4142

4243
/* Acknowledge the reload */
43-
timer_IntAcknowledge = TIMER1_RELOADED;
44+
timer_AckInterrupt(1, TIMER_RELOADED);
4445
}
4546
} while (!os_GetCSC());
4647

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
#include <tice.h>
22
#include <stdio.h>
33

4-
#define ONE_SECOND (32768/1)
5-
#define HALF_SECOND (32768/2)
6-
#define QUARTER_SECOND (32768/4)
4+
#define TIMER_FREQ 32768 /* Frequency of timer in Hz */
5+
#define ONE_SECOND (TIMER_FREQ / 1)
6+
#define HALF_SECOND (TIMER_FREQ / 2)
7+
#define QUARTER_SECOND (TIMER_FREQ / 4)
78

8-
void reset_counter(void);
9+
static void reset_counter(void)
10+
{
11+
/* Disable timer 1 so it doesn't run when setting the configuration */
12+
timer_Disable(1);
13+
14+
/* Count for 1 second. The counter will be reloaded once it reaches 0 */
15+
timer_Set(1, ONE_SECOND);
16+
timer_SetReload(1, ONE_SECOND);
17+
18+
/* Enable timer 1 */
19+
/* Set the timer to use the 32768 Hz clock */
20+
/* Trigger an interrupt once the timer reaches 0 and reloads */
21+
/* Set the timer to count down */
22+
timer_Enable(1, TIMER_32K, TIMER_0INT, TIMER_DOWN);
23+
}
924

1025
int main(void)
1126
{
1227
unsigned int count = 0;
1328
char str[10];
1429

15-
/* Set the match value to trigger the interrupt in the timer */
16-
timer_1_MatchValue_1 = ONE_SECOND;
17-
1830
/* Clear the homescreen */
1931
os_ClrHome();
2032

33+
/* Trigger an interrupt when the timer 1's match 1 count is equal to */
34+
/* one second. There are 2 match count comparators per timer. */
35+
timer_SetMatch(1, TIMER_MATCH(1), ONE_SECOND);
36+
2137
/* Reset the counter */
2238
reset_counter();
2339

2440
do
2541
{
26-
/* Poll until we reach the match value */
27-
if (timer_IntStatus & TIMER1_MATCH1)
42+
/* Poll until the match value is detected */
43+
if (timer_ChkInterrupt(1, TIMER_MATCH(1)))
2844
{
2945
/* Print the count */
3046
sprintf(str, "%u", count++);
@@ -35,26 +51,10 @@ int main(void)
3551
reset_counter();
3652

3753
/* Acknowledge the interrupt */
38-
timer_IntStatus = TIMER1_MATCH1;
54+
timer_AckInterrupt(1, TIMER_MATCH(1));
3955
}
4056

4157
} while (count < 5);
4258

4359
return 0;
4460
}
45-
46-
void reset_counter(void)
47-
{
48-
/* Disable the timer so it doesn't run when setting the configuration */
49-
timer_Control = TIMER1_DISABLE;
50-
51-
/* By using the 32768 kHz clock, we can count for a */
52-
/* precise interval of time */
53-
timer_1_ReloadValue = timer_1_Counter = timer_1_MatchValue_1;
54-
55-
/* Enable the timer */
56-
/* Set the timer to use the 32768 kHz clock */
57-
/* Enable an interrupt once the timer reaches 0 */
58-
/* Set the timer to count down */
59-
timer_Control = TIMER1_ENABLE | TIMER1_32K | TIMER1_0INT | TIMER1_DOWN;
60-
}
Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
#include <tice.h>
22

3-
void PrintTime(float elapsed);
3+
/* Print a stopwatch value on the home screen */
4+
static void PrintTime(float elapsed)
5+
{
6+
/* Float format for printf may be unimplemented, so go through an OS real */
7+
real_t elapsed_real;
8+
9+
/* Max stopwatch value is (2^32 - 1) / 32768 = 131072.00, */
10+
/* so create a buffer with room for 9 characters plus a null terminator */
11+
char str[10];
12+
13+
/* If the elapsed time is small enough that the OS would print it using */
14+
/* scientific notation, force it down to zero before conversion */
15+
elapsed_real = os_FloatToReal(elapsed <= 0.001f ? 0.0f : elapsed);
16+
17+
/* Convert the elapsed time real to a string */
18+
os_RealToStr(str, &elapsed_real, 8, 1, 2);
19+
20+
/* print the string */
21+
os_SetCursorPos(0, 0);
22+
os_PutStrFull(str);
23+
}
424

525
int main(void)
626
{
@@ -10,23 +30,24 @@ int main(void)
1030
/* Display an initial time of zero */
1131
PrintTime(0.0f);
1232

13-
/* Disable the timer */
14-
timer_Control = TIMER1_DISABLE;
33+
/* Disable timer 1 so it doesn't run when setting the configuration */
34+
timer_Disable(1);
1535

1636
/* Reset the timer's counter */
17-
timer_1_Counter = 0;
37+
timer_Set(1, 0);
1838

1939
/* Wait for a key press */
2040
while (!os_GetCSC());
2141

22-
/* Enable the timer while setting it to 32768 KHz and making it count up */
23-
timer_Control = TIMER1_ENABLE | TIMER1_32K | TIMER1_UP;
42+
/* Enable the timer while setting it to 32768 Hz and making it count up */
43+
timer_Enable(1, TIMER_32K, TIMER_0INT, TIMER_UP);
2444

2545
/* Continue running until a key is pressed */
2646
do
2747
{
2848
/* Calculate and print the elapsed time */
29-
float elapsed = (float)atomic_load_increasing_32(&timer_1_Counter) / 32768;
49+
/* timer_Get or timer_GetLow may be used in place of timer_GetSafe */
50+
float elapsed = (float)timer_GetSafe(1, TIMER_UP) / 32768;
3051
PrintTime(elapsed);
3152
} while (!os_GetCSC());
3253

@@ -35,25 +56,3 @@ int main(void)
3556

3657
return 0;
3758
}
38-
39-
/* PrintTime a stopwatch value on the home screen */
40-
void PrintTime(float elapsed)
41-
{
42-
/* Float format for printf may be unimplemented, so go through an OS real */
43-
real_t elapsed_real;
44-
45-
/* Max stopwatch value is (2^32 - 1) / 32768 = 131072.00, */
46-
/* so create a buffer with room for 9 characters plus a null terminator */
47-
char str[10];
48-
49-
/* If the elapsed time is small enough that the OS would print it using */
50-
/* scientific notation, force it down to zero before conversion */
51-
elapsed_real = os_FloatToReal(elapsed <= 0.001f ? 0.0f : elapsed);
52-
53-
/* Convert the elapsed time real to a string */
54-
os_RealToStr(str, &elapsed_real, 8, 1, 2);
55-
56-
/* print the string */
57-
os_SetCursorPos(0, 0);
58-
os_PutStrFull(str);
59-
}

0 commit comments

Comments
 (0)