| 3rdparty/libgpiod/build.sh | ●●●●● patch | view | raw | blame | history | |
| hal/api/leds.c | ●●●●● patch | view | raw | blame | history | |
| hal/api/leds.h | ●●●●● patch | view | raw | blame | history | |
| hal/api/libgpiod/build.sh | ●●●●● patch | view | raw | blame | history | |
| hal/api/makefile | ●●●●● patch | view | raw | blame | history |
3rdparty/libgpiod/build.sh
@@ -1,97 +1,186 @@ #!/bin/bash #+-------------------------------------------------------------------------------------------- #|Description: This shell script used download and compile libgpiod for ARM #| Author: GuoWenxue <guowenxue@gmail.com> #| ChangeLog: #| 1, Initialize 1.0.0 on 2011.04.12 #| #| Warnning: libgpiod configure depends on automake >= 1.14, use follow command to change it #| sudo update-alternatives --config automake #+-------------------------------------------------------------------------------------------- # library name and version # Official: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git LIB_NAME=libgpiod-2.0.1 PACK_SUFIX=tar.gz PRJ_PATH=`pwd` PREFIX_PATH=`pwd`/../install # LingYun source code FTP server LY_FTP=http://master.weike-iot.com:2211/src/ LYFTP_SRC=http://master.weike-iot.com:2211/src/ # library download URL address LIB_URL=$LY_FTP CROSSTOOL=/opt/gcc-aarch32-10.3-2021.07/bin/arm-none-linux-gnueabihf- # Cross compiler for cross compile on Linux server CROSS_COMPILE=arm-linux-gnueabihf- function msg_banner() { echo "" echo "+-----------------------------------------------------------------------" echo "| $1 " echo "+-----------------------------------------------------------------------" echo "" # compile jobs JOBS=`cat /proc/cpuinfo |grep "processor"|wc -l` # this project absolute path PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) # top project absolute path TOP_PATH=$(realpath $PRJ_PATH/..) # binaries install path PREFIX_PATH=$TOP_PATH/install BIN_PATH=$PREFIX_PATH/bin LIB_PATH=$PREFIX_PATH/lib INC_PATH=$PREFIX_PATH/include # check installed or not file INST_FILE=$PREFIX_PATH/lib/libgpiod.so # shell script will exit once get command error set -e #+-------------------------+ #| Shell script functions | #+-------------------------+ function pr_error() { echo -e "\033[40;31m $1 \033[0m" } function pr_warn() { echo -e "\033[40;33m $1 \033[0m" } function pr_info() { echo -e "\033[40;32m $1 \033[0m" } function check_result() { if [ $? != 0 ] ; then echo "" echo "+-----------------------------------------------------------------------" echo "| $1 " echo "+-----------------------------------------------------------------------" echo "" exit ; fi if [ $? != 0 ] ; then pr_error "$1" exit fi } function export_cross() # decompress a packet to destination path function do_unpack() { # export cross toolchain export CC=${CROSSTOOL}gcc export AS=${CROSSTOOL}as export AR=${CROSSTOOL}ar export LD=${CROSSTOOL}ld export NM=${CROSSTOOL}nm export RANLIB=${CROSSTOOL}ranlib export OBJDUMP=${CROSSTOOL}objdump export STRIP=${CROSSTOOL}strip tarball=$1 dstpath=`pwd` # export cross configure export CONFIG_CROSS=" --build=i686-pc-linux --host=arm-linux " if [[ $# == 2 ]] ; then dstpath=$2 fi # Clear LDFLAGS and CFLAGS export LDFLAGS= export CFLAGS= pr_info "decompress $tarball => $dstpath" mkdir -p $dstpath case $tarball in *.tar.gz) tar -xzf $tarball -C $dstpath ;; *.tar.bz2) tar -xjf $tarball -C $dstpath ;; *.tar.xz) tar -xJf $tarball -C $dstpath ;; *.tar.zst) tar -I zstd -xf $tarball -C $dstpath ;; *.tar) tar -xf $tarball -C $dstpath ;; *.zip) unzip -qo $tarball -d $dstpath ;; *) pr_error "decompress Unsupport packet: $tarball" return 1; ;; esac } function compile_libgpiod() function do_export() { SRC_NAME=libgpiod-2.0.1 PACK_SUFIX=tar.gz IMG_NAME=libgpiod.so.2.1.1 BUILD_ARCH=$(uname -m) if [[ $BUILD_ARCH =~ "arm" ]] ; then pr_warn "local($BUILD_ARCH) compile $LIB_NAME" return ; fi if [ -f ${PREFIX_PATH}/lib/${IMG_NAME} ] ; then msg_banner "$SRC_NAME already compile and installed" return 0; fi pr_warn "cross(${CROSS_COMPILE}) compile $LIB_NAME" msg_banner "Start cross compile $SRC_NAME " # export cross toolchain export CC=${CROSS_COMPILE}gcc export CXX=${CROSS_COMPILE}g++ export AS=${CROSS_COMPILE}as export AR=${CROSS_COMPILE}ar export LD=${CROSS_COMPILE}ld export NM=${CROSS_COMPILE}nm export RANLIB=${CROSS_COMPILE}ranlib export OBJDUMP=${CROSS_COMPILE}objdump export STRIP=${CROSS_COMPILE}strip if [ ! -f ${SRC_NAME}.${PACK_SUFIX} ] ; then #wget https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/snapshot/${SRC_NAME}.${PACK_SUFIX} wget $LYFTP_SRC/$SRC_NAME.$PACK_SUFIX check_result "ERROR: download ${SRC_NAME} failure" fi # export cross configure export CONFIG_CROSS=" --build=i686-pc-linux --host=arm-linux " tar -xzf ${SRC_NAME}.${PACK_SUFIX} cd ${SRC_NAME} ./autogen.sh echo "ac_cv_func_malloc_0_nonnull=yes" >arm-linux.cache ./configure --prefix=${PREFIX_PATH} ${CONFIG_CROSS} --cache-file=arm-linux.cache --enable-tools check_result "ERROR: configure ${SRC_NAME} failure" make && make install check_result "ERROR: compile ${SRC_NAME} failure" cd - # Clear LDFLAGS and CFLAGS export LDFLAGS= export CFLAGS= } export_cross function do_fetch() { if [ -e ${INST_FILE} ] ; then pr_warn "$LIB_NAME compile and installed alredy" exit ; fi compile_libgpiod if [ -d $LIB_NAME ] ; then pr_warn "$LIB_NAME fetch already" return ; fi if [ ! -f ${LIB_NAME}.${PACK_SUFIX} ] ; then wget ${LIB_URL}/${LIB_NAME}.${PACK_SUFIX} check_result "ERROR: download ${LIB_NAME} failure" fi do_unpack ${LIB_NAME}.${PACK_SUFIX} } function do_build() { cd $LIB_NAME ./autogen.sh do_export echo "ac_cv_func_malloc_0_nonnull=yes" > arm-linux.cache ./configure --prefix=${PREFIX_PATH} ${CONFIG_CROSS} --enable-static \ --cache-file=arm-linux.cache --enable-tools check_result "ERROR: configure ${LIB_NAME} failure" make -j ${JOBS} && make install check_result "ERROR: compile ${LIB_NAME} failure" } function do_clean() { rm -rf *${LIB_NAME}* } if [[ $# == 1 && $1 == -c ]] ;then pr_warn "start clean ${LIB_NAME}" do_clean exit; fi do_fetch do_build hal/api/leds.c
@@ -24,88 +24,18 @@ #include <time.h> #include <errno.h> #include <signal.h> #include <gpiod.h> #define DELAY 300 #define ON 1 #define OFF 0 /* Three LEDs number */ enum { LED_R = 0, LED_G, LED_B, LEDCNT, }; enum { ACTIVE_HIGH, /* High level will turn led on */ ACTIVE_LOW, /* Low level will turn led on */ }; /* Three LEDs hardware information */ typedef struct led_s { const char *name; /* RGB 3-color LED name */ int chip_num; /* RGB 3-color LED connect chip */ int gpio_num; /* RGB 3-color LED connect line */ int active; /* RGB 3-color LED active level */ struct gpiod_line_request *request; /* libgpiod gpio request handler */ } led_t; static led_t leds_info[LEDCNT] = { {"red", 0, 23, ACTIVE_HIGH, NULL }, /* GPIO1_IO23 on chip0 line 23, active high */ {"green", 4, 1, ACTIVE_HIGH, NULL }, /* GPIO5_IO01 on chip4 line 1, active high */ {"blue", 4, 8, ACTIVE_HIGH, NULL }, /* GPIO5_IO08 on chip4 line 8, active high */ }; /* Three LEDs API context */ typedef struct leds_s { led_t *leds; /* led pointer to leds_info */ int count; /* led count */ } leds_t; /* function declaration */ int init_led(leds_t *leds); int term_led(leds_t *leds); int turn_led(leds_t *leds, int which, int cmd); static inline void msleep(unsigned long ms); #include "leds.h" int g_stop = 0; void sig_handler(int signum) { switch( signum ) { case SIGINT: case SIGTERM: g_stop = 1; default: break; } return ; } static inline void msleep(unsigned long ms); void sig_handler(int signum); int main(int argc, char *argv[]) { int rv; leds_t leds = { .leds = leds_info, .count = LEDCNT, }; if( (rv=init_led(&leds)) < 0 ) if( (rv=init_led()) < 0 ) { printf("initial leds gpio failure, rv=%d\n", rv); return 1; @@ -117,55 +47,43 @@ while( !g_stop ) { turn_led(&leds, LED_R, ON); turn_led(LED_R, ON); msleep(DELAY); turn_led(&leds, LED_R, OFF); turn_led(LED_R, OFF); msleep(DELAY); turn_led(&leds, LED_G, ON); toggle_led(LED_G); msleep(DELAY); turn_led(&leds, LED_G, OFF); toggle_led(LED_G); msleep(DELAY); turn_led(&leds, LED_B, ON); msleep(DELAY); turn_led(&leds, LED_B, OFF); msleep(DELAY); blink_led(LED_B, DELAY); } term_led(&leds); term_led(); return 0; } int term_led(leds_t *leds) /*+---------------------------------+ *| Leds API based on libgpiod v2.0 | *+---------------------------------+*/ static led_t leds_info[LEDCNT] = { int i; led_t *led; {"red", 0, 23, ACTIVE_HIGH, 0, NULL}, /* GPIO1_IO23 on chip0 line 23, active high */ {"green", 4, 1, ACTIVE_HIGH, 0, NULL}, /* GPIO5_IO01 on chip4 line 1, active high */ {"blue", 4, 8, ACTIVE_HIGH, 0, NULL}, /* GPIO5_IO08 on chip4 line 8, active high */ }; printf("terminate RGB Led gpios\n"); /* Leds context */ static leds_t leds = { .leds = leds_info, .count = LEDCNT, }; if( !leds ) { printf("Invalid input arguments\n"); return -1; } for(i=0; i<leds->count; i++) { led = &leds->leds[i]; if( led->request ) { turn_led(leds, i, OFF); gpiod_line_request_release(led->request); } } return 0; } int init_led(leds_t *leds) int init_led(void) { led_t *led; int i, rv = 0; @@ -174,14 +92,6 @@ struct gpiod_line_settings *settings; /* gpio direction, bias, active_low, value */ struct gpiod_line_config *line_cfg; /* gpio line */ struct gpiod_request_config *req_cfg; /* gpio consumer, it can be NULL */ if( !leds ) { printf("Invalid input arguments\n"); return -1; } /* defined in libgpiod-2.0/lib/line-settings.c: @@ -239,9 +149,9 @@ goto cleanup; } for(i=0; i<leds->count; i++) for(i=0; i<leds.count; i++) { led = &leds->leds[i]; led = &leds.leds[i]; snprintf(chip_dev, sizeof(chip_dev), "/dev/gpiochip%d", led->chip_num); chip = gpiod_chip_open(chip_dev); @@ -275,7 +185,7 @@ cleanup: if( rv< 0 ) term_led(leds); term_led(); if( line_cfg ) gpiod_line_config_free(line_cfg); @@ -289,26 +199,78 @@ return rv; } int turn_led(leds_t *leds, int which, int cmd) int term_led(void) { int i; led_t *led; printf("terminate RGB Led gpios\n"); for(i=0; i<leds.count; i++) { led = &leds.leds[i]; if( led->request ) { turn_led(i, OFF); gpiod_line_request_release(led->request); } } return 0; } int turn_led(int which, int cmd) { led_t *led; int rv = 0; int value = 0; if( !leds || which<0 || which>=leds->count ) if( which<0 || which>=leds.count ) { printf("Invalid input arguments\n"); return -1; } led = &leds->leds[which]; led = &leds.leds[which]; value = OFF==cmd ? GPIOD_LINE_VALUE_INACTIVE : GPIOD_LINE_VALUE_ACTIVE; gpiod_line_request_set_value(led->request, led->gpio_num, value); led->status = OFF==cmd ? OFF : ON; return 0; } int toggle_led(int which) { led_t *led; if( which<0 || which>=leds.count ) { printf("Invalid input arguments\n"); return -1; } led = &leds.leds[which]; return turn_led(which, !led->status); } void blink_led(int which, int interval) { led_t *led; turn_led(which, ON); msleep(interval); turn_led(which, OFF); msleep(interval); } /*+-------------------------------+ *| Misc functions API | *+-------------------------------+*/ static inline void msleep(unsigned long ms) { @@ -330,3 +292,19 @@ return ; } void sig_handler(int signum) { switch( signum ) { case SIGINT: case SIGTERM: g_stop = 1; default: break; } return ; } hal/api/leds.h
New file @@ -0,0 +1,77 @@ /********************************************************************************* * Copyright: (C) 2024 LingYun IoT System Studio * All rights reserved. * * Filename: led.c * Description: This file is used to control RGB 3-colors LED * * * Pin connection: * RGB Led Module IGKBoard * R <-----> #Pin33 * G <-----> #Pin35 * B <-----> #Pin37 * GND <-----> GND * ********************************************************************************/ #ifndef _LEDS_H_ #define _LEDS_H_ #include "gpiod.h" #define DELAY 300 #define ON 1 #define OFF 0 /* Three LEDs number */ enum { LED_R = 0, LED_G, LED_B, LEDCNT, }; enum { ACTIVE_HIGH, /* High level will turn led on */ ACTIVE_LOW, /* Low level will turn led on */ }; /* Three LEDs hardware information */ typedef struct led_s { const char *name; /* RGB 3-color LED name */ int chip_num; /* RGB 3-color LED connect chip */ int gpio_num; /* RGB 3-color LED connect line */ int active; /* RGB 3-color LED active level */ int status; /* RGB 3-color LED current status */ struct gpiod_line_request *request; /* libgpiod gpio request handler */ } led_t; /* Three LEDs structure */ typedef struct leds_s { led_t *leds; /* led pointer to leds_info */ int count; /* led count */ } leds_t; /* initial leds */ int init_led(void); /* terminate leds */ int term_led(void); /* turn $which led ON/OFF */ int turn_led(int which, int cmd); /* toggle $which led status */ int toggle_led(int which); /* blink $which led status with $interval ms */ void blink_led(int which, int interval); #endif /* ----- #ifndef _LEDS_H_ ----- */ hal/api/libgpiod/build.sh
@@ -2,117 +2,185 @@ # library name and version # Official: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git LIB_NAME=libgpiod-2.0 LIB_NAME=libgpiod-2.0.1 PACK_SUFIX=tar.gz # LingYun source code FTP server LY_FTP=http://master.weike-iot.com:2211/src/ # library download URL address LIB_URL=$LY_FTP # Cross compiler for cross compile on Linux server CROSS_COMPILE=arm-linux-gnueabihf- # compile jobs JOBS=`cat /proc/cpuinfo |grep "processor"|wc -l` # this project absolute path PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) # top project absolute path TOP_PATH=$(realpath $PRJ_PATH/..) # binaries install path PREFIX_PATH=$PRJ_PATH/install PREFIX_PATH=$TOP_PATH/install BIN_PATH=$PREFIX_PATH/bin LIB_PATH=$PREFIX_PATH/lib INC_PATH=$PREFIX_PATH/include # check installed or not file INST_FILE=$PREFIX_PATH/lib/libgpiod.so # shell script will exit once get command error set -e #+-------------------------+ #| Shell script functions | #+-------------------------+ function pr_error() { echo -e "\033[40;31m $1 \033[0m" echo -e "\033[40;31m $1 \033[0m" } function pr_warn() { echo -e "\033[40;33m $1 \033[0m" echo -e "\033[40;33m $1 \033[0m" } function pr_info() { echo -e "\033[40;32m $1 \033[0m" echo -e "\033[40;32m $1 \033[0m" } function check_result() { if [ $? != 0 ] ; then pr_error $1 fi if [ $? != 0 ] ; then pr_error "$1" exit fi } # decompress a packet to destination path function do_unpack() { tarball=$1 dstpath=`pwd` if [[ $# == 2 ]] ; then dstpath=$2 fi pr_info "decompress $tarball => $dstpath" mkdir -p $dstpath case $tarball in *.tar.gz) tar -xzf $tarball -C $dstpath ;; *.tar.bz2) tar -xjf $tarball -C $dstpath ;; *.tar.xz) tar -xJf $tarball -C $dstpath ;; *.tar.zst) tar -I zstd -xf $tarball -C $dstpath ;; *.tar) tar -xf $tarball -C $dstpath ;; *.zip) unzip -qo $tarball -d $dstpath ;; *) pr_error "decompress Unsupport packet: $tarball" return 1; ;; esac } function do_export() { pr_warn "set cross(${CROSS_COMPILE})" BUILD_ARCH=$(uname -m) if [[ $BUILD_ARCH =~ "arm" ]] ; then pr_warn "local($BUILD_ARCH) compile $LIB_NAME" return ; fi # export cross toolchain export CC=${CROSS_COMPILE}gcc export CXX=${CROSS_COMPILE}g++ export AS=${CROSS_COMPILE}as export AR=${CROSS_COMPILE}ar export LD=${CROSS_COMPILE}ld export NM=${CROSS_COMPILE}nm export RANLIB=${CROSS_COMPILE}ranlib export OBJDUMP=${CROSS_COMPILE}objdump export STRIP=${CROSS_COMPILE}strip pr_warn "cross(${CROSS_COMPILE}) compile $LIB_NAME" # export cross configure export CONFIG_CROSS=" --build=i686-pc-linux --host=arm-linux " # export cross toolchain export CC=${CROSS_COMPILE}gcc export CXX=${CROSS_COMPILE}g++ export AS=${CROSS_COMPILE}as export AR=${CROSS_COMPILE}ar export LD=${CROSS_COMPILE}ld export NM=${CROSS_COMPILE}nm export RANLIB=${CROSS_COMPILE}ranlib export OBJDUMP=${CROSS_COMPILE}objdump export STRIP=${CROSS_COMPILE}strip # Clear LDFLAGS and CFLAGS export LDFLAGS= export CFLAGS= # export cross configure export CONFIG_CROSS=" --build=i686-pc-linux --host=arm-linux " # Clear LDFLAGS and CFLAGS export LDFLAGS= export CFLAGS= } function do_fetch() { if [ -e ${INST_FILE} ] ; then pr_warn "$LIB_NAME compile and installed alredy" exit ; fi if [ -e ${INST_FILE} ] ; then pr_warn "$LIB_NAME compile and installed alredy" exit ; fi if [ -d $LIB_NAME ] ; then pr_warn "$LIB_NAME fetch already" return ; fi if [ -d $LIB_NAME ] ; then pr_warn "$LIB_NAME fetch already" return ; fi if [ ! -f ${LIB_NAME}.${PACK_SUFIX} ] ; then wget https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/snapshot/${LIB_NAME}.${PACK_SUFIX} check_result "ERROR: download ${LIB_NAME} failure" fi if [ ! -f ${LIB_NAME}.${PACK_SUFIX} ] ; then wget ${LIB_URL}/${LIB_NAME}.${PACK_SUFIX} check_result "ERROR: download ${LIB_NAME} failure" fi pr_warn "$LIB_NAME download already, decompress it now" tar -xzf ${LIB_NAME}.${PACK_SUFIX} do_unpack ${LIB_NAME}.${PACK_SUFIX} } function do_build() { cd $LIB_NAME cd $LIB_NAME ./autogen.sh ./autogen.sh do_export do_export echo "ac_cv_func_malloc_0_nonnull=yes" > arm-linux.cache ./configure --prefix=${PREFIX_PATH} ${CONFIG_CROSS} --enable-static \ --cache-file=arm-linux.cache --enable-tools check_result "ERROR: configure ${LIB_NAME} failure" echo "ac_cv_func_malloc_0_nonnull=yes" > arm-linux.cache ./configure --prefix=${PREFIX_PATH} ${CONFIG_CROSS} --enable-static \ --cache-file=arm-linux.cache --enable-tools check_result "ERROR: configure ${LIB_NAME} failure" make -j ${JOBS} && make install check_result "ERROR: compile ${LIB_NAME} failure" make -j ${JOBS} && make install check_result "ERROR: compile ${LIB_NAME} failure" } function do_clean() { rm -rf ${LIB_NAME}* rm -rf install rm -rf *${LIB_NAME}* } if [[ $# == 1 && $1 == -c ]] ;then pr_warn "start clean $LIB_NAME" do_clean exit; pr_warn "start clean ${LIB_NAME}" do_clean exit; fi do_fetch do_build hal/api/makefile
@@ -27,9 +27,9 @@ BINARIES=$(SRCFILES:%.c=%) # libgpiod compile install path LIBGPIOD_PATH=libgpiod/install CFLAGS+=-I ${LIBGPIOD_PATH}/include LDFLAGS+=-L ${LIBGPIOD_PATH}/lib -lgpiod LIBS_PATH=install CFLAGS+=-I ${LIBS_PATH}/include LDFLAGS+=-L ${LIBS_PATH}/lib -lgpiod all: libs binaries install @@ -48,7 +48,8 @@ @rm -f *.o *.lo $(BINARIES) distclean: clean @rm -f tags cscope* @rm -f tags cscope* @make clean -C libgpiod @rm -rf install .PHONY: clean entry