Downloading a Block of Code from PunyForth
In a previous post, I was talking about how you can edit blocks of flash code directly on the ESP2866 punyforth system, if you want to do so. That raises the question of how you could get that code off the chip and saved onto your PC (say for version control) without having to do some painful copying and pasting. The answer is that you can use esptool.py to download the block of code that you want.
Let's say I have been editing a block:
(stack) CMH list
0 1073741824 constant: m
1 variable: y0 1 y0 !
2 variable: y1 1 y1 !
3 : iter y0 @ y1 @ + m % y1 ! ;
4 : sarnd y1 @ iter ;
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 /end
First of all, DON'T FORGET to run the flush command to save recent edits to flash.
Next, you need to get the blocknumber, which I have saved as a constant:
Multiply that by 4096 to get that actual starting byte number, in this case 606208.
Then use esptool.py to read 4096 bytes from that starting address into a file:
christopher@theoden ~/Scratch$ esptool.py --port /dev/ttyUSB0 read_flash 606208 4096 cmh.bin
If your module uses more than one block, just multiply 4096 in the above line by the number of blocks.
christopher@theoden ~/Scratch$ cat cmh.bin
1073741824 constant: m
variable: y0 1 y0 !
variable: y1 1 y1 !
: iter y0 @ y1 @ + m % y1 ! ;
: sarnd y1 @ iter ;
/end
You can see my program is not using much of that block yet.
The data in the outputted binary file contains the padding spaces as well, so I should be able to do this process in reverse also, using write_flash. I have not gotten around to trying that yet.
As I have mentioned previously, editing programs in this way does waste some flash space, since we inevitably have some unnecessary padding in a block. But, on these ESP systems at least, we have a LOT of flash to work with, at least compared to the amount of RAM available. Anyway, there are easy things that you could do later to turn your space-padded code into a compressed format, if you needed to do so.