cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103:Problem with USART when use System Tick Timer Delay in us

dark_killer_1796
Associate II
Posted on February 28, 2015 at 03:34

Hi all,

I am using STM32F103RCT6 and CP2102 as UART controller driver.

I have problem with USART when I use SysTick Timer to generate 1us Delay.

The problem is that when I configure SysTick at frequency 1Mhz, the program does not work.

SysTick_Config(SystemCoreClock / 1000000)   // SystemCoreClock is 72 Mhz

I use SysTick to generate 1us Delay: 

void Delay(__IO uint32_t nTime)

 

 

TimingDelay = nTime;        // Timing Delay is a volatile uint32_t variable

 

while(TimingDelay != 0);

 

}

 

void TimingDelay_Decrement(void)

 

{

 

if (TimingDelay != 0x00)

 

 

TimingDelay--;

 

}

 

}

 

SysTick handler

void SysTick_Handler(void)

{

 

TimingDelay_Decrement();

 

Tick++;     // my private variable for counting     

 

}

 

That is how I create my Delay function in microsecond.

It works well, but when I want to add USART code, the program doesn't work. 

When I call USART_Configuration(); in main, the program stucks.

void USART_Configuration(void)

 

{

 

USART_InitTypeDef USART_InitStructure;

 

 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 

 

 

 

/* Configure PA9 for USART Tx as alternate function push-pull */

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 

GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 

/* Configure PA10 for USART Rx as input floating */

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

 

GPIO_Init(GPIOA, &GPIO_InitStructure);

 

 

/* USARTx configured as follow:

 

        - BaudRate = 115200 baud  

 

        - Word Length = 8 Bits

 

        - One Stop Bit

 

        - No parity

 

        - Hardware flow control disabled (RTS and CTS signals)

 

        - Receive and transmit enabled

 

  */

 

USART_InitStructure.USART_BaudRate = 115200;

 

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

 

USART_InitStructure.USART_StopBits = USART_StopBits_1;

 

USART_InitStructure.USART_Parity = USART_Parity_No;

 

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

 

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 

 

/* USART configuration */

 

USART_Init(USART1, &USART_InitStructure);

 

 

/* Enable USART */

 

USART_Cmd(USART1, ENABLE);

 

}

 

I tried to configure SysTick at 100Khz (delay with resolution 10us), it worked.

SysTick_Config(SystemCoreClock / 100000);

But my application needs a delay at us. I don't know what is the reason and how to solve it. Can anyone give me some advice. 

Thank you for reading.

#it''s-deja-vu-all-over-again
3 REPLIES 3
Posted on February 28, 2015 at 04:43

Interrupting at 1 MHz is insane, 72 machine cycles, 22 cycles for an interrupt doing nothing, perhaps 3 cycles per FLASH read. You really need to ponder how the machine works.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Systick%20Delay%20functions&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=120]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FSystick%20Delay%20functions&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=120

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
childresss
Associate II
Posted on February 28, 2015 at 07:03

delayMicroseconds() with a 1mSec systick - is done by reading the systick timer and calculating how many microseconds (from counter's clock freq) since the last 1mSec counter match where the timer was reset. No silly loops.

Posted on February 28, 2015 at 16:24

But the granularity is a little courser, and the math is more awkward.. Not sure that's a win

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..