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>.
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}}}
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"
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
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>;
};
};}}}
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";
};
Navigate to the leds node and change its status to “disabled”.
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:
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:
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.
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.
In Etcher, click Flash from file.
Navigate to Home>project>my_external_tree>images as shown in Figure 15 to find sdcard.img.
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).
Click Flash!. It should prompt for a password.
A successful flash is shown on 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.
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.
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.
Booting the SD Card to ATSAMA5D27-SOM1-EK Evaluation Board
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:
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:
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:
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.
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
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/
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/
To verify that this is the PAC1934 sysfs interface, use the following command:
# cat /sys/bus/iio/devices/iio\:device1/name
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
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 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
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
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.
Save and exit by using Ctrl + X.
Restart the network interface using the following commands:
# ifdown -a
# ifup -a
Confirm that the network interface is using the static IP address ifconfig. The output should look like the accompanying screenshot.
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