To set all free pins as analog via Arduino IDE

Post here all questions related to STM32 core if you can't find a relevant section!
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

To set all free pins as analog via Arduino IDE

Post by mebab »

Hi
For STM32 based micros, it is simple with CubeIDE to set all free pins as analog. How to do this via Arduino IDE?

Thanks for your guidance.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: To set all free pins as analog via Arduino IDE

Post by dannyf »

all pins can be set as input, not analog input.
User avatar
MasterT
Posts: 33
Joined: Mon Apr 20, 2020 12:02 am

Re: To set all free pins as analog via Arduino IDE

Post by MasterT »

Something like this
void cfg_Pins1(void)
{
GPIO_InitTypeDef GPIO_InitStruct = { 0, 0, 0, 0, 0};

__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();

//DAC:
/**DAC1 GPIO Configuration
PA4 ------> DAC1_OUT1
PA5 ------> DAC1_OUT2
*/
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}
, called ones from setup(). And certanly, you should stuck all GPIO_PIN_0 |..........| GPIO_PIN_15, and repeat for each A, B, C...G ports, depends on MCU. Check programming reference manual how many GPIO ports its has.
User avatar
fpiSTM
Posts: 1746
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: To set all free pins as analog via Arduino IDE

Post by fpiSTM »

To sum up, the STM32 core is based on HAL/LL so you can call them at sketch level. ;)
ag123
Posts: 1657
Joined: Thu Dec 19, 2019 5:30 am
Answers: 25

Re: To set all free pins as analog via Arduino IDE

Post by ag123 »

normally it is just

Code: Select all

void setup() {
	pinMode(PAxx, INPUT_ANALOG);
}

void loop() {
	uint16_t value = analogRead(PAxx);
}
u'd need to check which are the adc pins. I'd guess adc 1 is normally 'just there'
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: To set all free pins as analog via Arduino IDE

Post by mebab »

MasterT wrote: Wed May 26, 2021 12:57 am Something like this
void cfg_Pins1(void)
{
GPIO_InitTypeDef GPIO_InitStruct = { 0, 0, 0, 0, 0};

__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();

//DAC:
/**DAC1 GPIO Configuration
PA4 ------> DAC1_OUT1
PA5 ------> DAC1_OUT2
*/
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}
, called ones from setup(). And certanly, you should stuck all GPIO_PIN_0 |..........| GPIO_PIN_15, and repeat for each A, B, C...G ports, depends on MCU. Check programming reference manual how many GPIO ports its has.
Thanks, ag123. I am doing this to reduce the consumption power of STM32. I try to implement it. Is it the same as what CubeIDE generates in the MX_GPIO_Init()?
User avatar
MasterT
Posts: 33
Joined: Mon Apr 20, 2020 12:02 am

Re: To set all free pins as analog via Arduino IDE

Post by MasterT »

mebab wrote: Wed May 26, 2021 1:04 pm
MasterT wrote: Wed May 26, 2021 12:57 am Something like this
///
, called ones from setup(). And certanly, you should stuck all GPIO_PIN_0 |..........| GPIO_PIN_15, and repeat for each A, B, C...G ports, depends on MCU. Check programming reference manual how many GPIO ports its has.
Thanks, ag123. I am doing this to reduce the consumption power of STM32. I try to implement it. Is it the same as what CubeIDE generates in the MX_GPIO_Init()?
Not sure, if my name is shown correctly or question is addressed to me, anyway, yea CubeMX generates correct peripheral initialization code,
though you always can use CubeMX as a reference.
ag123
Posts: 1657
Joined: Thu Dec 19, 2019 5:30 am
Answers: 25

Re: To set all free pins as analog via Arduino IDE

Post by ag123 »

mebab wrote: Wed May 26, 2021 1:04 pm Thanks, ag123. I am doing this to reduce the consumption power of STM32. I try to implement it. Is it the same as what CubeIDE generates in the MX_GPIO_Init()?
oops, here is the 'catch' normally the arduino core tries to clock the popular buses (e.g. gpio) and peripherals (e.g. adc, uart, timers, spi, i2c etc).
That is because most 'newbies' gets 'harassed' when all they are trying to do is to 'blink a led'.

read that 'clock the peripherals' guide here:
http://www.emcu.it/STM32/STM32VLDiscove ... brary.html

to save power, you can either look into the core, and cross reference the ref manual for your soc, in the board (i.e. variant) files and see where the clocks are turned on, you could edit your local copy.
or figure out how to turn off the clocks. that you can do in setup(). once those clocks are turned off power reduces, but the gotcha is if you digitalWrite( pin, HIGH) and it doesn't work, check your clocks settings, did you turn it off?.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: To set all free pins as analog via Arduino IDE

Post by mebab »

Thanks
ag123
again for the useful link and comments!
Whatever I do with pins, clock frequency, etc. doesn't reduce the consumed power! I still have 6 mA (supplying 3.3 V with ST-Link) only for running STM32L476 in the full load situation!
It is hard for me to transfer whatever is needed from the following generated code in CubeIDE to Arduino code:

Code: Select all

  
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_I2C3_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  ....
  
I have included the contents of the SystemClock_Config in the Variant.cpp file. However, I cannot figure out how to deactivate peripherals in Arduino IDE. Would you mind please provide me a piece of Arduino code to disable for example I2C1 or SPI2?

Thanks again
Last edited by mebab on Thu May 27, 2021 8:41 pm, edited 2 times in total.
User avatar
fpiSTM
Posts: 1746
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: To set all free pins as analog via Arduino IDE

Post by fpiSTM »

How do you measure the power consumption ?
Post Reply

Return to “General discussion”