Page 1 of 1

Sugestion regardin portable code between multiple HW's

Posted: Thu Nov 28, 2013 4:07 pm
by SvendStave
Hi, I have a suggestion to the PAL / board.chcfg.

I use my code on several different HW platforms though at this moment ony with STM32F4 cpu. But this is not something that is much specific to any particular cpu.

I had the issue when moving a few leds of my bootloader when I had to implement it on a different HW, the way board.h and pal.h is done the code is not portable even though the board.h configuration has been changed. I bet I am not the only one affected by this.

here is what I have done, and suggest for the future, it may not be the best solution, but it's work and is pretty clean.....

in the board.h I have added som port config lines like this:

Code: Select all

/*
 * IO pins assignments.
 */
#define GPIO0                   0
#define GPIO1                  1
#define GPIO2                  2
#define GPIO3                    3
#define GPIO4                    4
#define GPIO5                    5
#define GPIO6                6
#define GPIO7                7
#define GPIO8             8
#define GPIO9                 9
#define GPIO10               10
#define GPIO11                11
#define GPIO12                12
#define GPIO13                    13
#define GPIO14                    14
#define GPIO15                   15

.
.

Code: Select all

#define GPIO_input_0            ( ( (uint64_t) GPIOA << 4) + GPIO0 )
#define GPIO_PWR_OUT            ( ( (uint64_t) GPIOA << 4) + GPIO1 )
#define GPIO_Output_0            ( ( (uint64_t) GPIOA << 4) + GPIO2 )
#define GPIO_A_PIN3               ( ( (uint64_t) GPIOA << 4) + GPIO3 )
#define GPIO_DAC_1               ( ( (uint64_t) GPIOA << 4) + GPIO4 )
#define GPIO_SPI1_SCK            ( ( (uint64_t) GPIOA << 4) + GPIO5 )
#define GPIO_SPI1_MISO            ( ( (uint64_t) GPIOA << 4) + GPIO6 )
.
.
[i]and so forth for the rest of the GPIO's[/i]

.
.
the objective here is to have a single name for the signals attached to the GPIO's, which does not include any information in the name itself, and yest still carry both PIN_numnber and PORT_informatino in the value.

in the file pal.h I hav added a few macros to use instead of the original ones which just splits the above defines into the norman port, pad information.

Code: Select all

[b]#define palWritePP(portpad) palWritePad((GPIO_TypeDef *)((uint64_t) portpad >> 4),((uint64_t) portpad & 0x0F), bit)[/b]
#if !defined(pal_lld_writepad) || defined(__DOXYGEN__)
#define palWritePad(port, pad, bit)                                         \
  palWritePort(port, (palReadLatch(port) & ~PAL_PORT_BIT(pad)) |            \
                     (((bit) & 1) << pad))
#else
#define palWritePad(port, pad, bit) pal_lld_writepad(port, pad, bit)
#endif

[b]#define palSetPadPP(portpad) palSetPad((GPIO_TypeDef *)((uint64_t) portpad >> 4),((uint64_t) portpad & 0x0F))
#define palClearPadPP(portpad) palClearPad((GPIO_TypeDef *)((uint64_t) portpad >> 4),((uint64_t) portpad & 0x0F))
#define palTogglePadPP(portpad) palTogglePad((GPIO_TypeDef *)((uint64_t) portpad >> 4),((uint64_t) portpad & 0x0F))
#define palSetPadModePP(portpad) palSetPadMode((GPIO_TypeDef *)((uint64_t) portpad >> 4),((uint64_t) portpad & 0x0F), mode)[/b]


and so instead of writing the code like this:

Code: Select all

palClearPad(GPIOB, GPIOB_LED_RX_YELLOW);


I write :

Code: Select all

palClearPadPP(GPIO_LED_RX_YELLOW);


and the code is portable without any changes, all changes now lie in board.h

first I hope this is usefull to someone else, and second I hope this (or similar) will come into the ChibiOS code.

enjoy
Svend Stave

Re: Sugestion regardin portable code between multiple HW's

Posted: Thu Nov 28, 2013 4:30 pm
by Giovanni
Hi,

I understand your point, it is not a bad idea. The problem would be the functions working at port level or bus level, the combined identifiers could not be used for those.

Anyway, your approach is usable on top of the current API by defining helper macros like you did.

Giovanni

Re: Sugestion regardin portable code between multiple HW's

Posted: Thu Nov 28, 2013 10:00 pm
by SvendStave
Thanks Giovanni,

I like ChibiOS alot and use ChibiStudio - (I also have a paid version of different make, also Eclipse based.... not using that anymore )

Is it possible to get the extra definitions generated by the xml rocessor? (i'd do it if I can..)

I changed pal.h again, made it a bit nicer, I added 2 macros to handle the value extraction, to be included when needed.
I beleive it should allow port/bus level just from the pin label - the normal way requires the user to read the GPIOB from GPIOB_someName.
anyway - it does cause some warnings due to extending the variable size from 32 to 64 bit during the calculation, but maybe they can be avoided somehow..

Whats your opinion about including this syntax in the code?

Code: Select all

/**
 * @brief
 * @details This macro calculates the pinnumber within a portpad.
 *
 * @param[in] portpad   combined port and pin
 * @return              pin.
 */
#if !defined(palPad) || defined(__DOXYGEN__)
#define palPad(portpad) ((uint64_t) portpad & 0x0F)
#endif

/**
 * @brief
 * @details This macro calculates the port within a portpad.
 *
 * @param[in] portpad   combined port and pin
 * @return              port.
 */
#if !defined(palPort) || defined(__DOXYGEN__)
#define palPort(portpad) ((GPIO_TypeDef *)((uint64_t) portpad >> 4))
#endif

Re: Sugestion regardin portable code between multiple HW's

Posted: Sun Dec 01, 2013 9:49 pm
by SvendStave
short update..
the original board.h additions created some warnings, so GPIO definitions changing from


Code: Select all

#define GPIO_LED_TX_RED             ( ( (uint64_t)GPIOB << 4) + GPIO14 )
#define GPIO_LED_PWR_GREEN          ( ( (uint64_t) GPIOB << 4) + GPIO15 )


to

Code: Select all

#define GPIO_LED_TX_RED              ( (uint64_t)((uint32_t) GPIOB << 4) + GPIO14 )
#define GPIO_LED_PWR_GREEN           ( (uint64_t)((uint32_t) GPIOB << 4) + GPIO15 )


removes the warnings

/Svend

Re: Sugestion regardin portable code between multiple HW's

Posted: Mon Dec 02, 2013 10:37 am
by SvendStave
correction!

WRONG! looses MSB

Code: Select all

#define GPIO_LED_TX_RED              ( (uint64_t)((uint32_t) GPIOB << 4) + GPIO14 )
#define GPIO_LED_PWR_GREEN           ( (uint64_t)((uint32_t) GPIOB << 4) + GPIO15 )


correct:

Code: Select all

#define GPIO_LED_TX_RED              ( ((uint64_t)(uint32_t) GPIOB << 4) + GPIO14 )
#define GPIO_LED_PWR_GREEN           ( ((uint64_t)(uint32_t) GPIOB << 4) + GPIO15 )


/Svend

Re: Sugestion regardin portable code between multiple HW's

Posted: Mon Dec 02, 2013 11:01 am
by Giovanni
Hi Svend,

I am not too enthusiastic about adding 64bits arithmetic. I think another solution could be implemented, example:

Code: Select all

#define GPIO_LED_TX_RED               GPIOB,14
#define GPIO_LED_PWR_GREEN           GPIOB,15


This way the new definitions can be used on the current API without changes, the definition would contain both parameters without having to use 64 bits arithmethic. For example:

Code: Select all

palClearPad(GPIO_LED_TX_RED);


In addition it would be useful to introdice two new macros:

Code: Select all

#define PAL_PORT(port, pad) (port)
#define PAL_PAD(port,pad) (pad)


The macros would allow to extract the port/pad parts from the definitions.

Anyway, such changes will have to wait for ChibiOS 3.0 when a new development pass or the various device drivers will be perfomed. In ChibiOS 2.6.x I don't want to introduce compatibility-breaking changes.

Giovanni

Re: Sugestion regardin portable code between multiple HW's

Posted: Mon Dec 02, 2013 4:33 pm
by SvendStave
Thank you Giovanni,

I'm just happy that you will implement in such a way, that code can be hw-independant.
I like your way of doing it, much better than my own... obviously I'm no expert :)

regards
Svend

Re: Sugestion regardin portable code between multiple HW's

Posted: Mon Dec 02, 2013 11:09 pm
by SvendStave
Hi again...

Although it looked straight forward it wasn't :/

the code

Code: Select all

#define GPIO_LED_TX_RED               GPIOB,14
#define GPIO_LED_PWR_GREEN           GPIOB,15

is OK, the macro substitution is not, it still need an extra invokation to make the 'single' argument expand into two for it to work as expected.
So unfortunately it still breaks compatability.
pal.h:

Code: Select all

#define palSetPad(portpad) palSetPad1(portpad)
#if !defined(pal_lld_setpad) || defined(__DOXYGEN__)
#define palSetPad1(port, pad) palSetPort(port, PAL_PORT_BIT(pad))
#else
#define palSetPad1(port, pad) pal_lld_setpad(port, pad)
#endif


but, it's the better solution which I'll use until 3.0 (when I must)

Regards
Svend