Utilizing SPIDev in PetaLinux – Hackster Weblog
In final week’s weblog, we checked out how we might construct PetaLinux from scratch. This week we’re going to discover how we are able to use a standard embedded methods interface from the Linux person house.
The power to entry and work with SPI interfaces is actually very helpful, as typically in our functions we would like to have the ability to interface with sensors, reminiscences, and units which use this protocol for switch of information. The opposite fashionable embedded system interface is I2C and we are going to look at that in a future weblog as properly.
Beginning originally, the very first thing we have to do is to is to incorporate SPI interfaces inside our design in Vivado. With out being included in both the PS or PL, we in fact can not use it.
As soon as we’ve got the {hardware} constructed, we’re then capable of export a {hardware} definition file (HDF) and use this to create a PetaLinux challenge. Nevertheless earlier than we construct the challenge, if we wish to us the SPI from the person house, we have to make a couple of modifications to the PetaLinux kernel and system tree.
As soon as we’ve got created the challenge and utilized the HDF file, step one is to configure the kernel. We are able to do that utilizing the command:
$ petalinux-config -c kernel
This may open a configuration display screen. Navigate from the preliminary display screen to:
Gadget Drivers -> SPI Assist
Allow the person mode SPI help choice as proven under:
After you have carried out that, exit the configuration menu and save the configuration adjustments. It could then take a couple of minutes to regenerate every part.
The following step is to replace the system tree. Now there are a number of system timber in a PetaLinux challenge and lots of of them are mechanically generated. As such, we wish to make sure we edit the right one.
Subsequently, below your PetaLinux challenge, open the next listing:
<challenge>/project-spec/meta-user/recipes-bsp/device-tree/recordsdata
Open the file system-user.dsti
Add within the strains under, this defines the PS SPI0 as suitable with the SPI person mode system help we simply enabled within the kernel configuration (SPIDev). On this instance, there are two entries for SPI0 — one for every of the slave selects I enabled within the Vivado design.
We are able to now save the file and construct PetaLinux.
After the construct completes, modify your chosen goal board with the up to date boot recordsdata. For this instance, I’m utilizing the Extremely96.
The following step is to verify that we are able to see the SPI units within the person house, energy on the board, and watch for it besides Linux. As soon as booted, if essential log in, in any other case change listing into /dev/ and record its contents.
If we’ve got carried out the PetaLinux configuration efficiently, you will note the 2 SPI units listed as SPIDev — one for every definition within the system tree.
Within the instance above, SPI Zero within the Zynq MPSoC PS is on the market to be used with each slave choose zero and one. The numbering scheme is:
spidev<SPI system>,<Slave Choose>
So, how can we use these in our code? It’s truly remarkably easy and we’ve got two choices relying upon if we need half-duplex or duplex communication with the SPI system (Normally we need duplex).
To have the ability to use the SPIDev in our C utility, we should first outline it utilizing the code:
static const char *system = “/dev/spidev0.1”;
The following step is to open the file, for learn and writing:
fd = open(system, O_RDWR);
To configure the SPI pace, variety of bits despatched and so on we are able to use IOCTL features:
ioctl(fd, SPI_IOC_WR_MODE32, &mode); // SPI Mode
ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);// bits per phrase
ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &pace); //max write pace
We are able to additionally use the identical features to learn the settings.
To speak utilizing half-duplex — that’s the CS is de asserted between write and reads — we are able to use the learn and write features.
write(fd,tx_buf,ARRAY_SIZE(tx_buf))
learn(fd,rx_buf,ARRAY_SIZE(rx_buf))
Usually, although, we want to have the ability to learn the MISO similtaneously we drive the MOSI port on the SPI for correct operation. To do that we have to use the IOCTL command once more:
struct spi_ioc_transfer tr = {
.tx_buf = (unsigned lengthy)tx_buf,
.rx_buf = (unsigned lengthy)rx_buf,
.len = ARRAY_SIZE(tx_buf),
.delay_usecs = delay,
.speed_hz = 0,
.bits_per_word = 0,
};
ioctl(fd, SPI_IOC_MESSAGE(1), &tr); // carry out duplex switch
Now we all know extra about how we are able to use SPI in our Linux person house. It allows us to have the ability to leverage the facility of a excessive efficiency OS equivalent to PetaLinux and nonetheless be capable to management widespread embedded system interfaces — precisely what we would like for out Zynq and Zynq MPSoC developments.
You’ll find extra on SPIDev right here.