So we have compiled the binary what do we do with it?
The first thing I will recommend is to check if the code is what we are looking for. There is a good tool to help us with that task: m6811-elf-objdump.exe. Try this command:
m6811-elf-objdump -D blink.elf
Here is the output I have (yours may vary slightly since the compiler is evolving every day):
blink.elf: file format elf32-m68hc11
Disassembly of section .text:
00000000 <_start>:
0: 8e 00 ff lds #ff <_etext+0xbd>
3: 20 21 bra 26
<main>
5: 39 rts
00000006 <delay_ms>:
6: 34 des
7: 8f xgdx
8: 8c 00 00 cpx #0 <_start>
b: 27 17 beq 24 <delay_ms+0x1e>
d: c6 63 ldab #99
f: 18 30 tsy
11: 18 e7 00 stab 0,y
14: fc 10 0e ldd 100e <_io_ports+0xe>
17: 18 30 tsy
19: 18 6a 00 dec 0,y
1c: 26 f6 bne 14 <delay_ms+0xe>
1e: 09 dex
1f: 8c 00 00 cpx #0 <_start>
22: 26 e9 bne d <delay_ms+0x7>
24: 31 ins
25: 39 rts
00000026 <main>:
26: f6 10 00 ldab 1000 <_io_ports>
29: ca 40 orab #64
2b: f7 10 00 stab 1000 <_io_ports>
2e: cc 00 01 ldd #1 <_start+0x1>
31: 8d d3 bsr 6 <delay_ms>
33: f6 10 00 ldab 1000 <_io_ports>
36: c4 bf andb #191
38: f7 10 00 stab 1000 <_io_ports>
3b: cc 00 01 ldd #1 <_start+0x1>
3e: 8d c6 bsr 6 <delay_ms>
40: 20 e4 bra 26 <main>
Disassembly of section .data:
Most important of all se see is that our _start function is placed at address 0, which was one of the goals. The rest of the code looks fine and I'm not that much of an assembly person to decipher it properly.
Now that we are confident the code is what we expect it to be, we have to generate a suitable format out of the .elf file. The ELF is a great binary format, but it is way too complex for out little program. What we need is a pure binary data of the code of our program. We can get this using this command:
m6811-elf-objcopy -O binary blink.elf blink.bin
OBJCOPY is a great tool to transfer between various binary formats. What we did in this case is that we converted the ELF binary to "pure" binary. My blink.bin file is 66 bytes long and as you can see it contains exactly the same code bytes as the ELF file:
$ od -t x1 blink.bin
0000000 8e 00 ff 20 21 39 34 8f 8c 00 00 27 17 c6 63 18
0000020 30 18 e7 00 fc 10 0e 18 30 18 6a 00 26 f6 09 8c
0000040 00 00 26 e9 31 39 f6 10 00 ca 40 f7 10 00 cc 00
0000060 01 8d d3 f6 10 00 c4 bf f7 10 00 cc 00 01 8d c6
0000100 20 e4
0000102
Now we have to do only one last step before we can try this program: my "boot loader" program Prog68HC11, requires that the first byte of the binary file must have the value of 0xFF. This byte is required to synchronize the speed between the PC and the MPU so we must somehow insert it in the front of the file. The simplest way is to create a 1 byte file FF.bin which contains the value 0xFF. Then you can use the following command to concatenate both files:
copy /b ff.bin+blink.bin blink.b
Or if you are in cygwin shell:
cat ff.bin blink.bin> blink.b
Let's do a final check on the blink.b file:
$ od -t x1 blink.b
0000000 ff 8e 00 ff 20 21 39 34 8f 8c 00 00 27 17 c6 63
0000020 18 30 18 e7 00 fc 10 0e 18 30 18 6a 00 26 f6 09
0000040 8c 00 00 26 e9 31 39 f6 10 00 ca 40 f7 10 00 cc
0000060 00 01 8d d3 f6 10 00 c4 bf f7 10 00 cc 00 01 8d
0000100 c6 20 e4
As you can see it is exactly the same as blink.bin but with one byte 0xFF at the front.
Finally we are ready to run this program.
There are many boot loaders for 68HC11. Mine is by no means perfect, but It is implemented from scratch and it worked for me and you have the source code, so you can play with it. The Prog68HC11 boot loader should work with any board, which runs the MCU at 2MHz E clock and has RS-232 interface connected like this.
To run the program download the Prog68HC11 boot loader from the downloads section, connect the RS-232 cable to the board and set the MODA and MODB jumpers to "boot mode". For the RS232 connection I used simple 3 wire cable - receive, transmit and ground. Finally use this command, before powering up the board (assuming you are using COM1):
prog68hc11 -w blink.b
You will see the following message:
Power on the MCU when ready....
Apply power to the board. At this point the program will start downloading you code, you will see a series of dots and plusses. When the program finishes, your MCU should be executing our simple blink.c program and you should be able to observe the square wave on PortA.6 (pin 15 on 52-pin PLCC package).
Hooray!!!
You can now compile and run simple programs on the 68HC11 MCU. Later I'll add instructions how to connect an external memory chip, so we can compile and run even more powerful programs.