Skip to main content
After Forever
Senior III
August 16, 2017
Solved

STM32SnippetsL0 TIMER examples bug

  • August 16, 2017
  • 5 replies
  • 1065 views
Posted on August 16, 2017 at 17:33

In TIMERS/03_InputCapureOnTI1/main.c and TIMERS/04_PWM_Input/main.c the flags after an interrupt are being cleared this way:

TIMx->SR = ~(TIM_SR_CC1OF | TIM_SR_CC1IF); /* Clear the flags */

Shouldn't it be like below?

TIMx->SR &= ~(TIM_SR_CC1OF | TIM_SR_CC1IF); /* Clear the flags */

#timer #stm32snippetsl0 #bug
This topic has been closed for replies.
Best answer by Radosław
Posted on August 16, 2017 at 18:22

No,  version with and is wrong.  RMW operation for clearing flags is very bad idea.

5 replies

Radosław
RadosławBest answer
Associate II
August 16, 2017
Posted on August 16, 2017 at 18:22

No,  version with and is wrong.  RMW operation for clearing flags is very bad idea.

Tesla DeLorean
Guru
August 16, 2017
Posted on August 16, 2017 at 19:18

No this is a recurrent issue because people don't read and understand the manual and implementation.

RMW creates a hazard, it is not atomic, and the TIM can go through several cycles between the read and write operations.

Bit-Banding the TIM->SR is also highly hazardous, again not atomic from the peripherals perspective.

A singular write is contained to a cycle of the TIM.

The peripheral registers should not be viewed a memory cells, but as combinational logic.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
August 16, 2017
Posted on August 16, 2017 at 19:28

From the user point of view: if a register bit is marked as rc_w0 (or rc_w1) in the RM, it means extra circuitry which is there for a reason...

J\W