Think back. . .

This is a discussion on Think back. . . within the C Programming forums, part of the General Programming Boards category; I (last year, last job) rewrote the code for an embedded system for the US Government (actually the Army, but ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Think back. . .

    I (last year, last job) rewrote the code for an embedded system for the US Government (actually the Army, but close enough). The system is (we're working on this -> LINUX) currently "running" Dr. DOS. The entire system is located on an old 8-bit STD Bus with a few cards that react like frozen oil. The original programmer put in wait states in the form
    Code:
    for (i = 0; i < 25000; i++)
        continue;
    which vexs me greatly. I'd like to (make it processor independant) find a way to do the nanosleep available in GLibC for Linux. Delay isn't good enough as it only allows me to wait on the hunderths of a second range.

    Any Ideas anyone?
    Last edited by Kennedy; 08-17-2006 at 03:26 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Such a thing is always hardware-specific. You need a precise timer for that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by CornedBee
    Such a thing is always hardware-specific. You need a precise timer for that.
    Yup. . . I know. I just thought that *maybe* someone knew a way. . . I know the way, the truth and the light. . . no man cometh unto the nanosleep except by Linux.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about:
    Code:
    usleep(num_usec);

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by swoopy
    How about:
    Code:
    usleep(num_usec);
    Is that in DOS????? I remember it not being there. I'll check, though.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    I'm confused as to what OS/Platform the code is presently running on and the OS/Platform you're trying to get it to run on.

    Also, which processor(s) are you using, and which compiler(s).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You'd better make sure the hardware actually supports timing to the desired accuracy though, if it's an embedded system things could get minimalist. Also, some processors might only support nanosecond precision superfluously; if your processor clock speed is still in Mhz it's not much use, and some processor clocks have low granularity anyway regardless of actual clock speed.

    Do get it working, I wouldn't want a couple nukes to be launched ahead of schedule
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Maybe you want to have a look to select(2) man page. You can use select(2) to have a delay in your programs.

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    I'm confused as to what OS/Platform the code is presently running on and the OS/Platform you're trying to get it to run on.
    Dr. DOS.

    Quote Originally Posted by Salem
    Also, which processor(s) are you using, and which compiler(s).
    Versa Logic 586 board (not sure which processor is on the little guy, and there are two different versions of this board).
    WinSystems 586 board (again, don't know the processor, but it is NOT a well know [to me] processor).
    Last edited by Kennedy; 08-21-2006 at 11:12 AM.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    http://www.versalogic.com/Products/DS.asp?PN=Bobcat
    http://sbc.winsystems.com/products/sbcs/index2.html
    Step 1 is review the hardware specifications for the boards.

    Step 2 is check with the vendors to find out if they supply any "board support package" for the OS you have chosen.

    If that works, you may find yourself in possession of an API call which permits fine-grained timing.

    If that doesn't work out, then I guess you're going to have to implement your own "programmed delay", say something like
    Code:
    const int limit = 1000000;
    void wait1microsecond ( void ) {
      volatile int i;
      for ( i = 0 ; i < limit ; i++ )
      {
      }
    }
    Exactly how you calibrate that from board to board is another matter.
    Also, do you want to allow for elapsed time in interrupt service routines (for example)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    Yup, been there.
    Quote Originally Posted by Salem
    Step 2 is check with the vendors to find out if they supply any "board support package" for the OS you have chosen.
    Done that.
    Quote Originally Posted by Salem
    If that works, you may find yourself in possession of an API call which permits fine-grained timing.
    Which is why I was hoping someone knew a way in DOS to do this. . .
    Quote Originally Posted by Salem
    If that doesn't work out, then I guess you're going to have to implement your own "programmed delay", say something like
    Code:
    const int limit = 1000000;
    void wait1microsecond ( void ) {
      volatile int i;
      for ( i = 0 ; i < limit ; i++ )
      {
      }
    }
    Exactly how you calibrate that from board to board is another matter.
    Also, do you want to allow for elapsed time in interrupt service routines (for example)?
    Basically, that is what I'll end up doing. I'll supply the 'limit' in a config file for the machine. This change, however, is considered a "major change" and will "have to be tested" for it to be accepted. Ever work for a G'ment project? These two small sentences are very very . . . well, you know.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    MS DOS did not have any timer specific functions in the OS. I'm quite sure that Dr. DOS won't either if it also runs in real mode.

    Several options depending on CPU type, chipset type, etc, etc.

    1. Hook a timer interrupt and write a handler that increments a variable and returns the value in a register. Be careful with this b/c on Intel if you hook the incorrect interrupt, you will alter the BIOS time.
    2. Program the chipset timer - this could entail any multiple sets of solutions and will differ for all platforms, CPUs, chipsets, mobos, etc.

    As this is for an embedded system, I doubt highly you will find anything with timer support.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    If there is any timer at all on the board (I see some have RTC hardware), then it's perfectly possible to auto-calibrate the delay loop in the startup code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    If there is any timer at all on the board (I see some have RTC hardware), then it's perfectly possible to auto-calibrate the delay loop in the startup code.
    Key word ==>some<==. It cannot be assumed that there is a RTC. Also, some of these actaully have a RTC interface - the docs state that the RTC can be connected externaly. That is not an option for me.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Erm, so how does regular DR-DOS figure out how long a vanilla delay() call should take?
    Use whatever external "wall-clock" time source is available.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  2. UInt to CString and back
    By xlnk in forum Windows Programming
    Replies: 6
    Last Post: 08-27-2003, 03:08 PM
  3. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM
  4. Returning a Tree node back to void main()
    By gazza in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 02:48 AM
  5. I am back!
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-02-2002, 05:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21