Stm32Project/Src/spi3.c

119 lines
4.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "spi3.h"
/*********************************************************************************
___ _ _____ _____ _ _ _____ _____ _ __
/ _ \ | | |_ _|| ___|| \ | ||_ _|| ___|| | / /
/ /_\ \| | | | | |__ | \| | | | | |__ | |/ /
| _ || | | | | __| | . ` | | | | __| | \
| | | || |_____| |_ | |___ | |\ | | | | |___ | |\ \
\_| |_/\_____/\___/ \____/ \_| \_/ \_/ \____/ \_| \_/
* ******************************************************************************
* <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>ѧϰʹ<CFB0>ã<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><CEBA><EFBFBD>;
* ALIENTEK Pandora STM32L475 IOT<4F><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* SPI3<49><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <09><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>@ALIENTEK
* <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
* <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:2018/10/27
* <09><EFBFBD><E6B1BE>V1.0
* <09><>Ȩ<EFBFBD><C8A8><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD>ؾ<EFBFBD><D8BE><EFBFBD>
* Copyright(C) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿƼ<D3BF><C6BC><EFBFBD><EFBFBD>޹<EFBFBD>˾ 2014-2024
* All rights reserved
* ******************************************************************************
* <09><>ʼ<EFBFBD>
* ******************************************************************************/
SPI_HandleTypeDef SPI3_Handler; //SPI3<49><33><EFBFBD>
/**
* @brief SPI3<49><33>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBA3AC><EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>ģʽ
*
* @param void
*
* @return void
*/
void SPI3_Init(void)
{
SPI3_Handler.Instance=SPI3; //SPI3
SPI3_Handler.Init.Mode=SPI_MODE_MASTER; //<2F><><EFBFBD><EFBFBD>SPI<50><49><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ģʽ
SPI3_Handler.Init.Direction=SPI_DIRECTION_2LINES; //<2F><><EFBFBD><EFBFBD>SPI<50><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ:SPI<50><49><EFBFBD><EFBFBD>Ϊ˫<CEAA><CBAB>ģʽ
SPI3_Handler.Init.DataSize=SPI_DATASIZE_8BIT; //<2F><><EFBFBD><EFBFBD>SPI<50><49><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С:SPI<50><49><EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD>8λ֡<CEBB>
SPI3_Handler.Init.CLKPolarity=SPI_POLARITY_HIGH; //<2F><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC>ʱ<EFBFBD>ӵĿ<D3B5><C4BF><EFBFBD>״̬Ϊ<CCAC>ߵ<EFBFBD>ƽ
SPI3_Handler.Init.CLKPhase=SPI_PHASE_2EDGE; //<2F><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC>ʱ<EFBFBD>ӵĵڶ<C4B5><DAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><DDB1><EFBFBD><EFBFBD><EFBFBD>
SPI3_Handler.Init.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>
SPI3_Handler.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_2;//<2F><><EFBFBD><EFBFBD><E5B2A8><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>Ƶ<EFBFBD><C6B5>ֵ:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4>ƵֵΪ256
SPI3_Handler.Init.FirstBit=SPI_FIRSTBIT_MSB; //ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD>MSBλ<42><CEBB><EFBFBD><EFBFBD>LSBλ<42><CEBB>ʼ:<3A><><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD>MSBλ<42><CEBB>ʼ
SPI3_Handler.Init.TIMode=SPI_TIMODE_DISABLE; //<2F>ر<EFBFBD>TIģʽ
SPI3_Handler.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLE;//<2F>ر<EFBFBD>Ӳ<EFBFBD><D3B2>CRCУ<43><D0A3>
SPI3_Handler.Init.CRCPolynomial=7; //CRCֵ<43><D6B5><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>ʽ
HAL_SPI_Init(&SPI3_Handler);//<2F><>ʼ<EFBFBD><CABC>SPI3
__HAL_SPI_ENABLE(&SPI3_Handler); //ʹ<><CAB9>SPI3
}
/**
* @brief SPI3<49>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ʹ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param hspi SPI<50><49><EFBFBD>
*
* @return void
*/
// void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
// {
// GPIO_InitTypeDef GPIO_Initure;
// __HAL_RCC_GPIOB_CLK_ENABLE(); //ʹ<><CAB9>GPIOBʱ<42><CAB1>
// __HAL_RCC_SPI3_CLK_ENABLE(); //ʹ<><CAB9>SPI3ʱ<33><CAB1>
// //PB3.5
// GPIO_Initure.Pin=GPIO_PIN_3|GPIO_PIN_5;
// GPIO_Initure.Mode=GPIO_MODE_AF_PP; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// GPIO_Initure.Pull=GPIO_PULLUP; //<2F><><EFBFBD><EFBFBD>
// GPIO_Initure.Speed=GPIO_SPEED_FREQ_VERY_HIGH; //80Mhz
// GPIO_Initure.Alternate=GPIO_AF6_SPI3; //PB3.5<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪSPI3
// HAL_GPIO_Init(GPIOB,&GPIO_Initure);
// }
/**
* @brief SPI3 <20><>дһ<D0B4><D2BB><EFBFBD>ֽ<EFBFBD>
*
* @param TxData Ҫд<D2AA><D0B4><EFBFBD><EFBFBD>ֽ<EFBFBD>
*
* @return u8 <09><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
*/
u8 SPI3_ReadWriteByte(u8 TxData)
{
u8 Rxdata;
HAL_SPI_TransmitReceive(&SPI3_Handler,&TxData,&Rxdata,1, 1000);
return Rxdata; //<2F><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/**
* @brief SPI3 д<><D0B4>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
*
* @param TxData Ҫд<D2AA><D0B4><EFBFBD><EFBFBD>ֽ<EFBFBD>
* @param size д<><D0B4><EFBFBD>ֽڴ<D6BD>С
*
* @return u8 0:д<><D0B4>ɹ<EFBFBD>,<2C><><EFBFBD><EFBFBD><><D0B4>ʧ<EFBFBD><CAA7>
*/
u8 SPI3_WriteByte(u8 *TxData,u16 size)
{
return HAL_SPI_Transmit(&SPI3_Handler,TxData,size,1000);
}