-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathchat3-host.mu4
108 lines (78 loc) · 3.34 KB
/
chat3-host.mu4
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2025 David Frech. (Read the LICENSE for details.)
loading MSP430 serial chat protocol v3 (host)
hex
| Trying something simple and traditional.
|
| Responds to the following commands. NOTE: these are hex values!
|
| 00 - 2f Idle - these command bytes are ignored
|
| 30 GetVersion - get the chat firmware commit
| 31 GetStatus - get SP, PC, SR, and CP
| 32 Run - set PC, SR, CP, and execute
| 33 ReadWords - set address, then read N words
| 34 WriteWords - set address, then write N words
| 35 GetCheck - return accumulated checksum to host
|
| The following are for older - non-CPUX? non-FRAM? - MSP430s that have
| byte-sized i/o from 0020 to 00ff and sport regular Flash memory rather than
| FRAM.
|
| 36 ReadWordsB - set address, then read N words bytewise
| 37 FlashWords - set flash command, set address, then write N words to Flash
|
| 38 - ff Idle - these command bytes are ignored
variable checksum
: sum! checksum off ;
: sum ( w) checksum @ xor checksum ! ;
: >b dup sum send ;
: b> recv dup sum ;
: >w dup sum >hilo send send ;
: w> recv recv lohi> dup sum ;
: c.Idle 0 >b ;
: c.GetVersion ( - n) 30 >b w> w> ( lo hi) 10 << + ;
: c.GetStatus ( - sp pc sr cp) 31 >b w> w> w> w> ;
: c.Run ( pc sr cp) 32 >b rot >w swap >w >w ;
: c.ReadWords ( a n) 33 >b swap >w >b ; ( then read bytes)
: c.WriteWords ( a n) 34 >b swap >w >b ; ( then write bytes)
: c.GetCheck ( ) 35 >b w> drop ;
: c.ReadWordsB ( a n) 36 >b swap >w >b ; ( then read bytes)
: c.FlashWords ( cmd a n) 37 >b rot >w swap >w >b ; ( then write bytes)
( Send two no-ops, let them transmit, _then_ throw away any input bytes.)
: resync c.Idle c.Idle drain flush ;
.ifdef paranoid
( Reset host's and target's checksum accumulator.)
: ?reset resync c.GetCheck sum! ;
: ?ok c.GetCheck checksum @ sum!
=if ." checksum failed: " u. ^ then drop ;
.else
: ?reset resync ;
: ?ok ;
.then
: get-status resync c.GetStatus ;
: c.setup-chunk ( buf a u - #words a #words) rot m ! 1+ 2/ ( words) tuck ;
( Hook into interact code.)
: c.Hello
#115200 bps ?reset
cr ." Chat firmware version " c.GetVersion ?ok
radix preserve hex sep preserve -sep <# 4# 4# #> type ;
| This is for the sake of the G2553 and other chips that have bytewide i/o
| ports from 0020 to 00ff, and wordwide i/o from 0100 to 01ff.
: ?read-bytewise ( a #words - a #words)
over 0100 u< if c.ReadWordsB ^ then c.ReadWords ;
: c.ReadChunk ( buf a u)
c.setup-chunk ?read-bytewise for w> >hilo m& m& next ?ok ;
: c.WriteChunk ( buf a u)
c.setup-chunk c.WriteWords for m* m* lohi> >w next ?ok ;
: deprecated ( - -1)
cr ." Don't use this code! Use chat-v2 or chat-v4 instead." -1 ;
: c.ErasePage ( a - fail?) drop deprecated ;
: c.FlashChunk ( buf a u - fail?) drop 2drop deprecated ;
| NOTE: We don't define RunWait separately. Since we will always be reading
| the registers back after Run, that first read command will block until
| the target is ready.
: chat
chat-via c.Hello get-status c.Run
c.ReadChunk c.WriteChunk c.ErasePage c.FlashChunk ;