Nabin-Flash320 / rpi-pico-blink-led

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BLINK LED EXAMPLE FOR RASPBERRY-PI PICO

CMAKE EXPLAINED

  1. Build function for pico sdk is included first.
   include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
  1. Then path to FreeRTOSConfig.h and freertos source directory is provided.
   set(FREERTOS_CFG_DIRECTORY "<path to FreeRTOSConfig.h>")
   set(FREERTOS_SRC_DIRECTORY "<path to freertos source directory>")
  1. Initialize pico sdk. This will create a pico-sdk subdirectory in our project for the libraries.
   pico_sdk_init()
  1. Add freeRTOS as a library.
   add_library(FreeRTOS STATIC
      ${FREERTOS_SRC_DIRECTORY}/event_groups.c
      ${FREERTOS_SRC_DIRECTORY}/list.c
      ${FREERTOS_SRC_DIRECTORY}/queue.c
      ${FREERTOS_SRC_DIRECTORY}/stream_buffer.c
      ${FREERTOS_SRC_DIRECTORY}/tasks.c
      ${FREERTOS_SRC_DIRECTORY}/timers.c
      ${FREERTOS_SRC_DIRECTORY}/portable/MemMang/heap_3.c
      ${FREERTOS_SRC_DIRECTORY}/portable/GCC/ARM_CM0/port.c
   )
  1. Build freeRTOS
   target_include_directories(FreeRTOS PUBLIC
      ${FREERTOS_CFG_DIRECTORY}/
      ${FREERTOS_SRC_DIRECTORY}/include
      ${FREERTOS_SRC_DIRECTORY}/portable/GCC/ARM_CM0
   )
  1. Since .uf2 file is used rather than binary file, all (map/bin/hex/uf2) files should be generated.
   pico_add_extra_outputs(${PROJECT_NAME})
  1. Link pico_stdlib and freeRTOS to the project.
   target_link_libraries(${PROJECT_NAME}
            pico_stdlib
            FreeRTOS
   )
  1. To monitor through serial port disable UART communication and enable USB communication for the given project as below.
   pico_enable_stdio_usb(${PROJECT_NAME} 1)
   pico_enable_stdio_uart(${PROJECT_NAME} 0)

CODE EXPLAINED

  1. Include standard library for RPI-pico and stdio.(including freertos is not mandatory for this project so can be skipped.)
   #include "pico/stdlib.h"
   #include "FreeRTOS.h"
   #include "stdio.h"
  1. Initialize stdio functionalities for RPI-pico
   stdio_init_all();
  1. Select the pin for LED output. In this example default onboard led is used.
   const uint LED_PIN = PICO_DEFAULT_LED_PIN;
  1. Initialize the GPIO for I/O operation and set the GPIO direction as output.
   gpio_init(LED_PIN);
   gpio_set_dir(LED_PIN, GPIO_OUT);
  1. Within an infinite while loop, the gpio are toogled between on and off.
   while (true) {
      gpio_put(LED_PIN, 1);
      sleep_ms(250);
      printf("ON\n");
      gpio_put(LED_PIN, 0);
      sleep_ms(250);
      printf("OFF\n");
   }

COMPILING AND FLASHING

  1. Create a build direcotry in the project folder and cd into it.
   mkdir build && cd build
  1. Execute cmake and run make command
   cmake ..
   make
  1. After that connect the RPI-pico to the computer.
    • If it is very first flashing direction connection to the computer will work.
    • If another firmware is already running, push BOOTSEL button and then plug the USB to the computer.
  2. The RPI-pico is conneced to the computer as a drive named RPI-RP2. Copy the .uf2 file within in the build directory and paste it in RPI-RP2. Then the RPI-pico will automatically reboot and LED starts blinking.
  3. For monitoring through serial interface, any program or software that has serial monitro functionality will work. For linux following command will work as well:
   cat /dev/<port name it RPI-pico is connected>

THANK YOU

About


Languages

Language:CMake 77.6%Language:C 22.4%