##// END OF EJS Templates
Fat32 library partially working, can read a file.
jeandet -
r62:25c982b9ed94 dev_alexis
parent child
Show More
@@ -1,423 +1,506
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2011, Alexis Jeandet
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------
19 19 -- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #include "bsp.h"
23 23 #include <streamdevices.h>
24 24 #include <malloc.h>
25 25 #include <gpio.h>
26 26 #include <uart.h>
27 27 #include <stdio.h>
28 28 #include <stm32f4xx_gpio.h>
29 29 #include <stm32f4xx_fsmc.h>
30 30 #include <i2c.h>
31 31 #include <core.h>
32 32 #include <terminal.h>
33 #include <sdcard-spi.h>
34 #include <sdcard.h>
35 #include <mbr.h>
36 #include <VS10XX.h>
37 33
38 34 uint32_t OSC0 =8000000;
39 35 uint32_t INTOSC =16000000;
40 36 uint32_t RTCOSC =32768;
41 37 uint32_t currentCpuFreq=0;
42 38 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
43 39
44 40 LCD_IF_t lcdIF0={
45 41 .init = &bsp_FSMC_init,
46 42 .writereg = &bsp_lcd0_write_reg,
47 43 .readreg = &bsp_lcd0_read_reg,
48 44 .writeGRAM = &bsp_lcd0_writeGRAM,
49 45 .readGRAM = &bsp_lcd0_readGRAM
50 46 };
51 47
52 48 LCD_t lcd0={
53 49 .interface = &lcdIF0,
54 50 .init = &ili9328init,
55 51 .paint = &ili9328paint,
56 52 .paintText = &ili9328paintText,
57 53 .paintFilRect = &ili9328paintFilRect,
58 54 .refreshenable = &ili9328refreshenable,
59 55 .width= 240,
60 56 .height = 320
61 57 };
62 58
63 59 terminal_t terminal0;
64 60
65 61 volatile int8_t* lcd0_CMD=(volatile int8_t*) 0x60000000;
66 62 volatile int8_t* lcd0_DATA=(volatile int8_t*)(0x61FFFFF0);
67 63
68 64 float VREF0 =(float)3.3;
69 65 volatile vs10XXDev audioCodec0;
70 66
67 sdcardDev sdcard2;
68 blkdevice sdcard2blkdev;
69 dikpartition sdcard2Part1;
70 FAT32fs sdcard2FAT32part1;
71 dikpartition sdcard2Part2;
72 FAT32fs sdcard2FAT32part2;
73 dikpartition sdcard2Part3;
74 FAT32fs sdcard2FAT32part3;
75 dikpartition sdcard2Part4;
76 FAT32fs sdcard2FAT32part4;
77
71 78 int bsp_init()
72 79 {
73 80 int i=0;
74 81 for(i=0;i<__MAX_OPENED_FILES__;i++)
75 82 {
76 83 __opnfiles__[i] = NULL;
77 84 }
78 85 bsp_GPIO_init();
79 86 bsp_uart_init();
80 87 bsp_iic_init();
81 88 bsp_FSMC_init();
82 89 bsp_GTerm_init();
83 90 bsp_spi_init();
84 91 bsp_SD_init();
85 92 bsp_Audio_init();
86 93 printf("\r=====================\n\r");
87 94 printf( "=====================\n\r");
88 95 printf(BSP);
89 96 printf(" initialised\n\r");
90 97 printf( "=====================\n\r");
91 98 return 1;
92 99 }
93 100
94 101 void bsp_GPIO_init()
95 102 {
96 103 gpio_t GPIO_Out_init_List[]={LED1,LED2,LCD_RESET,LCD_BACKL,VS1053xCS,VS1053xDCS,VS1053xRESET,SDCARD2CS};
97 104 for(int i=0;i<8;i++)
98 105 {
99 106 gpio_t GPIO_init = gpioopen(GPIO_Out_init_List[i]);
100 107 GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype;
101 108 gpiosetconfig(&GPIO_init);
102 109 }
103 gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD};
104 for(int i=0;i<2;i++)
110 gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD,BP3};
111 for(int i=0;i<3;i++)
105 112 {
106 113 gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]);
107 114 GPIO_init |= gpiohighspeed | gpioindir;
108 115 gpiosetconfig(&GPIO_init);
109 116 }
110 117 gpioclr(VS1053xRESET);
111 118 gpioset(VS1053xCS);
112 119 gpioset(VS1053xDCS);
113 120 gpioset(SDCARD2CS);
114 121 gpioclr(LCD_RESET);
115 122 gpioclr(LCD_BACKL);
116 123 }
117 124
118 125 void bsp_uart_init()
119 126 {
120 127
121 128 }
122 129
123 130 /*
124 131 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
125 132 D5 PE8 D6 PE9 D7 PE10
126 133 A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S
127 134 FSMC_NOE PD4 RD
128 135 */
129 136
130 137 int bsp_FSMC_init()
131 138 {
132 139 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
133 140 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
134 141
135 142 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
136 143 ,PD4,PD5,PD7,PE4};
137 144 for(int i=0;i<12;i++)
138 145 {
139 146 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
140 147 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
141 148 gpiosetconfig(&LCD_DBx);
142 149 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
143 150 }
144 151
145 152 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
146 153 FSMC_NORSRAMTimingInitTypeDef p;
147 154
148 155 /* Enable FSMC clock */
149 156 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
150 157
151 158 /*-- FSMC Configuration ------------------------------------------------------*/
152 159 /*----------------------- SRAM Bank 3 ----------------------------------------*/
153 160 /* FSMC_Bank1_NORSRAM4 configuration */
154 161 //p.FSMC_AddressSetupTime = 5;
155 162 //p.FSMC_AddressSetupTime = 1;
156 163 p.FSMC_AddressSetupTime = 3;
157 164 p.FSMC_AddressHoldTime = 3;
158 165 //p.FSMC_DataSetupTime = 9;
159 166 //ili9328 -> data setup time > 10ns
160 167 p.FSMC_DataSetupTime = 1;
161 168 if(getCpuFreq()>100*1000*1000)
162 169 p.FSMC_DataSetupTime = 2;// 11;
163 170 p.FSMC_BusTurnAroundDuration = 0;
164 171 p.FSMC_CLKDivision = 0;
165 172 p.FSMC_DataLatency = 0;
166 173 //ili9328 -> data hold time > 15ns
167 174 if(getCpuFreq()>66*1000*1000)
168 175 p.FSMC_DataLatency = 0;
169 176 p.FSMC_AccessMode = FSMC_AccessMode_A;
170 177
171 178 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
172 179 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
173 180 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
174 181 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
175 182 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
176 183 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
177 184 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
178 185 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
179 186 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
180 187 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
181 188 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
182 189 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
183 190 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
184 191 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
185 192 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
186 193
187 194 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
188 195
189 196 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
190 197 gpioset(LCD_RESET);
191 198 gpioclr(LCD_RESET);
192 199 delay_100us(500);
193 200 gpioset(LCD_RESET);
194 201 delay_100us(500);
195 202 lcd0.init(&lcd0);
196 203 gpioset(LCD_BACKL);
197 204 return 1;
198 205 }
199 206
200 207 void bsp_spi_init()
201 208 {
202 // gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO};
203 // for(int i=0;i<3;i++)
204 // {
205 // gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]);
206 // SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
207 // gpiosetconfig(&SPI_DBx);
208 // GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1);
209 // }
210 // spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1);
209 gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO};
210 for(int i=0;i<3;i++)
211 {
212 gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]);
213 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
214 gpiosetconfig(&SPI_DBx);
215 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1);
216 }
217 spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1);
211 218
212 219 gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO};
213 220 for(int i=0;i<3;i++)
214 221 {
215 222 gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]);
216 223 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
217 224 gpiosetconfig(&SPI_DBx);
218 225 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3);
219 226 }
220 227 spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1);
221 228
222 229 }
223 230
224 231
225 232 void bsp_iic_init()
226 233 {
227 234 // i2copenandconfig(i2c2,0,10000,PF0,PF1);
228 235 }
229 236
230 237
231 238 void bsp_Audio_init()
232 239 {
233 240 vs10XXopen(&audioCodec0,VS1053SPI,vs1052setXCS,vs1052setRST,vs1052setXDCS,vs10XXDREQ);
241 if(audioCodec0.VERSION!=UNKNOWN)
242 {
243 printf("detected Audio codec ");
244 switch (audioCodec0.VERSION) {
245 case VS1001:
246 printf("VS1001\n");
247 break;
248 case VS1011:
249 printf("VS1011\n");
250 break;
251 case VS1002:
252 printf("VS1002\n");
253 break;
254 case VS1003:
255 printf("VS1003\n");
256 break;
257 case VS1053:
258 printf("VS1053\n");
259 break;
260 case VS1033:
261 printf("VS1033\n");
262 break;
263 case VS1103:
264 printf("VS1103\n");
265 break;
266 default:
267 printf("Unknown device\n");
268 break;
269 }
270 }
234 271 }
235 272
236 273 void bsp_SD_init()
237 274 {
238 // sdcardDev sdcard2;
239 // blkdevice sdcard2blkdev;
240 // dikpartition sdcard2Part1;
241 // sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed);
242 // sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected);
243 // sdcard2blkdev.initialize(&sdcard2blkdev);
244 // mbropen(&sdcard2blkdev,&sdcard2Part1,1);
275 if(bspsdcardpresent())
276 {
277 sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed);
278 sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected);
279 if(sdcard2blkdev.initialize(&sdcard2blkdev)!=STA_NOINIT)
280 {
281 if(mbropen(&sdcard2blkdev,&sdcard2Part1,1)==MBRnoErr)
282 {
283 if(FATnoErr!=fat32open(&sdcard2FAT32part1,&sdcard2Part1))
284 printf("Can't open fat32 partition 1\n");
285 }
286 else
287 {
288 printf("Can't open or read MBR\n");
289 }
290
291 if(mbropen(&sdcard2blkdev,&sdcard2Part2,2)==MBRnoErr)
292 {
293 if(FATnoErr!=fat32open(&sdcard2FAT32part2,&sdcard2Part2))
294 printf("Can't open fat32 partition 2\n");
295 }
296 else
297 {
298 printf("Can't open or read MBR\n");
299 }
300
301 if(mbropen(&sdcard2blkdev,&sdcard2Part3,3)==MBRnoErr)
302 {
303 if(FATnoErr!=fat32open(&sdcard2FAT32part3,&sdcard2Part3))
304 printf("Can't open fat32 partition 3\n");
305 }
306 else
307 {
308 printf("Can't open or read MBR\n");
309 }
310
311 if(mbropen(&sdcard2blkdev,&sdcard2Part4,4)==MBRnoErr)
312 {
313 if(FATnoErr!=fat32open(&sdcard2FAT32part4,&sdcard2Part4))
314 printf("Can't open fat32 partition 4\n");
315 }
316 else
317 {
318 printf("Can't open or read MBR\n");
319 }
320 }
321 else
322 {
323 printf("Can't initialize SDCARD\n");
324 }
325 }
245 326 }
246 327
247 328 void vs1052setXCS(char val)
248 329 {
249 330 gpiosetval(VS1053xCS,(int)val);
250 331 }
251 332
252 333 void vs1052setXDCS(char val)
253 334 {
335 //gpiosetval(LED1,(int)val);
254 336 gpiosetval(VS1053xDCS,(int)val);
255 337 }
256 338
257 339 void vs1052setRST(char val)
258 340 {
259 341 if(val)
260 342 gpioset(VS1053xRESET);
261 343 else
262 344 gpioclr(VS1053xRESET);
263 345 }
264 346
265 347 int vs10XXDREQ()
266 348 {
267 349 return gpiogetval(VS1053DREQ);
268 350 }
269 351
270 352
271 353 void bsppowersdcard(char onoff) //always ON
272 354 {
273 355
274 356 }
275 357
276 358 char bspsdcardpresent()
277 359 {
278 360 return gpiogetval(SDCARD2CD);
279 361 }
280 362
281 363 char bspsdcardwriteprotected()
282 364 {
283 365 return 0;
284 366 }
285 367
286 368 void bspsdcardselect(char YESNO)
287 369 {
370 // gpiosetval(LED1,(int)YESNO);
288 371 if(YESNO)
289 372 gpioclr(SDCARD2CS);
290 373 else
291 374 gpioset(SDCARD2CS);
292 375 }
293 376
294 377
295 378 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
296 379 {
297 380 uint8_t* pt8 = (uint8_t*)(void*)&reg;
298 381 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
299 382 *lcd0_CMD=pt8[3];
300 383 *lcd0_CMD=pt8[2];
301 384 pt8 = (uint8_t*)(void*)&data;
302 385 *lcd0_DATA=pt8[3];
303 386 *lcd0_DATA=pt8[2];
304 387 #else
305 388 *lcd0_CMD=pt8[1];
306 389 *lcd0_CMD=pt8[0];
307 390 pt8 = (uint8_t*)(void*)&data;
308 391 *lcd0_DATA=pt8[1];
309 392 *lcd0_DATA=pt8[0];
310 393 #endif
311 394
312 395 }
313 396
314 397 uint32_t bsp_lcd0_read_reg(uint32_t reg)
315 398 {
316 399 uint8_t* pt8 = (uint8_t*)(void*)&reg;
317 400 uint32_t DATA=0;
318 401 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
319 402 *lcd0_CMD=pt8[3];
320 403 *lcd0_CMD=pt8[2];
321 404 pt8 = (uint8_t*)(void*)&DATA;
322 405 pt8[3]=*lcd0_DATA;
323 406 pt8[2]=*lcd0_DATA;
324 407 #else
325 408
326 409 *lcd0_CMD=pt8[1];
327 410 *lcd0_CMD=pt8[0];
328 411 pt8 = (uint8_t*)(void*)&DATA;
329 412 pt8[1]=*lcd0_DATA;
330 413 pt8[0]=*lcd0_DATA;
331 414 #endif
332 415
333 416 return DATA;
334 417 }
335 418
336 419 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
337 420 {
338 421 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
339 422 uint8_t* pt8 = (uint8_t*)(void*)&reg;
340 423 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
341 424 *lcd0_CMD=pt8[3];
342 425 *lcd0_CMD=pt8[2];
343 426 pt8 = (uint8_t*)(void*)buffer;
344 427 for(int i=0;i<(int)count;i++)
345 428 {
346 429 *lcd0_DATA=pt8[(2*i) +1];
347 430 *lcd0_DATA=pt8[2*i];
348 431 }
349 432 #else
350 433
351 434 *lcd0_CMD=pt8[1];
352 435 *lcd0_CMD=pt8[0];
353 436 pt8 = (uint8_t*)(void*)buffer;
354 437 for(int i=0;i<(int)count;i++)
355 438 {
356 439
357 440 *lcd0_DATA=pt8[(2*i) +1];
358 441 *lcd0_DATA=pt8[2*i];
359 442 }
360 443 #endif
361 444 }
362 445
363 446 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
364 447 {
365 448 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
366 449 uint8_t* pt8 = (uint8_t*)(void*)&reg;
367 450 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
368 451 *lcd0_CMD=pt8[3];
369 452 *lcd0_CMD=pt8[2];
370 453 pt8 = (uint8_t*)(void*)buffer;
371 454 pt8[0] = *lcd0_DATA;
372 455 pt8[0] = *lcd0_DATA;
373 456 for(int i=0;i<(int)count;i++)
374 457 {
375 458 pt8[(2*i) +1]=*lcd0_DATA;
376 459 pt8[2*i]=*lcd0_DATA;
377 460 }
378 461 #else
379 462
380 463 *lcd0_CMD=pt8[1];
381 464 *lcd0_CMD=pt8[0];
382 465 pt8 = (uint8_t*)(void*)buffer;
383 466 pt8[0] = *lcd0_DATA;
384 467 pt8[0] = *lcd0_DATA;
385 468 for(int i=0;i<(int)count;i++)
386 469 {
387 470 pt8[(2*i) +1]=*lcd0_DATA;
388 471 pt8[2*i]=*lcd0_DATA;
389 472 }
390 473 #endif
391 474 }
392 475
393 476 void bsp_GTerm_init()
394 477 {
395 478 if(__opnfiles__[1]==NULL)
396 479 {
397 480 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
398 481 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
399 482 terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1);
400 483 __opnfiles__[1] = fd1;
401 484 }
402 485 else
403 486 {
404 487
405 488 }
406 489 }
407 490
408 491
409 492
410 493
411 494
412 495
413 496
414 497
415 498
416 499
417 500
418 501
419 502
420 503
421 504
422 505
423 506
@@ -1,116 +1,133
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2011, Alexis Jeandet
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------
19 19 -- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #ifndef BSP_H
23 23 #define BSP_H
24 24 #include <stm32f4xx.h>
25 25 #include <stm32f4xx_gpio.h>
26 26 #include <stm32f4xx_rcc.h>
27 27 #include <gpio.h>
28 28 #include <spi.h>
29 29 #include <ili9328.h>
30 30 #include <genericLCD_Controler.h>
31 #include <sdcard-spi.h>
32 #include <sdcard.h>
33 #include <mbr.h>
31 34 #include <VS10XX.h>
35 #include <fat32.h>
32 36
33 37 #define __MAX_OPENED_FILES__ 4
34 38 #define __FS_ROOT_SIZE__ 4
35 39
36 40
37 41 #define LED1 PC15
38 42 #define LED2 PC14
39 43
44 #define BP3 PA0
45
40 46 #define LCD_RESET PD10
41 47 #define LCD_BACKL PE11
42 48
43 49 #define VS1053SPI spi1
44 50 #define VS1053xCS PA3
45 51 #define VS1053xRESET PA2
46 52 #define VS1053xDCS PA4
47 53 #define VS1053DREQ PA1
48 54 #define VS1053MISO PA6
49 55 #define VS1053MOSI PA7
50 56 #define VS1053SCK PA5
51 57
52 58
53 59
54 60 #define SDCARD2SPI spi3
55 61 #define SDCARD2CS PB8
56 62 #define SDCARD2CD PB9
57 63 #define SDCARD2MISO PB4
58 64 #define SDCARD2MOSI PB5
59 65 #define SDCARD2SCK PB3
60 66
61 67 extern float VREF0;
62 68
63 69 extern uint32_t currentCpuFreq;
64 70 extern LCD_t lcd0;
65 71
66 72 extern volatile vs10XXDev audioCodec0;
67 73
74 extern sdcardDev sdcard2;
75 extern blkdevice sdcard2blkdev;
76 extern dikpartition sdcard2Part1;
77 extern FAT32fs sdcard2FAT32part1;
78 extern dikpartition sdcard2Part2;
79 extern FAT32fs sdcard2FAT32part2;
80 extern dikpartition sdcard2Part3;
81 extern FAT32fs sdcard2FAT32part3;
82 extern dikpartition sdcard2Part4;
83 extern FAT32fs sdcard2FAT32part4;
84
68 85 extern int bsp_init();
69 86
70 87 extern void bsp_GPIO_init();
71 88 extern void bsp_uart_init();
72 89 extern void bsp_iic_init();
73 90 extern void bsp_spi_init();
74 91 extern void bsp_SD_init();
75 92 extern void bsp_Audio_init();
76 93 extern void bsp_GTerm_init();
77 94 extern int bsp_FSMC_init();
78 95
79 96 /* VS1053 */
80 97 extern void vs1052setXCS(char val);
81 98 extern void vs1052setXDCS(char val);
82 99 extern void vs1052setRST(char val);
83 100 extern int vs10XXDREQ();
84 101
85 102 /* SD CARD */
86 103 void bsppowersdcard(char onoff);
87 104 char bspsdcardpresent();
88 105 void bspsdcardselect(char YESNO);
89 106 char bspsdcardwriteprotected();
90 107
91 108
92 109 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data);
93 110 uint32_t bsp_lcd0_read_reg(uint32_t reg);
94 111 void bsp_lcd0_writeGRAM(void *buffer, uint32_t count);
95 112 void bsp_lcd0_readGRAM(void *buffer, uint32_t count);
96 113
97 114 #endif
98 115
99 116
100 117
101 118
102 119
103 120
104 121
105 122
106 123
107 124
108 125
109 126
110 127
111 128
112 129
113 130
114 131
115 132
116 133
@@ -1,108 +1,182
1 1 #include <stdio.h>
2 2 #include <fat32.h>
3 3 #include <gpio.h>
4 4 #include <uart.h>
5 5 #include <stm32f4xx.h>
6 6 #include <bsp.h>
7 7 #include <core.h>
8 8 #include <VS10XX.h>
9 9 #include <bsp.h>
10 10 #include <spi.h>
11 11 #include <sdcard.h>
12 12 #include <sdcard-spi.h>
13 13 #include <fat32.h>
14 14 #include <ucdirent.h>
15 15 #include <string.h>
16 #include <terminal.h>
16 17
17 18
18 19 extern streamdevice* __opnfiles__[];
20 char buffer[512];
19 21
20 22 void randBoxesDemo()
21 23 {
22 24 int16_t x,y,w,h,t,r;
23 25 x=rand()%240;
24 26 y=rand()%320;
25 27 w=rand()%(240-x);
26 28 if(x>y)
27 29 r=(rand()%(y))%(320-y);
28 30 else
29 31 r=(rand()%(x))%(240-x);
30 32 h=rand()%(320-y);
31 33 t=rand()%(10);
32 34 ili9328paintFilRect(&lcd0,x,y,w,h,rand(),t,rand());
33 35 //ili9328paintFilCirc(&lcd0,x,y,r,rand(),t,rand());
34 36 //delay_100us(10);
35 37 //for(volatile int i=0;i<(1024*2);i++);
36 38 }
39 void tryToplay1()
40 {
41
42 extern FAT32fs sdcard2FAT32part1;
43 extern volatile vs10XXDev audioCodec0;
44 ucdirent root;
45 char direntName[]=" \n";
46 uint32_t fileLba,nextLba;
47 int i=0;
48 spisetspeed(audioCodec0.SPIdev,2000000);
49 if(DIRENT_noErr==fat32mkdirent(&sdcard2FAT32part1,&root))
50 {
51 if(DIRENT_noErr==fat32getrootfirstent(&root))
52 {
53 do{
54 fat32getdirentname(&root,direntName);
55 printf("%s\n",direntName);
56 nextLba=fat32getdirentlba(&root);
57 printf("Streaming File\n");
58 do
59 {
60 fileLba = nextLba;
61 if(sdcard2FAT32part1.part->phy->read(sdcard2FAT32part1.part->phy,buffer,fileLba,1)==RES_OK)
62 {
63 for(i=0;i<512;i+=32)
64 {
65 vs10XXstream32bytes(&audioCodec0,buffer+i);
66 }
67 }
68 }while ((fat32nextsectorlba(&sdcard2FAT32part1,fileLba,&nextLba)==DIRENT_noErr) && (!gpiogetval(BP3)));
69 //vs10XXsoftReset(&audioCodec0);
70 while (gpiogetval(BP3))delay_100us(1000);
71 printf("looking for next file\n");
72 }while (DIRENT_noErr==fat32nextdirent(&root));
73 }
74 }
75 if(DIRENT_noErr==fat32mkdirent(&sdcard2FAT32part2,&root))
76 {
77 if(DIRENT_noErr==fat32getrootfirstent(&root))
78 {
79 fat32getdirentname(&root,direntName);
80 printf("%s\n",direntName);
81 nextLba=fat32getdirentlba(&root);
82 printf("Streaming File\n");
83 do
84 {
85 fileLba = nextLba;
86 if(sdcard2FAT32part2.part->phy->read(sdcard2FAT32part2.part->phy,buffer,fileLba,1)==RES_OK)
87 {
88 for(i=0;i<512;i+=32)
89 {
90 vs10XXstream32bytes(&audioCodec0,buffer+i);
91 }
92 }
93 }while ((fat32nextsectorlba(&sdcard2FAT32part2,fileLba,&nextLba)==DIRENT_noErr) && (!gpiogetval(BP3)));
94 while (gpiogetval(BP3))delay_100us(1000);
95 }
96 }
97 if(DIRENT_noErr==fat32mkdirent(&sdcard2FAT32part3,&root))
98 {
99 if(DIRENT_noErr==fat32getrootfirstent(&root))
100 {
101 fat32getdirentname(&root,direntName);
102 printf("%s\n",direntName);
103 nextLba=fat32getdirentlba(&root);
104 printf("Streaming File\n");
105 do
106 {
107 fileLba = nextLba;
108 if(sdcard2FAT32part3.part->phy->read(sdcard2FAT32part3.part->phy,buffer,fileLba,1)==RES_OK)
109 {
110 for(i=0;i<512;i+=32)
111 {
112 vs10XXstream32bytes(&audioCodec0,buffer+i);
113 }
114 }
115 }while ((fat32nextsectorlba(&sdcard2FAT32part3,fileLba,&nextLba)==DIRENT_noErr) && (!gpiogetval(BP3)));
116 while (gpiogetval(BP3))delay_100us(1000);
117 }
118 }
119 if(DIRENT_noErr==fat32mkdirent(&sdcard2FAT32part4,&root))
120 {
121 if(DIRENT_noErr==fat32getrootfirstent(&root))
122 {
123 fat32getdirentname(&root,direntName);
124 printf("%s\n",direntName);
125 nextLba=fat32getdirentlba(&root);
126 printf("Streaming File\n");
127 do
128 {
129 fileLba = nextLba;
130 if(sdcard2FAT32part4.part->phy->read(sdcard2FAT32part4.part->phy,buffer,fileLba,1)==RES_OK)
131 {
132 for(i=0;i<512;i+=32)
133 {
134 vs10XXstream32bytes(&audioCodec0,buffer+i);
135 }
136 }
137 }while ((fat32nextsectorlba(&sdcard2FAT32part4,fileLba,&nextLba)==DIRENT_noErr) && (!gpiogetval(BP3)));
138 while (gpiogetval(BP3))delay_100us(1000);
139 }
140 }
141 }
142 void tryToplay2()
143 {
144
145 extern blkdevice sdcard2blkdev;
146 int i=0,l=0;
147
148 spisetspeed(audioCodec0.SPIdev,4000000);
149 printf("Streaming File\n");
150
151 //printf("New LBA=0x%X\n",nextLba);
152
153 while(sdcard2blkdev.read(&sdcard2blkdev,buffer,l++,1)==RES_OK)
154 {
155
156 for(i=0;i<512;i+=32)
157 {
158 vs10XXstream32bytes(&audioCodec0,buffer+i);
159 }
160 }
161 }
37 162
38 163 int main()
39 164 {
40 while(!bspsdcardpresent());
41 delay_100us(5000);
42 printf("About to initialize SDCARD");
43 sdcardDev sdcard2;
44 blkdevice sdcard2blkdev;
45 dikpartition sdcard2Part1;
46 FAT32fs partoche;
47 sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed);
48 sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected);
49 sdcard2blkdev.initialize(&sdcard2blkdev);
50 mbropen(&sdcard2blkdev,&sdcard2Part1,1);
51 if(FATnoErr==fat32open(&partoche,&sdcard2Part1))
52 {
53 printf("FAT32 partition opened\n");
54 char test[]=" \n";
55 ucdirent root,folder;
56 fat32mkdirent(&partoche,&root);
57 fat32getrootfirstent(&root);
58 fat32getVolName(&partoche,test);
59 printf("Vol Name=%s\n",test);
60 fat32getdirentname(&root,test);
61 printf("%s\n",test);
62 while (DIRENT_EndOfDir!=fat32nextdirent(&root))
63 {
64 if(test[0]=='m')
65 {
66 printf("enter music folder\n");
67 fat32enterdirent(&root,&folder);
68 while (DIRENT_EndOfDir!=fat32nextdirent(&folder))
69 {
70 printf("%s\n",test);
71 }
72 }
73 fat32getdirentname(&root,test);
74 printf("%s\n",test);
75 }
76 if(test[0]=='m')
77 {
78 printf("enter music folder\n");
79 fat32enterdirent(&root,&folder);
80 while (DIRENT_EndOfDir!=fat32nextdirent(&folder))
81 {
82 printf("%s\n",test);
83 }
84 }
85 }
86 while(1)
87 {
88 // printf("Audio Codec reg0=0x%x\n",vs10XXcmdread(&audioCodec0,0));
89 // printf("hello ");
90 // bsp_lcd0_write_reg(1,0x1234);
91 // delay_100us(10000);
92 // randBoxesDemo();
93
94 gpioset(LED1);
95 delay_100us(10000);
96 gpioclr(LED1);
97 delay_100us(10000);
98
99 }
100 printf("hello world\n\r");
165 extern terminal_t terminal0;
166 extern volatile vs10XXDev audioCodec0;
167 printf("Volume=0x%x\n",vs10XXcmdread(&audioCodec0,VSVOL));
168 vs10XXcmdwrite(&audioCodec0,VSCLOCKF,0x2000);
169 vs10XXcmdwrite(&audioCodec0,VSVOL,0x2222);
170 printf("VSCLOCKF=0x%x\n",vs10XXcmdread(&audioCodec0,VSCLOCKF));
171 printf("VSMODE=0x%x\n",vs10XXcmdread(&audioCodec0,VSMODE));
172 terminal_clear(&terminal0);
173 // tryToplay2();
174 tryToplay1();
101 175 return 0;
102 176 }
103 177
104 178
105 179
106 180
107 181
108 182
@@ -1,72 +1,132
1 1 /*------------------------------------------------------------------------------
2 2 #-- This file is a part of the libuc, microcontroler library
3 3 #-- Copyright (C) 2011, Alexis Jeandet
4 4 #--
5 5 #-- This program is free software; you can redistribute it and/or modify
6 6 #-- it under the terms of the GNU General Public License as published by
7 7 #-- the Free Software Foundation; either version 3 of the License, or
8 8 #-- (at your option) any later version.
9 9 #--
10 10 #-- This program is distributed in the hope that it will be useful,
11 11 #-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 #-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 #-- GNU General Public License for more details.
14 14 #--
15 15 #-- You should have received a copy of the GNU General Public License
16 16 #-- along with this program; if not, write to the Free Software
17 17 #-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 #-------------------------------------------------------------------------------
19 19 #-- Author : Alexis Jeandet
20 20 #-- Mail : alexis.jeandet@gmail.com
21 21 #-------------------------------------------------------------------------------*/
22 22 #include <spi.h>
23 23 #include <VS10XX.h>
24 24 #include <core.h>
25 #include <stdio.h>
25 26 //#include <bsp.h>
26 27
27 28 void vs10XXopen(vs10XXDev *codec, spi_t dev, void (*setxCS)(char), void (*setxRST)(char), void (*setxDCS)(char), int (*getDREQ)())
28 29 {
29 30 codec->SPIdev = dev;
30 31 codec->setxCS = setxCS;
31 32 codec->setxDCS = setxDCS;
32 33 codec->setxRST = setxRST;
33 34 codec->getDREQ = getDREQ;
34 35 codec->setxDCS(1);
35 36 codec->setxCS(1);
36 37 codec->setxRST(0);
37 38 delay_100us(2);
38 39 codec->setxRST(1);
40 int status;
41 do{
42 status= vs10XXcmdread(codec,VSSTATUS);
43 }while (status==0xc);
44 printf("Status=0x%x\n",status);
45 status = (status>>4) & 0xF;
46 switch (status) {
47 case 0:
48 codec->VERSION = VS1001;
49 break;
50 case 1:
51 codec->VERSION = VS1011;
52 break;
53 case 2:
54 codec->VERSION = VS1002;
55 break;
56 case 3:
57 codec->VERSION = VS1003;
58 break;
59 case 4:
60 codec->VERSION = VS1053;
61 break;
62 case 5:
63 codec->VERSION = VS1033;
64 break;
65 case 7:
66 codec->VERSION = VS1103;
67 break;
68 default:
69 codec->VERSION = UNKNOWN;
70 break;
71 }
39 72 }
40 73
41 74 void vs10XXsoftreset(vs10XXDev* dev)
42 75 {
43 76 vs10XXcmdwrite(dev,VSMODE,(1<<2));
44 77 }
45 78
79 void vs10XXsetCansel(vs10XXDev* dev)
80 {
81 int mode=vs10XXcmdread(dev,VSMODE);
82 vs10XXcmdwrite(dev,VSMODE,mode|8);
83 }
84
85 void vs10XXsoftReset(vs10XXDev* dev)
86 {
87 int mode=vs10XXcmdread(dev,VSMODE);
88 vs10XXcmdwrite(dev,VSMODE,mode|4);
89 }
90
91 int vs10XXcanselAccepted(vs10XXDev* dev)
92 {
93 int mode=vs10XXcmdread(dev,VSMODE);
94 return ((mode & 8)!=8);
95 }
96
46 97 int vs10XXcmdread(vs10XXDev* dev,char address)
47 98 {
48 99 int result;
49 100 while(!dev->getDREQ());
50 101 dev->setxCS(0);
51 102 spiputw(dev->SPIdev,3);
52 103 spiputw(dev->SPIdev,address);
53 104 result = (0xFF00 & (spigetw(dev->SPIdev)<<8)) + (0xFF & spigetw(dev->SPIdev));
54 105 dev->setxCS(1);
55 106 return result;
56 107 }
57 108
58 109
59 110 void vs10XXcmdwrite(vs10XXDev* dev,char address,int value)
60 111 {
61 112 while(!dev->getDREQ());
62 113 dev->setxCS(0);
63 114 spiputw(dev->SPIdev,2<<8);
64 115 spiputw(dev->SPIdev,address);
65 116 spiputw(dev->SPIdev,value>>8);
66 117 spiputw(dev->SPIdev,value);
67 118 dev->setxCS(1);
68 119 }
69 120
70 121
122 void vs10XXstream32bytes(vs10XXDev* dev,char* buffer)
123 {
124 while(!dev->getDREQ());
125 dev->setxCS(1);
126 dev->setxDCS(0);
127 spiputnc(dev->SPIdev,buffer,32);
128 dev->setxDCS(1);
129
130 }
71 131
72 132
@@ -1,326 +1,336
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2012, Alexis Jeandet
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------
19 19 -- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #define FAT32_PRIVATE
23 23 #include <fat32.h>
24 24 #include <stdio.h>
25 25 #include <stdint.h>
26 26 #include <hexviewer.h>
27 27
28 28 char fat32buff[512];
29 29 uint32_t lastSecAddrs=-1;
30 30 FAT32fs* lastFATFS=0;
31 31
32 32 int fat32open(FAT32fs* fs,dikpartition* part)
33 33 {
34 34 char sector[512];
35 35 fs->part = part;
36 36 if(!isFat32(fs->part->TypeCode))return FATBadpart;
37 37 if(fs->part->phy->read(fs->part->phy,sector,fs->part->LBABegin,1)!=RES_OK)return FATReadErr;
38 38 fs->BPB_BytsPerSec = fat32Ui8_2_Ui16(sector,BPB_BytsPerSecoff);
39 39 fs->BPB_RsvdSecCnt = fat32Ui8_2_Ui16(sector,BPB_RsvdSecCntoff);
40 40 fs->BPB_FATSz32 = fat32Ui8_2_Ui32(sector,BPB_FATSz32off);
41 41 fs->BPB_RootClus = fat32Ui8_2_Ui32(sector,BPB_RootClusoff);
42 42 fs->BPB_SecPerClus = sector[BPB_SecPerClusoff];
43 43 fs->BPB_NumFATs = sector[BPB_NumFATsoff];
44 44 fs->fat_begin_lba = (unsigned int)fs->part->LBABegin + (unsigned int)(unsigned short)fs->BPB_RsvdSecCnt;
45 45 fs->cluster_begin_lba = (unsigned int)fs->fat_begin_lba + ((unsigned int)fs->BPB_NumFATs * (unsigned int)fs->BPB_FATSz32 );
46 46 return FATnoErr;
47 47 }
48 48
49 49
50 50 int fat32mkdirent(FAT32fs* fs,ucdirent* dirent)
51 51 {
52 52 if(fs==0) return DIRENT_BabArg;
53 53 if(dirent==0) return DIRENT_BabArg;
54 54 dirent->fs = (UHANDLE)fs;
55 55 dirent->getrootfirstent = &fat32getrootfirstent;
56 56 dirent->nextdirent = &fat32nextdirent;
57 57 return DIRENT_noErr;
58 58 }
59 59
60 60
61 61 int fat32getrootfirstent(ucdirent* entry)
62 62 {
63 63 if(entry==0) return DIRENT_BabArg;
64 64 entry->CurrentSec = ((FAT32fs*)entry->fs)->cluster_begin_lba;
65 65 entry->Currententry = 0;
66 66 return fat32nextdirent(entry);
67 67 }
68 68
69 69
70 70 int fat32getVolName(FAT32fs* fs,char* Name)
71 71 {
72 72 if(fs==0) return DIRENT_BabArg;
73 73 if(Name==0) return DIRENT_BabArg;
74 74 int i=0;
75 75 ucdirent entry;
76 76 if(DIRENT_noErr==fat32mkdirent(fs,&entry))
77 77 {
78 78 entry.CurrentSec = ((FAT32fs*)entry.fs)->cluster_begin_lba;
79 79 entry.Currententry = -1;
80 80 if(DIRENT_noErr!=fat32nextdirent(&entry))return DIRENT_ReadErr;
81 81 for(i=0;i<16;i++)
82 82 {
83 83 Name[i] = entry.DIR_Name[i];
84 84 }
85 85 return DIRENT_noErr;
86 86 }
87 87 return DIRENT_ReadErr;
88 88 }
89 89
90 90
91 91 int fat32enterdirent(ucdirent* currententry,ucdirent* firstentry)
92 92 {
93 93 if(firstentry==0) return DIRENT_BabArg;
94 94 if(currententry==0) return DIRENT_BabArg;
95 95 fat32mkdirent(((FAT32fs*)currententry->fs),firstentry);
96 96 firstentry->CurrentSec = fat32getdirentlba(currententry);
97 97 firstentry->Currententry = -1;
98 98 return fat32nextdirent(firstentry);
99 99 }
100 100
101 101
102 102 int fat32nextdirent(ucdirent* entry)
103 103 {
104 104 uint32_t n=0,res=DIRENT_noErr,k,j;
105 k=(((signed char)entry->Currententry)+1)*32; /*Restart at last dir entry*/
105 k=(0xFF&((signed char)entry->Currententry)+1)*32; /*Restart at last dir entry*/
106 106 j=entry->CurrentSec;// /*Restart at last sector*/
107 107 do
108 108 {
109 109 fat32sectorreadout(((FAT32fs*)entry->fs),j);
110 110 for(k=k;k<(512);k+=32) /*512 Bytes per sector and 32 bytes per entry*/
111 111 {
112 112 if((fat32buff[DIR_Nameoff+k]!=(char)0xE5)) /*0xE5 = free entry*/
113 113 {
114 114 if(fat32buff[DIR_Nameoff+k]==(char)0)
115 115 return DIRENT_EndOfDir; /*0x00 = end of dir*/
116 116 else
117 if((fat32buff[DIR_Attroff + k]==ATTR_DIRECTORY)||(fat32buff[DIR_Attroff + k]==ATTR_ARCHIVE)||(fat32buff[DIR_Attroff + k]==(ATTR_READ_ONLY)))
117 if((fat32buff[DIR_Attroff + k]==(char)ATTR_DIRECTORY)||(fat32buff[DIR_Attroff + k]==(char)ATTR_ARCHIVE)||(fat32buff[DIR_Attroff + k]==(char)(ATTR_READ_ONLY)))
118 118 {
119 119 for(n=0;n<16;n++)entry->DIR_Name[n] = '\0';
120 120 for(n=0;n<11;n++)entry->DIR_Name[n] = fat32buff[n+DIR_Nameoff + k];
121 121 entry->DIR_Attr = (uint8_t)fat32buff[DIR_Attroff + k];
122 122 entry->DIR_CrtTimeTenth = (uint8_t)fat32buff[DIR_CrtTimeTenthoff + k];
123 123 entry->DIR_CrtTime = fat32extract16b(fat32buff,(DIR_CrtTimeoff + k));
124 124 entry->DIR_CrtDate = fat32extract16b(fat32buff,(DIR_CrtDateoff + k));
125 125 entry->DIR_LstAccDate = fat32extract16b(fat32buff,(DIR_LstAccDateoff + k));
126 126 entry->DIR_FstClusHI = fat32extract16b(fat32buff, (DIR_FstClusHIoff + k));
127 127 entry->DIR_WrtTime = fat32extract16b(fat32buff,(DIR_WrtTimeoff + k));
128 128 entry->DIR_FstClustLO = fat32extract16b(fat32buff, (DIR_FstClusLOoff + k));
129 129 entry->DIR_FileSize = fat32extract32b(fat32buff,(DIR_FileSizeoff + k));
130 130 entry->CurrentSec = j;
131 131 entry->Currententry = (unsigned char)(k/32);
132 132 return DIRENT_noErr;
133 133 }
134 134 }
135 135
136 136 }
137 137 j=fat32nextsectorlba(((FAT32fs*)entry->fs),j,&k);
138 138 if(j!=DIRENT_noErr)
139 139 {
140 140 if(j==DIRENT_LastSect) return DIRENT_EndOfDir; /*Not clean but can avoid some bugs*/
141 141 return j;
142 142 }
143 143 j=k;
144 144 k=0;
145 145 }while(res==DIRENT_noErr);
146 146 return res;
147 147 }
148 148
149 149
150 150 int fat32getdirentname(ucdirent* entry,char* nameBuffer)
151 151 {
152 152 uint32_t j;
153 153 int k,n,i;
154 154 int res=DIRENT_noErr;
155 155 char longName=0;
156 156 k=(((signed char)entry->Currententry))*32;
157 157 j=entry->CurrentSec;
158 158 fat32sectorreadout(((FAT32fs*)entry->fs),j);
159 159 longName |= fat32buff[DIR_Nameoff + 6 + k]=='~';
160 160 if(k>=32)
161 161 {
162 162 longName |= ((fat32buff[DIR_Nameoff + k-32]&0x40)==0x40)&((fat32buff[DIR_Nameoff + k-32 +11])==ATTR_LONGNAME);
163 163 }
164 164 else
165 165 {
166 166 uint32_t j_old=j;
167 167 if(fat32prevsectorlba((FAT32fs*)entry->fs, j,&j)!=DIRENT_noErr)return DIRENT_ReadErr;
168 168 fat32sectorreadout(((FAT32fs*)entry->fs),j);
169 169 k=512;
170 170 longName |= ((fat32buff[DIR_Nameoff + k-32]&0x40)==0x40)&((fat32buff[DIR_Nameoff + k-32 +11])==ATTR_LONGNAME);
171 171 if(!longName)
172 172 {
173 173 k=0;
174 174 j=j_old;
175 175 fat32sectorreadout(((FAT32fs*)entry->fs),j);
176 176 k=0;
177 177 }
178 178 }
179 179 if(longName) //long Name? or lower case name
180 180 {
181 181 i=0;
182 182 do{
183 183 if(k==0)
184 184 {
185 185 //get previous sector
186 186 if(fat32prevsectorlba((FAT32fs*)entry->fs, j,&j)!=DIRENT_noErr)return DIRENT_ReadErr;
187 187 fat32sectorreadout(((FAT32fs*)entry->fs),j);
188 188 k=512-32;
189 189 for(n=0;n<5;n++)nameBuffer[i++] = fat32buff[((n<<1)|1) + k];
190 190 for(n=14;n<26;n+=2)nameBuffer[i++] = fat32buff[n + k];
191 191 for(n=28;n<32;n+=2)nameBuffer[i++] = fat32buff[n + k];
192 192 }
193 193 else
194 194 {
195 195 k-=32;
196 196 for(n=0;n<5;n++)nameBuffer[i++] = fat32buff[((n<<1)|1) + k];
197 197 for(n=14;n<26;n+=2)nameBuffer[i++] = fat32buff[n + k];
198 198 for(n=28;n<32;n+=2)nameBuffer[i++] = fat32buff[n + k];
199 199 }
200 200 }while((fat32buff[k]&0x40)!=0x40);
201 201 nameBuffer[i]='\0';
202 202 }
203 203 else
204 204 {
205 205 i=0;
206 206 for(n=0;n<8;n++)
207 207 {
208 208 nameBuffer[i] = 0x20 | fat32buff[n+DIR_Nameoff + k];
209 209 if(nameBuffer[i]!=0x20)i++;
210 210 }
211 211 if((((fat32buff[DIR_Nameoff + k +11])&ATTR_DIRECTORY)!=ATTR_DIRECTORY)&&(fat32buff[8+DIR_Nameoff + k]!=0x20))
212 212 nameBuffer[i++]='.';
213 213 for(n=8;n<11;n++)
214 214 {
215 215 nameBuffer[i] = 0x20 | fat32buff[n+DIR_Nameoff + k];
216 216 if(nameBuffer[i]!=0x20)i++;
217 217 }
218 218
219 219 nameBuffer[i]='\0';
220 220 }
221 221 return res;
222 222 }
223 223
224 224
225 225 uint32_t fat32getdirentlba(ucdirent* entry)
226 226 {
227 227 uint32_t clusterNum = (uint32_t)((uint16_t)entry->DIR_FstClustLO) + ((uint32_t)((uint16_t)entry->DIR_FstClusHI) <<16);
228 228 uint32_t lba=clusterlba(((FAT32fs*)entry->fs),clusterNum);
229 229 return lba;
230 230 }
231 231
232 232
233 233 int fat32nextsectorlba(FAT32fs* fs,uint32_t lastsector_lba,uint32_t *nextsector_lba)
234 234 {
235 235
236 if(((lastsector_lba + 1 - fs->cluster_begin_lba) & (fs->BPB_SecPerClus-1))!=0) /*Is it the last sector of the cluster?*/
236 if(((lastsector_lba + 1 - fs->cluster_begin_lba) & ((uint32_t)(fs->BPB_SecPerClus)-1))!=0) /*Is it the last sector of the cluster?*/
237 237 {
238 238 *nextsector_lba = lastsector_lba+1; /*if not just increment lba*/
239 239 return DIRENT_noErr;
240 }
240 }
241
241 242 uint32_t clusternum=fat32sectorlbatoclusternum(fs,lastsector_lba); /*Get cluster Number from sector number*/
242 243 uint32_t fatsec=fat32clusterinfatsect(fs,clusternum); /*Get FAT sector number*/
243 244 fat32sectorreadout(fs,fatsec);
244 245 int i= fat32clusterinfatoff(clusternum);
245 246 *nextsector_lba=fat32Ui8_2_Ui32(fat32buff,i);
246 if(*nextsector_lba==0xFFFFFFF7)return DIRENT_BadSect;
247 if(*nextsector_lba>=0xFFFFFFF6)return DIRENT_LastSect;
247 if(*nextsector_lba==0xFFFFFFF7)
248 {
249
250 printf("DIRENT_BadSect\n");
251 return DIRENT_BadSect;
252 }
253 if((uint32_t)(*nextsector_lba)>=(uint32_t)0xFFFFFFF6)
254 {
255 printf("DIRENT_LastSect\n");
256 return DIRENT_LastSect;
257 }
248 258 *nextsector_lba = clusterlba(fs,*nextsector_lba);
249 259 return DIRENT_noErr;
250 260 }
251 261
252 262
253 263 int fat32prevsectorlba(FAT32fs* fs,uint32_t lastsector_lba,uint32_t *nextsector_lba)
254 264 {
255 265 uint32_t i=0,fatsec=0;
256 266 if(((lastsector_lba - 1 - fs->cluster_begin_lba) & (fs->BPB_SecPerClus-1))!=0) /*Is it the first sector of the cluster?*/
257 267 {
258 268 *nextsector_lba = lastsector_lba-1; /*if not just decrement lba*/
259 269 return DIRENT_noErr;
260 270 }
261 271 uint32_t clusternum=fat32sectorlbatoclusternum(fs,lastsector_lba); /*Get cluster Number from sector number*/
262 272 fatsec=fat32clusterinfatsect(fs,clusternum); /*Get FAT sector number*/
263 273 do
264 274 {
265 275
266 276 fat32sectorreadout(fs,fatsec);
267 277 fatsec--;
268 278 for(i=511;i>3;i-=4)
269 279 {
270 280 *nextsector_lba = fat32Ui8_2_Ui32(fat32buff,i-3);
271 281 if (*nextsector_lba ==clusternum)
272 282 {
273 283 *nextsector_lba = clusterlba(fs,*nextsector_lba);
274 284 return DIRENT_noErr;
275 285 }
276 286
277 287 }
278 288
279 289 }while(fatsec>=fs->fat_begin_lba);
280 290
281 291 clusternum=fat32sectorlbatoclusternum(fs,lastsector_lba); /*Get cluster Number from sector number*/
282 292 fatsec=fat32clusterinfatsect(fs,clusternum); /*Get FAT sector number*/
283 293 do
284 294 {
285 295
286 296 fat32sectorreadout(fs,fatsec);
287 297 fatsec++;
288 298 for(i=0;i<512;i+=4)
289 299 {
290 300 *nextsector_lba = fat32Ui8_2_Ui32(fat32buff,i);
291 301 if (*nextsector_lba ==clusternum)
292 302 {
293 303 *nextsector_lba = clusterlba(fs,*nextsector_lba);
294 304 return DIRENT_noErr;
295 305 }
296 306
297 307 }
298 308
299 309 }while(fatsec<=(fs->fat_begin_lba+fs->BPB_FATSz32));
300 310 return DIRENT_ReadErr;
301 311 }
302 312
303 313
304 314
305 315
306 316
307 317
308 318
309 319
310 320
311 321
312 322
313 323
314 324
315 325
316 326
317 327
318 328
319 329
320 330
321 331
322 332
323 333
324 334
325 335
326 336
@@ -1,17 +1,20
1 1 TEMPLATE = lib
2 2 OBJECTS_DIR = obj
3 3 TARGET = fat32
4 4
5 5 SOURCES += \
6 6 fat32.c
7 7
8 8 INCLUDEPATH += ../../../includes \
9 9 ../../CPU/STM32F4xx_StdPeriph_Driver/inc \
10 10 ../../CPU/CMSIS/Include
11 11
12 12
13 13 UCMODEL=stm32f4
14 14
15 15 target.path = $$[QT_INSTALL_LIBS]/$$UCMODEL
16 16 INSTALLS += target
17 17
18 HEADERS += \
19 ../../../includes/fat32.h
20
@@ -1,511 +1,511
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2012, Alexis Jeandet
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------
19 19 -- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #include <sdcard.h>
23 23 #include <sdcard-spi.h>
24 24 #include <stdio.h>
25 25 #include <core.h>
26 26
27 27 int sdcardselect (blkdeviceptr _this);
28 28 void sdcarddeselect (blkdeviceptr _this);
29 29 int sdcardwait_ready (sdcardDev* sdcard);
30 30 int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token);
31 31 int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr);
32 32 char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg);
33 33
34 34 DSTATUS sdcarddisk_status (blkdeviceptr _this);
35 35 DSTATUS sdcarddisk_initialize (blkdeviceptr _this);
36 36 DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count);
37 37 DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count);
38 38 DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff);
39 39
40 40 static volatile
41 41 unsigned int Timer1, Timer2; /* 1kHz decrement timer stopped at zero (disk_timerproc()) */
42 42
43 43 void sdcarddeselect (blkdeviceptr _this)
44 44 {
45 45 char d;
46 46 _this->select(0);
47 47 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO hi-z for multiple slave SPI) */
48 48 }
49 49
50 50 int sdcardselect (blkdeviceptr _this) /* 1:OK, 0:Timeout */
51 51 {
52 52 char d;
53 53 _this->select(1);
54 54 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO enabled) */
55 55
56 56 if (sdcardwait_ready(((sdcardDev*)_this->phy))) return 1; /* OK */
57 57 _this->select(0);
58 58 return 0; /* Timeout */
59 59 }
60 60
61 61 int sdcardwait_ready (sdcardDev* sdcard) /* 1:OK, 0:Timeout */
62 62 {
63 63 char d=0;
64 64 unsigned int tmr;
65 65 volatile unsigned int i=0;
66 66
67 67 for (tmr = 500; tmr>0; tmr--) { /* Wait for ready in timeout of 500ms */
68 68 sdcard->rcvr_mmc(sdcard->phy,&d, 1);
69 69 if (d == 0xFF) break;
70 70 delay_100us(10);
71 71 }
72 72
73 73 return tmr != 0;
74 74 }
75 75
76 76 int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr)
77 77 {
78 78 char d[2];
79 79 unsigned int tmr;
80 80 volatile unsigned int i=0;
81 81 for (tmr = 100; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */
82 82 sdcard->rcvr_mmc(sdcard->phy,d, 1);
83 83 if (d[0] != 0xFF) break;
84 84 delay_100us(10);
85 85 }
86 86 if (d[0] != 0xFE) return 0; /* If not valid data token, return with error */
87 87
88 88 sdcard->rcvr_mmc(sdcard->phy,buff, btr); /* Receive the data block into buffer */
89 89 sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Discard CRC */
90 90
91 91 return 1; /* Return with success */
92 92 }
93 93
94 94
95 95 int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token)
96 96 {
97 97 char d[2];
98 98
99 99
100 100 if (!sdcardwait_ready(sdcard)) return 0;
101 101
102 102 d[0] = token;
103 103 sdcard->xmit_mmc(sdcard->phy,d, 1); /* Xmit a token */
104 104 if (token != 0xFD) { /* Is it data token? */
105 105 sdcard->xmit_mmc(sdcard->phy,buff, 512); /* Xmit the 512 byte data block to MMC */
106 106 sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Xmit dummy CRC (0xFF,0xFF) */
107 107 sdcard->rcvr_mmc(sdcard->phy,d, 1); /* Receive data response */
108 108 if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */
109 109 return 0;
110 110 }
111 111
112 112 return 1;
113 113 }
114 114
115 115
116 116 char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg)
117 117 {
118 118 char n, d, buf[6];
119 119
120 120
121 121 if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
122 122 cmd &= 0x7F;
123 123 n = sdcardsend_cmd(_this,CMD55, 0);
124 124 if (n > 1) return n;
125 125 }
126 126 /* Select the card and wait for ready */
127 127 sdcarddeselect(_this);
128 128 if (!sdcardselect(_this)) {printf("Can't select SDCARD\n");return 0xFF;}
129 129 /* Send a command packet */
130 130 buf[0] = 0x40 | cmd; /* Start + Command index */
131 131 buf[1] = (char)(arg >> 24); /* Argument[31..24] */
132 132 buf[2] = (char)(arg >> 16); /* Argument[23..16] */
133 133 buf[3] = (char)(arg >> 8); /* Argument[15..8] */
134 134 buf[4] = (char)arg; /* Argument[7..0] */
135 135 n = 0x01; /* Dummy CRC + Stop */
136 136 if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */
137 137 if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */
138 138 if (cmd == CMD55) n = 0x63;
139 139 buf[5] = n;
140 140 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,buf, 6);
141 141 /* Receive command response */
142 142 if (cmd == CMD12)
143 143 {
144 144 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);
145 145 } /* Skip a stuff byte when stop reading */
146 146 n = 100; /* Wait for a valid response in timeout of 100 attempts */
147 147 do
148 148 {
149 149 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);
150 150 delay_100us(10);
151 151 //libucprintf("resp=%d\n\r",d);
152 152 }while ((d & 0x80) && --n);
153 153
154 154 return d; /* Return with the response value */
155 155 }
156 156
157 157
158 158
159 159
160 160 DSTATUS sdcarddisk_status (blkdeviceptr _this)
161 161 {
162 162 DSTATUS s = ((sdcardDev*)_this->phy)->Stat;
163 163 char ocr[4];
164 164
165 165
166 166 if ((_this==0) || !_this->detect()) {
167 167 s = STA_NODISK | STA_NOINIT;
168 168 } else {
169 169 s &= ~STA_NODISK;
170 170 if (_this->writeprotected()) /* Check card write protection */
171 171 s |= STA_PROTECT;
172 172 else
173 173 s &= ~STA_PROTECT;
174 174 if (!(s & STA_NOINIT)) {
175 175 if (sdcardsend_cmd(_this,CMD58, 0)) /* Check if the card is kept initialized */
176 176 s |= STA_NOINIT;
177 177 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
178 178 _this->select(0);
179 179 }
180 180 }
181 181 ((sdcardDev*)_this->phy)->Stat = s;
182 182
183 183 return s;
184 184 }
185 185
186 186
187 187
188 188
189 189 DSTATUS sdcarddisk_initialize (blkdeviceptr _this)
190 190 {
191 191 char cmd, ty, ocr[4],ocr2[4];
192 192 int n;
193 193 const char dummy=0xff;
194 194 uint32_t speed = 1000000;
195 195
196 196 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,350000);
197 197 if (_this==0) return STA_NOINIT; /* Supports only drive 0 */
198 198 /* Is card existing in the soket? */
199 199 if (((sdcardDev*)_this->phy)->Stat & STA_NODISK)
200 200 {
201 201 printf("No SDCARD\n");
202 202 return ((sdcardDev*)_this->phy)->Stat;
203 203 }
204 204 printf("SDCARD Detected\n");
205 for (n = 10; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */
205 for (n = 100; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */
206 206
207 207 ty = 0;
208 208 if (sdcardsend_cmd(_this,CMD0, 0) == 1) { /* Put the card SPI/Idle state */
209 209 printf("SDCARD in Idle mode\n");
210 210 Timer1 = 1000; /* Initialization timeout = 1 sec */
211 211 if (sdcardsend_cmd(_this,CMD8, 0x1AA) == 1) { /* SDv2? */
212 212 printf("SDCARD V2\n");
213 213 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
214 214 //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get 32 bit return value of R7 resp */
215 215 if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* Is the card supports vcc of 2.7-3.6V? */
216 216 printf("The card supports vcc of 2.7-3.6V\n");
217 217 while (Timer1 && sdcardsend_cmd(_this,ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */
218 218 if (Timer1 && sdcardsend_cmd(_this,CMD58, 0) == 0) { /* Check CCS bit in the OCR */
219 219 //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
220 220 printf("Check CCS bit in the OCR:\n");
221 221 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
222 222 printf("OCR[0]=0x%X\n",ocr[0]);
223 223 ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Card id SDv2 */
224 224 }
225 225 }
226 226 } else { /* Not SDv2 card */
227 227 if (sdcardsend_cmd(_this,ACMD41, 0) <= 1) { /* SDv1 or MMC? */
228 228 ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */
229 229 } else {
230 230 ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */
231 231 }
232 232 while (Timer1 && sdcardsend_cmd(_this,cmd, 0)) ; /* Wait for end of initialization */
233 233 if (!Timer1 || sdcardsend_cmd(_this,CMD16, 512) != 0) /* Set block length: 512 */
234 234 ty = 0;
235 235 }
236 236 }
237 237 ((sdcardDev*)_this->phy)->CardType = ty; /* Card type */
238 238 printf("CardType=0x%X\n",ty);
239 239
240 240 if (ty) { /* OK */
241 241 ((sdcardDev*)_this->phy)->Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */
242 242 sdcardsend_cmd(_this,CMD58, 0);
243 243 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
244 244 do
245 245 {
246 246 speed+=1000000;
247 247 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed);
248 248 sdcardsend_cmd(_this,CMD58, 0);
249 249 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr2, 4);
250 250 for(n=0;n<4;n++)
251 251 {
252 252 if(ocr[n]!=ocr2[n])
253 253 {
254 254 n=0;
255 255 break;
256 256 }
257 257 }
258 258 if((speed>(50*1000*1000)) || (speed >= getCpuFreq()))
259 259 {
260 260 break;
261 261 }
262 262 }while(n);
263 263 if(!n)
264 264 {
265 265 speed-=1000000;
266 266 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed);
267 267 }
268 268 printf("SDCARD speed = %dMHz\n",speed/1000000);
269 269
270 270 } else { /* Failed */
271 271 //power_off();
272 272 ((sdcardDev*)_this->phy)->Stat = STA_NOINIT;
273 273 }
274 274 sdcarddeselect (_this);
275 275 return ((sdcardDev*)_this->phy)->Stat;
276 276 }
277 277
278 278
279 279
280 280
281 281
282 282
283 283
284 284
285 285
286 286
287 287 DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count)
288 288 {
289 289 DSTATUS s;
290 290
291 291
292 292 s = sdcarddisk_status(_this);
293 293 if (s & STA_NOINIT) return RES_NOTRDY;
294 294 if (!count) return RES_PARERR;
295 295 if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
296 296
297 297 if (count == 1) { /* Single block read */
298 298 if ((sdcardsend_cmd(_this,CMD17, sector) == 0) && sdcardrcvr_datablock((sdcardDev*)(_this->phy),buff, 512))
299 299 count = 0;
300 300 }
301 301 else { /* Multiple block read */
302 302 if (sdcardsend_cmd(_this,CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
303 303 do {
304 304 if (!sdcardrcvr_datablock (((sdcardDev*)_this->phy),buff, 512)) break;
305 305 buff += 512;
306 306 } while (--count);
307 307 sdcardsend_cmd(_this,CMD12, 0); /* STOP_TRANSMISSION */
308 308 }
309 309 }
310 310 sdcarddeselect (_this);
311 311
312 312 return count ? RES_ERROR : RES_OK;
313 313 }
314 314
315 315
316 316
317 317
318 318
319 319 DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count)
320 320 {
321 321 DSTATUS s;
322 322
323 323
324 324 s = sdcarddisk_status(_this);
325 325 if (s & STA_NOINIT) return RES_NOTRDY;
326 326 if (s & STA_PROTECT) return RES_WRPRT;
327 327 if (!count) return RES_PARERR;
328 328 if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
329 329
330 330 if (count == 1) { /* Single block write */
331 331 if ((sdcardsend_cmd(_this,CMD24, sector) == 0) /* WRITE_BLOCK */
332 332 && sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFE))
333 333 count = 0;
334 334 }
335 335 else { /* Multiple block write */
336 336 if (((sdcardDev*)_this->phy)->CardType & CT_SDC) sdcardsend_cmd(_this,ACMD23, count);
337 337 if (sdcardsend_cmd(_this,CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
338 338 do {
339 339 if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFC)) break;
340 340 buff += 512;
341 341 } while (--count);
342 342 if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),0, 0xFD)) /* STOP_TRAN token */
343 343 count = 1;
344 344 }
345 345 }
346 346 sdcarddeselect (_this);
347 347
348 348 return count ? RES_ERROR : RES_OK;
349 349 }
350 350
351 351
352 352 DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff)
353 353 {
354 354 DRESULT res;
355 355 const char dummy=0xff;
356 356 char n, csd[16], *ptr = buff;
357 357 uint16_t csize;
358 358 uint32_t *dp, st, ed;
359 359
360 360
361 361 if (_this==0) return RES_PARERR; /* Check parameter */
362 362 if (((sdcardDev*)_this->phy)->Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
363 363
364 364 res = RES_ERROR;
365 365
366 366 switch (ctrl) {
367 367 case CTRL_SYNC : /* Wait for end of internal write process of the drive */
368 368 if (sdcardselect (_this)) {
369 369 sdcarddeselect (_this);
370 370 res = RES_OK;
371 371 }
372 372 break;
373 373
374 374 case GET_SECTOR_COUNT : /* Get drive capacity in unit of sector (DWORD) */
375 375 if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) {
376 376 if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
377 377 csize = csd[9] + ((uint16_t)csd[8] << 8) + 1;
378 378 *(uint32_t*)buff = (uint32_t)csize << 10;
379 379 } else { /* SDC ver 1.XX or MMC ver 3 */
380 380 n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
381 381 csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 3) << 10) + 1;
382 382 *(uint32_t*)buff = (uint32_t)csize << (n - 9);
383 383 }
384 384 res = RES_OK;
385 385 }
386 386 break;
387 387
388 388 case GET_SECTOR_SIZE : /* Get sector size in unit of byte (WORD) */
389 389 *(uint16_t*)buff = 512;
390 390 res = RES_OK;
391 391 break;
392 392
393 393 case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
394 394 if (((sdcardDev*)_this->phy)->CardType & CT_SD2) { /* SDC ver 2.00 */
395 395 if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* Read SD status */
396 396 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
397 397 if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read partial block */
398 398 for (n = 64 - 16; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Purge trailing data */
399 399 *(uint32_t*)buff = 16UL << (csd[10] >> 4);
400 400 res = RES_OK;
401 401 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
402 402 }
403 403 }
404 404 } else { /* SDC ver 1.XX or MMC */
405 405 if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read CSD */
406 406 if (((sdcardDev*)_this->phy)->CardType & CT_SD1) { /* SDC ver 1.XX */
407 407 *(uint32_t*)buff = (((csd[10] & 63) << 1) + ((uint16_t)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
408 408 } else { /* MMC */
409 409 *(uint32_t*)buff = ((uint16_t)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
410 410 }
411 411 res = RES_OK;
412 412 }
413 413 }
414 414 break;
415 415
416 416 case CTRL_ERASE_SECTOR : /* Erase a block of sectors (used when _USE_ERASE == 1) */
417 417 if (!(((sdcardDev*)_this->phy)->CardType & CT_SDC)) break; /* Check if the card is SDC */
418 418 if (sdcarddisk_ioctl(_this, MMC_GET_CSD, csd)) break; /* Get CSD */
419 419 if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */
420 420 dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */
421 421 if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) {
422 422 st *= 512; ed *= 512;
423 423 }
424 424 if (sdcardsend_cmd(_this,CMD32, st) == 0 && sdcardsend_cmd(_this,CMD33, ed) == 0 && sdcardsend_cmd(_this,CMD38, 0) == 0 && sdcardwait_ready(((sdcardDev*)_this->phy))) /* Erase sector block */
425 425 res = RES_OK; /* FatFs does not check result of this command */
426 426 break;
427 427
428 428 /* Following command are not used by FatFs module */
429 429
430 430 case MMC_GET_TYPE : /* Get MMC/SDC type (BYTE) */
431 431 *ptr = ((sdcardDev*)_this->phy)->CardType;
432 432 res = RES_OK;
433 433 break;
434 434
435 435 case MMC_GET_CSD : /* Read CSD (16 bytes) */
436 436 if (sdcardsend_cmd(_this,CMD9, 0) == 0 /* READ_CSD */
437 437 && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16))
438 438 res = RES_OK;
439 439 break;
440 440
441 441 case MMC_GET_CID : /* Read CID (16 bytes) */
442 442 if (sdcardsend_cmd(_this,CMD10, 0) == 0 /* READ_CID */
443 443 && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16))
444 444 res = RES_OK;
445 445 break;
446 446
447 447 case MMC_GET_OCR : /* Read OCR (4 bytes) */
448 448 if (sdcardsend_cmd(_this,CMD58, 0) == 0) { /* READ_OCR */
449 449 //for (n = 4; n; n--) *ptr++ =
450 450 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ptr, 4);
451 451 res = RES_OK;
452 452 }
453 453 break;
454 454
455 455 case MMC_GET_SDSTAT : /* Read SD status (64 bytes) */
456 456 if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* SD_STATUS */
457 457 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
458 458 if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 64))
459 459 res = RES_OK;
460 460 }
461 461 break;
462 462
463 463 default:
464 464 res = RES_PARERR;
465 465 }
466 466
467 467 sdcarddeselect (_this);
468 468
469 469 return res;
470 470 }
471 471
472 472
473 473
474 474 void sdcardspimake(sdcardDev* sdcard,UHANDLE phy,void (*rcvr_mmc) (UHANDLE,char *,uint32_t),void (*xmit_mmc) (UHANDLE,const char *,uint32_t ),void (*setspeed) (UHANDLE phy,uint32_t speed),uint32_t (*getspeed) (UHANDLE phy))
475 475 {
476 476 sdcard->phy = phy;
477 477 sdcard->rcvr_mmc = rcvr_mmc;
478 478 sdcard->xmit_mmc = xmit_mmc;
479 479 sdcard->setspeed = setspeed;
480 480 sdcard->getspeed = getspeed;
481 481 }
482 482
483 483 void sdcardspimakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected)
484 484 {
485 485 dev->phy=sdcard;
486 486 dev->select=select;
487 487 dev->power = power;
488 488 dev->detect = detect;
489 489 dev->writeprotected = writeprotected;
490 490 dev->write = sdcarddisk_write;
491 491 dev->read = sdcarddisk_read;
492 492 dev->ioctl = sdcarddisk_ioctl;
493 493 dev->initialize = sdcarddisk_initialize;
494 494 dev->status = sdcarddisk_status;
495 495 }
496 496
497 497
498 498
499 499
500 500
501 501
502 502
503 503
504 504
505 505
506 506
507 507
508 508
509 509
510 510
511 511
@@ -1,186 +1,187
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2013, Alexis Jeandet
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------
19 19 -- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #include <widget.h>
23 23 #include <terminal.h>
24 24 #include <stdint.h>
25 25 #include <stdio.h>
26 26 #include <gpio.h>
27 27 #include <core.h>
28 28
29 29
30 30 #define CHARXPOS(terminal,charwidth) (((terminal)->column * charwidth) + (terminal)->Xpos)
31 31 #define CHARYPOS(terminal,charheight) ((terminal)->line * charheight + (terminal)->Ypos+charheight)
32 32
33 33 streamdevice_ops TERMINAL_OPS=
34 34 {
35 35 .write = &terminal_write,
36 36 .read = &terminal_read,
37 37 .setpos= &terminal_setpos,
38 38 .close = NULL
39 39 };
40 40
41 41 int terminal_init(terminal_t* terminal,LCD_t* LCD,sFONT* font,streamdevice* strdev)
42 42 {
43 43 if((LCD!=NULL) && (font!=NULL) && (strdev!=NULL) && (terminal!=NULL))
44 44 {
45 45 terminal->LCD=LCD;
46 46 terminal->font=font;
47 47 terminal->line = 0;
48 48 terminal->column = 0;
49 49 terminal->horizontalSpace=0;
50 50 terminal->verticalSpace=0;
51 51 int charw=terminal->font->Width + terminal->horizontalSpace;
52 52 int charh=terminal->font->Height + terminal->verticalSpace;
53 53 terminal->textColor=0xFFFF;
54 54 terminal->backgroundColor=0x0000;
55 55 terminal_setgeometry(terminal,5,5,terminal->LCD->width-(10),terminal->LCD->height-(10));
56 56
57 57 strdev->_stream = (UHANDLE)terminal;
58 58 strdev->ops = &TERMINAL_OPS;
59 59 strdev->streamPt = 0;
60 60 terminal_clear(terminal);
61 61 terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
62 62 return 1;
63 63 }
64 64 return 0;
65 65 }
66 66
67 67 void terminal_setgeometry(terminal_t* terminal,uint16_t Xpos,uint16_t Ypos,uint16_t width,uint16_t height)
68 68 {
69 69 int charw=terminal->font->Width + terminal->horizontalSpace;
70 70 int charh=terminal->font->Height + terminal->verticalSpace;
71 71 if(terminal->LCD->height<(Ypos+height))
72 72 return;
73 73 if(terminal->LCD->width<(Xpos+width))
74 74 return;
75 75 terminal->Xpos = Xpos;
76 76 terminal->Ypos = Ypos;
77 77 terminal->height = height;
78 78 terminal->width = width;
79 79 terminal->lineCount = terminal->height/charh;
80 80 terminal->columnCount = (terminal->width/charw)-1;
81 81 terminal_clear(terminal);
82 82 }
83 83
84 84
85 85 void terminal_clear(terminal_t* terminal)
86 86 {
87 87 terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,terminal->Ypos,terminal->width,terminal->height,terminal->backgroundColor,0,terminal->backgroundColor);
88 88 }
89 89
90 90 void terminal_setbackgroundColor(terminal_t* terminal, uint32_t backgrooundColor)
91 91 {
92 92 terminal->backgroundColor = backgrooundColor;
93 93 }
94 94
95 95 void terminal_settextColor(terminal_t* terminal, uint32_t textColor)
96 96 {
97 97 terminal->textColor = textColor;
98 98 }
99 99
100 100
101 101 void terminal_movecursor(terminal_t* terminal,int n)
102 102 {
103 103 int charw=terminal->font->Width + terminal->horizontalSpace;
104 104 int charh=terminal->font->Height + terminal->verticalSpace;
105 105 terminal->column += n;
106 106 if(terminal->column>terminal->columnCount)
107 107 {
108 108 terminal->line += (terminal->column)/terminal->columnCount;
109 109 terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal,charh)-charh,charw*(terminal->columnCount-terminal->column),charh,terminal->backgroundColor,0,terminal->backgroundColor);
110 110 terminal->line = terminal->line % terminal->lineCount;
111 111 terminal->column = terminal->column % terminal->columnCount;
112 112 }
113 113 terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal,charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
114 114 }
115 115
116 116 void terminal_clearCurentLine(terminal_t* terminal)
117 117 {
118 118 int charw=terminal->font->Width + terminal->horizontalSpace;
119 119 int charh=terminal->font->Height + terminal->verticalSpace;
120 120 terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,CHARYPOS(terminal, charh)-charh,terminal->width,charh,terminal->backgroundColor,0,terminal->backgroundColor);
121 121 }
122 122
123 123 int terminal_writenc(terminal_t* terminal,char* data, int n)
124 124 {
125 125 int charw=terminal->font->Width + terminal->horizontalSpace;
126 126 int charh=terminal->font->Height + terminal->verticalSpace;
127 127 int l=0;
128 128 char buffer[2]=" ";
129 129 while(l<n)
130 130 {
131 131
132 132 buffer[0]=data[l];
133 133 if(buffer[0]=='\n')
134 134 {
135 135 terminal->line= (terminal->line+1) % terminal->lineCount;
136 136 terminal->column = 0;
137 terminal_clearCurentLine(terminal);
137 138 }else {
138 139 if(buffer[0]=='\t')
139 140 {
140 141 for(int i=0;i<1;i++)terminal_movecursor(terminal,1);
141 142 }else
142 143 if(terminal->column==0)terminal_clearCurentLine(terminal);
143 144 if(buffer[0]!='\r'){
144 145 terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
145 146 terminal->LCD->paintText(terminal->LCD,buffer,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh),terminal->font,terminal->textColor);
146 147 terminal_movecursor(terminal,1);
147 148 }
148 149 }
149 150 l++;
150 151 }
151 152 return n;
152 153 }
153 154
154 155 int terminal_write(streamdevice* device,void* data,int size, int n)
155 156 {
156 157 return terminal_writenc((terminal_t*)device->_stream,(char*) data,size*n);
157 158 }
158 159
159 160 int terminal_read(streamdevice* device,void* data,int size, int n)
160 161 {
161 162 return n*size;
162 163 }
163 164
164 165 int terminal_setpos(streamdevice* device,int pos)
165 166 {
166 167 return 1;
167 168 }
168 169
169 170 int terminal_close( streamdevice* device)
170 171 {
171 172 return 1;
172 173 }
173 174
174 175
175 176
176 177
177 178
178 179
179 180
180 181
181 182
182 183
183 184
184 185
185 186
186 187
@@ -1,82 +1,97
1 1 /*------------------------------------------------------------------------------
2 2 #-- This file is a part of the libuc, microcontroler library
3 3 #-- Copyright (C) 2011, Alexis Jeandet
4 4 #--
5 5 #-- This program is free software; you can redistribute it and/or modify
6 6 #-- it under the terms of the GNU General Public License as published by
7 7 #-- the Free Software Foundation; either version 3 of the License, or
8 8 #-- (at your option) any later version.
9 9 #--
10 10 #-- This program is distributed in the hope that it will be useful,
11 11 #-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 #-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 #-- GNU General Public License for more details.
14 14 #--
15 15 #-- You should have received a copy of the GNU General Public License
16 16 #-- along with this program; if not, write to the Free Software
17 17 #-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 #-------------------------------------------------------------------------------
19 19 #-- Author : Alexis Jeandet
20 20 #-- Mail : alexis.jeandet@gmail.com
21 21 #-------------------------------------------------------------------------------*/
22 22 #ifndef VS10XX_H
23 23 #define VS10XX_H
24 24 #include <uhandle.h>
25 25 #include <spi.h>
26 26
27 27 #define VSMODE 0
28 28 #define VSSTATUS 1
29 29 #define VSBASS 2
30 30 #define VSCLOCKF 3
31 31 #define VSDECODE_TIME 4
32 32 #define VSAUDATA 5
33 33 #define VSWRAM 6
34 34 #define VSWRAMADDR 7
35 35 #define VSHDAT0 8
36 36 #define VSHDAT1 9
37 37 #define VSAIADDR 0xA
38 38 #define VSVOL 0xB
39 39 #define VSAICTRL0 0xC
40 40 #define VSAICTRL1 0xD
41 41 #define VSAICTRL2 0xE
42 42 #define VSAICTRL3 0xF
43 43
44 typedef enum VS10XXVER_en
45 {
46 UNKNOWN,
47 VS1001,
48 VS1011,
49 VS1002,
50 VS1003,
51 VS1053,
52 VS1033,
53 VS1103
54 }VS10XXVER_en;
55
44 56 typedef struct vs10XXDev
45 57 {
46 58 spi_t SPIdev;
47 59 void (*setxCS)(char);
48 60 void (*setxRST)(char);
49 61 void (*setxDCS)(char);
50 62 int (*getDREQ)();
51 int a;
63 VS10XXVER_en VERSION;
52 64 }vs10XXDev;
53 65
54 66
55 67 void vs10XXopen(vs10XXDev* codec,spi_t dev, void (*setxCS)(char),void (*setxRST)(char),void (*setxDCS)(char),int (*getDREQ)());
56 68 void vs10XXsoftreset(vs10XXDev* dev);
57 69 int vs10XXcmdread(vs10XXDev* dev,char address);
58 70 void vs10XXcmdwrite(vs10XXDev* dev,char address,int value);
59
60 extern void vs10XXclearXCS();
61 extern void vs10XXsetXCS();
62 extern int vs10XXDREQ();
71 void vs10XXstream32bytes(vs10XXDev* dev,char* buffer);
72 void vs10XXsetCansel(vs10XXDev* dev);
73 int vs10XXcanselAccepted(vs10XXDev* dev);
74 void vs10XXsoftReset(vs10XXDev* dev);
75 //extern void vs10XXclearXCS();
76 //extern void vs10XXsetXCS();
77 //extern int vs10XXDREQ();
63 78
64 79 #endif //VS10XX_H
65 80
66 81
67 82
68 83
69 84
70 85
71 86
72 87
73 88
74 89
75 90
76 91
77 92
78 93
79 94
80 95
81 96
82 97
@@ -1,176 +1,176
1 1 /*------------------------------------------------------------------------------
2 2 -- This file is a part of the libuc, microcontroler library
3 3 -- Copyright (C) 2012, Alexis Jeandet
4 4 --
5 5 -- This program is free software; you can redistribute it and/or modify
6 6 -- it under the terms of the GNU General Public License as published by
7 7 -- the Free Software Foundation; either version 3 of the License, or
8 8 -- (at your option) any later version.
9 9 --
10 10 -- This program is distributed in the hope that it will be useful,
11 11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 -- GNU General Public License for more details.
14 14 --
15 15 -- You should have received a copy of the GNU General Public License
16 16 -- along with this program; if not, write to the Free Software
17 17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 18 -------------------------------------------------------------------------------
19 19 -- Author : Alexis Jeandet
20 20 -- Mail : alexis.jeandet@gmail.com
21 21 -------------------------------------------------------------------------------*/
22 22 #ifndef FAT32_H
23 23 #define FAT32_H
24 24 #include <mbr.h>
25 25 #include <libucfs.h>
26 26 #include <ucdirent.h>
27 27 #include <stdint.h>
28 28 #include <stdint-gcc.h>
29 29 /*BPB (Boot Sector) Offsets */
30 30
31 31 #define BS_jmpBootoff 0
32 32 #define BS_OEMNameoff 3
33 33 #define BPB_BytsPerSecoff 11
34 34 #define BPB_SecPerClusoff 13
35 35 #define BPB_RsvdSecCntoff 14
36 36 #define BPB_NumFATsoff 16
37 37 #define BPB_RootEntCntoff 17
38 38 #define BPB_TotSec16off 19
39 39 #define BPB_Mediaoff 21
40 40 #define BPB_FATSz16off 22
41 41 #define BPB_SecPerTrkoff 24
42 42 #define BPB_NumHeadsoff 26
43 43 #define BPB_HiddSecoff 28
44 44 #define BPB_TotSec32off 32
45 45 /*FAT16*/
46 46 #define BS_DrvNumFAT16off 36
47 47 #define BS_Reserved1FAT16off 37
48 48 #define BS_BootSigFAT16off 38
49 49 #define BS_VolIDFAT16off 39
50 50 #define BS_VolLabFAT16off 43
51 51 #define BS_FilSysTypeFAT16off 54
52 52 /*FAT32*/
53 53 #define BPB_FATSz32off 36
54 54 #define BPB_ExtFlagsoff 40
55 55 #define BPB_FSVeroff 42
56 56 #define BPB_RootClusoff 44
57 57 #define BPB_FSInfooff 48
58 58 #define BPB_BkBootSecoff 50
59 59 #define BPB_Reservedoff 52
60 60 #define BS_DrvNumFAT32off 64
61 61 #define BS_Reserved1FAT32off 65
62 62 #define BS_BootSigFAT32off 66
63 63 #define BS_VolIDFAT32off 67
64 64 #define BS_VolLabFAT32off 71
65 65 #define BS_FilSysTypeFAT32off 82
66 66
67 67
68 68 #define DIR_Nameoff 0
69 69 #define DIR_Attroff 0xb
70 70 #define DIR_FstClusHIoff 0x14
71 71 #define DIR_FstClusLOoff 0x1A
72 72 #define DIR_FileSizeoff 0x1c
73 73 #define DIR_CrtTimeTenthoff 13
74 74 #define DIR_CrtTimeoff 14
75 75 #define DIR_CrtDateoff 16
76 76 #define DIR_LstAccDateoff 18
77 77 #define DIR_WrtTimeoff 22
78 78 #define DIR_WrtDateoff 24
79 79
80 80
81 81
82 82 #define FATBadpart 1
83 83 #define FATBabArg 2
84 84 #define FATReadErr 3
85 85 #define FATnoErr 0
86 86
87 87 typedef struct FAT32fs
88 88 {
89 89 uint16_t BPB_BytsPerSec;
90 90 uint16_t BPB_RsvdSecCnt;
91 91 uint32_t BPB_FATSz32;
92 92 uint32_t BPB_RootClus;
93 93 uint32_t fat_begin_lba;
94 94 uint32_t cluster_begin_lba;
95 95 uint8_t BPB_SecPerClus;
96 96 uint8_t BPB_NumFATs;
97 97 dikpartition* part;
98 98 }FAT32fs;
99 99
100 100
101 101 #define castUI64(val) ((uint64_t)(val))
102 102 #define castUI32(val) ((uint32_t)(val))
103 103 #define castUI16(val) ((uint16_t)(val))
104 104 #define castUI8(val) ((uint8_t)(val))
105 105
106 106 #define castI64(val) ((int64_t)(val))
107 107 #define castI32(val) ((int32_t)(val))
108 108 #define castI16(val) ((int16_t)(val))
109 109 #define castI8(val) ((int8_t)(val))
110 110
111 111 #define fat32Ui8_2_Ui32(table,lsBindex) ((uint32_t)((0xFF&(table[lsBindex]))+(0xFF00&(((uint32_t)table[lsBindex+1])<<8))+(0xFF0000&(((uint32_t)table[lsBindex+2])<<16))+(0xFF000000&(((uint32_t)table[lsBindex+3])<<24))))
112 112 #define fat32Ui8_2_Ui16(table,lsBindex) ((uint16_t)((0xFF&(table[lsBindex]))+(0xFF00&(((uint16_t)table[lsBindex+1])<<8))))
113 113
114 114 /* Get the first sector lba of given cluster number*/
115 115 #define clusterlba(fs,cluster_number) ((uint32_t)fs->cluster_begin_lba + (((uint32_t)cluster_number) - (2)) * (uint32_t)fs->BPB_SecPerClus)
116 116
117 117 //#define fat32clusterinfatsect(fs,cluster_number) ((((uint32_t)(cluster_number))>>6) + fs->fat_begin_lba)
118 118 #define fat32clusterinfatsect(fs,cluster_number) ((((uint32_t)(cluster_number))>>7) + fs->fat_begin_lba)
119 #define fat32clusterinfatoff(cluster_number) (((cluster_number)&0x3F)*4)
119 #define fat32clusterinfatoff(cluster_number) (((cluster_number)&0x7F)*4)
120 120
121 121 /*Get cluster index in FAT Table from sector address*/
122 122 #define fat32masksectorlba(sectorlba,fs) (sectorlba & (castUI32(-1)^castUI32(fs->BPB_SecPerClus-1)))
123 123 #define fat32sectorlbatoclusternum(fs,sectorlba) (((fat32masksectorlba(castUI32(sectorlba),fs) -(castUI32(fs->cluster_begin_lba)))/castUI32(fs->BPB_SecPerClus))+castUI32(2))
124 124
125 125 #define fat32extract16b(table, LSBOffset) (uint16_t)(( (uint16_t) ((uint8_t)table[(LSBOffset)]) ) + (uint16_t)( ((uint8_t)table[(LSBOffset)+1]) << 8 ))
126 126 #define fat32extract32b(table, LSBOffset) (uint32_t)(( (uint32_t) ((uint8_t)table[(LSBOffset)]) ) + (uint32_t)( ((uint8_t)table[(LSBOffset)+1]) << 8 )+ (uint32_t)( ((uint8_t)table[(LSBOffset)+2]) << 16 )+ (uint32_t)( ((uint8_t)table[(LSBOffset)+3]) << 24 ))
127 127
128 128 #ifdef FAT32_PRIVATE
129 129 #define fat32sectorreadout(fs,fatsec) \
130 130 if((lastSecAddrs!=(fatsec)) || (lastFATFS!=(fs))) /*Check if sector already buffered*/ \
131 131 {\
132 132 if((fs)->part->phy->read((fs)->part->phy,fat32buff,(fatsec),1)!=RES_OK) \
133 133 {\
134 134 return DIRENT_ReadErr;\
135 135 }\
136 136 }\
137 137 lastSecAddrs=(fatsec);\
138 138 lastFATFS=(fs);
139 139 #endif
140 140
141 141 extern int fat32open(FAT32fs* fs,dikpartition* part);
142 142 extern int fat32mkdirent(FAT32fs* fs,ucdirent* dirent);
143 143 extern int fat32getVolName(FAT32fs* fs,char* Name);
144 144 extern int fat32getrootfirstent(ucdirent* entry);
145 145 extern int fat32nextdirent(ucdirent* entry);
146 146 extern int fat32nextsectorlba(FAT32fs* fs,uint32_t lastsector_lba,uint32_t* nextsector_lba);
147 147 extern int fat32prevsectorlba(FAT32fs* fs,uint32_t lastsector_lba,uint32_t *nextsector_lba);
148 148 extern uint32_t fat32getdirentlba(ucdirent* entry);
149 149 extern int fat32getdirentname(ucdirent* entry,char* nameBuffer);
150 150
151 151 #endif
152 152
153 153
154 154
155 155
156 156
157 157
158 158
159 159
160 160
161 161
162 162
163 163
164 164
165 165
166 166
167 167
168 168
169 169
170 170
171 171
172 172
173 173
174 174
175 175
176 176
General Comments 0
You need to be logged in to leave comments. Login now