使用两个SPI1和SPI2,都与W25Q80相接,但是SPI2一直读写不成功
/********************************* COPYRIGHT 2014 -------- ********* BEGIN OF FILE ********************************/
/**********************************************************************
* file : spi1.c
* author : --
* version : --
* date : 2014-9-2
* brief : spi1 program body
* Description : --
**********************************************************************/
/* Includes == 文件包含 --------------------------------------------*/
#include "spi1.h"
/* Private typedef == 私有类型 ----------------------------------*/
/* Private define == 私有定义 ----------------------------------*/
//-----------------------------------------//
#define sEE_SPI SPI1
#define sEE_SPI_CLK RCC_APB2Periph_SPI1
#define sEE_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */
#define sEE_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
#define sEE_SPI_SCK_GPIO_CLK RCC_AHBPeriph_GPIOA
#define sEE_SPI_SCK_SOURCE GPIO_PinSource5
#define sEE_SPI_SCK_AF GPIO_AF_SPI1
#define sEE_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */
#define sEE_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */
#define sEE_SPI_MISO_GPIO_CLK RCC_AHBPeriph_GPIOA
#define sEE_SPI_MISO_SOURCE GPIO_PinSource6
#define sEE_SPI_MISO_AF GPIO_AF_SPI1
#define sEE_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */
#define sEE_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */
#define sEE_SPI_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOA
#define sEE_SPI_MOSI_SOURCE GPIO_PinSource7
#define sEE_SPI_MOSI_AF GPIO_AF_SPI1
/* Private macro == 私有宏 ------------------------------------*/
/* Private variables == 私有变量 ----------------------------------*/
/* Private functions == 私有函数定义 ------------------------------*/
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/*!< sEE_CS_GPIO, sEE_MOSI_GPIO, sEE_MISO_GPIO and sEE_SCK_GPIO
Periph clock enable */
RCC_AHBPeriphClockCmd(sEE_SPI_MOSI_GPIO_CLK | sEE_SPI_MISO_GPIO_CLK | sEE_SPI_SCK_GPIO_CLK , ENABLE);
/*!< sEE Periph clock enable */
RCC_APB2PeriphClockCmd(sEE_SPI_CLK, ENABLE);
/*!< Configure sEE pins: SCK */
GPIO_InitStructure.GPIO_Pin = sEE_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(sEE_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = sEE_SPI_MISO_PIN;
GPIO_Init(sEE_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = sEE_SPI_MOSI_PIN;
GPIO_Init(sEE_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* Connect PXx to sEE_SPI_SCK */
GPIO_PinAFConfig(sEE_SPI_SCK_GPIO_PORT, sEE_SPI_SCK_SOURCE, sEE_SPI_SCK_AF);
/* Connect PXx to sEE_SPI_MISO */
GPIO_PinAFConfig(sEE_SPI_MISO_GPIO_PORT, sEE_SPI_MISO_SOURCE, sEE_SPI_MISO_AF);
/* Connect PXx to sEE_SPI_MOSI */
GPIO_PinAFConfig(sEE_SPI_MOSI_GPIO_PORT, sEE_SPI_MOSI_SOURCE, sEE_SPI_MOSI_AF);
/*!< sEE SPI Config */
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_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Hard;//SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(sEE_SPI, &SPI_InitStructure);
SPI_Cmd(sEE_SPI, ENABLE); /*!< sEE_SPI enable */
}
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 SPI1_ReadWriteByte(u8 dat)
{
/*!< Loop while DR register in not empty */
while (SPI_I2S_GetFlagStatus(sEE_SPI, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI peripheral */
SPI_SendData(sEE_SPI, dat);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(sEE_SPI, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return (uint8_t)SPI_ReceiveData(sEE_SPI);
}
/********************************* COPYRIGHT 2014 -------- ********* END OF FILE **********************************/
/********************************* COPYRIGHT 2014 -------- ********* BEGIN OF FILE ********************************/
/**********************************************************************
* file : spi2.c
* author : --
* version : --
* date : 2018-12-29
* brief : spi1 program body
* Description : --
**********************************************************************/
/* Includes == 文件包含 --------------------------------------------*/
#include "spi2.h"
/* Private typedef == 私有类型 ----------------------------------*/
/* Private define == 私有定义 ----------------------------------*/
//-----------------------------------------//
#define sEE_SPI2 SPI2
#define sEE_SPI2_CLK RCC_APB1Periph_SPI2
#define sEE_SPI2_SCK_PIN GPIO_Pin_13 /* PB.13 */
#define sEE_SPI2_SCK_GPIO_PORT GPIOB /* GPIOB */
#define sEE_SPI2_SCK_GPIO_CLK RCC_AHBPeriph_GPIOB
#define sEE_SPI2_SCK_SOURCE GPIO_PinSource13
#define sEE_SPI2_SCK_AF GPIO_AF_SPI2
#define sEE_SPI2_MISO_PIN GPIO_Pin_14 /* PB.14 */
#define sEE_SPI2_MISO_GPIO_PORT GPIOB /* GPIOB */
#define sEE_SPI2_MISO_GPIO_CLK RCC_AHBPeriph_GPIOB
#define sEE_SPI2_MISO_SOURCE GPIO_PinSource14
#define sEE_SPI2_MISO_AF GPIO_AF_SPI2
#define sEE_SPI2_MOSI_PIN GPIO_Pin_15 /* PB.15 */
#define sEE_SPI2_MOSI_GPIO_PORT GPIOA /* GPIOB */
#define sEE_SPI2_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOB
#define sEE_SPI2_MOSI_SOURCE GPIO_PinSource15
#define sEE_SPI2_MOSI_AF GPIO_AF_SPI2
/* Private macro == 私有宏 ------------------------------------*/
/* Private variables == 私有变量 ----------------------------------*/
/* Private functions == 私有函数定义 ------------------------------*/
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/*!< sEE_CS_GPIO, sEE_MOSI_GPIO, sEE_MISO_GPIO and sEE_SCK_GPIO
Periph clock enable */
RCC_AHBPeriphClockCmd(sEE_SPI2_MOSI_GPIO_CLK | sEE_SPI2_MISO_GPIO_CLK | sEE_SPI2_SCK_GPIO_CLK , ENABLE);
/*!< sEE Periph clock enable */
RCC_APB1PeriphClockCmd(sEE_SPI2_CLK, ENABLE);
/*!< Configure sEE pins: SCK */
GPIO_InitStructure.GPIO_Pin = sEE_SPI2_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(sEE_SPI2_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = sEE_SPI2_MISO_PIN;
GPIO_Init(sEE_SPI2_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sEE_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = sEE_SPI2_MOSI_PIN;
GPIO_Init(sEE_SPI2_MOSI_GPIO_PORT, &GPIO_InitStructure);
/* Connect PXx to sEE_SPI_SCK */
GPIO_PinAFConfig(sEE_SPI2_SCK_GPIO_PORT, sEE_SPI2_SCK_SOURCE, sEE_SPI2_SCK_AF);
/* Connect PXx to sEE_SPI_MISO */
GPIO_PinAFConfig(sEE_SPI2_MISO_GPIO_PORT, sEE_SPI2_MISO_SOURCE, sEE_SPI2_MISO_AF);
/* Connect PXx to sEE_SPI_MOSI */
GPIO_PinAFConfig(sEE_SPI2_MOSI_GPIO_PORT, sEE_SPI2_MOSI_SOURCE, sEE_SPI2_MOSI_AF);
/*!< sEE SPI Config */
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_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Hard;//SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(sEE_SPI2, &SPI_InitStructure);
SPI_Cmd(sEE_SPI2, ENABLE); /*!< sEE_SPI enable */
}
/**********************************************************************
* @Fuctionname : None
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 SPI2_ReadWriteByte(u8 dat)
{
/*!< Loop while DR register in not empty */
while (SPI_I2S_GetFlagStatus(sEE_SPI2, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI peripheral */
SPI_SendData(sEE_SPI2, dat);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(sEE_SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return (uint8_t)SPI_ReceiveData(sEE_SPI2);
}
/********************************* COPYRIGHT 2014 -------- ********* END OF FILE **********************************/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
/********************************* COPYRIGHT 2018 -------- ********* BEGIN OF FILE ********************************/
/**********************************************************************
* @file : w25q80.c
* @Author : --
* @version : --
* @date : 2015-05-18
* @brief : w25q80 program body
* @Description : --
**********************************************************************/
/* Includes == 文件包含 --------------------------------------------*/
#include "w25q80.h"
#include "spi2.h"
#include "systick.h"
u16 W25Q80_TYPE=W25Q80;
/* Private typedef == 私有类型 ----------------------------------*/
/* Private define == 私有定义 ----------------------------------*/
#define W25Q80_CS_GPIO GPIOB
#define W25Q80_CS_PIN GPIO_Pin_12
#define W25Q80_CS_CLK RCC_AHBPeriph_GPIOB
/* Private macro == 私有宏 ------------------------------------*/
/* Private variables == 私有变量 ----------------------------------*/
/* Private functions == 私有函数定义 ------------------------------*/
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOD Periph clock enable */
RCC_AHBPeriphClockCmd(W25Q80_CS_CLK, ENABLE);
/* Configure PD0 and PD1 or PD3 and PD7 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = W25Q80_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(W25Q80_CS_GPIO, &GPIO_InitStructure);
GPIO_SetBits(W25Q80_CS_GPIO, W25Q80_CS_PIN);
SPI2_Init();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_CS_Low(void)
{
GPIO_ResetBits(W25Q80_CS_GPIO, W25Q80_CS_PIN);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_CS_High(void)
{
GPIO_SetBits(W25Q80_CS_GPIO, W25Q80_CS_PIN);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 W25Q80_ReadWriteByte(u8 dat)
{
return SPI2_ReadWriteByte(dat);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 W25Q80_ReadSR(void)
{
u8 byte=0;
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteStatusReg);
byte=W25Q80_ReadWriteByte(0Xff);
W25Q80_CS_High();
return byte;
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_SR(u8 sr)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteStatusReg);
W25Q80_ReadWriteByte(sr);
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_Enable(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteEnable);
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_Disable(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_WriteDisable);
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u16 W25Q80_ReadID(void)
{
u16 Temp = 0;
W25Q80_CS_Low();
W25Q80_ReadWriteByte(0x90);
W25Q80_ReadWriteByte(0x00);
W25Q80_ReadWriteByte(0x00);
W25Q80_ReadWriteByte(0x00);
Temp|=W25Q80_ReadWriteByte(0xFF)<<8;
Temp|=W25Q80_ReadWriteByte(0xFF);
W25Q80_CS_High();
return Temp;
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Read(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
u16 i;
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_ReadData);
W25Q80_ReadWriteByte((u8)((ReadAddr)>>16));
W25Q80_ReadWriteByte((u8)((ReadAddr)>>8));
W25Q80_ReadWriteByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer=W25Q80_ReadWriteByte(0XFF);
}
W25Q80_CS_High();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_Page(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
u16 i;
W25Q80_Write_Enable();
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_PageProgram);
W25Q80_ReadWriteByte((u8)((WriteAddr)>>16));
W25Q80_ReadWriteByte((u8)((WriteAddr)>>8));
W25Q80_ReadWriteByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)
{
W25Q80_ReadWriteByte(pBuffer);
}
W25Q80_CS_High();
W25Q80_Wait_Busy();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr % 256;
if(NumByteToWrite<=pageremain)
pageremain=NumByteToWrite;
while(1)
{
W25Q80_Write_Page(pBuffer,WriteAddr,pageremain);
if(NumByteToWrite==pageremain)
break;
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain;
if(NumByteToWrite>256)
pageremain=256;
else pageremain=NumByteToWrite;
}
};
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
u8 W25Q80_BUFFER[4096];
void W25Q80_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
u8 * W25Q80_BUF;
W25Q80_BUF=W25Q80_BUFFER;
secpos=WriteAddr/4096; // 扇区地址 0--255 for w25q80
secoff=WriteAddr%4096; // 在扇区内的偏移
secremain=4096-secoff; //
if(NumByteToWrite<=secremain)
secremain=NumByteToWrite; // 不大于4096字节
while(1)
{
W25Q80_Read(W25Q80_BUF,secpos*4096,4096); // 读出整个扇区内容
for(i=0;i<secremain;i++) // 校验数据
{
if(W25Q80_BUF[secoff+i]!=0XFF)break; // 需要擦除
}
if(i<secremain) // 需要擦除
{
W25Q80_Erase_Sector(secpos); // 擦除整个扇区
for(i=0;i<secremain;i++) // 复制数据
{
W25Q80_BUF[i+secoff]=pBuffer;
}
W25Q80_Write_NoCheck(W25Q80_BUF,secpos*4096,4096); // 写入整个扇区
}
else
W25Q80_Write_NoCheck(pBuffer,WriteAddr,secremain); // 写已经擦除了的,直接写入扇区剩余空间
if(NumByteToWrite==secremain)
break; // 写入结束
else
{
secpos++;
secoff=0;
pBuffer+=secremain;
WriteAddr+=secremain;
NumByteToWrite-=secremain;
if(NumByteToWrite>4096)secremain=4096;
else secremain=NumByteToWrite;
}
};
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Erase_Chip(void)
{
W25Q80_Write_Enable();
W25Q80_Wait_Busy();
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_ChipErase);
W25Q80_CS_High();
W25Q80_Wait_Busy();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Erase_Sector(u32 Dst_Addr)
{
Dst_Addr*=4096;
W25Q80_Write_Enable();
W25Q80_Wait_Busy();
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_SectorErase);
W25Q80_ReadWriteByte((u8)((Dst_Addr)>>16));
W25Q80_ReadWriteByte((u8)((Dst_Addr)>>8));
W25Q80_ReadWriteByte((u8)Dst_Addr);
W25Q80_CS_High();
W25Q80_Wait_Busy();
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Wait_Busy(void)
{
while ((W25Q80_ReadSR()&0x01)==0x01);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_PowerDown(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W2580_PowerDown);
W25Q80_CS_High();
delay_us(3);
}
/**********************************************************************
* @Brief : None
* @Input : None
* @Output : None
* @Return : None
* @Remark : None
**********************************************************************/
void W25Q80_Wakeup(void)
{
W25Q80_CS_Low();
W25Q80_ReadWriteByte(W25Q80_ReleasePowerDown);
W25Q80_CS_High();
delay_us(3);
}
/********************************* COPYRIGHT 2018 -------- ********* END OF FILE **********************************/
一周热门 更多>