S32K148 SPI Peripheral Configuration for Bare Metal Development

SPI Driver Implementation Steps

  1. Configure SPI pin multiplexing
  2. Enable peripheral clock controller for SPI
  3. Disable SPI module for configuration
  4. Set up control registers: configuration, clock, transmit, and receive
  5. Enable SPI modulee operatoin

SPI Module Initialization

void configure_spi_module(void)
{
    // GPIO pin configuration for SPI
    initialize_gpio_pin(GPIO_PORT_E, 6, 1, 1);
    PORT_E->PIN_CONTROL[2] |= (1 << 9);
    PORT_E->PIN_CONTROL[1] |= (1 << 9);
    PORT_E->PIN_CONTROL[0] |= (1 << 9);
    
    // Disable clock for peripheral configuration
    PERIPHERAL_CLOCK_CONTROLLER->CLOCK_REGISTERS[LPSPI0_CLOCK_INDEX] = 0;
    
    // Configure peripheral clock source
    PERIPHERAL_CLOCK_CONTROLLER->CLOCK_REGISTERS[LPSPI0_CLOCK_INDEX] = 0xC6000000;
    
    // Module disable for configuration
    LPSPI0->CONTROL = 0x00000000;
    LPSPI0->INTERRUPT_ENABLE = 0x00000000;
    LPSPI0->DMA_ENABLE = 0x00000000;
    LPSPI0->CONFIG_0 = 0x00000000;
    
    // Master mode configuration settings
    LPSPI0->CONFIG_1 = 0x00000001;
    
    // Transmit command register setup
    LPSPI0->TRANSMIT_CONTROL = 0xC8000007;
    
    // Clock configuration parameters
    LPSPI0->CLOCK_CONTROL = 0x04040812;
    
    // FIFO control settings
    LPSPI0->FIFO_CONTROL = 0x00000003;
    
    // Enable module operation
    LPSPI0->CONTROL = 0x00000009;
}

SPI Data Transfer Funcsion

uint8_t spi_transfer_byte(uint8_t transmit_data)
{
    uint8_t receive_data;
    
    // Wait for transmit FIFO availability
    while ((LPSPI0->STATUS & 0x1) == 0);
    
    // Load data into transmit register
    LPSPI0->TRANSMIT_DATA = transmit_data;
    
    // Wait for receive data availability
    while (((LPSPI0->STATUS & 0x2) >> 1) == 0);
    
    // Retrieve received data
    receive_data = LPSPI0->RECEIVE_DATA;
    
    // Clear transmit data flag
    LPSPI0->STATUS |= 0x1;
    
    // Clear receive data flag
    LPSPI0->STATUS |= 0x2;
    
    return receive_data;
}

Tags: S32K148 SPI bare-metal microcontroller embedded-systems

Posted on Mon, 28 Sep 2026 16:10:09 +0000 by rubberjohn