1 \ -*- coding: utf-8 -*-
3 \ ==============================================================================
4 \ routines RTC for MSP430FRxxxx
5 \ your target must have a LF_XTAL 32768Hz
6 \ ==============================================================================
8 \ to see kernel options, download FastForthSpecs.f
9 \ FastForth kernel minimal addons: MSP430ASSEMBLER, CONDCOMP
11 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
12 \ MSP_EXP430FR5739 MSP_EXP430FR5969 MSP_EXP430FR5994 MSP_EXP430FR6989
13 \ MSP_EXP430FR4133 CHIPSTICK_FR2433 MSP_EXP430FR2433 MSP_EXP430FR2355
16 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
18 \ or, from windows explorer:
19 \ drag and drop this file onto SendSourceFileToTarget.bat
20 \ then select your TARGET when asked.
22 \ ASSEMBLER REGISTERS USAGE
23 \ R4 to R7 must be saved before use and restored after
24 \ scratch registers Y to S are free for use
25 \ under interrupt, IP is free for use
27 \ PUSHM order : PSP,TOS, IP, S, T, W, X, Y, rEXIT,rDOVAR,rDOCON, rDODOES, R3, SR,RSP, PC
28 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8, R7 , R6 , R5 , R4 , R3, R2, R1, R0
30 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
32 \ POPM order : PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT, Y, X, W, T, S, IP,TOS,PSP
33 \ POPM order : R0, R1, R2, R3, R4 , R5 , R6 , R7 , R8, R9,R10,R11,R12,R13,R14,R15
35 \ example : POPM #6,IP pop Y,X,W,T,S,IP registers from return stack
38 \ FORTH conditionnals: unary{ 0= 0< 0> }, binary{ = < > U< }
40 \ ASSEMBLER conditionnal usage with IF UNTIL WHILE S< S>= U< U>= 0= 0<> 0>=
41 \ ASSEMBLER conditionnal usage with ?JMP ?GOTO S< S>= U< U>= 0= 0<> 0<
50 0<> IF MOV #0,TOS THEN \ if TOS <> 0 (FIXPOINT input), set TOS = 0
53 SUB #307,TOS \ FastForth V3.7
55 $0D EMIT \ return to column 1 without CR
56 ABORT" FastForth version = 3.7 please!"
57 ABORT" target without LF_XTAL !"
58 PWR_STATE \ if no abort remove this word
63 ; --------------------
65 ; --------------------
68 \ to set date, type : d m y DATE!
69 \ to view date, type DATE?
70 \ to set time, type : h m s TIME!, or h m TIME!
71 \ to view time, type TIME?
73 [DEFINED] {RTC} [IF] {RTC} [THEN]
75 MARKER {RTC} \ restore the state before MARKER definition
76 \ {RTC}+8 = BODY+4 = RET_ADR: MARKER_DOES does a call to RET_ADR by default
77 8 ALLOT \ make room for:
78 \ {RTC}+10 for content of previous RTC_VEC
79 \ {RTC}+12 for content of previous COLD_PFA
80 \ {RTC}+14 for content of previous WARM_PFA
81 \ {RTC}+16 for content of previous SLEEP_PFA
85 \ https://forth-standard.org/standard/core/OR
86 \ C OR x1 x2 -- x3 logical OR
94 \ https://forth-standard.org/standard/core/CFetch
95 \ C@ c-addr -- char fetch char from memory
103 \ https://forth-standard.org/standard/core/CStore
104 \ C! char c-addr -- store char in memory
106 MOV.B @PSP+,0(TOS) \ 4
113 [UNDEFINED] SWAP [IF]
114 \ https://forth-standard.org/standard/core/SWAP
115 \ SWAP x1 x2 -- x2 x1 swap top two items
124 [UNDEFINED] OVER [IF]
125 \ https://forth-standard.org/standard/core/OVER
126 \ OVER x1 x2 -- x1 x2 x1
128 MOV TOS,-2(PSP) \ 3 -- x1 (x2) x2
129 MOV @PSP,TOS \ 2 -- x1 (x2) x1
130 SUB #2,PSP \ 1 -- x1 x2 x1
135 [UNDEFINED] DUP [IF] \define DUP and DUP?
136 \ https://forth-standard.org/standard/core/DUP
137 \ DUP x -- x x duplicate top of stack
139 BW1 SUB #2,PSP \ 2 push old TOS..
140 MOV TOS,0(PSP) \ 3 ..onto stack
144 \ https://forth-standard.org/standard/core/qDUP
145 \ ?DUP x -- 0 | x x DUP if nonzero
147 CMP #0,TOS \ 2 test for TOS nonzero
153 [UNDEFINED] DROP [IF]
154 \ https://forth-standard.org/standard/core/DROP
155 \ DROP x -- drop top of stack
162 [UNDEFINED] DEPTH [IF]
163 \ https://forth-standard.org/standard/core/DEPTH
164 \ DEPTH -- +n number of items on stack, must leave 0 if stack empty
168 SUB PSP,TOS \ PSP-S0--> TOS
169 RRA TOS \ TOS/2 --> TOS
170 SUB #2,PSP \ post decrement stack...
176 \ https://forth-standard.org/standard/core/toR
177 \ >R x -- R: -- x push to return stack
186 \ https://forth-standard.org/standard/core/Rfrom
187 \ R> -- x R: x -- pop from return stack ; CALL #RFROM performs DOVAR
197 \ https://forth-standard.org/standard/core/OnePlus
198 \ 1+ n1/u1 -- n2/u2 add 1 to TOS
207 SUB @PSP+,TOS \ 2 u2-u1
211 AND #0,TOS \ 1 flag Z = 1
219 \ https://forth-standard.org/standard/core/Equal
220 \ = x1 x2 -- flag test x1=x2
227 XOR #-1,TOS \ 1 flag Z = 1
232 [UNDEFINED] IF [IF] \ define IF THEN
233 \ https://forth-standard.org/standard/core/IF
234 \ IF -- IFadr initialize conditional forward branch
238 MOV &DP,TOS \ -- HERE
239 ADD #4,&DP \ compile one word, reserve one word
240 MOV #QFBRAN,0(TOS) \ -- HERE compile QFBRAN
241 ADD #2,TOS \ -- HERE+2=IFadr
245 \ https://forth-standard.org/standard/core/THEN
246 \ THEN IFadr -- resolve forward branch
247 CODE THEN \ immediate
248 MOV &DP,0(TOS) \ -- IFadr
254 [UNDEFINED] ELSE [IF]
255 \ https://forth-standard.org/standard/core/ELSE
256 \ ELSE IFadr -- ELSEadr resolve forward IF branch, leave ELSEadr on stack
257 CODE ELSE \ immediate
258 ADD #4,&DP \ make room to compile two words
261 MOV W,0(TOS) \ HERE+4 ==> [IFadr]
263 MOV W,TOS \ -- ELSEadr
268 [UNDEFINED] DO [IF] \ define DO LOOP +LOOP
269 \ https://forth-standard.org/standard/core/DO
270 \ DO -- DOadr L: -- 0
274 ADD #2,&DP \ make room to compile xdo
275 MOV &DP,TOS \ -- HERE+2
276 MOV #XDO,-2(TOS) \ compile xdo
277 ADD #2,&LEAVEPTR \ -- HERE+2 LEAVEPTR+2
279 MOV #0,0(W) \ -- HERE+2 L-- 0
283 \ https://forth-standard.org/standard/core/LOOP
284 \ LOOP DOadr -- L-- an an-1 .. a1 0
285 CODE LOOP \ immediate
287 BW1 ADD #4,&DP \ make room to compile two words
289 MOV X,-4(W) \ xloop --> HERE
290 MOV TOS,-2(W) \ DOadr --> HERE+2
291 BEGIN \ resolve all "leave" adr
292 MOV &LEAVEPTR,TOS \ -- Adr of top LeaveStack cell
293 SUB #2,&LEAVEPTR \ --
294 MOV @TOS,TOS \ -- first LeaveStack value
295 CMP #0,TOS \ -- = value left by DO ?
297 MOV W,0(TOS) \ move adr after loop as UNLOOP adr
303 \ https://forth-standard.org/standard/core/PlusLOOP
304 \ +LOOP adrs -- L-- an an-1 .. a1 0
307 GOTO BW1 \ goto BW1 LOOP
311 [UNDEFINED] CASE [IF]
312 \ https://forth-standard.org/standard/core/CASE
313 : CASE 0 ; IMMEDIATE \ -- #of-1
315 \ https://forth-standard.org/standard/core/OF
316 : OF \ #of-1 -- orgOF #of
318 >R \ move off the stack in case the control-flow stack is the data stack.
319 POSTPONE OVER POSTPONE = \ copy and test case value
320 POSTPONE IF \ add orig to control flow stack
321 POSTPONE DROP \ discards case value if =
322 R> \ we can bring count back now
325 \ https://forth-standard.org/standard/core/ENDOF
326 : ENDOF \ orgOF #of -- orgENDOF #of
327 >R \ move off the stack in case the control-flow stack is the data stack.
329 R> \ we can bring count back now
332 \ https://forth-standard.org/standard/core/ENDCASE
333 : ENDCASE \ orgENDOF1..orgENDOFn #of --
342 \ https://forth-standard.org/standard/core/Plus
343 \ + n1/u1 n2/u2 -- n3/u3
351 \ https://forth-standard.org/standard/core/Minus
352 \ - n1/u1 n2/u2 -- n3/u3 n3 = n1-n2
354 SUB @PSP+,TOS \ 2 -- n2-n1 ( = -n3)
356 ADD #1,TOS \ 1 -- n3 = -(n2-n1) = n1-n2
361 [UNDEFINED] MAX [IF] \define MAX and MIN
363 CODE MAX \ n1 n2 -- n3 signed maximum
370 CODE MIN \ n1 n2 -- n3 signed minimum
380 \ https://forth-standard.org/standard/core/TwoTimes
381 \ 2* x1 -- x2 arithmetic left shift
389 \ https://forth-standard.org/standard/core/UMTimes
390 \ UM* u1 u2 -- ud unsigned 16x16->32 mult.
392 MOV @PSP,&MPY \ Load 1st operand for unsigned multiplication
393 MOV TOS,&OP2 \ Load 2nd operand
394 MOV &RES0,0(PSP) \ low result on stack
395 MOV &RES1,TOS \ high result in TOS
400 [UNDEFINED] UM/MOD [IF]
401 \ https://forth-standard.org/standard/core/UMDivMOD
402 \ UM/MOD udlo|udhi u1 -- ur uq unsigned 32/16->r16 q16
405 MOV #MUSMOD,PC \ execute MUSMOD then return to DROP
410 \ U*/ u1 u2 u3 -- uq u1*u2/u3
412 >R UM* R> UM/MOD SWAP DROP
416 [UNDEFINED] U/MOD [IF]
417 \ U/MOD u1 u2 -- ur uq unsigned division
424 \ https://forth-standard.org/standard/core/Div
425 \ U/ u1 u2 -- uq signed quotient
431 [UNDEFINED] SPACES [IF]
432 \ https://forth-standard.org/standard/core/SPACES
433 \ SPACES n -- output n spaces
447 MOV @PSP+,TOS \ -- drop n
452 [UNDEFINED] HERE [IF]
459 : U.R \ u n -- display u unsigned in n width (n >= 2)
461 R> OVER - 0 MAX SPACES TYPE
465 $81EF DEVICEID @ U< ; search device ID: MSP430FR4133 or...
466 DEVICEID @ $8241 U< ; ...MSP430FR2433
468 $830B DEVICEID @ U< ; MSP430FR21xx/23xx/24xx/25xx/26xx
473 \ ==============================================================================
474 \ driver for RTC without calendar
475 \ ==============================================================================
477 CREATE RTCSEC 2 ALLOT
478 CREATE RTCMIN 2 ALLOT
479 CREATE RTCHOUR 2 ALLOT
480 CREATE RTCDOW 2 ALLOT
481 CREATE RTCDAY 2 ALLOT
482 CREATE RTCMON 2 ALLOT
483 CREATE RTCYEAR 2 ALLOT
485 \ ************************************\
486 CODE RTC_INT \ computes sec min hour day month year
487 \ ************************************\
488 ADD #2,RSP \ remove previous_SR
489 BIT #1,&RTCIV \ clear RTC_IFG
490 ADD.B #1,&RTCSEC \ sec+1
493 MOV.B #0,&RTCSEC \ sec=0
494 ADD.B #1,&RTCMIN \ min+1
497 MOV.B #0,&RTCMIN \ min=0
498 ADD.B #1,&RTCHOUR \ hour+1
501 MOV.B #0,&RTCHOUR \ hour=0
502 ADD.B #1,&RTCDOW \ dow+1
505 MOV.B #0,&RTCDOW \ dow=0
507 ADD.B #1,&RTCDAY \ day+1
508 CMP.B #2,&RTCMON \ February month ?
509 \ ------------------------\ here we compute leap year
526 MOV TOS,X \ X = 29|30
528 \ ------------------------\
529 ELSE \ month other than Feb
533 0>= IF \ month >= August?
538 ADD #1,X \ 31 days / month
542 U>= IF \ max day of month is exceeded
543 MOV.B #1,&RTCDAY \ day=1
544 ADD.B #1,&RTCMON \ mon+1
547 MOV.B #1,&RTCMON \ mon=1
548 ADD #1,&RTCYEAR \ year+1
554 MOV @RSP+,PC \ RET to BACKGrouND routine, with GIE disabled
557 \ ------------------------\
558 ASM STOP_RTC \ define STOP_RTC as new COLD_APP subroutine, called by {RTC}|WIPE|RST|COLD|SYS_failures.
559 \ ------------------------\ ------------------------------------------
560 CMP #RET_ADR,&{RTC}+8 \
561 0<> IF \ and only if RTC_APP is started by START_RTC
563 MOV #RET_ADR,-2(X) \ restore {RTC}+8 default value
564 MOV @X+,&RTC_VEC \ restore previous RTC_VEC content from {RTC}+10
565 MOV @X+,&COLD+2 \ restore previous STOP_APP from {RTC}+12 to COLD_PFA
566 MOV @X+,&WARM+2 \ restore previous INI_APP from {RTC}+14 to WARM_PFA
567 \ MOV @X+,&SLEEP+2 \ restore previous BACKGND_APP from {RTC}+16 to SLEEP_PFA
569 \ ------------------------\
570 MOV #0,&RTCCTL \ stops RTC and RTC_INT, see RTC15 in MSP430FR2xxx errata sheet
571 MOV.B #XIN,X \ X = bit_position of XT1 Xtal
572 BIC.B X,&XT1_SEL \ XIN as GPIO
573 BIS.B X,&XT1_DIR \ XIN as output
574 BIC.B X,&XT1_OUT \ RTC15 :"toggle twice XIN ouput"
575 BIS.B X,&XT1_OUT \ "with at least 2 rising or falling edges".
578 BIC.B X,&XT1_DIR \ restore default state of XIN
579 BIS.B X,&XT1_SEL \ XIN as XT1 input
580 \ ------------------------\
581 MOV &COLD+2,PC \ 5 link (branch) to the previous STOP_APP subroutine,
582 \ ------------------------\ then RET to MARKER_DOES or to COLD+4
584 \ ------------------------\
586 \ ----------------------------------------\
587 ASM INI_RTC \ define INI_HDWR_APP called first by START_RTC then by WARM
588 \ ----------------------------------------\ ---------------------------------------------------------
589 CALL &{RTC}+14 \ call previous INI_APP (which sets TOS = RSTIV_MEM)
590 CMP #0,&RTCCTL \ if RTCCTL = 0 = reset state, app is STOPPED and must to be started
591 0= IF \ and if RTCCTL <> 0, we don't restart app and no time is lost.
592 MOV #$7F,&RTCMOD \ RTCMOD = 127
593 BIT #-1,&RTCIV \ clear RTC_IFG
594 MOV #%0010_0110_0100_0010,&RTCCTL \ starts RTC with XT1CLK/256, enables RTC_INT
596 MOV @RSP+,PC \ RET to BODYWARM|START_RTC
598 \ ----------------------------------------\
600 \\ -------------------------------------------------------------------------------
601 \\ WARNING! because RTC_INT have higher priority than eUSCI used for TERMINAL,
602 \\ BACKGND_APP default subroutine execute pending RTC_INT, so you can download a file without RTC time lost.
603 \\ but if you manualy type a command, pending RTC_INT may not be executed during this time.
604 \\ -------------------------------------------------------------------------------
605 \\ --------------------\
606 \\ ASM BACKGND_RTC \ define BACKGND_RTC to replace actual BACKGND_APP
607 \\ --------------------\
609 \ MOV &LPM_MODE,SR \ enter to SLEEP mode, waiting RTC_INT
610 \ AGAIN \ loop back to BEGIN is executed before CPU shut down
611 \\ --------------------\
613 \\ -------------------------------------------------------------------------------
614 \\ WARNING! because unlinked, this BACKGND_APP doesn't execute XON, TERMINAL is MUTEd
615 \\ but maybe that is what you want: RTC time keeps its accuracy.
616 \\ -------------------------------------------------------------------------------
618 \ --------------------------------\
619 CODE START_RTC \ save current content of WARM_PFA, COLD_PFA, SLEEP_PFA, RTC_VEC
620 \ --------------------------------\ then replace them by INI_RTC, STOP_RTC, BACKGND_RTC, RTC_INT then execute INI_RTC.
621 CMP #STOP_RTC,&{RTC}+8 \ content of {RTC}+8 = STOP_RTC ?
623 MOV #STOP_RTC,&{RTC}+8 \ STOP_RTC must be executed by MARKER_DOES of {RTC}, else RTC15 hangs out!
624 MOV &RTC_VEC,&{RTC}+10 \ save content of RTC_VEC to {RTC}+10...
625 MOV #RTC_INT,&RTC_VEC \ then set RTC_VEC with RTC_INT
626 MOV &COLD+2,&{RTC}+12 \ save content of COLD_PFA to {RTC}+12...
627 MOV #STOP_RTC,&COLD+2 \ ...and replace it by STOP_RTC, else RTC15 hangs out with Deep_RST!
628 MOV &WARM+2,&{RTC}+14 \ save content of WARM_PFA to {RTC}+14...
629 MOV #INI_RTC,&WARM+2 \ ...and replace it by INI_RTC
630 \ MOV &SLEEP+2,&{RTC}+16 \ save content of SLEEP_PFA to {RTC}+16...
631 \ MOV #BACKGND_RTC,&SLEEP+2 \ ...and replace it by BACKGND_RTC
635 \ --------------------------------\
637 \ --------------------------------\
639 : TIME? \ display time
640 RTCHOUR C@ 2 U.R $3A EMIT
641 RTCMIN C@ 2 U.R $3A EMIT
645 : TIME! \ hour min sec ---
646 START_RTC \ if not yet done, obviously!
648 U< IF \ if 3 numbers on stack
656 : DATE? \ display date
660 \ ==============================================================================
661 \ driver RTC for RTC_B|RTC_C hardware with calendar
662 \ ==============================================================================
666 BIT.B #RTCRDY,&RTCCTL1
667 0<> UNTIL \ wait until RTCRDY high
669 RTCHOUR C@ 2 U.R $3A EMIT
670 RTCMIN C@ 2 U.R $3A EMIT
676 U< IF \ if 3 numbers on stack
684 CODE DATE? \ display date
686 BIT.B #RTCRDY,&RTCCTL1
687 0<> UNTIL \ wait until windows time RTC_ReaDY is high
692 \ ==============================================================================
693 \ end of RTC software|harware calendar
694 \ ==============================================================================
695 \ resume with common part of DATE? definition:
697 RTCDOW C@ \ -- weekday {0=Sat...6=Fri}
709 RTCDAY C@ \ -- year mon day
711 2 U.R $2F EMIT \ -- year mon
712 2 U.R $2F EMIT \ -- year
718 : DATE! \ year mon day --
720 U< IF \ if 3 numbers on stack
727 RTCYEAR @ \ -- day mon year
728 \ ------------------------------------------
729 \ Zeller's congruence for gregorian calendar
730 \ see https://www.rosettacode.org/wiki/Day_of_the_week#Forth
731 \ : ZELLER \ day mon year -- weekday {0=Sat, ..., 6=Fri}
733 \ IF 1- SWAP 12 + SWAP
734 \ THEN \ -- d m' y' with m' {3=March, ..., 14=february}
735 \ 100 /MOD \ -- d m' K J with K = y' in century, J = century
736 \ DUP 4 / SWAP 2* - \ -- d m' K (J/4 - 2J)
737 \ SWAP DUP 4 / + + \ -- d m' ((J/4 - 2J) + (K + K/4))
738 \ SWAP 1+ 13 5 */ + + \ -- (d + (((J/4 - 2J) + (K + K/4)) + (m+1)*13/5))
739 \ 7 MOD \ -- weekday = {0=Sat, ..., 6=Fri}
740 \ ------------------------------------------
743 THEN \ -- d m' y' with m' {3=March, ..., 14=february}
744 100 U/MOD \ -- d m' K J with K = y' in century, J = century
745 DUP 4 U/ SWAP 2* - \ -- d m' K (J/4 - 2J)
746 SWAP DUP 4 U/ + + \ -- d m' ((J/4 - 2J) + (K + K/4))
747 SWAP 1+ 13 5 U*/ + + \ -- (d + (((J/4 - 2J) + (K + K/4)) + (m+1)*13/5))
748 7 U/MOD DROP \ -- weekday = {0=Sat, ..., 6=Fri}
749 \ ------------------------------------------
757 CODE S_ \ Squote alias with blank instead quote separator
758 MOV #0,&CAPS \ turn CAPS OFF
760 XSQUOTE , \ compile run-time code
761 $20 WORD \ -- c-addr (= HERE)
763 MOV.B @TOS,TOS \ -- len compile string
764 ADD #1,TOS \ -- len+1
766 ADDC TOS,&DP \ store aligned DP
768 MOV @RSP+,IP \ pop paired with push COLON
769 MOV #$20,&CAPS \ turn CAPS ON (default state)
777 0= IF MOV @IP+,PC \ interpret time usage disallowed
781 POSTPONE LITERAL \ compile-time code : lit $1B
782 POSTPONE EMIT \ compile-time code : EMIT
783 POSTPONE S_ \ compile-time code : S_ <escape_sequence>
784 POSTPONE TYPE \ compile-time code : TYPE
788 [UNDEFINED] >BODY [IF]
789 \ https://forth-standard.org/standard/core/toBODY
790 \ >BODY -- addr leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
797 [UNDEFINED] EXECUTE [IF] \ "
798 \ https://forth-standard.org/standard/core/EXECUTE
799 \ EXECUTE i*x xt -- j*x execute Forth word at 'xt'
803 MOV @RSP+,PC \ 4 xt --> PC
807 [UNDEFINED] EVALUATE [IF]
808 \ https://forth-standard.org/standard/core/EVALUATE
809 \ EVALUATE \ i*x c-addr u -- j*x interpret string
811 MOV #SOURCE_LEN,X \ 2
812 MOV @X+,S \ 2 S = SOURCE_LEN
813 MOV @X+,T \ 2 T = SOURCE_ORG
814 MOV @X+,W \ 2 W = TOIN
815 PUSHM #4,IP \ 6 PUSHM IP,S,T,W
820 MOV @RSP+,&SOURCE_ORG \ 4
821 MOV @RSP+,&SOURCE_LEN \ 4
828 ESC [8;42;96t \ set terminal display 42L * 96C
829 42 0 DO CR LOOP \ to avoid erasing any line of source, create 42 empty lines
830 ESC [H \ then set cursor home
833 ['] ACCEPT >BODY \ find default part of deferred ACCEPT (from terminal input)
834 EXECUTE \ wait human input for D M Y
835 EVALUATE \ interpret this input
839 ['] ACCEPT >BODY \ find default part of deferred ACCEPT (from terminal input)
840 EXECUTE \ wait human input for H M S
841 EVALUATE \ interpret this input
843 RST_STATE \ remove code beyond RST_HERE