Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

DescriptionCode

SW host computer: reads the ID of the SPI Flash

This is pseudocode close to the real one.
The real code need indirection for SPI command (if written in C#).

Code Block
languagec#
titlePseudocode, The real code need indirection for SPI command (if written in C#)
byte[] Command = new byte[64];


byte[] Reply = new byte[64];


Command[0] = (byte) FX2_Commands.CMD_FX2_FLASH_WRITE_COMMAND;


Command[1] = (byte) 1; //Numeber of SPI commands used by spi_command() : putcSPI(SPI_RDID);
 
 

Command[2] = (byte) 3; //Number of SPI bytes as reply: mid == 0xEF && did == 0x40 && uid == 0x17


Command[3] = (byte) 0x9F; //(byte)SPI_Commands.CMD_SPI_RDID; // SPI_RDID 0x9F
 ≡ get ID command


Command[4] = (byte)0;


Command[5] = (byte)0;


Command[6] = (byte)0;


/*

TE_USB_FX2_SendCommand (Command[0] = (byte) FX2_Commands.CMD_FX2_FLASH_WRITE_COMMAND) calls "case CMD_FLASH_WRITE_COMMAND"


"case CMD_FLASH_WRITE_COMMAND" calls spi_command(EP1OUTBUF[1], &EP1OUTBUF[3], EP1OUTBUF[2], &EP1INBUF[1]) with EP1OUTBUF[3]= SPI_RDID SPI Flash Command


EP1OUTBUF = Command

Reply = EP1INBUF

*/


if (TE_USB_FX2_SendCommand(..., Command, CmdLength, Reply, ReplyLength, 5000) == true)
 
 
{

LogTextLine += "SPI Flash IDCODE " + "uid = 0x" + Reply[1].ToString("x") + "mid = 0x "+ Reply[2].ToString("x") + " did = 0x" + Reply[3].ToString("x") + "\r\n";

}

FW running on USB FX2 microcontroller

This is a piece of real code (FW running on USB FX2 microcontroller)

https://github.com/Trenz-Electronic/TE-USB-Suite/blob/master/TE_USB_FX2.firmware/te_usb_api.ver.3.2/te_api.c

Lines 207-211

EP1INBUF: read Reply[] from USB FX2 microcontroller to host computer

EP1OUTBUF: write Command[] from host computer to USB FX2 microcontroller

Scroll Title
titleFlash IDCODEs

Flash

Manufacturer ID

Memory Type

Capacity

M25P32

20h - Micron

20h

16h

W25Q64FV

EFh - Winbond

40h

17h

Code Block
languagecpp
titleLines 207-211 of te_api.c
case CMD_FLASH_WRITE_COMMAND:


EP1INBUF[0] = 0x55;


//void spi_command(BYTE CmdLen, unsigned char *CmdData, BYTE RdLen, unsigned char *RdData)


spi_command(EP1OUTBUF[1], &EP1OUTBUF[3], EP1OUTBUF[2], &EP1INBUF[1]);


new_data = 1;


break;


/*


Command[0] = CMD_FLASH_WRITE_COMMAND; used by TE_USB_FX2_SendCommand () to call "case CMD_FLASH_WRITE_COMMAND" and then spi_command()


EP1OUTBUF[1] = CmdLen = Command[1]= CmdLength = 1; // used by spi_command(), MD_SPI_RDID = 0x9F is a single byte


EP1OUTBUF[2] = RdLen = Command[2]= ReplyLength = 3; // used by spi_command(), SPI Flash ID should be 3 byte


EP1OUTBUF[3] = Command[3] = CMD_SPI_RDID = 0x9F; //used by spi_command()


Reply[0] = EP1INBUF[0] = 0x55;


Reply[1] = EP1INBUF[1] = 0x20; // for example


Reply[2] = EP1INBUF[2] = 0x20; // for example


Reply[3] = EP1INBUF[3] = 0x16; // for example


*/

FW running on USB FX2 microcontroller

This is a piece of real code

https://github.com/Trenz-Electronic/TE-USB-Suite/blob/master/TE_USB_FX2.firmware/te_usb_api.ver.3.2/spi.c

Lines 63-89

Code Block
languagecpp
titleLines 63-89 of spi.c
void spi_command(BYTE CmdLen, unsigned char *CmdData, BYTE RdLen, unsigned char *RdData)


{


volatile unsigned char spi_count, rd_buff;// pr_address;


OED = 0x73; // 0b01110011;


FPGA_POWER = 0; //power off fpga


FLASH_ENABLE; //assert chip select


//Write command


spi_count = CmdLen;


if (spi_count > 64) spi_count = 64;


while (spi_count > 0)


{


putcSPI(*CmdData); //send read command


CmdData++;


spi_count = spi_count - 1;


}


//Read response


spi_count = RdLen;


if (spi_count > 64) spi_count = 64;


while (spi_count > 0)


{


rd_buff = getcSPI();


*RdData = rd_buff;


RdData++;


spi_count = spi_count - 1;


}


FLASH_DISABLE;

}

 

}d