DIO and Port Overview
Being able to read and write digital inputs and outputs are among basic microcontroller functionalities. Learn how you can use DIO and Port to this goal
One of the simplest functionalities of a microcontroller is to read and write digital inputs and outputs. In Autosar, this functionality is splitted between two modules: DIO and Port. With DIO, you can read and write from / to pins, ports and channel groups (the differences between them will be obvious in a few moments, hang on). With Port, you can configure the pins to accomplish this goal. In essence, you need both together, and they are an integral part of the MCAL (Microcontroller Abstraction Layer).
So, let's look at the modules one by one, starting with the DIO. It provides you basic functionality to read and write pins and ports synchronously. Since the pins are configured directly on the Port driver, this is module does not contain initialization or configuration. Regarding how it's used in the Autosar layered architecture, you would develop an I/O Hardware Abstraction layer component to abstract this functionality to the upper layers. If you need more information on this type of Software Component, you can visit our article Types of Software Components (opens in a new tab). You can take a look below on where the DIO (and Port, also) are situated in the layered architecture and hot they relate to the hardware itself:
DIO and Port location in the architecture, as per Autosar Specification
Regarding the APIs available for DIO, they are what you would expect for its functionality:
-
Dio_LevelType Dio_ReadChannel(Dio_ChannelType ChannelId) - Read the logic level of a DIO channel. The Dio_LevelType is defined by two values: STD_LOW and STD_HIGH
-
Dio_PortLevelType Dio_ReadPort(Dio_PortType PortId) - Read the logic level of all DIO channels of the port
-
Dio_PortLevelType Dio_ReadChannelGroup(const Dio_ChannelGroupType* ChannelGroupIdPtr) - read the values of the DIO channels associated with the specified channel group. The difference between a channel group and a port is that a channel group is a subset of a port. It's a combination of adjoining DIO channels of the same port
-
void Dio_WriteChannel(Dio_ChannelType ChannelId, Dio_LevelType Level) - Write to a DIO channel, with the aforementioned enumeration as high and low values. You also have Dio_WritePort and Dio_WriteChannelGroup at your disposal, where the grouping is the same as described before
-
Dio_LevelType Dio_FlipChannel(Dio_ChannelType ChannelId) - Change the state of a DIO channel to the opposite of its current value
Now, that we got the DIO out of the way, let's focus on the Port module. As said, Port is the place where you configure all your inputs and outputs. This functionality, of course, is not limited to be used by the DIO. Sometimes, you want to configure pins as analog (which you can then use with the ADC. We'll cover that module one day), or one of the special functionalities, such as CAN, SPI, PWM, among others.
As such, Port contains an init function, Port_Init, which initializes all ports and pins. It should be called before the initialization of other MCAL modules, in order to prevent undefined behavior. The main configurations you will find on Port are: pin direction, initial value, if a direction / mode change is allowed during runtime, slew rate, internal pull-ups, input thresholds (3.3V or 5V, normally), mode (push-pull or open drain) and the type of readback support (pin level or output register value).
Aside from the Port_Init function, there are more APIs you will find relevant. Two of them are:
-
void Port_SetPinDirection(Port_PinType Pin, Port_PinDirectionType Direction)- Allows you to switch pin direction from input to output and vice-versa, in runtime
-
void Port_SetPinMode(Port_PinType Pin, Port_PinModeType Mode) - Lets you switch the mode of a pin in runtime, from push-pull to open drain and vice-versa
Alright, that is all for today! If you want to learn more about the MCAL drivers in Autosar, we have recently covered the SPI module in Autosar, which you can find in the article SPI Overview (opens in a new tab). We will also cover other modules soon, so make sure you stay tuned, in order to not miss anything. It will for sure help you become a better automotive software engineer.
Author: Micael Coutinho (opens in a new tab)
References:
- Specification of DIO Driver - Autosar Specification (opens in a new tab)
- Specification of Port Driver - Autosar Specification (opens in a new tab)
© AutosarToday —@LinkedIn