Difference between revisions of "Raspberry"

From Random Stuff
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 156: Line 156:
  
 
= Control GPIO’s =
 
= Control GPIO’s =
 +
 +
Outputing a signal
  
 
  # Export pin to userspace
 
  # Export pin to userspace
Line 168: Line 170:
 
  # Set pin 18 to low
 
  # Set pin 18 to low
 
  echo "0" > /sys/class/gpio/gpio18/value
 
  echo "0" > /sys/class/gpio/gpio18/value
 +
 +
----------------------------------
 +
 +
Reading a signal
 +
 +
# Export pin to userspace
 +
echo "23" > /sys/class/gpio/export
 +
 +
# Set pin 23 as an input
 +
echo "in" > /sys/class/gpio/gpio23/direction
 +
 +
# Set interrupt on rising edge for pin 23
 +
echo "rising" > /sys/class/gpio/gpio23/edge
 +
 +
# Read the state of pin 23
 +
cat /sys/class/gpio/gpio23/value
 +
 +
# Invert the value
 +
echo "1" > /sys/class/gpio/gpio23/active_low
  
 
= Putting it all together =
 
= Putting it all together =

Latest revision as of 22:43, 28 July 2021

Setup environment

Assuming a debian distribution, install the following dependencies:

 sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib \
     build-essential chrpath socat cpio python3 python3-pip python3-pexpect \
     xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev \
     pylint3 xterm



Fetching the sources

Create a new project directory in your workspace folder. For example in ~/workspace

# Create the new project structure:
mkdir -p raspberry/sources


# Enter the sources folder
cd raspberry/sources


# And clone the following repositories
git clone git://git.yoctoproject.org/poky -b hardknott
git clone https://git.openembedded.org/meta-openembedded -b hardknott
git clone git://git.yoctoproject.org/meta-raspberrypi -b hardknott



Configuring the project

# Move back to the project folder and source the environment
cd -
. sources/poky/oe-init-build-env


Notice the you are now in a new build directory. ie. ~/workspace/raspberry/build, and some new configuration files have been added in a conf folder.

# Edit the bblayer.conf
gedit conf/bblayers.conf

and make sure it now has the following content for BBLAYERS:

# Make sure the following variable has the following layers set:
BBLAYERS ?= " \
  ${TOPDIR}/../sources/poky/meta \
  ${TOPDIR}/../sources/poky/meta-poky \
  ${TOPDIR}/../sources/poky/meta-yocto-bsp \
  ${TOPDIR}/../sources/meta-raspberrypi \
  ${TOPDIR}/../sources/meta-openembedded/meta-oe \
  ${TOPDIR}/../sources/meta-openembedded/meta-multimedia \
  ${TOPDIR}/../sources/meta-openembedded/meta-networking \
  ${TOPDIR}/../sources/meta-openembedded/meta-python \
"
# Now we will edit the second configuration file and we will make sure the MACHINE is set to our target board.
gedit conf/local.conf
# Make sure the following machine is set:
MACHINE ?= "raspberrypi4"


Building an image

To build an image we will run:

bitbake core-image-full-cmdline

'Note': A rough way to get an idea of what other images you may build would be to run from the sources folder:

find . -wholename *images/*.bb

However for the moment core-image-full-cmdline satisfies our needs.


Flashing the image onto the sdcard

To find the image go from your build folder in: tmp/deploy/images/raspberrypi4/

If we’ve build core-image-full-cmdline you should see here a file named: core-image-full-cmdline.wic.bz2


# Extract the file
bzip2 -df core-image-full-cmdline.wic.bz2

Now you should see that core-image-full-cmdline.wic.bz2 dissapeared and there is a new file named: core-image-full-cmdline.wic


Insert the sd card in the pc and check the device to which it corresponds using:

lsblk -p


If you are unsure. Unplug the sdcard, run the command again check which device dissapears/appears

Possible sample output:

NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
/dev/loop0         7:0    0 215.4M  1 loop /snap/rpi-imager/170
/dev/loop1         7:1    0  99.2M  1 loop /snap/core/10958
/dev/loop2         7:2    0  61.7M  1 loop /snap/core20/975
/dev/loop3         7:3    0  61.7M  1 loop /snap/core20/1026
/dev/loop4         7:4    0  99.4M  1 loop /snap/core/11316
/dev/loop5         7:5    0 196.5M  1 loop /snap/rpi-imager/184
/dev/sda           8:0    0 111.8G  0 disk 
├─/dev/sda1        8:1    0  37.3G  0 part /
├─/dev/sda2        8:2    0     1K  0 part 
├─/dev/sda5        8:5    0   3.7G  0 part [SWAP]
└─/dev/sda6        8:6    0  70.8G  0 part /home
/dev/sdb           8:16   1 698.7G  0 disk 
├─/dev/sdb1        8:17   1   200G  0 part /media/nicu/6E56907E5690492B1
├─/dev/sdb2        8:18   1   200G  0 part /media/nicu/Local Disk3
├─/dev/sdb3        8:19   1   201G  0 part /media/nicu/Local Disk2
├─/dev/sdb4        8:20   1     1K  0 part 
├─/dev/sdb5        8:21   1   4.6G  0 part 
└─/dev/sdb6        8:22   1  93.1G  0 part /media/nicu/DebianPart3
/dev/mmcblk0     179:0    0    30G  0 disk 
├─/dev/mmcblk0p1 179:1    0  50.3M  0 part /media/nicu/boot
└─/dev/mmcblk0p2 179:2    0 192.4M  0 part /media/nicu/root


In the above example the sdcard is: /dev/mmcblk0 and it’s a 32 GB card. It has two partitions: mmcblk0p1 and mmcblk0p2.

Now that we have identified the image and the device, we will have to write the image.

Don’t proceed if you are unsure you have the right device, as you might erase the data on your laptop. The data on the sdcard will be erased.

So if all is ok run the following command:

dd if=<raspberry image> of=<device> bs=4M conv=fsync

so in my case:

dd if=core-image-full-cmdline.wic of=/dev/mmcblk0 bs=4M conv=fsync

The process shall take around 1 minute, wait until it finishes.

Once done plug the sdcard in the raspberry and power it on.


Raspberry pinout

Raspberry pi4 pinout


Basic led

There are two types of rgb leds: with common cathod or common anode. The longest pin is usually the common one.


Led pins



Control GPIO’s

Outputing a signal

# Export pin to userspace
echo "18" > /sys/class/gpio/export 
# Set pin 18 as an output
echo "out" > /sys/class/gpio/gpio18/direction
# Set pin 18 to high
echo "1" > /sys/class/gpio/gpio18/value
# Set pin 18 to low
echo "0" > /sys/class/gpio/gpio18/value

Reading a signal

# Export pin to userspace
echo "23" > /sys/class/gpio/export 
# Set pin 23 as an input
echo "in" > /sys/class/gpio/gpio23/direction
# Set interrupt on rising edge for pin 23
echo "rising" > /sys/class/gpio/gpio23/edge
# Read the state of pin 23
cat /sys/class/gpio/gpio23/value
# Invert the value
echo "1" > /sys/class/gpio/gpio23/active_low

Putting it all together

For my case I chose pin 18(the 6th pin on the right side). this pin will be connected to the anod (longest pin of the led) and I will connect any of the other pins to a ground(ex. 3rd pin on the right side).

When the pin is high the led should light up. When it’s low, it should shut down.


Connect to the device with a patch cord cable, and use a command like nmap to find the address of the device:

ex. nmap -sP 192.168.10.0/24

Connect to the device through ssh:

ex. ssh root@192.168.10.151

One led connection


Playing with layers and recipes

Lazy way to set a static ip for a yocto image

We could edit the files we've fetched directly:

For example in `sources/poky/meta/recipes-core/init-ifupdown/init-ifupdown-1.0/interfaces`

and replace:

iface eth0 inet dhcp

with:

iface eth0 inet static
  address 192.168.10.2
  netmask 255.255.255.0
  network 192.168.10.0
  gateway 192.168.10.1

It's lazy because we are editing the actual source files that we do not own, so if we want to switch to a different version of poky(maybe because it supports a new kernel or has support for some special apps), we would have to fetch the new version wich does not have our changes so we would need to reapply them manually, and that is not pleasant.


A better way

A smarter way is to create a new layer, that would normally just enhance some existing recipes.

A layer is basically a collection of recipes, and is applied over existing layers based on a priority. (kind of a short description ... )

A recipe is a set of rules by which a component is fetched(could be from git), built and finally packaged and installed on the image.

Creating a layer

As an example, lets create a new layer called `meta-aurora`.


Assuming we've sourced our environment `. sources/poky/oe-init-build-env` and we are in the build folder, we'll simply run the command:

bitbake-layers create-layer --priority 25 ../sources/meta-aurora

This layer shall have an assigned priority that is higher than the poky, openembedded and raspberry layers, because we want to add new feature to recipes in those layers(`applied over`).


After running the command a new folder should have been crated in `../sources/meta-aurora` with a sample recipe. Within a layer folder we have a file `/conf/layer.conf` which gives us hints of the priority of the layer and where it loads recipes from(`BBFILES`)

BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
            ${LAYERDIR}/recipes-*/*/*.bbappend"

Normally, we might just want to change the behavior of an existing component, by adding a custom configuration file. For this we would normally create a *.bbappend file that simply adds some additional steps to build a recipe.

Alternatively, for the static ip change we could also create a git *.patch file that would be applied when configuring the component. More on that later .... much later.


Let's say we want to add a dhcp server. A dhcp server basically has some rules by which it can assign an ip to devices that connect to it. In our case, we'll be connecting the PC to the raspberryPi directly through an ethernet cable without a router in between. So, we want the PC to be assigned an IP address in the same subnet as the raspberry.

A recipe that provides a dhcp server in the `hardknott` version of poky is `kea`. To add it to the image we would need to update our local.conf file from `build/conf/local.conf` and simply append to it:

 IMAGE_INSTALL_append = " kea"

Now if we were to build the image `bitbake core-image-full-cmdline` we would have a dhcp server running with some default configuration, assigning IP addresses in a range that we might not like, so we would like to create a custom configuration file. => we enhance the recipe adding a bbappend file.


First let's add our layer to the image search path by updating build/conf/bblayers.conf: conf/bblayers.conf and add to the end of BBLAYERS

 ${TOPDIR}/../sources/meta-aurora \

Note: We could use a command to do this like: "bitbake-layers add-layer ../sources/meta-aurora", but it adds an absolute path and we won't want that in the future.

Creating our first recipe

Next, in `sources/meta-aurora` we'll create a new folder `recipes-conectivity` in which we'll add yet another folder which has the same name as the recipe we want to enhance. ie. kea (we've chosen these names because kea may be found in `sources/poky/meta/recipes-connectivity/kea`, however that's not mandatory. What's important is the name of the recipe - *bb or *bbappend) In this folder we would create a file named: kea_%.bbappend

The % indicates a wildcard indicating that this bbappend file applies to kea for whatever version. If we'd want the bbappend to apply only to a particular version we'd be more explicit. For example: `kea_1.8.2.bbappend`. But since we're adding a config file, it's likely it will work even on future versions of the component. Worst case, in a degraded mode or without some features active.

Sidenote: you could have multiple versions of a recipe, in your environment in different layers. You could explicitly specify the one which you want to pick, or leave yocto to choose the latest one. (too much info)

Anyway, we would add to the `kea_%.bbappend` file the following content:

FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:" 

SRC_URI += "file://kea-dhcp4.conf" 

do_install_append() { 
    install -m 644 ${WORKDIR}/kea-dhcp4.conf ${D}${sysconfdir}/kea/kea-dhcp4.conf
}
FILESEXTRAPATHS will indicate that we want to add some extra paths to the location in yocto is looking for files that we might reference later.
_prepend indicates that we want to add the path at the begining of the list in which it searches for files. We want to add it at the begining because once it finds a file, it stops the search. ... and the search is in the order of: FILESEXTRAPATHS
${THISDIR} refers to the directory of the recipe
${PN} refers to the project name, which is the name of the bb/bbappend file, so 'kea'. We could also have ${PV} which would be project version. etc.
the ':' at the end is required because the paths in FILESEXTRAPATHS are separated by ':'
${THISDIR}/${PN} would refer in our case to `sources/meta-aurora/recipes-connectivity/kea/kea/`
SRC_URI indicates a file we want copied to the current working directory. (will be referenced later)
"file://" indicates we're referencing a file from FILESEXTRAPATHS. but that could just as well be git:// if we want yocto to fetch the sources from an external repository
do_install refers to what happens in the build step. We have an additional `_append` here because we don't want to overwrite the recipes existing do_install script, we just want to add some additional steps.
install -m 644 ${WORKDIR}/kea-dhcp4.conf ${D}${sysconfdir}/kea/kea-dhcp4.conf => basically copy from the working directory the file we've indicated through SRC_URI to '${sysconfdir}' which on the image coresponds to the `/etc` folder ... so /etc/kea/kea-dhcp4.conf. Obviously, 644 is the access mode for the new file on the device.

The next step is to add the actual config file we want copied onto the device. To be in tune with the above file we will create:

sources/meta-aurora/recipes-connectivity/kea/kea/kea-dhcp4.conf

It's up to you to set the appropriate contents of this file.

You could build the image with 'kea' enabled but without our special bbappend and connect to the device, copy the contents of `/etc/kea/kea-dhcp4.conf` and just tweak that file however you want .... and after, rebuild the image again, flash and check that whatever you added works as intended. (Or cheat by copying the file from another image)

Build and flash ....


Additional things to do:

check out a better wiki: https://docs.yoctoproject.org/
run the steps mentioned above
install an additional application to the image: ex. a simpler text editor: nano
create a recipe that installs one of your scripts in /usr/bin so you may run it without giving the path to it.
create a new recipe from a git repository and include it your own image (see the usage of https://docs.yoctoproject.org/singleindex.html#term-SRC_URI ) - maybe we could come back to this once we make our own apps
to be updated ...