Using timing functions - page 2 - General Discussion and Assistance - CHDK Forum supplierdeeply

Using timing functions

  • 66 Replies
  • 25108 Views
*

Offline srsa_4c

  • ******
  • 4451
Re: Using timing functions
« Reply #10 on: 20 / March / 2014, 17:17:46 »
Advertisements
tl;dr

Note that using SetHPTimerAfterNow can (and will) cause timing drift, you may want to compensate for that somehow (by using that usec counter for example).
Another note: using the HP timer with that high repetition rate may cause problems. I'd recommend you to use a much slower loop to check for a 'start' signal (longer pulse), and switch to the high resolution timer when that arrives. This is for the receiver end of course.

@reyalp / philmoz
Can you split out all posts starting from here to a new thread ("Using the timer functions" or something)? edit: thx.  :D
« Last Edit: 21 / March / 2014, 15:54:58 by srsa_4c »

Re: Using timing functions
« Reply #11 on: 25 / March / 2014, 02:10:09 »
Quote
Note that using SetHPTimerAfterNow can (and will) cause timing drift, you may want to compensate for that somehow (by using that usec counter for example).
Another note: using the HP timer with that high repetition rate may cause problems. I'd recommend you to use a much slower loop to check for a 'start' signal (longer pulse), and switch to the high resolution timer when that arrives. This is for the receiver end of course.
Thanks for the tips, I'll probably check the input every 10 ms or so.
Is it possible to instead of setting the timer after now, to set it at a particular time (based on checking the usec counter). If this isn't possible, I might just run a continuous loop that checks the input, and reads the usec counter when it changes.

After some work and much distractions (please don't click the link if your a developer, we wouldn't want CHDK development to come to a sudden halt :D ) I've written some experimental code to transmit and receive a 16 bit packet.

However, it won't compile, giving the following error (amongst other identical errors with other functions)
../generic/wrappers.c: In function 'master_send_bit':
../generic/wrappers.c:1990:41: error: 'master_send_space' undeclared (first use in this function)

As master_send_bit is declared before master_send_space. But if I were to swap them around, it would be vice-versa. Any ideas on how I could get around this?

Here's some of the code in question:
Code: [Select]
int master_send_bit()
{
    *((volatile int *) MASTER_OUTPUT_ADDRESS) = MASTER_OUTPUT_MAGIC_NUMBER_ON;//Set LED on
    if (master_message_buffer & (1 << master_bit_number)) //Check if the next bit is a 1
    {
_SetHPTimerAfterNow(MASTER_BIT_1_DELAY,master_send_space,master_send_space,0);//delay for 1
    }
    else
    {
_SetHPTimerAfterNow(MASTER_BIT_0_DELAY,master_send_space,master_send_space,0);//delay for 0
    }
    master_bit_number++; //Should hopefully increase before master_send_space runs
}

int master_send_space()
{
    *((volatile int *) MASTER_OUTPUT_ADDRESS) = MASTER_OUTPUT_MAGIC_NUMBER_OFF;//Set LED off
    if(master_bit_number == 16) //data transmission has finished.
    {
_SetHPTimerAfterNow(MASTER_END_DELAY,master_end_transmission,master_end_transmission,0);
    }
    else
    {
        _SetHPTimerAfterNow(MASTER_DELAY_SPACE,master_send_bit,master_send_bit,0);
    }
}

edit: another question, I have the bitwise operator master_message_buffer & (1 << master_bit_number) used to extract the bit I want to send, it would be correct to have master_bit_number set to 0 to read the first bit right?
« Last Edit: 25 / March / 2014, 02:14:35 by Recyclojunk64 »

*

Offline reyalp

  • ******
  • 14080
Re: Using timing functions
« Reply #12 on: 25 / March / 2014, 02:24:33 »
Don't forget what the H stands for.

Re: Using timing functions
« Reply #13 on: 25 / March / 2014, 03:21:07 »
https://www.google.com/search?q=c+function+prototype

Sorry for the short answer, I have important things to do.

Thanks, works a treat. I'll be back with the results after a few more games...


*

Offline srsa_4c

  • ******
  • 4451
Re: Using timing functions
« Reply #14 on: 25 / March / 2014, 13:16:06 »
Is it possible to instead of setting the timer after now, to set it at a particular time (based on checking the usec counter).
It is possible, use SetHPTimerAfterTimeout.
http://chdk.setepontos.com/index.php?topic=10114.msg109695#msg109695
It's using *(int*)0xc0242014 as time base (reminder: this is a 20bit counter, prepare for wraparound).

Quote
another question, I have the bitwise operator master_message_buffer & (1 << master_bit_number) used to extract the bit I want to send, it would be correct to have master_bit_number set to 0 to read the first bit right?
Yes.

Re: Using timing functions
« Reply #15 on: 26 / March / 2014, 08:27:37 »
Quote
It is possible, use SetHPTimerAfterTimeout.
http://chdk.setepontos.com/index.php?topic=10114.msg109695#msg109695
It's using *(int*)0xc0242014 as time base (reminder: this is a 20bit counter, prepare for wraparound).

Thanks, however it seems to behave just like SetHPTimerAfterNow for some reason.

_SetHPTimerAfterTimeout(*(int*)0xc0242014,131072,master_begin_transmission,master_begin_transmission,0);
I would have expected the above to run when *(int*)0xc0242014 is equal to 131072, but it instead runs after 1/8 of a second, as SetHPTimerAfterNow would.

Here's the flashing code anyway. Ignore the unusual function and variable names.
Code: [Select]
int master_begin_transmission()
{
    *((volatile int *) MASTER_OUTPUT_ADDRESS) = MASTER_OUTPUT_MAGIC_NUMBER_ON;//Set LED on
    _SetHPTimerAfterTimeout(*(int*)0xc0242014,131072,master_begin_transmission_delay,master_begin_transmission_delay,0);
//    _SetHPTimerAfterNow(131072,master_begin_transmission_delay,master_begin_transmission_delay,0);
}

int master_begin_transmission_delay()
{
    *((volatile int *) MASTER_OUTPUT_ADDRESS) = MASTER_OUTPUT_MAGIC_NUMBER_OFF;//Set LED off
    _SetHPTimerAfterTimeout(*(int*)0xc0242014,131072,master_begin_transmission,master_begin_transmission,0);
//    _SetHPTimerAfterNow(131072,master_begin_transmission,master_begin_transmission,0);
}

I did have another idea though; a CPU hogging approach where 0xc0242014 would be continually read in a loop, and whenever it reaches the desired value, the LED will change. Wraparound shouldn't be to hard, my only question would be how fast would the continuous loop be, and would it likely interfere with othe canon processes?

Re: Using timing functions
« Reply #16 on: 26 / March / 2014, 08:29:43 »
and would it likely interfere with othe canon processes?
Only until the camera's watchdog timer rebooted everthing.
Ported :   A1200    SD940   G10    Powershot N    G16

Re: Using timing functions
« Reply #17 on: 26 / March / 2014, 10:22:09 »
Quote
my only question would be how fast would the continuous loop be

Initial tests good went with this, to test how long it takes to count to 64 million.
Code: [Select]
int master_begin_transmission()
{
    while (variable_to_increment < 67108864)
    {
        variable_to_increment++;
// unread_variable = *(short*)0xc090004a;
// unread_variable1 = *(int*)0xc0242014;
    }
    *((volatile int *) MASTER_OUTPUT_ADDRESS) = MASTER_OUTPUT_MAGIC_NUMBER_ON;//Set LED on
}

And it did it in about two seconds, 30 million per second. When polling the temp input, it was of course slower, at about 6 million per second, which is still more than fast enough. And about 4 million per second with reading the usec timer aswell. And the live-preview didn't even freeze while it was counting (although the buttons did of course).

So I will perform some tests with this method to see how accurately and fast I flash the LED. I do certainly hope the LED isn't fitted with a bypass cap...


Re: Using timing functions
« Reply #18 on: 26 / March / 2014, 12:32:43 »
When polling the temp input, it was of course slower, at about 6 million per second, which is still more than fast enough. And about 4 million per second with reading the usec timer aswell.
Seems strange that its slower reading a timer than an A/D converter.

Did you try polling the USB input bit and see how much that slows things down ?  ( get_usb_bit() )

IIRC, the watchdog timer takes 4 or 5 seconds to kick it.
Ported :   A1200    SD940   G10    Powershot N    G16

*

Offline srsa_4c

  • ******
  • 4451
Re: Using timing functions
« Reply #19 on: 26 / March / 2014, 19:49:28 »
Thanks, however it seems to behave just like SetHPTimerAfterNow for some reason.

_SetHPTimerAfterTimeout(*(int*)0xc0242014,131072,master_begin_transmission,master_begin_transmission,0);
I would have expected the above to run when *(int*)0xc0242014 is equal to 131072, but it instead runs after 1/8 of a second, as SetHPTimerAfterNow would.
Then try passing 0 as the first argument (which is the time base).

 

Related Topics