1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

pointer questions as related to code snippet.

Discussion in 'Embedded' started by Steve Wenner, Sep 22, 2003.

  1. Steve Wenner

    Steve Wenner Guest

    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
     
    Steve Wenner, Sep 22, 2003
    #1
    1. Advertisements

  2. Steve Wenner

    Jack Klein Guest

    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.
    This is a function that accepts an unsigned character VALUE as an
    argument, and returns another unsigned character VALUE.
    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.
    This is another function that accepts an unsigned char by value and
    returns an unsigned char value.
    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.
    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
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
     
    Jack Klein, Sep 22, 2003
    #2
    1. Advertisements



  3. 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, Sep 23, 2003
    #3
  4. Steve Wenner

    Jack Klein Guest

    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
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
     
    Jack Klein, Sep 23, 2003
    #4
  5. Steve Wenner

    Gary Kato Guest

    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.
     
    Gary Kato, Sep 23, 2003
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.