1 \ -*- coding: utf-8 -*-
7 \ ==============================================================================
8 \ routines RTC for MSP430fr5xxx and MSP430FR6xxx families only
9 \ your target must have a LF_XTAL 32768Hz
10 \ if no present, add a LF_XTAL line for your target in ThingsInFirst.inc.
11 \ ==============================================================================
13 \ to see kernel options, download FastForthSpecs.f
14 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP
17 \ MSP_EXP430FR5739 MSP_EXP430FR5969 MSP_EXP430FR5994 MSP_EXP430FR6989
20 \ R4 to R7 must be saved before use and restored after
21 \ scratch registers Y to S are free for use
22 \ under interrupt, IP is free for use
24 \ PUSHM order : PSP,TOS, IP, S, T, W, X, Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
25 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8, R7 , R6 , R5 , R4 , R3, R2, R1, R0
27 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
29 \ POPM order : PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT, Y, X, W, T, S, IP,TOS,PSP
30 \ POPM order : R0, R1, R2, R3, R4 , R5 , R6 , R7 , R8, R9,R10,R11,R12,R13,R14,R15
32 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
35 \ FORTH conditionnals: unary{ 0= 0< 0> }, binary{ = < > U< }
37 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE S< S>= U< U>= 0= 0<> 0>=
38 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO S< S>= U< U>= 0= 0<> 0<
41 \ to set date, type : d m y DATE!
42 \ to view date, type DATE?
43 \ to set time, type : h m s TIME!, or h m TIME!
44 \ to view time, type TIME?
46 \ allow to write a file on a SD_Card with a valid date and a valid time
51 [DEFINED] {RTC} [IF] {RTC} [THEN]
55 [UNDEFINED] IF [IF] \ define IF THEN
56 \ https://forth-standard.org/standard/core/IF
57 \ IF -- IFadr initialize conditional forward branch
62 ADD #4,&DP \ compile one word, reserve one word
63 MOV #QFBRAN,0(TOS) \ -- HERE compile QFBRAN
64 ADD #2,TOS \ -- HERE+2=IFadr
68 \ https://forth-standard.org/standard/core/THEN
69 \ THEN IFadr -- resolve forward branch
71 MOV &DP,0(TOS) \ -- IFadr
80 ECHO $0D EMIT \ return to column 0
81 ABORT" no RTC on this device !"
86 \ https://forth-standard.org/standard/core/Fetch
87 \ @ c-addr -- char fetch char from memory
96 SUB @PSP+,TOS \ 2 u2-u1
100 AND #0,TOS \ 1 flag Z = 1
108 \ https://forth-standard.org/standard/core/Equal
109 \ = x1 x2 -- flag test x1=x2
116 XOR #-1,TOS \ 1 flag Z = 1
122 \ https://forth-standard.org/standard/core/OR
123 \ C OR x1 x2 -- x3 logical OR
131 $81EF DEVICEID @ U< ; MSP430FR4133 or...
132 DEVICEID @ $8241 U< ; ...MSP430FR2433
134 $830B DEVICEID @ U< ; MSP430FR21xx/23xx/24xx/25xx/26xx
135 OR ; -- flag 0 ==> RTC, -1 ==> no RTC
138 [UNDEFINED] SWAP [IF]
139 \ https://forth-standard.org/standard/core/SWAP
140 \ SWAP x1 x2 -- x2 x1 swap top two items
149 [UNDEFINED] OVER [IF]
150 \ https://forth-standard.org/standard/core/OVER
151 \ OVER x1 x2 -- x1 x2 x1
153 MOV TOS,-2(PSP) \ 3 -- x1 (x2) x2
154 MOV @PSP,TOS \ 2 -- x1 (x2) x1
155 SUB #2,PSP \ 1 -- x1 x2 x1
160 [UNDEFINED] EXECUTE [IF] \ "
161 \ https://forth-standard.org/standard/core/EXECUTE
162 \ EXECUTE i*x xt -- j*x execute Forth word at 'xt'
164 MOV TOS,W \ 1 put word address into W
165 MOV @PSP+,TOS \ 2 fetch new TOS
166 MOV W,PC \ 3 fetch code address into PC
170 [UNDEFINED] DO [IF] \ define DO LOOP +LOOP
171 \ https://forth-standard.org/standard/core/DO
172 \ DO -- DOadr L: -- 0
176 ADD #2,&DP \ make room to compile xdo
177 MOV &DP,TOS \ -- HERE+2
178 MOV #XDO,-2(TOS) \ compile xdo
179 ADD #2,&LEAVEPTR \ -- HERE+2 LEAVEPTR+2
181 MOV #0,0(W) \ -- HERE+2 L-- 0
185 \ https://forth-standard.org/standard/core/LOOP
186 \ LOOP DOadr -- L-- an an-1 .. a1 0
187 CODE LOOP \ immediate
189 BW1 ADD #4,&DP \ make room to compile two words
191 MOV X,-4(W) \ xloop --> HERE
192 MOV TOS,-2(W) \ DOadr --> HERE+2
193 BEGIN \ resolve all "leave" adr
194 MOV &LEAVEPTR,TOS \ -- Adr of top LeaveStack cell
195 SUB #2,&LEAVEPTR \ --
196 MOV @TOS,TOS \ -- first LeaveStack value
197 CMP #0,TOS \ -- = value left by DO ?
199 MOV W,0(TOS) \ move adr after loop as UNLOOP adr
205 \ https://forth-standard.org/standard/core/PlusLOOP
206 \ +LOOP adrs -- L-- an an-1 .. a1 0
209 GOTO BW1 \ goto BW1 LOOP
214 \ https://forth-standard.org/standard/core/Minus
215 \ - n1/u1 n2/u2 -- n3/u3 n3 = n1-n2
217 SUB @PSP+,TOS \ 2 -- n2-n1 ( = -n3)
219 ADD #1,TOS \ 1 -- n3 = -(n2-n1) = n1-n2
224 [UNDEFINED] MAX [IF] \define MAX and MIN
226 CODE MAX \ n1 n2 -- n3 signed maximum
233 CODE MIN \ n1 n2 -- n3 signed minimum
242 [UNDEFINED] SPACES [IF]
243 \ https://forth-standard.org/standard/core/SPACES
244 \ SPACES n -- output n spaces
258 MOV @PSP+,TOS \ -- drop n
263 [UNDEFINED] DUP [IF] \define DUP and DUP?
264 \ https://forth-standard.org/standard/core/DUP
265 \ DUP x -- x x duplicate top of stack
267 BW1 SUB #2,PSP \ 2 push old TOS..
268 MOV TOS,0(PSP) \ 3 ..onto stack
272 \ https://forth-standard.org/standard/core/qDUP
273 \ ?DUP x -- 0 | x x DUP if nonzero
275 CMP #0,TOS \ 2 test for TOS nonzero
281 [UNDEFINED] DEPTH [IF]
282 \ https://forth-standard.org/standard/core/DEPTH
283 \ DEPTH -- +n number of items on stack, must leave 0 if stack empty
287 SUB PSP,TOS \ PSP-S0--> TOS
288 RRA TOS \ TOS/2 --> TOS
289 SUB #2,PSP \ post decrement stack...
295 \ https://forth-standard.org/standard/core/toR
296 \ >R x -- R: -- x push to return stack
305 \ https://forth-standard.org/standard/core/Rfrom
306 \ R> -- x R: x -- pop from return stack ; CALL #RFROM performs DOVAR
315 [UNDEFINED] >BODY [IF]
316 \ https://forth-standard.org/standard/core/toBODY
317 \ >BODY -- addr leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
324 [UNDEFINED] EVALUATE [IF]
325 \ https://forth-standard.org/standard/core/EVALUATE
326 \ EVALUATE \ i*x c-addr u -- j*x interpret string
328 MOV #SOURCE_LEN,X \ 2
329 MOV @X+,S \ 2 S = SOURCE_LEN
330 MOV @X+,T \ 2 T = SOURCE_ORG
331 MOV @X+,W \ 2 W = TOIN
332 PUSHM #4,IP \ 6 PUSHM IP,S,T,W
337 MOV @RSP+,&SOURCE_ORG \ 4
338 MOV @RSP+,&SOURCE_LEN \ 4
344 [UNDEFINED] EXECUTE [IF] \ "
345 \ https://forth-standard.org/standard/core/EXECUTE
346 \ EXECUTE i*x xt -- j*x execute Forth word at 'xt'
348 MOV TOS,W \ 1 put word address into W
349 MOV @PSP+,TOS \ 2 fetch new TOS
350 MOV W,PC \ 3 fetch code address into PC
355 : U.R \ u n -- display u unsigned in n width (n >= 2)
357 R> OVER - 0 MAX SPACES TYPE
365 BIT.B #RTCRDY,&RTCCTL1 \ test RTCRDY flag
366 0<> UNTIL \ wait until RTCRDY high
367 MOV &RTCYEARL,2(PSP) \ year
369 MOV TOS,0(PSP) \ month
370 MOV.B &RTCDAY,TOS \ day
379 MOV TOS,&RTCYEARL \ year
380 MOV.B @PSP,&RTCMON \ month \ @PSP+ don't work because byte format !
381 MOV.B 2(PSP),&RTCDAY \ day \ @PSP+ don't work because byte format !
391 MOV TOS,4(PSP) \ save TOS
393 BIT.B #RTCRDY,&RTCCTL1 \
394 0<> UNTIL \ wait until RTCRDY high
396 MOV TOS,2(PSP) \ seconds
398 MOV TOS,0(PSP) \ minutes
399 MOV.B &RTCHOUR,TOS \ hours
408 MOV TOS,&RTCSEC \ seconds
409 MOV.B @PSP,&RTCMIN \ minutes \ @PSP+ don't work because byte format !
410 MOV.B 2(PSP),&RTCHOUR \ hours \ @PSP+ don't work because byte format !
422 CODE S_ \ Squote alias with blank instead quote separator
423 MOV #0,&CAPS \ turn CAPS OFF
425 XSQUOTE , \ compile run-time code
426 $20 WORD \ -- c-addr (= HERE)
428 MOV.B @TOS,TOS \ -- len compile string
429 ADD #1,TOS \ -- len+1
431 ADDC TOS,&DP \ store aligned DP
433 MOV @RSP+,IP \ pop paired with push COLON
434 MOV #$20,&CAPS \ turn CAPS ON (default state)
442 0= IF MOV @IP+,PC \ interpret time usage disallowed
446 POSTPONE LITERAL \ compile-time code : lit $1B
447 POSTPONE EMIT \ compile-time code : EMIT
448 POSTPONE S_ \ compile-time code : S_ <escape_sequence>
449 POSTPONE TYPE \ compile-time code : TYPE
453 : PAD_ACCEPT \ -- org len
455 DUP PAD_LEN \ -- org org len
457 $4030 = \ if CFA content = $4030 (MOV @PC+,PC), ACCEPT is deferred
458 IF >BODY \ find default part address of deferred ACCEPT
460 EXECUTE \ -- org len'
464 PWR_STATE \ all words after PWR_HERE marker will be lost
465 42 \ number of terminal lines
466 0 DO CR LOOP \ don't erase any line of source