[<<][staapl-blog][>>][..]
Wed Aug 12 19:19:03 CEST 2009

A Forth console on PIC 18F1320

# This demo requires these files[1][2] from the source distribution.
# The PLaneT package only contains the compiler.

# Start with staapl/app/1220-8.fm[2] as the baseline.  This will
# provide an interactive interpreter into which code can be loaded.

# For the buld process, look at staapl/app/Makefile for more
# information.  In short:

tom@zni:~/staapl/app$ make 1220-8.hex
cat 1220-8.fm | grep '\\ #sh#' | cut -b7- >1220-8.program
mzc -vk 1220-8.fm
mzc v4.2.1.1 [3m], Copyright (c) 2004-2009 PLT Scheme Inc.
"1220-8.fm":
  making "/home/tom/darcs/brood-5/app/1220-8.fm"
 include "/home/tom/staapl/staapl/pic18/p18f1220.f"
 include "/home/tom/staapl/staapl/pic18/p18f1220-const.f"
 include "/home/tom/staapl/staapl/pic18/monitor-serial.f"
 include "/home/tom/staapl/staapl/pic18/monitor-serial-core.f"
 include "/home/tom/staapl/staapl/pic18/monitor-serial-warm.f"
 [output to "./compiled/1220-8_fm.zo"]
mzscheme -p zwizwa/staapl/staaplc -- -c /dev/ttyUSB0 1220-8.fm

# This will produce a .hex file that can be uploaded to the target and
# a .dict file with a symbol table necessary to interact with the
# stripped machine code on the PIC.

# If you have a PicKit 2 w. pk2cmd you can use the following,
# otherwise simply upload the .hex file using your programmer.

tom@zni:~/staapl/app$ make 1220-8.flash
cat 1220-8.fm | grep '\\ #sh#' | cut -b7- >1220-8.program
sh 1220-8.program 1220-8.hex
PICkit 2 Program Report
12-8-2009, 19:29:22
Device Type: PIC18F1220

Program Succeeded.
Device ID = 07C0
Revision  = 0007
Device Name = PIC18F1320   !WARNING! -P device mismatch

Operation Succeeded
rm 1220-8.program

# Now start the console.  The .dict file is packaged as an mzscheme
# module.  Invoke as:

tom@zni:~/staapl/app$ mzscheme 1220-8.dict
Connected (/dev/ttyUSB0 38400)
Press ctrl-D to quit.
OK

# At this point you have interactive access to the pic chip.  The
# 18f1320 has 8kB of flash.  The flash erase unit is 64 byte blocks.
# A map of the available blocks can be printed like this:

8 kb
x x x x x x x x
x x . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
OK

# Currently the interactive monitor uses 640 bytes.  The rest is
# yours.  The first block is reserved for boot code, and we'll use it
# later to add the startup code.

# Now it is possible to define code.  Let's create and upload a word
# `add1'.  The dot before the `OK' indicates that one line (8 bytes)
# of flash has been written.

: add1 1 + ;
.OK

# Run the code:

123 add1 p
124 OK

# The `p' word prints the top of the stack.  (The `.' word does the
# same, and is th standard way.)  Next to `p' there is `px' to print
# hex and `ps' to print unsigned numbers.

123 px
7B OK

# When you exit the interpreter by pressing ctrl-D, the next time you
# start it up, all your interactively loaded code will be erased.

tom@zni:~/staapl/app$ mzscheme 1220-8.dict
Connected (/dev/ttyUSB0 38400)
erasing blocks: 10 memory clear.
Press ctrl-D to quit.
OK

# Permanent changes are best written as modules and made part of the
# kernel .hex file.  This code will be left intact.  Think of console
# interaction as using `scratch space' next to the kernel code on the
# PIC.  Stuff you load on top of this is mainly for debugging and as
# such can be cleared on boot to start with a clean slate.

# To inspect the machine code use `see'.  This takes byte addresses or
# symbolic words.

see add1
<anonymous>:
	0280 0F01 [addlw 1]
	0282 0012 [return 0]
	0284 FFFF [_nop 4095]
	0286 FFFF [_nop 4095]
	0288 FFFF [_nop 4095]
	028A FFFF [_nop 4095]
	028C FFFF [_nop 4095]
	028E FFFF [_nop 4095]
	0290 FFFF [_nop 4095]
	0292 FFFF [_nop 4095]
	0294 FFFF [_nop 4095]
	0296 FFFF [_nop 4095]
	0298 FFFF [_nop 4095]
	029A FFFF [_nop 4095]
	029C FFFF [_nop 4095]
	029E FFFF [_nop 4095]
OK
see 0
<anonymous>:
	0000 D01F [bra block1]
	0002 FFFF [_nop 4095]
	0004 FFFF [_nop 4095]
	0006 FFFF [_nop 4095]
	0008 FFFF [_nop 4095]
	000A FFFF [_nop 4095]
	000C FFFF [_nop 4095]
	000E FFFF [_nop 4095]
	0010 FFFF [_nop 4095]
	0012 FFFF [_nop 4095]
	0014 FFFF [_nop 4095]
	0016 FFFF [_nop 4095]
	0018 FFFF [_nop 4095]
	001A FFFF [_nop 4095]
	001C FFFF [_nop 4095]
	001E FFFF [_nop 4095]
OK
see block1
<anonymous>:
	0040 D0F1 [bra warm]
	0042 FFFF [_nop 4095]
	0044 BA9E [btfsp 1 jsr/ack 5 0]
	0046 D001 [bra .L2]
	0048 D7FD [bra .L0]
	004A A2AB [btfsp 0 171 1 0]
	004C D003 [bra .L5]
	004E 98AB [bpf 1 171 4 0]
	0050 88AB [bpf 0 171 4 0]
	0052 D7F8 [bra .L0]
	0054 6EEC [movwf 236 0]
	0056 50AE [movf .L30 0 0]
	0058 A4AB [btfsp 0 171 2 0]
	005A D002 [bra .L9]
	005C 50ED [movf lda 0 0]
	005E D7F2 [bra .L0]
OK



[1] http://zwizwa.be/darcs/staapl/app/Makefile
[2] http://zwizwa.be/darcs/staapl/app/1220-8.fm




[Reply][About]
[<<][staapl-blog][>>][..]