cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO with stm32l0 discovery

nashimoto
Associate II
Posted on November 02, 2015 at 12:11

Hi everyone,

I'm a newbie of embedded programming and this is my first post. I have a STM32L0 Discovery board and wanted to supply electricity to an external circuit. The external circuit has only a LED and a resistor. I referred to official samples (stm32cubel0 and stm32snippetsl0) and tried it. But it didn't work. This is my partial code.

void
Configure_GPIO(
void
)
{
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Pull = GPIO_NOPULL;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void
main(
void
)
{
// ---- initialization ----
...
...
Configure_GPIO();
// my operations: make an external LED blink
while
(1) 
/* Infinite loop */
{
volatile
int
i=0;
// PA1-4, 6: turn on
GPIOA->BSRR = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6;
for
(i=0;i<900000;i++); 
// wait
// PA1-4, 6: turn off
GPIOA->BRR = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6;
for
(i=0;i<1800000;i++); 
// wait
} 
}

I connected PA1 (or PA2, 3,...) and GND line to the external circuit. I found that only PA4 could supply electricity and made the LED blink, but others could not do in spite of the same configuration. Also, I checked the flag status of GPIO (such as GPIOA->MODE) and PA1-4, 6 were same configuration except IDR and ODR. What should I do next? Would you help me? #stm32l0-stm32l0-discovery #stm32l053
4 REPLIES 4
AvaTar
Lead
Posted on November 02, 2015 at 14:39

Perhaps because PA0 .. PA3 are already used otherwise on the discovery board (PA0 for the user button, PA1 for wake-up, PA2 + PA3 for the touchsensor) ?

Download the

http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1848/PF260319?sc=stm32l0-discovery

and check pages 25ff, or the schematics. The onboard components connected to this ports clash with your intended usage.

nashimoto
Associate II
Posted on November 03, 2015 at 07:11

Hi AvaTar,

Thank you for your kind reply.

I checked the user manual and found that only PA4 (among PA0..6) is ''Free I/O.''

I misunderstood the word ''general'' from GPIO.

I thought it meant that I could use GPIO pins freely for Input or Output.

So, for example, when I want to use a pin for a trigger of an external interruption (EXTI), I should select a Free I/O pin and make it into input mode by HAL_GPIO_Init(), right?

Also I misunderstood that the HAL_GPIO_Init() could overwrite the default function of these pins.

But, are there any ways to overwrite default functions of such non free I/O pins?

AvaTar
Lead
Posted on November 03, 2015 at 09:09

I misunderstood the word ''general'' from GPIO.

 

I thought it meant that I could use GPIO pins freely for Input or Output.

 

I think you not really misunderstood.

In theory, you can use any GPIO for such in/output purposes. Only when you aquire a board, you need to check which pins are assigned to a fixed purpose by the design.

If you buy a board a board for a specific purpose, check not only if the MCU has the required capability (i.e. peripheral), but also if the pins are actually available on that board.

Some of the latest F4xx discovery/Nucleo boards can serve as a ''negative'' example. These board often feature LCD, ext. RAM an Flash, which binds lots of pins to the FSMC functionality.

Also I misunderstood that the HAL_GPIO_Init() could overwrite the default function of these pins.

 

Usually, the MCU starts up with most peripherals turned off.

Just turning them on would activate the default functionality, defined by the startup values of the configuration registers (check the reference manual/datasheet for this values). This is rarely what you want, so in most cases you need to reconfigure those pins to your intended purpose.

But, are there any ways to overwrite default functions of such non free I/O pins?

 

If you have some external circuitry attached to the pin (like on the discovery board), you can either use it in the intended way, or remove that circuitry to fit it to you own purposes - by desoldering, cutting PCB tracks, or sometimes by solder bridges.

Or, you can check if the MCU routes similiar functionality to other, free pins.

So, for example, when I want to use a pin for a trigger of an external interruption (EXTI), I should select a Free I/O pin and make it into input mode by HAL_GPIO_Init(), right?

 

I think the Cube software provides example EXTI, too, so have a look at these.

(I stick with the ''old'' SPL, which had plenty of such examples).

nashimoto
Associate II
Posted on November 03, 2015 at 09:31

Hi AvaTar,

Thank you for your quick reply.

Your valuable advice enhanced my embedded programming skills.