Table of Contents

VIM1S WiringPi

Introduction

WiringPi is a C++ library for Raspberry Pi, we port it to Khadas SBC, With this library you can use many of the functionalities provided by the GPIO header: digital pins, SPI, I2C, UART, etc.

WiringPi Instructions

There are two methods here, via command line or C program.

Get Pin Info

Run gpio readall, and it will print a table about the status of all GPIO pins.

GPIO –> GPIO native number
wPi –> WiringPi number
Mode –> GPIO Mode ,ALT mean that this pin defined as a special function
V –> 1 - HIGH, 0 - LOW
PU/PD –> PU - pull up, PD - pull down, DSBLD - disabled

Using via Command Line

Here’s an example of controlling wpi number 1.

  • Set GPIO mode to output
$ gpio mode 1 out
  • Set output low level
$ gpio write 1 0
  • Set output high level
$ gpio write 1 1

Using via C Program

The test program changes the level value every 5S.

wiringpi.c
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
/*
 * Copyright (c) 2022 Wesion Technology Co., Ltd.
 *
 * Author: Yan <yan-wyb@foxmail.com> 
 *
 */
 
#include <stdio.h>
#include <wiringPi.h>
 
const int gpio_pin = 1;
 
int main()
{
    if(-1 == wiringPiSetup()){
        printf("set up error");
        exit(1);
    }
 
    pinMode(gpio_pin,OUTPUT);
 
    while(1){
        digitalWrite(gpio_pin,HIGH);
        printf("wPi Pin %d now is GIGH\n",gpio_pin);
        delay(5000);
        digitalWrite(gpio_pin,LOW);
        printf("wPi Pin %d now is LOW\n",gpio_pin);
        delay(5000);
    }
 
    exit(0);
}

Compile

$ gcc -o wiringpi wiringpi.c -lwiringPi -lpthread -lrt -lm -lcrypt

Run

sudo ./wiringpi
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOW
wPi Pin 1 now is GIGH
wPi Pin 1 now is LOW

You can use gpio read 1 to observe the pin level changes.

Special Pin Functions

Special pin functions of wiringpi include SPI,I2C,ADC,SoftPWM. Please refer to the 40-Pin Header for the corresponding physical pins.

2022/06/30 00:03 · nick