Exact time between loop

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Exact time between loop

748 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Sat May 21 15:35:06 MST 2011
Hi everyone,

I am having an problem, let me explain.
I am trying to build an quad copter, i need to reed 2 sensors, an acc and gyro.
To control the quad copter i need to use an DCM IMU, this well calculate the sensor data to amount degrees my quad copter is.
With that data i can make an pid to control it.

The problem is, the DCM IMU needs to run exact an amount of time, for example 50hz. So the time between every loop needs to be 20ms.
I used the example libary and used timer.c & timer.h.
To make the counter count every 10us i edited this:
    LPC_TIM0->PR  = pclk/1000000; /* set prescaler to get 1 M counts/sec */

    LPC_TIM0->MR0 = 10; /* Set up 10 mS interval 1000 * 10 = 10ms */
    LPC_TIM0->MR1 = 10; /* Set up 10 mS interval */


The counter needs to be 2000.
It could be that the loops runs at 23ms, to be sure my calculations are right i can use this:
G_Dt = timer/100000.0
This should always be 0.02.

I think the loop is not precise 20ms, how can i measure this?
The problem could be i2c, i need to read my data by i2c, this could have some delay in it.
Can i see how long it takes to run an i2c reading loop?
Is there also another way to make an loop that runs precise on 20ms?
To be sure the counter is always 2000 i made an if statement, used the debug mode to see if the counter is always 2000 when it enters the loop. It's always 2000.

Using an lpc EM 1769 board.
I hope i explained everything so you can understand what i mean.
If not please ask.
0 Kudos
16 Replies

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu May 26 09:43:09 MST 2011
If i knew i needed to use the V1 lib i am sure i did use it.
It's not a problem to make a little search to find the right variable to get my program working.
Systick works great, i am having a loop delay time of 1ms.
The logic analyzer gives me an delay of 1ms so that's great.
I now added some i2c readings and my time has rised to more then 4ms.
I can not use fast i2c because one of my sensors does not support fast dmx.
I am now running the loop at 5ms (250hz) that will be high enough, the next time i will use analog sensors, reading analog sensors will be much faster.
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Thu May 26 05:28:19 MST 2011
Thanks,

Quote: larryvc
Yes NXP,  WHY?
...
Sorry, but there is really no excuse for sloppiness from a company as reputable as yours.



I feel like I've been banging my head against the wall with similar issues.
It seems like they don't care about the quality of documentation and software.

The bad thing is that I am not confident that companies like ST, Atmel or TI will be different :eek:

I think it is up to us, users of the LPCXpresso tools, to create a working set of initialization code and drivers for the different lpc1xxx variants. One complexity is that we may need different drivers for stand-alone use and use with an RTOS like FreeRTOS. I already made some changes to my I2C driver to make it thread safe with FreeRTOS so that I can address different peripherals on the same I2C bus from different tasks running in parallel.

BTW: FreeRTOS is a very nice way of getting the exact time between loops needed. Using the vTaskDelayUntil() you can let a task sleep until a certain point in time. If you need a 1 msec. interval (I use this for debouncing keys) you can use the tick hook function.

Rob
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu May 26 04:19:10 MST 2011
No problem ;)  Just to be sure.
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Thu May 26 03:37:43 MST 2011
:o Oh, I'm sorry Lithium,  that was for NXP.  Definitely not for you.:):)
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu May 26 02:53:28 MST 2011
This is to NXP or to me?:confused:
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Thu May 26 02:50:10 MST 2011

Quote: Lithium

I downloaded the example code and found the version 2, why do they publish a version that's not bug free?



Yes NXP,  WHY?  Many of us, while trying to learn about your chips, found that the CMSIS labeled v2 in that code bundle is in fact v1.2.  This has wasted our time, and for many of us time is money.

Sorry, but there is really no excuse for sloppiness from a company as reputable as yours.

Please take the mcb1700.code.bundle.lpc1769.lpcxpresso.zip code bundle off your web site until CMSIS v2 is fixed so others don't get led astray.

Thank You,
Larry
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Thu May 26 01:59:29 MST 2011
Thanks for your answers.
I didn't know that, good to know!

I now made a while project with that lib, everything works fine and i did not had any problems like that.
I will be a work to bring it back to version 1.
I downloaded the example code and found the version 2, why do they publish a version that's not bug free?

That also explains why i can't use any other example code.
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed May 25 10:10:53 MST 2011
Specifically this post in that thread:

http://knowledgebase.nxp.com/showpost.php?p=7076&postcount=14

I have been in a dialog with CodeRedSupport about this issue and the last thing they reported is that NXP is currently working to resolve this discrepancy.  It might be a while.

As whitcoe has suggested, don't use the CMSISv2 provided in the  MCB1700 bundle.  It will only confuse and frustrate you as it did me.  Stick with CMSISv1p30_LPC17xx for now.

CodeRedSupport can probably update us later on this continuing saga.
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by whitecoe on Wed May 25 08:31:59 MST 2011

Quote: Lithium
HI,

Thanks for your anwser.
I am not using v1 but v2.
Found SystemFrequency is that correct?



SystemFrquency is basically the same as SystemCoreClock.

But it sounds like you are using the CMSIS library NXP provided in the port  of their MCB1700 bundle. I would suggest that you don't - there are  issues with this that are still to be resolved....

http://knowledgebase.nxp.com/showthread.php?t=1600

HTH!
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Wed May 25 08:24:40 MST 2011
HI,

Thanks for your anwser.
I am not using v1 but v2.
Found SystemFrequency is that correct?
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed May 25 08:22:26 MST 2011

Quote: Lithium
I am trying to use the example you told me to use.
I am getting an error on SystemCoreClock. It can not find it. (Undeclared, first use in this function)



SystemCoreClock is defined within CMSIS. Thus it sounds like you are not linking with the appropriate CMSIS library project (CMSISv1p30_LPC17xx). More information on CMSIS support in LPCXpresso IDE at:

http://support.code-red-tech.com/CodeRedWiki/Support4CMSIS

Regards,
CodeRedSupport.
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Wed May 25 05:02:01 MST 2011
I am trying to use the example you told me to use.
I am getting an error on SystemCoreClock. It can not find it. (Undeclared, first use in this function)
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Sun May 22 09:31:31 MST 2011
yes i know,

I posted it with the question if i understand the code correctly.
I measured the time between every loop with an logic analyser, 20ms loop in program is in real 23.9ms.
Think that could be the problem.
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat May 21 16:07:29 MST 2011
This is a Keil example (96MHz).
Please use LPCXpresso1768_systick example in LPCXpresso1768.zip (example folder).
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Lithium on Sat May 21 15:56:14 MST 2011
I also use i timer for my RC input, thought i can use it also for my 50hz loop.
I already have seen the sys tick but i can not understand how to use it.
Do not know how to calculate the amount of ticks for example 1us.
I have now imported the systick example:

#include "NXP/LPC17xx/LPC17xx.h"
#include "NXP/LPC17xx/type.h"
#include "NXP/LPC17xx/target.h"

#define NVIC_SYSTICK_CLK        0x00000004
#define NVIC_SYSTICK_INT        0x00000002
#define NVIC_SYSTICK_ENABLE        0x00000001

int gi_SystickCount;

// Enable interrupts
static inline void __enable_irq()    { asm volatile ("cpsie i"); }

// Initialize the CM3 System Tick timer, given desired rate and the current CPU rate
void vF_SysTickInit(int i_CPUClockHz, int i_SystickHz)
{
    gi_SystickCount = 0;
    
    /* Configure SysTick to interrupt at the requested rate. */
    NVIC_ST_RELOAD = ( i_CPUClockHz / i_SystickHz ) - 1UL;
    NVIC_ST_CTRL = NVIC_SYSTICK_CLK | NVIC_SYSTICK_INT | NVIC_SYSTICK_ENABLE;
}

// Interrupt handler just increments a count forever.
// Note that CM3 interrupt handlers are regular C functions - no special attributes.
void vF_SysTickHander(void)
{
    gi_SystickCount++;
    
}

int main(void) {

    TargetResetInit();    
    
    vF_SysTickInit(96000000, 10);

    __enable_irq();
    
    // Enter an infinite loop, just incrementing a counter
    volatile static int i = 0 ;
    while(1) {
        i++ ;
    }
    return 0 ;
}

I am trying to understand how to use it.
vF_SysTickInit(96000000, 10);
96000000 is the frequency, that should be 12000000 for my board?
The function vF_SysTickInit uses the var name i_SystickHz, i think i need to give an number to count at x hz.
I can use 10 for 10 hz? Is that correct?
0 Kudos

645 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat May 21 15:49:58 MST 2011
Why don't you use SysTick Timer?

Simplest method to check timing is port toggling & scope.
0 Kudos