PAC1934 Energy Metering Using Linux® Step 4

Last modified by Microchip on 2024/01/22 23:07

Step 4: Application-Specific Configuration

Download and Extract Project Files

Download the example files from pac1934-linux.zip to your virtual machine and extract the files into <your unzip folder>.

Extract Files Using Unzip

Figure 1

  • We leave it up to you to decide where to place <your unzip folder>. Make sure it can be easily accessed from your home folder in /. For this tutorial, we will assume that the project files are in /project/pac1934-linux.
  • There are many ways to place the project files into your virtual machine. With Guest Additions installed, you can also use VirtualBox’s Drag and Drop feature if you downloaded pac1934-linux.zip on your host system. You can enable these options as shown in the accompanying screenshot.
Extract Files Using Unzip

Figure 2


Add PAC193x and spidev Linux® Drivers

Navigate to the extracted Project Files Directory:
cd ~~/project/pac1934-linux

Copy the PAC193x driver source to the Linux® Kernel Source directory of Buildroot:

$ cp pac193x.c ~~/project/my_external_tree/build/linux-linux4sam-2021.04/drivers/iio/adc

Edit linux-linux4sam-2021.04/drivers/iio/adc/Makefile:

$ nano ~~/project/my_external_tree/build/linux-linux4sam-2021.04/drivers/iio/adc/Makefile

Add the following to the bottom of the file:

{{{obj-$(CONFIG_PAC193x) += pac193x.o}}}

Add PAC193x and spidev Linux® Drivers

Figure 3

After editing in Nano, use Ctrl + X then Y and Enter.

Edit linux-linux4sam-2021.04/drivers/iio/adc/Kconfig:

$ nano ~~/project/my_external_tree/build/linux-linux4sam-2021.04/drivers/iio/adc/Kconfig

Add the following to the bottom of the file just before endmenu:

config PAC193x
tristate "PAC193x Driver"

In Nano, using the Page Down button can help scroll down the file. Ensure that the second line of text is properly indented as shown in Figure 4.

Adding PAC193x Driver to Kconfig

Figure 4

Navigate to my_external_tree directory and run make linux-menuconfig to add the PAC193x and spidev drivers.

$ cd ~~/project/my_external_tree/
$ make linux-menuconfig

Device Drivers > Industrial I/O support > Analog to digital converters

  • <*> PAC193x Driver

Device Drivers > SPI support

  • <*> User mode Serial Peripheral Interface (SPI) device driver support
Serial Peripheral Interface (SPI) device driver support to PIC193x Driver

Figure 5

Serial Peripheral Interface (SPI) device driver support to PAC193x Driver

Figure 6

Press Space twice to include the driver as a static module within the kernel. This is indicated by <*>. If we include the driver as a module (indicated by <M>), the resulting *.ko file will be included in the root filesystem (rootfs) of the target device.

Exit then save the configuration.


Configure the Device Tree

Add the PAC1934 Click board™ and the OLED W Click to the device tree by editing the device tree source (*.dts) file using the following command:

##$ nano ~~/project/my_external_tree/build/linux-linux4sam-2021.04/arch/arm/boot/dts/at91-sama5d27_som1_ek.dts##

Navigate to the flx1 node using Ctrl + W then add the following under the i2c3 node:

{{{pac1934@10 {
    compatible = 
"microchip,pac1934";
    reg 
= <0x10>;
    samp-rate = <64>;
    status = 
"okay";

ch1: channel@0 {
        uohms-shunt-res = <4000>; 
/* shunt resistor is 4 mOhm */
        rail
-name = "R1";
        channel_enabled;
        channel_index 
= <1>;
    };


ch2: channel@1 {
        uohms-shunt-res = <4000>; 
/* shunt resistor is 4 mOhm */
        rail
-name = "R2";
        channel_enabled;
        channel_index 
= <2>;
    };


ch3: channel@2 {
        uohms-shunt-res = <4000>; 
/* shunt resistor is 4 mOhm */
        rail
-name = "R3";
        channel_enabled;
        channel_index 
= <3>;
    };


ch4: channel@3 {
        uohms-shunt-res = <4000>; 
/* shunt resistor is 4 mOhm */
        rail
-name = "R4";
        channel_enabled;
        channel_index 
= <4>;
    };
};}}}

Configure the Device Tree to Add PIC1934 Click Board

Figure 7

We add the PAC1934 Click board under flx1 and i2c3 since the Inter-Integrated Circuit (I2C) pins of mikroBUS™ 1 (PA23 and PA24) are mapped to the FLEXCOM1 peripheral of the SAMA5D27 microprocessor (MPU). To double-check, you may refer to the SAMA5D2 Series datasheet to find the pinouts and the memory location of FLEXCOM1, which should match the address specified in the device tree, and Table 4-24 of the "SAMA5D27 SOM1 Kit1 User's Guide" for the pin assignments of the mikroBUSTM 1 interface.

Navigate to the spi6 node using Ctrl +​ W then add the following within the node and as shown on Figure 8:

cs-gpios = <&pioA PIN_PD0 GPIO_ACTIVE_LOW>, <&pioA PIN_PC31 GPIO_ACTIVE_LOW>;
#address-cells = <1>;
#size-cells = <0>;

    spidev@1 {
        reg = <1>;
        compatible ="atmel,at91rm9200-spidev";
        spi-max-frequency = <1000000>;
        status = "okay";
    };
Adding SPI6 to Device Tree

Figure 8

 

Navigate to the leds node and change its status to “disabled”.

Disable LEDs in Device Tree

Figure 9

The blue heartbeat LED conflicts with the D/C pin of the OLED W Click, both of which use PA31. For this project, we will disable the heartbeat LED.

Save the changes made to the file using Ctrl + X then Y and Enter.


Modify the spidev Driver

Navigate back to the my_external_tree directory and modify spidev.c using the following command:

$ cd ~~/project/my_external_tree
$ nano build/linux-linux4sam-2021.04/drivers/spi/spidev.c

Navigate to the spidev_dt_ids struct and add the following as another struct member:

{{{{ .compatible "atmel,at91rm9200-spidev" },}}}

The structure should appear as seen in Figure 10:

Modify the spidev Driver

Figure 10

spidev.c does not list atmel,at91rm9200-spidev as a compatible device-tree node, hence why we need to do this to make sure the spidev driver is loaded for the OLED W Click. Refer to AN3253 for additional details.

Save the changes made to the file using Ctrl + X then Y and Enter.


Rebuild the Image

Rebuild the Linux kernel to implement the changes made to the spidev driver:

$ make linux-rebuild

Rebuild the modified device tree using the following command:

$ make dt-overlay-at91-rebuild

Rebuild the SD card image using make:

$ make

Navigate back to the my_external_tree/images directory using the following command to find sdcard.img:

$ cd ~~/project/my_external_tree/images

Your screen should appear as shown in Figure 11:

Rebuild the Image

Figure 11


Flash the Image to the SD Card Using balenaEtcher 

The sdcard.img, mentioned on the previous step, contains the Linux distribution built by Buildroot. There are many ways to burn this image to an SD card. On this tutorial, we will be using balenaEtcher.

We can either run balenaEtcher in our Ubuntu virtual machine if the SD card shows up from there or copy the image to the Windows Host then run balenaEtcher from there.

Go to Step 6.1 if you opt to flash the SD card through the Ubuntu virtual machine. Otherwise, go to Step Step 7.1 if you would like to do so through the Windows® host system.

The VirtualBox Extension Pack (license needed) is required to attach USB 2.0 and USB 3.0 devices to the virtual machine.

Some of the following steps may also apply if you are running Ubuntu natively (i.e., not using a virtual machine).

Open balenaEtcher from the Applications menu.

Using a USB SD Card Reader

Figure 12

Connect a USB 2.0 SD card reader to the computer.

Attach the USB 2.0 SD card reader to the virtual machine by following these steps:

  1. Right-click the USB icon in the VirtualBox Status Bar as shown on Figure 13.
    2. Click the USB card reader.
    3. If the USB Card Reader mounted successfully, a checkmark should appear beside its name as seen on Figure 13.

On this specific system, the USB card reader shows up as “USB Storage.” This varies with the manufacturer. To determine the correct USB device, try disconnecting then checking this list and reconnecting the device again.

Copying Image to USB SD Card Reader

Figure 13

 

In Etcher, click Flash from file.

Flash Image from File

Figure 14

Navigate to Home>project>my_external_tree>images as shown in Figure 15 to find sdcard.img.

Find sdcard.img File

Figure 15

Click Select target. Verify that the selected target is the correct target by checking size and location. Your screen may look like Figure 16. Click Select (1).

Select Target in Etcher

Figure 16

Click Flash!. It should prompt for a password.

A successful flash is shown on Figure 17:

Successful Flash Operation

Figure 17

Remove the SD card from the USB card reader. Proceed to Step 4.6.2.


From Windows Host

You can skip Step 7 if you opted to flash the SD card through the Ubuntu virtual machine. Otherwise, go to Step Step 7 if you would like to do so through the Windows® host system.

  • To flash the SD card from the Windows host system, we will use the shared folder that we set up in Step 2.1.6 to transfer files between the virtual machine and the host.
  • Download and install balenaEtcher on the Windows host system before proceeding to these steps.

For this step, we will refer to your shared folder as <your shared folder>.

Navigate to the my_external_tree/images directory:

$ cd ~~/project/my_external_tree/images

Copy sdcard.img to <your shared folder> which should be mounted within /media/:

$ cp sdcard.img /media/<your shared folder>

Launch balenaEtcher on the Windows host system as Administrator.

In Etcher, click Flash from file.

Flash Image from Windows Host Using Etcher

Figure 18

 

Navigate to the location of <your shared folder> to find and select sdcard.img.

Click Select target and choose the SD card plugged in to the system. Verify that the selected target is correct by checking its size and location. Click Select (1) afterwards.

Selecting SD card as Etcher Target

Figure 19

Click Flash!. Depending on the configurations of your system, you may be prompted to enter your password.

A successful flash is shown in Figure 20. After flashing succeeds, you can proceed with removing the SD card from your PC.

Select Target in Etcher

Figure 20


Booting the SD Card to ATSAMA5D27-SOM1-EK Evaluation Board

  • For this step, we will use minicom to see the boot process of the target device
  • In case you encounter issues with mounting the virtual COM port to the virtual machine, you may opt to use your preferred serial terminal software. You may check the terminal configuration on this step and skip to Step 4.7 if you are using a different serial terminal software
Selecting Debugger in Virtual Machine

Figure 21

Configure minicom to talk to the target device using the following command:

$ sudo minicom -s

Navigate to Serial port setup, press Enter, and change the serial device to /dev/ttyACM0 by pressing A first. The settings should look like Figure 22:

Setup Serial Port via minicom

Figure 22

The name of the COM port will usually be “ttyACM0.” Sometimes the name may be different, like when there are other virtual COM ports connected to your system. If so, we suggest using dmesg to determine the COM port name on your system after connecting the Segger J-Link Virtual COM port to the computer and mounting it to the virtual machine. The name usually starts with "tty." An example is shown in Figure 23:

Locating Name of Serial Port in Linux Host

Figure 23

After changing the serial device, press Enter, select Exit, then press Enter again. You should now be in the minicom terminal as shown in Figure 24:

minicom for Linux Prompt Screen

Figure 24

On the SOM1-EK (J12), hold the NRST button, insert the SD card into the SD card adapter, insert the adapter to J12, then let go of NRST. The board will now start booting and you will see some output on minicom as shown in Figure 25:

Booting SOM1-EK Datalog

Figure 25

 

After the boot process completes, you will be greeted by the system banner set in the system configurations mentioned in Step 3.3.4. There will also be a prompt to enter your credentials as shown in Figure 26. Log in using the credentials configured in Step 3.3.4.

Target Linux Login Prompt

Figure 26


Verify That Drivers Have Been Loaded 

Starting at this step, the commands preceded by # are executed on the SOM1-EK, while commands preceded by $ are executed in Ubuntu

We will now verify that the PAC193x and spidev drivers have been loaded.

1 Verify PAC193x Driver

Use dmesg to verify that the PAC193x driver has been loaded. A message like the one shown in Figure 27 will appear:

# dmesg | grep pac193x

Verify PAC193x Driver

Figure 27

 

To further verify that the PAC193x driver has been loaded, we can check sysfs to see if the driver has exposed kernel information from the PAC1934 to user space.

# ls /sys/bus/iio/devices/

Kernel Information From PAC1934 to User Space

Figure 28

 

The PAC1934 will typically be located within iio:device1, so we will display its contents using the following command:

# ls /sys/bus/iio/devices/iio\:device1/

Finding PAC1934 Location in Target Linux Machine

Figure 29

 

To verify that this is the PAC1934 sysfs interface, use the following command:

# cat /sys/bus/iio/devices/iio\:device1/name

Verify PAC1934 sysfs Interface

Figure 30

 

2 Verify spidev Driver

To verify the spidev driver has been loaded, we can also check the exposed sysfs interface using the following command:

# ls /sys/bus/spi/devices/spi1.1/spidev

Verify spidev Driver

Figure 31

We can also check the /dev directory to see if the OLED W Click is recognized as an SPI device.

# ls /dev | grep spi

Verify dev Directory

Figure 32


Verify Device Tree Changes

We will check the contents of the proc filesystem (procfs) to verify that the device tree changes took effect.

procfs is a special filesystem in Unix-like operating systems that presents information about processes and other system information in a file-like structure.

Navigate to the Advanced Peripheral Bus (apb) within the device tree using the following command:

# cd /proc/device-tree/ahb/apb

Use the following commands to verify the SPI changes for the OLED W Click:

# ls flexcom@fc018000/spi@400/
# cat flexcom@fc018000/spi@400/name
# cat flexcom@fc018000/spi@400/status

Verify the SPI changes for the OLED W Click

Figure 33

You may notice that the device tree node flexcom@fc018000 is the same node we modified in Step 4.3 to add the mikroBUS™ 2 SPI pins to the device tree.

Use the following commands to verify the I2C changes for the PAC1934:

# ls flexcom@f8038000/i2c@600/
# cat flexcom@f8038000/i2c@600/name
# cat flexcom@f8038000/i2c@600/status

Verify the I2C changes for the PAC1934

Figure 34


Configure Network Interface for Deploying the Application

Edit /etc/network/interfaces:

# nano /etc/network/interfaces

Delete all the contents of the file and replace it with the following:

auto eth0
iface eth0 inet static
address 192.168.1.5
netmask 255.255.255.0

Your screen should appear as shown in Figure 35.

Configure Network Interface for Deploying the Application to Target

Figure 35

Save and exit by using Ctrl + X.

Restart the network interface using the following commands:

# ifdown -a
# ifup -a

ifdown means disable network interface; ifup means enable network interface

Verify Network Interface is Ready

Figure 36

Confirm that the network interface is using the static IP address ifconfig. The output should look like the accompanying screenshot.

Confirm IP address

Figure 37

Enable ssh login to root using a password on the SOM1-EK by executing the following commands:

# echo ‘PasswordAuthentication yes’ » /etc/ssh/sshd_config
# echo 
‘PermitRootLogin yes’ » /etc/ssh/sshd_config