Here is my test code. I'm able to pass the addresses of 2 unsigned long variables to a function, that then sets
their values and returns. I display the variable values before and after the function call, and it looks okay.
Next step is to add the code to my dancing robots doing BT comm, and that will be the real test.
Code: Select all
// File iomap_write_08.nxc
// Created by: hdrake, TabbyCat Robots 2012Jan12
// The called function., that writes into addresses passed to it.
bool bt_receive_remote_number(unsigned char queue_id,
bool clear_queue,
unsigned long msg_addr,
unsigned long msg_type_addr);
task main()
{
// A design decision is that all variables that are passed to a called function,
// by their address, will be unsigned long. Two reasons for this. 1. To
// reduce the amount of my testing. 2. To reduces chances of making a mistake
// through brainfade, e.g. read_args.Count will always be equal = 4.
// After control is passed back to the calling function, then the value may be
// assigned to a different variable type.
// The following 2 variables are used for passing back values from the called
// function.
unsigned long received_msg;
unsigned long received_type;
// The following 2 addresses are used for the passing.
unsigned long received_msg_addr;
unsigned long received_type_addr;
// The following 3 addresses are used for my testing only, not needed in final
// code.
unsigned long msg_data_addr;
unsigned long data_type_addr;
unsigned long xger_id_addr;
unsigned short msg_data;
unsigned char data_type;
unsigned char xger_id;
bool bt_receive_status;
xger_id = 5;
// For testing assign an arbitrary values to variables, that will be assigned
// values in the called function.
received_msg = 0;
received_type = 0;
// For testing, assign initial values to variables, that be assigned returned
// values.
msg_data = 0x7fed;
data_type = 31;
xger_id_addr = reladdressOf(xger_id);
received_msg_addr = reladdressOf(received_msg);
received_type_addr = reladdressOf(received_type);
msg_data_addr = reladdressOf(msg_data);
data_type_addr = reladdressOf(data_type);
// Display the starting values.
TextOut(0, LCD_LINE1, FormatNum("%x", xger_id_addr));
TextOut(15, LCD_LINE1, FormatNum("%x", xger_id));
TextOut(30, LCD_LINE1, FormatNum("%x", received_msg_addr));
TextOut(45, LCD_LINE1, FormatNum("%x", received_msg));
TextOut(60, LCD_LINE1, FormatNum("%x", received_type_addr));
TextOut(75, LCD_LINE1, FormatNum("%x", received_type));
TextOut( 0, LCD_LINE2, FormatNum("%x", msg_data_addr));
TextOut(20, LCD_LINE2, FormatNum("%x", msg_data));
TextOut(48, LCD_LINE2, FormatNum("%x", data_type_addr));
TextOut(68, LCD_LINE2, FormatNum("%x", data_type));
// Call a function that will assign values to variables, local to this, the
// calling function.
bt_receive_status = bt_receive_remote_number(xger_id,
TRUE,
received_msg_addr,
received_type_addr);
// Assign the returned values to variables of different data types.
msg_data = received_msg;
data_type = received_type;
// As part of testing, see if any addresses change.
received_msg_addr = reladdressOf(received_msg);
msg_data_addr = reladdressOf(msg_data);
data_type_addr = reladdressOf(data_type);
// Display values after the function call, and subsequent assignments.
TextOut( 0, LCD_LINE6, FormatNum("%x", received_msg_addr));
TextOut(15, LCD_LINE6, FormatNum("%x", received_msg));
TextOut( 0, LCD_LINE7, FormatNum("%x", msg_data_addr));
TextOut(20, LCD_LINE7, FormatNum("%x", msg_data));
TextOut(48, LCD_LINE7, FormatNum("%x", data_type_addr));
TextOut(68, LCD_LINE7, FormatNum("%x", data_type));
until(ButtonPressed(BTNCENTER, TRUE));
} // End of - task main()
bool bt_receive_remote_number(unsigned char queue_id,
bool clear_queue,
unsigned long msg_addr,
unsigned long msg_type_addr)
{
bool bt_op_succeeds;
// Again, the variable involved in the transfer are unsigned long. See note
// in main.
unsigned long received_data;
unsigned long received_data_type;
unsigned long received_data_addr;
unsigned long received_data_type_addr;
IOMapReadByIDType read_args;
IOMapWriteByIDType write_args;
// In the real code the folowing 3 values would be from a BT operation.
received_data = 0xba7e;
received_data_type = 2;
bt_op_succeeds = TRUE;
received_data_addr = reladdressOf(received_data);
received_data_type_addr = reladdressOf(received_data_type);
TextOut( 0, LCD_LINE4, FormatNum("%x", received_data_addr));
TextOut(20, LCD_LINE4, FormatNum("%x", received_data));
TextOut(48, LCD_LINE4, FormatNum("%x", received_data_type_addr));
TextOut(68, LCD_LINE4, FormatNum("%x", received_data_type));
// Read a value, to be transferred, into the SysIOMap buffer.
read_args.ModuleID = CommandModuleID;
read_args.Offset = CommandOffsetMemoryPool + received_data_addr;
read_args.Count = 4;
SysIOMapReadByID(read_args);
// Write the value back to the address passed in. Some magic has occurred,
// that I don't understand, such that the byte count to be written doesn't
// need to be specified. (Did the Count from the read get stored somewhere?)
write_args.ModuleID = CommandModuleID;
write_args.Offset = CommandOffsetMemoryPool + msg_addr;
write_args.Buffer = read_args.Buffer;
SysIOMapWriteByID(write_args);
read_args.ModuleID = CommandModuleID;
read_args.Offset = CommandOffsetMemoryPool + received_data_type_addr;
read_args.Count = 4;
SysIOMapReadByID(read_args);
write_args.ModuleID = CommandModuleID;
write_args.Offset = CommandOffsetMemoryPool + msg_type_addr;
write_args.Buffer = read_args.Buffer;
SysIOMapWriteByID(write_args);
return bt_op_succeeds;
} // End of - bt_receive_remote_number()
// End of - File iomap_write_08.nxc