void SpiFrequency(Spi_t *obj, uint32_t hz) { uint32_t spiSourceClock; spiSourceClock = CLOCK_SYS_GetSpiFreq(obj->instance); /* Disable Spi module */ SPI_HAL_Disable(obj->Spi); SPI_HAL_SetBaud(obj->Spi, hz, spiSourceClock); /* Enable Spi module */ SPI_HAL_Enable(obj->Spi); }
/*FUNCTION********************************************************************** * * Function Name : SPI_DRV_MasterConfigureBus * Description : Configures the SPI port to access a device on the bus. * The term "device" is used to indicate the SPI device for which the SPI master is communicating. * The user has two options to configure the device parameters: either pass in the * pointer to the device configuration structure to the desired transfer function (see * SPI_DRV_MasterTransferDataBlocking or SPI_DRV_MasterTransferData) or pass it in to the * SPI_DRV_MasterConfigureBus function. The user can pass in a device structure to the transfer * function which contains the parameters for the bus (the transfer function will then call * this function). However, the user has the option to call this function directly especially * to get the calculated baud rate, at which point they may pass in NULL for the device * struct in the transfer function (assuming they have called this configure bus function * first). * *END**************************************************************************/ void SPI_DRV_MasterConfigureBus(uint32_t instance, const spi_master_user_config_t * device, uint32_t * calculatedBaudRate) { assert(device); /* instantiate local variable of type spi_master_state_t and point to global state */ spi_master_state_t * spiState = (spi_master_state_t *)g_spiStatePtr[instance]; SPI_Type *base = g_spiBase[instance]; /* Configure the bus to access the provided device.*/ *calculatedBaudRate = SPI_HAL_SetBaud(base, device->bitsPerSec, spiState->spiSourceClock); SPI_HAL_SetDataFormat(base, device->polarity, device->phase, device->direction); #if FSL_FEATURE_SPI_16BIT_TRANSFERS SPI_HAL_Set8or16BitMode(base, device->bitCount); #endif }
/** * @brief 初始化SPI模块 * @param SPI_InitStruct :spi初始化配置结构体 * @retval None */ void SPI_Init(SPI_InitTypeDef* SPI_InitStruct) { uint32_t clock; /* enable clock gate */ *(uint32_t*)SIM_SPIClockGateTable[SPI_InitStruct->instance].addr |= SIM_SPIClockGateTable[SPI_InitStruct->instance].mask; /* stop SPI */ SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_SPE_MASK; /* master or slave */ switch(SPI_InitStruct->mode) { case kSPI_Master: SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_MSTR_MASK; break; case kSPI_Slave: SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_MSTR_MASK; break; default: break; } /* bit order */ switch(SPI_InitStruct->bitOrder) { case kSPI_MSBFirst: SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_LSBFE_MASK; break; case kSPI_LSBFirst: SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_LSBFE_MASK; break; default: break; } /* clock parity */ switch(SPI_InitStruct->frameFormat) { case kSPI_CPOL0_CPHA0: SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPHA_MASK; SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPOL_MASK; break; case kSPI_CPOL0_CPHA1: SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_CPHA_MASK; SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPOL_MASK; break; case kSPI_CPOL1_CPHA0: SPI_InstanceTable[SPI_InitStruct->instance]->C1 &= ~SPI_C1_CPHA_MASK; SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_CPOL_MASK; break; case kSPI_CPOL1_CPHA1: SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_CPHA_MASK; SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_CPOL_MASK; break; default: break; } /* config SS as PCS0 output */ SPI_InstanceTable[SPI_InitStruct->instance]->C2 |= SPI_C2_MODFEN_MASK; SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_SSOE_MASK; /* baudrate */ if(SPI_InitStruct->instance == HW_SPI0) { CLOCK_GetClockFrequency(kBusClock, &clock); } else { CLOCK_GetClockFrequency(kSystemClock, &clock); } SPI_HAL_SetBaud(SPI_InitStruct->instance, SPI_InitStruct->baudrate, clock); /* enable SPI */ SPI_InstanceTable[SPI_InitStruct->instance]->C1 |= SPI_C1_SPE_MASK; }