GeekIMU/2.Firmware/STM32/Firmware/driver/spi.c

71 lines
3.4 KiB
C
Raw Normal View History

2024-11-09 21:39:20 +08:00
/******************** (C) COPYRIGHT 2020 GEEKIMU *******************************
* File Name : spi.c
* Current Version : V2.0 & ST 3.5.0
* Author : zhanli 719901725@qq.com & JustFeng.
* Date of Issued : 2019.5.22 zhanli : Create
* Comments : SPI<EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
********************************************************************************/
#include "stm32f10x_spi.h"
#include "spi.h"
#include "sys.h"
/**----------------------------------------------------------------------
* Function : MEMS_SPI_Init
* Description : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD>SPI<EFBFBD>ӿ<EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2019/5/04 zhanli
*---------------------------------------------------------------------**/
void MEMS_SPI_Init()
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* <20><><EFBFBD><EFBFBD>GPIOB<4F><42>ʱ<EFBFBD><CAB1><EFBFBD>Լ<EFBFBD> SPI2<49><32>ʱ<EFBFBD><CAB1> */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/* <20><><EFBFBD><EFBFBD>SPI<50><49>SCK<43><4B>MISO<53><4F>MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // <20><><EFBFBD><EFBFBD>SPI<50><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ:SPI<50><49><EFBFBD><EFBFBD>Ϊ˫<CEAA><CBAB>˫<EFBFBD><CBAB>ȫ˫<C8AB><CBAB>
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // <20><><EFBFBD><EFBFBD>SPI<50><49><EFBFBD><EFBFBD>ģʽ:<3A><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // <20><><EFBFBD><EFBFBD>SPI<50><49><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С:SPI<50><49><EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD>8λ֡<CEBB>
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // ѡ<><D1A1><EFBFBD>˴<EFBFBD><CBB4><EFBFBD>ʱ<EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>̬:ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>ոߵ<D5B8>ƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // <20><><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD>ڵ<EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // NSS<53>ź<EFBFBD><C5BA><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD>NSS<53>ܽţ<DCBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>SSIλ<49><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:<3A>ڲ<EFBFBD>NSS<53>ź<EFBFBD><C5BA><EFBFBD>SSIλ<49><CEBB><EFBFBD><EFBFBD>
//SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; // <20><><EFBFBD>õ<EFBFBD>DK2<4B>IJ<EFBFBD><C4B2><EFBFBD>
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // <20><><EFBFBD>õ<EFBFBD>9250<35><30><EFBFBD><EFBFBD>
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD>MSBλ<42><CEBB><EFBFBD><EFBFBD>LSBλ<42><CEBB>ʼ:<3A><><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD>MSBλ<42><CEBB>ʼ
SPI_InitStructure.SPI_CRCPolynomial = 7; // CRCֵ<43><D6B5><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>ʽ
SPI_Init(SPI2, &SPI_InitStructure); // <20><><EFBFBD><EFBFBD>SPI_InitStruct<63><74>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SPIx<49>Ĵ<EFBFBD><C4B4><EFBFBD>
SPI_Cmd(SPI2, ENABLE); // ʹ<><CAB9>SPI<50><49><EFBFBD><EFBFBD>
}
/**----------------------------------------------------------------------
* Function : SPI2_ReadWriteByte
* Description : <EFBFBD><EFBFBD>дSPI2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>Ҫ<EFBFBD>Ƕ<EFBFBD>дMPU6500 + LIS3MDL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
uint8_t SPI2_ReadWriteByte(uint8_t TxData)
{
uint8_t retry = 0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) {
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI2, TxData);
retry = 0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) {
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData(SPI2);
}