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

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [include/] [ztex-fpga-flash.h] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 ZTEX
/*!
2 8 ZTEX
   ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
3
   Copyright (C) 2009-2011 ZTEX GmbH.
4 4 ZTEX
   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 flash memory
21
*/
22
 
23
 
24 8 ZTEX
__code BYTE fpga_flash_boot_id[] = {'Z','T','E', 'X', 'B', 'S', '\1', '\1'};
25 4 ZTEX
 
26
/* *********************************************************************
27
   ***** fpga_configure_from_flash *************************************
28
   ********************************************************************* */
29
/*
30
    Configure the FPGA using a bitstream from flash.
31
    If force == 0 a already configured FPGA is not re-configured.
32
    Return values:
33 8 ZTEX
 
34
        1 : FPGA already configured
35 4 ZTEX
        2 : Flash error
36
        3 : No bitstream found
37
        4 : Configuration error
38
 
39
    The format of the boot sector (secor 0 of the flash memeory) is
40
        0..7    ID, must be "ZTEXBS",1,1
41
        8..9    Number of used sectors, or 0 is disabled
42
        10..11  Number of bytes in the last sector, i.e. th total size of bitstream is ((bs[8] | (bs[9]<<8) - 1) * flash_sector_size + ((bs[10] | (bs[11]<<8)
43
        the rest of the sector is reserved for future use and preserved by the host software
44
*/
45
 
46
BYTE fpga_configure_from_flash( BYTE force) {
47
    WORD i,j,k;
48
    if ( force == 0 ) {
49
        OEA &= ~bmBIT1;
50
        if ( IOA1 )  {                  // FPGA already configured
51
            IOA1 = 1;
52
            OEA |= bmBIT1;
53
            return 1;
54
        }
55
    }
56
    {
57
        PRE_FPGA_RESET
58
    }
59
    reset_fpga_flash();                 // reset FPGA
60
 
61
    // read the boot sector
62
    if ( flash_read_init( 0 ) )          // prepare reading sector 0
63
        return 2;
64
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
65
    if ( i != 8 ) {
66
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
67
        return 3;
68
    }
69
    i = flash_read_byte();
70
    i |= flash_read_byte() << 8;
71
    j = flash_read_byte();
72
    j |= flash_read_byte() << 8;
73
    flash_read_finish(flash_sector_size - 12);          // dummy-read the rest of the sector + finish read opration
74
    if ( i==0 )
75
        return 3;
76
 
77
    // read the bitstream
78 9 ZTEX
    if ( flash_read_init( 1 ) )                 // prepare reading sector 1
79 8 ZTEX
        return 2;
80 4 ZTEX
    for ( k=1; k<i; k++ ) {
81
        fpga_send_bitstream_from_flash( flash_sector_size );
82 8 ZTEX
        if ( flash_read_next() )                // prepare reading next sector
83
            return 2;
84 4 ZTEX
    }
85
    fpga_send_bitstream_from_flash( j );
86
    flash_read_finish(flash_sector_size - j);   // finish read opration
87
 
88
    finish_fpga_configuration();                // finish the FPGA configuration
89
 
90
    OEA &= ~bmBIT1;
91
    if ( IOA1 )  {                              // FPGA configured
92
        IOA1 = 1;
93
        OEA |= bmBIT1;
94
        return 0;
95
    }
96
 
97
    IOA1 = 1;
98
    OEA |= bmBIT1;
99
    return 4;                                   // FPGA not configured correctly
100
}
101
 
102
/* *********************************************************************
103
   ***** fpga_first_free_sector ****************************************
104
   ********************************************************************* */
105
// First free sector. Returns 0 if no boot sector exeists.   
106
// Use the macro FLASH_FIRST_FREE_SECTOR instead of this function.
107
#define[FLASH_FIRST_FREE_SECTOR][fpga_first_free_sector()];
108
WORD fpga_first_free_sector() {
109
    BYTE i,j;
110
    flash_read_init( 0 );                                // prepare reading sector 0
111
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
112
    if ( i != 8 ) {
113
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
114
        return 0;
115
    }
116
    i=flash_read_byte();
117
    j=flash_read_byte();
118
    flash_read_finish(flash_sector_size - 10);          // dummy-read the rest of the sector + finish read opration
119
 
120
    return (i | (j<<8))+1;
121
}
122
 
123
 
124
/* *********************************************************************
125
   ***** fpga_configure_from_flash_init ********************************
126
   ********************************************************************* */
127
// this function is colled by init_USB;
128
void fpga_configure_from_flash_init() {
129 9 ZTEX
    if ( ! flash_enabled ) {
130
        fpga_flash_result = 2;
131
        return;
132
    }
133
 
134 4 ZTEX
    fpga_flash_result = fpga_configure_from_flash(0);
135
    if ( fpga_flash_result == 1 ) {
136
        post_fpga_config();
137
    }
138
    else if ( fpga_flash_result == 4 ) {
139
        fpga_flash_result = fpga_configure_from_flash(0);        // up to two tries
140
    }
141
}
142
 
143
 
144
#define[INIT_CMDS;][INIT_CMDS;
145
fpga_flash_result= 255;
146
]

powered by: WebSVN 2.1.0

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