#47 unsigned long variable scope...

closed
None
5
2006-06-05
2006-05-07
wastono
No

unsigned long variable in global scope is not work when
access from subroutine function. It still work when
used in main function.

I attach source code for this "Support Requests".

Thanks
Wastono

Discussion

  • wastono

    wastono - 2006-05-07

    Source code in C with anomaly of unsigned long variable scope

     
  • wastono

    wastono - 2006-05-07

    assembly file generated from wiegand test.c

     
  • Maarten Brock

    Maarten Brock - 2006-05-07

    Logged In: YES
    user_id=888171

    Wastono,

    What you did not tell on the forum was that unsigned long
    variable wiegand is also changed in the interrupt service
    routine (ISR). When you read it outside the ISR it needs
    atomic access otherwise it might be changed during the
    read. Look up the keyword critical in the manual and use a
    getter like this:

    unsigned long get_wiegand(void) critical
    {
    return wiegand;
    }

    I've set the report to pending. If you consider it solved
    you can close it.

    Happy programming,
    Maarten

     
  • Maarten Brock

    Maarten Brock - 2006-05-07
    • assigned_to: nobody --> maartenbrock
    • status: open --> pending
     
  • wastono

    wastono - 2006-05-08

    Logged In: YES
    user_id=1518094

    Hi, maartenbrock
    Thanks to you, but please see some code that I tried.

    1. This code is work.

    void showWiegand() critical
    {
    unsigned long comp1 = wiegand;
    // ...
    }

    2. This code is not work.

    void showWiegand() critical
    {
    unsigned long comp1;
    // calling functions for lcd ...
    comp1 = wiegand;
    // ...
    }

    3. This code is not work to.

    void showWiegand()
    {
    unsigned long comp1;
    // calling functions for lcd ...
    critical{ comp1 = wiegand; }
    // ...
    }

    What do you think?
    I am waiting your response, and please tell me
    how to close this session. :)

     
  • wastono

    wastono - 2006-05-08
    • status: pending --> open
     
  • Maarten Brock

    Maarten Brock - 2006-05-14

    Logged In: YES
    user_id=888171

    Hi Wastono,

    What does it mean that it does not work? You have to be
    more specific than that.

    Have you made all access to wiegand atomic by using the
    keyword critical or only one? Have you tried using SDCC
    2.5.6?

    Why did you make variable dd global?

    And this piece is really unreadable code:
    cursorlcd( 0x38 + 8 ** udg );
    How about:
    cursorlcd( 0x38 + 8 * *udg );

    Maarten

     
  • wastono

    wastono - 2006-05-15

    Logged In: YES
    user_id=1518094

    the main loop (while(1)) runs 3 times only. after that,
    program is hang.

    i don't know where i can get SDCC 2.5.6.

    sorry for unreadable code. ;)

    A lot of thanks.
    wastono

     
  • Maarten Brock

    Maarten Brock - 2006-05-21
    • status: open --> pending
     
  • Maarten Brock

    Maarten Brock - 2006-05-21

    Logged In: YES
    user_id=888171

    I looked into it some more. Like I said you must make ALL
    access to wiegand atomic. That includes setWiegand26 and
    clrWiegand26. Enclose them in a critical block or better
    throw them out.
    Furthermore, if you want to avoid buffer overflow I
    recommend to use double-buffering for wiegand.

    wiegand.c

    static unsigned long wiegand = 0;
    bit wiegandReady = 0;

    void wiegand_bit(void) interrupt 0
    {
    static unsigned long my_wiegand = 0;
    static unsigned char wiegandcnt = 0;
    bit wiegand26;

    wiegand26 = D0;
    if( wiegand26 != D1 )
    {
    my_wiegand = my_wiegand * 2;
    if( wiegand26 )
    {
    /* wiegand++; */
    /* For better optimized code use this instead */
    my_wiegand |= 1;
    }
    wiegandcnt++;
    if (wiegandcnt == 26)
    {
    wiegand = my_wiegand;
    my_wiegand = 0;
    wiegandReady = 1;
    }
    }
    }

    unsigned long getWiegand(void) critical
    {
    wiegandReady = 0;
    return wiegand;
    }

    wiegand.h

    extern bit wiegandReady;
    void wiegand_bit(void) interrupt 0;
    unsigned long getWiegand(void);

    test.c

    if (wiegandReady)
    comp1 = getWiegand();

    SDCC 2.5.6 can be obtained from the snapshots page. Go to
    the homepage of SDCC and on the left click snapshots.

    I assume that if you log in to SF you can set the status
    of items you submitted to closed. Anyway I'll set it
    pending once again so it closes automatically if you don't
    respond.

     
  • SourceForge Robot

    Logged In: YES
    user_id=1312539

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     
  • SourceForge Robot

    • status: pending --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks