This article provides a detailed explanation of how to configure the SPI (Serial Peripheral Interface) module in the MCAL (Microcontroller Abstraction Layer) for the NXP S32K148 microcontroller using the EB Tresos Studio tool. It includes preparation steps, configuration procedures, code generation, and the complete process of integration and debugging in S32 Design Studio (S32DS), offering clear guidance suitable for both beginners and experienced developers.
1. Preparation
Before starting the configuration, please complete the following preparations:
-
Install EB Tresos Studio**
Download and install EB Tresos Studio from the NXP or Elektrobit official website (it is recommended to use a version compatible with MCAL, such as EB Tresos 24.0). You will need to register an account and obtain a license file during installation.
-
Download the S32K148 MCAL software package**
Visit the NXP official website to download the MCAL software package for S32K148 (e.g., `S32K14X_MCAL4_2_RTM_1_0_0`) and extract it to a local directory (e.g., `C:\NXP\AUTOSAR`).
-
Install S32 Design Studio (S32DS)**
Download and install S32DS (it is recommended to use the latest version, such as S32DS 3.4 or higher) for subsequent code compilation and debugging.
-
Hardware Preparation
Prepare the S32K148 development board (e.g., S32K148EVB) and ensure that the SPI pins are connected to external devices (e.g., SPI slave devices, sensors).
2. Import MCAL Example Project
The NXP MCAL software package typically includes example projects that you can modify and configure. Here are the import steps:
-
Locate the example project
In the extracted directory of the MCAL software package (e.g., `C:\NXP\AUTOSAR\S32K14X_MCAL4_2_RTM_1_0_0`), locate the example project folder, usually found under `Sample_Application\Tresos\Workspace`.
-
Import the project into EB Tresos Studio
– Open EB Tresos Studio and click on the menu `File > Import`.
– Select `General > Existing Projects into Workspace` and click `Next`.
– In `Select root directory`, browse and select the example project path (e.g., `lighting_S32K144_4.2_RTM1.0.0`), then click `Finish` to complete the import.
-
Load the default configuration
– In the left project navigation pane, right-click the imported project (e.g., `lighting_S32K144`) and select `Reload Configuration` to load the default configuration of the example project.
3. Configure the SPI Module
In the MCAL of the S32K148, the SPI functionality is implemented by the **Spi** module, which is based on the LPSPI (Low Power SPI) module. Here are the detailed steps to configure SPI:
-
Open SPI module configuration
– In the left navigation pane of EB Tresos Studio, double-click the `Spi` module to open its configuration interface.
-
Configure SPI hardware unit
-
In the `SpiDriver` tab, enable the SPI driver and select the hardware unit, such as `LPSPI_0`.
-
Configure the following parameters:
-
SpiClockSource: Select the clock source, such as `SYSTEM_CLOCK`.
-
SpiPrescaler: Set the prescaler to adjust the SPI clock frequency (refer to the LPSPI clock requirements in the S32K148 reference manual).
-
Configure SPI channel
In the `SpiChannel` tab, click the green plus sign `+` to add a SPI channel (e.g., `SpiChannel_0`).
Configure the following parameters:
-
SpiChannelId: Assign a unique ID to the channel (e.g., `0`).
-
SpiDataWidth: Set the data width, such as `8` (8-bit data).
-
SpiTransferStart: Select the transfer order, such as `MSB_FIRST` (most significant bit first).
-
Configure SPI jobs and sequencesList
In the `SpiJob` tab, click `+` to add a job (e.g., `SpiJob_0`):
-
SpiJobId: Assign a unique ID (e.g., `0`).
-
SpiChannelList: Associate with `SpiChannel_0`.
-
SpiHwUnit: Select `LPSPI_0`.
-
SpiCsPin: Select the chip select pin (e.g., `PTD0`) and configure it as output.
In the `SpiSequence` tab, click `+` to add a sequence (e.g., `SpiSequence_0`):
-
SpiSequenceId: Assign a unique ID (e.g., `0`).
-
SpiJobList: Associate with `SpiJob_0`.
-
Configure SPI timing and pins
In the `SpiExternalDevice` tab, configure the external device parameters:
-
SpiBaudrate: Set the baud rate, such as `1000000` (1Mbps).
-
SpiClockPolarity: Select the clock polarity, such as `LOW` (low level when idle).
-
SpiClockPhase: Select the clock phase, such as `FIRST_EDGE` (sample on the first edge).
-
SpiCsPolarity: Select the chip select polarity, such as `ACTIVE_LOW` (active low).
In the `Port` module, configure the SPI pins (e.g., `PTD15` for SCK, `PTD16` for SIN, `PTD17` for SOUT, `PTD0` for CS):
-
Set `PortPinMode` to `PORT_PIN_MODE_SPI`.
-
Save configuration
Click `File > Save` to save all configuration changes.
**Notes**
– Ensure that the selected pins support LPSPI functionality, refer to the S32K148 pin multiplexing table.
– The SPI baud rate must match the rate supported by the slave device.
4. Generate Code
After configuration is complete, generate the corresponding C code files:
-
Execute code generation
– In the left navigation pane, right-click the project name and select `Generate Project`.
– EB Tresos Studio will generate `include` and `src` folders in the project directory, containing files related to SPI configuration (e.g., `Spi_Cfg.h` and `Spi_Cfg.c`).
-
Verify the generation results*
Open the generated directory (e.g., `C:\EB_Workspace\S32K148_Project\lighting_S32K144\cfg`) and confirm that files such as `Spi_Cfg.h` have been generated.
5. Integration and Debugging
Integrate the generated code into S32DS for compilation and debugging:
-
Import code into S32DS
– Open S32DS and create a new S32K148 project (or import an existing project).
– Copy the `include` and `src` folders generated by EB Tresos Studio into the S32DS project directory.
-
Write application code
In `main.c`, include the generated header file and call the MCAL SPI API for communication. For example:
#include "Spi.h"
uint8_t txData[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
uint8_t rxData[8] = {0};
int main(void) {
Spi_Init(&Spi_ConfigSet); // Initialize SPI configuration
while (1) {
Spi_SyncTransmit(0, txData, rxData, 8); // Synchronous data transmission, 0 indicates sequence ID
// Process rxData
}
}
`Spi_ConfigSet` is the configuration structure defined in the generated code, and `0` indicates the SPI sequence ID; the specific name and ID depend on your configuration.
-
Compile and Debug
– Compile the project in S32DS and check for errors.
– Connect the S32K148 development board (e.g., S32K148EVB) and connect the SPI pins to the slave device.
– Use a debugger (e.g., J-Link or PEmicro) to download the program and run it.
– Use a logic analyzer or oscilloscope to verify that the SPI signals are correct.
6. Notes
-
Pin Multiplexing Conflicts
Ensure that the configured SPI pins are not occupied by other modules (e.g., GPIO, UART), refer to the S32K148 pin multiplexing table.
-
Hardware Connections
– Ensure that the SPI pins are correctly connected to the slave device (SCK, MOSI, MISO, CS).
– Check that the power and ground of the slave device are functioning properly.
7. Conclusion
Through this document, you can configure the SPI module in the MCAL for the NXP S32K148 using EB Tresos Studio and generate usable code. This code can be compiled and debugged in S32DS, suitable for actual project development (e.g., communication with SPI sensors or memory).