samples/basic/blinky


Tristen Ennemuist
 

cheers lawrence i've been tinkering and i can confirm the following will behave as i originally expected, all four leds are initially off and then cycling through 'em in turn. i'm in the right universe after all, thank you!

gpio_pin_configure(green_led, DT_ALIAS_LED0_GPIOS_PIN, GPIO_OUTPUT_INACTIVE | GPIO_ACTIVE_LOW);
gpio_pin_configure(orange_led, DT_ALIAS_LED1_GPIOS_PIN, GPIO_OUTPUT_INACTIVE | GPIO_ACTIVE_LOW);
gpio_pin_configure(red_led, DT_ALIAS_LED2_GPIOS_PIN, GPIO_OUTPUT_INACTIVE | GPIO_ACTIVE_LOW);
gpio_pin_configure(blue_led, DT_ALIAS_LED3_GPIOS_PIN, GPIO_OUTPUT_INACTIVE | GPIO_ACTIVE_LOW);

u32_t counter = 0;

while (1)
{
counter++;

if (counter == 1)
{
printk("green on\n");

gpio_pin_set(green_led, DT_ALIAS_LED0_GPIOS_PIN, 1U);
gpio_pin_set(orange_led, DT_ALIAS_LED1_GPIOS_PIN, 0U);
gpio_pin_set(red_led, DT_ALIAS_LED2_GPIOS_PIN, 0U);
gpio_pin_set(blue_led, DT_ALIAS_LED3_GPIOS_PIN, 0U);
}

if (counter == 2)
{
printk("orange on\n");

gpio_pin_set(green_led, DT_ALIAS_LED0_GPIOS_PIN, 0U);
gpio_pin_set(orange_led, DT_ALIAS_LED1_GPIOS_PIN, 1U);
gpio_pin_set(red_led, DT_ALIAS_LED2_GPIOS_PIN, 0U);
gpio_pin_set(blue_led, DT_ALIAS_LED3_GPIOS_PIN, 0U);
}

if (counter == 3)
{
printk("red on\n");

gpio_pin_set(green_led, DT_ALIAS_LED0_GPIOS_PIN, 0U);
gpio_pin_set(orange_led, DT_ALIAS_LED1_GPIOS_PIN, 0U);
gpio_pin_set(red_led, DT_ALIAS_LED2_GPIOS_PIN, 1U);
gpio_pin_set(blue_led, DT_ALIAS_LED3_GPIOS_PIN, 0U);
}

if (counter == 4)
{
printk("blue on\n");

gpio_pin_set(green_led, DT_ALIAS_LED0_GPIOS_PIN, 0U);
gpio_pin_set(orange_led, DT_ALIAS_LED1_GPIOS_PIN, 0U);
gpio_pin_set(red_led, DT_ALIAS_LED2_GPIOS_PIN, 0U);
gpio_pin_set(blue_led, DT_ALIAS_LED3_GPIOS_PIN, 1U);

counter = 0;
}

k_sleep(1000);
}


Lawrence King
 

Hi Patrice:

 

Your statement that the behavior of the LED is ‘inverted’ is completely correct.

 

Your statement about the current is at best confusing. It is true that the GPIO doesn’t need to ‘source’ the current to turn the LED, but the GPIO does need to ‘sink’ the current. The amount of current flowing either into or out of the GPIO will be same no matter how the LED and resistor are connected. The reason we electrical engineers always choose to have the GPIO ‘sink’ the current is purely historical.

 

In the dark ages when we used +5V TTL logic (example 74LS04), the outputs of a logic gate could only source a small amount of current typically 6mA, and could sink a larger amount of current, typically 18mA. The LEDs available at the time needed at least 10mA to light up (and even at 10mA were not very bright), hence we were forced to use the sink capabilities of the gate to connect a LED to an output.

 

For no good reason we have continued with this convention, current generation LEDs are very bright with only 2mA of current, and current logic gates are symmetric drive which can source or sink 10mA or even 20mA. There is no good reason to have the logic ‘inverted’ other than historical convention.

 

I haven’t tried out all of the features of the new GPIO functions yet, but I think you can setup a GPIO to be “GPIO_ACTIVE_LOW” when calling gpio_pin_configure() and then drive the pin to GPIO_OUTPUT_ACTIVE when calling gpio_pin_set(), I believe this should turn the LED on.

 

Lawrence King

Principal Developer

+1(416)627-7302

 

From: users@... <users@...> On Behalf Of patrice.gerin via lists.zephyrproject.org
Sent: Monday, May 4, 2020 3:41 AM
To: users@...
Subject: Re: [Zephyr-users] samples/basic/blinky

 

Hellor Tristen,

The behavior of your LED seems normal!
In embedded design, LED are usually and voluntary inverted. Doing this will avoid the GPIO pin to provide the current by itself, the LED current will be supplied by the power directly.
                led       resistor
VCC |------|>|-----^v^v^v----------< GPIO |
If GPIO is set => no current
if GPIO is unset => current from VCC to the GPIO, no current provided by the stm32!
The LED is "active low".
Patrice


Tristen Ennemuist
 

ah, thank you patrice - and now i've gained an understanding of the reason as well, much appreciated.  


Tristen Ennemuist
 

heya nik thank you for responding, thankfully the schematics are available for download so i'll take a look and see what i can learn.

whew i've done plenty of programming on linux but i've never had to understand anything at this level, so thank you again for taking the time to help me out.


patrice.gerin@...
 

Hellor Tristen,

The behavior of your LED seems normal!
In embedded design, LED are usually and voluntary inverted. Doing this will avoid the GPIO pin to provide the current by itself, the LED current will be supplied by the power directly.
                led       resistor
VCC |------|>|-----^v^v^v----------< GPIO |
If GPIO is set => no current
if GPIO is unset => current from VCC to the GPIO, no current provided by the stm32!
The LED is "active low".
Patrice


Tristen Ennemuist
 

hello, i am [slowly] working my way through the examples, and i am a bit confused with the blinky sample project.

it seems that when 0 (zero) is written using gpio_pin_set then the LED on my board lights up and when 1 (one) is written the LED turns off. this is the opposite of what i was expecting,

however i am completely new to embedded development so i must apologise in embarrassment if it is my expectation that is the opposite!

ok, with the code:

i increased SLEEP_TIME_MS to 10000 so i could be sure there wasn't a timing issue on the output and i added the if/printk block to the while loop, and sure enough writing a zero results in led on and writing a one results in led off.

ie: the led is unlit and "led is now on", the led is lit and "led is now off".

my board is the stm32f469i_disco and i'm developing on fedora, below is the modified while loop, any feedback/advice would be much appreciated, thank you.

 while (1) {
gpio_pin_set(dev, PIN, (int)led_is_on);

if (led_is_on)
{
printk("led is now on\n");
}
else
{
printk("led is now off\n");
}

led_is_on = !led_is_on;
k_sleep(SLEEP_TIME_MS);
}