@@ -1,423 +1,423 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2011, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include "bsp.h" |
|
23 | 23 | #include <streamdevices.h> |
|
24 | 24 | #include <malloc.h> |
|
25 | 25 | #include <gpio.h> |
|
26 | 26 | #include <uart.h> |
|
27 | 27 | #include <stdio.h> |
|
28 | 28 | #include <stm32f4xx_gpio.h> |
|
29 | 29 | #include <stm32f4xx_fsmc.h> |
|
30 | 30 | #include <i2c.h> |
|
31 | 31 | #include <core.h> |
|
32 | 32 | #include <terminal.h> |
|
33 | 33 | #include <sdcard-spi.h> |
|
34 | 34 | #include <sdcard.h> |
|
35 | 35 | #include <mbr.h> |
|
36 | 36 | #include <VS10XX.h> |
|
37 | 37 | |
|
38 | 38 | uint32_t OSC0 =8000000; |
|
39 | 39 | uint32_t INTOSC =16000000; |
|
40 | 40 | uint32_t RTCOSC =32768; |
|
41 | 41 | uint32_t currentCpuFreq=0; |
|
42 | 42 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; |
|
43 | 43 | |
|
44 | 44 | LCD_IF_t lcdIF0={ |
|
45 | 45 | .init = &bsp_FSMC_init, |
|
46 | 46 | .writereg = &bsp_lcd0_write_reg, |
|
47 | 47 | .readreg = &bsp_lcd0_read_reg, |
|
48 | 48 | .writeGRAM = &bsp_lcd0_writeGRAM, |
|
49 | 49 | .readGRAM = &bsp_lcd0_readGRAM |
|
50 | 50 | }; |
|
51 | 51 | |
|
52 | 52 | LCD_t lcd0={ |
|
53 | 53 | .interface = &lcdIF0, |
|
54 | 54 | .init = &ili9328init, |
|
55 | 55 | .paint = &ili9328paint, |
|
56 | 56 | .paintText = &ili9328paintText, |
|
57 | 57 | .paintFilRect = &ili9328paintFilRect, |
|
58 | 58 | .refreshenable = &ili9328refreshenable, |
|
59 | 59 | .width= 240, |
|
60 | 60 | .height = 320 |
|
61 | 61 | }; |
|
62 | 62 | |
|
63 | 63 | terminal_t terminal0; |
|
64 | 64 | |
|
65 | 65 | volatile int8_t* lcd0_CMD=(volatile int8_t*) 0x60000000; |
|
66 | 66 | volatile int8_t* lcd0_DATA=(volatile int8_t*)(0x61FFFFF0); |
|
67 | 67 | |
|
68 | 68 | float VREF0 =(float)3.3; |
|
69 | 69 | volatile vs10XXDev audioCodec0; |
|
70 | 70 | |
|
71 | 71 | int bsp_init() |
|
72 | 72 | { |
|
73 | 73 | int i=0; |
|
74 | 74 | for(i=0;i<__MAX_OPENED_FILES__;i++) |
|
75 | 75 | { |
|
76 | 76 | __opnfiles__[i] = NULL; |
|
77 | 77 | } |
|
78 | 78 | bsp_GPIO_init(); |
|
79 | 79 | bsp_uart_init(); |
|
80 | 80 | bsp_iic_init(); |
|
81 | 81 | bsp_FSMC_init(); |
|
82 | 82 | bsp_GTerm_init(); |
|
83 | 83 | bsp_spi_init(); |
|
84 | 84 | bsp_SD_init(); |
|
85 | 85 | bsp_Audio_init(); |
|
86 | 86 | printf("\r=====================\n\r"); |
|
87 | 87 | printf( "=====================\n\r"); |
|
88 | 88 | printf(BSP); |
|
89 | 89 | printf(" initialised\n\r"); |
|
90 | 90 | printf( "=====================\n\r"); |
|
91 | 91 | return 1; |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void bsp_GPIO_init() |
|
95 | 95 | { |
|
96 | 96 | gpio_t GPIO_Out_init_List[]={LED1,LED2,LCD_RESET,LCD_BACKL,VS1053xCS,VS1053xDCS,VS1053xRESET,SDCARD2CS}; |
|
97 | 97 | for(int i=0;i<8;i++) |
|
98 | 98 | { |
|
99 | 99 | gpio_t GPIO_init = gpioopen(GPIO_Out_init_List[i]); |
|
100 | 100 | GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype; |
|
101 | 101 | gpiosetconfig(&GPIO_init); |
|
102 | 102 | } |
|
103 | 103 | gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD}; |
|
104 | 104 | for(int i=0;i<2;i++) |
|
105 | 105 | { |
|
106 | 106 | gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]); |
|
107 | 107 | GPIO_init |= gpiohighspeed | gpioindir; |
|
108 | 108 | gpiosetconfig(&GPIO_init); |
|
109 | 109 | } |
|
110 | 110 | gpioclr(VS1053xRESET); |
|
111 | 111 | gpioset(VS1053xCS); |
|
112 | 112 | gpioset(VS1053xDCS); |
|
113 | 113 | gpioset(SDCARD2CS); |
|
114 | 114 | gpioclr(LCD_RESET); |
|
115 | 115 | gpioclr(LCD_BACKL); |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | void bsp_uart_init() |
|
119 | 119 | { |
|
120 | 120 | |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | /* |
|
124 | 124 | D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7 |
|
125 | 125 | D5 PE8 D6 PE9 D7 PE10 |
|
126 | 126 | A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S |
|
127 | 127 | FSMC_NOE PD4 RD |
|
128 | 128 | */ |
|
129 | 129 | |
|
130 | 130 | int bsp_FSMC_init() |
|
131 | 131 | { |
|
132 | 132 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) |
|
133 | 133 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) |
|
134 | 134 | |
|
135 | 135 | gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\ |
|
136 | 136 | ,PD4,PD5,PD7,PE4}; |
|
137 | 137 | for(int i=0;i<12;i++) |
|
138 | 138 | { |
|
139 | 139 | gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); |
|
140 | 140 | LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
141 | 141 | gpiosetconfig(&LCD_DBx); |
|
142 | 142 | GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; |
|
146 | 146 | FSMC_NORSRAMTimingInitTypeDef p; |
|
147 | 147 | |
|
148 | 148 | /* Enable FSMC clock */ |
|
149 | 149 | RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); |
|
150 | 150 | |
|
151 | 151 | /*-- FSMC Configuration ------------------------------------------------------*/ |
|
152 | 152 | /*----------------------- SRAM Bank 3 ----------------------------------------*/ |
|
153 | 153 | /* FSMC_Bank1_NORSRAM4 configuration */ |
|
154 | 154 | //p.FSMC_AddressSetupTime = 5; |
|
155 | 155 | //p.FSMC_AddressSetupTime = 1; |
|
156 | 156 | p.FSMC_AddressSetupTime = 3; |
|
157 | 157 | p.FSMC_AddressHoldTime = 3; |
|
158 | 158 | //p.FSMC_DataSetupTime = 9; |
|
159 | 159 | //ili9328 -> data setup time > 10ns |
|
160 | 160 | p.FSMC_DataSetupTime = 1; |
|
161 | 161 | if(getCpuFreq()>100*1000*1000) |
|
162 | 162 | p.FSMC_DataSetupTime = 2;// 11; |
|
163 | 163 | p.FSMC_BusTurnAroundDuration = 0; |
|
164 | 164 | p.FSMC_CLKDivision = 0; |
|
165 | 165 | p.FSMC_DataLatency = 0; |
|
166 | 166 | //ili9328 -> data hold time > 15ns |
|
167 | 167 | if(getCpuFreq()>66*1000*1000) |
|
168 | 168 | p.FSMC_DataLatency = 0; |
|
169 | 169 | p.FSMC_AccessMode = FSMC_AccessMode_A; |
|
170 | 170 | |
|
171 | 171 | FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; |
|
172 | 172 | FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; |
|
173 | 173 | FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; |
|
174 | 174 | FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b; |
|
175 | 175 | FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; |
|
176 | 176 | FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; |
|
177 | 177 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; |
|
178 | 178 | FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; |
|
179 | 179 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; |
|
180 | 180 | FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; |
|
181 | 181 | FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; |
|
182 | 182 | FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; |
|
183 | 183 | FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; |
|
184 | 184 | FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; |
|
185 | 185 | FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; |
|
186 | 186 | |
|
187 | 187 | FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); |
|
188 | 188 | |
|
189 | 189 | FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); |
|
190 | 190 | gpioset(LCD_RESET); |
|
191 | 191 | gpioclr(LCD_RESET); |
|
192 | 192 | delay_100us(500); |
|
193 | 193 | gpioset(LCD_RESET); |
|
194 | 194 | delay_100us(500); |
|
195 | 195 | lcd0.init(&lcd0); |
|
196 | 196 | gpioset(LCD_BACKL); |
|
197 | 197 | return 1; |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | void bsp_spi_init() |
|
201 | 201 | { |
|
202 | gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO}; | |
|
203 | for(int i=0;i<3;i++) | |
|
204 | { | |
|
205 | gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]); | |
|
206 | SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
|
207 | gpiosetconfig(&SPI_DBx); | |
|
208 | GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1); | |
|
209 | } | |
|
210 | spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1); | |
|
202 | // gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO}; | |
|
203 | // for(int i=0;i<3;i++) | |
|
204 | // { | |
|
205 | // gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]); | |
|
206 | // SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
|
207 | // gpiosetconfig(&SPI_DBx); | |
|
208 | // GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1); | |
|
209 | // } | |
|
210 | // spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1); | |
|
211 | 211 | |
|
212 | 212 | gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO}; |
|
213 | 213 | for(int i=0;i<3;i++) |
|
214 | 214 | { |
|
215 | 215 | gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]); |
|
216 | 216 | SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
217 | 217 | gpiosetconfig(&SPI_DBx); |
|
218 | 218 | GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3); |
|
219 | 219 | } |
|
220 | 220 | spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1); |
|
221 | 221 | |
|
222 | 222 | } |
|
223 | 223 | |
|
224 | 224 | |
|
225 | 225 | void bsp_iic_init() |
|
226 | 226 | { |
|
227 | 227 | // i2copenandconfig(i2c2,0,10000,PF0,PF1); |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | |
|
231 | 231 | void bsp_Audio_init() |
|
232 | 232 | { |
|
233 | 233 | vs10XXopen(&audioCodec0,VS1053SPI,vs1052setXCS,vs1052setRST,vs1052setXDCS,vs10XXDREQ); |
|
234 | 234 | } |
|
235 | 235 | |
|
236 | 236 | void bsp_SD_init() |
|
237 | 237 | { |
|
238 | sdcardDev sdcard2; | |
|
239 | blkdevice sdcard2blkdev; | |
|
240 | dikpartition sdcard2Part1; | |
|
241 | sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed); | |
|
242 | sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected); | |
|
243 | sdcard2blkdev.initialize(&sdcard2blkdev); | |
|
244 | mbropen(&sdcard2blkdev,&sdcard2Part1,1); | |
|
238 | // sdcardDev sdcard2; | |
|
239 | // blkdevice sdcard2blkdev; | |
|
240 | // dikpartition sdcard2Part1; | |
|
241 | // sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed); | |
|
242 | // sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected); | |
|
243 | // sdcard2blkdev.initialize(&sdcard2blkdev); | |
|
244 | // mbropen(&sdcard2blkdev,&sdcard2Part1,1); | |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | void vs1052setXCS(char val) |
|
248 | 248 | { |
|
249 | 249 | gpiosetval(VS1053xCS,(int)val); |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | void vs1052setXDCS(char val) |
|
253 | 253 | { |
|
254 | 254 | gpiosetval(VS1053xDCS,(int)val); |
|
255 | 255 | } |
|
256 | 256 | |
|
257 | 257 | void vs1052setRST(char val) |
|
258 | 258 | { |
|
259 | 259 | if(val) |
|
260 | 260 | gpioset(VS1053xRESET); |
|
261 | 261 | else |
|
262 | 262 | gpioclr(VS1053xRESET); |
|
263 | 263 | } |
|
264 | 264 | |
|
265 | 265 | int vs10XXDREQ() |
|
266 | 266 | { |
|
267 | 267 | return gpiogetval(VS1053DREQ); |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | |
|
271 | 271 | void bsppowersdcard(char onoff) //always ON |
|
272 | 272 | { |
|
273 | 273 | |
|
274 | 274 | } |
|
275 | 275 | |
|
276 | 276 | char bspsdcardpresent() |
|
277 | 277 | { |
|
278 | 278 | return gpiogetval(SDCARD2CD); |
|
279 | 279 | } |
|
280 | 280 | |
|
281 | 281 | char bspsdcardwriteprotected() |
|
282 | 282 | { |
|
283 | 283 | return 0; |
|
284 | 284 | } |
|
285 | 285 | |
|
286 | 286 | void bspsdcardselect(char YESNO) |
|
287 | 287 | { |
|
288 | 288 | if(YESNO) |
|
289 | 289 | gpioclr(SDCARD2CS); |
|
290 | 290 | else |
|
291 | 291 | gpioset(SDCARD2CS); |
|
292 | 292 | } |
|
293 | 293 | |
|
294 | 294 | |
|
295 | 295 | void bsp_lcd0_write_reg(uint32_t reg,uint32_t data) |
|
296 | 296 | { |
|
297 | 297 | uint8_t* pt8 = (uint8_t*)(void*)® |
|
298 | 298 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ |
|
299 | 299 | *lcd0_CMD=pt8[3]; |
|
300 | 300 | *lcd0_CMD=pt8[2]; |
|
301 | 301 | pt8 = (uint8_t*)(void*)&data; |
|
302 | 302 | *lcd0_DATA=pt8[3]; |
|
303 | 303 | *lcd0_DATA=pt8[2]; |
|
304 | 304 | #else |
|
305 | 305 | *lcd0_CMD=pt8[1]; |
|
306 | 306 | *lcd0_CMD=pt8[0]; |
|
307 | 307 | pt8 = (uint8_t*)(void*)&data; |
|
308 | 308 | *lcd0_DATA=pt8[1]; |
|
309 | 309 | *lcd0_DATA=pt8[0]; |
|
310 | 310 | #endif |
|
311 | 311 | |
|
312 | 312 | } |
|
313 | 313 | |
|
314 | 314 | uint32_t bsp_lcd0_read_reg(uint32_t reg) |
|
315 | 315 | { |
|
316 | 316 | uint8_t* pt8 = (uint8_t*)(void*)® |
|
317 | 317 | uint32_t DATA=0; |
|
318 | 318 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ |
|
319 | 319 | *lcd0_CMD=pt8[3]; |
|
320 | 320 | *lcd0_CMD=pt8[2]; |
|
321 | 321 | pt8 = (uint8_t*)(void*)&DATA; |
|
322 | 322 | pt8[3]=*lcd0_DATA; |
|
323 | 323 | pt8[2]=*lcd0_DATA; |
|
324 | 324 | #else |
|
325 | 325 | |
|
326 | 326 | *lcd0_CMD=pt8[1]; |
|
327 | 327 | *lcd0_CMD=pt8[0]; |
|
328 | 328 | pt8 = (uint8_t*)(void*)&DATA; |
|
329 | 329 | pt8[1]=*lcd0_DATA; |
|
330 | 330 | pt8[0]=*lcd0_DATA; |
|
331 | 331 | #endif |
|
332 | 332 | |
|
333 | 333 | return DATA; |
|
334 | 334 | } |
|
335 | 335 | |
|
336 | 336 | void bsp_lcd0_writeGRAM(void* buffer,uint32_t count) |
|
337 | 337 | { |
|
338 | 338 | uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM; |
|
339 | 339 | uint8_t* pt8 = (uint8_t*)(void*)® |
|
340 | 340 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ |
|
341 | 341 | *lcd0_CMD=pt8[3]; |
|
342 | 342 | *lcd0_CMD=pt8[2]; |
|
343 | 343 | pt8 = (uint8_t*)(void*)buffer; |
|
344 | 344 | for(int i=0;i<(int)count;i++) |
|
345 | 345 | { |
|
346 | 346 | *lcd0_DATA=pt8[(2*i) +1]; |
|
347 | 347 | *lcd0_DATA=pt8[2*i]; |
|
348 | 348 | } |
|
349 | 349 | #else |
|
350 | 350 | |
|
351 | 351 | *lcd0_CMD=pt8[1]; |
|
352 | 352 | *lcd0_CMD=pt8[0]; |
|
353 | 353 | pt8 = (uint8_t*)(void*)buffer; |
|
354 | 354 | for(int i=0;i<(int)count;i++) |
|
355 | 355 | { |
|
356 | 356 | |
|
357 | 357 | *lcd0_DATA=pt8[(2*i) +1]; |
|
358 | 358 | *lcd0_DATA=pt8[2*i]; |
|
359 | 359 | } |
|
360 | 360 | #endif |
|
361 | 361 | } |
|
362 | 362 | |
|
363 | 363 | void bsp_lcd0_readGRAM(void* buffer,uint32_t count) |
|
364 | 364 | { |
|
365 | 365 | uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM; |
|
366 | 366 | uint8_t* pt8 = (uint8_t*)(void*)® |
|
367 | 367 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ |
|
368 | 368 | *lcd0_CMD=pt8[3]; |
|
369 | 369 | *lcd0_CMD=pt8[2]; |
|
370 | 370 | pt8 = (uint8_t*)(void*)buffer; |
|
371 | 371 | pt8[0] = *lcd0_DATA; |
|
372 | 372 | pt8[0] = *lcd0_DATA; |
|
373 | 373 | for(int i=0;i<(int)count;i++) |
|
374 | 374 | { |
|
375 | 375 | pt8[(2*i) +1]=*lcd0_DATA; |
|
376 | 376 | pt8[2*i]=*lcd0_DATA; |
|
377 | 377 | } |
|
378 | 378 | #else |
|
379 | 379 | |
|
380 | 380 | *lcd0_CMD=pt8[1]; |
|
381 | 381 | *lcd0_CMD=pt8[0]; |
|
382 | 382 | pt8 = (uint8_t*)(void*)buffer; |
|
383 | 383 | pt8[0] = *lcd0_DATA; |
|
384 | 384 | pt8[0] = *lcd0_DATA; |
|
385 | 385 | for(int i=0;i<(int)count;i++) |
|
386 | 386 | { |
|
387 | 387 | pt8[(2*i) +1]=*lcd0_DATA; |
|
388 | 388 | pt8[2*i]=*lcd0_DATA; |
|
389 | 389 | } |
|
390 | 390 | #endif |
|
391 | 391 | } |
|
392 | 392 | |
|
393 | 393 | void bsp_GTerm_init() |
|
394 | 394 | { |
|
395 | 395 | if(__opnfiles__[1]==NULL) |
|
396 | 396 | { |
|
397 | 397 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); |
|
398 | 398 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); |
|
399 | 399 | terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1); |
|
400 | 400 | __opnfiles__[1] = fd1; |
|
401 | 401 | } |
|
402 | 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 | 1 | #include <stdio.h> |
|
2 | 2 | #include <fat32.h> |
|
3 | 3 | #include <gpio.h> |
|
4 | 4 | #include <uart.h> |
|
5 | 5 | #include <stm32f4xx.h> |
|
6 | 6 | #include <bsp.h> |
|
7 | 7 | #include <core.h> |
|
8 | 8 | #include <VS10XX.h> |
|
9 | 9 | #include <bsp.h> |
|
10 | #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 | 18 | extern streamdevice* __opnfiles__[]; |
|
12 | 19 | |
|
13 | 20 | void randBoxesDemo() |
|
14 | 21 | { |
|
15 | 22 | int16_t x,y,w,h,t,r; |
|
16 | 23 | x=rand()%240; |
|
17 | 24 | y=rand()%320; |
|
18 | 25 | w=rand()%(240-x); |
|
19 | 26 | if(x>y) |
|
20 | 27 | r=(rand()%(y))%(320-y); |
|
21 | 28 | else |
|
22 | 29 | r=(rand()%(x))%(240-x); |
|
23 | 30 | h=rand()%(320-y); |
|
24 | 31 | t=rand()%(10); |
|
25 | 32 | ili9328paintFilRect(&lcd0,x,y,w,h,rand(),t,rand()); |
|
26 | 33 | //ili9328paintFilCirc(&lcd0,x,y,r,rand(),t,rand()); |
|
27 | 34 | //delay_100us(10); |
|
28 | 35 | //for(volatile int i=0;i<(1024*2);i++); |
|
29 | 36 | } |
|
30 | 37 | |
|
31 | 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 | 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 | 89 | // printf("hello "); |
|
37 | 90 | // bsp_lcd0_write_reg(1,0x1234); |
|
38 | 91 | // delay_100us(10000); |
|
39 | // randBoxesDemo(); | |
|
92 | // randBoxesDemo(); | |
|
93 | ||
|
40 | 94 | gpioset(LED1); |
|
41 | 95 | delay_100us(10000); |
|
42 | 96 | gpioclr(LED1); |
|
43 | 97 | delay_100us(10000); |
|
44 | 98 | |
|
45 | 99 | } |
|
46 | 100 | printf("hello world\n\r"); |
|
47 | 101 | return 0; |
|
48 | 102 | } |
|
49 | 103 | |
|
50 | 104 | |
|
51 | 105 | |
|
52 | 106 | |
|
53 | 107 | |
|
54 | 108 |
@@ -1,82 +1,83 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2012, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include "mbr.h" |
|
23 | 23 | #include <stdio.h> |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | int mbropen(blkdevice* phy,dikpartition* part,char partNum) |
|
27 | 27 | { |
|
28 | 28 | printf("enter MBR open function\n"); |
|
29 | 29 | char mbr[512]; |
|
30 | 30 | if((part==0) || (phy ==0) || (partNum>4) || (partNum==0) ) return MBRBabArg; |
|
31 | 31 | part->phy = phy; |
|
32 | 32 | if(phy->read(phy,mbr,0,1)!=RES_OK) |
|
33 | 33 | { |
|
34 | 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 | 36 | return MBRReadErr; |
|
36 | 37 | } |
|
37 | 38 | if(((mbr[510]) == (char)0x55) && ((mbr[511]) == (char)0xAA)) |
|
38 | 39 | { |
|
39 | 40 | part->TypeCode = mbr[(512-82+TypeCodeoffset)+partNum*16]; |
|
40 | 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 | 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 | 43 | printf("MBR Ok\n"); |
|
43 | 44 | return MBRnoErr; |
|
44 | 45 | } |
|
45 | 46 | printf("Bad MBR MBR[510]=0x%.2X MBR[511]=0x%.2X!\n",0xff&mbr[510],0xff&mbr[511]); |
|
46 | 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 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2012, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include <sdcard.h> |
|
23 | 23 | #include <sdcard-spi.h> |
|
24 | 24 | #include <stdio.h> |
|
25 | 25 | #include <core.h> |
|
26 | 26 | |
|
27 | 27 | int sdcardselect (blkdeviceptr _this); |
|
28 | 28 | void sdcarddeselect (blkdeviceptr _this); |
|
29 | 29 | int sdcardwait_ready (sdcardDev* sdcard); |
|
30 | 30 | int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token); |
|
31 | 31 | int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr); |
|
32 | 32 | char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg); |
|
33 | 33 | |
|
34 | 34 | DSTATUS sdcarddisk_status (blkdeviceptr _this); |
|
35 | 35 | DSTATUS sdcarddisk_initialize (blkdeviceptr _this); |
|
36 | 36 | DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count); |
|
37 | 37 | DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count); |
|
38 | 38 | DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff); |
|
39 | 39 | |
|
40 | 40 | static volatile |
|
41 | 41 | unsigned int Timer1, Timer2; /* 1kHz decrement timer stopped at zero (disk_timerproc()) */ |
|
42 | 42 | |
|
43 | 43 | void sdcarddeselect (blkdeviceptr _this) |
|
44 | 44 | { |
|
45 | 45 | char d; |
|
46 | 46 | _this->select(0); |
|
47 | 47 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO hi-z for multiple slave SPI) */ |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | int sdcardselect (blkdeviceptr _this) /* 1:OK, 0:Timeout */ |
|
51 | 51 | { |
|
52 | 52 | char d; |
|
53 | 53 | _this->select(1); |
|
54 | 54 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO enabled) */ |
|
55 | 55 | |
|
56 | 56 | if (sdcardwait_ready(((sdcardDev*)_this->phy))) return 1; /* OK */ |
|
57 | 57 | _this->select(0); |
|
58 | 58 | return 0; /* Timeout */ |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | int sdcardwait_ready (sdcardDev* sdcard) /* 1:OK, 0:Timeout */ |
|
62 | 62 | { |
|
63 | 63 | char d=0; |
|
64 | 64 | unsigned int tmr; |
|
65 | 65 | volatile unsigned int i=0; |
|
66 | 66 | |
|
67 |
for (tmr = |
|
|
67 | for (tmr = 500; tmr>0; tmr--) { /* Wait for ready in timeout of 500ms */ | |
|
68 | 68 | sdcard->rcvr_mmc(sdcard->phy,&d, 1); |
|
69 | 69 | if (d == 0xFF) break; |
|
70 | 70 | delay_100us(10); |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | return tmr != 0; |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr) |
|
77 | 77 | { |
|
78 | 78 | char d[2]; |
|
79 | 79 | unsigned int tmr; |
|
80 | 80 | volatile unsigned int i=0; |
|
81 |
|
|
|
81 | for (tmr = 100; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */ | |
|
82 | 82 | sdcard->rcvr_mmc(sdcard->phy,d, 1); |
|
83 | 83 | if (d[0] != 0xFF) break; |
|
84 | for(i=0;i<64;i++); | |
|
84 | delay_100us(10); | |
|
85 | 85 | } |
|
86 | 86 | if (d[0] != 0xFE) return 0; /* If not valid data token, return with error */ |
|
87 | 87 | |
|
88 | 88 | sdcard->rcvr_mmc(sdcard->phy,buff, btr); /* Receive the data block into buffer */ |
|
89 | 89 | sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Discard CRC */ |
|
90 | 90 | |
|
91 | 91 | return 1; /* Return with success */ |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | |
|
95 | 95 | int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token) |
|
96 | 96 | { |
|
97 | 97 | char d[2]; |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | if (!sdcardwait_ready(sdcard)) return 0; |
|
101 | 101 | |
|
102 | 102 | d[0] = token; |
|
103 | 103 | sdcard->xmit_mmc(sdcard->phy,d, 1); /* Xmit a token */ |
|
104 | 104 | if (token != 0xFD) { /* Is it data token? */ |
|
105 | 105 | sdcard->xmit_mmc(sdcard->phy,buff, 512); /* Xmit the 512 byte data block to MMC */ |
|
106 | 106 | sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Xmit dummy CRC (0xFF,0xFF) */ |
|
107 | 107 | sdcard->rcvr_mmc(sdcard->phy,d, 1); /* Receive data response */ |
|
108 | 108 | if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */ |
|
109 | 109 | return 0; |
|
110 | 110 | } |
|
111 | 111 | |
|
112 | 112 | return 1; |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | |
|
116 | 116 | char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg) |
|
117 | 117 | { |
|
118 | 118 | char n, d, buf[6]; |
|
119 | 119 | |
|
120 | 120 | |
|
121 | 121 | if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ |
|
122 | 122 | cmd &= 0x7F; |
|
123 | 123 | n = sdcardsend_cmd(_this,CMD55, 0); |
|
124 | 124 | if (n > 1) return n; |
|
125 | 125 | } |
|
126 | 126 | /* Select the card and wait for ready */ |
|
127 | 127 | sdcarddeselect(_this); |
|
128 | 128 | if (!sdcardselect(_this)) {printf("Can't select SDCARD\n");return 0xFF;} |
|
129 | 129 | /* Send a command packet */ |
|
130 | 130 | buf[0] = 0x40 | cmd; /* Start + Command index */ |
|
131 | 131 | buf[1] = (char)(arg >> 24); /* Argument[31..24] */ |
|
132 | 132 | buf[2] = (char)(arg >> 16); /* Argument[23..16] */ |
|
133 | 133 | buf[3] = (char)(arg >> 8); /* Argument[15..8] */ |
|
134 | 134 | buf[4] = (char)arg; /* Argument[7..0] */ |
|
135 | 135 | n = 0x01; /* Dummy CRC + Stop */ |
|
136 | 136 | if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */ |
|
137 | 137 | if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */ |
|
138 | 138 | if (cmd == CMD55) n = 0x63; |
|
139 | 139 | buf[5] = n; |
|
140 | 140 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,buf, 6); |
|
141 | 141 | /* Receive command response */ |
|
142 | if (cmd == CMD12) {((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);} /* Skip a stuff byte when stop reading */ | |
|
143 | n = 10; /* Wait for a valid response in timeout of 10 attempts */ | |
|
142 | if (cmd == CMD12) | |
|
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 | 147 | do |
|
145 | 148 | { |
|
146 | 149 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); |
|
150 | delay_100us(10); | |
|
147 | 151 | //libucprintf("resp=%d\n\r",d); |
|
148 | } | |
|
149 | while ((d & 0x80) && --n); | |
|
152 | }while ((d & 0x80) && --n); | |
|
150 | 153 | |
|
151 | 154 | return d; /* Return with the response value */ |
|
152 | 155 | } |
|
153 | 156 | |
|
154 | 157 | |
|
155 | 158 | |
|
156 | 159 | |
|
157 | 160 | DSTATUS sdcarddisk_status (blkdeviceptr _this) |
|
158 | 161 | { |
|
159 | 162 | DSTATUS s = ((sdcardDev*)_this->phy)->Stat; |
|
160 | 163 | char ocr[4]; |
|
161 | 164 | |
|
162 | 165 | |
|
163 | 166 | if ((_this==0) || !_this->detect()) { |
|
164 | 167 | s = STA_NODISK | STA_NOINIT; |
|
165 | 168 | } else { |
|
166 | 169 | s &= ~STA_NODISK; |
|
167 | 170 | if (_this->writeprotected()) /* Check card write protection */ |
|
168 | 171 | s |= STA_PROTECT; |
|
169 | 172 | else |
|
170 | 173 | s &= ~STA_PROTECT; |
|
171 | 174 | if (!(s & STA_NOINIT)) { |
|
172 | 175 | if (sdcardsend_cmd(_this,CMD58, 0)) /* Check if the card is kept initialized */ |
|
173 | 176 | s |= STA_NOINIT; |
|
174 | 177 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
175 | 178 | _this->select(0); |
|
176 | 179 | } |
|
177 | 180 | } |
|
178 | 181 | ((sdcardDev*)_this->phy)->Stat = s; |
|
179 | 182 | |
|
180 | 183 | return s; |
|
181 | 184 | } |
|
182 | 185 | |
|
183 | 186 | |
|
184 | 187 | |
|
185 | 188 | |
|
186 | 189 | DSTATUS sdcarddisk_initialize (blkdeviceptr _this) |
|
187 | 190 | { |
|
188 | 191 | char cmd, ty, ocr[4],ocr2[4]; |
|
189 | 192 | int n; |
|
190 | 193 | const char dummy=0xff; |
|
191 | 194 | uint32_t speed = 1000000; |
|
192 | 195 | |
|
193 | 196 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,350000); |
|
194 | 197 | if (_this==0) return STA_NOINIT; /* Supports only drive 0 */ |
|
195 | 198 | /* Is card existing in the soket? */ |
|
196 | 199 | if (((sdcardDev*)_this->phy)->Stat & STA_NODISK) |
|
197 | 200 | { |
|
198 | 201 | printf("No SDCARD\n"); |
|
199 | 202 | return ((sdcardDev*)_this->phy)->Stat; |
|
200 | 203 | } |
|
201 | 204 | printf("SDCARD Detected\n"); |
|
202 | 205 | for (n = 10; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */ |
|
203 | 206 | |
|
204 | 207 | ty = 0; |
|
205 | 208 | if (sdcardsend_cmd(_this,CMD0, 0) == 1) { /* Put the card SPI/Idle state */ |
|
206 | 209 | printf("SDCARD in Idle mode\n"); |
|
207 | 210 | Timer1 = 1000; /* Initialization timeout = 1 sec */ |
|
208 | 211 | if (sdcardsend_cmd(_this,CMD8, 0x1AA) == 1) { /* SDv2? */ |
|
212 | printf("SDCARD V2\n"); | |
|
209 | 213 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
210 | 214 | //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get 32 bit return value of R7 resp */ |
|
211 | 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 | 217 | while (Timer1 && sdcardsend_cmd(_this,ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */ |
|
213 | 218 | if (Timer1 && sdcardsend_cmd(_this,CMD58, 0) == 0) { /* Check CCS bit in the OCR */ |
|
214 | 219 | //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); |
|
215 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); | |
|
220 | printf("Check CCS bit in the OCR:\n"); | |
|
221 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); | |
|
222 | printf("OCR[0]=0x%X\n",ocr[0]); | |
|
216 | 223 | ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Card id SDv2 */ |
|
217 | 224 | } |
|
218 | 225 | } |
|
219 | 226 | } else { /* Not SDv2 card */ |
|
220 | 227 | if (sdcardsend_cmd(_this,ACMD41, 0) <= 1) { /* SDv1 or MMC? */ |
|
221 | 228 | ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */ |
|
222 | 229 | } else { |
|
223 | 230 | ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */ |
|
224 | 231 | } |
|
225 | 232 | while (Timer1 && sdcardsend_cmd(_this,cmd, 0)) ; /* Wait for end of initialization */ |
|
226 | 233 | if (!Timer1 || sdcardsend_cmd(_this,CMD16, 512) != 0) /* Set block length: 512 */ |
|
227 | 234 | ty = 0; |
|
228 | 235 | } |
|
229 | 236 | } |
|
230 | 237 | ((sdcardDev*)_this->phy)->CardType = ty; /* Card type */ |
|
238 | printf("CardType=0x%X\n",ty); | |
|
231 | 239 | |
|
232 | 240 | if (ty) { /* OK */ |
|
233 | 241 | ((sdcardDev*)_this->phy)->Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */ |
|
234 | 242 | sdcardsend_cmd(_this,CMD58, 0); |
|
235 | 243 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
236 | 244 | do |
|
237 | 245 | { |
|
238 | 246 | speed+=1000000; |
|
239 | 247 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed); |
|
240 | 248 | sdcardsend_cmd(_this,CMD58, 0); |
|
241 | 249 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr2, 4); |
|
242 | 250 | for(n=0;n<4;n++) |
|
243 | 251 | { |
|
244 | 252 | if(ocr[n]!=ocr2[n]) |
|
245 | 253 | { |
|
246 | 254 | n=0; |
|
247 | 255 | break; |
|
248 | 256 | } |
|
249 | 257 | } |
|
250 | if(speed>(50*1000*1000)) | |
|
258 | if((speed>(50*1000*1000)) || (speed >= getCpuFreq())) | |
|
251 | 259 | { |
|
252 | 260 | break; |
|
253 | 261 | } |
|
254 | 262 | }while(n); |
|
255 | 263 | if(!n) |
|
256 | 264 | { |
|
257 | 265 | speed-=1000000; |
|
258 | 266 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed); |
|
259 | 267 | } |
|
268 | printf("SDCARD speed = %dMHz\n",speed/1000000); | |
|
260 | 269 | |
|
261 | 270 | } else { /* Failed */ |
|
262 | 271 | //power_off(); |
|
263 | 272 | ((sdcardDev*)_this->phy)->Stat = STA_NOINIT; |
|
264 | 273 | } |
|
265 | 274 | sdcarddeselect (_this); |
|
266 | 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 | 287 | DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count) |
|
279 | 288 | { |
|
280 | 289 | DSTATUS s; |
|
281 | 290 | |
|
282 | 291 | |
|
283 | 292 | s = sdcarddisk_status(_this); |
|
284 | 293 | if (s & STA_NOINIT) return RES_NOTRDY; |
|
285 | 294 | if (!count) return RES_PARERR; |
|
286 | 295 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */ |
|
287 | 296 | |
|
288 | 297 | if (count == 1) { /* Single block read */ |
|
289 | 298 | if ((sdcardsend_cmd(_this,CMD17, sector) == 0) && sdcardrcvr_datablock((sdcardDev*)(_this->phy),buff, 512)) |
|
290 | 299 | count = 0; |
|
291 | 300 | } |
|
292 | 301 | else { /* Multiple block read */ |
|
293 | 302 | if (sdcardsend_cmd(_this,CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */ |
|
294 | 303 | do { |
|
295 | 304 | if (!sdcardrcvr_datablock (((sdcardDev*)_this->phy),buff, 512)) break; |
|
296 | 305 | buff += 512; |
|
297 | 306 | } while (--count); |
|
298 | 307 | sdcardsend_cmd(_this,CMD12, 0); /* STOP_TRANSMISSION */ |
|
299 | 308 | } |
|
300 | 309 | } |
|
301 | 310 | sdcarddeselect (_this); |
|
302 | 311 | |
|
303 | 312 | return count ? RES_ERROR : RES_OK; |
|
304 | 313 | } |
|
305 | 314 | |
|
306 | 315 | |
|
307 | 316 | |
|
308 | 317 | |
|
309 | 318 | |
|
310 | 319 | DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count) |
|
311 | 320 | { |
|
312 | 321 | DSTATUS s; |
|
313 | 322 | |
|
314 | 323 | |
|
315 | 324 | s = sdcarddisk_status(_this); |
|
316 | 325 | if (s & STA_NOINIT) return RES_NOTRDY; |
|
317 | 326 | if (s & STA_PROTECT) return RES_WRPRT; |
|
318 | 327 | if (!count) return RES_PARERR; |
|
319 | 328 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */ |
|
320 | 329 | |
|
321 | 330 | if (count == 1) { /* Single block write */ |
|
322 | 331 | if ((sdcardsend_cmd(_this,CMD24, sector) == 0) /* WRITE_BLOCK */ |
|
323 | 332 | && sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFE)) |
|
324 | 333 | count = 0; |
|
325 | 334 | } |
|
326 | 335 | else { /* Multiple block write */ |
|
327 | 336 | if (((sdcardDev*)_this->phy)->CardType & CT_SDC) sdcardsend_cmd(_this,ACMD23, count); |
|
328 | 337 | if (sdcardsend_cmd(_this,CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */ |
|
329 | 338 | do { |
|
330 | 339 | if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFC)) break; |
|
331 | 340 | buff += 512; |
|
332 | 341 | } while (--count); |
|
333 | 342 | if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),0, 0xFD)) /* STOP_TRAN token */ |
|
334 | 343 | count = 1; |
|
335 | 344 | } |
|
336 | 345 | } |
|
337 | 346 | sdcarddeselect (_this); |
|
338 | 347 | |
|
339 | 348 | return count ? RES_ERROR : RES_OK; |
|
340 | 349 | } |
|
341 | 350 | |
|
342 | 351 | |
|
343 | 352 | DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff) |
|
344 | 353 | { |
|
345 | 354 | DRESULT res; |
|
346 | 355 | const char dummy=0xff; |
|
347 | 356 | char n, csd[16], *ptr = buff; |
|
348 | 357 | uint16_t csize; |
|
349 | 358 | uint32_t *dp, st, ed; |
|
350 | 359 | |
|
351 | 360 | |
|
352 | 361 | if (_this==0) return RES_PARERR; /* Check parameter */ |
|
353 | 362 | if (((sdcardDev*)_this->phy)->Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */ |
|
354 | 363 | |
|
355 | 364 | res = RES_ERROR; |
|
356 | 365 | |
|
357 | 366 | switch (ctrl) { |
|
358 | 367 | case CTRL_SYNC : /* Wait for end of internal write process of the drive */ |
|
359 | 368 | if (sdcardselect (_this)) { |
|
360 | 369 | sdcarddeselect (_this); |
|
361 | 370 | res = RES_OK; |
|
362 | 371 | } |
|
363 | 372 | break; |
|
364 | 373 | |
|
365 | 374 | case GET_SECTOR_COUNT : /* Get drive capacity in unit of sector (DWORD) */ |
|
366 | 375 | if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { |
|
367 | 376 | if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */ |
|
368 | 377 | csize = csd[9] + ((uint16_t)csd[8] << 8) + 1; |
|
369 | 378 | *(uint32_t*)buff = (uint32_t)csize << 10; |
|
370 | 379 | } else { /* SDC ver 1.XX or MMC ver 3 */ |
|
371 | 380 | n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; |
|
372 | 381 | csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 3) << 10) + 1; |
|
373 | 382 | *(uint32_t*)buff = (uint32_t)csize << (n - 9); |
|
374 | 383 | } |
|
375 | 384 | res = RES_OK; |
|
376 | 385 | } |
|
377 | 386 | break; |
|
378 | 387 | |
|
379 | 388 | case GET_SECTOR_SIZE : /* Get sector size in unit of byte (WORD) */ |
|
380 | 389 | *(uint16_t*)buff = 512; |
|
381 | 390 | res = RES_OK; |
|
382 | 391 | break; |
|
383 | 392 | |
|
384 | 393 | case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */ |
|
385 | 394 | if (((sdcardDev*)_this->phy)->CardType & CT_SD2) { /* SDC ver 2.00 */ |
|
386 | 395 | if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* Read SD status */ |
|
387 | 396 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
388 | 397 | if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read partial block */ |
|
389 | 398 | for (n = 64 - 16; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Purge trailing data */ |
|
390 | 399 | *(uint32_t*)buff = 16UL << (csd[10] >> 4); |
|
391 | 400 | res = RES_OK; |
|
392 | 401 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
393 | 402 | } |
|
394 | 403 | } |
|
395 | 404 | } else { /* SDC ver 1.XX or MMC */ |
|
396 | 405 | if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read CSD */ |
|
397 | 406 | if (((sdcardDev*)_this->phy)->CardType & CT_SD1) { /* SDC ver 1.XX */ |
|
398 | 407 | *(uint32_t*)buff = (((csd[10] & 63) << 1) + ((uint16_t)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1); |
|
399 | 408 | } else { /* MMC */ |
|
400 | 409 | *(uint32_t*)buff = ((uint16_t)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1); |
|
401 | 410 | } |
|
402 | 411 | res = RES_OK; |
|
403 | 412 | } |
|
404 | 413 | } |
|
405 | 414 | break; |
|
406 | 415 | |
|
407 | 416 | case CTRL_ERASE_SECTOR : /* Erase a block of sectors (used when _USE_ERASE == 1) */ |
|
408 | 417 | if (!(((sdcardDev*)_this->phy)->CardType & CT_SDC)) break; /* Check if the card is SDC */ |
|
409 | 418 | if (sdcarddisk_ioctl(_this, MMC_GET_CSD, csd)) break; /* Get CSD */ |
|
410 | 419 | if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */ |
|
411 | 420 | dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */ |
|
412 | 421 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) { |
|
413 | 422 | st *= 512; ed *= 512; |
|
414 | 423 | } |
|
415 | 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 | 425 | res = RES_OK; /* FatFs does not check result of this command */ |
|
417 | 426 | break; |
|
418 | 427 | |
|
419 | 428 | /* Following command are not used by FatFs module */ |
|
420 | 429 | |
|
421 | 430 | case MMC_GET_TYPE : /* Get MMC/SDC type (BYTE) */ |
|
422 | 431 | *ptr = ((sdcardDev*)_this->phy)->CardType; |
|
423 | 432 | res = RES_OK; |
|
424 | 433 | break; |
|
425 | 434 | |
|
426 | 435 | case MMC_GET_CSD : /* Read CSD (16 bytes) */ |
|
427 | 436 | if (sdcardsend_cmd(_this,CMD9, 0) == 0 /* READ_CSD */ |
|
428 | 437 | && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16)) |
|
429 | 438 | res = RES_OK; |
|
430 | 439 | break; |
|
431 | 440 | |
|
432 | 441 | case MMC_GET_CID : /* Read CID (16 bytes) */ |
|
433 | 442 | if (sdcardsend_cmd(_this,CMD10, 0) == 0 /* READ_CID */ |
|
434 | 443 | && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16)) |
|
435 | 444 | res = RES_OK; |
|
436 | 445 | break; |
|
437 | 446 | |
|
438 | 447 | case MMC_GET_OCR : /* Read OCR (4 bytes) */ |
|
439 | 448 | if (sdcardsend_cmd(_this,CMD58, 0) == 0) { /* READ_OCR */ |
|
440 | 449 | //for (n = 4; n; n--) *ptr++ = |
|
441 | 450 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ptr, 4); |
|
442 | 451 | res = RES_OK; |
|
443 | 452 | } |
|
444 | 453 | break; |
|
445 | 454 | |
|
446 | 455 | case MMC_GET_SDSTAT : /* Read SD status (64 bytes) */ |
|
447 | 456 | if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* SD_STATUS */ |
|
448 | 457 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
449 | 458 | if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 64)) |
|
450 | 459 | res = RES_OK; |
|
451 | 460 | } |
|
452 | 461 | break; |
|
453 | 462 | |
|
454 | 463 | default: |
|
455 | 464 | res = RES_PARERR; |
|
456 | 465 | } |
|
457 | 466 | |
|
458 | 467 | sdcarddeselect (_this); |
|
459 | 468 | |
|
460 | 469 | return res; |
|
461 | 470 | } |
|
462 | 471 | |
|
463 | 472 | |
|
464 | 473 | |
|
465 | 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 | 476 | sdcard->phy = phy; |
|
468 | 477 | sdcard->rcvr_mmc = rcvr_mmc; |
|
469 | 478 | sdcard->xmit_mmc = xmit_mmc; |
|
470 | 479 | sdcard->setspeed = setspeed; |
|
471 | 480 | sdcard->getspeed = getspeed; |
|
472 | 481 | } |
|
473 | 482 | |
|
474 | 483 | void sdcardspimakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected) |
|
475 | 484 | { |
|
476 | 485 | dev->phy=sdcard; |
|
477 | 486 | dev->select=select; |
|
478 | 487 | dev->power = power; |
|
479 | 488 | dev->detect = detect; |
|
480 | 489 | dev->writeprotected = writeprotected; |
|
481 | 490 | dev->write = sdcarddisk_write; |
|
482 | 491 | dev->read = sdcarddisk_read; |
|
483 | 492 | dev->ioctl = sdcarddisk_ioctl; |
|
484 | 493 | dev->initialize = sdcarddisk_initialize; |
|
485 | 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 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2013, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include <widget.h> |
|
23 | 23 | #include <terminal.h> |
|
24 | 24 | #include <stdint.h> |
|
25 | 25 | #include <stdio.h> |
|
26 | 26 | #include <gpio.h> |
|
27 | 27 | #include <core.h> |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | #define CHARXPOS(terminal,charwidth) (((terminal)->column * charwidth) + (terminal)->Xpos) |
|
31 | 31 | #define CHARYPOS(terminal,charheight) ((terminal)->line * charheight + (terminal)->Ypos+charheight) |
|
32 | 32 | |
|
33 | 33 | streamdevice_ops TERMINAL_OPS= |
|
34 | 34 | { |
|
35 | 35 | .write = &terminal_write, |
|
36 | 36 | .read = &terminal_read, |
|
37 | 37 | .setpos= &terminal_setpos, |
|
38 | 38 | .close = NULL |
|
39 | 39 | }; |
|
40 | 40 | |
|
41 | 41 | int terminal_init(terminal_t* terminal,LCD_t* LCD,sFONT* font,streamdevice* strdev) |
|
42 | 42 | { |
|
43 | 43 | if((LCD!=NULL) && (font!=NULL) && (strdev!=NULL) && (terminal!=NULL)) |
|
44 | 44 | { |
|
45 | 45 | terminal->LCD=LCD; |
|
46 | 46 | terminal->font=font; |
|
47 | 47 | terminal->line = 0; |
|
48 | 48 | terminal->column = 0; |
|
49 |
terminal->horizontalSpace= |
|
|
50 |
terminal->verticalSpace= |
|
|
49 | terminal->horizontalSpace=0; | |
|
50 | terminal->verticalSpace=0; | |
|
51 | 51 | int charw=terminal->font->Width + terminal->horizontalSpace; |
|
52 | 52 | int charh=terminal->font->Height + terminal->verticalSpace; |
|
53 | 53 | terminal->textColor=0xFFFF; |
|
54 | 54 | terminal->backgroundColor=0x0000; |
|
55 | 55 | terminal_setgeometry(terminal,5,5,terminal->LCD->width-(10),terminal->LCD->height-(10)); |
|
56 | 56 | |
|
57 | 57 | strdev->_stream = (UHANDLE)terminal; |
|
58 | 58 | strdev->ops = &TERMINAL_OPS; |
|
59 | 59 | strdev->streamPt = 0; |
|
60 | 60 | terminal_clear(terminal); |
|
61 | 61 | terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor); |
|
62 | 62 | return 1; |
|
63 | 63 | } |
|
64 | 64 | return 0; |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | void terminal_setgeometry(terminal_t* terminal,uint16_t Xpos,uint16_t Ypos,uint16_t width,uint16_t height) |
|
68 | 68 | { |
|
69 | 69 | int charw=terminal->font->Width + terminal->horizontalSpace; |
|
70 | 70 | int charh=terminal->font->Height + terminal->verticalSpace; |
|
71 | 71 | if(terminal->LCD->height<(Ypos+height)) |
|
72 | 72 | return; |
|
73 | 73 | if(terminal->LCD->width<(Xpos+width)) |
|
74 | 74 | return; |
|
75 | 75 | terminal->Xpos = Xpos; |
|
76 | 76 | terminal->Ypos = Ypos; |
|
77 | 77 | terminal->height = height; |
|
78 | 78 | terminal->width = width; |
|
79 | 79 | terminal->lineCount = terminal->height/charh; |
|
80 | 80 | terminal->columnCount = (terminal->width/charw)-1; |
|
81 | 81 | terminal_clear(terminal); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | void terminal_clear(terminal_t* terminal) |
|
86 | 86 | { |
|
87 | 87 | terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,terminal->Ypos,terminal->width,terminal->height,terminal->backgroundColor,0,terminal->backgroundColor); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | void terminal_setbackgroundColor(terminal_t* terminal, uint32_t backgrooundColor) |
|
91 | 91 | { |
|
92 | 92 | terminal->backgroundColor = backgrooundColor; |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void terminal_settextColor(terminal_t* terminal, uint32_t textColor) |
|
96 | 96 | { |
|
97 | 97 | terminal->textColor = textColor; |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | void terminal_movecursor(terminal_t* terminal,int n) |
|
102 | 102 | { |
|
103 | 103 | int charw=terminal->font->Width + terminal->horizontalSpace; |
|
104 | 104 | int charh=terminal->font->Height + terminal->verticalSpace; |
|
105 | 105 | terminal->column += n; |
|
106 | 106 | if(terminal->column>terminal->columnCount) |
|
107 | 107 | { |
|
108 | 108 | terminal->line += (terminal->column)/terminal->columnCount; |
|
109 | 109 | terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal,charh)-charh,charw*(terminal->columnCount-terminal->column),charh,terminal->backgroundColor,0,terminal->backgroundColor); |
|
110 | 110 | terminal->line = terminal->line % terminal->lineCount; |
|
111 | 111 | terminal->column = terminal->column % terminal->columnCount; |
|
112 | 112 | } |
|
113 | 113 | terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal,charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor); |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | void terminal_clearCurentLine(terminal_t* terminal) |
|
117 | 117 | { |
|
118 | 118 | int charw=terminal->font->Width + terminal->horizontalSpace; |
|
119 | 119 | int charh=terminal->font->Height + terminal->verticalSpace; |
|
120 | 120 | terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,CHARYPOS(terminal, charh)-charh,terminal->width,charh,terminal->backgroundColor,0,terminal->backgroundColor); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | int terminal_writenc(terminal_t* terminal,char* data, int n) |
|
124 | 124 | { |
|
125 | 125 | int charw=terminal->font->Width + terminal->horizontalSpace; |
|
126 | 126 | int charh=terminal->font->Height + terminal->verticalSpace; |
|
127 | 127 | int l=0; |
|
128 | 128 | char buffer[2]=" "; |
|
129 | 129 | while(l<n) |
|
130 | 130 | { |
|
131 | 131 | |
|
132 | 132 | buffer[0]=data[l]; |
|
133 | 133 | if(buffer[0]=='\n') |
|
134 | 134 | { |
|
135 | 135 | terminal->line= (terminal->line+1) % terminal->lineCount; |
|
136 | 136 | terminal->column = 0; |
|
137 | 137 | }else { |
|
138 | 138 | if(buffer[0]=='\t') |
|
139 | 139 | { |
|
140 | 140 | for(int i=0;i<1;i++)terminal_movecursor(terminal,1); |
|
141 | 141 | }else |
|
142 | 142 | if(terminal->column==0)terminal_clearCurentLine(terminal); |
|
143 | 143 | if(buffer[0]!='\r'){ |
|
144 | 144 | terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor); |
|
145 | 145 | terminal->LCD->paintText(terminal->LCD,buffer,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh),terminal->font,terminal->textColor); |
|
146 | 146 | terminal_movecursor(terminal,1); |
|
147 | 147 | } |
|
148 | 148 | } |
|
149 | 149 | l++; |
|
150 | 150 | } |
|
151 | 151 | return n; |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | int terminal_write(streamdevice* device,void* data,int size, int n) |
|
155 | 155 | { |
|
156 | 156 | return terminal_writenc((terminal_t*)device->_stream,(char*) data,size*n); |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | int terminal_read(streamdevice* device,void* data,int size, int n) |
|
160 | 160 | { |
|
161 | 161 | return n*size; |
|
162 | 162 | } |
|
163 | 163 | |
|
164 | 164 | int terminal_setpos(streamdevice* device,int pos) |
|
165 | 165 | { |
|
166 | 166 | return 1; |
|
167 | 167 | } |
|
168 | 168 | |
|
169 | 169 | int terminal_close( streamdevice* device) |
|
170 | 170 | { |
|
171 | 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