STM32F103C8T6 delay not precise

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

User avatar
InvalidException
Posts: 13
Joined: Tue Nov 15, 2016 2:33 pm
Been thanked: 3 times

Re: STM32F103C8T6 delay not precise

Post by InvalidException »

he is using this
https://github.com/PetteriAimonen/ED060 ... c#L79-L105

and I don't like to mix it with timers/delays (not reliable) so trying right way
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: STM32F103C8T6 delay not precise

Post by RoccoMarco »

It seems another repository. it is uGFX. BTW a specific driver which uses timer is the best solution I thinks. On that driver you can built a small graphic library and thus a complex application.
User avatar
InvalidException
Posts: 13
Joined: Tue Nov 15, 2016 2:33 pm
Been thanked: 3 times

Re: STM32F103C8T6 delay not precise

Post by InvalidException »

I have tried with timers but no success :(
Not at that level yet that I can easily develop with ChibiOS (yet :) )

But here is solution I have got working and tested with Logic Analyzer
Got times
for turning ON 1us
for turning OFF 1.125us (without palSetPadModetime is 1.35us just for reference)

Question
Is possible to get direct access to registers for bit changing (Instead palSetPad/palClearPad)???
Maybe this small delay is related to that

Code: Select all

#include "hal.h"
#include "ch.h"

void clockdelay(int us);

int main(void) {
    halInit();
    chSysInit();

   palSetPadMode(GPIOC, GPIOC_LED, PAL_MODE_OUTPUT_PUSHPULL);

    while(1) {
        palSetPad(GPIOC, GPIOC_LED);
      clockdelay(1);
        palClearPad(GPIOC, GPIOC_LED);
      clockdelay(1);
    }
    return 0;
}

void clockdelay(int us)
{
    for(int i = 0; i<12*us;i++) {
      __asm__("  nop;");
   }
}
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: STM32F103C8T6 delay not precise

Post by steved »

InvalidException wrote:Question
Is possible to get direct access to registers for bit changing (Instead palSetPad/palClearPad)???

They are macros which implement direct register access in the most efficient manner supported by the processor. (For example, the ST family have instructions allowing you to set and clear specific bits in a register without affecting the others).
So coding it in a processor-specific way shouldn't make any difference
User avatar
InvalidException
Posts: 13
Joined: Tue Nov 15, 2016 2:33 pm
Been thanked: 3 times

Re: STM32F103C8T6 delay not precise

Post by InvalidException »

I check a bit implementation and it is OK have no impact

But would like to sort this micro delay, can someone help me with timers and how I can get it working for delay in us???
I'm new to this and would use some help, I have enable GPT and not sure which timer should I use?
Can be any of timers or need to be special ones? as in docs only timer 2 is 32bit rest are 16bit
And I saw in few examples TIM6 is used for general purposes so can I use it also?

My biggest confusion with timers is how to use them for delay...
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F103C8T6 delay not precise

Post by Giovanni »

Hi,

An accurate way to have delays is to use gptPolledDelay() however consider that IRQs and higher priority threads preempting your code will add additional delay. Delays should always to be handled as "guaranteed minimum".

What is your use case exactly? there are tricks that can be used to generate accurate signals, using SPI for example or timers.

Giovanni
User avatar
InvalidException
Posts: 13
Joined: Tue Nov 15, 2016 2:33 pm
Been thanked: 3 times

Re: STM32F103C8T6 delay not precise

Post by InvalidException »

Hi Giovanni

I need delays for LCD, they need to be 1, 2, 4, 5, 10, 100 and 200 micro seconds(us)
so some helper method would be nice to make: delay_us(uint32_t utime)

Also I did next:
In halconf.h
#define HAL_USE_GPT TRUE

In mcuconf.h
#define STM32_GPT_USE_TIM1 TRUE

TImer config

Code: Select all

static const GPTConfig gpt1cfg = {
  10000,
  NULL, 
  0,
  0
};


and in main.c

Code: Select all

gptStart(&GPTD1, &gpt1cfg);


so my delay_us would be

Code: Select all

void delay_us(int us)
{
    gptPolledDelay(&GPTD1, us);
}


this is only theory didn't try yet as not sure if this is right path? Anything I missed or this is OK?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F103C8T6 delay not precise

Post by Giovanni »

You need to set the GPT clock to 1000000 if you want uS resolution.

It is not magic, it is an HW timer, you need to read the STM32 reference manual.

Giovanni
Post Reply