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 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 ZTEX
/*!
2
   ZTEX Firmware Kit for EZ-USB Microcontrollers
3
   Copyright (C) 2009-2010 ZTEX e.K.
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 flash memory
21
*/
22
 
23
 
24
code BYTE fpga_flash_boot_id[] = {'Z','T','E', 'X', 'B', 'S', '\1', '\1'};
25
 
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
 
34
        1 : fpga already configured
35
        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
    for ( k=1; k<i; k++ ) {
79
        if ( flash_read_init( k ) )             // prepare reading sector k
80
            return 2;
81
        fpga_send_bitstream_from_flash( flash_sector_size );
82
        flash_read_finish(0);                    // finish read opration
83
    }
84
    if ( flash_read_init( i ) )                 // prepare reading sector i
85
        return 2;
86
    fpga_send_bitstream_from_flash( j );
87
    flash_read_finish(flash_sector_size - j);   // finish read opration
88
 
89
    finish_fpga_configuration();                // finish the FPGA configuration
90
 
91
    OEA &= ~bmBIT1;
92
    if ( IOA1 )  {                              // FPGA configured
93
        IOA1 = 1;
94
        OEA |= bmBIT1;
95
        return 0;
96
    }
97
 
98
    IOA1 = 1;
99
    OEA |= bmBIT1;
100
    return 4;                                   // FPGA not configured correctly
101
}
102
 
103
/* *********************************************************************
104
   ***** fpga_first_free_sector ****************************************
105
   ********************************************************************* */
106
// First free sector. Returns 0 if no boot sector exeists.   
107
// Use the macro FLASH_FIRST_FREE_SECTOR instead of this function.
108
#define[FLASH_FIRST_FREE_SECTOR][fpga_first_free_sector()];
109
WORD fpga_first_free_sector() {
110
    BYTE i,j;
111
    flash_read_init( 0 );                                // prepare reading sector 0
112
    for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
113
    if ( i != 8 ) {
114
        flash_read_finish(flash_sector_size - i);       // dummy-read the rest of the sector + finish read opration
115
        return 0;
116
    }
117
    i=flash_read_byte();
118
    j=flash_read_byte();
119
    flash_read_finish(flash_sector_size - 10);          // dummy-read the rest of the sector + finish read opration
120
 
121
    return (i | (j<<8))+1;
122
}
123
 
124
 
125
/* *********************************************************************
126
   ***** fpga_configure_from_flash_init ********************************
127
   ********************************************************************* */
128
// this function is colled by init_USB;
129
void fpga_configure_from_flash_init() {
130
    fpga_flash_result = fpga_configure_from_flash(0);
131
    if ( fpga_flash_result == 1 ) {
132
        post_fpga_config();
133
    }
134
    else if ( fpga_flash_result == 4 ) {
135
        fpga_flash_result = fpga_configure_from_flash(0);        // up to two tries
136
    }
137
}
138
 
139
 
140
#define[INIT_CMDS;][INIT_CMDS;
141
fpga_flash_result= 255;
142
]

powered by: WebSVN 2.1.0

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