1 |
2 |
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 |
|
|
supports AT45D* Flash on Port E
|
21 |
|
|
*/
|
22 |
|
|
|
23 |
|
|
#ifndef[ZTEX_FLASH2_H]
|
24 |
|
|
#define[ZTEX_FLASH2_H]
|
25 |
|
|
|
26 |
|
|
#define[@CAPABILITY_FLASH;]
|
27 |
|
|
|
28 |
|
|
#define[FLASH_SI_BIT][6]
|
29 |
|
|
#define[FLASH_SO_BIT][4]
|
30 |
|
|
#define[FLASH_CLK_BIT][5]
|
31 |
|
|
#ifeq[PRODUCT_IS][UFM-1_1]
|
32 |
|
|
#define[FLASH_CS_BIT][3]
|
33 |
|
|
//#warning[FLASH_CS_BIT=3]
|
34 |
|
|
#else
|
35 |
|
|
#define[FLASH_CS_BIT][7]
|
36 |
|
|
//#warning[FLASH_CS_BIT=7]
|
37 |
|
|
#endif
|
38 |
|
|
|
39 |
|
|
xdata BYTE flash_buffer = 1; // current buffer 1
|
40 |
|
|
xdata BYTE flash_bufferModified = 0; // 1 indicates that the current flash page needs to be reprogrammeded
|
41 |
|
|
xdata WORD flash_page = 0xffff; // current flash page
|
42 |
|
|
xdata WORD flash_nextPage = 0xffff; // next flash page (usually flashPage+1)
|
43 |
|
|
xdata WORD flash_maxPage = 0; // next flash page (usually flashPage+1)
|
44 |
|
|
xdata WORD flash_pageSize = 528; // page size (512 or 528)
|
45 |
|
|
xdata BYTE flash_register[4]; // used to store register content
|
46 |
|
|
|
47 |
|
|
/* *********************************************************************
|
48 |
|
|
***** flash_setPage *************************************************
|
49 |
|
|
********************************************************************* */
|
50 |
|
|
/* set the current page, i.e. executes the following steps
|
51 |
|
|
1. wait if busy
|
52 |
|
|
2. read the desired page from the flash array into the new buffer
|
53 |
|
|
3. write the old buffer to the flash array (if modified)
|
54 |
|
|
*/
|
55 |
|
|
static void flash_setPage(WORD page)
|
56 |
|
|
{
|
57 |
|
|
flash_nextPage = page + 1;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
/* *********************************************************************
|
61 |
|
|
***** flash_write ***************************************************
|
62 |
|
|
********************************************************************* */
|
63 |
|
|
/* writes <bytes> bytes at address <addr> to the current buffer starting at <offs>.
|
64 |
|
|
If <setNextPage> = 1 the next page (defined by <flash_nextPage> is set automatically
|
65 |
|
|
*/
|
66 |
|
|
static void flash_write(WORD addr, WORD bytes, WORD offs, BYTE setNextPage)
|
67 |
|
|
{
|
68 |
|
|
addr = 0;
|
69 |
|
|
bytes = 0;
|
70 |
|
|
offs = 0;
|
71 |
|
|
setNextPage = 0;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/* *********************************************************************
|
75 |
|
|
***** flash_write ***************************************************
|
76 |
|
|
********************************************************************* */
|
77 |
|
|
static void flash_readRegister(BYTE cmd)
|
78 |
|
|
{
|
79 |
|
|
BYTE oe,b,i,j;
|
80 |
|
|
|
81 |
|
|
b = OEE & ( ~( (1 << FLASH_SO_BIT) | (1 << FLASH_SI_BIT) | (1 << FLASH_CLK_BIT) | (1 << FLASH_CS_BIT) ) );
|
82 |
|
|
oe = IOE & b;
|
83 |
|
|
IOE = oe | (1 << FLASH_CS_BIT); // CS = 1
|
84 |
|
|
OEE = b | (1 << FLASH_SI_BIT) | (1 << FLASH_CLK_BIT) | (1 << FLASH_CS_BIT);
|
85 |
|
|
IOE = oe; // CS = 0
|
86 |
|
|
|
87 |
|
|
// CLK=0
|
88 |
|
|
for ( i=0; i<8; i++ ) {
|
89 |
|
|
IOE = b = oe | ((cmd & 128) >> (7-FLASH_SI_BIT)); // CLK = 0, SI=x
|
90 |
|
|
IOE = b | (1 << FLASH_CLK_BIT); // CLK = 1, SI=x
|
91 |
|
|
cmd <<= 1;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
for ( j=0; j<4; j++ ) {
|
95 |
|
|
// CLK = 1
|
96 |
|
|
b = 0;
|
97 |
|
|
for ( i=0; i<8; i++ ) {
|
98 |
|
|
IOE = oe; // CLK = 0, SO=x
|
99 |
|
|
b = (b << 1) | ((IOE >> FLASH_SO_BIT) & 1);
|
100 |
|
|
IOE = oe | (1 << FLASH_CLK_BIT); // CLK = 1
|
101 |
|
|
}
|
102 |
|
|
flash_register[j]=b;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
IOE = oe | (1 << FLASH_CS_BIT); // CS = 1
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
/* *********************************************************************
|
110 |
|
|
***** EP0 vendor request 0x40 ***************************************
|
111 |
|
|
********************************************************************* */
|
112 |
|
|
ADD_EP0_VENDOR_REQUEST((0x40,,
|
113 |
|
|
flash_readRegister(SETUPDAT[2]);
|
114 |
|
|
EP0BUF[0] = flash_register[0];
|
115 |
|
|
EP0BUF[1] = flash_register[1];
|
116 |
|
|
EP0BUF[2] = flash_register[2];
|
117 |
|
|
EP0BUF[3] = flash_register[3];
|
118 |
|
|
EP0BCH = 0;
|
119 |
|
|
EP0BCL = 4;
|
120 |
|
|
,,));;
|
121 |
|
|
|
122 |
|
|
#endif /*ZTEX_FLASH2_H*/
|