1
0

fix!(lab03): correct initialisation of the lab silly led

BREAKING CHANGE: we now use the scope according to src / report
This commit is contained in:
2026-04-18 09:16:45 +00:00
parent a65c061642
commit a9c9d11521
5 changed files with 154 additions and 23 deletions

View File

@@ -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
# Include the standard application Makefile for the CSEL1 labs
include ./appl.mk

View File

@@ -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)

View File

@@ -0,0 +1,5 @@
build:
cmake -S . -B build && cmake --build build
clean:
rm -rf build

View File

@@ -1,7 +1,102 @@
#include <stdio.h>
/**
* 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 <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
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;
}
}

45
src/appl.mk Normal file
View File

@@ -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