Files

84 lines
4.1 KiB
C

#include "spi.h"
//////////////////////////////////////////////////////////////////////////////////
//This program is for educational purposes only, without the author's permission, may not be used for any other purpose
//ALIENTEK STM32F4 Explorer Board
//SPI Driver code
//Hot Atomic @ALIENTEK
//Forum: www.openedv.com
//Date modified: 2014/5/6
//Version: V1.0
//All rights reserved pirated reserved.
//Copyright(C) Guangzhou wing electronic technology limited company 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
//Following is the SPI module initialization code, and configured to master mode
//SPI port initialization
//Needle is SPI1 initialization
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // Enable GPIOB clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE); // Enable SPI2 clock
//GPIO B13, B14, B15 initialization settings
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13
| GPIO_Pin_14
| GPIO_Pin_15
; // PB13~15 reuse function output
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Multiplexing function
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // Push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // Pull-up
GPIO_Init(GPIOB, &GPIO_InitStructure); // Initialize
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13, GPIO_AF_SPI2); // PB13 multiplexing SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14, GPIO_AF_SPI2); // PB14 multiplexing SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15, GPIO_AF_SPI2); // PB15 multiplexing SPI1
//Here only for the SPI port initialization
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // Set SPI unidirectional or bi-directional data modes: SPI is set to two-lane bi-directional full-duplex
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // Set SPI mode: set master SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // Set SPI data size: send receive 8-bit SPI frame structure
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // Serial synchronous clock idle high
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // Serial synchronous clocks second jump (increase or decrease) the data is sampled
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // NSS is set by hardware (NSS pins) or software (using SSI) management: internal NSS SSI signal level control
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // Define baud rate predissociation spectrum of values: baud rate predissociation spectrum value of 256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // Specifies the data transmission starting from MSB or LSB bit: data transmission starting from the MSB
SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC calculate polynomials
SPI_Init(SPI2, &SPI_InitStructure); // According to the parameters specified in SPI_InitStruct to initialize peripherals SPIx register
SPI_Cmd(SPI2, ENABLE); // Make SPI peripherals
SPI2_ReadWriteByte(0xff); // To start the transfer
}
//SPI1 speed setting function
//SpeedSet:0~7
//SPI speed =fAPB2/2^ (SpeedSet+1)
//FAPB2 84Mhz
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI2->CR1 &= 0XFFC7;
SPI2->CR1 |= SPI_BaudRatePrescaler; //Set SPI1 speed
SPI_Cmd(SPI2, ENABLE);
}
//SPI1 read and write bytes
//TxData: the bytes to write
//Return value: reads a byte
u8 SPI2_ReadWriteByte(u8 TxData)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}//Waiting to be sent
SPI_I2S_SendData(SPI2, TxData); //Send a byte data through peripheral SPIx
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET){} //Wait to receive a one byte
return SPI_I2S_ReceiveData(SPI2); //Returned by SPIx recently received data
}