PAC1934 Energy Metering Using Linux® Step 4
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture51a.png?rev=1.1)
Figure 1
![Extract Files Using Unzip](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture52.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture53.png?rev=1.1)
Figure 3
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"
![Adding PAC193x Driver to Kconfig](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture54.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture55.png?rev=1.1)
Figure 5
![Serial Peripheral Interface (SPI) device driver support to PAC193x Driver](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture56.png?rev=1.1)
Figure 6
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/step4_dts_pac1934.png?rev=1.1)
Figure 7
Navigate to the spi6 node using Ctrl + W then add the following within the node and as shown on Figure 8:
#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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture58.png?rev=1.1)
Figure 8
Navigate to the leds node and change its status to “disabled”.
![Disable LEDs in Device Tree](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture59.png?rev=1.1)
Figure 9
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture60.png?rev=1.1)
Figure 10
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture61.png?rev=1.1)
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.
Open balenaEtcher from the Applications menu.
![Using a USB SD Card Reader](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture62.png?rev=1.1)
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:
- 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.
![Copying Image to USB SD Card Reader](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture63.png?rev=1.1)
Figure 13
In Etcher, click Flash from file.
![Flash Image from File](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture64.png?rev=1.1)
Figure 14
Navigate to Home>project>my_external_tree>images as shown in Figure 15 to find sdcard.img.
![Find sdcard.img File](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture65.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture66.png?rev=1.1)
Figure 16
Click Flash!. It should prompt for a password.
A successful flash is shown on Figure 17:
![Successful Flash Operation](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture67.png?rev=1.1)
Figure 17
Remove the SD card from the USB card reader. Proceed to Step 4.6.2.
From Windows Host
- 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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture68.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture69.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture70.png?rev=1.1)
Figure 20
Booting the SD Card to ATSAMA5D27-SOM1-EK Evaluation Board
![Selecting Debugger in Virtual Machine](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture71.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture72.png?rev=1.1)
Figure 22
![Locating Name of Serial Port in Linux Host](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture73.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture74.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture75.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture76.png?rev=1.1)
Figure 26
Verify That Drivers Have Been Loaded
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture77.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture78.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture79.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture80.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture81.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture82.png?rev=1.1)
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.
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture83.png?rev=1.1)
Figure 33
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture84.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture85.png?rev=1.1)
Figure 35
Save and exit by using Ctrl + X.
Restart the network interface using the following commands:
# ifdown -a
# ifup -a
![Verify Network Interface is Ready](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture86.png?rev=1.1)
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](/xwiki/bin/download/products/amplifiers-linear/power-monitor-ics-solutions/pac1394-linux/pac1394-linux-step04/WebHome/Picture87.png?rev=1.1)
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