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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [fx2/] [ztex-fpga-flash1.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-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
 
26
/*
27
    Common functions for FPGA configuration from microSD flash
28
*/
29
 
30
 
31
__code BYTE fpga_flash_boot_id[] = {'Z','T','E', 'X', 'B', 'S', '\1', '\1'};
32
 
33
/* *********************************************************************
34
   ***** fpga_configure_from_flash *************************************
35
   ********************************************************************* */
36
/*
37
    Configure the FPGA using a bitstream from flash.
38
    If force == 0 a already configured FPGA is not re-configured.
39
    Return values:
40
 
41
        1 : FPGA already configured
42
        2 : Flash error
43
        3 : No bitstream found
44
        4 : Configuration error
45
 
46
    The format of the boot sector (secor 0 of the flash memeory) is
47
        0..7    ID, must be "ZTEXBS",1,1
48
        8..9    Number of used sectors, or 0 is disabled
49
        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)
50
        the rest of the sector is reserved for future use and preserved by the host software
51
*/
52
 
53
BYTE fpga_configure_from_flash( BYTE force) {
54
    WORD i,j,k;
55
    if ( force == 0 ) {
56
        OEA &= ~bmBIT1;
57
        if ( IOA1 )  {                  // FPGA already configured
58
            IOA1 = 1;
59
            OEA |= bmBIT1;
60
            return 1;
61
        }
62
    }
63
    {
64
        PRE_FPGA_RESET
65
    }
66
    reset_fpga_flash();                 // reset FPGA
67
 
68
    // read the boot sector
69
    if ( flash_read_init( 0 ) )          // prepare reading sector 0
70
        return 2;
71
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
72
    if ( i != 8 ) {
73
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
74
        return 3;
75
    }
76
    i = flash_read_byte();
77
    i |= flash_read_byte() << 8;
78
    j = flash_read_byte();
79
    j |= flash_read_byte() << 8;
80
    flash_read_finish(flash_sector_size - 12);          // dummy-read the rest of the sector + finish read opration
81
    if ( i==0 )
82
        return 3;
83
 
84
    // read the bitstream
85
    if ( flash_read_init( 1 ) )                 // prepare reading sector 1
86
        return 2;
87
    for ( k=1; k<i; k++ ) {
88
        fpga_send_bitstream_from_flash( flash_sector_size );
89
        if ( flash_read_next() )                // prepare reading next sector
90
            return 2;
91
    }
92
    fpga_send_bitstream_from_flash( j );
93
    flash_read_finish(flash_sector_size - j);   // finish read opration
94
 
95
    finish_fpga_configuration();                // finish the FPGA configuration
96
 
97
    OEA &= ~bmBIT1;
98
    if ( IOA1 )  {                              // FPGA configured
99
        IOA1 = 1;
100
        OEA |= bmBIT1;
101
        return 0;
102
    }
103
 
104
    IOA1 = 1;
105
    OEA |= bmBIT1;
106
    return 4;                                   // FPGA not configured correctly
107
}
108
 
109
/* *********************************************************************
110
   ***** fpga_first_free_sector ****************************************
111
   ********************************************************************* */
112
// First free sector. Returns 0 if no boot sector exeists.   
113
// Use the macro FLASH_FIRST_FREE_SECTOR instead of this function.
114
#define[FLASH_FIRST_FREE_SECTOR][fpga_first_free_sector()];
115
WORD fpga_first_free_sector() {
116
    BYTE i,j;
117
    flash_read_init( 0 );                                // prepare reading sector 0
118
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
119
    if ( i != 8 ) {
120
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
121
        return 0;
122
    }
123
    i=flash_read_byte();
124
    j=flash_read_byte();
125
    flash_read_finish(flash_sector_size - 10);          // dummy-read the rest of the sector + finish read opration
126
 
127
    return (i | (j<<8))+1;
128
}
129
 
130
 
131
/* *********************************************************************
132
   ***** fpga_configure_from_flash_init ********************************
133
   ********************************************************************* */
134
// this function is called by init_USB;
135
void fpga_configure_from_flash_init() {
136
    if ( ! flash_enabled ) {
137
        fpga_flash_result = 2;
138
        return;
139
    }
140
 
141
    fpga_flash_result = fpga_configure_from_flash(0);
142
    if ( fpga_flash_result == 1 ) {
143
        post_fpga_config();
144
    }
145
    else if ( fpga_flash_result == 4 ) {
146
        fpga_flash_result = fpga_configure_from_flash(0);        // up to two tries
147
    }
148
}
149
 
150
 
151
#define[INIT_CMDS;][INIT_CMDS;
152
fpga_flash_result= 255;
153
]

powered by: WebSVN 2.1.0

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