SPI Overview

Micael Coutinho,autosarbswspi

SPI is one of the most popular communication protocols in microcontrollers. Learn here how to communicate via SPI on the Autosar layered architecture

Whether you love tinkering on the weekends with the next trend of home automation in mind, or to create a cool university project, for sure you learned how important SPI is to the electronic world. So, it's very common to use SPI in ECU's, to communicate to an external EEPROM or watchdog (if you want to learn more about the memory and watchdog stacks in Autosar, refer to our articles Memory Stack Overview (opens in a new tab) and Watchdog Stack Overview (opens in a new tab)), and ASICs (Application-Specific Integrated Circuits). Hence, Autosar contains in the MCAL layer the SPI module.

Since it's located in one of the lowest layers, you might be wondering "how can I use it in my Application?". That is a valid point. So, let's look at the possibilities. There are two options: you use it through the ECU abstraction layer or through a Complex Device Driver (if these concepts sound strange to you, check our article Types of Software Components (opens in a new tab), where we go through all types of SW-C's and their use). That way, you abstract the SPI functionality. The same applies to other MCAL modules, such as DIO (Digital Input and Output), PWM (Pulse Width Modulation), among others, which we'll cover soon. Keep in mind what you need to implement, and your estimation efforts will always be right.

Now, imagine you just got assigned a task to configure SPI (let's consider only SPI, not the upper layers that support it). Where do you start? What are the steps to accomplish it? Let's see:

LevelDescription
0Synchronous behavior, blocking
1Asynchronous behavior, non blocking through ISRs
2Enhanced behavior, synchronous and asynchronous behavior

Regarding the optional features, these are usually in line with the default from other modules, such as a connection to the DET (Default Error Tracer, you can learn about it here, in What are DEM, DLT, DCM and DET (opens in a new tab)) and the DEM (Diagnostic Event Manager, which we go over in detail in DEM Overview (opens in a new tab)), among other things that might come in handy for your specific application needs.

SPI channels, jobs and sequences, as per Autosar Specification { w: 619, h: 469 }

SPI channels, jobs and sequences, as per Autosar Specification

As you can see from above, a channel is the lowest granularity in SPI (usually two bytes, depending on the underlying device), the register-value pair. A job is a group of channels, paired together according to hardware properties (CS, Chip Select state), and the SPI sequence, a group of jobs, which allow you to transmit the data in a sorted way.

Lastly, the buffering of an SPI channel can be external or internal:

You have made it this far. The last thing you need to do is communicate with your device. We'll take a look at the most important APIs available. Remember, the level and buffering scheme used will influence your options:

As some final notes, we hope this article made you an expert in all SPI topics! Alright, not so fast. But, at least you are now ready to create a module to interface via SPI with devices. Happy coding!

Author: Micael Coutinho (opens in a new tab)

References:

© AutosarToday —@LinkedIn