Timer troubles

9 posts / 0 new
Last post
tekmage
tekmage's picture
Offline
Last seen: 2 years 2 weeks ago
Joined: 2011-10-09 03:19
Timer troubles

Hi All,

Running in to a bit of trouble with the timer.device. I'm been able to build one of the examples on the AmigaOS dev wiki which runs with out issue. I'm trying to use the time is such a way that I can jump around my code while maintaining the timer state. Here's my code:

  1. //Test of My Timer functions
  2.  
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <devices/timer.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8.  
  9.  
  10. struct BlankerData
  11. {
  12. struct MsgPort *TimerMP;
  13. struct TimeRequest *TimerIO;
  14. };
  15.  
  16. int PrepareTimer(struct BlankerData *bd)
  17. {
  18.  
  19.  
  20. uint32 error;
  21. int32 result_code = 0;
  22.  
  23.  
  24. IDOS->Printf( "--> PrepareTimer() Start\n" );
  25.  
  26.  
  27. //Allocate the TimerMP, assigned in blanker.h to create ASOT
  28. if ( bd->TimerMP == NULL )
  29. {
  30. IDOS->Printf( "--> PrepareTimer() AllocSysObjectTags bd->TimerMP\n" );
  31. bd->TimerMP = IExec->AllocSysObjectTags(ASOT_PORT, TAG_END);
  32. }
  33.  
  34. ///Setup the Timer device for high res control of the rendering
  35.  
  36.  
  37. if ( (bd->TimerIO == NULL) && (bd->TimerMP != NULL) )
  38. {
  39. IDOS->Printf( "--> PrepareTimer() AllocSysObjectTags bd->TimerIO\n" );
  40.  
  41. bd->TimerIO = IExec->AllocSysObjectTags(ASOT_IOREQUEST,
  42. ASOIOR_Size, sizeof(struct TimeRequest),
  43. ASOIOR_ReplyPort, bd->TimerMP,
  44. TAG_END);
  45. }
  46.  
  47. if (bd->TimerIO != NULL )
  48. {
  49. // Open the timer device
  50. error = IExec->OpenDevice( TIMERNAME, UNIT_VBLANK,
  51. (struct IORequest *) bd->TimerIO, 0L);
  52.  
  53. if ( error != 0 )
  54. {
  55. IDOS->Printf("--> PrepareTimer() Error: Could not OpenDevice %d\n", error);
  56. result_code = 1;
  57. }
  58. }
  59.  
  60. return result_code;
  61. }
  62.  
  63. int WaitForEvent(struct BlankerData *bd)
  64. {
  65. //Here we check to see if we get a signal from the timer or the blanker
  66. int32 return_code, loopcount = 0;
  67. int run = 0;
  68.  
  69. IDOS->Printf("-->WaitForEvent() Enter\n");
  70.  
  71. IExec->WaitPort(bd->TimerMP);
  72. /* Get the reply message */
  73. struct Message *TimerMSG;
  74. TimerMSG = IExec->GetMsg(bd->TimerMP);
  75.  
  76. if (TimerMSG == (struct Message *)bd->TimerIO)
  77. {
  78. IDOS->Printf("Request finished\n");
  79. }
  80. else
  81. {
  82. IDOS->Printf("Not our request, this is odd");
  83. }
  84.  
  85. // while (run == 0 )
  86. // {
  87.  
  88. // if ( (IExec->CheckIO( bd->TimerIO) ) != NULL )
  89. // {
  90. // IDOS->Printf("-->WaitForEvent() CheckIO is NULL \n");
  91.  
  92. // run = 1;
  93. // return_code = 0;
  94.  
  95. // }
  96.  
  97. // if ( loopcount >= 30 )
  98. // {
  99. // run = 1;
  100. // return_code = 2;
  101. // }
  102.  
  103.  
  104. // IDOS->Printf("-->WaitForEvent() Current Time %d\n", bd->TimerIO->Time.Seconds );
  105.  
  106.  
  107. // IDOS->Delay( 50 );
  108. // loopcount++;
  109.  
  110. // }
  111.  
  112.  
  113. return 0;
  114.  
  115.  
  116. }
  117.  
  118. void CleanForExit(struct BlankerData *bd)
  119. {
  120.  
  121.  
  122.  
  123. if ( bd->TimerIO != NULL )
  124. {
  125.  
  126. IDOS->Printf("-->CleanForExit() Freeing TimerIO\n");
  127.  
  128. IExec->CloseDevice((struct IORequest *) bd->TimerIO);
  129. IExec->FreeSysObject(ASOT_IOREQUEST, bd->TimerIO);
  130.  
  131. }
  132.  
  133. if ( bd->TimerMP != NULL)
  134. {
  135. IDOS->Printf("-->CleanForExit() Freeing TimerMP\n");
  136. IExec->FreeSysObject(ASOT_PORT, bd->TimerMP);
  137. }
  138.  
  139. }
  140.  
  141. int main()
  142. {
  143.  
  144. struct BlankerData *bd;
  145.  
  146. PrepareTimer(bd);
  147.  
  148. //Set the timer for when to show the next photo
  149. bd->TimerIO->Request.io_Command = TR_ADDREQUEST;
  150. bd->TimerIO->Time.Seconds = 10;
  151. bd->TimerIO->Time.Microseconds = 0;
  152. IDOS->Printf("--> main() Initialized TimerIO \n");
  153. IExec->SendIO((struct IORequest *)bd->TimerIO);
  154.  
  155. WaitForEvent(bd);
  156.  
  157. CleanForExit(bd);
  158.  
  159. bd = NULL;
  160.  
  161. return 0;
  162. }

The code builds but crashes here: TimerMSG = IExec->GetMsg(bd->TimerMP);

Uncommenting the while loop in WaitforEvent (and commenting out the WaitPort stuff, will allow it to build and run but it crashes after exit. The crash is what I'm really after...

Any suggestion on what I'm doing wrong here?

Thanks!
Bill "tekmage" Borsari

LyleHaze
LyleHaze's picture
Offline
Last seen: 2 years 2 weeks ago
Joined: 2011-05-26 03:58
I'm working from a tiny

I'm working from a tiny netbook, so I might have missed the "big picture".;)

But when/where is the BlankerData structure allocated?

Main declares a POINTER to a BlankerData structure.. not the structure itself..

Unless I missed that part. ??

bd = AllocVecTags(sizeof(struct BlankerData... ))
Or something like that, with a matching free()later mght be a good start.

LyleHaze

tekmage
tekmage's picture
Offline
Last seen: 2 years 2 weeks ago
Joined: 2011-10-09 03:19
Hi Lyle! The BlankerData

Hi Lyle!

The BlankerData structure is declared at the beginning of the code. I played with AllocVecTags with no joy. Now I get a DSI at "bd->TimerIO->Request.io_Command = TR_ADDREQUEST;"

  1. int main()
  2. {
  3. struct BlankerData *bd;
  4. bd = IExec->AllocVecTags(sizeof(struct BlankerData));
  5.  
  6. ...
  7.  
  8. IExec->FreeVec(bd);
  9.  
  10. return 0
  11. }

Thanks,
Bill

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
bd =


bd = IExec->AllocVecTags(sizeof(struct BlankerData));

That should be at least:
bd = IExec->AllocVecTags(sizeof(struct BlankerData), TAG_END);

Also since your PrepareTimer() function seems to expect that the memory is cleared you really should request that it is in fact so:

  1. int main()
  2. {
  3. struct BlankerData *bd;
  4. bd = IExec->AllocVecTags(sizeof(*bd),
  5. AVT_ClearWithValue, 0,
  6. TAG_END);
  7.  
  8. ...
  9.  
  10. IExec->FreeVec(bd);
  11.  
  12. return 0
  13. }

Also your IExec->WaitPort()/IExec->GetMsg() code in WaitForEvent() can be replaced by a simple call to IExec->WaitIO():
IExec->WaitIO((struct IORequest *)bd->TimerIO)

LyleHaze
LyleHaze's picture
Offline
Last seen: 2 years 2 weeks ago
Joined: 2011-05-26 03:58
I'm so confused.. Bill, I

I'm so confused..

Bill, I didn't see the allocation in the original code.. I still don't.

In your revised code I see an AllocVecTags without any tags after it.. As Salass pointed out, Needs a TAG_END after all other tags, including a CLEARTOVALUE, 0, tag pair..
I do NOT have the autodocs in front of me. I'm away visiting family..
A good idea, especially when adding new code, is to look up each function call in the autodocs to get all the details needed.

Finally, Salass pointed out that you need to allocate sizeof(struct BlankerData), but then his example showed sizeof(*bd).. sizeof a pointer will not do.. size of the BlankerData structure is needed.

Stuck in a tiny room with three adults arguing about parade floats, a four year old crying about..?? (no clue) and grampa pulling another laptop out from under this one because "it HAS to be on"... Happy Thanksgiving!

I could be completely mistaken. But I'll try anyway!

LyleHaze

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Finally, Salass pointed out

Finally, Salass pointed out that you need to allocate sizeof(struct BlankerData), but then his example showed sizeof(*bd).. sizeof a pointer will not do.. size of the BlankerData structure is needed.

sizeof(bd) would be the size of the pointer.
sizeof(*bd) is the size of what the pointer points to, i.e. struct BlankerData.

P.S. I would never dare to hack on my laptop with relatives sitting around me trying to do conversation... (actually I wouldn't have my laptop with me in such a situation) ;-)

LyleHaze
LyleHaze's picture
Offline
Last seen: 2 years 2 weeks ago
Joined: 2011-05-26 03:58
You are correct.. Thank

You are correct.. Thank You!

Using * to de-reference a pointer might be confusing to new programmers (or old programmers trying to post from a netbook!)

Thanks for pointing that out.

:)
Lyle

LyleHaze

ssolie
ssolie's picture
Offline
Last seen: 11 years 3 weeks ago
Joined: 2011-02-03 06:55
Your "bd" pointer is pointing

Your "bd" pointer is pointing to nowhere so PrepareTimer() is clobbering memory.

hypex
hypex's picture
Offline
Last seen: 3 months 3 weeks ago
Joined: 2011-09-09 16:20
@tekmage Have you also tried

@tekmage

Have you also tried a standard WaitIO() on the Timer request?

Log in or register to post comments