diff --git a/solutions/04_system/silly/Makefile b/solutions/04_system/silly/Makefile index c37afab..b7be12c 100644 --- a/solutions/04_system/silly/Makefile +++ b/solutions/04_system/silly/Makefile @@ -1,19 +1,5 @@ -# Makefile for CMake project with intelligent configuration +EXE=app +SRCS=$(wildcard *.c) -# Default target -all: build/build.ninja - cmake --build build - -# Create build directory and generate build files if needed -build/build.ninja : CMakeLists.txt - cmake -S . -B build -G "Ninja" - -# Clean build directory -clean: - rm -rf build - -# Rebuild from scratch -rebuild: clean all - -# Phony targets (targets that don't represent files) -.PHONY: all clean rebuild \ No newline at end of file +# Include the standard application Makefile for the CSEL1 labs +include ./appl.mk diff --git a/src/03-led-controller/CMakeLists.txt b/src/03-led-controller/CMakeLists.txt index 9faf9c8..62abb23 100644 --- a/src/03-led-controller/CMakeLists.txt +++ b/src/03-led-controller/CMakeLists.txt @@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 3.28) project(led-controller) include(../nanopi.cmake) -add_executable(main main.c) +add_executable(led-controller main.c) diff --git a/src/03-led-controller/justfile b/src/03-led-controller/justfile new file mode 100644 index 0000000..b965e1c --- /dev/null +++ b/src/03-led-controller/justfile @@ -0,0 +1,5 @@ +build: + cmake -S . -B build && cmake --build build + +clean: + rm -rf build diff --git a/src/03-led-controller/main.c b/src/03-led-controller/main.c index c7b5246..c2bf45e 100644 --- a/src/03-led-controller/main.c +++ b/src/03-led-controller/main.c @@ -1,7 +1,102 @@ -#include +/** + * Copyright 2018 University of Applied Sciences Western Switzerland / Fribourg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Project: HEIA-FR / HES-SO MSE - MA-CSEL1 Laboratory + * + * Abstract: System programming - file system + * + * Purpose: NanoPi silly status led control system + * + * Autĥor: Daniel Gachet + * Date: 07.11.2018 + */ +#include +#include +#include +#include +#include +#include +#include +#include -int main() { - printf("Led controller"); +/* + * status led - gpioa.10 --> gpio10 + * power led - gpiol.10 --> gpio362 + */ +#define GPIO_EXPORT "/sys/class/gpio/export" +#define GPIO_UNEXPORT "/sys/class/gpio/unexport" +#define GPIO_LED "/sys/class/gpio/gpio10" +#define LED "10" + +static int open_led() +{ + // unexport pin out of sysfs (reinitialization) + int f = open(GPIO_UNEXPORT, O_WRONLY); + write(f, LED, strlen(LED)); + close(f); + + // export pin to sysfs + f = open(GPIO_EXPORT, O_WRONLY); + write(f, LED, strlen(LED)); + close(f); + + // config pin + f = open(GPIO_LED "/direction", O_WRONLY); + write(f, "out", 3); + close(f); + + // open gpio value attribute + f = open(GPIO_LED "/value", O_RDWR); + return f; +} + +int main(int argc, char* argv[]) +{ + long duty = 2; // % + long period = 1000; // ms + if (argc >= 2) period = atoi(argv[1]); + period *= 1000000; // in ns + + // compute duty period... + long p1 = period / 100 * duty; + long p2 = period - p1; + + int led = open_led(); + pwrite(led, "1", sizeof("1"), 0); + + struct timespec t1; + clock_gettime(CLOCK_MONOTONIC, &t1); + + int k = 0; + while (1) { + struct timespec t2; + clock_gettime(CLOCK_MONOTONIC, &t2); + + long delta = + (t2.tv_sec - t1.tv_sec) * 1000000000 + (t2.tv_nsec - t1.tv_nsec); + + int toggle = ((k == 0) && (delta >= p1)) | ((k == 1) && (delta >= p2)); + if (toggle) { + t1 = t2; + k = (k + 1) % 2; + if (k == 0) + pwrite(led, "1", sizeof("1"), 0); + else + pwrite(led, "0", sizeof("0"), 0); + } + } return 0; -} +} \ No newline at end of file diff --git a/src/appl.mk b/src/appl.mk new file mode 100644 index 0000000..667cb94 --- /dev/null +++ b/src/appl.mk @@ -0,0 +1,45 @@ +CFLAGS=-Wall -Wextra -g -c -O0 -MD -std=gnu11 +CFLAGS+=$(EXTRA_CFLAGS) + +TOOLCHAIN_PATH=/buildroot/output/host/usr/bin/ +TOOLCHAIN=$(TOOLCHAIN_PATH)aarch64-linux- +CFLAGS+=-mcpu=cortex-a53 -funwind-tables +##CFLAGS+=-O2 -fno-omit-frame-pointer +OBJDIR=.obj/nano +EXEC=$(EXE) + +ifeq ($(target),host) +EXEC=$(EXE)_h +endif + +CC=$(TOOLCHAIN)gcc +LD=$(TOOLCHAIN)gcc +AR=$(TOOLCHAIN)ar +STRIP=$(TOOLCHAIN)strip + +OBJDIR=.obj/$(target) +OBJS= $(addprefix $(OBJDIR)/, $(SRCS:.c=.o)) + +$(OBJDIR)/%o: %c + $(CC) $(CFLAGS) $< -o $@ + +all: $(OBJDIR)/ $(EXEC) + +$(EXEC): $(OBJS) $(LINKER_SCRIPT) + $(LD) $(OBJS) $(LDFLAGS) -o $@ + +$(OBJDIR)/: + mkdir -p $(OBJDIR) + +clean: + rm -Rf $(OBJDIR) $(EXEC) $(EXEC)_s *~ + +clean_all: clean + rm -Rf .obj $(EXE) $(EXE)_s $(EXE)_a $(EXE)_a_s $(EXE)_h $(EXE)_h_s + +-include $(OBJS:.o=.d) + +.PHONY: all clean clean_all + + +