Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPIO SIM #46

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ OreSat Linux image builder and utilities.
- **Image Builder:** Used to build OreSat Linux images.
- **Octavo USB eMMC Flasher:** Used to flash a OreSat Linux card's eMMC over USB.
- **Octavo EEPROM Flasher:** Used to flash a new OreSat Linux card's EEPROM.
- **GPIO-SIM:** Used to setup simulated GPIO on development machines.

## OreSat Linux Images

Expand Down
29 changes: 29 additions & 0 deletions gpio_sim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# GPIO-SIM
For working with Simulated GPIOs

## Setup

### Build and install the linux Kernel from source
- Debian
```bash
./kernel_build_debian.sh
```
### Add udev rules giving user permissions for GPIO
```bash
./gpio_rules.sh
```
## USE

### Add simulated GPIO chips and lanes
- Parses from the device-tree
- Must be run on each boot, consider adding to ~/.bashrc
```bash
./setup_sims.sh
```
### remove simulated GPIO chips
```bash
./clean_sims.sh
```



16 changes: 16 additions & 0 deletions gpio_sim/clean_sims.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0 OR MIT

# A helper to remove any orphaned gpio-sims from the system.
# This should only be necessary if a test was killed abnormally
# preventing it from cleaning up the sims it created, or if you
# created a sim using basic_sim.sh.

SIMDIR="/sys/kernel/config/gpio-sim"
find $SIMDIR -type d -name hog -exec rmdir '{}' '+'
find $SIMDIR -type d -name "line*" -exec rmdir '{}' '+'
find $SIMDIR -type d -name "bank*" -exec rmdir '{}' '+'
rmdir $SIMDIR/*

9 changes: 9 additions & 0 deletions gpio_sim/gpiod_rules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
sudo groupadd gpiod
sudo usermod -aG gpiod $USER

# This gives user permissions for all gpio.
# Might want to change it to specific pins?

echo '# udev rules for gpio port access through libgpiod
SUBSYSTEM=="gpio", KERNEL=="gpiochip[0-4]", GROUP="gpiod", MODE="0660"' | sudo tee /etc/udev/rules.d/60-gpiod.rules
54 changes: 54 additions & 0 deletions gpio_sim/kernel_build_debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# For Debian
# Build and install GPIO-SIM kernel module
# Main reference https://www.debian.org/doc/manuals/debian-kernel-handbook/ch-common-tasks.html

# Install prerequisites
sudo apt-get update
sudo apt-get -y install dpkg-dev
sudo apt-get -y install build-essential fakeroot
sudo apt-get -y build-dep linux

# Get linux source
sudo apt-get -y install linux-source
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid apt-get in scripts. A better place would be in the README

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll move them out later today.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved all apt-get commands to the README 43fb2a9

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a different kernel from what we use. May not matter at long as it is a 5.10 release?

Depends how this is used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to be run on development machines to simulate the C3 GPIO. Unfortunately we can't have matching kernel version to the real C3 because the GPIO-SIM module requires at least version 5.19. So I had it grab the latest kernel instead.


# Get the directory of the sourced script
SCRIPT_DIR=$(dirname "$(realpath "$0")")

# Make the build directory
BUILD_DIR=$SCRIPT_DIR/Kernel_Build
mkdir -p $BUILD_DIR

cd /usr/src
# Find the latest version of the downloaded source
SOURCE=$(ls | grep linux-source | sort -V | tail -n 1)

# Extract the source
tar xaf /usr/src/$SOURCE -C $BUILD_DIR

# Get the version number (X.Y) from the source name
VERSION=${SOURCE#linux-source-}
VERSION=${VERSION%.tar.bz2}
VERSION=${VERSION%.tar.xz}

# Move into the source directory in the build directory
cd $BUILD_DIR/linux-source-$VERSION

# Build the Kernel
MAKEFLAGS="-j$(nproc) LOCALVERSION=-gpio-sim CONFIG_GPIO_SIM=y"
make clean
make oldconfig # Combine this Kernel's configuration with the current running Kernel
make deb-pkg $MAKEFLAGS

# Move to the parent directory where the .deb files are located
cd ..

# Confirm before installing the kernel packages
read -p "Are you sure you want to install the kernel packages? (y/n) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
sudo dpkg -i linux-image*.deb linux-headers*.deb
echo "Restart your machine to have the new build-in modules load on boot"
fi
58 changes: 58 additions & 0 deletions gpio_sim/setup_sims.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

# Creates GPIO-SIM entries that match the device-tree configuration
# described in FILE

# Get the directory of the sourced script
script_dir=$(dirname "${BASH_SOURCE[0]}")

# The device tree file to parse
FILE=$script_dir/../device_trees/oresat-c3-0601.dts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is running on an OreSat card, you can read from /boot/dtbs/<kernel_verion>/am335x-pocketbeagle.dtbo (it is symbolically linked to correct device tree) rather than hard-coding this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this will be running on a development machine rather than the C3 it needs to grab the dts file.


mountpoint /sys/kernel/config > /dev/null || mount -t configfs configfs /sys/kernel/config

# Initialize banks
# 4 banks 32 gpio each
dir=/sys/kernel/config/gpio-sim/basic
for i in {0..3}
do
mkdir -p $dir/bank$i
echo 32 > $dir/bank$i/num_lines
done

# Read the device tree file line by line
while IFS= read -r line
do
# Check if the line contains "gpio-name"
if [[ $line == *"gpio-name"* ]]; then
# Extract the gpio-name
gpio_name=$(echo $line | cut -d'"' -f 2)
echo "GPIO Name: $gpio_name"

read -r line
# Extract the gpiochip number and lane number
gpiochip_number=$(echo $line | cut -d' ' -f 3 | tr -dc '0-9')
lane_number=$(echo $line | cut -d' ' -f 4)
echo "GPIOCHIP Number: $gpiochip_number"
echo "Lane Number: $lane_number"

mkdir -p $dir/bank$gpiochip_number/line$lane_number
echo "$gpio_name" > $dir/bank$gpiochip_number/line$lane_number/name

# not setting direction becauase gpio-sim does not allow setting an initial direction
# Only when "hogging" the lane by the kernel it can set as input, output-high, and output-low

# read -r line
## Extract the direction
# direction=$(echo $line | tr -d '[:space:]' | sed 's/;$//')
# echo "Direction: $direction"

#mkdir -p dir/bank$gpiochip_number/line$lane_number/hog/
#echo "hog-name-example" > $dir/bank$gpiochip_number/line$lane_number/hog/name
#echo "$direction" > $dir/bank$gpiochip_number/line$lane_number/hog/direction
#
fi

done < "$FILE"

echo 1 > $dir/live