S32K312 Lpspi通信例子
时间: 2023-12-03 22:04:41 浏览: 197
根据提供的引用内容,我无法确定您需要的是哪个厂家的S32K312芯片的Lpspi通信例子。但是,我可以为您提供一个通用的Lpspi通信例子,您可以根据自己的需要进行修改。
```c
#include "fsl_lpspi.h"
#include "fsl_debug_console.h"
#define EXAMPLE_LPSPI_MASTER_BASEADDR (LPSPI1)
#define EXAMPLE_LPSPI_MASTER_IRQN (LPSPI1_IRQn)
#define EXAMPLE_LPSPI_MASTER_IRQHandler (LPSPI1_IRQHandler)
#define EXAMPLE_LPSPI_MASTER_PCS_FOR_INIT (kLPSPI_Pcs0)
#define EXAMPLE_LPSPI_MASTER_PCS_FOR_TRANSFER (kLPSPI_MasterPcs0)
#define EXAMPLE_LPSPI_DEALY_COUNT 0xfffffU
#define TRANSFER_SIZE 64U /*! Transfer dataSize */
/*******************************************************************************
* Prototypes
******************************************************************************/
/* LPSPI user callback */
void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_handle_t *handle, status_t status, void *userData);
/*******************************************************************************
* Variables
******************************************************************************/
uint8_t masterRxData[TRANSFER_SIZE] = {0};
uint8_t masterTxData[TRANSFER_SIZE] = {0};
volatile bool isMasterTransferCompleted = false;
lpspi_master_handle_t g_m_handle;
SemaphoreHandle_t lpspi_sem;
/*******************************************************************************
* Code
******************************************************************************/
void EXAMPLE_LPSPI_MASTER_IRQHandler(void)
{
LPSPI_MasterTransferHandleIRQ(EXAMPLE_LPSPI_MASTER_BASEADDR, &g_m_handle);
}
void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_handle_t *handle, status_t status, void *userData)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (status == kStatus_Success)
{
PRINTF("LPSPI master transfer completed successfully.\r\n");
}
else
{
PRINTF("LPSPI master transfer completed with error.\r\n");
}
isMasterTransferCompleted = true;
xSemaphoreGiveFromISR(lpspi_sem, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/*!
* @brief Main function
*/
int main(void)
{
lpspi_master_config_t masterConfig;
uint32_t sourceClock = 0U;
lpspi_transfer_t masterXfer;
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
PRINTF("LPSPI one board master example started!\r\n");
/* Initialize LPSPI master */
/*
* masterConfig->baudRate_Bps = 500000U;
* masterConfig->bitsPerFrame = 8U;
* masterConfig->cpol = kLPSPI_ClockPolarityActiveHigh;
* masterConfig->cpha = kLPSPI_ClockPhaseFirstEdge;
* masterConfig->direction = kLPSPI_MsbFirst;
* masterConfig->pinCfg = kLPSPI_SdiInSdoOut;
* masterConfig->dataOutConfig = kLpspiDataOutRetained;
* masterConfig->txFifoWatermark = 0;
* masterConfig->rxFifoWatermark = 0;
* masterConfig->txDmaSrc = kLpspiDmaSrcDMA;
* masterConfig->rxDmaSrc = kLpspiDmaSrcDMA;
* masterConfig->whichPcs = kLPSPI_Pcs0;
* masterConfig->polarityFlags = kLPSPI_MasterPolarity0;
* masterConfig->isPcsContinuous = false;
*/
LPSPI_MasterGetDefaultConfig(&masterConfig);
masterConfig.baudRate_Bps = 500000U;
masterConfig.whichPcs = EXAMPLE_LPSPI_MASTER_PCS_FOR_INIT;
sourceClock = LPSPI_MASTER_CLK_FREQ;
LPSPI_MasterInit(EXAMPLE_LPSPI_MASTER_BASEADDR, &masterConfig, sourceClock);
/* Create Semaphore for communication*/
lpspi_sem = xSemaphoreCreateBinary();
/* Set up transfer */
masterXfer.txData = masterTxData;
masterXfer.rxData = masterRxData;
masterXfer.dataSize = TRANSFER_SIZE;
masterXfer.configFlags = kLPSPI_MasterPcsContinuous | kLPSPI_MasterByteSwap;
/* Start master transfer */
isMasterTransferCompleted = false;
LPSPI_MasterTransferNonBlocking(EXAMPLE_LPSPI_MASTER_BASEADDR, &g_m_handle, &masterXfer);
/* Wait for transfer to complete */
if (xSemaphoreTake(lpspi_sem, portMAX_DELAY) == pdTRUE)
{
PRINTF("LPSPI master transfer completed.\r\n");
}
/* Deinit the LPSPI. */
LPSPI_Deinit(EXAMPLE_LPSPI_MASTER_BASEADDR);
while (1)
{
}
}
```
阅读全文