Sonsivri

Electronics => AVR, 8051 Family and ARM Area => Topic started by: karri on January 14, 2016, 09:59:32 09:59



Title: STM2F1, coocox a _delay_us
Post by: karri on January 14, 2016, 09:59:32 09:59
Hi, I am not sure, If I did not asked before, but forum search has not found anything.

I am new in CooCox and ARM, I worked with AVRs before. What I miss in CooCox is "delay.h" lib similar to AVR GCC. I hnow, that I can use SysTimer for milisecond delays, but what are you using for micro second delays? (something like _delay_us)

My current solution is:
Code:
void _delay_us(uint16_t nTime)
{
   TIM3->CNT = 0;
   while ( (TIM3->CNT) < nTime);
}
where TIM3 si set for us clock
Code:
void Timer3_Init(void)
{
    // 1 MHz clock
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Prescaler = 21 ;  // I have 22MHz clock
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    // Set one shot
    TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
    /* TIM2 enable counter */
    TIM_Cmd(TIM3, ENABLE);
}
It is clear, that it is not accurate for very short delays. But my typical delays are longer than 10us.
Is this OK, or you have some better solution?
Thank you


Title: Re: STM2F1, coocox a _delay_us
Post by: sam_des on February 02, 2016, 04:23:38 04:23
Good old Software delay with manually counting instruction cycles is only way.

For trial & error method, Use a simple for() loop in separate function, see how much delay it generates on scope & adjust it until you get desired result. Just know that each compiler or even each version of same compiler may implement your code differently. So you have to check every time you change compiler or version.

If you want more precision, get down to writing a ASM routine. Your routine will be portable at least across different versions of compiler without issues.

In any case you will have to adjust your parameters as per core clock you're using. Also any interrupts occurring during execution of routine will affect actual delay, unless you toggle them.

_sam_des