Author Topic: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

Offline igtabaTopic starter

  • Newbie
  • Posts: 8
  • Country: ar
Hello guys, again here I am with some newbiee questions.

I'm trying to tiding up a bit of code, and I want to simplify the code by defining actions so I can call them and erase some repetition.
I commented the next bit of code so you could see if what I'm trying to do is correct or not.

I'm using KEIL uVision 5 and running the code in a STM32F103C8

Code: [Select]
        // I'm pretty sure that this is correct, is a define just to know which pin is connected to the stteper motor
#define StepperA GPIO_PIN_9
#define StepperB GPIO_PIN_8
#define StepperC GPIO_PIN_7
#define StepperD GPIO_PIN_6
#define StepperE GPIO_PIN_5

        // This is what I'm NOT sure if is wright, I have seen this done is some libraries in the past, but never exactly like this, so can you tell me is this is ok?
#define StepperInit (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET))
#define StepperP1 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET))
#define StepperP2 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_RESET))
#define StepperP3 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_RESET))

        // Again, of this I'm pretty sure that works
#define StepperE_H (HAL_GPIO_WritePin(GPIOB, StepperE, GPIO_PIN_SET))
#define StepperE_L (HAL_GPIO_WritePin(GPIOB, StepperE, GPIO_PIN_RESET))


     // Then in the rest of the code, I would use it like this for example:
        //Init Stepper Motor
StepperE_H; // Enable HIGH
StepperInit; // Init A,B,C,D
HAL_Delay(1);
StepperE_L; // Enable LOW

So, please can you help me to know if the definition of multiple GPIO set and reset lines can be done in a single line of a #define ?
« Last Edit: December 04, 2018, 05:49:22 pm by igtaba »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11905
  • Country: us
    • Personal site
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence?
« Reply #1 on: December 04, 2018, 04:16:59 pm »
In its simplest form #define is a simple substitution. So what you are doing here will work. But it is not the best code practice. Just define the functions with those writes and call them instead.
Alex
 

Offline KaneTW

  • Frequent Contributor
  • **
  • Posts: 811
  • Country: de
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence?
« Reply #2 on: December 04, 2018, 04:26:45 pm »
Why not just make a function? You can even inline it if you're worried about the overhead. Macros are a terrible idea for that kind of thing.
 

Offline igtabaTopic starter

  • Newbie
  • Posts: 8
  • Country: ar
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]
« Reply #3 on: December 04, 2018, 05:46:43 pm »
I'm due for a project presentation tomorrow in uni, but it's the first time that I'm programming Cortex and using Keil uVision 5. It's been a buggy ride and sometimes the functions do not work properly and even that example that I put before didn't work. So I just stated everything every time I need it and works like a charm, maybe after the presentation I'm gonna sit relaxed to make the code more efficient, but for now works.

I actually want to keep learning Cortex but programming in Microsoft's Visual since it's what I always had used. But for this case the professor insisted that we use ARM IDE, so after this I'm ditching it

Sorry for my bad english, and again thanks for the answers!!
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11905
  • Country: us
    • Personal site
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]
« Reply #4 on: December 04, 2018, 05:52:26 pm »
Ok, I can pretty much guarantee that it is not Keil's fault. Keil has its bugs, but they are not something a beginner will ever experience.

Also what do you mean by  "Microsoft's Visual"? Where are you going to get that for Cotrex-M devices?
Alex
 

Offline igtabaTopic starter

  • Newbie
  • Posts: 8
  • Country: ar
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]
« Reply #5 on: December 13, 2018, 02:34:07 am »
I have always programmed in Microsoft Visual Studio, and you can use it to program a cortex device, there is a few tools to do it, but its like using Eclipse or others IDE with GNU tools
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4857
  • Country: dk
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]
« Reply #6 on: December 13, 2018, 02:50:53 am »
Hello guys, again here I am with some newbiee questions.

I'm trying to tiding up a bit of code, and I want to simplify the code by defining actions so I can call them and erase some repetition.
I commented the next bit of code so you could see if what I'm trying to do is correct or not.

I'm using KEIL uVision 5 and running the code in a STM32F103C8

Code: [Select]
        // I'm pretty sure that this is correct, is a define just to know which pin is connected to the stteper motor
#define StepperA GPIO_PIN_9
#define StepperB GPIO_PIN_8
#define StepperC GPIO_PIN_7
#define StepperD GPIO_PIN_6
#define StepperE GPIO_PIN_5

        // This is what I'm NOT sure if is wright, I have seen this done is some libraries in the past, but never exactly like this, so can you tell me is this is ok?
#define StepperInit (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET))
#define StepperP1 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET))
#define StepperP2 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_RESET))
#define StepperP3 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_RESET))

        // Again, of this I'm pretty sure that works
#define StepperE_H (HAL_GPIO_WritePin(GPIOB, StepperE, GPIO_PIN_SET))
#define StepperE_L (HAL_GPIO_WritePin(GPIOB, StepperE, GPIO_PIN_RESET))


     // Then in the rest of the code, I would use it like this for example:
        //Init Stepper Motor
StepperE_H; // Enable HIGH
StepperInit; // Init A,B,C,D
HAL_Delay(1);
StepperE_L; // Enable LOW

So, please can you help me to know if the definition of multiple GPIO set and reset lines can be done in a single line of a #define ?

#defines are simple text substitution so just separate things with ; like you normally would and put { } around the whole things so
something like

if(...) StepperE_H
 
doesn't fool you


 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4349
  • Country: us
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]
« Reply #7 on: December 13, 2018, 03:06:50 am »
Quote
Code: [Select]
	#define StepperInit (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET))
Traditionally, if it MUST be a macro (and I agree with everyone who says it should be function), you would do something like:
Code: [Select]
#define StepperInit do {                                \
   HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET);  \
   HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET);    \
   HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET);    \
   HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET);    \
  } while (0)
The "\" characters are line continuation, making the #define behave like a single long line, but easier to read.  The "do { } while (0)" surrounding the statements causes the whole multi-statement combination to behave like a single statement at the C level, so that things don't go badly when you use:
Code: [Select]
  if (buttonPressed())    StepperInit;
Your original code uses the "comma operator", which is an uncommon bit of C that is usually avoided.  I had to check to make sure that "(a,b)" is guaranteed to evaluate "a" first, and I would be nervous about "(a,b,c)" without testing!
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4857
  • Country: dk
Re: Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED]
« Reply #8 on: December 13, 2018, 03:22:08 am »
additionally I'd probably chance
#define StepperA         GPIO_PIN_9
HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET);
to
#define StepperA         GPIOB,GPIO_PIN_9
HAL_GPIO_WritePin(StepperA, GPIO_PIN_RESET);

to keep the pin mapping in one place
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf