1 |
2 |
ZTEX |
/*%
|
2 |
|
|
ZTEX Firmware Kit for EZ-USB FX3 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 |
|
|
Example implementation of SD card as secondary flash device.
|
27 |
|
|
*/
|
28 |
|
|
|
29 |
|
|
#include "cyu3system.h"
|
30 |
|
|
#include "cyu3os.h"
|
31 |
|
|
#include "cyu3usb.h"
|
32 |
|
|
#include "cyu3dma.h"
|
33 |
|
|
#include "cyu3error.h"
|
34 |
|
|
#include "cyu3sib.h"
|
35 |
|
|
|
36 |
|
|
#define ZTEX_SD_EC_TIMEOUT 2
|
37 |
|
|
#define ZTEX_SD_EC_BUSY 3
|
38 |
|
|
#define ZTEX_SD_EC_PENDING 4
|
39 |
|
|
#define ZTEX_SD_EC_NOTSUPPORTED 7
|
40 |
|
|
#define ZTEX_SD_EC_RUNTIME 8
|
41 |
|
|
|
42 |
|
|
#define ENABLE_SPORT0
|
43 |
|
|
|
44 |
|
|
#define ZTEX_SD_DONE_EVENT (1 << 0) /* SIB transfer done event. */
|
45 |
|
|
|
46 |
|
|
struct __attribute__((__packed__)) ztex_sd_t {
|
47 |
|
|
uint8_t enabled; // 0 1: enabled, 0:disabled
|
48 |
|
|
uint16_t sector_size; // 1 in bytes
|
49 |
|
|
uint32_t sectors; // 3 number of sectors
|
50 |
|
|
uint8_t ec; // 7 error code
|
51 |
|
|
|
52 |
|
|
uint16_t page_sectors; // maximum amount of sectors per transfer
|
53 |
|
|
};
|
54 |
|
|
|
55 |
|
|
struct ztex_sd_t ztex_sd;
|
56 |
|
|
|
57 |
|
|
CyU3PDmaChannel ztex_sd_wr_handle, ztex_sd_rd_handle;
|
58 |
|
|
CyU3PEvent ztex_sd_event;
|
59 |
|
|
|
60 |
|
|
/* *********************************************************************
|
61 |
|
|
***** ztex_sd_callback **********************************************
|
62 |
|
|
********************************************************************* */
|
63 |
|
|
void ztex_sd_callback (uint8_t portId, CyU3PSibEventType evt, CyU3PReturnStatus_t status) {
|
64 |
|
|
if ( evt == CY_U3P_SIB_EVENT_XFER_CPLT ) CyU3PEventSet (&ztex_sd_event, ZTEX_SD_DONE_EVENT, CYU3P_EVENT_OR);
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/* *********************************************************************
|
68 |
|
|
***** ztex_sd_get_info **********************************************
|
69 |
|
|
********************************************************************* */
|
70 |
|
|
void ztex_sd_get_info () {
|
71 |
|
|
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
|
72 |
|
|
CyU3PSibDevInfo_t devInfo;
|
73 |
|
|
|
74 |
|
|
ztex_sd.enabled = 0;
|
75 |
|
|
ztex_sd.ec = ZTEX_SD_EC_NOTSUPPORTED;
|
76 |
|
|
ztex_sd.sector_size = 512;
|
77 |
|
|
ztex_sd.sectors = 0;
|
78 |
|
|
ztex_sd.page_sectors = 8;
|
79 |
|
|
|
80 |
|
|
// read SD card properties
|
81 |
|
|
ZTEX_REC( status = CyU3PSibQueryDevice (0, &devInfo) );
|
82 |
|
|
if (status != CY_U3P_SUCCESS) return;
|
83 |
|
|
|
84 |
|
|
if ( (devInfo.numUnits == 0) || ( (devInfo.cardType != CY_U3P_SIB_DEV_SD) && (devInfo.cardType != CY_U3P_SIB_DEV_MMC)) ) return; // not supported
|
85 |
|
|
|
86 |
|
|
if ( devInfo.blkLen > 2048 ) {
|
87 |
|
|
ZTEX_LOG("Unsupported sector size: %d", devInfo.blkLen);
|
88 |
|
|
return;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
ztex_sd.enabled = 1;
|
92 |
|
|
ztex_sd.sector_size = devInfo.blkLen;
|
93 |
|
|
ztex_sd.sectors = devInfo.numBlks;
|
94 |
|
|
ztex_sd.page_sectors = 4096 / devInfo.blkLen;
|
95 |
|
|
ztex_sd.ec = 0;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
/* *********************************************************************
|
99 |
|
|
***** ztex_sd_read **************************************************
|
100 |
|
|
********************************************************************* */
|
101 |
|
|
uint8_t ztex_sd_read (uint8_t* buf, uint32_t sector, uint32_t nblk) {
|
102 |
|
|
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
|
103 |
|
|
|
104 |
|
|
CyU3PDmaBuffer_t buf_p;
|
105 |
|
|
uint32_t b2, evStat;
|
106 |
|
|
|
107 |
|
|
buf_p.buffer = buf;
|
108 |
|
|
buf_p.status = 0;
|
109 |
|
|
|
110 |
|
|
while ( nblk>0 ) {
|
111 |
|
|
b2 = nblk;
|
112 |
|
|
if ( b2 > ztex_sd.page_sectors ) b2 = ztex_sd.page_sectors;
|
113 |
|
|
|
114 |
|
|
buf_p.size = b2 * ztex_sd.sector_size;
|
115 |
|
|
buf_p.count = 0;
|
116 |
|
|
|
117 |
|
|
ZTEX_REC(status = CyU3PDmaChannelSetupRecvBuffer (&ztex_sd_rd_handle, &buf_p));
|
118 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn1;
|
119 |
|
|
|
120 |
|
|
ZTEX_REC(status = CyU3PSibReadWriteRequest (CyTrue, 0, 0, b2, sector, (uint8_t)CY_U3P_SIB_SOCKET_1) );
|
121 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn1;
|
122 |
|
|
|
123 |
|
|
CyU3PEventGet(&ztex_sd_event, ZTEX_SD_DONE_EVENT, CYU3P_EVENT_OR_CLEAR, &evStat, 1000);
|
124 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn2;
|
125 |
|
|
|
126 |
|
|
// ZTEX_REC(status = CyU3PDmaChannelWaitForCompletion (&ztex_sd_rd_handle, 1000));
|
127 |
|
|
ZTEX_REC(status = CyU3PDmaChannelWaitForRecvBuffer (&ztex_sd_rd_handle, &buf_p, 1000) );
|
128 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn2;
|
129 |
|
|
|
130 |
|
|
nblk-=b2;
|
131 |
|
|
buf_p.buffer+=b2 * ztex_sd.sector_size;
|
132 |
|
|
sector+=b2;
|
133 |
|
|
}
|
134 |
|
|
return 0;
|
135 |
|
|
|
136 |
|
|
errreturn2:
|
137 |
|
|
CyU3PSibAbortRequest (0);
|
138 |
|
|
errreturn1:
|
139 |
|
|
CyU3PDmaChannelReset (&ztex_sd_rd_handle);
|
140 |
|
|
ztex_sd.ec = ZTEX_SD_EC_RUNTIME;
|
141 |
|
|
return ztex_sd.ec;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/* *********************************************************************
|
145 |
|
|
***** ztex_sd_write *************************************************
|
146 |
|
|
********************************************************************* */
|
147 |
|
|
uint8_t ztex_sd_write (uint8_t* buf, uint32_t sector, uint32_t nblk) {
|
148 |
|
|
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
|
149 |
|
|
|
150 |
|
|
CyU3PDmaBuffer_t buf_p;
|
151 |
|
|
uint32_t b2, evStat;
|
152 |
|
|
|
153 |
|
|
buf_p.buffer = buf;
|
154 |
|
|
buf_p.status = 0;
|
155 |
|
|
|
156 |
|
|
while ( nblk>0 ) {
|
157 |
|
|
b2 = nblk;
|
158 |
|
|
if ( b2 > ztex_sd.page_sectors ) b2 = ztex_sd.page_sectors;
|
159 |
|
|
|
160 |
|
|
buf_p.size = b2 * ztex_sd.sector_size;
|
161 |
|
|
buf_p.count = b2 * ztex_sd.sector_size;
|
162 |
|
|
|
163 |
|
|
ZTEX_REC(status = CyU3PSibReadWriteRequest (CyFalse, 0, 0, b2, sector, (uint8_t)CY_U3P_SIB_SOCKET_0) );
|
164 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn1;
|
165 |
|
|
|
166 |
|
|
ZTEX_REC(status = CyU3PDmaChannelSetupSendBuffer (&ztex_sd_wr_handle, &buf_p));
|
167 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn2;
|
168 |
|
|
|
169 |
|
|
ZTEX_REC( status = CyU3PEventGet(&ztex_sd_event, ZTEX_SD_DONE_EVENT, CYU3P_EVENT_OR_CLEAR, &evStat, 5000) );
|
170 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn2;
|
171 |
|
|
|
172 |
|
|
ZTEX_REC(status = CyU3PDmaChannelWaitForCompletion (&ztex_sd_wr_handle, 500));
|
173 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn2;
|
174 |
|
|
nblk-=b2;
|
175 |
|
|
buf_p.buffer+=b2 * ztex_sd.sector_size;
|
176 |
|
|
sector+=b2;
|
177 |
|
|
}
|
178 |
|
|
return 0;
|
179 |
|
|
|
180 |
|
|
errreturn2:
|
181 |
|
|
CyU3PSibAbortRequest (0);
|
182 |
|
|
errreturn1:
|
183 |
|
|
ztex_sd.ec = ZTEX_SD_EC_RUNTIME;
|
184 |
|
|
CyU3PDmaChannelReset (&ztex_sd_wr_handle);
|
185 |
|
|
return ztex_sd.ec;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
/* *********************************************************************
|
189 |
|
|
***** vr_flash2_info ************************************************
|
190 |
|
|
********************************************************************* */
|
191 |
|
|
// VR 0x44
|
192 |
|
|
uint8_t vr_flash2_info(uint16_t value, uint16_t index, uint16_t length ) {
|
193 |
|
|
ztex_sd_get_info();
|
194 |
|
|
CyU3PMemCopy( ztex_ep0buf, (uint8_t*)&ztex_sd, 8);
|
195 |
|
|
ZTEX_REC_RET( CyU3PUsbSendEP0Data( 8, ztex_ep0buf ) );
|
196 |
|
|
return 0;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
/* *********************************************************************
|
200 |
|
|
***** vr_flash2_read ************************************************
|
201 |
|
|
********************************************************************* */
|
202 |
|
|
// VR 0x45
|
203 |
|
|
uint8_t vr_flash2_read(uint16_t value, uint16_t index, uint16_t length ) {
|
204 |
|
|
// ZTEX_LOG("F2R: %d, %d", index, value);
|
205 |
|
|
if ( ztex_sd_read(ztex_ep0buf, (index << 16) | value, length/ztex_sd.sector_size ) ) return 255;
|
206 |
|
|
ZTEX_REC_RET ( CyU3PUsbSendEP0Data( length, ztex_ep0buf ) );
|
207 |
|
|
return 0;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
/* *********************************************************************
|
211 |
|
|
***** vr_flash2_write ************************************************
|
212 |
|
|
********************************************************************* */
|
213 |
|
|
// VC 0x46
|
214 |
|
|
uint8_t vc_flash2_write(uint16_t value, uint16_t index, uint16_t length ) {
|
215 |
|
|
// ZTEX_LOG("F2W: %d, %d", index, value);
|
216 |
|
|
ZTEX_REC_RET ( CyU3PUsbGetEP0Data (length, ztex_ep0buf, NULL) );
|
217 |
|
|
if ( ztex_sd_write(ztex_ep0buf, (index << 16) | value, length/ztex_sd.sector_size ) ) return 255;
|
218 |
|
|
return 0;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
/* *********************************************************************
|
222 |
|
|
***** ztex_sd_init **************************************************
|
223 |
|
|
********************************************************************* */
|
224 |
|
|
void ztex_sd_init () {
|
225 |
|
|
CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
|
226 |
|
|
CyU3PSibIntfParams_t intfParams;
|
227 |
|
|
|
228 |
|
|
ztex_sd.enabled = 0;
|
229 |
|
|
ztex_sd.ec = ZTEX_SD_EC_NOTSUPPORTED;
|
230 |
|
|
|
231 |
|
|
ztex_register_vendor_req(0x44, vr_flash2_info);
|
232 |
|
|
ztex_register_vendor_req(0x45, vr_flash2_read);
|
233 |
|
|
ztex_register_vendor_cmd(0x46, vc_flash2_write);
|
234 |
|
|
|
235 |
|
|
ZTEX_REC( CyU3PDeviceGpioOverride (43, CyTrue) );
|
236 |
|
|
ZTEX_REC( CyU3PDeviceGpioOverride (44, CyTrue) );
|
237 |
|
|
|
238 |
|
|
// Configure SD card port
|
239 |
|
|
// intfParams.cardDetType = CY_U3P_SIB_DETECT_DAT_3; // Card detect based on SD_DAT[3]
|
240 |
|
|
intfParams.cardDetType = CY_U3P_SIB_DETECT_GPIO; // Card detect based on deticated GPIO pin
|
241 |
|
|
intfParams.writeProtEnable = CyTrue; // Write protecttion handling enabled
|
242 |
|
|
// intfParams.useDdr = CyFalse; // DDR clocking enabled
|
243 |
|
|
// intfParams.maxFreq = CY_U3P_SIB_FREQ_104MHZ; // No S port clock limitation
|
244 |
|
|
intfParams.useDdr = CyFalse; // DDR clocking disabled
|
245 |
|
|
intfParams.maxFreq = CY_U3P_SIB_FREQ_52MHZ; // S port clock limited to 52 MHz
|
246 |
|
|
intfParams.cardInitDelay = 5; // SD/MMC initialization delay of 5ms
|
247 |
|
|
intfParams.resetGpio = 0xFF; // No reset GPIO's
|
248 |
|
|
intfParams.rstActHigh = CyTrue;
|
249 |
|
|
intfParams.voltageSwGpio = 0xFF; // No low voltage switch GPIO's
|
250 |
|
|
intfParams.lvGpioState = CyFalse;
|
251 |
|
|
intfParams.lowVoltage = CyFalse;
|
252 |
|
|
ZTEX_REC( status = CyU3PSibSetIntfParams (0, &intfParams) );
|
253 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn;
|
254 |
|
|
|
255 |
|
|
// start the module and initialize SD card
|
256 |
|
|
ZTEX_REC( status = CyU3PSibStart() );
|
257 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn;
|
258 |
|
|
|
259 |
|
|
// init event handler
|
260 |
|
|
ZTEX_REC(status = CyU3PEventCreate (&ztex_sd_event));
|
261 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn;
|
262 |
|
|
|
263 |
|
|
ZTEX_REC(status = CyU3PSibRegisterCbk (ztex_sd_callback));
|
264 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn;
|
265 |
|
|
|
266 |
|
|
// create DMA channels
|
267 |
|
|
CyU3PDmaChannelConfig_t dmaConfig;
|
268 |
|
|
CyU3PMemSet ((uint8_t *)&dmaConfig, 0, sizeof(dmaConfig));
|
269 |
|
|
dmaConfig.size = 4096;
|
270 |
|
|
// No buffers need to be allocated as this channel will be used only in override mode.
|
271 |
|
|
dmaConfig.count = 0;
|
272 |
|
|
dmaConfig.prodAvailCount = 0;
|
273 |
|
|
dmaConfig.dmaMode = CY_U3P_DMA_MODE_BYTE;
|
274 |
|
|
dmaConfig.prodHeader = 0;
|
275 |
|
|
dmaConfig.prodFooter = 0;
|
276 |
|
|
dmaConfig.consHeader = 0;
|
277 |
|
|
dmaConfig.notification = 0;
|
278 |
|
|
dmaConfig.cb = NULL;
|
279 |
|
|
|
280 |
|
|
// Channel to write to SD flash
|
281 |
|
|
dmaConfig.prodSckId = CY_U3P_CPU_SOCKET_PROD;
|
282 |
|
|
dmaConfig.consSckId = CY_U3P_SIB_SOCKET_0;
|
283 |
|
|
ZTEX_REC(status = CyU3PDmaChannelCreate (&ztex_sd_wr_handle, CY_U3P_DMA_TYPE_MANUAL_OUT, &dmaConfig) );
|
284 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn;
|
285 |
|
|
|
286 |
|
|
// Channel to read from SD flash
|
287 |
|
|
dmaConfig.prodSckId = CY_U3P_SIB_SOCKET_1;
|
288 |
|
|
dmaConfig.consSckId = CY_U3P_CPU_SOCKET_CONS;
|
289 |
|
|
ZTEX_REC(status = CyU3PDmaChannelCreate (&ztex_sd_rd_handle, CY_U3P_DMA_TYPE_MANUAL_IN, &dmaConfig) );
|
290 |
|
|
if (status != CY_U3P_SUCCESS) goto errreturn;
|
291 |
|
|
|
292 |
|
|
// read SD card properties
|
293 |
|
|
ztex_sd_get_info();
|
294 |
|
|
if ( ztex_sd.enabled == 0 ) goto noflashreturn;
|
295 |
|
|
|
296 |
|
|
ZTEX_LOG("Found %d MByte SD Flash",(ztex_sd.sectors>>14)*(ztex_sd.sector_size>>6));
|
297 |
|
|
return;
|
298 |
|
|
errreturn:
|
299 |
|
|
ztex_sd.ec = ZTEX_SD_EC_RUNTIME;
|
300 |
|
|
noflashreturn:
|
301 |
|
|
ztex_log("No SD Flash found");
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/* *********************************************************************
|
305 |
|
|
***** ztex_usb_stop_sd **********************************************
|
306 |
|
|
********************************************************************* */
|
307 |
|
|
void ztex_usb_stop_sd() {
|
308 |
|
|
CyU3PDmaChannelReset (&ztex_sd_wr_handle);
|
309 |
|
|
CyU3PDmaChannelReset (&ztex_sd_rd_handle);
|
310 |
|
|
}
|