Create Project
Create New Project - Simple LED Blink and Print Serial Message
Let's create a new custom project.
1. Create Folder and Files
Let's create project named "mySooniLED" at C:\ncs\workspace\mySooniLED
You need 3 files for your first project.
mySooniLED\ (project folder)
src\main.c
CMakeLists.txt
prj.conf
So, It should look like this.


2. Source Codes
This is a simple led blink example codes.
2.1 src/main.c
We get gpio device and control directly instead of alias name such as led-0
#include <zephyr.h>
#include <zephyr/types.h>
#include <sys/printk.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <stdint.h>
#include <stdbool.h>
#define LED_HEAD_PIN 7
#define LED_RESET_PIN 6
#define LED_BOOT_PIN 5
void main(void)
{
struct device *dev;
int ret;
int count = 0;
printk("mySooniLED start\n");
// get gpio0 device
dev = device_get_binding("GPIO_0");
if(dev == NULL)
{
printk("device_get_binding:error, GPIO_0\n");
return;
}
printk("device_get_binding:ok, GPIO_0\n");
// config led pin
ret = gpio_pin_configure(dev, LED_HEAD_PIN, GPIO_OUTPUT_HIGH);
if(ret != 0)
{
printk("gpio_pin_configure:error, %d\n", ret);
return;
}
ret = gpio_pin_configure(dev, LED_RESET_PIN, GPIO_OUTPUT_HIGH);
if(ret != 0)
{
printk("gpio_pin_configure:error, %d\n", ret);
return;
}
ret = gpio_pin_configure(dev, LED_BOOT_PIN, GPIO_OUTPUT_HIGH);
if(ret != 0)
{
printk("gpio_pin_configure:error, %d\n", ret);
return;
}
printk("gpio_pin_configure:ok\n");
// blink and printk
while(true)
{
count++;
printk("count=%d, time=%u\n", count, k_uptime_get_32());
gpio_pin_set_raw(dev, LED_HEAD_PIN, 0); // turn on
gpio_pin_set_raw(dev, LED_RESET_PIN, 1); // turn off
gpio_pin_set_raw(dev, LED_BOOT_PIN, 1); // turn off
k_msleep(200);
gpio_pin_set_raw(dev, LED_HEAD_PIN, 1); // turn off
gpio_pin_set_raw(dev, LED_RESET_PIN, 0); // turn on
gpio_pin_set_raw(dev, LED_BOOT_PIN, 1); // turn off
k_msleep(200);
gpio_pin_set_raw(dev, LED_HEAD_PIN, 1); // turn off
gpio_pin_set_raw(dev, LED_RESET_PIN, 1); // turn off
gpio_pin_set_raw(dev, LED_BOOT_PIN, 0); // turn on
k_msleep(200);
}
}
2.2 CMakeLists.txt
#
# Copyright (c) 2020 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mySooniLED)
# SOURCE FILES
target_sources(app PRIVATE src/main.c)
2.3 prj.conf
#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
# General config
CONFIG_NEWLIB_LIBC=y
CONFIG_HW_STACK_PROTECTION=y
CONFIG_SERIAL=y
# GPIO
CONFIG_GPIO=y
# Heap and stacks
CONFIG_HEAP_MEM_POOL_SIZE=2048
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
3. Open as Segger Project
3.1 Open Segger IDE
3.3 Select Project and Board
Select "C:\ncs\workspace\mySooniLED" project folder by use "..." button.
Check '3rd party kits'.
Select 'soonisys_sldev_nrf9160ns'.
OK.

3.4 Build and Run
Last updated