bsp_spi.c 3.37 KB
#include "bsp.h"
#include  <includes.h>
#include "w5100s_conf.h"
#include "wizchip_conf.h"

extern void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb));
extern void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void));

void spi_gpio_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);   // 2020-2-12 add
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

	/* PC-10 -> SCL | PC-11 -> MISO | PC-12 -> MOSI */
	//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	//	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;    // ADD 2020-2-6 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	/* PA-04 -> CS */
	//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;  // ADD 2020-2-6 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void spiinitailize(void)
{
  SPI_InitTypeDef SPI_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);      // PA5 A6 A7---SPI1
  
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_Init(SPI1, &SPI_InitStructure);

  SPI_Cmd(SPI1,ENABLE);
}

void cs_high(void)     // A4---CS
{
  GPIO_SetBits(GPIOA,GPIO_Pin_4);
}

void cs_low(void)
{
  GPIO_ResetBits(GPIOA,GPIO_Pin_4);
}

void spi_send_byte(u8 byte)
{
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);    // SPI_I2S_FLAG_TXE--发送缓冲区空
  SPI_I2S_SendData(SPI1, byte);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  SPI_I2S_ReceiveData(SPI1);
}

u8 spi_read_byte(void)
{
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  SPI_I2S_SendData(SPI1, 0);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  return SPI_I2S_ReceiveData(SPI1);
}



void SpiInit()
{	
	reset_break_gpio_init();              // 以太网复位与中断管脚初始化
  spi_gpio_init();                      // 以太网SPI管脚初始化
	
	spiinitailize();                      // 以太网SPI配置初始化
	
	reg_wizchip_spi_cbfunc(spi_read_byte,spi_send_byte);          // SPI读写数据函数映射
	reg_wizchip_cs_cbfunc(cs_low,cs_high);                        // SPI片选控制函数映射
	
	reset_w5100s();                       // W5100S硬件复位 
	
//	PHY_check();                          // 网线检测程序  检测PHYSR是否连接上 若未连接 则等待直到连接上
	set_w5100s_mac();                     // 设置W5100SMAC地址
	set_w5100s_netinfo();                 // 设置W5100S网络参数
	wizchip_init(txsize,rxsize);          // 初始化4个Socket的发送接收缓存大小 将设定值写入发送和接收缓存寄存器
}