QSPI_SRECBootloader/bootloader.c
static uint8_t flash_get_srec_line (uint8_t *buf)
{
    int Status;
	uint8_t c;
	uint8_t endFlag = 0;
    int count = 0;
    static int addrIncr = 0, traverseCnt = 0;
    int mode = READ_EXTRA_BYTES;
    static uint32_t readAddress, readNumBytes;

	/*
	 * Set the
	 * - Address in the Serial Flash where the data is to be read from.
	 * - Number of bytes to be read from the Serial Flash.
	 * - Read Buffer to which the data is to be read.
	 */
	readAddress = flbuf + addrIncr*128; // First addr: 0x0, 2nd addr: 0x80, 3rd adr: 0x100
	readNumBytes = PAGE_SIZE; // Reading overlaps
	addrIncr++;

	if (addrIncr == 4)
		usleep(100);

	SpiClearReadBuffer();

	Status = SpiFlashRead(&Spi, readAddress, readNumBytes, SPI_READ_COMMAND);

	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	//qspi_print_control(qspi_check_control());
	//qspi_print_status(qspi_check_status());
	//rfifo = Xil_In32(QSPI_BASEADDR + XSP_RFO_OFFSET);
	//tfifo = Xil_In32(QSPI_BASEADDR + XSP_TFO_OFFSET);
	u32 drr = Xil_In32(QSPI_BASEADDR + AXI_QSPI_SPIDRR); // Displays amount of data that is not yet read


	/* traverse ReadBuffer from the back, and terminate the buffer */
	/* Readbuffer will consist of 9 extra bytes + (256 + addrOffset) of data  */
	for (int i = PAGE_SIZE + READ_EXTRA_BYTES ; i > READ_EXTRA_BYTES - 1; i--){
		if (ReadBuffer[i] == '\r'){
			ReadBuffer[i] = '\0';
			break;
		}
		if (i == READ_EXTRA_BYTES){
			/* The readbuffer is filled */
			xil_printf("End of readBuffer reached\n\r");
		}

	}

	SpiFlashWaitForFlashReady();


	/* Traverse the beginning of the array to find S and remove the extra bytes*/
	/* If beginning is imcomplete, just skip to the next srec elements because
	 * we assume that this is already read. */
	for (int j = READ_EXTRA_BYTES; j < 128 + READ_EXTRA_BYTES; j++){
		if (ReadBuffer[j] == 'S')
		{
			/* Index of SREC Data = CMD Extra bytes + 1 */
			if (j != READ_EXTRA_BYTES + 1)
				//xil_printf("Redundant bytes detected in buffer\r\n");
			mode = j;
			break;
		}
		if (j == 64){
			xil_printf("S search failed\r\n");
			return XST_FAILURE;
		}
	}



    c  = ReadBuffer[mode];

    /* Check if S is found. If it is, then remove extra bytes */
    if (c == 'S') {
		memcpy(buf, (void*)(ReadBuffer+mode), readNumBytes*sizeof(uint8_t));
		//*buf = ReadBuffer+(uint8_t)mode;
		return 0;
    } else {
    	xil_printf("No Srec data found!\r\n");
    	return 1;
    }
}
/*
 * This function reads a page of memory using quad 4 bytes command read.
 * It was modified from the original spi srec code from xilinx. It uses
 * axi_spi library instead of the xilisf lib which is deprecated since vivado 2019.2
 * This code was modified under the following assumptions:
 * -
 * - the size of one srec line is unknown
 * 		- therefore the last element can incomplete. this is a problem that is handled here
 * - the copying of srec elements may not be repeated
 * 		- the code tries to get the incomplete srec elements in the next loop by counting the bytes
 * 		needed to complete it.
 * -
 *  */
This function reads a page of memory using quad 4 bytes command read.
It was modified from the original spi srec code from xilinx. It uses
axi_spi library instead of the xilisf lib which is deprecated since vivado 2019.2
This code was modified under the following assumptions:

- the size of one srec line is unknown
- therefore the last element can incomplete. this is a problem that is handled here
- the copying of srec elements may not be repeated
- the code tries to get the incomplete srec elements in the next loop by counting the bytes
needed to complete it.
This function reads a page of memory using quad 4 bytes command read . It was modified from the original spi srec code from xilinx . It uses axi_spi library instead of the xilisf lib which is deprecated since vivado 2019 . 2 This code was modified under the following assumptions : - the size of one srec line is unknown - therefore the last element can incomplete . this is a problem that is handled here - the copying of srec elements may not be repeated - the code tries to get the incomplete srec elements in the next loop by counting the bytes needed to complete it .
static uint8_t flash_get_srec_line (uint8_t *buf)
{
    int Status;
	uint8_t c;
	uint8_t endFlag = 0;
    int count = 0;
    static int addrIncr = 0, traverseCnt = 0;
    int mode = READ_EXTRA_BYTES;
    static uint32_t readAddress, readNumBytes;
	readAddress = flbuf + addrIncr*128; 
	readNumBytes = PAGE_SIZE; 
	addrIncr++;
	if (addrIncr == 4)
		usleep(100);
	SpiClearReadBuffer();
	Status = SpiFlashRead(&Spi, readAddress, readNumBytes, SPI_READ_COMMAND);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	u32 drr = Xil_In32(QSPI_BASEADDR + AXI_QSPI_SPIDRR); 
	for (int i = PAGE_SIZE + READ_EXTRA_BYTES ; i > READ_EXTRA_BYTES - 1; i--){
		if (ReadBuffer[i] == '\r'){
			ReadBuffer[i] = '\0';
			break;
		}
		if (i == READ_EXTRA_BYTES){
			xil_printf("End of readBuffer reached\n\r");
		}
	}
	SpiFlashWaitForFlashReady();
	for (int j = READ_EXTRA_BYTES; j < 128 + READ_EXTRA_BYTES; j++){
		if (ReadBuffer[j] == 'S')
		{
			if (j != READ_EXTRA_BYTES + 1)
			mode = j;
			break;
		}
		if (j == 64){
			xil_printf("S search failed\r\n");
			return XST_FAILURE;
		}
	}
    c  = ReadBuffer[mode];
    if (c == 'S') {
		memcpy(buf, (void*)(ReadBuffer+mode), readNumBytes*sizeof(uint8_t));
		return 0;
    } else {
    	xil_printf("No Srec data found!\r\n");
    	return 1;
    }
}
static uint8_t flash_get_srec_line ( uint8_t * buf ) { int Status ; uint8_t c ; uint8_t endFlag = 0 ; int count = 0 ; static int addrIncr = 0 , traverseCnt = 0 ; int mode = READ_EXTRA_BYTES ; static uint32_t readAddress , readNumBytes ; readAddress = flbuf + addrIncr * 128 ; readNumBytes = PAGE_SIZE ; addrIncr ++ ; if ( addrIncr == 4 ) usleep ( 100 ) ; SpiClearReadBuffer ( ) ; Status = SpiFlashRead ( & Spi , readAddress , readNumBytes , SPI_READ_COMMAND ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } u32 drr = Xil_In32 ( QSPI_BASEADDR + AXI_QSPI_SPIDRR ) ; for ( int i = PAGE_SIZE + READ_EXTRA_BYTES ; i > READ_EXTRA_BYTES - 1 ; i -- ) { if ( ReadBuffer [ i ] == ' \r ' ) { ReadBuffer [ i ] = ' \0 ' ; break ; } if ( i == READ_EXTRA_BYTES ) { xil_printf ( " \n \r " ) ; } } SpiFlashWaitForFlashReady ( ) ; for ( int j = READ_EXTRA_BYTES ; j < 128 + READ_EXTRA_BYTES ; j ++ ) { if ( ReadBuffer [ j ] == ' ' ) { if ( j != READ_EXTRA_BYTES + 1 ) mode = j ; break ; } if ( j == 64 ) { xil_printf ( " \r \n " ) ; return XST_FAILURE ; } } c = ReadBuffer [ mode ] ; if ( c == ' ' ) { memcpy ( buf , ( void * ) ( ReadBuffer + mode ) , readNumBytes * sizeof ( uint8_t ) ) ; return 0 ; } else { xil_printf ( " \r \n " ) ; return 1 ; } }
This function reads a page of memory using quad 4 bytes command read.
This function reads a page of memory using quad 4 bytes command read .
/*
	 * Set the
	 * - Address in the Serial Flash where the data is to be read from.
	 * - Number of bytes to be read from the Serial Flash.
	 * - Read Buffer to which the data is to be read.
	 */ // First addr: 0x0, 2nd addr: 0x80, 3rd adr: 0x100
 // Reading overlaps
 //qspi_print_control(qspi_check_control());
 //qspi_print_status(qspi_check_status());
 //rfifo = Xil_In32(QSPI_BASEADDR + XSP_RFO_OFFSET);
 //tfifo = Xil_In32(QSPI_BASEADDR + XSP_TFO_OFFSET);
 // Displays amount of data that is not yet read
 /* traverse ReadBuffer from the back, and terminate the buffer */ /* Readbuffer will consist of 9 extra bytes + (256 + addrOffset) of data  */ /* The readbuffer is filled */ /* Traverse the beginning of the array to find S and remove the extra bytes*/ /* If beginning is imcomplete, just skip to the next srec elements because
	 * we assume that this is already read. */ /* Index of SREC Data = CMD Extra bytes + 1 */ //xil_printf("Redundant bytes detected in buffer\r\n");
 /* Check if S is found. If it is, then remove extra bytes */ //*buf = ReadBuffer+(uint8_t)mode;
