The 40-pin header connector¶
On KNEO Pi board, you can find a 40-pin header. The pin headers have a 0.1in (2.54mm) pin pitch. The functions of the General Purpose I/O (GPIO) pins are predefined for KNEO Pi.
Info
In addition to functioning as GPIO input and output, these pins can be configured for alternate functions(SPI
/I2C
/I2S
/UART
/pwm
/etc.), which vary depending on the specific pin. Configuration methods are detailed in the standard SDK. For advanced configuration options, refer to the full-function SDK page.
Using GPIO¶
GPIO
stands for General-Purpose Input/Output. The sysfs interface allows users to interact with GPIO pins directly from the file system, making it a simple and effective way to control GPIOs without requiring additional libraries. Below is a guide on using GPIO with sysfs, along with the basic concepts you need to understand.
libgpiod
usage is also supported
Basic Concepts of GPIO¶
-
GPIO Direction:
A GPIO pin can be configured as either an input or an output.
- When set as
in
, the pin reads the signal levels (high or low) applied externally. - When set as
out
, the pin drives the signal levels to connected devices.
- When set as
-
GPIO Value:
A GPIO pin set as input will reflect the state of the connected signal. A GPIO pin set as output allows you to control its state, either high (
1
) or low (0
). -
GPIO Pin Numbering:
The sysfs interface uses the global GPIO number, which is defined by the GPIO numbering scheme of the hardware architecture and the Linux kernel. The pinout table provides the mapping between the physical pin number and its associated GPIO controller and line.
E.g.
The mapping for physical pin 40 is labeled as GPIO_2_IO_D[7], which is with the global GPIO number, 71.Understanding the Global GPIO Calculation
In
GPIO_2_IO_D[7]
, the pin is part of GPIO Controller-2 and represents the 7th line.The global GPIO number is calculated as:
32 * Controller Number + Line Number = 32 * 2 + 7 = 71 (noted as gpio71).
Using GPIO via Sysfs Interface¶
The sysfs GPIO interface is typically located at /sys/class/gpio/
. Follow these steps to interact with GPIO pins:
-
export the GPIO Pin
This makes the GPIO pin accessible in the sysfs interface. Replace
with the number of your desired GPIO pin. -
Set GPIO Direction
To set the pin as output:
To set the pin as input:
-
Read or Write GPIO Value
To write a value to the pin (when configured as output):
To read the pin value (when configured as input): 4. Unexport the GPIO Pin$ echo 1 > /sys/class/gpio/gpio<gpio_number>/value # Set pin high $ echo 0 > /sys/class/gpio/gpio<gpio_number>/value # Set pin low
When you are done using the GPIO, unexport it to release resources:
Two onboard GPIO-controlled LEDs
LED-green: gpio82
LED-blue: gpio83
Using PWM¶
Pulse Width Modulation (PWM) is a technique used to control the amount of power delivered to a device by varying the width of the pulses in a signal. It is commonly used for applications such as:
- Controlling the brightness of LEDs
- Adjusting the speed of motors
- Generating audio signals
In the KNEO Pi platform, GPIO pins(phyical pin 32, 33) are configured to function as PWM outputs.
Basic Concepts of PWM¶
- Duty Cycle: The proportion of time the signal is "high" in a single period. A 50% duty cycle means the signal is high for half the time and low for the other half.
- Frequency: The number of times the signal completes a full cycle (high + low) per second, measured in Hertz (Hz).
Using PWM via sysfs¶
The Linux kernel provides a sysfs interface for controlling PWM. Here’s how to use it:
-
Locate the PWM Chip and Channel
Each PWM controller is represented as a PWM chip in the
/sys/class/pwm
directory.
Channels are indexed under the chip. -
Export the PWM Channel
To enable a specific PWM channel, export it:
Here, 0 refers to the first PWM channel of pwmchip0.Info
KNEO Pi provides 2 channels.
If using
AGPO_D[0](pwm)
/phyical pin 32,echo 0 > /sys/class/pwm/pwmchip0/export
If usingAGPO_D[1](pwm)
/phyical pin 33,echo 1 > /sys/class/pwm/pwmchip0/export
-
Configure PWM Parameters
Navigate to the PWM channel directory:
Set the period (in nanoseconds):
Set the duty cycle (in nanoseconds):
Warning
The period and duty cycle values must be consistent, with the duty cycle not exceeding the period.
Enable the PWM signal:
-
Disable PWM
To stop the PWM signal, disable it:
If you no longer need the channel, unexport it:
Using UART¶
UART (Universal Asynchronous Receiver/Transmitter) is a hardware communication protocol used for serial communication. It enables two devices to exchange data over a serial connection, commonly used for debugging, connecting peripheral devices, or interfacing with external microcontrollers.
-
Connect the USB-to-TTL serial cable to your computer and the GPIO pin header KNEO Pi.
-
GND
at pin-6,TX
at pin-8 andRX
at pin-10
Install Driver for CP2102N USB-to-UART Chip on Windows
The USB-to-TTL serial cable included in the box features an embedded Silicon Labs CP2102N USB-to-UART chip. Follow these steps to install Virtual COM Port (VCP) driver for CP2102N on Windows:
-
Download the Driver
To enable the CP2102N to function as a standard COM port, you need to install the latest Virtual COM Port (VCP) drivers. You can download the most recent version from the official Silicon Labs website: CP210x USB-to-UART Bridge VCP Drivers
-
Connect the Device via USB
Connect the USB-to-TTL serial cable to your host PC.
-
Detect the Device
- If this is the first time connecting the CP2102N to your PC, it may appear as "CP2102N USB to UART Bridge Controller" in Device Manager.
- If the driver is not yet installed, install it by executing the unzipped downloaded file,
[unzipped folder]\CP210x_VCP_Windows\CP210xVCPInstaller_x64.exe
- Once the driver installation is complete, the CP2102N will be recognized as a COM port in Device Manager. Note: The assigned COM port number may vary depending on your system.
-
-
Use a console tool (e.g.
Minicom
andgtkterm
on Linux OS orHyperTerminal
andPuTTY
on Windows OS) to connect to the KNEO Pi -
Configure the console tool with the appropriate baud rate and settings
Option Configuration Serial Port Unix-like
: ttyUSB#
windows
: COM#Bits per Second 115200 Data Bits 8 bits Parity None Stop Bits 1 -
Once connected, you will have console access to control and manage the device, even without a network connection.