To set all free pins as analog via Arduino IDE
To set all free pins as analog via Arduino IDE
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.
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.
Re: To set all free pins as analog via Arduino IDE
all pins can be set as input, not analog input.
Re: To set all free pins as analog via Arduino IDE
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.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);
}
Re: To set all free pins as analog via Arduino IDE
To sum up, the STM32 core is based on HAL/LL so you can call them at sketch level. 

Re: To set all free pins as analog via Arduino IDE
normally it is just
u'd need to check which are the adc pins. I'd guess adc 1 is normally 'just there'
Code: Select all
void setup() {
pinMode(PAxx, INPUT_ANALOG);
}
void loop() {
uint16_t value = analogRead(PAxx);
}
Re: To set all free pins as analog via Arduino IDE
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()?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.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);
}
Re: To set all free pins as analog via Arduino IDE
Not sure, if my name is shown correctly or question is addressed to me, anyway, yea CubeMX generates correct peripheral initialization code,mebab wrote: Wed May 26, 2021 1:04 pmThanks, 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()?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.
though you always can use CubeMX as a reference.
Re: To set all free pins as analog via Arduino IDE
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).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()?
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?.
Re: To set all free pins as analog via Arduino IDE
Thanks
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:
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
again for the useful link and comments!ag123
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();
....
Thanks again
Last edited by mebab on Thu May 27, 2021 8:41 pm, edited 2 times in total.
Re: To set all free pins as analog via Arduino IDE
How do you measure the power consumption ?