forked from M2IHP13-admin/JonesForth-arm
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathblinker.f
81 lines (67 loc) · 2.73 KB
/
blinker.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
\
\ Blinking LED example in FORTH
\
: '-' 45 ;
: '.' 46 ;
\
\ RPi-specific definitions
\
16# 2000B420 CONSTANT ARM_TIMER_CNT
\ us@ ( -- t ) fetch microsecond timer value
: us@ ARM_TIMER_CNT @ ;
: usecs ; \ usecs ( n -- dt ) convert microseconds to timer offset
: msecs 1000 * ; \ msecs ( n -- dt ) convert milliseconds to timer offset
: secs 1000000 * ; \ secs ( n -- dt ) convert seconds to timer offset
: us \ ( dt -- ) busy-wait until dt microseconds elapse
us@ + \ timeout = current + dt
BEGIN
DUP \ copy timeout
us@ - \ past|future = timeout - current
0<= \ loop until not future
UNTIL
DROP \ drop timeout
;
16# 20200004 CONSTANT GPFSEL1 \ GPIO function select (pins 10..19)
16# 001C0000 CONSTANT GPIO16_FSEL \ GPIO pin 16 function select mask
16# 00040000 CONSTANT GPIO16_OUT \ GPIO pin 16 function is output
GPFSEL1 @ \ read GPIO function selection
GPIO16_FSEL INVERT AND \ clear function for pin 16
GPIO16_OUT OR \ set function to output
GPFSEL1 ! \ write GPIO function selection
16# 2020001C CONSTANT GPSET0 \ GPIO pin output set (pins 0..31)
16# 20200028 CONSTANT GPCLR0 \ GPIO pin output clear (pins 0..31)
16# 00010000 CONSTANT GPIO16_PIN \ GPIO pin 16 set/clear
: +gpio16 GPIO16_PIN GPSET0 ! ; \ set GPIO pin 16
: -gpio16 GPIO16_PIN GPCLR0 ! ; \ clear GPIO pin 16
: LED_ON -gpio16 ; \ turn on ACT/OK LED (clear 16)
: LED_OFF +gpio16 ; \ turn off ACT/OK LED (set 16)
\
\ Morse code (http://en.wikipedia.org/wiki/Morse_code)
\
: units 50 msecs * ; \ units ( n -- dt ) convert dot-units to timer offset
: eoc 1 units us ; \ end of character
: dit \ dot
'.' EMIT \ print dot
LED_ON
1 units us \ wait 1 unit time
LED_OFF
eoc \ end
;
: dah \ dash
'-' EMIT \ print dash
LED_ON
3 units us \ wait 3 unit times
LED_OFF
eoc \ end
;
: eol SPACE 2 units us ; \ end of letter (assumes preceeding eoc)
: ___ eol eol ; \ word-break space (assumes preceeding eol)
: _C_ dah dit dah dit eol ;
: _D_ dah dit dit eol ;
: _E_ dit eol ;
: _M_ dah dah eol ;
: _O_ dah dah dah eol ;
: _R_ dit dah dit eol ;
: _S_ dit dit dit eol ;
: _SOS_ dit dit dit dah dah dah dit dit dit eol ;
_M_ _O_ _R_ _S_ _E_ ___ _C_ _O_ _D_ _E_