Let's create a new custom project.
You need 3 files for your first project.
So, It should look like this.
This is a simple led blink example codes.
#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);
}
}
#
# 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)
#
# 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