cancel
Showing results for 
Search instead for 
Did you mean: 

MCU family Preprocessor checks to determine functionality in a library

Enforcer83
Associate II

I am writing multiple libraries that use either I2C or SPI for communication.  I recently discovered that the STM32F4xx family had an interesting "feature" that will take the SPI NSS signal low and keep it low until the SPI module is disabled versus toggling the NSS line based on data transmission when hardware control is specified for the NSS signal.  I want the libraries I am writing to be "smarter than the average bear" so a user who has no knowledge of any particular MCU's quirks can easily use the library.

Taking a generic SPI Flash memory module as an example and using the aforementioned STM32F4xx family MCU, is there a "define" specified within the IDE or the HAL's that will allow me to do something similar to the following code snippet?  I found something similar to what I am trying to achieve in the system_stm32f4xxx.c file that is auto generated by CubeMX/CubeIDE.  I am just looking for a way to cover an entire family of MCU's.

#include "stm32f4xx_hal.h"
#include <stdbool.h>

typedef struct {

    // SPI
	SPI_HandleTypeDef *_spiHandle;
	GPIO_TypeDef *_csBank;
	uint16_t _csPin;

    bool _usingHW_CS

} FLASH_MEM;

void genericFlashInit(FLASH_MEM *myFlash, SPI_HandleTypeDef *spiHandle,
                      GPIO_TypeDef *csBank, uint16_t csPin, bool usingHW_CS) {    
    
    myFlash->_spiHandle = spiHandle;
    myFlash->_csBank = csBank;
    myFlash->_csPin = csPin;
    
    #if defined(STM32F4xx)
    myFlash->_usingHW_CS = false;
    #else
    myFlash->_usingHW_CS = usingHW_CS;
    #endif

}

 

2 REPLIES 2

@Enforcer83 wrote:

the STM32F4xx family had an interesting "feature" that will take the SPI NSS signal low and keep it low until the SPI module is disabled


This is the documented behaviour.

 

The used chip part number gets defined in the Project defines:

AndrewNeil_0-1747309704175.png

you could use that?

 

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@Andrew Neil wrote:


This is the documented behaviour.


I know, I was just using a euphemism because documented or not its not what is expected.

 


@Andrew Neil wrote:

The used chip part number gets defined in the Project defines:

you could use that?




I could but that would mean specifying every chip part number within the preprocessor check.  I want to avoid that as much as possible such as in a situation where the entire family of chips is affected versus just a couple within a family.

I suppose I could perform a check against the top level define located in the HAL header file as the check in my library for full families.  I just didn't know if there was better way.