pointer questions as related to code snippet.

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hello all,
I have a Z8 encore platform and am trying to learn some c.  Below r some
snippets of code from Zilogs website using the SPI bus.  I am posting to see
if anyone can shed some light on the cryptic flow going on below with the
pointer.  In particular two questions:

Question
#1    The Read_Temperature function gets called in the isr_timer0 and passes
what appears to be the left value of the pointers as arguments to a function
which is defined to accept the right value of pointers as arguments?? (Very
confusing)

#2    The Read_Temperature function definition then appears to have
arguments that are the same as the return arguments of the DS1722_Read
function?? (once again very confusing)


This pointer stuff can drive a person nuts!

*********************************************CODE
SNIPPET*********************************************************
//declarations
unsigned char DS1722_Read (unsigned char r_addr);
unsigned char Read_Temperature (unsigned char *H_data, unsigned char
*L_data);
unsigned char SPI_Read (unsigned char addr);
void isr_timer0(void);

#pragma interrupt
void isr_timer0(void)
{
  unsigned char *high_data;
    unsigned char *low_data;
 Read_Temperature (high_data, low_data);
}

unsigned char Read_Temperature(unsigned char *H_data, unsigned char *L_data)
{

  *H_data  = DS1722_Read(HIGH_Read_address); //Read MSB Temperature
  *L_data = DS1722_Read(LOW_Read_address);  //Read LSB Temperature
}

unsigned char DS1722_Read(unsigned char r_addr)
{
   unsigned char r_data=0;
 r_data = SPI_Read (r_addr);
  return (r_data);
}

unsigned char SPI_Read (unsigned char addr)
{
 unsigned char data;

 SPIMODE |= SSV_HIGH;     // SS pin high

 SPIDATA = addr;      // Write Address to SPI Data Reg.
 SPI_Transmit_Data_Empty ();   // Wait for Transmit Buffer Empty
 SPIDATA = 0x00;      // Start SPI Read with dummy write
 SPI_Transmit_Data_Empty ();   // Wait for Transmit Buffer Empty
 data = SPIDATA;      // Read SPI Data Reg.
 SPIMODE &= ~SSV_HIGH;     // SS pin low

 return data;
}
*********************************************CODE
SNIPPET*********************************************************



thanks for all the responses!
Steve Wenner



Re: pointer questions as related to code snippet.
On Mon, 22 Sep 2003 01:24:02 GMT, "Steve Wenner"

Quoted text here. Click to load it

All functions in C receive arguments by value.  It is the only method
that C has of passing anything.  Passing by reference does not exist
in the language.
Quoted text here. Click to load it

This is a function that accepts an unsigned character VALUE as an
argument, and returns another unsigned character VALUE.

Quoted text here. Click to load it

This is a function that accepts two pointers to unsigned char by
VALUE.  Note that the only way a C function can modify a variable in a
calling function is for that function to pass a pointer to that
variable.

Quoted text here. Click to load it

This is another function that accepts an unsigned char by value and
returns an unsigned char value.

Quoted text here. Click to load it

Is the function above really published in an application note on
Zilog's web site?  If so, you should notify them of a serious error in
their sample code.

The function defines two variables of type "pointer to unsigned char"
named high_data and low_data.  But it does not initialize them at all.
Defining a pointer in C does not create anything for the pointer to
point at, nor does it point the pointer at anything in particular.

So what you have here are two pointers (addresses of memory locations)
that point to who knows where.  Technically under the C standard, even
passing these values to the Read_Temperature function is undefined
behavior.

In reality, passing these pointers to a function that will use them to
write data could cause that function to try to write to any location
in memory.  It could correspond to a memory-mapped hardware register,
or overwrite your program's stack in RAM.

Quoted text here. Click to load it

You are absolutely right to be confused about this usage of pointers,
since it represents a serious bug.

--
Jack Klein
Home: http://JK-Technology.Com
We've slightly trimmed the long signature. Click to see the full one.
Re: pointer questions as related to code snippet.
Quoted text here. Click to load it
[snip]
Quoted text here. Click to load it



There was a thread a few months ago about the quality of the code supplied
by Zilog in their Z8 Encore kit, which I am assuming this is a part of.

The consensus was that the code must have been written by a co-op student.

Mostafa

------------------------------------------------------
Mostafa Kassem
http://www.hasata.ca
Reply to: M DOT Kassem AT ieee DOT org
-----------------------------------------------------


Re: pointer questions as related to code snippet.
On Tue, 23 Sep 2003 00:59:40 GMT, Mostafa Kassem

Quoted text here. Click to load it

That's sad.  They are cheating the co-op student as well as the
developers who try to make sense out of their example code.

--
Jack Klein
Home: http://JK-Technology.Com
We've slightly trimmed the long signature. Click to see the full one.
Re: pointer questions as related to code snippet.
The testcode directory had about the worst code I've seen in my life. Some of
the other directories looked like they had better code. I don't see the
testcode in the directory tree of the last version of the development software
so maybe they replaced it with something slightly better.

Site Timeline