102 lines
4.4 KiB
C
102 lines
4.4 KiB
C
#include "spi2.h"
|
|
#include "delay.h"
|
|
#include "includes.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); //Enables GPIOB clock
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE); //Enable SPI2 clock
|
|
|
|
//GPIOB13, 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 SPI2
|
|
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); //PB14 multiplexing SPI2
|
|
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //PB15 multiplexing SPI2
|
|
|
|
|
|
//Here only for the SPI port initialization
|
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE ); //Reset SPI2
|
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE); //Stop reset SPI2
|
|
|
|
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_16b; //Set SPI data size: send receive 16-bit SPI frame structure
|
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //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, 0, 100); //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 SPI2 speed
|
|
SPI_Cmd(SPI2,ENABLE);
|
|
}
|
|
//SPI1 read and write bytes
|
|
//TxData: the bytes to write
|
|
//RxData: the words to read pointer
|
|
//t_us: timeout(us)
|
|
//Return value: result (0: OK, 1: Tx fail, 2: Rx fail)
|
|
u8 SPI2_ReadWriteByte(u16 TxData, u16* RxData, u32 t_us)
|
|
{
|
|
u32 timeout = t_us;
|
|
u16 rd;
|
|
|
|
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //Waiting to be sent
|
|
{
|
|
if (timeout-- == 0) return 1;
|
|
delay_us(2);
|
|
}
|
|
SPI_I2S_SendData(SPI2, TxData); //Send a byte data through peripheral SPIx
|
|
|
|
timeout = t_us;
|
|
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //Wait to receive a one byte
|
|
{
|
|
if (timeout-- == 0) return 2;
|
|
delay_us(2);
|
|
}
|
|
rd = SPI_I2S_ReceiveData(SPI2); //Returned by SPIx recently received data
|
|
|
|
if (RxData != NULL)
|
|
*RxData = rd;
|
|
|
|
return 0;
|
|
}
|