efm32 硬件SPI
void SPI_setup(uint8_t spiNumber, uint8_t location, bool master)
{
USART_TypeDef *spi;
/* Determining USART */
switch (spiNumber)
{
case 0:
spi = USART0;
break;
case 1:
spi = USART1;
break;
case 2:
// spi = USART2;
break;
default:
return;
}
//USART_Reset(spi);
/* Setting baudrate */
spi->CLKDIV = 128 * (SPI_PERCLK_FREQUENCY / SPI_BAUDRATE - 2);
/* Configure SPI */
/* Using synchronous (SPI) mode*/
//spi->CTRL = USART_CTRL_SYNC;//|USART_CTRL_CLKPHA;
spi->CTRL = USART_CTRL_SYNC ;//|USART_CTRL_CLKPHA;//|USART_CTRL_CLKPOL ;
//spi->FRAME|=USART_FRAME_DATABITS_EIGHT;
/* Clearing old transfers/receptions, and disabling interrupts */
spi->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX;
spi->IEN = 0;
/* Enabling pins and setting location */
spi->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | USART_ROUTE_CLKPEN | USART_ROUTE_CSPEN | (location << 8);
/* Set GPIO config to slave */
/* Set to master and to control the CS line */
if (master)
{
/* Enabling Master, TX and RX */
spi->CMD = USART_CMD_MASTEREN | USART_CMD_TXEN | USART_CMD_RXEN;
spi->CTRL |= USART_CTRL_AUTOCS;
/* 数据传输高位在前 */
spi->CTRL |=USART_CTRL_MSBF;
/* Set GPIO config to master */
gpioModeMosi = gpioModePushPull;
gpioModeMiso = gpioModePushPull;
gpioModeCs = gpioModePushPull;
gpioModeClk = gpioModePushPull;
}
else
{
/* Enabling TX and RX */
spi->CMD = USART_CMD_TXEN | USART_CMD_RXEN;
/* Set GPIO config to slave */
gpioModeMosi = gpioModeInput;
gpioModeMiso = gpioModePushPull;
gpioModeCs = gpioModeInput;
gpioModeClk = gpioModeInput;
}
/* Clear previous interrupts */
spi->IFC = _USART_IFC_MASK;
/* IO configuration */
switch(spiNumber)
{
case 0: switch(location)
{
case 0: /* IO configuration (USART 0, Location #0) */
GPIO_PinModeSet(gpioPortE, 10, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortE, 11, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortE, 13, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortE, 12, gpioModeClk, 0); /* Clock */
break;
case 1: /* IO configuration (USART 0, Location #1) */
GPIO_PinModeSet(gpioPortE, 7, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortE, 6, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortE, 4, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortE, 5, gpioModeClk, 0); /* Clock */
break;
case 2: /* IO configuration (USART 0, Location #2) */
GPIO_PinModeSet(gpioPortC, 11, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortC, 10, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortC, 8, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortC, 9, gpioModeClk, 0); /* Clock */
break;
default: break;
}
break;
case 1: switch(location)
{
case 0: /* IO configuration (USART 1, Location #0) */
GPIO_PinModeSet(gpioPortC, 0, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortC, 1, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortB, 8, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortB, 7, gpioModeClk, 0); /* Clock */
break;
case 1: /* IO configuration (USART 1, Location #1) */
GPIO_PinModeSet(gpioPortD, 0, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortD, 1, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortD, 3, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortD, 2, gpioModeClk, 0); /* Clock */
break;
default: break;
}
break;
case 2: switch(location)
{
case 0: /* IO configuration (USART 2, Location #0) */
GPIO_PinModeSet(gpioPortC, 2, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortC, 3, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortC, 5, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortC, 4, gpioModeClk, 0); /* Clock */
break;
case 1: /* IO configuration (USART 2, Location #1) */
GPIO_PinModeSet(gpioPortB, 3, gpioModeMosi, 0); /* MOSI */
GPIO_PinModeSet(gpioPortB, 4, gpioModeMiso, 0); /* MISO */
GPIO_PinModeSet(gpioPortB, 6, gpioModeCs, 0); /* CS */
GPIO_PinModeSet(gpioPortB, 5, gpioModeClk, 0); /* Clock */
break;
default: break;
}
break;
default: break;
}
}
我这里对其初始化,可是在示波器看到cs输出波形不对,请大家帮忙解决一下
提问者:sdfsgsd
地点:-
浏览次数:6401
提问时间:03-12 08:19
我有更好的答案