cancel
Showing results for 
Search instead for 
Did you mean: 

stm32_lock.h used in CPP critical section - Won't build without change

JGurley
Associate

I have created a CPP critical section class using stm32_lock_acquire and stm32_lock_release.  The main issue is in stm32_lock.h the file  #include <cmsis_compiler.h>.  The stock version of this file, included with CubeMx won't work if included in a c++ file.  Investigating, I found that if I move the cmsis_compiler.h include in to the extern "C" block, it works just fine.  Am I misusing stm32_lock?  or is this a suitable way to create a critical section in cpp?

Before change - won't compile because cmsis_complier.h or something it includes cannot complied with a c++ compiler.

#ifndef __STM32_LOCK_H__
#define __STM32_LOCK_H__

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stddef.h>
#include <cmsis_compiler.h>

#ifndef STM32_THREAD_SAFE_STRATEGY
#define STM32_THREAD_SAFE_STRATEGY 2 /**< Assume strategy 2 if not specified */
#endif /* STM32_THREAD_SAFE_STRATEGY */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


/* Function prototypes -------------------------------------------------------*/
void Error_Handler(void);

After change - now it will compile fine since the include is inside the extern "C" block.

#ifndef __STM32_LOCK_H__
#define __STM32_LOCK_H__

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stddef.h>

#ifndef STM32_THREAD_SAFE_STRATEGY
#define STM32_THREAD_SAFE_STRATEGY 2 /**< Assume strategy 2 if not specified */
#endif /* STM32_THREAD_SAFE_STRATEGY */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

  #include <cmsis_compiler.h>

/* Function prototypes -------------------------------------------------------*/
void Error_Handler(void);

 

 

Header file

#ifndef __CRITICAL_SECTION_H__
#define __CRITICAL_SECTION_H__

#include "stm32_lock.h"

namespace Library
{
  /*
  CriticalSection
  ---
  Usage:
  void someInterruptSensitiveCode(void)
  {
    //not interrupt sensitive code
    {
      //this disables interrupts in the constructor
      //the name of the variable is not important.
      Library::CriticalSection newSection;

      //interrupts are now disabled
      //you may change variables without fear of being interrupted

    }//interrupts are re-enabled when newSection goes out of context or {}

    //not interrupt sensitive code

  }
  */
  class CriticalSection
  {
    public:

      CriticalSection();
      ~CriticalSection();

    private:

      LockingData_t lock = LOCKING_DATA_INIT;
  };
} // namespace Library

#endif //__CRITICAL_SECTION_H__

CPP file

#include "CriticalSection.h"
#include "stm32_lock.h"

namespace Library
{
  CriticalSection::CriticalSection()
  {
    // thread and interrupt safe lock
    stm32_lock_acquire(&lock);
  }

  CriticalSection::~CriticalSection()
  {
    // thread and interrupt safe lock
    stm32_lock_release(&lock);
  }
} // namespace Library

 

Warmest Regards,
Jason
0 REPLIES 0