#include "Spi.h"
|
|
void Spi_Init(void)
|
{
|
SPI_InitTypeDef SPI_InitStructure;
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
/* Enable GPIO clock */
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
|
/* Enable SPI clock */
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
|
/* SPI GPIO setup */
|
// SPIx SCK and MOSI pin setup
|
GPIO_InitStructure.GPIO_Pin = SPIx_SCK | SPIx_MOSI;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
|
|
// SPIx MISO pin setup
|
GPIO_InitStructure.GPIO_Pin = SPIx_MISO;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
|
|
// SPIx CS pin setup
|
GPIO_InitStructure.GPIO_Pin = SPIx_CS;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_Init(SPIx_CS_GPIO, &GPIO_InitStructure);
|
|
// Set CS high
|
GPIO_SetBits(SPIx_CS_GPIO, SPIx_CS);
|
|
|
SPI_I2S_DeInit(SPIx);
|
|
// SPIx Mode setup
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
SPI_InitStructure.SPI_CPOL = SPIx_CPOL;
|
SPI_InitStructure.SPI_CPHA = SPIx_CPHA;
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPIx_PRESCALER_SLOW;
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
|
SPI_Init(SPIx, &SPI_InitStructure);
|
|
// Disable SPIx SS Output
|
SPI_SSOutputCmd(SPIx, DISABLE);
|
|
// Enable SPIx
|
SPI_Cmd(SPIx, ENABLE);
|
}
|
|
|
void Spi_ChangePrescaler(uint16_t scaling_factor)
|
{
|
uint16_t tmpreg = 0;
|
|
/* Get the SPIx CR1 value */
|
tmpreg = SPIx->CR1;
|
|
/*clear the scaling bits*/
|
tmpreg &= 0xFFC7;
|
|
/*set the scaling bits*/
|
tmpreg |= scaling_factor;
|
|
/* Write to SPIx CR1 */
|
SPIx->CR1 = tmpreg;
|
}
|