141 lines
5.1 KiB
C
141 lines
5.1 KiB
C
/**
|
|
******************************************************************************
|
|
* @file spi.c
|
|
* @author AMG Energy Solution Team
|
|
* @version V1.0.0
|
|
* @date 07-Feb-2022
|
|
* @brief This file provides all the code of the spi communication.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
|
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
|
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
|
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
|
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
|
*
|
|
* <h2><center>© COPYRIGHT 2022 AMG</center></h2>
|
|
******************************************************************************
|
|
*/
|
|
|
|
/** @addtogroup mBMS2
|
|
* @{
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "spi.h"
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* SPI_SCK Port/Pin definition */
|
|
#define SPI_SCK_PORT (PortB)
|
|
#define SPI_SCK_PIN (Pin13)
|
|
#define SPI_SCK_FUNC (Func_Spi3_Sck)
|
|
|
|
/* SPI_MOSI Port/Pin definition */
|
|
#define SPI_MOSI_PORT (PortB)
|
|
#define SPI_MOSI_PIN (Pin15)
|
|
#define SPI_MOSI_FUNC (Func_Spi3_Mosi)
|
|
|
|
/* SPI_MISO Port/Pin definition */
|
|
#define SPI_MISO_PORT (PortB)
|
|
#define SPI_MISO_PIN (Pin14)
|
|
#define SPI_MISO_FUNC (Func_Spi3_Miso)
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
//Following is the SPI module initialization code, and configured to master mode
|
|
//SPI port initialization
|
|
//Needle is SPI2 initialization
|
|
void Spi_Config(void)
|
|
{
|
|
stc_spi_init_t stcSpiInit;
|
|
stc_port_init_t stcPortInit;
|
|
|
|
/* configuration structure initialization */
|
|
MEM_ZERO_STRUCT(stcPortInit);
|
|
|
|
stcPortInit.enPinMode = Pin_Mode_Out;
|
|
stcPortInit.enExInt = Disable;
|
|
stcPortInit.enPullUp = Enable;
|
|
|
|
/* SPI Port/Pin initialization */
|
|
PORT_Init(SPI_MISO_PORT, SPI_MISO_PIN, &stcPortInit);
|
|
|
|
stcPortInit.enPinMode = Pin_Mode_In;
|
|
|
|
PORT_Init(SPI_MOSI_PORT, SPI_MOSI_PIN, &stcPortInit);
|
|
|
|
/* configuration structure initialization */
|
|
MEM_ZERO_STRUCT(stcSpiInit);
|
|
|
|
/* Configuration peripheral clock */
|
|
PWC_Fcg1PeriphClockCmd(SPI_UNIT_CLOCK, Enable);
|
|
|
|
/* Configuration SPI pin */
|
|
PORT_SetFunc(SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_FUNC, Disable);
|
|
PORT_SetFunc(SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_FUNC, Disable);
|
|
PORT_SetFunc(SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_FUNC, Disable);
|
|
|
|
/* Configuration SPI structure */
|
|
stcSpiInit.enClkDiv = SpiClkDiv2;
|
|
stcSpiInit.enFrameNumber = SpiFrameNumber1;
|
|
stcSpiInit.enDataLength = SpiDataLengthBit8;
|
|
stcSpiInit.enFirstBitPosition = SpiFirstBitPositionMSB;
|
|
stcSpiInit.enSckPolarity = SpiSckIdleLevelLow;
|
|
stcSpiInit.enSckPhase = SpiSckOddSampleEvenChange;
|
|
stcSpiInit.enReadBufferObject = SpiReadReceiverBuffer;
|
|
stcSpiInit.enWorkMode = SpiWorkMode3Line;
|
|
stcSpiInit.enTransMode = SpiTransFullDuplex;
|
|
stcSpiInit.enCommAutoSuspendEn = Disable;
|
|
stcSpiInit.enModeFaultErrorDetectEn = Disable;
|
|
stcSpiInit.enParitySelfDetectEn = Disable;
|
|
stcSpiInit.enParityEn = Disable;
|
|
stcSpiInit.enParity = SpiParityEven;
|
|
stcSpiInit.enMasterSlaveMode = SpiModeMaster;
|
|
stcSpiInit.stcDelayConfig.enSsSetupDelayOption = SpiSsSetupDelayCustomValue;
|
|
stcSpiInit.stcDelayConfig.enSsSetupDelayTime = SpiSsSetupDelaySck1;
|
|
stcSpiInit.stcDelayConfig.enSsHoldDelayOption = SpiSsHoldDelayCustomValue;
|
|
stcSpiInit.stcDelayConfig.enSsHoldDelayTime = SpiSsHoldDelaySck1;
|
|
stcSpiInit.stcDelayConfig.enSsIntervalTimeOption = SpiSsIntervalCustomValue;
|
|
stcSpiInit.stcDelayConfig.enSsIntervalTime = SpiSsIntervalSck6PlusPck2;
|
|
|
|
SPI_Init(SPI_UNIT, &stcSpiInit);
|
|
SPI_Cmd(SPI_UNIT, Enable);
|
|
}
|
|
|
|
/**
|
|
*******************************************************************************
|
|
** \brief SPI flash write byte function
|
|
**
|
|
** \param [in] u8Data SPI write data to flash
|
|
**
|
|
** \retval uint8_t SPI receive data from flash
|
|
**
|
|
******************************************************************************/
|
|
uint8_t SpiFlash_WriteReadByte(uint8_t u8Data)
|
|
{
|
|
uint8_t u8Byte = 0;
|
|
|
|
/* Wait tx buffer empty */
|
|
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSendBufferEmpty))
|
|
{
|
|
}
|
|
/* Send data */
|
|
SPI_SendData8(SPI_UNIT, u8Data);
|
|
/* Wait rx buffer full */
|
|
while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagReceiveBufferFull))
|
|
{
|
|
}
|
|
/* Receive data */
|
|
u8Byte = SPI_ReceiveData8(SPI_UNIT);
|
|
|
|
return u8Byte;
|
|
}
|
|
|
|
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/
|