##// END OF EJS Templates
Fixed some issues on SCARD driver!
jeandet -
r61:efd3992e476b dev_alexis
parent child
Show More
@@ -1,423 +1,423
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>
33 #include <sdcard-spi.h>
34 #include <sdcard.h>
34 #include <sdcard.h>
35 #include <mbr.h>
35 #include <mbr.h>
36 #include <VS10XX.h>
36 #include <VS10XX.h>
37
37
38 uint32_t OSC0 =8000000;
38 uint32_t OSC0 =8000000;
39 uint32_t INTOSC =16000000;
39 uint32_t INTOSC =16000000;
40 uint32_t RTCOSC =32768;
40 uint32_t RTCOSC =32768;
41 uint32_t currentCpuFreq=0;
41 uint32_t currentCpuFreq=0;
42 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
42 extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__];
43
43
44 LCD_IF_t lcdIF0={
44 LCD_IF_t lcdIF0={
45 .init = &bsp_FSMC_init,
45 .init = &bsp_FSMC_init,
46 .writereg = &bsp_lcd0_write_reg,
46 .writereg = &bsp_lcd0_write_reg,
47 .readreg = &bsp_lcd0_read_reg,
47 .readreg = &bsp_lcd0_read_reg,
48 .writeGRAM = &bsp_lcd0_writeGRAM,
48 .writeGRAM = &bsp_lcd0_writeGRAM,
49 .readGRAM = &bsp_lcd0_readGRAM
49 .readGRAM = &bsp_lcd0_readGRAM
50 };
50 };
51
51
52 LCD_t lcd0={
52 LCD_t lcd0={
53 .interface = &lcdIF0,
53 .interface = &lcdIF0,
54 .init = &ili9328init,
54 .init = &ili9328init,
55 .paint = &ili9328paint,
55 .paint = &ili9328paint,
56 .paintText = &ili9328paintText,
56 .paintText = &ili9328paintText,
57 .paintFilRect = &ili9328paintFilRect,
57 .paintFilRect = &ili9328paintFilRect,
58 .refreshenable = &ili9328refreshenable,
58 .refreshenable = &ili9328refreshenable,
59 .width= 240,
59 .width= 240,
60 .height = 320
60 .height = 320
61 };
61 };
62
62
63 terminal_t terminal0;
63 terminal_t terminal0;
64
64
65 volatile int8_t* lcd0_CMD=(volatile int8_t*) 0x60000000;
65 volatile int8_t* lcd0_CMD=(volatile int8_t*) 0x60000000;
66 volatile int8_t* lcd0_DATA=(volatile int8_t*)(0x61FFFFF0);
66 volatile int8_t* lcd0_DATA=(volatile int8_t*)(0x61FFFFF0);
67
67
68 float VREF0 =(float)3.3;
68 float VREF0 =(float)3.3;
69 volatile vs10XXDev audioCodec0;
69 volatile vs10XXDev audioCodec0;
70
70
71 int bsp_init()
71 int bsp_init()
72 {
72 {
73 int i=0;
73 int i=0;
74 for(i=0;i<__MAX_OPENED_FILES__;i++)
74 for(i=0;i<__MAX_OPENED_FILES__;i++)
75 {
75 {
76 __opnfiles__[i] = NULL;
76 __opnfiles__[i] = NULL;
77 }
77 }
78 bsp_GPIO_init();
78 bsp_GPIO_init();
79 bsp_uart_init();
79 bsp_uart_init();
80 bsp_iic_init();
80 bsp_iic_init();
81 bsp_FSMC_init();
81 bsp_FSMC_init();
82 bsp_GTerm_init();
82 bsp_GTerm_init();
83 bsp_spi_init();
83 bsp_spi_init();
84 bsp_SD_init();
84 bsp_SD_init();
85 bsp_Audio_init();
85 bsp_Audio_init();
86 printf("\r=====================\n\r");
86 printf("\r=====================\n\r");
87 printf( "=====================\n\r");
87 printf( "=====================\n\r");
88 printf(BSP);
88 printf(BSP);
89 printf(" initialised\n\r");
89 printf(" initialised\n\r");
90 printf( "=====================\n\r");
90 printf( "=====================\n\r");
91 return 1;
91 return 1;
92 }
92 }
93
93
94 void bsp_GPIO_init()
94 void bsp_GPIO_init()
95 {
95 {
96 gpio_t GPIO_Out_init_List[]={LED1,LED2,LCD_RESET,LCD_BACKL,VS1053xCS,VS1053xDCS,VS1053xRESET,SDCARD2CS};
96 gpio_t GPIO_Out_init_List[]={LED1,LED2,LCD_RESET,LCD_BACKL,VS1053xCS,VS1053xDCS,VS1053xRESET,SDCARD2CS};
97 for(int i=0;i<8;i++)
97 for(int i=0;i<8;i++)
98 {
98 {
99 gpio_t GPIO_init = gpioopen(GPIO_Out_init_List[i]);
99 gpio_t GPIO_init = gpioopen(GPIO_Out_init_List[i]);
100 GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype;
100 GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype;
101 gpiosetconfig(&GPIO_init);
101 gpiosetconfig(&GPIO_init);
102 }
102 }
103 gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD};
103 gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD};
104 for(int i=0;i<2;i++)
104 for(int i=0;i<2;i++)
105 {
105 {
106 gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]);
106 gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]);
107 GPIO_init |= gpiohighspeed | gpioindir;
107 GPIO_init |= gpiohighspeed | gpioindir;
108 gpiosetconfig(&GPIO_init);
108 gpiosetconfig(&GPIO_init);
109 }
109 }
110 gpioclr(VS1053xRESET);
110 gpioclr(VS1053xRESET);
111 gpioset(VS1053xCS);
111 gpioset(VS1053xCS);
112 gpioset(VS1053xDCS);
112 gpioset(VS1053xDCS);
113 gpioset(SDCARD2CS);
113 gpioset(SDCARD2CS);
114 gpioclr(LCD_RESET);
114 gpioclr(LCD_RESET);
115 gpioclr(LCD_BACKL);
115 gpioclr(LCD_BACKL);
116 }
116 }
117
117
118 void bsp_uart_init()
118 void bsp_uart_init()
119 {
119 {
120
120
121 }
121 }
122
122
123 /*
123 /*
124 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
124 D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7
125 D5 PE8 D6 PE9 D7 PE10
125 D5 PE8 D6 PE9 D7 PE10
126 A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S
126 A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S
127 FSMC_NOE PD4 RD
127 FSMC_NOE PD4 RD
128 */
128 */
129
129
130 int bsp_FSMC_init()
130 int bsp_FSMC_init()
131 {
131 {
132 #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
132 #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)
133 #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
134
134
135 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
135 gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\
136 ,PD4,PD5,PD7,PE4};
136 ,PD4,PD5,PD7,PE4};
137 for(int i=0;i<12;i++)
137 for(int i=0;i<12;i++)
138 {
138 {
139 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
139 gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]);
140 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
140 LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
141 gpiosetconfig(&LCD_DBx);
141 gpiosetconfig(&LCD_DBx);
142 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
142 GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC);
143 }
143 }
144
144
145 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
145 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
146 FSMC_NORSRAMTimingInitTypeDef p;
146 FSMC_NORSRAMTimingInitTypeDef p;
147
147
148 /* Enable FSMC clock */
148 /* Enable FSMC clock */
149 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
149 RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
150
150
151 /*-- FSMC Configuration ------------------------------------------------------*/
151 /*-- FSMC Configuration ------------------------------------------------------*/
152 /*----------------------- SRAM Bank 3 ----------------------------------------*/
152 /*----------------------- SRAM Bank 3 ----------------------------------------*/
153 /* FSMC_Bank1_NORSRAM4 configuration */
153 /* FSMC_Bank1_NORSRAM4 configuration */
154 //p.FSMC_AddressSetupTime = 5;
154 //p.FSMC_AddressSetupTime = 5;
155 //p.FSMC_AddressSetupTime = 1;
155 //p.FSMC_AddressSetupTime = 1;
156 p.FSMC_AddressSetupTime = 3;
156 p.FSMC_AddressSetupTime = 3;
157 p.FSMC_AddressHoldTime = 3;
157 p.FSMC_AddressHoldTime = 3;
158 //p.FSMC_DataSetupTime = 9;
158 //p.FSMC_DataSetupTime = 9;
159 //ili9328 -> data setup time > 10ns
159 //ili9328 -> data setup time > 10ns
160 p.FSMC_DataSetupTime = 1;
160 p.FSMC_DataSetupTime = 1;
161 if(getCpuFreq()>100*1000*1000)
161 if(getCpuFreq()>100*1000*1000)
162 p.FSMC_DataSetupTime = 2;// 11;
162 p.FSMC_DataSetupTime = 2;// 11;
163 p.FSMC_BusTurnAroundDuration = 0;
163 p.FSMC_BusTurnAroundDuration = 0;
164 p.FSMC_CLKDivision = 0;
164 p.FSMC_CLKDivision = 0;
165 p.FSMC_DataLatency = 0;
165 p.FSMC_DataLatency = 0;
166 //ili9328 -> data hold time > 15ns
166 //ili9328 -> data hold time > 15ns
167 if(getCpuFreq()>66*1000*1000)
167 if(getCpuFreq()>66*1000*1000)
168 p.FSMC_DataLatency = 0;
168 p.FSMC_DataLatency = 0;
169 p.FSMC_AccessMode = FSMC_AccessMode_A;
169 p.FSMC_AccessMode = FSMC_AccessMode_A;
170
170
171 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
171 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
172 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
172 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
173 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
173 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
174 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
174 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
175 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
175 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
176 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
176 FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
177 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
177 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
178 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
178 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
179 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
179 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
180 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
180 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
181 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
181 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
182 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
182 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
183 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
183 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
184 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
184 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
185 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
185 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
186
186
187 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
187 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
188
188
189 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
189 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
190 gpioset(LCD_RESET);
190 gpioset(LCD_RESET);
191 gpioclr(LCD_RESET);
191 gpioclr(LCD_RESET);
192 delay_100us(500);
192 delay_100us(500);
193 gpioset(LCD_RESET);
193 gpioset(LCD_RESET);
194 delay_100us(500);
194 delay_100us(500);
195 lcd0.init(&lcd0);
195 lcd0.init(&lcd0);
196 gpioset(LCD_BACKL);
196 gpioset(LCD_BACKL);
197 return 1;
197 return 1;
198 }
198 }
199
199
200 void bsp_spi_init()
200 void bsp_spi_init()
201 {
201 {
202 gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO};
202 // gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO};
203 for(int i=0;i<3;i++)
203 // for(int i=0;i<3;i++)
204 {
204 // {
205 gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]);
205 // gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]);
206 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
206 // SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
207 gpiosetconfig(&SPI_DBx);
207 // gpiosetconfig(&SPI_DBx);
208 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1);
208 // GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1);
209 }
209 // }
210 spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1);
210 // spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1);
211
211
212 gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO};
212 gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO};
213 for(int i=0;i<3;i++)
213 for(int i=0;i<3;i++)
214 {
214 {
215 gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]);
215 gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]);
216 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
216 SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype;
217 gpiosetconfig(&SPI_DBx);
217 gpiosetconfig(&SPI_DBx);
218 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3);
218 GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3);
219 }
219 }
220 spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1);
220 spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1);
221
221
222 }
222 }
223
223
224
224
225 void bsp_iic_init()
225 void bsp_iic_init()
226 {
226 {
227 // i2copenandconfig(i2c2,0,10000,PF0,PF1);
227 // i2copenandconfig(i2c2,0,10000,PF0,PF1);
228 }
228 }
229
229
230
230
231 void bsp_Audio_init()
231 void bsp_Audio_init()
232 {
232 {
233 vs10XXopen(&audioCodec0,VS1053SPI,vs1052setXCS,vs1052setRST,vs1052setXDCS,vs10XXDREQ);
233 vs10XXopen(&audioCodec0,VS1053SPI,vs1052setXCS,vs1052setRST,vs1052setXDCS,vs10XXDREQ);
234 }
234 }
235
235
236 void bsp_SD_init()
236 void bsp_SD_init()
237 {
237 {
238 sdcardDev sdcard2;
238 // sdcardDev sdcard2;
239 blkdevice sdcard2blkdev;
239 // blkdevice sdcard2blkdev;
240 dikpartition sdcard2Part1;
240 // dikpartition sdcard2Part1;
241 sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed);
241 // sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed);
242 sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected);
242 // sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected);
243 sdcard2blkdev.initialize(&sdcard2blkdev);
243 // sdcard2blkdev.initialize(&sdcard2blkdev);
244 mbropen(&sdcard2blkdev,&sdcard2Part1,1);
244 // mbropen(&sdcard2blkdev,&sdcard2Part1,1);
245 }
245 }
246
246
247 void vs1052setXCS(char val)
247 void vs1052setXCS(char val)
248 {
248 {
249 gpiosetval(VS1053xCS,(int)val);
249 gpiosetval(VS1053xCS,(int)val);
250 }
250 }
251
251
252 void vs1052setXDCS(char val)
252 void vs1052setXDCS(char val)
253 {
253 {
254 gpiosetval(VS1053xDCS,(int)val);
254 gpiosetval(VS1053xDCS,(int)val);
255 }
255 }
256
256
257 void vs1052setRST(char val)
257 void vs1052setRST(char val)
258 {
258 {
259 if(val)
259 if(val)
260 gpioset(VS1053xRESET);
260 gpioset(VS1053xRESET);
261 else
261 else
262 gpioclr(VS1053xRESET);
262 gpioclr(VS1053xRESET);
263 }
263 }
264
264
265 int vs10XXDREQ()
265 int vs10XXDREQ()
266 {
266 {
267 return gpiogetval(VS1053DREQ);
267 return gpiogetval(VS1053DREQ);
268 }
268 }
269
269
270
270
271 void bsppowersdcard(char onoff) //always ON
271 void bsppowersdcard(char onoff) //always ON
272 {
272 {
273
273
274 }
274 }
275
275
276 char bspsdcardpresent()
276 char bspsdcardpresent()
277 {
277 {
278 return gpiogetval(SDCARD2CD);
278 return gpiogetval(SDCARD2CD);
279 }
279 }
280
280
281 char bspsdcardwriteprotected()
281 char bspsdcardwriteprotected()
282 {
282 {
283 return 0;
283 return 0;
284 }
284 }
285
285
286 void bspsdcardselect(char YESNO)
286 void bspsdcardselect(char YESNO)
287 {
287 {
288 if(YESNO)
288 if(YESNO)
289 gpioclr(SDCARD2CS);
289 gpioclr(SDCARD2CS);
290 else
290 else
291 gpioset(SDCARD2CS);
291 gpioset(SDCARD2CS);
292 }
292 }
293
293
294
294
295 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
295 void bsp_lcd0_write_reg(uint32_t reg,uint32_t data)
296 {
296 {
297 uint8_t* pt8 = (uint8_t*)(void*)&reg;
297 uint8_t* pt8 = (uint8_t*)(void*)&reg;
298 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
298 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
299 *lcd0_CMD=pt8[3];
299 *lcd0_CMD=pt8[3];
300 *lcd0_CMD=pt8[2];
300 *lcd0_CMD=pt8[2];
301 pt8 = (uint8_t*)(void*)&data;
301 pt8 = (uint8_t*)(void*)&data;
302 *lcd0_DATA=pt8[3];
302 *lcd0_DATA=pt8[3];
303 *lcd0_DATA=pt8[2];
303 *lcd0_DATA=pt8[2];
304 #else
304 #else
305 *lcd0_CMD=pt8[1];
305 *lcd0_CMD=pt8[1];
306 *lcd0_CMD=pt8[0];
306 *lcd0_CMD=pt8[0];
307 pt8 = (uint8_t*)(void*)&data;
307 pt8 = (uint8_t*)(void*)&data;
308 *lcd0_DATA=pt8[1];
308 *lcd0_DATA=pt8[1];
309 *lcd0_DATA=pt8[0];
309 *lcd0_DATA=pt8[0];
310 #endif
310 #endif
311
311
312 }
312 }
313
313
314 uint32_t bsp_lcd0_read_reg(uint32_t reg)
314 uint32_t bsp_lcd0_read_reg(uint32_t reg)
315 {
315 {
316 uint8_t* pt8 = (uint8_t*)(void*)&reg;
316 uint8_t* pt8 = (uint8_t*)(void*)&reg;
317 uint32_t DATA=0;
317 uint32_t DATA=0;
318 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
318 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
319 *lcd0_CMD=pt8[3];
319 *lcd0_CMD=pt8[3];
320 *lcd0_CMD=pt8[2];
320 *lcd0_CMD=pt8[2];
321 pt8 = (uint8_t*)(void*)&DATA;
321 pt8 = (uint8_t*)(void*)&DATA;
322 pt8[3]=*lcd0_DATA;
322 pt8[3]=*lcd0_DATA;
323 pt8[2]=*lcd0_DATA;
323 pt8[2]=*lcd0_DATA;
324 #else
324 #else
325
325
326 *lcd0_CMD=pt8[1];
326 *lcd0_CMD=pt8[1];
327 *lcd0_CMD=pt8[0];
327 *lcd0_CMD=pt8[0];
328 pt8 = (uint8_t*)(void*)&DATA;
328 pt8 = (uint8_t*)(void*)&DATA;
329 pt8[1]=*lcd0_DATA;
329 pt8[1]=*lcd0_DATA;
330 pt8[0]=*lcd0_DATA;
330 pt8[0]=*lcd0_DATA;
331 #endif
331 #endif
332
332
333 return DATA;
333 return DATA;
334 }
334 }
335
335
336 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
336 void bsp_lcd0_writeGRAM(void* buffer,uint32_t count)
337 {
337 {
338 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
338 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
339 uint8_t* pt8 = (uint8_t*)(void*)&reg;
339 uint8_t* pt8 = (uint8_t*)(void*)&reg;
340 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
340 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
341 *lcd0_CMD=pt8[3];
341 *lcd0_CMD=pt8[3];
342 *lcd0_CMD=pt8[2];
342 *lcd0_CMD=pt8[2];
343 pt8 = (uint8_t*)(void*)buffer;
343 pt8 = (uint8_t*)(void*)buffer;
344 for(int i=0;i<(int)count;i++)
344 for(int i=0;i<(int)count;i++)
345 {
345 {
346 *lcd0_DATA=pt8[(2*i) +1];
346 *lcd0_DATA=pt8[(2*i) +1];
347 *lcd0_DATA=pt8[2*i];
347 *lcd0_DATA=pt8[2*i];
348 }
348 }
349 #else
349 #else
350
350
351 *lcd0_CMD=pt8[1];
351 *lcd0_CMD=pt8[1];
352 *lcd0_CMD=pt8[0];
352 *lcd0_CMD=pt8[0];
353 pt8 = (uint8_t*)(void*)buffer;
353 pt8 = (uint8_t*)(void*)buffer;
354 for(int i=0;i<(int)count;i++)
354 for(int i=0;i<(int)count;i++)
355 {
355 {
356
356
357 *lcd0_DATA=pt8[(2*i) +1];
357 *lcd0_DATA=pt8[(2*i) +1];
358 *lcd0_DATA=pt8[2*i];
358 *lcd0_DATA=pt8[2*i];
359 }
359 }
360 #endif
360 #endif
361 }
361 }
362
362
363 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
363 void bsp_lcd0_readGRAM(void* buffer,uint32_t count)
364 {
364 {
365 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
365 uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM;
366 uint8_t* pt8 = (uint8_t*)(void*)&reg;
366 uint8_t* pt8 = (uint8_t*)(void*)&reg;
367 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
367 #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
368 *lcd0_CMD=pt8[3];
368 *lcd0_CMD=pt8[3];
369 *lcd0_CMD=pt8[2];
369 *lcd0_CMD=pt8[2];
370 pt8 = (uint8_t*)(void*)buffer;
370 pt8 = (uint8_t*)(void*)buffer;
371 pt8[0] = *lcd0_DATA;
371 pt8[0] = *lcd0_DATA;
372 pt8[0] = *lcd0_DATA;
372 pt8[0] = *lcd0_DATA;
373 for(int i=0;i<(int)count;i++)
373 for(int i=0;i<(int)count;i++)
374 {
374 {
375 pt8[(2*i) +1]=*lcd0_DATA;
375 pt8[(2*i) +1]=*lcd0_DATA;
376 pt8[2*i]=*lcd0_DATA;
376 pt8[2*i]=*lcd0_DATA;
377 }
377 }
378 #else
378 #else
379
379
380 *lcd0_CMD=pt8[1];
380 *lcd0_CMD=pt8[1];
381 *lcd0_CMD=pt8[0];
381 *lcd0_CMD=pt8[0];
382 pt8 = (uint8_t*)(void*)buffer;
382 pt8 = (uint8_t*)(void*)buffer;
383 pt8[0] = *lcd0_DATA;
383 pt8[0] = *lcd0_DATA;
384 pt8[0] = *lcd0_DATA;
384 pt8[0] = *lcd0_DATA;
385 for(int i=0;i<(int)count;i++)
385 for(int i=0;i<(int)count;i++)
386 {
386 {
387 pt8[(2*i) +1]=*lcd0_DATA;
387 pt8[(2*i) +1]=*lcd0_DATA;
388 pt8[2*i]=*lcd0_DATA;
388 pt8[2*i]=*lcd0_DATA;
389 }
389 }
390 #endif
390 #endif
391 }
391 }
392
392
393 void bsp_GTerm_init()
393 void bsp_GTerm_init()
394 {
394 {
395 if(__opnfiles__[1]==NULL)
395 if(__opnfiles__[1]==NULL)
396 {
396 {
397 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
397 //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t));
398 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
398 streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice));
399 terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1);
399 terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1);
400 __opnfiles__[1] = fd1;
400 __opnfiles__[1] = fd1;
401 }
401 }
402 else
402 else
403 {
403 {
404
404
405 }
405 }
406 }
406 }
407
407
408
408
409
409
410
410
411
411
412
412
413
413
414
414
415
415
416
416
417
417
418
418
419
419
420
420
421
421
422
422
423
423
@@ -1,54 +1,108
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>
11 #include <sdcard.h>
12 #include <sdcard-spi.h>
13 #include <fat32.h>
14 #include <ucdirent.h>
15 #include <string.h>
16
10
17
11 extern streamdevice* __opnfiles__[];
18 extern streamdevice* __opnfiles__[];
12
19
13 void randBoxesDemo()
20 void randBoxesDemo()
14 {
21 {
15 int16_t x,y,w,h,t,r;
22 int16_t x,y,w,h,t,r;
16 x=rand()%240;
23 x=rand()%240;
17 y=rand()%320;
24 y=rand()%320;
18 w=rand()%(240-x);
25 w=rand()%(240-x);
19 if(x>y)
26 if(x>y)
20 r=(rand()%(y))%(320-y);
27 r=(rand()%(y))%(320-y);
21 else
28 else
22 r=(rand()%(x))%(240-x);
29 r=(rand()%(x))%(240-x);
23 h=rand()%(320-y);
30 h=rand()%(320-y);
24 t=rand()%(10);
31 t=rand()%(10);
25 ili9328paintFilRect(&lcd0,x,y,w,h,rand(),t,rand());
32 ili9328paintFilRect(&lcd0,x,y,w,h,rand(),t,rand());
26 //ili9328paintFilCirc(&lcd0,x,y,r,rand(),t,rand());
33 //ili9328paintFilCirc(&lcd0,x,y,r,rand(),t,rand());
27 //delay_100us(10);
34 //delay_100us(10);
28 //for(volatile int i=0;i<(1024*2);i++);
35 //for(volatile int i=0;i<(1024*2);i++);
29 }
36 }
30
37
31 int main()
38 int main()
32 {
39 {
40 while(!bspsdcardpresent());
41 delay_100us(5000);
42 printf("About to initialize SDCARD");
43 sdcardDev sdcard2;
44 blkdevice sdcard2blkdev;
45 dikpartition sdcard2Part1;
46 FAT32fs partoche;
47 sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed);
48 sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected);
49 sdcard2blkdev.initialize(&sdcard2blkdev);
50 mbropen(&sdcard2blkdev,&sdcard2Part1,1);
51 if(FATnoErr==fat32open(&partoche,&sdcard2Part1))
52 {
53 printf("FAT32 partition opened\n");
54 char test[]=" \n";
55 ucdirent root,folder;
56 fat32mkdirent(&partoche,&root);
57 fat32getrootfirstent(&root);
58 fat32getVolName(&partoche,test);
59 printf("Vol Name=%s\n",test);
60 fat32getdirentname(&root,test);
61 printf("%s\n",test);
62 while (DIRENT_EndOfDir!=fat32nextdirent(&root))
63 {
64 if(test[0]=='m')
65 {
66 printf("enter music folder\n");
67 fat32enterdirent(&root,&folder);
68 while (DIRENT_EndOfDir!=fat32nextdirent(&folder))
69 {
70 printf("%s\n",test);
71 }
72 }
73 fat32getdirentname(&root,test);
74 printf("%s\n",test);
75 }
76 if(test[0]=='m')
77 {
78 printf("enter music folder\n");
79 fat32enterdirent(&root,&folder);
80 while (DIRENT_EndOfDir!=fat32nextdirent(&folder))
81 {
82 printf("%s\n",test);
83 }
84 }
85 }
33 while(1)
86 while(1)
34 {
87 {
35 // printf("Audio Codec reg0=0x%x\n",vs10XXcmdread(&audioCodec0,0));
88 // printf("Audio Codec reg0=0x%x\n",vs10XXcmdread(&audioCodec0,0));
36 // printf("hello ");
89 // printf("hello ");
37 // bsp_lcd0_write_reg(1,0x1234);
90 // bsp_lcd0_write_reg(1,0x1234);
38 // delay_100us(10000);
91 // delay_100us(10000);
39 // randBoxesDemo();
92 // randBoxesDemo();
93
40 gpioset(LED1);
94 gpioset(LED1);
41 delay_100us(10000);
95 delay_100us(10000);
42 gpioclr(LED1);
96 gpioclr(LED1);
43 delay_100us(10000);
97 delay_100us(10000);
44
98
45 }
99 }
46 printf("hello world\n\r");
100 printf("hello world\n\r");
47 return 0;
101 return 0;
48 }
102 }
49
103
50
104
51
105
52
106
53
107
54
108
@@ -1,82 +1,83
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 "mbr.h"
22 #include "mbr.h"
23 #include <stdio.h>
23 #include <stdio.h>
24
24
25
25
26 int mbropen(blkdevice* phy,dikpartition* part,char partNum)
26 int mbropen(blkdevice* phy,dikpartition* part,char partNum)
27 {
27 {
28 printf("enter MBR open function\n");
28 printf("enter MBR open function\n");
29 char mbr[512];
29 char mbr[512];
30 if((part==0) || (phy ==0) || (partNum>4) || (partNum==0) ) return MBRBabArg;
30 if((part==0) || (phy ==0) || (partNum>4) || (partNum==0) ) return MBRBabArg;
31 part->phy = phy;
31 part->phy = phy;
32 if(phy->read(phy,mbr,0,1)!=RES_OK)
32 if(phy->read(phy,mbr,0,1)!=RES_OK)
33 {
33 {
34 printf("can't read MBR!\n");
34 printf("can't read MBR!\n");
35 printf("%x %x %x %x %x %x %x %x\n",mbr[0],mbr[1],mbr[2],mbr[3],mbr[4],mbr[5],mbr[6],mbr[7]);
35 return MBRReadErr;
36 return MBRReadErr;
36 }
37 }
37 if(((mbr[510]) == (char)0x55) && ((mbr[511]) == (char)0xAA))
38 if(((mbr[510]) == (char)0x55) && ((mbr[511]) == (char)0xAA))
38 {
39 {
39 part->TypeCode = mbr[(512-82+TypeCodeoffset)+partNum*16];
40 part->TypeCode = mbr[(512-82+TypeCodeoffset)+partNum*16];
40 part->LBABegin = (unsigned int)mbr[(512-82+LBABeginoffset)+partNum*16] + ((unsigned int)mbr[(512-82+LBABeginoffset+1)+partNum*16]<<8)+ ((unsigned int)mbr[(512-82+LBABeginoffset+2)+partNum*16]<<16)+ ((unsigned int)mbr[(512-82+LBABeginoffset+3)+partNum*16]<<24);
41 part->LBABegin = (unsigned int)mbr[(512-82+LBABeginoffset)+partNum*16] + ((unsigned int)mbr[(512-82+LBABeginoffset+1)+partNum*16]<<8)+ ((unsigned int)mbr[(512-82+LBABeginoffset+2)+partNum*16]<<16)+ ((unsigned int)mbr[(512-82+LBABeginoffset+3)+partNum*16]<<24);
41 part->NumOfSec = (unsigned int)mbr[(512-82+NumOfSecoffset)+partNum*16] + ((unsigned int)mbr[(512-82+NumOfSecoffset+1)+partNum*16]<<8)+ ((unsigned int)mbr[(512-82+NumOfSecoffset+2)+partNum*16]<<16)+ ((unsigned int)mbr[(512-82+NumOfSecoffset+3)+partNum*16]<<24);
42 part->NumOfSec = (unsigned int)mbr[(512-82+NumOfSecoffset)+partNum*16] + ((unsigned int)mbr[(512-82+NumOfSecoffset+1)+partNum*16]<<8)+ ((unsigned int)mbr[(512-82+NumOfSecoffset+2)+partNum*16]<<16)+ ((unsigned int)mbr[(512-82+NumOfSecoffset+3)+partNum*16]<<24);
42 printf("MBR Ok\n");
43 printf("MBR Ok\n");
43 return MBRnoErr;
44 return MBRnoErr;
44 }
45 }
45 printf("Bad MBR MBR[510]=0x%.2X MBR[511]=0x%.2X!\n",0xff&mbr[510],0xff&mbr[511]);
46 printf("Bad MBR MBR[510]=0x%.2X MBR[511]=0x%.2X!\n",0xff&mbr[510],0xff&mbr[511]);
46 return MBRBadMbr;
47 return MBRBadMbr;
47
48
48 }
49 }
49
50
50
51
51
52
52
53
53
54
54
55
55
56
56
57
57
58
58
59
59
60
60
61
61
62
62
63
63
64
64
65
65
66
66
67
67
68
68
69
69
70
70
71
71
72
72
73
73
74
74
75
75
76
76
77
77
78
78
79
79
80
80
81
81
82
82
83
@@ -1,502 +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 = 1000; 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 = 50000; 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 for(i=0;i<64;i++);
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) {((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);} /* Skip a stuff byte when stop reading */
142 if (cmd == CMD12)
143 n = 10; /* Wait for a valid response in timeout of 10 attempts */
143 {
144 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);
145 } /* Skip a stuff byte when stop reading */
146 n = 100; /* Wait for a valid response in timeout of 100 attempts */
144 do
147 do
145 {
148 {
146 ((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);
147 //libucprintf("resp=%d\n\r",d);
151 //libucprintf("resp=%d\n\r",d);
148 }
152 }while ((d & 0x80) && --n);
149 while ((d & 0x80) && --n);
150
153
151 return d; /* Return with the response value */
154 return d; /* Return with the response value */
152 }
155 }
153
156
154
157
155
158
156
159
157 DSTATUS sdcarddisk_status (blkdeviceptr _this)
160 DSTATUS sdcarddisk_status (blkdeviceptr _this)
158 {
161 {
159 DSTATUS s = ((sdcardDev*)_this->phy)->Stat;
162 DSTATUS s = ((sdcardDev*)_this->phy)->Stat;
160 char ocr[4];
163 char ocr[4];
161
164
162
165
163 if ((_this==0) || !_this->detect()) {
166 if ((_this==0) || !_this->detect()) {
164 s = STA_NODISK | STA_NOINIT;
167 s = STA_NODISK | STA_NOINIT;
165 } else {
168 } else {
166 s &= ~STA_NODISK;
169 s &= ~STA_NODISK;
167 if (_this->writeprotected()) /* Check card write protection */
170 if (_this->writeprotected()) /* Check card write protection */
168 s |= STA_PROTECT;
171 s |= STA_PROTECT;
169 else
172 else
170 s &= ~STA_PROTECT;
173 s &= ~STA_PROTECT;
171 if (!(s & STA_NOINIT)) {
174 if (!(s & STA_NOINIT)) {
172 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 */
173 s |= STA_NOINIT;
176 s |= STA_NOINIT;
174 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
177 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
175 _this->select(0);
178 _this->select(0);
176 }
179 }
177 }
180 }
178 ((sdcardDev*)_this->phy)->Stat = s;
181 ((sdcardDev*)_this->phy)->Stat = s;
179
182
180 return s;
183 return s;
181 }
184 }
182
185
183
186
184
187
185
188
186 DSTATUS sdcarddisk_initialize (blkdeviceptr _this)
189 DSTATUS sdcarddisk_initialize (blkdeviceptr _this)
187 {
190 {
188 char cmd, ty, ocr[4],ocr2[4];
191 char cmd, ty, ocr[4],ocr2[4];
189 int n;
192 int n;
190 const char dummy=0xff;
193 const char dummy=0xff;
191 uint32_t speed = 1000000;
194 uint32_t speed = 1000000;
192
195
193 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,350000);
196 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,350000);
194 if (_this==0) return STA_NOINIT; /* Supports only drive 0 */
197 if (_this==0) return STA_NOINIT; /* Supports only drive 0 */
195 /* Is card existing in the soket? */
198 /* Is card existing in the soket? */
196 if (((sdcardDev*)_this->phy)->Stat & STA_NODISK)
199 if (((sdcardDev*)_this->phy)->Stat & STA_NODISK)
197 {
200 {
198 printf("No SDCARD\n");
201 printf("No SDCARD\n");
199 return ((sdcardDev*)_this->phy)->Stat;
202 return ((sdcardDev*)_this->phy)->Stat;
200 }
203 }
201 printf("SDCARD Detected\n");
204 printf("SDCARD Detected\n");
202 for (n = 10; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */
205 for (n = 10; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */
203
206
204 ty = 0;
207 ty = 0;
205 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 */
206 printf("SDCARD in Idle mode\n");
209 printf("SDCARD in Idle mode\n");
207 Timer1 = 1000; /* Initialization timeout = 1 sec */
210 Timer1 = 1000; /* Initialization timeout = 1 sec */
208 if (sdcardsend_cmd(_this,CMD8, 0x1AA) == 1) { /* SDv2? */
211 if (sdcardsend_cmd(_this,CMD8, 0x1AA) == 1) { /* SDv2? */
212 printf("SDCARD V2\n");
209 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
213 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
210 //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 */
211 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");
212 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) */
213 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 */
214 //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");
215 ((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]);
216 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 */
217 }
224 }
218 }
225 }
219 } else { /* Not SDv2 card */
226 } else { /* Not SDv2 card */
220 if (sdcardsend_cmd(_this,ACMD41, 0) <= 1) { /* SDv1 or MMC? */
227 if (sdcardsend_cmd(_this,ACMD41, 0) <= 1) { /* SDv1 or MMC? */
221 ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */
228 ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */
222 } else {
229 } else {
223 ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */
230 ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */
224 }
231 }
225 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 */
226 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 */
227 ty = 0;
234 ty = 0;
228 }
235 }
229 }
236 }
230 ((sdcardDev*)_this->phy)->CardType = ty; /* Card type */
237 ((sdcardDev*)_this->phy)->CardType = ty; /* Card type */
238 printf("CardType=0x%X\n",ty);
231
239
232 if (ty) { /* OK */
240 if (ty) { /* OK */
233 ((sdcardDev*)_this->phy)->Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */
241 ((sdcardDev*)_this->phy)->Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */
234 sdcardsend_cmd(_this,CMD58, 0);
242 sdcardsend_cmd(_this,CMD58, 0);
235 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
243 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4);
236 do
244 do
237 {
245 {
238 speed+=1000000;
246 speed+=1000000;
239 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed);
247 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed);
240 sdcardsend_cmd(_this,CMD58, 0);
248 sdcardsend_cmd(_this,CMD58, 0);
241 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr2, 4);
249 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr2, 4);
242 for(n=0;n<4;n++)
250 for(n=0;n<4;n++)
243 {
251 {
244 if(ocr[n]!=ocr2[n])
252 if(ocr[n]!=ocr2[n])
245 {
253 {
246 n=0;
254 n=0;
247 break;
255 break;
248 }
256 }
249 }
257 }
250 if(speed>(50*1000*1000))
258 if((speed>(50*1000*1000)) || (speed >= getCpuFreq()))
251 {
259 {
252 break;
260 break;
253 }
261 }
254 }while(n);
262 }while(n);
255 if(!n)
263 if(!n)
256 {
264 {
257 speed-=1000000;
265 speed-=1000000;
258 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed);
266 ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed);
259 }
267 }
268 printf("SDCARD speed = %dMHz\n",speed/1000000);
260
269
261 } else { /* Failed */
270 } else { /* Failed */
262 //power_off();
271 //power_off();
263 ((sdcardDev*)_this->phy)->Stat = STA_NOINIT;
272 ((sdcardDev*)_this->phy)->Stat = STA_NOINIT;
264 }
273 }
265 sdcarddeselect (_this);
274 sdcarddeselect (_this);
266 return ((sdcardDev*)_this->phy)->Stat;
275 return ((sdcardDev*)_this->phy)->Stat;
267 }
276 }
268
277
269
278
270
279
271
280
272
281
273
282
274
283
275
284
276
285
277
286
278 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)
279 {
288 {
280 DSTATUS s;
289 DSTATUS s;
281
290
282
291
283 s = sdcarddisk_status(_this);
292 s = sdcarddisk_status(_this);
284 if (s & STA_NOINIT) return RES_NOTRDY;
293 if (s & STA_NOINIT) return RES_NOTRDY;
285 if (!count) return RES_PARERR;
294 if (!count) return RES_PARERR;
286 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 */
287
296
288 if (count == 1) { /* Single block read */
297 if (count == 1) { /* Single block read */
289 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))
290 count = 0;
299 count = 0;
291 }
300 }
292 else { /* Multiple block read */
301 else { /* Multiple block read */
293 if (sdcardsend_cmd(_this,CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
302 if (sdcardsend_cmd(_this,CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
294 do {
303 do {
295 if (!sdcardrcvr_datablock (((sdcardDev*)_this->phy),buff, 512)) break;
304 if (!sdcardrcvr_datablock (((sdcardDev*)_this->phy),buff, 512)) break;
296 buff += 512;
305 buff += 512;
297 } while (--count);
306 } while (--count);
298 sdcardsend_cmd(_this,CMD12, 0); /* STOP_TRANSMISSION */
307 sdcardsend_cmd(_this,CMD12, 0); /* STOP_TRANSMISSION */
299 }
308 }
300 }
309 }
301 sdcarddeselect (_this);
310 sdcarddeselect (_this);
302
311
303 return count ? RES_ERROR : RES_OK;
312 return count ? RES_ERROR : RES_OK;
304 }
313 }
305
314
306
315
307
316
308
317
309
318
310 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)
311 {
320 {
312 DSTATUS s;
321 DSTATUS s;
313
322
314
323
315 s = sdcarddisk_status(_this);
324 s = sdcarddisk_status(_this);
316 if (s & STA_NOINIT) return RES_NOTRDY;
325 if (s & STA_NOINIT) return RES_NOTRDY;
317 if (s & STA_PROTECT) return RES_WRPRT;
326 if (s & STA_PROTECT) return RES_WRPRT;
318 if (!count) return RES_PARERR;
327 if (!count) return RES_PARERR;
319 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 */
320
329
321 if (count == 1) { /* Single block write */
330 if (count == 1) { /* Single block write */
322 if ((sdcardsend_cmd(_this,CMD24, sector) == 0) /* WRITE_BLOCK */
331 if ((sdcardsend_cmd(_this,CMD24, sector) == 0) /* WRITE_BLOCK */
323 && sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFE))
332 && sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFE))
324 count = 0;
333 count = 0;
325 }
334 }
326 else { /* Multiple block write */
335 else { /* Multiple block write */
327 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);
328 if (sdcardsend_cmd(_this,CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
337 if (sdcardsend_cmd(_this,CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
329 do {
338 do {
330 if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFC)) break;
339 if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFC)) break;
331 buff += 512;
340 buff += 512;
332 } while (--count);
341 } while (--count);
333 if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),0, 0xFD)) /* STOP_TRAN token */
342 if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),0, 0xFD)) /* STOP_TRAN token */
334 count = 1;
343 count = 1;
335 }
344 }
336 }
345 }
337 sdcarddeselect (_this);
346 sdcarddeselect (_this);
338
347
339 return count ? RES_ERROR : RES_OK;
348 return count ? RES_ERROR : RES_OK;
340 }
349 }
341
350
342
351
343 DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff)
352 DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff)
344 {
353 {
345 DRESULT res;
354 DRESULT res;
346 const char dummy=0xff;
355 const char dummy=0xff;
347 char n, csd[16], *ptr = buff;
356 char n, csd[16], *ptr = buff;
348 uint16_t csize;
357 uint16_t csize;
349 uint32_t *dp, st, ed;
358 uint32_t *dp, st, ed;
350
359
351
360
352 if (_this==0) return RES_PARERR; /* Check parameter */
361 if (_this==0) return RES_PARERR; /* Check parameter */
353 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 */
354
363
355 res = RES_ERROR;
364 res = RES_ERROR;
356
365
357 switch (ctrl) {
366 switch (ctrl) {
358 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 */
359 if (sdcardselect (_this)) {
368 if (sdcardselect (_this)) {
360 sdcarddeselect (_this);
369 sdcarddeselect (_this);
361 res = RES_OK;
370 res = RES_OK;
362 }
371 }
363 break;
372 break;
364
373
365 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) */
366 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)) {
367 if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
376 if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
368 csize = csd[9] + ((uint16_t)csd[8] << 8) + 1;
377 csize = csd[9] + ((uint16_t)csd[8] << 8) + 1;
369 *(uint32_t*)buff = (uint32_t)csize << 10;
378 *(uint32_t*)buff = (uint32_t)csize << 10;
370 } else { /* SDC ver 1.XX or MMC ver 3 */
379 } else { /* SDC ver 1.XX or MMC ver 3 */
371 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;
372 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;
373 *(uint32_t*)buff = (uint32_t)csize << (n - 9);
382 *(uint32_t*)buff = (uint32_t)csize << (n - 9);
374 }
383 }
375 res = RES_OK;
384 res = RES_OK;
376 }
385 }
377 break;
386 break;
378
387
379 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) */
380 *(uint16_t*)buff = 512;
389 *(uint16_t*)buff = 512;
381 res = RES_OK;
390 res = RES_OK;
382 break;
391 break;
383
392
384 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) */
385 if (((sdcardDev*)_this->phy)->CardType & CT_SD2) { /* SDC ver 2.00 */
394 if (((sdcardDev*)_this->phy)->CardType & CT_SD2) { /* SDC ver 2.00 */
386 if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* Read SD status */
395 if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* Read SD status */
387 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
396 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
388 if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read partial block */
397 if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read partial block */
389 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 */
390 *(uint32_t*)buff = 16UL << (csd[10] >> 4);
399 *(uint32_t*)buff = 16UL << (csd[10] >> 4);
391 res = RES_OK;
400 res = RES_OK;
392 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
401 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
393 }
402 }
394 }
403 }
395 } else { /* SDC ver 1.XX or MMC */
404 } else { /* SDC ver 1.XX or MMC */
396 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 */
397 if (((sdcardDev*)_this->phy)->CardType & CT_SD1) { /* SDC ver 1.XX */
406 if (((sdcardDev*)_this->phy)->CardType & CT_SD1) { /* SDC ver 1.XX */
398 *(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);
399 } else { /* MMC */
408 } else { /* MMC */
400 *(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);
401 }
410 }
402 res = RES_OK;
411 res = RES_OK;
403 }
412 }
404 }
413 }
405 break;
414 break;
406
415
407 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) */
408 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 */
409 if (sdcarddisk_ioctl(_this, MMC_GET_CSD, csd)) break; /* Get CSD */
418 if (sdcarddisk_ioctl(_this, MMC_GET_CSD, csd)) break; /* Get CSD */
410 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 */
411 dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */
420 dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */
412 if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) {
421 if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) {
413 st *= 512; ed *= 512;
422 st *= 512; ed *= 512;
414 }
423 }
415 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 */
416 res = RES_OK; /* FatFs does not check result of this command */
425 res = RES_OK; /* FatFs does not check result of this command */
417 break;
426 break;
418
427
419 /* Following command are not used by FatFs module */
428 /* Following command are not used by FatFs module */
420
429
421 case MMC_GET_TYPE : /* Get MMC/SDC type (BYTE) */
430 case MMC_GET_TYPE : /* Get MMC/SDC type (BYTE) */
422 *ptr = ((sdcardDev*)_this->phy)->CardType;
431 *ptr = ((sdcardDev*)_this->phy)->CardType;
423 res = RES_OK;
432 res = RES_OK;
424 break;
433 break;
425
434
426 case MMC_GET_CSD : /* Read CSD (16 bytes) */
435 case MMC_GET_CSD : /* Read CSD (16 bytes) */
427 if (sdcardsend_cmd(_this,CMD9, 0) == 0 /* READ_CSD */
436 if (sdcardsend_cmd(_this,CMD9, 0) == 0 /* READ_CSD */
428 && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16))
437 && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16))
429 res = RES_OK;
438 res = RES_OK;
430 break;
439 break;
431
440
432 case MMC_GET_CID : /* Read CID (16 bytes) */
441 case MMC_GET_CID : /* Read CID (16 bytes) */
433 if (sdcardsend_cmd(_this,CMD10, 0) == 0 /* READ_CID */
442 if (sdcardsend_cmd(_this,CMD10, 0) == 0 /* READ_CID */
434 && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16))
443 && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16))
435 res = RES_OK;
444 res = RES_OK;
436 break;
445 break;
437
446
438 case MMC_GET_OCR : /* Read OCR (4 bytes) */
447 case MMC_GET_OCR : /* Read OCR (4 bytes) */
439 if (sdcardsend_cmd(_this,CMD58, 0) == 0) { /* READ_OCR */
448 if (sdcardsend_cmd(_this,CMD58, 0) == 0) { /* READ_OCR */
440 //for (n = 4; n; n--) *ptr++ =
449 //for (n = 4; n; n--) *ptr++ =
441 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ptr, 4);
450 ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ptr, 4);
442 res = RES_OK;
451 res = RES_OK;
443 }
452 }
444 break;
453 break;
445
454
446 case MMC_GET_SDSTAT : /* Read SD status (64 bytes) */
455 case MMC_GET_SDSTAT : /* Read SD status (64 bytes) */
447 if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* SD_STATUS */
456 if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* SD_STATUS */
448 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
457 ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1);
449 if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 64))
458 if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 64))
450 res = RES_OK;
459 res = RES_OK;
451 }
460 }
452 break;
461 break;
453
462
454 default:
463 default:
455 res = RES_PARERR;
464 res = RES_PARERR;
456 }
465 }
457
466
458 sdcarddeselect (_this);
467 sdcarddeselect (_this);
459
468
460 return res;
469 return res;
461 }
470 }
462
471
463
472
464
473
465 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))
466 {
475 {
467 sdcard->phy = phy;
476 sdcard->phy = phy;
468 sdcard->rcvr_mmc = rcvr_mmc;
477 sdcard->rcvr_mmc = rcvr_mmc;
469 sdcard->xmit_mmc = xmit_mmc;
478 sdcard->xmit_mmc = xmit_mmc;
470 sdcard->setspeed = setspeed;
479 sdcard->setspeed = setspeed;
471 sdcard->getspeed = getspeed;
480 sdcard->getspeed = getspeed;
472 }
481 }
473
482
474 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)
475 {
484 {
476 dev->phy=sdcard;
485 dev->phy=sdcard;
477 dev->select=select;
486 dev->select=select;
478 dev->power = power;
487 dev->power = power;
479 dev->detect = detect;
488 dev->detect = detect;
480 dev->writeprotected = writeprotected;
489 dev->writeprotected = writeprotected;
481 dev->write = sdcarddisk_write;
490 dev->write = sdcarddisk_write;
482 dev->read = sdcarddisk_read;
491 dev->read = sdcarddisk_read;
483 dev->ioctl = sdcarddisk_ioctl;
492 dev->ioctl = sdcarddisk_ioctl;
484 dev->initialize = sdcarddisk_initialize;
493 dev->initialize = sdcarddisk_initialize;
485 dev->status = sdcarddisk_status;
494 dev->status = sdcarddisk_status;
486 }
495 }
487
496
488
497
489
498
490
499
491
500
492
501
493
502
494
503
495
504
496
505
497
506
498
507
499
508
500
509
501
510
502
511
@@ -1,186 +1,186
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=-2;
49 terminal->horizontalSpace=0;
50 terminal->verticalSpace=2;
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 }else {
137 }else {
138 if(buffer[0]=='\t')
138 if(buffer[0]=='\t')
139 {
139 {
140 for(int i=0;i<1;i++)terminal_movecursor(terminal,1);
140 for(int i=0;i<1;i++)terminal_movecursor(terminal,1);
141 }else
141 }else
142 if(terminal->column==0)terminal_clearCurentLine(terminal);
142 if(terminal->column==0)terminal_clearCurentLine(terminal);
143 if(buffer[0]!='\r'){
143 if(buffer[0]!='\r'){
144 terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
144 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);
145 terminal->LCD->paintText(terminal->LCD,buffer,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh),terminal->font,terminal->textColor);
146 terminal_movecursor(terminal,1);
146 terminal_movecursor(terminal,1);
147 }
147 }
148 }
148 }
149 l++;
149 l++;
150 }
150 }
151 return n;
151 return n;
152 }
152 }
153
153
154 int terminal_write(streamdevice* device,void* data,int size, int n)
154 int terminal_write(streamdevice* device,void* data,int size, int n)
155 {
155 {
156 return terminal_writenc((terminal_t*)device->_stream,(char*) data,size*n);
156 return terminal_writenc((terminal_t*)device->_stream,(char*) data,size*n);
157 }
157 }
158
158
159 int terminal_read(streamdevice* device,void* data,int size, int n)
159 int terminal_read(streamdevice* device,void* data,int size, int n)
160 {
160 {
161 return n*size;
161 return n*size;
162 }
162 }
163
163
164 int terminal_setpos(streamdevice* device,int pos)
164 int terminal_setpos(streamdevice* device,int pos)
165 {
165 {
166 return 1;
166 return 1;
167 }
167 }
168
168
169 int terminal_close( streamdevice* device)
169 int terminal_close( streamdevice* device)
170 {
170 {
171 return 1;
171 return 1;
172 }
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
General Comments 0
You need to be logged in to leave comments. Login now