小弟我用STM8的SPI功能,调试MX25L6406E这个64MB的SPI_Flash(硬件是搭了一个万用表然后飞线到学习板的SPI口上面的).使用的是SPI的模式3方式进行数据的传送接收.时钟使用Fcpu / 2.
主要涉及的代码如下:
/*
* Function: Wait_Flash_WarmUp
* Arguments: None.
* Description: Wait some time until flash read / write enable.
* Return Message: None.
*/
void Wait_Flash_WarmUp()
{
uint32 time_cnt = FlashFullAccessTime;
while( time_cnt > 0 )
{
time_cnt--;
}
}
/*
* Function: Initial_Spi
* Arguments: None
* Description: Initial spi flash state and wait flash warm-up
* (enable read/write).
* Return Message: None
*/
void Initial_Spi()
{
GPIO_Init(CS_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(SPI_SCK_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(SPI_MOSI_PIN,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(SPI_MISO_PIN,GPIO_MODE_IN_PU_NO_IT);
SPI_DeInit();
SPI_Init(SPI_FIRSTBIT_MSB,SPI_BAUDRATEPRESCALER_16,SPI_MODE_MASTER,SPI_CLOCKPOLARITY_HIGH,\
SPI_CLOCKPHASE_2EDGE,SPI_DATADIRECTION_2LINES_FULLDUPLEX,SPI_NSS_SOFT,0x07);
SPI_Cmd(TRUE);
// Wait flash warm-up
Wait_Flash_WarmUp();
}
/*
* Function: CS_Low, CS_High
* Arguments: None.
* Description: Chip select go low / high.
* Return Message: None.
*/
void CS_Low()
{
GPIO_WriteLow(CS_PIN);
}
void CS_High()
{
//--- insert your chip select code here. ---//
GPIO_WriteHigh(CS_PIN);
}
/*
* Function: SendByte
* Arguments: byte_value, data transfer to flash
* transfer_type, select different type of I/O mode.
* Seven mode:
* SIO, single IO
* DIO, dual IO
* QIO, quad IO
* PIO, parallel
* DTSIO, double transfer rate SIO
* DTDIO, double transfer rate DIO
* DTQIO, double transfer rate QIO
* Description: Send one byte data to flash
* Return Message: None.
*/
void SendByte( uint8 byte_value, uint8 transfer_type )
{
while(SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET);
SPI_SendData(byte_value);
}
/*
* Function: GetByte
* Arguments: byte_value, data receive from flash
* transfer_type, select different type of I/O mode.
* Seven mode:
* SIO, single IO
* DIO, dual IO
* QIO, quad IO
* PIO, parallel IO
* DTSIO, double transfer rate SIO
* DTDIO, double transfer rate DIO
* DTQIO, double transfer rate QIO
* Description: Get one byte data to flash
* Return Message: 8 bit data
*/
uint8 GetByte( uint8 transfer_type )
{
uint8 data_buf;
SendByte( DUMMY_BYTE, SIO );
while(SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);
data_buf = SPI_ReceiveData();
return data_buf;
}
///////////////以下是测试代码//////////////////////////////
#define TRANS_LENGTH 16
#define RANDOM_SEED 106
#define FLASH_TARGET_ADDR 0x00000000
#define Error_inc(x) x = x + 1;
/*
* Simple flash ID test
*/
uint8 FlashID_Test( void )
{
uint32 flash_id = 0;
uint16 error_cnt = 0;
uint16 rems_id;
uint8 electric_id = 0;
FlashStatus flash_state = {0};
ReturnMsg msg;
/* Read flash device id */
msg = CMD_RDID( &flash_id );
if( msg != (ReturnMsg)FlashOperationSuccess )
return FALSE;
msg = CMD_RES( &electric_id );
if( msg != (ReturnMsg)FlashOperationSuccess )
return FALSE;
/* Decide rems_id order. 0: { manufacturer id, device id }
1: { device id, manufacturer id } */
flash_state.ArrangeOpt = 0;
msg = CMD_REMS( &rems_id, &flash_state );
if( msg != (ReturnMsg)FlashOperationSuccess )
return FALSE;
/* Compare to expected value */
if( flash_id != FlashID )
Error_inc( error_cnt );
if( electric_id != ElectronicID )
Error_inc( error_cnt );
if( flash_state.ArrangeOpt )
{
if( rems_id != RESID1 )
Error_inc( error_cnt );
}else
{
if( rems_id != RESID0 )
Error_inc( error_cnt );
}
if( error_cnt != 0 )
return FALSE;
else
return TRUE;
}
//////
发现测试代码读取回来的值并非datasheet说的值.datasheet的值如下:
#define FlashID 0xc22017
#define ElectronicID 0x16
#define RESID0 0xc216
#define RESID1 0x16c2
因此,小弟想请教一下,SPI这个读数据会不会跟硬件是飞线的原因造成的.另外MISO及MOSI这个两个口都没有接外部上拉电阻.
还是我上面软件配置哪里有异常?
请大家指教,谢谢
|
|