Control time of a function's execution time

Post here first, or if you can't find a relevant section!
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Control time of a function's execution time

Post by mebab »

Hi, everyone!
I use STM32 to take photos by using an ARDUCAM mini. From time to time (for instance once a 100 times) it may stick to a piece of code including read/write registers functions and cannot come out of one of them.
I used a while-loop to limit the time that it takes to execute that piece of code. However, it didn't help at all. I know that a watchdog timer might be a solution to restart the code from scratch. However, I am looking to find a better solution to bypass that part because the next time in the loop, it might be able to simply execute it.

Many thanks for any help.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Control time of a function's execution time

Post by GonzoG »

It it goes into infinite loop then there is something wrong with the code.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Control time of a function's execution time

Post by dannyf »

tough to help without seeing what you are trying to do.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Control time of a function's execution time

Post by mebab »

I just found out that the function that gets stuck is the following:

Code: Select all

  while (!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK));   
But as I mentioned, it works fine most of the time and may stop rarely. I cannot find neither the reason nor any solution to bypass it. Therefore, the only way is to use a Watchdog timer. Please let me know if you know a better solution.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Control time of a function's execution time

Post by dannyf »

while (!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK)) if (cnt++>TIMEOUT_CNT) break;
assuming that myCAM.get_bit() returns frequently.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Control time of a function's execution time

Post by mebab »

Thank you!
Does this differ from the following method for 1000 ms that I have already implemented.

Code: Select all

  long cnt = millis();
  while (!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK)) if (millis()-cnt>1000) break;
If it is different, please let me know how to define cnt and TIMEOUT_CNT.

I think there is a problem with the function myCAM.get_bit. As I understand, it doesn't return a value for the while loop.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Control time of a function's execution time

Post by dannyf »

it doesn't return a value for the while loop.
unlikely - the program wouldn't compile if that's the case.

I think you will need to figure out if myCAM.get_bit() is returning the wrong value (0 in this case, assuming your code is correct), *or* its has a hung execution.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Control time of a function's execution time

Post by mebab »

When I print the values of the following command under discussion, before and after the mentioned while loop in normal situation, they are 0 and 8, respectively.
Still looking to catch the case where the while loop stops, to present the value at least before the while loop. I will inform you as soon as I have a new achievement.

Code: Select all

myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK) 
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Control time of a function's execution time

Post by ozcar »

mebab wrote: Sat Feb 18, 2023 9:01 pm Thank you!
Does this differ from the following method for 1000 ms that I have already implemented.

Code: Select all

  long cnt = millis();
  while (!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK)) if (millis()-cnt>1000) break;
If it is different, please let me know how to define cnt and TIMEOUT_CNT.
No real difference, but your way of using an actual time limit may be better.

If that does not work (as in, occasionally you still get "stuck" there even with you extra code in place), then it would seem that it is hanging somewhere within the myCAM.get_bit() processing. From a quick look at the ARDUCAM code, perhaps SPI.transfer() is not returning for some reason. To find out where, you would have to drill down into the ARDUCAM code and likely into the SPI processing too. That is something that would be quite easy to do if you are set up for "real" debugging using an STLINK, but it could also be done without that if you are prepared to spend some time on it.

As it stands myCAM.get_bit() has no way to return to you saying "I'm sorry Dave, I'm afraid I can't do that." I don't know how practical it would be to add some sort of timeout to ARDUCAM functions like that. So maybe that does mean you have to look at IWatchdog.

Edit:
I just noticed - you should really have defined cnt as unsigned (that is what millis() returns), but still lilely that myCAM.get_bit() is not returning to you.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Control time of a function's execution time

Post by dannyf »

before and after the mentioned while loop in normal situation, they are 0 and 8, respectively.
that means that the routine returns execution to the main loop, just with the unexpected value (8). you will need to see 1) why it returns that value; and 2) how you would continue execution if that value is returned.

so the question now is much simpler.
Post Reply

Return to “General discussion”