-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbsl-5xx-6xx.mu4
128 lines (98 loc) · 4.27 KB
/
bsl-5xx-6xx.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2025 David Frech. (Read the LICENSE for details.)
loading MSP430 bootstrap loader (BSL) support (5xx and 6xx devices)
hex
| comment %connections%
|
| For the FR6989, connections are as follows:
|
| TX == P2.0
| RX == P2.1
| TEST == SBWTCK
|
| Jumper TEST low. Hold down the RESET switch. Jumper TEST high, then low,
| then high again. Release the RESET switch. Tie TEST low.
|
| With luck, this will put the device into BSL mode. Sometimes it takes a few
| tries.
|
| %connections%
( This version of the BSL uses CRC16-CCITT as a checksum.)
variable checksum
: sum! 0ffff checksum ! ;
: sum ( b) checksum @ swap crc16-byte checksum ! ;
: >b ( b) dup sum send ;
: b> ( - b) recv dup sum ;
: >w >hilo >b >b ;
: w> b> b> lohi> ;
( Send an address - 3 bytes!)
: >addr ( a) >hilo >b >w ;
: ?match ( want got) 2dup = if 2drop ^ then
." expected " .h8_ ." got " .h8_ ;
| >buf reads bytes from buf and sends them;
| buf> recvs bytes and writes them into buf.
: >buf ( buf u) swap m ! for m* >b next ;
: buf> ( buf u) swap m ! for b> m& next ;
: ?ack recv
dup 0= if drop ^ then ( Ok)
dup 51 = if drop ." Header incorrect" rdrop ^ then
dup 52 = if drop ." Checksum incorrect" rdrop ^ then
dup 53 = if drop ." Packet size zero" rdrop ^ then
dup 54 = if drop ." Packet too big" rdrop ^ then
dup 55 = if drop ." Unknown error" rdrop ^ then
dup 56 = if drop ." Unknown baud rate" rdrop ^ then
57 = if ." Packet size error" rdrop ^ then ;
: ?core b>
dup 0= if drop ." Success" ^ then
dup 01 = if drop ." Memory write check failed" ^ then
dup 04 = if drop ." BSL locked" ^ then
dup 05 = if drop ." BSL password error" ^ then
07 = if ." Unknown command" ^ then ;
| If we crc the checksum bytes, high order byte *first*, and the received checksum
| is correct, then the result will be 0.
: ?chk recv ( lo) recv sum ( hi) sum ( lo)
checksum @ if ." Bad checksum received" then ;
: hdr> ( - len) recv 80 ?match w> ;
: core> ?ack hdr> 2 ?match ( length should be 2)
sum! b> 3b ?match ?core ?chk ;
: corebuf> ( a) ?ack hdr> 1- ( length includes status)
sum! b> 3b = if ?core ?chk ^ then buf> ?chk ;
: <bsl ( cmd length) 80 send >w sum! >b ;
: bsl> checksum @ >w ;
( cmd len)
: bsl-txbuf ( buf a u) 10 over 4 + <bsl swap >addr >buf bsl> core> ;
: bsl-rxbuf ( buf a u) 18 06 <bsl swap >addr >w bsl> corebuf> ;
: bsl-unlock 11 21 <bsl 10 for 0ffff >w next bsl> core> ;
: bsl-unlock-image 11 21 <bsl ffe0 image+ 20 >buf bsl> core> ;
: bsl-erase ( a) 12 04 <bsl >addr bsl> core> ;
( bsl-mass-erase 15 01 <bsl bsl> ; )
: bsl-loadpc ( pc) 17 04 <bsl >addr bsl> ;
: bsl-version 19 01 <bsl bsl> pad corebuf> ;
: bsl-crc ( a u) 16 06 <bsl swap >addr >w bsl> pad corebuf> ;
| Naming convention note: All the following b.Foobar commands are the BSL
| _implementations_ of the chat command interface.
: b.Hello ( start things going)
#9600 bps ignore-parity even-parity bsl-unlock
pad on bsl-version cr ." BSL version "
pad 4 for c@+ swap .h8_ next drop ;
: b.GetStatus ( - sp pc sr cp) "dead @ram 0 "dead ;
: b.Run ( pc sr cp) 2drop bsl-loadpc ;
: safe? ( a - f) @flash u< ;
: b.ReadWord ( a - w) pad swap \m cell bsl-rxbuf pad leh@ ;
: b.WriteWord ( w a) dup safe? if
push pad leh! pad pop \m cell bsl-txbuf ^ then
2drop ;
: b.ReadChunk ( buf a u) bsl-rxbuf ;
: b.WriteChunk ( buf a u)
over safe? if bsl-txbuf ^ then
2drop drop ;
.equates. .contains FCTL1 .if
: b.ErasePage ( a - fail?) bsl-erase 0 ;
: b.FlashChunk ( buf a u - fail?) bsl-txbuf 0 ;
.else ( FRAM?)
: b.ErasePage ( a - fail?) bsl-erase 0 ; ( XXX ???)
: b.FlashChunk ( buf a u - fail?) bsl-txbuf 0 ;
.then
: bsl chat-via b.Hello b.GetStatus b.Run
b.ReadChunk b.WriteChunk b.ErasePage b.FlashChunk ;