BluePill delayMicroseconds issue

InvalidException
Fri Nov 11, 2016 2:37 am
Hi all

have LCD project ongoing but is time crucial and I need clear 1us delay but on sample I have on LED if I put delayMicroseconds(1) in reality is bit different
Here is LogicAnalyzer image
Image

How can this be done to get normal 1us delay ???

P.S.
delay in ms works fine


Rick Kimball
Fri Nov 11, 2016 2:43 am
one approach: http://www.stm32duino.com/viewtopic.php?t=326

-rick


RogerClark
Fri Nov 11, 2016 4:04 am
Or turn off interrupts and use “nop”s

InvalidException
Fri Nov 11, 2016 7:07 pm
Here is update with @Rick implementation
Image

small difference shouldn’t effect a lot (on 100us got 100.1us)
but it is working nice :)

Thx a lot for help


RogerClark
Fri Nov 11, 2016 7:57 pm
Are you sure the timing on your logic analyser is accurate to a few hundred nano seconds ?

ddrown
Fri Nov 11, 2016 9:39 pm
RogerClark wrote:Are you sure the timing on your logic analyser is accurate to a few hundred nano seconds ?

RogerClark
Fri Nov 11, 2016 10:38 pm
I have a 100Mhz analyser and also a 100Mhz bandwidth scope, if you OP could post the exact code I could look at it on those.

InvalidException
Fri Nov 11, 2016 11:26 pm
I have tested on @Rick blink sample and measure on Rigol 100Mhz
here is output and it looks OK

Image


InvalidException
Sun Nov 13, 2016 7:55 pm
trying to replicate issue for STM library SPL and HAL but can’t get correct value
Does anybody knows what is so special/different in Arduino code that works normally but on SW4STM32 and ST libs I can’t get same timings

InvalidException
Wed Nov 16, 2016 12:59 am
Here is my example I’m trying to replicate and for ms is working fine (got 1.01ms)
but if I run _delay_us(1) I get strange value (2.25us) and that is lowest value I can get :(
P.S. (with GPIO_SetBits/GPIO_ResetBits that same value is 2.625us)

Do I need to configure clocks/interupts or what to get same timing like STMduino???
Where did I make mistake? Anyone faced similar issue?

#include "stm32f10x.h"

void _delay_us(uint32_t us);
void _delay_ms(uint32_t ms);
void InitGPIO();

int main(void) {
InitGPIO();
while (1) {
GPIOC->ODR = 1<<13;
_delay_ms(1);
GPIOC->ODR = 0<<13;
_delay_ms(1);
}
}

void InitGPIO() {
//def port C (LED)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructureC;
GPIO_InitStructureC.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructureC.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructureC.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructureC);
}

void _delay_us(uint32_t nus) {
u32 temp;
SysTick->LOAD = 8 * nus;
SysTick->VAL = 0X00;
SysTick->CTRL = 0X01;
do {
temp = SysTick->CTRL;
} while ((temp & 0x01) && (!(temp & (1 << 16))));
SysTick->CTRL = 0x00;
SysTick->VAL = 0X00;
}

void _delay_ms(uint32_t nms) {
u32 temp;
SysTick->LOAD = 9000 * nms;
SysTick->VAL = 0X00;
SysTick->CTRL = 0X01;
do {
temp = SysTick->CTRL;
} while ((temp & 0x01) && (!(temp & (1 << 16))));
SysTick->CTRL = 0x00;
SysTick->VAL = 0X00;
}


RogerClark
Wed Nov 16, 2016 1:49 am
Libmaple sets up systick to increment every millisecond.

Leave a Reply

Your email address will not be published. Required fields are marked *