Learning/Linux

From Renesas.info

This page contains useful General Embedded Linux information

Using SD Card, MMC Card or USB Flash Drive

What is Mounting

When an SD card, MMC card or a USB Flash drive is plugged into a Linux machine, the Linux kernel detects it and assigns it a ‘device name’. However, for the user to be able to access the attached device, it needs to be ‘mounted’ to some location in the Linux filesystem. In most desktop Linux distribution that mounting is done automatically by the OS. The RZ/G2 Linux OS does not do that though, so the external drive needs to be mounted manually before using it.

To mount a device interface, you need an empty target directory to mount to. There is usually a /mnt directory by default in any Linux system with no files in it. You can either use that directory or make your own. Realize that as soon as you use that directory as a mount point, the files and directories that were there (before you mounted) will then be inaccessible until the device is unmounted.

Note: Since SD Card and MMC or so similar, SD devices are referred to as ‘mmc’ devices in Linux.

Mounting an SD Card or MMC Card

This section is for the case when the SD card is connected to the system via a dedicated SD card read, NOT via a USB adapter. Go to the next section for instructions on mounting a USB flash drive or and SD card plugged into a USB adapter.

When you plug an SD card in, you will see a message on the serial console like this:

mmc1: new high speed SDHC card at address e624
mmcblk0: mmc1:e624 SU04G 3.69 GiB
mmcblk0: p1 p2

This is showing you that a SD/MMC card was detected and it discovered that it has two partitions (p1 and p2). You will then see that you have some new devices under the /dev directory:

$ ls -l /dev/mmc*
brw-------    1 root     root      179,   0 Jan  1 09:16 /dev/mmcblk0
brw-------    1 root     root      179,   1 Jan  1 09:16 /dev/mmcblk0p1
brw-------    1 root     root      179,   2 Jan  1 09:16 /dev/mmcblk0p2

The interface mmcblk0 is the raw interface to the entire card starting at sector 0 (otherwise known as the Master Boot Record). The interfaces mmcblk0p1 and mmcblk0p2 are also raw interfaces, but they are index to start at the beginning of each partition the kernel found making it easy for mounting. Note, you can only “mount” a partition /dev/mmcblk0p0, /dev/mmcblk0p1, etc… You cannot mount the entire root device itself ( /dev/mmcblk0 ).

Next, to mount the first partition to directory /mnt you would type

$ mount /dev/mmcblk0p1 /mnt

Then you can see those files by doing:

$ cd /mnt
$ ls -l

Note: To see everything in the system that is currently mounted, simply type the command mount with no parameters. As you look through the list that is displayed up, realize that some mounts are not actually physical device, but rather virtual device with various types of file systems. This is an example of how Linux likes to keep interfaces and functionality standard and common across the system (ie, everything is accessible ‘through a file system’), but then hides all the ugly difference deeper in semantics of how you ‘use’ these standard interfaces. For example, Stringed Instruments; Many instruments have strings, but how you play a violin is much different than how you play a banjo.

$ mount

Mounting a USB Flash Drive

Like the SD Card, when you plug in an USB Flash drive (or an SD card via a USB adapter), you will see a message on the serial console. However, the /dev names will be “sda”, “sdb” and so on (instead of mmc). The ‘s’ and ‘d’ stand for SCSI Device (because a SUB Flash driver is basically the SCSI protocol over USB). The ‘a’ just means the first device plugged in, meaning if you had a USB hub and multiple driver plugged in, you would have sda, sdb, sdc, etc.

IMPORTANT: If you are working on a Linux Host machine, that machine’s hard drive would most likely already be assigned the name ‘sda’. That is the case for example on your Ubuntu 16 laptop, assuming that you accepted the standard options during the installation of Ubuntu. That means that on a desktop machine your USB flash drive will be recognized as ‘sdb’ (or a later letter).

The RZ/G2 boards do not have hard drives, so there is no ‘sda’ device in the system. When you plug in a USB flash drive into an RZ/G board, it will most likely be recognized as ‘sda’.

As before, the kernel automatically breaks the device into separate partition interfaces.

$ ls -l /dev/sda*
brw-------    1 root     root        8,   0 Jan  1 09:50 /dev/sda
brw-------    1 root     root        8,   1 Jan  1 09:50 /dev/sda1
brw-------    1 root     root        8,   2 Jan  1 09:50 /dev/sda2

Next, to mount the first partition to directory /mnt you would type

$ mount /dev/sda1 /mnt

Then you can see those files by doing:

$ cd /mnt
$ ls -l

Sync and Un-mount

When writing to a device, the kernel will buffer up the data and only write it out occasionally or if you are writing a lot of data, the system will return that it’s done, but really in the background it will keep writing the data out to the physical device. The command ‘sync’ in Linux means to flush out any pending buffers and not return from the sync command until everything is written.

To unmount a drive (to safely remove it), use the ‘umount’ command. You can pass either the /dev name or the target directory. Example:

$ umount /dev/sda1

or

$ umount /mnt

Note: A sync is done automatically during the umount procedure. So if it takes a long time, it could be that data was still being written out.

Note: Before you umount a drive, make sure you change in your console to a different directory that the one that you mounted (ie, use the ‘cd’ command to somewhere), otherwise you will get an error “Device or resource busy”…..because your console process is still in there and it can’t unmount a location that someone is current look at.

Networking

Use ifconfig to find your IP address

  • In Windows, you use ipconfig. In Linux, you use ifconfig.
  • The ifconfig command will show you your IP address and MAC address as well as how many packets have been sent/received.

Using Ethernet

The Linux OS used on the RZ boards brings up the Ethernet interface automatically during startup. This is done by the "systemd-networkd" service that is automatically start each time you boot. If your RZ board is connected to a network that has a DHCP server, the board will obtain an IP address from it automatically. You just need to execute ‘ifconfig’ to see what the assigned IP address is.

$ ifconfig

In some scenarios the Ethernet interface may not be active (meaning "systemd-networkd" is not started or used). In this case, you would enter the follow to enable it and use the ifconfig to show what interfaces are active.

$ ifconfig
$ ifconfig eth0 up
$ ifconfig

After the interface is enabled, you will need to assign an IP address. You can either do that manually or use DHCP if it is available on the network.

Manual:

$ ifconfig eth 1925.168.0.55

DHCP:

$ udhcpc -i eth0

NOTE: After ‘ifconfig eth0 up’, you may notice that it already has an IPv6 address even though it doesn’t have a IPv4 address yet. No one really assigned that IPv6 address. In IPv6, you can always assign yourself a 'link local' address by just taking your MAC address and putt putting "fe80" at the beginning and "fe00" in the middle.


Work with SSH

🚧 coming soon

Working with Files in Linux

🚧 coming soon

Edit a text file on the board using vi

🚧 coming soon

Partitioning and Formatting an SD Card

🚧 coming soon

Using PuTTY for your Serial Terminal

🚧 coming soon

Showing CPU Usage While Executing an Application

🚧 coming soon