OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_13/usb_fpga_2_13/trunk

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [include/] [ztex-fpga-flash2.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
3
   Copyright (C) 2009-2014 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
/*
20
    Common functions for FPGA configuration from SPI flash
21
*/
22
 
23
__code BYTE fpga_flash_boot_id[] = {'Z','T','E', 'X', 'B', 'S', '\1', '\1'};
24
 
25
/* *********************************************************************
26
   ***** fpga_first_free_sector ****************************************
27
   ********************************************************************* */
28
// First free sector. Returns 0 if no boot sector exeists.   
29
// Use the macro FLASH_FIRST_FREE_SECTOR instead of this function.
30
#define[FLASH_FIRST_FREE_SECTOR][fpga_first_free_sector()];
31
WORD fpga_first_free_sector() {
32
    BYTE i,j;
33
#ifdef[@CAPABILITY_MAC_EEPROM;]
34
    __xdata WORD buf[2];
35
 
36
    if ( config_data_valid ) {
37
        mac_eeprom_read ( (__xdata BYTE*) buf, 26, 4 );         // read actual and max bitstream size 
38
        if ( buf[1] != 0 ) {
39
            return ( ( ( buf[1] > buf[0] ? buf[1] : buf[0] ) - 1 ) >> ((flash_sector_size & 255) - 12) ) + 1;
40
        }
41
    }
42
#endif    
43
    flash_read_init( 0 );                                // prepare reading sector 0
44
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
45
    if ( i != 8 ) {
46
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
47
        return 0;
48
    }
49
    i=flash_read_byte();
50
    j=flash_read_byte();
51
    flash_read_finish(flash_sector_size - 10);          // dummy-read the rest of the sector + finish read opration
52
 
53
    return (i | (j<<8))+1;
54
}
55
 
56
/* *********************************************************************
57
   ***** fpga_configure_from_flash_init ********************************
58
   ********************************************************************* */
59
// this function is called by init_USB;
60
BYTE fpga_configure_from_flash_init() {
61
    BYTE i;
62
 
63
#ifdef[@CAPABILITY_MAC_EEPROM;]
64
    __xdata WORD buf[2];
65
 
66
    if ( config_data_valid ) {
67
        mac_eeprom_read ( (__xdata BYTE*) buf, 26, 4 );         // read actual and max bitstream size 
68
        if ( buf[1] != 0 ) {
69
            if ( buf[0] == 0 ) {
70
                return fpga_flash_result = 3;
71
            }
72
//          return 10;
73
            goto flash_config;
74
        }
75
//          return 15;
76
    }
77
#endif    
78
 
79
    // read the boot sector
80
    if ( flash_read_init( 0 ) )          // prepare reading sector 0
81
        return fpga_flash_result = 2;
82
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
83
    if ( i != 8 ) {
84
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
85
        return fpga_flash_result = 3;
86
    }
87
    i = flash_read_byte();
88
    i |= flash_read_byte();
89
    flash_read_finish(flash_sector_size - 10);          // dummy-read the rest of the sector + finish read opration
90
    if ( i==0 )
91
        return fpga_flash_result = 3;
92
 
93
flash_config:
94
    fpga_flash_result = fpga_configure_from_flash(0);
95
    if ( fpga_flash_result == 1 ) {
96
        post_fpga_config();
97
    }
98
    else if ( fpga_flash_result == 4 ) {
99
        fpga_flash_result = fpga_configure_from_flash(0);        // up to two tries
100
    }
101
    return fpga_flash_result;
102
}
103
 
104
#define[INIT_CMDS;][INIT_CMDS;
105
fpga_flash_result= 255;
106
]

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.