OSDN Git Service

5b369a058a9350b891c549ec987dd16aba6052c7
[fast-forth/master.git] / MSP430-FORTH / RC5toLCD.f
1 \ -*- coding: utf-8 -*-
2
3 ; -----------------------------------
4 ; RC5TOLCD.f
5 ; -----------------------------------
6 \
7 \ to see kernel options, download FastForthSpecs.f
8 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP, FREQUENCY = 8/16/24 MHz
9 \
10 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
11 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
12 \ MSP_EXP430FR2355
13 \ LP_MSP430FR2476
14 \
15 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
16 \
17 \ OR
18 \
19 \ drag and drop this file onto SendSourceFileToTarget.bat
20 \ then select your TARGET when asked.
21 \
22 \
23 \ ================================================================================
24 \ REGISTERS USAGE for embedded MSP430 ASSEMBLER
25 \ ================================================================================
26 \ don't use R2, R3, R4
27 \ R5, R6, R7 must be PUSHed/POPed before/after use, OR restored after: MOV #{XDOCOL|XDOCON|R>},{rDODOES|rDOCON|rDOVAR}
28 \ scratch registers Y to S are free,
29 \ under interrupt, IP is free,
30 \ use FORTH rules for reg. TOS, PSP, RSP.
31 \
32 \ PUSHM order : PSP,TOS, IP, S , T , W , X , Y ,rDOVAR,rDOCON,rDODOES,rDOCOL, R3, SR,RSP, PC
33 \ PUSHM order : R15,R14,R13,R12,R11,R10, R9, R8,  R7  ,  R6  ,  R5   ,  R4  , R3, R2, R1, R0
34 \
35 \ example : PUSHM #6,IP pushes IP,S,T,W,X,Y registers to return stack
36 \
37 \ POPM  order :  PC,RSP, SR, R3, rDODOES,rDOCON,rDOVAR,rEXIT,  Y,  X,  W,  T,  S, IP,TOS,PSP
38 \ POPM  order :  R0, R1, R2, R3,   R4   ,  R5  ,  R6  ,  R7 , R8, R9,R10,R11,R12,R13,R14,R15
39 \
40 \ example : POPM #6,IP   pop Y,X,W,T,S,IP registers from return stack
41 \
42 \ ASSEMBLER conditionnal usage after IF UNTIL WHILE : S< S>= U< U>= 0= 0<> 0>=
43 \ ASSEMBLER conditionnal usage before      ?GOTO    : S< S>= U< U>= 0= 0<> 0<
44 \
45 \
46 \ display on a LCD 2x20 CHAR the code sent by an IR remote under philips RC5 protocol
47 \ target : any TI MSP-EXP430FRxxxx launchpad (FRAM)
48 \ LPM_MODE = LPM0 because use SMCLK for LCDVo
49 \
50 \ DEMO : driver for IR remote compatible with the PHILIPS RC5 protocol
51 \ plus : driver for 5V LCD 2x20 characters display with 4 bits data interface
52 \        without usage of an auxiliary 5V to feed the LCD_Vo
53 \        and without potentiometer to adjust the LCD contrast :
54 \        to adjust LCD contrast, just press S1 (-) or S2 (+)
55 \        LCDVo current consumption ~ 500 uA.
56 \
57 \ ===================================================================================
58 \ notice : adjust WDT_TIM_EX0,LCD_TIM_CTL,LCD_TIM_EX0 and 20_us to the target frequency if <> 8MHz !
59 \ ===================================================================================
60 \
61 \
62 \ layout : I/O are defined in the launchpad.pat file (don't work with ChipStick_FR2433)
63 \
64 \  GND  <-------o---0V0---------->  1 LCD_Vss
65 \  VCC  >-------|---3V6-----o---->  2 LCD_Vdd
66 \               |           |
67 \              ___    470n ---
68 \               ^          ---
69 \              / \ 1N4148   |
70 \              ---          |
71 \          100n |    2k2    |
72 \ TB0.2 >---||--o--^/\/\/v--o---->  3 LCD_Vo (= 0V6 without modulation)
73 \       ------------------------->  4 LCD_RW
74 \       ------------------------->  5 LCD_RW
75 \       ------------------------->  6 LCD_EN
76 \       <------------------------> 11 LCD_DB4
77 \       <------------------------> 12 LCD_DB5
78 \       <------------------------> 13 LCD_DB5
79 \       <------------------------> 14 LCD_DB7
80 \
81 \       <----- LCD contrast + <---    Sw1   <--- (finger) :-)
82 \       <----- LCD contrast - <---    Sw2   <--- (finger) :-)
83 \
84 \ rc5   <--- OUT IR_Receiver (1 TSOP32236)
85
86
87 \ first, we do some tests allowing the download
88     CODE ABORT_RC5TOLCD
89     SUB #2,PSP
90     MOV TOS,0(PSP)
91     MOV &VERSION,TOS
92     SUB #400,TOS        \ FastForth V4.0
93     COLON
94     'CR' EMIT           \ return to column 1 without 'LF'
95     ABORT" FastForth V4.0 please!"
96     RST_RET             \ remove ABORT_UARTI2CS definition before resuming
97     ;
98
99     ABORT_RC5TOLCD
100
101     MARKER {RC5TOLCD}   \ restore the state before MARKER definition
102 \                       \ {UARTI2CS}-2 = RET_ADR: by default MARKER_DOES does CALL #RET_ADR
103     8 ALLOT             \ {UARTI2CS}    make room to save previous HARD_APP address
104                         \ {RC5TOLCD}+2  make room to save previous WDT_TIM_0_VEC
105                         \ {RC5TOLCD}+4  make room to save previous IR_VEC
106                         \ {RC5TOLCD}+6  make room for 20 us count loop.
107
108 ; ------------------------------------------------------------------
109 ; first we download the set of definitions we need (from CORE_ANS.f)
110 ; ------------------------------------------------------------------
111
112 \ https://forth-standard.org/standard/core/Equal
113 \ =      x1 x2 -- flag         test x1=x2
114     [UNDEFINED] =
115     [IF]
116     CODE =
117     SUB @PSP+,TOS   \ 2
118     0<> IF          \ 2
119         AND #0,TOS  \ 1
120         MOV @IP+,PC \ 4
121     THEN
122     XOR #-1,TOS     \ 1 flag Z = 1
123     MOV @IP+,PC     \ 4
124     ENDCODE
125     [THEN]
126
127     [UNDEFINED] IF
128     [IF]     \ define IF and THEN
129 \ https://forth-standard.org/standard/core/IF
130 \ IF       -- IFadr    initialize conditional forward branch
131     CODE IF       \ immediate
132     SUB #2,PSP              \
133     MOV TOS,0(PSP)          \
134     MOV &DP,TOS             \ -- HERE
135     ADD #4,&DP              \           compile one word, reserve one word
136     MOV #QFBRAN,0(TOS)      \ -- HERE   compile QFBRAN
137     ADD #2,TOS              \ -- HERE+2=IFadr
138     MOV @IP+,PC
139     ENDCODE IMMEDIATE
140
141 \ https://forth-standard.org/standard/core/THEN
142 \ THEN     IFadr --                resolve forward branch
143     CODE THEN               \ immediate
144     MOV &DP,0(TOS)          \ -- IFadr
145     MOV @PSP+,TOS           \ --
146     MOV @IP+,PC
147     ENDCODE IMMEDIATE
148     [THEN]
149
150 \ https://forth-standard.org/standard/core/ELSE
151 \ ELSE     IFadr -- ELSEadr        resolve forward IF branch, leave ELSEadr on stack
152     [UNDEFINED] ELSE
153     [IF]
154     CODE ELSE     \ immediate
155     ADD #4,&DP              \ make room to compile two words
156     MOV &DP,W               \ W=HERE+4
157     MOV #BRAN,-4(W)
158     MOV W,0(TOS)            \ HERE+4 ==> [IFadr]
159     SUB #2,W                \ HERE+2
160     MOV W,TOS               \ -- ELSEadr
161     MOV @IP+,PC
162     ENDCODE IMMEDIATE
163     [THEN]
164
165 \ \ https://forth-standard.org/standard/core/DEFERStore
166 \ \ Set the word xt1 to execute xt2. An ambiguous condition exists if xt1 is not for a word defined by DEFER.
167 \     [UNDEFINED] IS
168 \     [IF]     \ define DEFER! and IS
169 \     CODE DEFER!             \ xt2 xt1 --
170 \     MOV @PSP+,2(TOS)        \ -- xt1=CFA_DEFER          xt2 --> [CFA_DEFER+2]
171 \     MOV @PSP+,TOS           \ --
172 \     MOV @IP+,PC
173 \     ENDCODE
174 \
175 \ \ https://forth-standard.org/standard/core/IS
176 \ \ IS <name>        xt --
177 \ \ used as is :
178 \ \ DEFER DISPLAY                         create a "do nothing" definition (2 CELLS)
179 \ \ inline command : ' U. IS DISPLAY      U. becomes the runtime of the word DISPLAY
180 \ \ or in a definition : ... ['] U. IS DISPLAY ...
181 \ \ KEY, EMIT, CR, ACCEPT and WARM are examples of DEFERred words
182 \ \
183 \ \ as IS replaces the PFA value of any word, it's a TO alias for VARIABLE and CONSTANT words...
184 \
185 \     : IS
186 \     STATEADR @
187 \     IF  POSTPONE ['] POSTPONE DEFER!
188 \     ELSE ' DEFER!
189 \     THEN
190 \     ; IMMEDIATE
191 \     [THEN]
192
193 \ https://forth-standard.org/standard/core/CR
194 \ CR      --               send CR+LF to the output device
195     [UNDEFINED] CR
196     [IF]
197 \ create a primary defered word, i.e. with its default runtime beginning at the >BODY of the definition
198     CODE CR     \ part I : DEFERed definition of CR
199     MOV #NEXT_ADR,PC                \ [PFA] = NEXT_ADR
200     ENDCODE
201
202     :NONAME
203     'CR' EMIT 'LF' EMIT
204     ; IS CR
205     [THEN]
206
207 \ https://forth-standard.org/standard/core/toBODY
208 \ >BODY     -- addr      leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
209     [UNDEFINED] >BODY
210     [IF]
211     CODE >BODY
212     ADD #4,TOS
213     MOV @IP+,PC
214     ENDCODE
215     [THEN]
216
217 ; --------------------------
218 ; end of definitions we need
219 ; --------------------------
220
221     CODE 20_US                      \ n --
222     BEGIN                           \          J_loop           8000    16000  24000  kHz
223         MOV &{RC5TOLCD}+6,X         \            3          X = {40      80     120}
224         SUB #2,X                    \           +1          X = {38      78     118} I_loops + 2 J_loops = {40 80 120} * 4 cycles
225         BEGIN                       \  I_loop
226             NOP                     \    1
227             SUB #1,X                \   +1
228         0=  UNTIL                   \   +2
229         NOP                         \           +1
230         SUB #1,TOS                  \           +1
231     0= UNTIL                        \           +2
232     MOV @PSP+,TOS                   \
233     MOV @RSP+,IP                    \
234     ENDCODE
235
236 \                                   \ if write : %xxxx_WWWW --
237 \                                   \ if read  : -- %0000_RRRR
238     CODE TOP_LCD                    \ LCD Sample
239     BIS.B #LCD_EN,&LCD_CMD_OUT      \ lcd_en 0-->1
240     BIT.B #LCD_RW,&LCD_CMD_IN       \ lcd_rw test
241     0= IF                           \ write LCD bits pattern
242         AND.B #LCD_DB,TOS           \
243         MOV.B TOS,&LCD_DB_OUT       \ send LCD_Data
244         BIC.B #LCD_EN,&LCD_CMD_OUT  \ lcd_en 1-->0 ==> strobe data
245         MOV @PSP+,TOS               \
246         MOV @IP+,PC
247     THEN                            \ read LCD bits pattern
248     SUB #2,PSP
249     MOV TOS,0(PSP)
250     BIC.B #LCD_EN,&LCD_CMD_OUT  \ lcd_en 1-->0 ==> strobe data
251     MOV.B &LCD_DB_IN,TOS        \ get LCD_Data
252     AND.B #LCD_DB,TOS           \
253     MOV @IP+,PC
254     ENDCODE
255
256     CODE LCD_WRC                \ char --         Write Char
257     BIS.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=1
258 BW1 SUB #2,PSP                  \
259     MOV TOS,0(PSP)              \ -- %HHHH_LLLL %HHHH_LLLL
260     RRUM #4,TOS                 \ -- %xxxx_LLLL %xxxx_HHHH
261     BIC.B #LCD_RW,&LCD_CMD_OUT  \ lcd_rw=0
262     BIS.B #LCD_DB,&LCD_DB_DIR   \ LCD_Data as output
263     COLON                       \ high level word starts here
264     TOP_LCD 2 20_US             \ write high nibble first
265     TOP_LCD 2 20_US
266     ;
267
268     CODE LCD_WRF                \ func --         Write Fonction
269     BIC.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=0
270     GOTO BW1
271     ENDCODE
272
273     : LCD_CLEAR $01 LCD_WRF 100 20_us ;    \  $01 LCD_WrF 80 20_us ==> bad init !
274     : LCD_HOME $02 LCD_WRF 100 20_us ;
275
276 \     CODE LCD_ENTRY_SET
277 \     BIS #$04,TOS
278 \ BW1 COLON
279 \     LCD_WrF
280 \     ;
281 \
282 \     CODE LCD_DSP_CTRL
283 \     BIS#$08,TOS
284 \     GOTO BW1
285 \     ENDCODE
286 \
287 \     CODE LCD_DSP_SHIFT
288 \     BIS#$10,TOS
289 \     GOTO BW1
290 \     ENDCODE
291 \
292 \     CODE LCD_FN_SET
293 \     BIS#$20,TOS
294 \     GOTO BW1
295 \     ENDCODE
296 \
297 \     CODE LCD_CGRAM_SET
298 \     BIS #$40,TOS
299 \     GOTO BW1
300 \     ENDCODE
301 \
302 \     CODE LCD_GOTO
303 \     BIS #$80,TOS
304 \     GOTO BW1
305 \     ENDCODE
306 \
307 \ CODE LCD_RDS                    \ -- status       Read Status
308 \     BIC.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=0
309 \ BW1 BIC.B #LCD_DB,&LCD_DB_DIR   \ LCD_Data as intput
310 \     BIS.B #LCD_RW,&LCD_CMD_OUT  \ lcd_rw=1
311 \ COLON                           \ starts a FORTH word
312 \     TOP_LCD 2 20_us             \ -- %0000_HHHH
313 \     TOP_LCD 2 20_us             \ -- %0000_HHHH %0000_LLLL
314 \ HI2LO                           \ switch from FORTH to assembler
315 \     RLAM #4,0(PSP)              \ -- %HHHH_0000 %0000_LLLL
316 \     ADD.B @PSP+,TOS             \ -- %HHHH_LLLL
317 \     MOV @RSP+,IP                \ restore IP saved by COLON
318 \     MOV @IP+,PC                 \
319 \ ENDCODE
320 \
321 \ CODE LCD_RDC                    \ -- char         Read Char
322 \     BIS.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=1
323 \     GOTO BW1
324 \ ENDCODE
325 \
326 \
327 \   ********************************\
328     HDNCODE WDT_INT                 \ Watchdog interrupt routine, warning : not FORTH executable !
329 \   ********************************\
330 \    XOR.B #LED1,&LED1_OUT           \ to visualise WDT
331     BIT.B #SW2,&SW2_IN              \ test switch S2
332     0= IF                           \ case of switch S2 pressed
333         CMP #19,&LCD_TIM_CCRn       \ maxi Ton = 19/20 & VDD=3V6 ==> LCD_Vo = -1V4
334         U< IF
335             ADD #1,&LCD_TIM_CCRn    \ action for switch S2 (P2.5) : 150 mV / increment
336         THEN
337     ELSE
338         BIT.B #SW1,&SW1_IN          \ test switch S1 input
339         0= IF                       \ case of Switch S1 pressed
340             CMP #3,&LCD_TIM_CCRn    \ mini Ton = 3/20 & VDD=3V6 ==> LCD_Vo = 0V
341             U>= IF                  \
342             SUB #1,&LCD_TIM_CCRn    \ action for switch S1 (P2.6) : -150 mV / decrement
343             THEN                    \
344         THEN                        \
345     THEN                            \
346     RETI                            \ 5
347     ENDCODE                         \
348 \   ********************************\
349
350 \   ********************************\
351     HDNCODE RC5_INT                 \   wake up on Px.RC5 change interrupt
352 \   ********************************\
353 \   IR_RC5 driver                   \ IP,S,T,W,X,Y registers are free for use
354 \   ********************************\
355 \                                   \ in :  SR(9)=old Toggle bit memory (ADD on)
356 \                                   \       SMclock = 8|16|24 MHz
357 \                                   \ use : T,W,X,Y, RC5_TIM_ timer, RC5_TIM_R register
358 \                                   \ out : X = 0 C6 C5 C4 C3 C2 C1 C0
359 \                                   \       SR(9)=new Toggle bit memory (ADD on)
360 \   ********************************\
361 \   RC5_FirstStartBitHalfCycle:     \
362 \   ********************************\
363     MOV #1778,X                     \ RC5_Period in us
364     MOV #14,W                       \ count of loop
365     BEGIN                           \
366 \       ****************************\
367 \       RC5_HalfCycle               \ <--- loop back ---+ with readjusted RC5_Period
368 \       ****************************\                   |
369       MOV #%1011100100,&RC5_TIM_CTL \ (re)start timer_A | SMCLK/8 time interval,free running,clear RC5_TIM__IFG and RC5_TIM_R
370 \       RC5_Compute_3/4_Period:     \                   |
371         RRUM    #1,X                \ X=1/2 cycle       |
372         MOV     X,Y                 \                   ^
373         RRUM    #1,Y                \ Y=1/4
374         ADD     X,Y                 \ Y=3/4 cycle
375         BEGIN                       \
376             CMP Y,&RC5_TIM_R        \ 3 wait 1/2 + 3/4 cycle = n+1/4 cycles
377         U>= UNTIL                   \ 2
378 \       ****************************\
379 \       RC5_SampleOnFirstQuarter    \ at n+1/4 cycles, we sample RC5_input, ST2/C6 bit first
380 \       ****************************\
381         BIT.B   #RC5,&IR_IN         \ C_flag = IR bit
382         ADDC    T,T                 \ C_flag <-- T(15):T(0) <-- C_flag
383         MOV.B   &IR_IN,&IR_IES      \ preset Px_IES.y state for next IFG
384         BIC.B   #RC5,&IR_IFG        \ clear Px_IFG.y after 4/4 cycle pin change
385         SUB     #1,W                \ decrement count loop
386 \                                   \  count = 13 ==> T = x  x  x  x  x  x  x  x |x  x  x  x  x  x  x /C6
387 \                                   \  count = 0  ==> T = x  x /C6 Tg A4 A3 A2 A1|A0 C5 C4 C3 C2 C1 C0  1
388     0<> WHILE                       \ ----> out of loop ----+
389         ADD X,Y                     \                       |   Y = n+3/4 cycles = time out because n+1/2 cycles edge is always present
390         BEGIN                       \                       |
391             MOV &RC5_TIM_R,X        \ 3                     |   X grows from n+1/4 up to n+3/4 cycles
392             CMP Y,X                 \ 1                 ^   |   cycle time out of bound ?
393             U>= ?GOTO FW1           \                   |   |   quit on truncated RC5 message
394             BIT.B #RC5,&IR_IFG      \ 3                 |   |   n+1/2 cycles edge is always present
395         0<> UNTIL                   \ 2                 |   |
396     REPEAT                          \ ----> loop back --+   |   with X = new RC5_period value
397 \   ********************************\                       |
398 \   RC5_SampleEndOf:                \ <---------------------+
399 \   ********************************\
400     BIC #$30,&RC5_TIM_CTL           \   stop timer
401 \   ********************************\
402 \   RC5_ComputeNewRC5word           \
403 \   ********************************\
404     RLAM    #1,T                    \ T =  x /C6 Tg A4 A3 A2 A1 A0|C5 C4 C3 C2 C1 C0  1  0
405     MOV.B   T,X                     \ X = C5 C4 C3 C2 C1 C0  1  0
406     RRUM    #2,X                    \ X =  0  0 C5 C4 C3 C2 C1 C0
407 \   ********************************\
408 \   RC5_ComputeC6bit                \
409 \   ********************************\
410     BIT     #BIT14,T                \ test /C6 bit in T
411     0= IF   BIS #BIT6,X             \ set C6 bit in X
412     THEN                            \ X =  0  C6 C5 C4 C3 C2 C1 C0
413 \   ********************************\
414 \   RC5_CommandByteIsDone           \
415 \   ********************************\
416 \   Only New_RC5_Command ADD_ON     \ use SR(10) bit as toggle bit
417 \   ********************************\
418     RRUM    #3,T                    \ new toggle bit = T(13) ==> T(10)
419     XOR     @RSP,T                  \ (new XOR old) Toggle bits
420     BIT     #UF10,T                 \ repeated RC5_command ?
421     0= ?GOTO FW2                    \ yes, RETI without UF10 change and without action !
422     XOR #UF10,0(RSP)                \ 5 toggle bit memory
423 \   ********************************\
424 \   Display IR_RC5 code             \
425 \   ********************************\
426     SUB #6,PSP                      \   -- x x x TOS
427     MOV TOS,4(PSP)                  \   -- TOS x x TOS
428     MOV &BASEADR,2(PSP)             \   -- TOS Base x TOS
429     MOV #$10,&BASEADR               \                               set hexadecimal base
430     MOV X,0(PSP)                    \   -- TOS Base RC5_code TOS    convert number to ascii low word = RC5 byte
431     MOV #0,TOS                      \   -- TOS Base RC5_code 0      convert double number to ascii
432     LO2HI                           \                               switch from assembler to FORTH
433     LCD_CLEAR                       \                               set LCD cursor at home
434     <# # #S #36 HOLD #>             \   -- TOS Base adr cnt         32 bits conversion as "$xx"
435     ['] LCD_WRC IS EMIT             \                               redirect EMIT to LCD
436     TYPE                            \   -- TOS Base                 display "$xx" on LCD
437     ['] EMIT >BODY IS EMIT          \                               restore EMIT
438     HI2LO                           \                               switch from FORTH to assembler
439     MOV @PSP+,&BASEADR              \   -- TOS                      restore current BASE
440     MOV @PSP+,TOS                   \   --
441 FW1 BIC #$30,&RC5_TIM_CTL           \ stop timer (case of truncated RC5 message)
442 FW2 BIC #%1111_1000,0(RSP)          \ force CPU Active Mode and disable GIE in saved SR
443     RETI                            \
444     ENDCODE                         \
445 \   ********************************\
446
447 \ define our STOP_APP
448 \ ----------------------------------\
449     HDNCODE STOP_R2L                \ called by STOP|INIT_R2L|{RC5TOLCD}
450 \ ----------------------------------\
451     CMP #WDT_INT,&WDT_TIM_0_VEC     \ value set by START
452     0= IF                           \ only if START is done
453         BIC.B #RC5,&IR_IE           \ clear I/O RC5_Int
454         BIC.B #RC5,&IR_IFG          \ clear I/O RC5_Int flag
455         MOV #0,&LCD_TIM_CTL         \ stop LCD_TIMER, clear LCD_TIMER IFG
456         MOV #0,&WDT_TIM_CTL         \ stop WDT_TIMER
457         MOV #0,&WDT_TIM_CCTL0       \ clear CCIFG0 disable CCIE0
458         MOV #{RC5TOLCD},W           \
459         MOV #RET_ADR,-2(W)          \ clear MARKER_DOES call
460         MOV @W+,&HARD_APP           \ restore previous ini_APP
461         MOV @W+,&WDT_TIM_0_VEC      \ restore Vector previous value
462         MOV @W+,&IR_VEC             \ restore Vector previous value
463     THEN
464     MOV @RSP+,PC                    \ RET to STOP|WARM+4|{RC5TOLCD}
465     ENDCODE
466 \ ----------------------------------\
467
468 \ ----------------------------------\
469     CODE STOP                       \ also called by INIT_R2L for some events
470 \ ----------------------------------\
471 BW1 CALL #STOP_R2L
472     COLON                           \
473     ECHO                            \
474     ." type START to start RC5toLCD"
475     ;
476 \ ----------------------------------\
477
478 \ this routine completes the INIT_HARD of FORTH, with INIT_HARD for this app.
479 \ ----------------------------------\
480     HDNCODE INIT_R2L                \ called by START|SYS
481 \ ----------------------------------\
482 \ LCD_TIM_CTL =  %0000 0010 1001 0100\$3C0
483 \                    - -             \CNTL Counter lentgh \ 00 = 16 bits
484 \                        --          \TBSSEL TimerB clock select \ 10 = SMCLK
485 \                           --       \ID input divider \ 10 = /4
486 \                             --     \MC Mode Control \ 01 = up to LCD_TIM_CCR0
487 \                                 -  \TBCLR TimerB Clear
488 \                                  - \TBIE
489 \                                   -\TBIFG
490 \ ----------------------------------\
491 \ LCD_TIM_CCTLx = %0000 0000 0110 0000\$3C{2,4,6,8,A,C,E}
492 \                  --                 \CM Capture Mode
493 \                    --               \CCIS
494 \                       -             \SCS
495 \                        --           \CLLD
496 \                          -          \CAP
497 \                            ---      \OUTMOD \ 011 = set/reset
498 \                               -     \CCIE
499 \                                 -   \CCI
500 \                                  -  \OUT
501 \                                   - \COV
502 \                                    -\CCIFG
503 \ ----------------------------------\
504 \ LCD_TIM_CCRx                      \
505 \ ----------------------------------\
506 \ LCD_TIM_EX0                       \
507 \ ----------------------------------\
508 \ set LCD_TIM_ to make 50kHz PWM    \ for LCD_Vo; works without interrupt
509 \ ----------------------------------\
510     MOV #%10_1101_0100,&LCD_TIM_CTL \ SMCLK/8, up mode, clear timer, no int, set IFG
511 \    MOV #0,&RC5_TIM_EX0             \ predivide by 1 in RC5_TIM_EX0 register, reset value
512 \    MOV #0,&LCD_TIM_EX0             \ predivide by 1 in LCD_TIM_EX0 register (8 MHZ)
513     FREQ_KHZ @ 16000 =
514     [IF]                            \ if 16 MHz
515         MOV #1,&RC5_TIM_EX0         \ predivide by 2 in RC5_TIM_EX0 register
516         MOV #1,&LCD_TIM_EX0         \ predivide by 2 in LCD_TIM_EX0 register (16 MHZ)
517     [THEN]
518     FREQ_KHZ @ 24000 =
519     [IF]                            \ if 24 MHz
520         MOV #2,&RC5_TIM_EX0         \ predivide by 3 in RC5_TIM_EX0 register
521         MOV #2,&LCD_TIM_EX0         \ predivide by 3 in LCD_TIM_EX0 register (24 MHZ)
522     [THEN]
523         MOV #19,&LCD_TIM_CCR0       \ 19+1=20*1us=20us
524 \ ----------------------------------\
525 \ set LCD_TIM_.2 to generate PWM for LCD_Vo
526 \ ----------------------------------\
527     MOV #%0110_0000,&LCD_TIM_CCTLn  \ output mode = set/reset \ clear CCIFG
528     MOV #10,&LCD_TIM_CCRn           \ contrast adjust : 10/20 ==> LCD_Vo = -0V6|+3V6 (Vcc=3V6)
529 \    MOV #12,&LCD_TIM_CCRn           \ contrast adjust : 12/20 ==> LCD_Vo = -1V4|+3V3 (Vcc=3V3)
530 \ ----------------------------------\
531     BIS.B #LCDVo,&LCDVo_DIR         \
532     BIS.B #LCDVo,&LCDVo_SEL         \ SEL.2
533 \ ----------------------------------\
534     BIS.B #LCD_CMD,&LCD_CMD_DIR     \ lcd_cmd as outputs
535     BIC.B #LCD_CMD,&LCD_CMD_REN     \ lcd_cmd pullup/down disable
536 \ ----------------------------------\
537     BIS.B #LCD_DB,&LCD_DB_DIR       \ as output, wired to DB(4-7) LCD_Data
538     BIC.B #LCD_DB,&LCD_DB_REN       \ LCD_Data pullup/down disable
539 \ ----------------------------------\
540 \ init RC5_Int                      \
541 \ ----------------------------------\
542     BIS.B #RC5,&IR_IE               \ enable RC5_Int
543     BIC.B #RC5,&IR_IFG              \ reset RC5_Int flag
544 \ ----------------------------------\
545 \ init WatchDog WDT_TIM_            \ eUSCI_A0 (FORTH terminal) has higher priority than WDT_TIM_
546 \ ----------------------------------\
547 \              %01 0001 0100        \ TAxCTL
548 \               --                  \ TASSEL    CLK = ACLK = LFXT = 32768 Hz
549 \                  --               \ ID        divided by 1
550 \                    --             \ MC        MODE = up to TAxCCRn
551 \                        -          \ TACLR     clear timer count
552 \                         -         \ TAIE
553 \                          -        \ TAIFG
554 \ ----------------------------------\
555     MOV #%01_0001_0100,&WDT_TIM_CTL \ start WDT_TIM_, ACLK, up mode, disable int,
556 \ ----------------------------------\
557 \                        000        \ TAxEX0
558 \                        ---        \ TAIDEX    pre divisor
559 \ ----------------------------------\
560 \          %0000 0000 0000 0101     \ TAxCCR0
561     MOV ##3276,&WDT_TIM_CCR0        \ else init WDT_TIM_ for LFXT: 32768/20=1638 ==> 100ms
562 \ ----------------------------------\
563 \          %0000 0000 0001 0000     \ TAxCCTL0
564 \                   -               \ CAP capture/compare mode = compare
565 \                        -          \ CCIEn
566 \                             -     \ CCIFGn
567     MOV #%10000,&WDT_TIM_CCTL0      \ enable compare interrupt, clear CCIFG0
568 \ ----------------------------------\
569 \ activate I/O                      \
570 \ ----------------------------------\
571     CALL &{RC5TOLCD}                \ run previous INIT_HARD_APP
572 \ ----------------------------------\
573 \ RESET events handling             \ search "SYSRSTIV" in your MSP430FRxxxx datasheet to get listing
574 \ ----------------------------------\
575     CMP #$0E,TOS                    \ SYSRSTIV = SVSHIFG SVSH event ?
576     0<> IF                          \ if not
577         CMP #$0A,TOS                \   SYSRSTIV >= violation memory protected areas | USERSYS <0 = DEEP_RESET request ?
578         U>= ?GOTO BW1               \   if yes execute STOP_R2L then RET to BODY of WARM
579     THEN                            \
580 \    CMP #2,TOS                      \   Power_ON event
581 \    0= ?GOTO BW1                    \   uncomment if you want to loose application in this case...
582     CMP #4,TOS                      \   SYSRSTIV|USERSYS RST ?
583     0= ?GOTO BW1                    \   if yes run STOP.
584 \    CMP #$0E,TOS                    \   SYSRSTIV = SVSHIFG SVSH event ?
585 \    0= ?GOTO BW1                    \   SVSHIFG SVSH event performs STOP
586 \ ----------------------------------\
587     LO2HI                           \
588 \ ----------------------------------\
589 \   Init LCD 2x20                   \
590 \ ----------------------------------\
591     #1000 20_US                     \ 1- wait 20 ms
592     %011 TOP_LCD                    \ 2- send DB5=DB4=1
593     #205 20_US                      \ 3- wait 4,1 ms
594     %011 TOP_LCD                    \ 4- send again DB5=DB4=1
595     #5 20_US                        \ 5- wait 0,1 ms
596     %011 TOP_LCD                    \ 6- send again again DB5=DB4=1
597     #2 20_US                        \    wait 40 us = LCD cycle
598     %010 TOP_LCD                    \ 7- send DB5=1 DB4=0
599     #2 20_US                        \    wait 40 us = LCD cycle
600     %00101000 LCD_WRF               \ 8- %001DNFxx "FonctionSet" D=8/4 DataBus width, Number of lines=2/1, Font bold/normal
601     %1000 LCD_WRF                   \ 9- %1DCB   "DisplayControl" : Display off, Cursor off, Blink off.
602     LCD_CLEAR                       \ 10- "LCD_Clear"
603     %0110 LCD_WRF                   \ 11- %01xx   "LCD_EntrySet" : address and cursor shift after writing in RAM
604     %1100 LCD_WRF                   \ 12- %1DCB "DisplayControl" : Display on, Cursor off, Blink off.
605     LCD_CLEAR                       \ 10- "LCD_Clear"
606     HI2LO                           \
607     MOV @RSP+,PC                    \ RET to WARM|START
608     ENDCODE
609 \ ----------------------------------\
610
611 \ ----------------------------------\
612     CODE START                      \ this routine replaces INT_HARD_APP default values by these of this application.
613 \ ----------------------------------\
614     CMP #WDT_INT,&WDT_TIM_0_VEC     \ value set by START
615     0= IF                           \
616         MOV @IP+,PC                 \ does nothing if already initialised
617     THEN
618     MOV #STOP_R2L,&{RC5TOLCD}-2     \ execution of {RC5TOLCD} will perform STOP_R2L.
619        MOV &HARD_APP,&{RC5TOLCD}    \ save previous HARD_APP subroutine
620        MOV #INIT_R2L,&HARD_APP      \ replace it by RC5toLCD INIT_R2L
621     MOV &WDT_TIM_0_VEC,&{RC5TOLCD}+2 \ save Vector previous value
622     MOV #WDT_INT,&WDT_TIM_0_VEC     \ for only CCIFG0 int, this interrupt clears automatically CCIFG0
623     MOV &IR_VEC,&{RC5TOLCD}+4       \ save Vector previous value
624     MOV #RC5_INT,&IR_VEC            \ init interrupt vector
625 \ ----------------------------------\
626 \   init 20 us count loop           \ see 20_US
627 \ ----------------------------------\ -- TOS
628     SUB #6,PSP                      \ -- x x x TOS
629     MOV TOS,4(PSP)                  \ -- TOS x x TOS
630     MOV &FREQ_KHZ,2(PSP)            \ -- TOS DVDlo x TOS
631     MOV #0,0(PSP)                   \ -- TOS DVDlo DVDhi TOS
632     MOV #200,TOS                    \ -- TOS DVDlo DVDhi DIVlo
633     CALL #MUSMOD                    \ -- TOS REMlo QUOTlo QUOThi
634     MOV @PSP,&{RC5TOLCD}+6          \                       set count+2 for 20_US
635     ADD #4,PSP                      \ -- TOS QUOThi
636     MOV @PSP+,TOS                   \ -- TOS
637 \ ----------------------------------\
638     CALL #INIT_R2L                  \ run new INIT_HARD_APP
639     LO2HI
640 \    ['] LCD_HOME IS CR              \ ' CR redirected to LCD_HOME
641 \    ['] LCD_WRC  IS EMIT            \ ' EMIT redirected to LCD_WrC
642 \    CR ." I love you"               \ display message on LCD
643 \    ['] CR >BODY IS CR              \ CR executes its default value
644 \    ['] EMIT >BODY IS EMIT          \ EMIT executes its defaulte value
645     ." RC5toLCD is running,"        \
646     ."  Type STOP to quit."         \ display message on FastForth Terminal
647     HI2LO
648     MOV #ABORT,PC                   \ goto FORTH interpreter without WARM message.
649     ENDCODE                         \
650 \ ----------------------------------\
651
652 RST_SET 
653
654 ECHO
655
656 \ START