hal macro PAL_MODE_ALTERNATE

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

hal macro PAL_MODE_ALTERNATE

Post by alex31 »

Hello.

This is my first post, so i will began to thanks the authors of chibios, which seems, for my beginners eyes,
a fantastic, well designed, and orthogonal project to simplfy / permit the use of the body-builded mcu that we can afford now.

After having using pic32/freertos, i try chibios/stm32f4, for educational purpose, and also
to use it in a future version of an uav autopilot.

I use 2.5.0 git version of chibios, a stm32f4 discovery board wired with sdcard reader in sdio mode,
and have merged the discovery demo and Barthess sdio code to log sensors and adc data to the sdcard.

It works now, but i don't understand the use of the PAL_MODE_ALTERNATE macro.
I need to use

Code: Select all

palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));

in order to have serial console on usart2, but standard location of usart2 is already
on PA2,3. So i think that i don't have understand PAL_MODE_ALTERNATE use
(the parameter is a pin number, or a function ?, if it's a function why it is not named by a macro ?)

I have hit another problem with dma channel conflict, would it be possible to have a preprocessor based system which can
statically detect conflicts ?

I don't know if it's possible to verify that just with preprocessor, or should i write
a perl script which could detect such conflicts by parsing mcuconf.h, with that script called by the Makefile.

Thanks for any help.

ps: i have read thread about the need of a freertos like version of the documentation,
of course it's a lot of work, but it would be a major help for chibios beginers :-)

Alexandre
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: hal macro PAL_MODE_ALTERNATE

Post by Giovanni »

Hi Alexandre,

In the STM32F4 each pin can have up to 16 alternate functions, that macro does two things:
- Programs the pin as alternate instead of GPIO
- Selects the alternate function number

The table of alternate functions is in the Data Sheet (note, not the Reference Manual), see section "Alternate function mapping", table 7.

About DMAs, the HAL allows DMA sharing so conflict cannot be detected at compile time, you can have two drivers sharing the same DMAs, you just have to take care to not use them at the same time.
For example you could use the same DMA for an SPI and an ADC, just take care to start one driver at time:

Code: Select all

  adcStart();
  /* Work with ADC.*/
  adcStop();
  spiStart();
  /* Work with SPI.*/
  spiStop();


Stopping drivers when not needed also has the advantage to improve power usage.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: hal macro PAL_MODE_ALTERNATE

Post by alex31 »

many thanks for theses useful answers.

I have seen the table, wouldn't be useful to have macro in the stm32f4 port layer to name the alternate function ?
the work should be not to hard since a function has always the same alternate number for all the port/pin combination.


Alexandre
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: hal macro PAL_MODE_ALTERNATE

Post by Giovanni »

Yes, it could be useful but it could make some think that all functions can be used on any pin without any error generated.
The solution would be to define the whole triplet like:

#define GPIOC_7_USART6_RX GPIOC,7,PAL_MODE_ALTERNATE(8)

The problem is that would require hundreds definitions for each platform.

Giovanni
Post Reply