How to write the control statements for chip definitions ?

Hi,

I’m working on lcd128x64 spi library. And was working with my Arduino Atmega328p chip.

Now I want to run the code with my maple mini board.

So I have to differentiate the name on the pins for each chip. I find these statements in big libraries that supports different chips.

But I want to learn the basics of how to write these statements.

First of all, this is an example from SPI.CPP for Arduino core libraries.

// mapping of interrupt numbers to bits within SPI_AVR_EIMSK
#if defined(__AVR_ATmega32U4__)
  #define SPI_INT0_MASK  (1<<INT0)
  #define SPI_INT1_MASK  (1<<INT1)
  #define SPI_INT2_MASK  (1<<INT2)
  #define SPI_INT3_MASK  (1<<INT3)
  #define SPI_INT4_MASK  (1<<INT6)
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  #define SPI_INT0_MASK  (1<<INT0)
  #define SPI_INT1_MASK  (1<<INT1)
  #define SPI_INT2_MASK  (1<<INT2)
  #define SPI_INT3_MASK  (1<<INT3)
  #define SPI_INT4_MASK  (1<<INT4)
  #define SPI_INT5_MASK  (1<<INT5)
  #define SPI_INT6_MASK  (1<<INT6)
  #define SPI_INT7_MASK  (1<<INT7)
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
  #define SPI_INT0_MASK  (1<<INT4)
  #define SPI_INT1_MASK  (1<<INT5)
  #define SPI_INT2_MASK  (1<<INT0)
  #define SPI_INT3_MASK  (1<<INT1)
  #define SPI_INT4_MASK  (1<<INT2)
  #define SPI_INT5_MASK  (1<<INT3)
  #define SPI_INT6_MASK  (1<<INT6)
  #define SPI_INT7_MASK  (1<<INT7)
#else
  #ifdef INT0
  #define SPI_INT0_MASK  (1<<INT0)
  #endif
  #ifdef INT1
  #define SPI_INT1_MASK  (1<<INT1)
  #endif
  #ifdef INT2
  #define SPI_INT2_MASK  (1<<INT2)
  #endif
#endif

What I want to do is similar, so it should be like this:

  1. I found in STM32 spi examples the pin locations for each spi module.
/*    Using the second SPI port (SPI_2)
    SS    <-->  PB12 <-->  BOARD_SPI2_NSS_PIN
    SCK   <-->  PB13 <-->  BOARD_SPI2_SCK_PIN
    MISO  <-->  PB14 <-->  BOARD_SPI2_MISO_PIN
    MOSI  <-->  PB15 <-->  BOARD_SPI2_MOSI_PIN
*/

So I guess depending on the type of STM chip that specifies that it’s the on used on maple mini which is stm32f103.

So I found in stm32.h this definition but I don’t know if it specifies that it’s the chip on the maple mini.

There’s a main definition for stm32f1 series, then after that it defines the different chips in that series.

#if defined(__AVR_ATmega328P__)
	#define CS_PIN					10
	#define MOSI_PIN				11
	#define CLK_PIN					13
#elif defined(STM32_SERIES_F1)	
	#define CS_PIN					PB12
	#define MOSI_PIN				PB15
	#define CLK_PIN					PB13
#endif

I’m pretty sure that the names I used aren’t correct. I even didn’t encounter this line:

#if defined(__AVR_ATmega328P__) I wrote it myself.

When I get to the part of defining different chips, they mention chips other than the Atmega328p, why ? Is the Atmega328p the default one ?

Any suggestions ?

When I get to the part of defining different chips, they mention chips other than the Atmega328p, why ? Is the Atmega328p the default one ?

not sure why you suggest the Atmega328p is the default

the defines should be specific (there's an #if defined case) for a board, not processor. there can be many different boards using a Atmega328p.

you can always add a new case for your own board.

some libraries allow you to specify the pins to use as arguments to the library (e.g. class object constructor)

Yep, that what I mean, where to find the board defs ?

Also for the stm32 defs, could you tell me where to find it ?

i would look for a schematic

I mean the attribution name, like here:

#if defined(__AVR_ATmega328P__)
	#define CS_PIN					10
	#define MOSI_PIN				11
	#define CLK_PIN					13
#elif defined(STM32_SERIES_F1)	
	#define CS_PIN					PB12
	#define MOSI_PIN				PB15
	#define CLK_PIN					PB13
#endif

I don't mean the pins names, I mean the attribution names inside the braces.

I don't think the stm32 attribution name is correct ?

why would the code use different names the STM32 board

Have you tried "STM32_MCU_SERIES"?

the pins names are different:

in the arduino the spi pins are 10, 11 & 13
in stm32 there are 2 spi modules the ones on spi2 are PB12, PB15 & PB13

Danois90:
Have you tried "STM32_MCU_SERIES"?

No but I'm pretty sure it's not the right attribution. I just write it as an example of what I want to do.

Use a #if or #elif for each chip you support, then put a #error in the #else, so that it refuses to compile
for a chip it doesn't support.

MarkT:
Use a #if or #elif for each chip you support, then put a #error in the #else, so that it refuses to compile
for a chip it doesn't support.

OK, but I want the right attribution name for the chip I'm working with.

Like, the correct attribution for Atmega32u4 is AVR_ATmega32u4

But for STM32 I don't know how to get its attribution name. I'm trying to search in the libraries of STM32 but I don't know where to find it.

When I try to compile the code for the maple mini I get this:

Arduino: 1.8.12 (Windows 10), Board: "Maple Mini, Original (17k RAM,108k Flash), 72MHz (Normal), Smallest (default)"

H:\Programming\Program_Files\Arduino\libraries\lcd128x64_spi\lcd128x64_spi.cpp:14:20: fatal error: avr/io.h: No such file or directory

 #include <avr/io.h>

                    ^

compilation terminated.

exit status 1
Error compiling for board Maple Mini.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

The defines "AVR_ATmega328P" and alike are AFAIK generated by the preprocessor, and are based on the architechture and the MCU. Docs from microchip.

Danois90:
The defines “AVR_ATmega328P” and alike are AFAIK generated by the preprocessor, and are based on the architechture and the MCU. Docs from microchip.

Thank you so much, I want the same thing for the STM32 chip that is used in maple mini.