首页 > 代码库 > FPGA配置芯片EPCS读写操作--STM32读写

FPGA配置芯片EPCS读写操作--STM32读写

注意事项:

(1)首先STM32需要设置nCE和nConfig信号,即nCE置高,nConfig拉低,获得EPCS的控制权,而后对EPCS操作,操作完成后需要释放这两个管脚,即nCE拉低,nConfig置高。

(2)EPCS的极性为:sck为空闲状态为高电平,采样边沿为SCK的第二个跳变沿,(即上升沿,注意前提是SCK空闲为高)

SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; 
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft ;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

(3)EPCS操作时对于地址和指令是MSB的,而对于写数据却是LSB的,因为上电FPGA读取EPCS内部数据时是LSB读取的。所以有如下操作代码。

SPI_FLASH_Write_Enable();                  //SET WEL

spi_cs_low;
SPI_Send_byte(EPCS16_ByteProgram );     //write byte
add1 = (u8)((WritingAddr)>>16);
add2 = (u8)((WritingAddr)>>8);
add3 = (u8)((WritingAddr));
SPI_Send_byte(add1);  
SPI_Send_byte(add2);  
SPI_Send_byte(add3); 
for(num=0;num<NumByteToWrite;num++)
{
wdata = http://www.mamicode.com/pBuffer[WritingNum];
if(MSB_SWAP_LSB)
wdata = http://www.mamicode.com/((wdata&0x01)<<7)|((wdata&0x02)<<5)|((wdata&0x04)<<3)|((wdata&0x08)<<1)|((wdata&0x10)>>1)|((wdata&0x20)>>3)|((wdata&0x40)>>5)|                  ((wdata&0x80)>>7);
else
wdata = http://www.mamicode.com/pBuffer[WritingNum];
SPI_Send_byte(wdata);
WritingAddr++;
WritingNum++;
if(WritingAddr%SPI_FLASH_PAGE_SIZE==0 || WritingNum==NumByteToWrite)
{
break;
}
}