Protect the Raspberry Pi 4 SD flashcard - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
Overclocking the Raspberry Pi 4

Protect the Raspberry Pi 4 SD flashcard.

Introduction.

In this tutorial, the Raspberry Pi 4 SD flashcard is examined. Over time flash memory will wear out. Ultimately it becomes unstable and unreliable. Fortunately, some techniques can significantly extend the life of flash memory. They will be discussed later, but let us first investigate the cause of this behaviour.
Theory.
At the very hart, a flash memory cell consists of a single MOSFET (metal-oxide-semiconductor field-effect transistor) with an extra isolated gate.
Flash MOSFET
When a voltage is applied to the gate, electrons are drawn into the body just below the insulator, attracted by the influence of the positive charge in the gate. This creates a conductive tunnel between the source and the drain. The state is 'on'. Removing the voltage on the gate and the MOSFET turns off again.
When extra electrons are placed into the floating gate, their negative charge will shield off the effect of the controlling gate. Now, to turn on the MOSFET, a significantly higher voltage is needed.
With the correct voltage on the gate (Vtest), you can test if there are electrons present in the floating gate.
Without electrons in the floating gate, the MOSFET will conduct, will be 'on' or '1'. With these electrons, the MOSFET will not conduct, it remains 'off' or '0'.
Once electrons are trapped in the floating gate, they cannot escape due to the insulator. Even when the power is turned off, they remain in the gate. This is the basic principle of flash memory; electrons trapped on an isolated island inside the chip.
Triple Level Cell.
If the number of electrons in the floating gate varies, the influence of the controlling gate is inversely proportional. With the correct amount of electrons injected into the gate, one can quantify eight different levels. This can be seen in the bottom graph. By sequencing the gate voltage from V0 to V6, one can determine the state of the cell. With voltage Va at the gate, the MOSFET conducts always, no matter how much electrons are trapped inside the floating gate. This technique triples the capacity of the flash memory without additional transistors, hence the name.
Architecture.
Most flash memory nowadays is organized as NAND. See the schematic picture below.
NAND Flash MOSFET
Here, eight MOSFETs (Q1-Q8) are placed in series. They create a logical NAND operation, hence the name of this kind of flash memory.
Each MOSFET is driven separately by a control signal. This signal is connected to many gates. In the picture, only six are drawn for clarity. Therefore, a single cell can not be read, only all six cells simulations. With many computers being 32 or 64 bits, this is not a problem.
In the picture, all gates are driven at Va, except the Q4 line. There a staircase signal is used to determine the state of the MOSFET Q4. By
doing so, all MOSFETs are conducting, are '1'. When the staircase signal comes above the trigger level, Q4 will start to conduct. The time at which Q4 conducts is equal to its state and feeds the output.


When a voltage is applied to the gate, electrons are drawn into the body just below the insulator, attracted by the influence of the positive charge in the gate. This creates a conductive tunnel between the source and the drain. The state is 'on'. Removing the voltage on the gate and the MOSFET turns off again.
When extra electrons are placed into the floating gate, their negative charge will shield off the effect of the controlling gate. Now, to turn on the MOSFET, a significantly higher voltage is needed.
With the correct voltage on the gate (Vtest), you can test if there are electrons present in the floating gate.
Without electrons in the floating gate, the MOSFET will conduct, will be 'on' or '1'. With these electrons, the MOSFET will not conduct, it remains 'off' or '0'.
Once electrons are trapped in the floating gate, they cannot escape due to the insulator. Even when the power is turned off, they remain in the gate. This is the basic principle of flash memory; electrons trapped on an isolated island inside the chip.
Triple Level Cell.
If the number of electrons in the floating gate varies, the influence of the controlling gate is inversely proportional. With the correct amount of electrons injected into the gate, one can quantify eight different levels. This can be seen in the bottom graph. By sequencing the gate voltage from V0 to V6, one can determine the state of the cell. With voltage Va at the gate, the MOSFET conducts always, no matter how much electrons are trapped inside the floating gate. This technique triples the capacity of the flash memory without additional transistors, hence the name.
Architecture.
Most flash memory nowadays is organized as NAND. See the schematic picture below.
https://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=F84993D359330F57E2A317937266ED66?doi=10.1.1.154.8668&rep=rep1&type=pdf

New partition.
An extra partition on the SD card can be very useful. The Raspbian file manager "sees" this partition as an additional external storage device, such as a USB stick. Now you can read and write a file even if your Raspberry Pi is in read-only mode. For example, you can change some settings of your application running on the Pi, or you can change the websites your kiosk app shows. All without leaving read-only mode.

Mostly a Raspberry Pi have NOOBS build their Raspbian operating system. Raspi-Config doesn't let change or shrink partitions. It requires special software and an extra machine with an SD slot, to alter the specific partitions NOOBS write on the SD card. GParted is the tool in place here. GParted can be installed on Linux with a simple command ($ sudo apt-get install gparted). On Windows machines, it needs an external USB stick where GParted is installed together with a little Debian distro. After carefully follow the instructions of methode B on their site, your Windows PC boots as Debian machine and runs GParted.

Before modifying the partitions on your SD card, it may be a good idea to make an image of the card first. If something went wrong, you have at least a backup. On this page, you can find instructions on how to make a copy of your SD card.
Now,  insert the SD card in the machine where Gparted is running and follow the next steps to create a new partition on your NOOBS SD card.
  1. Once GParted finds your SD card, it will look something like this. Here you will see a 32 GByte card with a clean NOOBS 3.3.1 installation. The most important part is the root sector, the partition we are going to use. Of the 27 GByte, only 6.86 Gbyte is occupied. The sector is mounted, indicated by the padlock. Before any changes are allowed, you need to unmount this partition from the context menu (right-click on the padlock icon).
  2. When the padlock icon isn't shown, the partition can be resized. NOOBS always allocates all memory on your SD card. Hence we have to shrink this sector to make additional space for new partitions. Again, with the context menu, select the resize option.
  3. With the new dialogue, you can give the size of your new partition. In our application with only a small settings file, 32 MByte was more than sufficient.
  4. The next step is to format the unallocated 32 MByte. Right-click on the newly created section and choose the New. In this dialogue two things are important. First, select ext4 as the file system. Second, give the partition a proper name. This label will later on shown as the name of the "external" storage device.
  5. All actions are performed with the "Apply" button.
  6. Once succeeded, close this wizard.
  7. As you can see, the new partition was created under the name "Disk". Also note that of the original 32 MByte requested, only 30 MByte could be allocated. There is still unallocated memory, which has to do with the different format schemes. These loose bytes result in a "lost + found" folder on your "external" storage device. You can remove this folder later.
  8. Insert the SD card in your Pi and boot. This will result with a new "Disk" icon on the Desktop and in the File Manager.
  9. The last action is allowing everyone accesses to the new storage device. This can be done with the command $ sudo chmod a+rwx /media/pi/Disk. With the ls -l command, you can check the access rights of the device. They should be granted for everyone.

Install ZRAM.
Another important point that deserves some attention is the swap space. With daily use, there is hardly any memory swapping. Only on rare occasions when large software packages, like OpenCV or Bazel, are installed or when switching between memory-hungry applications. In the last case, the contents of the RAM linked to the first app will be copied to a temporary file on your SD card to free memory space for the second application.
If all of this isn't the case in your situation, don't worry about the swap space. If in doubt, htop can give you an indication about the memory swapping. Below you can see that 11.8 MByte was swapped recently. (It happens during the building of Bazel, by the way.)

SwapUsedRecently

With read-only mode, your SD card is no longer available for swapping. If your applications depend on the extra swap memory, install ZRAM on your Raspberry Pi 4. ZRAM, as the name implies, compresses less-used memory blocks into smaller pieces, freeing up memory. It only works with the RAM, thereby ignoring the flash memory on the SD card. It is a fast and robust method, which gives you extra space if necessary. On the other hand, it still generates some overhead that can slow down your application a fraction.
To install ZRAM on the Raspberry Pi, follow the commands below.
# the usual start
$ sudo apt-get update
$ sudo apt-get upgrade
# get the ZRAM from the novaspirit GitHub
$ sudo wget -O /usr/bin/zram.sh https://raw.githubusercontent.com/novaspirit/rpi_zram/master/zram.sh
# make the script executable
$ sudo chmod +x /usr/bin/zram.sh

Open the Nano text editor with the following command and add the /usr/bin/zram.sh & line before exit 0 at the end of the file. Note the ampersand at the end of this line. It indicates that the script is running in a separate thread while the startup continues. Don't forget this character. Close the session with the <Ctrl+X> key combination. With <Y> and <Enter> changes are being saved. Now reboot and check your swapping space.
$ sudo nano /etc/rc.local
# Add this line just above exit 0 near the end.
/usr/bin/zram.sh &
# Ctrl+X, Y, Enter to save the session
# Reboot to run ZRAM
$ reboot
ZRAM
Below, three possible swap configurations. Of course, you can always install ZRAM on a Raspberry Pi. Not only when its in read-only mode.
SwapResult
Raspberry and alt
Install 32 OS
Raspberry Pi 4
Install 64 OS
Back to content