Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
short IO commands
#1
Hi all,
I am a poor typist, never learnt to touch type, therefore I always try to use the shortest commands.
For the STM32F103, I use the following macros in the .h file:
#define Varfromcomb(x, y) x
#define Bitfromcomb(x, y) y                      page numbers refer to RM0008 Reference manual
#define sbit(comb)  Varfromcomb(comb)->BSRR = 1 << Bitfromcomb(comb)//p172 Port Set/Reset Register
#define cbit(comb)  Varfromcomb(comb)->BRR  = 1 << Bitfromcomb(comb)//p173 Port Reset Register
#define rbit(comb)  Varfromcomb(comb)->IDR &  1 << Bitfromcomb(comb)//p171 Port input data register
Then the IO ports get specified               
#define Led     GPIOC,13 //on-board LED
#define jaws    GPIOB, 9 etc.

Now to set a bit: sbit(Led);
Clear a bit: cbit(jaws);
Read a bit: rbit(jaws);
Where bits are manipulated a lot in a program, this saves a lot of typing, and gives the shortest assembler output.

Hoping this can be of use to somebody.
Reply
#2
@smitjs,

I like these ideas. Do you have similar solutions in case the pin is not known at compilation time? For example, if you are doing a library for generic communication interface. In that case the port number and bit number are parameters for the initialization code.
Cheers, Ollie
Reply
#3
Hi Ollie,
Never had to do that, but certainly the macros can be included in the library.
Then it is only necessary to add the #defines for the I/O port bits when known. 
Should not be a problem.
Cheers
Reply
#4
Clever. Good work. Direct register manipulation is the best.

To extend your idea could not ...

#define obit(comb)  Varfromcomb(comb)->MODER |= (  1 << ( Bitfromcomb(comb )  << 1 ) )

#define ibit(comb)  Varfromcomb(comb)->MODER &= ~(  3 << ( Bitfromcomb(comb )  << 1 ) )

... be about to initially open a bit for output? The ibit() macro can be used to first clear whatever was in MODER.

Note: This is based on STM32F407 knowledge, I'm not sure if your 103 is similar.

Regards, Workalot 
Reply
#5
(04-07-2017, 08:40 AM)Workalot Wrote: #define obit(comb)  Varfromcomb(comb)->MODER |= (  1 << ( Bitfromcomb(comb )  << 1 ) )

#define ibit(comb)  Varfromcomb(comb)->MODER &= ~(  3 << ( Bitfromcomb(comb )  << 1 ) )

.Note: This is based on STM32F407 knowledge, I'm not sure if your 103 is similar.

Regards, Workalot 

Sure that would work. I do not know the F407, but I assume the registers are similar to the F103.
Whatever works is good.
Regards
Johan Smit
Reply
#6
(14-12-2016, 12:23 PM)smitjs Wrote: Hi all,
...
Hoping this can be of use to somebody.

It certainly is to me! Thanks, pretty clever!
-E
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)