README.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. .. zephyr:code-sample:: blinky
  2. :name: Blinky
  3. :relevant-api: gpio_interface
  4. Blink an LED forever using the GPIO API.
  5. Overview
  6. ********
  7. The Blinky sample blinks an LED forever using the :ref:`GPIO API <gpio_api>`.
  8. The source code shows how to:
  9. #. Get a pin specification from the :ref:`devicetree <dt-guide>` as a
  10. :c:struct:`gpio_dt_spec`
  11. #. Configure the GPIO pin as an output
  12. #. Toggle the pin forever
  13. See :zephyr:code-sample:`pwm-blinky` for a similar sample that uses the PWM API instead.
  14. .. _blinky-sample-requirements:
  15. Requirements
  16. ************
  17. Your board must:
  18. #. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
  19. Zephyr's :ref:`boards`).
  20. #. Have the LED configured using the ``led0`` devicetree alias.
  21. Building and Running
  22. ********************
  23. Build and flash Blinky as follows, changing ``reel_board`` for your board:
  24. .. zephyr-app-commands::
  25. :zephyr-app: samples/basic/blinky
  26. :board: reel_board
  27. :goals: build flash
  28. :compact:
  29. After flashing, the LED starts to blink. If a runtime error occurs, the sample
  30. exits without printing to the console.
  31. Build errors
  32. ************
  33. You will see a build error at the source code line defining the ``struct
  34. gpio_dt_spec led`` variable if you try to build Blinky for an unsupported
  35. board.
  36. On GCC-based toolchains, the error looks like this:
  37. .. code-block:: none
  38. error: '__device_dts_ord_DT_N_ALIAS_led_P_gpios_IDX_0_PH_ORD' undeclared here (not in a function)
  39. Adding board support
  40. ********************
  41. To add support for your board, add something like this to your devicetree:
  42. .. code-block:: DTS
  43. / {
  44. aliases {
  45. led0 = &myled0;
  46. };
  47. leds {
  48. compatible = "gpio-leds";
  49. myled0: led_0 {
  50. gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
  51. };
  52. };
  53. };
  54. The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
  55. ``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
  56. the pin is set to its high state, and off when the pin is in its low state.
  57. Tips:
  58. - See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
  59. in devicetree.
  60. - If you're not sure what to do, check the devicetrees for supported boards which
  61. use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.
  62. - See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
  63. in devicetree.
  64. - If the LED is built in to your board hardware, the alias should be defined in
  65. your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
  66. define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.