cancel
Showing results for 
Search instead for 
Did you mean: 

Any atomic I/O bit access functions for STM8L?

Pavel A.
Evangelist III
Posted on October 23, 2017 at 21:36

I'm looking at file stm8l10x_gpio.c from the 'standard peripheral library'

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm8-embedded-software/stsw-stm8012.html

 

and there does not seem to be a function to switch individual GPIO bits atomically.

For example the following compiles with Cosmic compiler to read-modify-write sequence

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint8_t GPIO_Pin)

{

GPIOx->ODR |= GPIO_Pin;

}

as well as this:

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, BitAction GPIO_BitVal)

{

if (GPIO_BitVal != RESET)

{

  SetBit(GPIOx->ODR, GPIO_Pin);

}

else

{

  ClrBit(GPIOx->ODR, GPIO_Pin);

}

}

So when interrupt handler drives some GPIO pin and the main program/other ISR uses other pin in same GPIO register, locking is needed. But ST8 has special instructions for manipulating single bits.

Macros SetBit, ClrBit are defined in stm8l10x.h and neither these are atomic.

Are there any intrinsic functions for bit set/clear (Cosmic compiler if possible)?

Thanks,

-- pa

12 REPLIES 12
Posted on October 28, 2017 at 00:09

Philipp,

My concern is only about functionality of specific part of the STM8 library (GPIOs). 

I think, if the architecture supports lockless toggling of individual GPIO bits, the periphs library should feature it. 

None far going implications on C compilers. Inline asm can work for for compilers that cannot generate bit instructions.

On STM32 (if a similar library exists there, I have not checked) the GPIO API should use method suitable for that architecture.

Is there any way to propose patch for the periphs library to ST?

Regards,

-- pa

Posted on October 28, 2017 at 16:59

I was developping a lot on STM8 few years back and at that time Cosmic compiler was taking care of the volatile registers properly. To me, with so many commercial devices out there the problem might just have been solved...

http://www.st.com/content/st_com/en/about/media-center/press-item.html/t3782.html

 
Posted on October 31, 2017 at 21:38

The best solution for me is to inline definition of 

GPIO_WriteBit and move it to the .h file.

When the bit numbers are constants (they typically are)  Cosmic compiler produces bset and bres instructions.

Actually, SetBit and ClrBit macros 

produce bset and bres instructions, as is, without compiler-specific tricks.

Dunno if inline definitions in .h files are against some coding standard or policy. Works for me

The only thing left to wish is a compiler intrinsic equivalent to GCC __builtin_constant_p , to ensure that parameters that must be constants indeed are.

Regards,

- p