MANAGING YOUR MOBILE MONKEY
//*******************************************************
//* INITIALIZE DISK DRIVE
//*******************************************************
char init_disk(void)
{
sendchar(‘@’);
sendchar(‘i’);
do{
++scratch8;
}while(!(CharInQueue()));
return (bytein = recvchar());
}
The initialize Disk Drive Memory Card command
consists of two bytes: “@i.” Now you see why the “i” in
initialize is not capitalized in the command name. Once the
disk drive memory card is initialized, an ACK is returned
by the uDrive. All of the API functions report back to the
calling function with a return code. The return code will be
either an ACK or a NAK. This allows you to optionally error
check each API call your application makes. For instance,
here’s how we invoked the init_disk function:
rc = init_disk();
The return code (rc) can then be used to make a GO or
NOGO decision like this:
If(rc == uACK)
{
Your GO code here.
}
else
{
Your NOGO code here..
}
Coding the Set Memory Address
API Function
We’ll need 32 bits of address information to read and
write a byte at a time over the 2 GB of disk drive space we
have. Those 32 bits of address information are exactly what
the uDrive’s Set Memory Address command needs to help
us do those byte-wise reads and writes. The Set Memory
Address command is called with “@A” followed by four bytes
of address information. This command must be executed
before any byte-wise read or write operation is performed.
Our Set Memory Address API function looks like this:
rc = set_mem_addr(0x00000000); //call the API function
//*******************************************************
//* SET MEMORY ADDRESS
//*******************************************************
char set_mem_addr(unsigned long addr)
{
sendchar(‘@’);
sendchar(‘A’);
sendchar((addr & 0xFF000000) >> 24);
sendchar((addr & 0x00FF0000) >> 16);
sendchar((addr & 0x0000FF00) >> 8);
sendchar((addr & 0x000000FF));
do{
++scratch8;
}while(!(CharInQueue()));
return (bytein = recvchar());
}
54 SERVO 05.2008
I added the C statement to call the set_mem_addr
function to show you what the address argument of the
function looked like. In my example, we’re setting the
read/write memory address to 0x00000000. I chopped up
the 32-bit address into eight-bit chunks and sent them
along their way in order using the sendchar function. Once
all of the six bytes of the Set Memory Address command
have been sent, we loop and wait for an ACK or NAK to be
returned to us by the uDrive. Let’s use the Set Memory
Address command and its API function code to build up a
write-a-byte API function.
Writing and Reading a Byte
to the Disk Drive
We only need to send three bytes to write a byte to a
memory location within the disk drive memory card: “@w”
+ data. The format of the write-a-byte API function is
identical to the Set Memory Address API function code:
//*******************************************************
//* WRITE BYTE
//*******************************************************
char write_byte(char data,unsigned long addr)
{
set_mem_addr(addr);
sendchar(‘@’);
sendchar(‘w’);
sendchar(data);
do{
++scratch8;
}while(!(CharInQueue()));
return (bytein = recvchar());
}
You can see the write-byte command sequence in the
write_byte function source code. Note that we stuffed a
set_mem_addr function call in at the beginning of the
write-byte function code. The address information is
included as an argument of the write-byte API function.
I’m positive that you’ve already logically deduced the
command string for a byte read: “@r.” So, without further
ado, here’s the read-byte API function source code:
//*******************************************************
//* READ BYTE
//*******************************************************
char read_byte(unsigned long addr)
{
set_mem_addr(addr);
sendchar(‘@’);
sendchar(‘r’);
do{
++scratch8;
}while(!(CharInQueue()));
return (bytein = recvchar());
}
There’s nothing in the read-a-byte code that you
haven’t seen before, other than the read- byte command
string. Here’s all it takes to read and write a byte to address
0x00000000 using the API code we’ve just built: