Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
153 user(s) are online (124 user(s) are browsing Forums)

Members: 2
Guests: 151

orgin, trixie, more...

Headlines

 
  Register To Post  

Timer.device problem
Just can't stay away
Just can't stay away


See User information
Hey guys,

what is wong with the following code?? I'm trying to make it wait for 10 seconds then print timeout and exit. Instead it just returns imidiately.

Quote:

#include <proto/exec.h>
#include <devices/timer.h>
#include <stdio.h>

int main (int argc, char **argv)
{
//open timer.device
struct MsgPort *timerPort = IExec->CreatePort (NULL, 0L);
uint32 timerSignalMask = (1 << timerPort->mp_SigBit);
struct TimeRequest *timerRequest = (struct TimeRequest *) IExec->CreateIORequest (timerPort, sizeof (struct TimeRequest));

IExec->OpenDevice (TIMERNAME, UNIT_MICROHZ, (struct IORequest *)timerRequest, 0L);

//set timeval and start IO
timerRequest->Time.Seconds = 10;
timerRequest->Time.Microseconds = 0;
IExec->SendIO ((struct IORequest *)timerRequest);

//await result
IExec->WaitPort (timerPort);
printf("timeout!\n");

//close timer.device
IExec->CloseDevice ((struct IORequest *)timerRequest);
IExec->DeleteIORequest ((struct IORequest *)timerRequest);
IExec->DeletePort (timerPort);

return (0);
}


Help!

EDIT: I have tried with BeginIO and DoIO as well, same result.

Go to top
Re: Timer.device problem
Supreme Council
Supreme Council


See User information
@alfkil

If it were me, I'd add some checks to make sure the IORequest and the port were created properly.

Just assuming that something succeeded is bound to bite you eventually.

Simon

Comments made in any post are personal opinion, and are in no-way representative of any commercial entity unless specifically stated as such.
----
http://codebench.co.uk
Go to top
Re: Timer.device problem
Just can't stay away
Just can't stay away


See User information
@alfkil

You forgot to add the line:
timerRequest->Request.io_Command = TR_ADDREQUEST;
before IExec->SendIO().

Go to top
Re: Timer.device problem
Just can't stay away
Just can't stay away


See User information
@salass00

Ahh... Thank you very much!

To justify my lack of knowledge, I can tell, that my source of information for the above code was "The programmers guide to the Amiga" from 1987...

Go to top
Re: Timer.device problem
Not too shy to talk
Not too shy to talk


See User information
@alfkil

Your code is missing a call to WaitIO to clean up the request sent by SendIO.

Also it makes absolutely no sense to use WaitPort on an I/O port. If you want to wait for an IORequest only, you can use WaitIO instead of WaitPort. If you want to wait for multiple signals, you have to use Wait anyway.

Finally, if you use WaitIO immediately after SendIO, you can as well replace both by one call to DoIO.

Use this as an example for an asynchronous timer:

#define __USE_OLD_TIMEVAL__ 1 // for compatibility with OS 3.x and below

#include <proto/exec.h>
#include <proto/dos.h>
#include <devices/timer.h>

int main (void)

{
struct MsgPort *port;
struct timerequest *timer;
BOOL running;
ULONG timersig;
ULONG received;
ULONG counter;

port CreateMsgPort();
if (
port)
    {
    
timer = (struct timerequest *) CreateIORequest (port,sizeof(struct timerequest));
    if (
timer)
        {
        if (!
OpenDevice ("timer.device",UNIT_VBLANK,(struct IORequest *)timer,0))
            {
            
timersig 1L << port->mp_SigBit;

            
timer->tr_node.io_Command TR_ADDREQUEST;
            
timer->tr_time.tv_secs    1;
            
timer->tr_time.tv_micro   0;
            
SendIO ((struct IORequest *)timer);

            
counter 5;

            
running TRUE;
            do    {
                
Printf ("Please press Ctrl-C (%lu seconds to go)\n",counter);

                
received Wait (timersig SIGBREAKF_CTRL_C);

                if (
received SIGBREAKF_CTRL_C)
                    {
                    
Printf ("Ctrl-C received; timer stopping.\n");
                    
running FALSE;
                    }

                if (
received timersig)
                    {
                    
WaitIO ((struct IORequest *)timer);

                    
counter --;
                    if (
counter == 0)
                        {
                        
Printf ("Timeout; no Ctrl-C received.\n");
                        
running FALSE;
                        }
                    else
                        {
                        
timer->tr_node.io_Command TR_ADDREQUEST;
                        
timer->tr_time.tv_secs    1;
                        
timer->tr_time.tv_micro   0;
                        
SendIO ((struct IORequest *)timer);
                        }
                    }
                }
            while (
running);

            if (
counter 0)
                {
                if (!
CheckIO ((struct IORequest *)timer))
                    
AbortIO ((struct IORequest *)timer);
                
WaitIO ((struct IORequest *)timer);
                }

            
CloseDevice ((struct IORequest *)timer);
            }
        
DeleteIORequest ((struct IORequest *)timer);
        }
    
DeleteMsgPort (port);
    }

return (
0);
}


Using UNIT_MICROHZ for long-term intervals is a waste of timer hardware and inaccurate also. There is a discussion about the different units in the beginning of the timer.device autodocs.

Go to top
Re: Timer.device problem
Just can't stay away
Just can't stay away


See User information
@thomas + alfkil

Also use AllocSysObjectTags() instead of CreateMsgPort() and CreateIORequest() on AmigaOS 4.x+.

Rock lobster bit me - so I'm here forever
X1000 + AmigaOS 4.1 FE
"Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
Go to top
Re: Timer.device problem
Just can't stay away
Just can't stay away


See User information
@TSK & thomas

Thanks guys, that's a lot of great info right there . And sorry for being such a dumbass, these are still my very first steps with device programming.

@TSK

What exactly is the reason for using AllocSysObjectsTags intsead of
CreateIORequest? Is it smth with the type of memory allocated or...?

Go to top
Re: Timer.device problem
Supreme Council
Supreme Council


See User information
@alfkil

AllocSysObject() and AllocDosObject() both implement resource tracking, so that unfreed resources will be returned to the system after the current task exits.

Of course, you should never rely on this fact to implement lazy programming, but it is usefull in genuine situations were deallocations get missed.

Simon

Comments made in any post are personal opinion, and are in no-way representative of any commercial entity unless specifically stated as such.
----
http://codebench.co.uk
Go to top
Re: Timer.device problem
Not too shy to talk
Not too shy to talk


See User information
I am sure that CreateIORequest and CreateMsgPort call AllocSysObject internally, so it's not such a big difference to call this or that function.

AllocSysObject was introduced to standardize the allocation of system structures and to reduce the number of CreateXXX functions.

It's ok to use AllocSysObject if you write software for OS4 only. But if your code should remain portable to other Amiga platforms, it's better to use functions which exist on all systems.

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project