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