#include "MB85RS256.h"
#include "spi.h"
u32 MB85R_TYPE = RS256B_ID;
u8 RS256B_ReadWriteByte(u8 TxData)
{
return SPI3_ReadWriteByte(TxData);
}
void RS256B_TransmitReceive(u8* TxData,u8* RxData,u8 size)
{
SPI3_TransmitReceive(TxData,RxData,size);
}
u8 RS256B_Init(void)
{
u8 res = 0;
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOB_CLK_ENABLE(); //使能GPIOB时钟
//PB6
GPIO_Initure.Pin=GPIO_PIN_6; //PB6
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//高速
HAL_GPIO_Init(GPIOB,&GPIO_Initure); //初始化
RS256B_CS=1; //SPI LCD不选中
SPI3_Init(); //初始化SPI
SPI3_SetSpeed(SPI_BAUDRATEPRESCALER_2); //设置为42M时钟,高速模式
MB85R_TYPE = RS256B_ReadDeviceID();
if(MB85R_TYPE == 0x047F0509) res = 1;
return res;
}
u32 RS256B_ReadDeviceID(void)
{
u32 id=0;
RS256B_CS = 0;
RS256B_ReadWriteByte(0x9F);
id = id + RS256B_ReadWriteByte(0xff);
id = id <<8;
id = id + RS256B_ReadWriteByte(0xff);
id = id <<8;
id = id + RS256B_ReadWriteByte(0xff);
id = id <<8;
id = id + RS256B_ReadWriteByte(0xff);
RS256B_CS = 1;
return id;
}
//MB85RSXX写使能
//将WEL置位
void RS256B_Write_Enable(void)
{
RS256B_CS=0; //使能器件
SPI3_ReadWriteByte(RS256B_WriteEnable); //发送写使能
RS256B_CS=1; //取消片选
}
//W25QXX写禁止
//将WEL清零
void RS256B_Write_Disable(void)
{
RS256B_CS=0; //使能器件
SPI3_ReadWriteByte(RS256B_WriteDisable); //发送写禁止指令
RS256B_CS=1; //取消片选
}
void RS256B_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead) //读取flash
{
if((ReadAddr+NumByteToRead)>RS256B_END_ADDR) return;
RS256B_CS=0; //使能器件
SPI3_ReadWriteByte(RS256B_ReadData); //发送读数据指令
SPI3_ReadWriteByte(ReadAddr>>8);
SPI3_ReadWriteByte(ReadAddr&0xff);
RS256B_TransmitReceive(pBuffer,pBuffer,NumByteToRead);//写入数据
RS256B_CS=1;
}
void RS256B_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)//写入flash
{
if((WriteAddr+NumByteToWrite)>RS256B_END_ADDR) return;
RS256B_Write_Enable();
RS256B_CS=0; //使能器件
SPI3_ReadWriteByte(RS256B_WriteData); //发送写数据指令
SPI3_ReadWriteByte(WriteAddr>>8);
SPI3_ReadWriteByte(WriteAddr&0xff);
RS256B_TransmitReceive(pBuffer,pBuffer,NumByteToWrite);//写入数据
RS256B_CS=1;
}
- 1
- 2
前往页