Skip to content

Commit ae19350

Browse files
runer112Matt Waltz
authored and
Matt Waltz
committed
Add millisecond delay function to tice.h (#71)
* Add millisecond delay function * Add delay example Still needed: * Screenshot (and include in readme?) * Discover expeced hash values for autotester * Confirm that autotester succeeds * Optimize delay for size * Don't bother to disable the timer after delay * Atomically set timer control bits in delay * Improve delay documentation and example * Fix delay autotester * Add screenshot and update readme for delay demo
1 parent 5ded968 commit ae19350

File tree

6 files changed

+207
-18
lines changed

6 files changed

+207
-18
lines changed

examples/delay/autotester.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"rom": "84pce_515.rom",
3+
"transfer_files": [
4+
"bin/DEMO.8xp"
5+
],
6+
"target": {
7+
"name": "DEMO",
8+
"isASM": true
9+
},
10+
"sequence": [
11+
"action|launch",
12+
"hashWait|1",
13+
"delay|4800",
14+
"hashWait|2"
15+
],
16+
"hashes": {
17+
"1": {
18+
"description": "Waiting for 5000 ms...",
19+
"start": "vram_start",
20+
"size": "vram_16_size",
21+
"expected_CRCs": [ "690F0EEC" ]
22+
},
23+
"2": {
24+
"description": "Done",
25+
"start": "vram_start",
26+
"size": "vram_16_size",
27+
"expected_CRCs": [ "948248E" ],
28+
"timeout": 250
29+
}
30+
}
31+
}

examples/delay/makefile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# ----------------------------
2+
# Set NAME to the program name
3+
# Set DEBUGMODE to "DEBUG" to use debug functions
4+
# Set ICON to the png icon file name
5+
# Set DESCRIPTION to display within a compatible shell
6+
# Set COMPRESSED to "YES" to create a compressed program
7+
# ** Add all shared library names to L **
8+
# ----------------------------
9+
10+
NAME ?= DEMO
11+
DEBUGMODE ?= NDEBUG
12+
COMPRESSED ?= NO
13+
ICON ?= iconc.png
14+
DESCRIPTION ?= "C SDK Demo"
15+
16+
L ?=
17+
18+
# ----------------------------
19+
# Specify source and output locations
20+
# ----------------------------
21+
22+
SRCDIR ?= src
23+
OBJDIR ?= obj
24+
BINDIR ?= bin
25+
GFXDIR ?= src/gfx
26+
27+
# ----------------------------
28+
# Use OS helper functions (Advanced)
29+
# ----------------------------
30+
31+
USE_FLASH_FUNCTIONS ?= YES
32+
33+
include $(CEDEV)/include/.makefile

examples/delay/readme.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Delay Demo
2+
3+
Demonstrates using the millisecond delay function in tice.h.
4+
5+
![Screenshot](screenshot.gif)
6+
7+
---
8+
9+
This demo is a part of the C SDK Toolchain for use on the CE.

examples/delay/src/main.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdbool.h>
2+
#include <stddef.h>
3+
#include <stdint.h>
4+
#include <tice.h>
5+
6+
void main(void) {
7+
/* Clear the homescreen */
8+
os_ClrHome();
9+
10+
os_PutStrFull("Waiting for 5000 ms...");
11+
delay(5000);
12+
os_SetCursorPos(1, 22);
13+
os_PutStrFull("Done");
14+
delay(100);
15+
}

src/ce/delay.src

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
.assume adl=1
2+
.def _delay
3+
4+
mpTmrRange equ 0F20000h
5+
6+
tmr2Counter equ 10h
7+
tmr2Load equ 14h
8+
tmr2Match1 equ 18h
9+
tmr2Match2 equ 1Ch
10+
11+
tmrCtrl equ 30h
12+
13+
bTmr2Enable equ 3
14+
tmr2Enable equ 1<<bTmr2Enable
15+
bTmr2Crystal equ 4
16+
tmr2Crystal equ 1<<bTmr2Crystal
17+
bTmr2Overflow equ 5
18+
tmr2Overflow equ 1<<bTmr2Overflow
19+
bTmr2CountUp equ 10
20+
tmr2CountUp equ 1<<bTmr2CountUp
21+
22+
; void delay(uint16_t millis);
23+
24+
_delay:
25+
; Read argument.
26+
pop de
27+
ex (sp),hl ; 0hl = millis
28+
push de
29+
; Abort ASAP if millis == 0.
30+
ld a,h
31+
or a,l
32+
ret z
33+
; Convert millis to CPU cycles by multiplying by 48000.
34+
; Subtract 2 to offset overhead and the unknown counter low byte.
35+
ld b,h
36+
ld c,l ; 0bc = millis
37+
dec.s bc ; bc = millis-1; need to set bcu = 0
38+
srl b
39+
rr c ; bc = (millis-1)>>1
40+
; = millis*(48000&0FFh)>>8,
41+
; off by -1 or -0.5
42+
ld e,l
43+
ld d,48000>>8 ; 48000/256 == 187.5
44+
ld l,d
45+
mlt de ; de = (millis&0FFh)*(48000>>8)
46+
mlt hl ; hl = (millis>>8)*(48000>>8)
47+
dec hl
48+
push hl
49+
dec sp
50+
pop hl
51+
inc sp
52+
ld l,-2 ; hl = (millis&0FF00h)*(48000>>8)-2
53+
add hl,de ; hl = millis*(48000>>8)-2
54+
add hl,bc ; hl ~ (millis*48000>>8)-3
55+
; Set up timer 2 control bits: disabled, CPU clock, no interrupt, count down.
56+
ex de,hl ; de ~ (millis*48000>>8)-3
57+
ld hl,mpTmrRange+tmrCtrl
58+
res bTmr2Enable,(hl)
59+
res bTmr2Crystal,(hl)
60+
res bTmr2Overflow,(hl)
61+
inc hl
62+
res bTmr2CountUp-8,(hl)
63+
; Set up timer 2 counter, match, and reload values.
64+
ld l,tmr2Counter+1
65+
ld (hl),de ; counter = millis*48000-768+0xxh
66+
ld l,tmr2Match1+3
67+
ld (hl),0FFh ; match1 = 0FFxxxxxxh
68+
ld l,tmr2Match2+3
69+
ld (hl),0FFh ; match2 = 0FFxxxxxxh
70+
ld l,tmr2Load+3
71+
ld (hl),0FEh ; reload = 0FExxxxxxh
72+
; Enable timer 2.
73+
ld l,tmrCtrl
74+
set bTmr2Enable,(hl)
75+
; Wait until timer 2 expires (underflows).
76+
; Will catch the underflow if not interrupted for a continuous period of
77+
; ((254-188)<<24)/48000000 ~ 23.1s.
78+
ld l,tmr2Counter+3
79+
ld a,1+((0FFFFh*24000<<1)-12>>24) ; = 188
80+
; 380 cc up to this point
81+
_delay_wait:
82+
cp a,(hl)
83+
jr nc,_delay_wait
84+
; 12-29 cc to exit waitloop
85+
; Done.
86+
ret
87+
; ~421 cc up to this point
88+
; caller overhead:
89+
; ld bc,millis
90+
; push bc
91+
; call _delay
92+
; pop bc
93+
; ~485 cc total

src/ce/tice.h

+26-18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ extern "C" {
2626
#define randInt(min, max) \
2727
((unsigned)rand() % ((max) - (min) + 1) + (min))
2828

29+
/**
30+
* Delays for a number of milliseconds.
31+
* <p>
32+
* Counts time spent while interrupted. Assumes a CPU clock speed of 48MHz.
33+
*
34+
* @param msec number of milliseconds
35+
*/
36+
void delay(uint16_t msec);
37+
2938
/**
3039
* Gets a combination of the RTC time; useful for srand()
3140
*/
@@ -104,7 +113,7 @@ extern "C" {
104113
#define timer_2_Counter (*(volatile uint32_t*)0xF20010)
105114
#define timer_1_ReloadValue (*(uint32_t*)0xF20004)
106115
#define timer_2_ReloadValue (*(uint32_t*)0xF20014)
107-
#define timer_1_MatchValue_1 (*(uint32_t*)0xF20008)
116+
#define timer_1_MatchValue_1 (*(uint32_t*)0xF20008)
108117
#define timer_1_MatchValue_2 (*(uint32_t*)0xF2000C)
109118
#define timer_2_MatchValue_1 (*(uint32_t*)0xF20018)
110119
#define timer_2_MatchValue_2 (*(uint32_t*)0xF2001C)
@@ -144,7 +153,7 @@ extern "C" {
144153
(240)
145154

146155
/**
147-
* Total number of pixels in LCD
156+
* Total number of pixels in LCD
148157
*/
149158
#define LCD_SIZE \
150159
(LCD_WIDTH*LCD_HEIGHT*2)
@@ -182,7 +191,7 @@ typedef struct { uint16_t len; char data[1]; } equ_t;
182191
*/
183192
typedef struct { uint16_t size; uint8_t data[1]; } var_t;
184193

185-
/**
194+
/**
186195
* Gets an element from a matrix
187196
*
188197
* @param matrix Structure of matrix
@@ -192,12 +201,12 @@ typedef struct { uint16_t size; uint8_t data[1]; } var_t;
192201
*/
193202
#define matrix_element(matrix, row, col) ((matrix)->items[(row)+(col)*(matrix)->rows])
194203

195-
/**
204+
/**
196205
* Resets the OS homescreen; accounts for split screen
197206
*/
198207
#define os_ClrHome() do { _OS(asm_ClrLCD); _OS(asm_HomeUp); _OS(asm_DrawStatusBar); } while (0)
199208

200-
/**
209+
/**
201210
* Resets the OS homescreen fully
202211
*/
203212
#define os_ClrHomeFull() do { _OS(asm_ClrLCDFull); _OS(asm_HomeUp); _OS(asm_DrawStatusBar); } while (0)
@@ -208,7 +217,7 @@ typedef struct { uint16_t size; uint8_t data[1]; } var_t;
208217

209218
/**
210219
* Sets the calculator's date
211-
*
220+
*
212221
* Performs checks to ensure date is within range
213222
* @param day Day to set
214223
* @param month Month to set
@@ -218,7 +227,7 @@ void boot_SetDate(uint8_t day, uint8_t month, uint16_t year);
218227

219228
/**
220229
* Gets the calculator's date
221-
*
230+
*
222231
* @param day Pointer to variable to store day
223232
* @param month Pointer to variable to store month
224233
* @param year Pointer to variable to store year
@@ -227,7 +236,7 @@ void boot_GetDate(uint8_t *day, uint8_t *month, uint16_t *year);
227236

228237
/**
229238
* Sets the calculator's time
230-
*
239+
*
231240
* Performs checks to ensure time is within range
232241
* @param seconds Seconds to set
233242
* @param minutes Minutes to set
@@ -237,7 +246,7 @@ void boot_SetTime(uint8_t seconds, uint8_t minutes, uint8_t hours);
237246

238247
/**
239248
* Gets the calculator's time
240-
*
249+
*
241250
* @param seconds Pointer to variable to store seconds
242251
* @param minutes Pointer to variable to store minutes
243252
* @param hours Pointer to variable to store hours
@@ -278,7 +287,7 @@ void boot_ClearVRAM(void);
278287
*/
279288
bool boot_CheckOnPressed(void);
280289

281-
/**
290+
/**
282291
* Basically a reimplemented form of printf that prints to some debugging device
283292
*
284293
* @param string String to send to debug device
@@ -444,7 +453,7 @@ uint8_t os_GetFlagByte(int offset);
444453

445454
/**
446455
* Get amount of free ram in order to allocate extra ram
447-
*
456+
*
448457
* @param free Set to start of free available ram
449458
* @returns Size of available ram
450459
*/
@@ -570,7 +579,7 @@ real_t os_RealSub(const real_t *arg1, const real_t *arg2);
570579

571580
/**
572581
* Rounds a real_t
573-
*
582+
*
574583
* @note digits must be in the range 0 - 9
575584
*/
576585
real_t os_RealRound(const real_t *arg, char digits);
@@ -628,7 +637,7 @@ int os_RealToStr(char *result, const real_t *arg, int8_t maxLength, uint8_t mode
628637

629638
/**
630639
* This converts a ti-ascii string to a ti-float.
631-
*
640+
*
632641
* String format regexp: / *[-\032+]?[0-9]*(\.[0-9]*)?([eE\033][-\032+]?[0-9]*)?/
633642
* @param string TI-ascii string to convert
634643
* @param end If non-null, pointer to end of parsed number is stored here
@@ -645,7 +654,7 @@ void os_ResetFlagBits(int16_t offset_pattern);
645654

646655
/**
647656
* Gets a key from the OS
648-
*
657+
*
649658
* @returns Key code
650659
* @returns Extended key code in high byte
651660
*/
@@ -664,7 +673,7 @@ typedef uint8_t sk_key_t;
664673
* const char *chars = "\0\0\0\0\0\0\0\0\0\0\"WRMH\0\0?[VQLG\0\0:ZUPKFC\0 YTOJEB\0\0XSNIDA\0\0\0\0\0\0\0\0";
665674
* uint8_t key, i = 0;
666675
* char buffer[50];
667-
*
676+
*
668677
* while((key = os_GetCSC()) != sk_Enter) {
669678
* if(chars[key]) {
670679
* buffer[i++] = chars[key];
@@ -959,7 +968,7 @@ typedef enum {
959968
#define os_Polar6LineColor (*(uint8_t*)0xD024ED)
960969
#define os_SecULineColor (*(uint8_t*)0xD024EE)
961970
#define os_SecVLineColor (*(uint8_t*)0xD024EF)
962-
#define os_SecWLineColor (*(uint8_t*)0xD024F0)
971+
#define os_SecWLineColor (*(uint8_t*)0xD024F0)
963972

964973
#define os_AppErr1 ((char*)0xD025A9) /**< String [1] for custom error */
965974
#define os_AppErr2 ((char*)0xD025B6) /**< String [2] for custom error */
@@ -1560,7 +1569,7 @@ typedef enum {
15601569
#define tXmax 0x0B
15611570
#define tYmin 0x0C
15621571
#define tYmax 0x0D
1563-
#define tTmin 0x0E
1572+
#define tTmin 0x0E
15641573
#define tTmax 0x0F
15651574
#define tThetaMin 0x10
15661575
#define tThetaMax 0x11
@@ -1939,4 +1948,3 @@ typedef enum {
19391948
#endif
19401949

19411950
#endif
1942-

0 commit comments

Comments
 (0)