<spi.h>
#ifndef SPI_H_
#define SPI_H_
#include "hw_config.h"
#define SPI1_CLK RCC_APB2Periph_SPI1
#define SPI1_GPIO GPIOA
#define SPI1_PIN_NSS GPIO_PIN_4
#define SPI1_PIN_SCK GPIO_PIN_5
#define SPI1_PIN_MISO GPIO_PIN_6
#define SPI1_PIN_MOSI GPIO_PIN_7
#define SPI2_CLK RCC_APB1Periph_SPI2
#define SPI2_GPIO GPIOB
#define SPI2_PIN_NSS GPIO_PIN_12
#define SPI2_PIN_SCK GPIO_PIN_13
#define SPI2_PIN_MISO GPIO_PIN_14
#define SPI2_PIN_MOSI GPIO_PIN_15
#define BufferSize 32
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
//------------------------------------------------//
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
void SPI_Configuration();
void SPI_Test();
#endif
<spi.c>
#include "spi.h"
uint8_t SPI1_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
0x1D, 0x1E, 0x1F, 0x20};
uint8_t SPI2_Buffer_Tx[BufferSize] = {0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E,
0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65,
0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
0x6D, 0x6E, 0x6F, 0x70};
uint8_t SPI1_Buffer_Rx[BufferSize], SPI2_Buffer_Rx[BufferSize];
uint8_t TxIdx = 0, RxIdx = 0, k = 0;
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
volatile TestStatus TransferStatus3 = FAILED, TransferStatus4 = FAILED;
//--------송수신 완료 후 데이터가 송수신 되었는지 데이터 검사. PASSED 반환--------//
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while (BufferLength--)
{
if (*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
//--------SPI관련 포트설정. SPI1을 master, SPI2를 slave로 설정--------//
void SPI_Configuration()
{
//--------GPIO Clock Setting--------//
RCC_APB2PeriphClockCmd(SPI1_CLK, ENABLE);
RCC_APB1PeriphClockCmd(SPI2_CLK, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SPI1_PIN_SCK | SPI1_PIN_MOSI | SPI1_PIN_MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI2_PIN_SCK | SPI2_PIN_MOSI | SPI2_PIN_MISO;
GPIO_Init(SPI2_GPIO, &GPIO_InitStructure);
/*
SPI1 setting :
- baud rate : 9Mhz
- data size : 8bits
- SPI mode : Master
- Full Duplex
*/
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // PCLK2 / 8 = 9MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/*
SPI1 setting :
- baud rate : 9Mhz
- data size : 8bits
- SPI mode : Slave
- Full Duplex
*/
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPI_Cmd(SPI2, ENABLE);
}
//-------SPI 송수신 테스트--------//
void SPI_Test()
{
/* Transfer procedure */
while (TxIdx < BufferSize)
{
/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI2 data */
SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[TxIdx]);
/* Send SPI1 data */
SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
/* Wait for SPI2 data reception */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI2 received data */
SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2);
/* Wait for SPI1 data reception */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI1 received data */
SPI1_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
}
/* Check the corectness of written dada */
TransferStatus1 = Buffercmp(SPI2_Buffer_Rx, SPI1_Buffer_Tx, BufferSize);
TransferStatus2 = Buffercmp(SPI1_Buffer_Rx, SPI2_Buffer_Tx, BufferSize);
/* TransferStatus1, TransferStatus2 = PASSED, if the transmitted and received data
are equal */
/* TransferStatus1, TransferStatus2 = FAILED, if the transmitted and received data
are different */
if(PASSED == TransferStatus1)
{
printf("phase 1> Transfer Status 1 PASSED\n");
}
else
{
printf("phase 1> Transfer Status 1 FAILED\n");
}
if(PASSED == TransferStatus2)
{
printf("phase 1> Transfer Status 2 PASSED\n");
}
else
{
printf("phase 1> Transfer Status 2 FAILED\n");
}
}