帮我基于LPC5516 的 spi_dma_b2b 例程 写一个 8 路 SPI 同时通过 DMA0 传输数据的 C语言程序
时间: 2023-06-02 17:07:29 浏览: 248
好的,我知道了。关于你的第一个问题,以下是基于LPC5516的spi_dma_b2b例程修改后的8路SPI同时通过DMA0传输数据的C语言程序:
```c
#include "board.h"
#include "periph_spi_dma.h"
#define BUFFER_SIZE 32
/* SPI master select, to select slave to communicate */
#define LPC_SLAVE_A_SPI_PORT 0
#define LPC_SLAVE_A_SPI_PIN 15
#define LPC_SLAVE_B_SPI_PORT 0
#define LPC_SLAVE_B_SPI_PIN 16
#define LPC_SLAVE_C_SPI_PORT 0
#define LPC_SLAVE_C_SPI_PIN 17
#define LPC_SLAVE_D_SPI_PORT 0
#define LPC_SLAVE_D_SPI_PIN 18
#define LPC_SLAVE_E_SPI_PORT 0
#define LPC_SLAVE_E_SPI_PIN 19
#define LPC_SLAVE_F_SPI_PORT 0
#define LPC_SLAVE_F_SPI_PIN 20
#define LPC_SLAVE_G_SPI_PORT 0
#define LPC_SLAVE_G_SPI_PIN 21
#define LPC_SLAVE_H_SPI_PORT 0
#define LPC_SLAVE_H_SPI_PIN 22
/* SPI master and slave configuration */
static SPI_CONFIG_T mstCfg0 = {
SPI_MODE_MASTER, /* Transfer mode */
0, /* Bus configuration: ignored in master mode */
SPI_CLOCK_CPHA0_CPOL0, /* Clock mode */
SPI_DATA_MSB_FIRST, /* Data order: MSB first */
SPI_SSEL_ACTIVE_LO, /* Slave select is active low */
8 /* SPI clock divider - not used in master mode */
};
static SPI_DMA_CONFIG_T dmaCfg = {
SPI_DMA_CHAN_TX_RX, /* DMA configuration */
SPI_DMA_INT_EN, /* DMA interrupt enable */
SPI_DMA_INTSTAT_TXERR, /* DMA interrupt status */
BUFFER_SIZE /* DMA transfer size */
};
static SPI_DMA_HANDLE_T spiHandle;
/* Sample data buffer */
static uint32_t txBuf[ BUFFER_SIZE ];
static uint32_t rxBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveABuf[ BUFFER_SIZE ];
static uint32_t rxSlaveBBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveCBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveDBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveEBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveFBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveGBuf[ BUFFER_SIZE ];
static uint32_t rxSlaveHBuf[ BUFFER_SIZE ];
/* Function prototypes */
static void initSpiMaster( void );
static void initSpiSlave( uint8_t port, uint8_t pin );
int main(void)
{
/* Initialize board */
Board_Init();
/* Initialize master and slaves */
initSpiMaster();
initSpiSlave( LPC_SLAVE_A_SPI_PORT, LPC_SLAVE_A_SPI_PIN );
initSpiSlave( LPC_SLAVE_B_SPI_PORT, LPC_SLAVE_B_SPI_PIN );
initSpiSlave( LPC_SLAVE_C_SPI_PORT, LPC_SLAVE_C_SPI_PIN );
initSpiSlave( LPC_SLAVE_D_SPI_PORT, LPC_SLAVE_D_SPI_PIN );
initSpiSlave( LPC_SLAVE_E_SPI_PORT, LPC_SLAVE_E_SPI_PIN );
initSpiSlave( LPC_SLAVE_F_SPI_PORT, LPC_SLAVE_F_SPI_PIN );
initSpiSlave( LPC_SLAVE_G_SPI_PORT, LPC_SLAVE_G_SPI_PIN );
initSpiSlave( LPC_SLAVE_H_SPI_PORT, LPC_SLAVE_H_SPI_PIN );
/* Start SPI transfers */
SPI_DMA_TRANSFER_T transfer;
transfer.pTxData = txBuf;
transfer.pRxData[0] = rxBuf;
transfer.pRxData[1] = rxSlaveABuf;
transfer.pRxData[2] = rxSlaveBBuf;
transfer.pRxData[3] = rxSlaveCBuf;
transfer.pRxData[4] = rxSlaveDBuf;
transfer.pRxData[5] = rxSlaveEBuf;
transfer.pRxData[6] = rxSlaveFBuf;
transfer.pRxData[7] = rxSlaveGBuf;
transfer.pRxData[8] = rxSlaveHBuf;
transfer.dataLength = BUFFER_SIZE;
SPI_DmaTransfer( &spiHandle, &dmaCfg, &transfer );
while (1) {}
}
static void initSpiMaster( void )
{
/* Initialize SPI0 as master with SSEL0 */
Chip_SPI_Init( LPC_SPI0 );
Chip_SPI_ConfigureSPI( LPC_SPI0, &mstCfg0 );
Chip_Clock_EnablePeriphClock( SYSCTL_CLOCK_SPI0 );
Chip_SPI_Enable( LPC_SPI0 );
/* Set SPI 0.15 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_A_SPI_PORT, LPC_SLAVE_A_SPI_PIN);
/* Set SPI 0.16 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_B_SPI_PORT, LPC_SLAVE_B_SPI_PIN);
/* Set SPI 0.17 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_C_SPI_PORT, LPC_SLAVE_C_SPI_PIN);
/* Set SPI 0.18 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_D_SPI_PORT, LPC_SLAVE_D_SPI_PIN);
/* Set SPI 0.19 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_E_SPI_PORT, LPC_SLAVE_E_SPI_PIN);
/* Set SPI 0.20 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_F_SPI_PORT, LPC_SLAVE_F_SPI_PIN);
/* Set SPI 0.21 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_G_SPI_PORT, LPC_SLAVE_G_SPI_PIN);
/* Set SPI 0.22 as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LPC_SLAVE_H_SPI_PORT, LPC_SLAVE_H_SPI_PIN);
}
static void initSpiSlave( uint8_t port, uint8_t pin )
{
/* Set SPI slave select as GPIO output */
Chip_GPIO_SetPinDIROutput(LPC_GPIO, port, pin);
/* Set SPI slave select high to de-select slave */
Chip_GPIO_SetPinState(LPC_GPIO, port, pin, true);
}
```
请注意,以上程序仅供参考,请根据实际要求进行修改和调试。
阅读全文