spi 与flash公用,flash 没问题,但是MB85RS1MT 死活没反应,读取id 或者写入数据再次读出来,都是0xff,不知道卡在哪,有人用这颗ic吗
void MX_SPI2_Init(void)
{
/*##-1- Configure the SPI peripheral #######################################*/
/* Set the SPI parameters */
Spi2Handle.Instance = SPIx ;
Spi2Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
Spi2Handle.Init.Direction = SPI_DIRECTION_2LINES;
Spi2Handle.Init.CLKPhase = SPI_PHASE_2EDGE;
Spi2Handle.Init.CLKPolarity = SPI_POLARITY_HIGH;
Spi2Handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
Spi2Handle.Init.CRCPolynomial = 10;
Spi2Handle.Init.DataSize = SPI_DATASIZE_8BIT;
Spi2Handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
Spi2Handle.Init.NSS = SPI_NSS_SOFT;
Spi2Handle.Init.TIMode = SPI_TIMODE_DISABLED;
Spi2Handle.Init.Mode = SPI_MODE_MASTER;
HAL_SPI_Init(&Spi2Handle);
}
static void SPIx_Error (void)
{
/* De-initialize the SPI communication BUS */
HAL_SPI_DeInit(&Spi2Handle);
/* Re- Initiaize the SPI communication BUS */
MX_SPI2_Init();
}
uint32_t MB85RS1MT_ReadID(void)
{
uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0,Temp3 = 0;
/* Select the FLASH: Chip Select low */
MB85RS1MT_CS_LOW();
/* Send "RDID " instruction */
MB85RS1MT_SPI_SendByte(MB85RS1MT_READ_ID );
/* Read a byte from the FLASH */
Temp0 = MB85RS1MT_SPI_SendByte(0xa5);
/* Read a byte from the FLASH */
Temp1 = MB85RS1MT_SPI_SendByte(0xa5);
/* Read a byte from the FLASH */
Temp2 = MB85RS1MT_SPI_SendByte(0xa5);
/* Read a byte from the FLASH */
Temp3 =MB85RS1MT_SPI_SendByte(0xa5);
/* Deselect the FLASH: Chip Select high */
MB85RS1MT_CS_HIGH();
Temp = (Temp0 << 24) | (Temp1 << 16) | (Temp2<<8)|Temp3;
return Temp;
}
uint8_t MB85RS1MT_SPI_ReadByte(void)
{
return (MB85RS1MT_SPI_SendByte(0xa5));
}
uint8_t MB85RS1MT_SPI_SendByte(uint8_t byte)
{
HAL_StatusTypeDef status = HAL_OK;
uint8_t readvalue = 0x0;
uint8_t writevalue = byte ;
uint32_t spixTimeout=200 ;
status = HAL_SPI_TransmitReceive(&Spi2Handle, (uint8_t *) &writevalue, (uint8_t *) &readvalue, 1, spixTimeout);
/* Check the communication status */
if(status != HAL_OK)
{
/* Execute user timeout callback */
SPIx_Error();
}
return readvalue;
}
void MB85RS1MT_WriteDisable(void)
{
/* Select the FLASH: Chip Select low */
MB85RS1MT_CS_LOW();
/* Send "Write Enable" instruction */
MB85RS1MT_SPI_SendByte(MB85RS1MT_WRDI);
/* Deselect the FLASH: Chip Select high */
MB85RS1MT_CS_HIGH();
}
void MB85RS1MT_WriteEnable(void)
{
/* Select the FLASH: Chip Select low */
MB85RS1MT_CS_LOW();
/* Send "Write Enable" instruction */
MB85RS1MT_SPI_SendByte(MB85RS1MT_WREN);
/* Deselect the FLASH: Chip Select high */
MB85RS1MT_CS_HIGH();
}
void MB85RS1MT_Read_Bytes(uint8_t *data,uint32_t address,uint16_t len)//写入一个字节到特定地址空间
{ uint16_t i;
uint8_t addr_tempH,addr_tempM,addr_tempL;
MB85RS1MT_CS_LOW();
MB85RS1MT_SPI_SendByte(MB85RS1MT_READ);
MB85RS1MT_SPI_SendByte(address>>16);// 高8位地址
MB85RS1MT_SPI_SendByte(address>>8);//中8位地址
MB85RS1MT_SPI_SendByte(address); //低8位地址
for(i=0;i<len;i++){
data[i]= MB85RS1MT_SPI_SendByte(0x55);
}
MB85RS1MT_CS_HIGH();
}
void MB85RS1MT_Write_Bytes(uint8_t *data,uint32_t address,uint16_t len)//写入一个字节到特定地址空间
{ uint16_t i;
uint8_t addr_tempH,addr_tempM,addr_tempL;
MB85RS1MT_WriteEnable();
MB85RS1MT_CS_LOW();
MB85RS1MT_SPI_SendByte(MB85RS1MT_WRITE );
MB85RS1MT_SPI_SendByte(address>>16);// 高8位地址
MB85RS1MT_SPI_SendByte(address>>8);//中8位地址
MB85RS1MT_SPI_SendByte(address); //低8位地址
for(i=0;i<len;i++){
MB85RS1MT_SPI_SendByte(data[i]);
}
MB85RS1MT_WriteDisable();
MB85RS1MT_CS_HIGH();
}
uint8_t MB85RS1MT_Read_Status(void)
{
uint8_t flag;
MB85RS1MT_CS_LOW();
MB85RS1MT_SPI_SendByte(MB85RS1MT_RDSR );
flag= MB85RS1MT_SPI_ReadByte();
MB85RS1MT_CS_HIGH();
return flag;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>