@@ -0,0 +1,65 | |||
|
1 | /*------------------------------------------------------------------------------ | |
|
2 | -- This file is a part of the libuc, microcontroler library | |
|
3 | -- Copyright (C) 2012, Alexis Jeandet | |
|
4 | -- | |
|
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 | |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
|
8 | -- (at your option) any later version. | |
|
9 | -- | |
|
10 | -- This program is distributed in the hope that it will be useful, | |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
13 | -- GNU General Public License for more details. | |
|
14 | -- | |
|
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 | |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
|
18 | ------------------------------------------------------------------------------- | |
|
19 | -- Author : Alexis Jeandet | |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
|
21 | -------------------------------------------------------------------------------*/ | |
|
22 | #ifndef SDCARD-SDIO_H | |
|
23 | #define SDCARD-SDIO_H | |
|
24 | #include <blkdevice.h> | |
|
25 | #include <libucstrings.h> | |
|
26 | #include <uhandle.h> | |
|
27 | #include <sdcard.h> | |
|
28 | ||
|
29 | extern void sdcardsdiomake(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)); | |
|
30 | extern void sdcardsdiomakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected); | |
|
31 | ||
|
32 | ||
|
33 | #endif | |
|
34 | ||
|
35 | ||
|
36 | ||
|
37 | ||
|
38 | ||
|
39 | ||
|
40 | ||
|
41 | ||
|
42 | ||
|
43 | ||
|
44 | ||
|
45 | ||
|
46 | ||
|
47 | ||
|
48 | ||
|
49 | ||
|
50 | ||
|
51 | ||
|
52 | ||
|
53 | ||
|
54 | ||
|
55 | ||
|
56 | ||
|
57 | ||
|
58 | ||
|
59 | ||
|
60 | ||
|
61 | ||
|
62 | ||
|
63 | ||
|
64 | ||
|
65 |
@@ -0,0 +1,66 | |||
|
1 | /*------------------------------------------------------------------------------ | |
|
2 | -- This file is a part of the libuc, microcontroler library | |
|
3 | -- Copyright (C) 2012, Alexis Jeandet | |
|
4 | -- | |
|
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 | |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
|
8 | -- (at your option) any later version. | |
|
9 | -- | |
|
10 | -- This program is distributed in the hope that it will be useful, | |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
13 | -- GNU General Public License for more details. | |
|
14 | -- | |
|
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 | |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
|
18 | ------------------------------------------------------------------------------- | |
|
19 | -- Author : Alexis Jeandet | |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
|
21 | -------------------------------------------------------------------------------*/ | |
|
22 | #ifndef SDCARD-SPI_H | |
|
23 | #define SDCARD-SPI_H | |
|
24 | #include <blkdevice.h> | |
|
25 | #include <libucstrings.h> | |
|
26 | #include <uhandle.h> | |
|
27 | #include <sdcard.h> | |
|
28 | ||
|
29 | ||
|
30 | extern 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)); | |
|
31 | extern void sdcardspimakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected); | |
|
32 | ||
|
33 | ||
|
34 | #endif | |
|
35 | ||
|
36 | ||
|
37 | ||
|
38 | ||
|
39 | ||
|
40 | ||
|
41 | ||
|
42 | ||
|
43 | ||
|
44 | ||
|
45 | ||
|
46 | ||
|
47 | ||
|
48 | ||
|
49 | ||
|
50 | ||
|
51 | ||
|
52 | ||
|
53 | ||
|
54 | ||
|
55 | ||
|
56 | ||
|
57 | ||
|
58 | ||
|
59 | ||
|
60 | ||
|
61 | ||
|
62 | ||
|
63 | ||
|
64 | ||
|
65 | ||
|
66 |
@@ -1,319 +1,326 | |||
|
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 | uint32_t OSC0 =8000000; |
|
33 | 33 | uint32_t INTOSC =16000000; |
|
34 | 34 | uint32_t RTCOSC =32768; |
|
35 | 35 | uint32_t currentCpuFreq=0; |
|
36 | 36 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; |
|
37 | 37 | |
|
38 | 38 | LCD_IF_t lcdIF0={ |
|
39 | 39 | .init = &bsp_FSMC_init, |
|
40 | 40 | .writereg = &bsp_lcd0_write_reg, |
|
41 | 41 | .readreg = &bsp_lcd0_read_reg, |
|
42 | 42 | .writeGRAM = &bsp_lcd0_writeGRAM, |
|
43 | 43 | .readGRAM = &bsp_lcd0_readGRAM |
|
44 | 44 | }; |
|
45 | 45 | |
|
46 | 46 | LCD_t lcd0={ |
|
47 | 47 | .interface = &lcdIF0, |
|
48 | 48 | .init = &ili9328init, |
|
49 | 49 | .paint = &ili9328paint, |
|
50 | 50 | .paintText = &ili9328paintText, |
|
51 | 51 | .paintFilRect = &ili9328paintFilRect, |
|
52 | 52 | .refreshenable = &ili9328refreshenable, |
|
53 | 53 | .width= 240, |
|
54 | 54 | .height = 320 |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | volatile int16_t* lcd0_CMD=(volatile int16_t*) (0x60000000 | 0x08000000); |
|
58 | 58 | volatile int16_t* lcd0_DATA=((volatile int16_t*)(0x60000000 | 0x08000002)); |
|
59 | 59 | |
|
60 | 60 | float VREF0 =(float)3.3; |
|
61 | 61 | |
|
62 | 62 | int bsp_init() |
|
63 | 63 | { |
|
64 | 64 | int i=0; |
|
65 | 65 | for(i=0;i<32;i++) |
|
66 | 66 | { |
|
67 | 67 | __opnfiles__[i] = NULL; |
|
68 | 68 | } |
|
69 | 69 | bsp_GPIO_init(); |
|
70 | 70 | bsp_uart_init(); |
|
71 | 71 | bsp_iic_init(); |
|
72 | 72 | bsp_FSMC_init(); |
|
73 | 73 | lcd0.init(&lcd0); |
|
74 | 74 | printf("\r================================================================\n\r"); |
|
75 | 75 | printf("================================================================\n\r"); |
|
76 | 76 | printf(BSP); |
|
77 | 77 | printf(" initialised\n\r"); |
|
78 | 78 | printf("================================================================\n\r"); |
|
79 | 79 | return 1; |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | void bsp_GPIO_init() |
|
83 | 83 | { |
|
84 | 84 | gpio_t gpio1 = gpioopen(LED1); |
|
85 | 85 | gpio_t gpio2 = gpioopen(LED2); |
|
86 | 86 | gpio_t gpio3 = gpioopen(LED3); |
|
87 | 87 | gpiosetspeed(&gpio1,gpiohighspeed); |
|
88 | 88 | gpiosetspeed(&gpio2,gpiohighspeed); |
|
89 | 89 | gpiosetspeed(&gpio3,gpiohighspeed); |
|
90 | 90 | gpiosetdir(&gpio1,gpiooutdir); |
|
91 | 91 | gpiosetdir(&gpio2,gpiooutdir); |
|
92 | 92 | gpiosetdir(&gpio3,gpiooutdir); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void bsp_uart_init() |
|
96 | 96 | { |
|
97 | 97 | if(__opnfiles__[1]==NULL) |
|
98 | 98 | { |
|
99 | 99 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); |
|
100 | 100 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); |
|
101 | 101 | uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PC10,PC11,-1,-1); |
|
102 | 102 | uartmkstreamdev(uart,fd1); |
|
103 | 103 | __opnfiles__[1] = fd1; |
|
104 | 104 | } |
|
105 | 105 | else |
|
106 | 106 | { |
|
107 | 107 | uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PC10,PC11,-1,-1); |
|
108 | 108 | } |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | /* |
|
112 | 112 | D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7 |
|
113 | 113 | D5 PE8 D6 PE9 D7 PE10 D8 PE11 D9 PE12 |
|
114 | 114 | D10 PE13 D11 PE14 D12 PE15 D13 PD8 D14 PD9 |
|
115 | 115 | D15 PD10 |
|
116 | 116 | A0 PF0 = RS FSMC_NE3 PG10 CS FSMC_NWE PD5 W/S |
|
117 | 117 | FSMC_NOE PD4 RD |
|
118 | 118 | */ |
|
119 | 119 | /*-- GPIOs Configuration -----------------------------------------------------*/ |
|
120 | 120 | /* |
|
121 | 121 | +-------------------+--------------------+------------------+------------------+ |
|
122 | 122 | + SRAM pins assignment + |
|
123 | 123 | +-------------------+--------------------+------------------+------------------+ |
|
124 | 124 | | PD0 <-> FSMC_D2 | PE0 <-> FSMC_NBL0 | PF0 <-> FSMC_A0 | PG0 <-> FSMC_A10 | |
|
125 | 125 | | PD1 <-> FSMC_D3 | PE1 <-> FSMC_NBL1 | PF1 <-> FSMC_A1 | PG1 <-> FSMC_A11 | |
|
126 | 126 | | PD4 <-> FSMC_NOE | PE3 <-> FSMC_A19 | PF2 <-> FSMC_A2 | PG2 <-> FSMC_A12 | |
|
127 | 127 | | PD5 <-> FSMC_NWE | PE4 <-> FSMC_A20 | PF3 <-> FSMC_A3 | PG3 <-> FSMC_A13 | |
|
128 | 128 | | PD8 <-> FSMC_D13 | PE7 <-> FSMC_D4 | PF4 <-> FSMC_A4 | PG4 <-> FSMC_A14 | |
|
129 | 129 | | PD9 <-> FSMC_D14 | PE8 <-> FSMC_D5 | PF5 <-> FSMC_A5 | PG5 <-> FSMC_A15 | |
|
130 | 130 | | PD10 <-> FSMC_D15 | PE9 <-> FSMC_D6 | PF12 <-> FSMC_A6 | PG9 <-> FSMC_NE2 | |
|
131 | 131 | | PD11 <-> FSMC_A16 | PE10 <-> FSMC_D7 | PF13 <-> FSMC_A7 |------------------+ |
|
132 | 132 | | PD12 <-> FSMC_A17 | PE11 <-> FSMC_D8 | PF14 <-> FSMC_A8 | |
|
133 | 133 | | PD13 <-> FSMC_A18 | PE12 <-> FSMC_D9 | PF15 <-> FSMC_A9 | |
|
134 | 134 | | PD14 <-> FSMC_D0 | PE13 <-> FSMC_D10 |------------------+ |
|
135 | 135 | | PD15 <-> FSMC_D1 | PE14 <-> FSMC_D11 | |
|
136 | 136 | | | PE15 <-> FSMC_D12 | |
|
137 | 137 | +-------------------+--------------------+ |
|
138 | 138 | */ |
|
139 | 139 | int bsp_FSMC_init() |
|
140 | 140 | { |
|
141 | 141 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) |
|
142 | 142 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) |
|
143 | 143 | |
|
144 | 144 | gpio_t LCD_DBxList[]={ |
|
145 | 145 | PD0 ,PD1 ,PD4 ,PD5 ,PD8 ,PD9 ,PD10,PD11,PD12,PD13,PD14,PD15, |
|
146 | 146 | PE0 ,PE1 ,PE3 ,PE4 ,PE7 ,PE8 ,PE9 ,PE10,PE11,PE12,PE13,PE14, |
|
147 | 147 | PE15,PF0 ,PF1 ,PF2 ,PF3 ,PF4 ,PF5 ,PF12,PF13,PF14,PF15,PG0 , |
|
148 | 148 | PG1 ,PG2 ,PG3 ,PG4 ,PG5 ,PG9 ,PG10 |
|
149 | 149 | }; |
|
150 | 150 | |
|
151 | 151 | for(int i=0;i<43;i++) |
|
152 | 152 | { |
|
153 | 153 | gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); |
|
154 | 154 | LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
155 | 155 | gpiosetconfig(&LCD_DBx); |
|
156 | 156 | GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; |
|
160 | 160 | FSMC_NORSRAMTimingInitTypeDef p; |
|
161 | 161 | |
|
162 | 162 | /* Enable FSMC clock */ |
|
163 | 163 | RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); |
|
164 | 164 | |
|
165 | 165 | /*-- FSMC Configuration ------------------------------------------------------*/ |
|
166 | 166 | /*----------------------- SRAM Bank 3 ----------------------------------------*/ |
|
167 | 167 | /* FSMC_Bank1_NORSRAM3 configuration */ |
|
168 | 168 | p.FSMC_AddressSetupTime = 1; |
|
169 | 169 | p.FSMC_AddressHoldTime = 0; |
|
170 | 170 | p.FSMC_DataSetupTime = getCpuFreq()/14545450 ;// 11; |
|
171 | 171 | p.FSMC_BusTurnAroundDuration = 0; |
|
172 | 172 | p.FSMC_CLKDivision = 0; |
|
173 | 173 | p.FSMC_DataLatency = 0; |
|
174 | 174 | p.FSMC_AccessMode = FSMC_AccessMode_A; |
|
175 | 175 | /* Color LCD configuration ------------------------------------ |
|
176 | 176 | LCD configured as follow: |
|
177 | 177 | - Data/Address MUX = Disable |
|
178 | 178 | - Memory Type = SRAM |
|
179 | 179 | - Data Width = 16bit |
|
180 | 180 | - Write Operation = Enable |
|
181 | 181 | - Extended Mode = Enable |
|
182 | 182 | - Asynchronous Wait = Disable */ |
|
183 | 183 | |
|
184 | 184 | FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3; |
|
185 | 185 | FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; |
|
186 | 186 | FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; |
|
187 | 187 | FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; |
|
188 | 188 | FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; |
|
189 | 189 | FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; |
|
190 | 190 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; |
|
191 | 191 | FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; |
|
192 | 192 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; |
|
193 | 193 | FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; |
|
194 | 194 | FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; |
|
195 | 195 | FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; |
|
196 | 196 | FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; |
|
197 | 197 | FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; |
|
198 | 198 | FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; |
|
199 | 199 | |
|
200 | 200 | FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); |
|
201 | 201 | |
|
202 | 202 | /* Enable FSMC NOR/SRAM Bank1 */ |
|
203 | 203 | FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE); |
|
204 | 204 | |
|
205 | 205 | |
|
206 | 206 | p.FSMC_AddressSetupTime = getCpuFreq()/50000000; |
|
207 | 207 | p.FSMC_AddressHoldTime = 0; |
|
208 | 208 | p.FSMC_DataSetupTime = getCpuFreq()/25000000; |
|
209 | 209 | p.FSMC_BusTurnAroundDuration = 1; |
|
210 | 210 | p.FSMC_CLKDivision = 0; |
|
211 | 211 | p.FSMC_DataLatency = 0; |
|
212 | 212 | p.FSMC_AccessMode = FSMC_AccessMode_A; |
|
213 | 213 | |
|
214 | 214 | FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM2; |
|
215 | 215 | FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; |
|
216 | 216 | FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_PSRAM; |
|
217 | 217 | FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; |
|
218 | 218 | FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; |
|
219 | 219 | FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; |
|
220 | 220 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; |
|
221 | 221 | FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; |
|
222 | 222 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; |
|
223 | 223 | FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; |
|
224 | 224 | FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; |
|
225 | 225 | FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; |
|
226 | 226 | FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; |
|
227 | 227 | FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; |
|
228 | 228 | FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; |
|
229 | 229 | |
|
230 | 230 | FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); |
|
231 | 231 | |
|
232 | 232 | /*!< Enable FSMC Bank1_SRAM2 Bank */ |
|
233 | 233 | FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM2, ENABLE); |
|
234 | 234 | |
|
235 | 235 | |
|
236 | 236 | return 1; |
|
237 | 237 | } |
|
238 | 238 | |
|
239 | 239 | void bsp_spi_init() |
|
240 | 240 | { |
|
241 | 241 | |
|
242 | 242 | } |
|
243 | 243 | |
|
244 | 244 | |
|
245 | 245 | void bsp_iic_init() |
|
246 | 246 | { |
|
247 | 247 | //i2copenandconfig(i2c2,0,400000,PF0,PF1); |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | void bsp_SD_init() |
|
251 | 251 | { |
|
252 | ||
|
252 | gpio_t SDIO_DBxList[]={PC8 ,PC9 ,PC10 ,PC11 ,PC12,PD2}; | |
|
253 | for(int i=0;i<6;i++) | |
|
254 | { | |
|
255 | gpio_t SDIO_DBx = gpioopen(SDIO_DBxList[i]); | |
|
256 | SDIO_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
|
257 | gpiosetconfig(&SDIO_DBx); | |
|
258 | GPIO_PinAFConfig(GPIOGETPORT(SDIO_DBx), (uint8_t)(SDIO_DBx & 0xF), GPIO_AF_SDIO); | |
|
259 | } | |
|
253 | 260 | } |
|
254 | 261 | |
|
255 | 262 | void vs10XXclearXCS(){} |
|
256 | 263 | void vs10XXsetXCS(){} |
|
257 | 264 | int vs10XXDREQ() |
|
258 | 265 | { |
|
259 | 266 | return 1; |
|
260 | 267 | } |
|
261 | 268 | |
|
262 | 269 | |
|
263 | 270 | void bsppowersdcard(char onoff) //always ON |
|
264 | 271 | { |
|
265 | 272 | |
|
266 | 273 | } |
|
267 | 274 | |
|
268 | 275 | char bspsdcardpresent() |
|
269 | 276 | { |
|
270 | 277 | return 0; |
|
271 | 278 | } |
|
272 | 279 | |
|
273 | 280 | char bspsdcardwriteprotected() |
|
274 | 281 | { |
|
275 | 282 | return 0; |
|
276 | 283 | } |
|
277 | 284 | |
|
278 | 285 | void bspsdcardselect(char YESNO) |
|
279 | 286 | { |
|
280 | 287 | |
|
281 | 288 | } |
|
282 | 289 | |
|
283 | 290 | |
|
284 | 291 | void bsp_lcd0_write_reg(uint32_t reg,uint32_t data) |
|
285 | 292 | { |
|
286 | 293 | *lcd0_CMD=(uint16_t)reg; |
|
287 | 294 | *lcd0_DATA=(uint16_t)data; |
|
288 | 295 | } |
|
289 | 296 | |
|
290 | 297 | uint32_t bsp_lcd0_read_reg(uint32_t reg) |
|
291 | 298 | { |
|
292 | 299 | *lcd0_CMD=(uint16_t)reg; |
|
293 | 300 | return (uint16_t)*lcd0_DATA; |
|
294 | 301 | } |
|
295 | 302 | |
|
296 | 303 | void bsp_lcd0_writeGRAM(void* buffer,uint32_t count) |
|
297 | 304 | { |
|
298 | 305 | *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM; |
|
299 | 306 | uint16_t* castedBuff=(uint16_t*)buffer; |
|
300 | 307 | for(int i=0;i<(int)count;i++) |
|
301 | 308 | { |
|
302 | 309 | *lcd0_DATA=castedBuff[i]; |
|
303 | 310 | } |
|
304 | 311 | } |
|
305 | 312 | |
|
306 | 313 | void bsp_lcd0_readGRAM(void* buffer,uint32_t count) |
|
307 | 314 | { |
|
308 | 315 | *lcd0_CMD=(uint16_t)ILI9328_REGISTER_WRITEDATATOGRAM; |
|
309 | 316 | uint16_t* castedBuff=(uint16_t*)buffer; |
|
310 | 317 | castedBuff[0]=*lcd0_DATA; |
|
311 | 318 | for(int i=0;i<(int)count;i++) |
|
312 | 319 | { |
|
313 | 320 | castedBuff[i]=*lcd0_DATA; |
|
314 | 321 | } |
|
315 | 322 | } |
|
316 | 323 | |
|
317 | 324 | |
|
318 | 325 | |
|
319 | 326 |
@@ -1,479 +1,494 | |||
|
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 |
#include |
|
|
22 | #include <sdcard.h> | |
|
23 | #include <sdcard-spi.h> | |
|
24 | ||
|
25 | int sdcardselect (blkdeviceptr _this); | |
|
26 | void sdcarddeselect (blkdeviceptr _this); | |
|
27 | int sdcardwait_ready (sdcardDev* sdcard); | |
|
28 | int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token); | |
|
29 | int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr); | |
|
30 | char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg); | |
|
31 | ||
|
32 | DSTATUS sdcarddisk_status (blkdeviceptr _this); | |
|
33 | DSTATUS sdcarddisk_initialize (blkdeviceptr _this); | |
|
34 | DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count); | |
|
35 | DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count); | |
|
36 | DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff); | |
|
37 | ||
|
23 | 38 | static volatile |
|
24 | 39 | unsigned int Timer1, Timer2; /* 1kHz decrement timer stopped at zero (disk_timerproc()) */ |
|
25 | 40 | |
|
26 | 41 | void sdcarddeselect (blkdeviceptr _this) |
|
27 | 42 | { |
|
28 | 43 | char d; |
|
29 | 44 | _this->select(0); |
|
30 | 45 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO hi-z for multiple slave SPI) */ |
|
31 | 46 | } |
|
32 | 47 | |
|
33 | 48 | int sdcardselect (blkdeviceptr _this) /* 1:OK, 0:Timeout */ |
|
34 | 49 | { |
|
35 | 50 | char d; |
|
36 | 51 | _this->select(1); |
|
37 | 52 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); /* Dummy clock (force DO enabled) */ |
|
38 | 53 | |
|
39 | 54 | if (sdcardwait_ready(((sdcardDev*)_this->phy))) return 1; /* OK */ |
|
40 | 55 | _this->select(0); |
|
41 | 56 | return 0; /* Timeout */ |
|
42 | 57 | } |
|
43 | 58 | |
|
44 | 59 | int sdcardwait_ready (sdcardDev* sdcard) /* 1:OK, 0:Timeout */ |
|
45 | 60 | { |
|
46 | 61 | char d; |
|
47 | 62 | unsigned int tmr; |
|
48 | 63 | volatile unsigned int i=0; |
|
49 | 64 | |
|
50 | 65 | for (tmr = 100000; tmr; tmr--) { /* Wait for ready in timeout of 500ms */ |
|
51 | 66 | sdcard->rcvr_mmc(sdcard->phy,&d, 1); |
|
52 | 67 | if (d == 0xFF) break; |
|
53 | 68 | for(i=0;i<128;i++); |
|
54 | 69 | } |
|
55 | 70 | |
|
56 | 71 | return tmr ? 1 : 0; |
|
57 | 72 | } |
|
58 | 73 | |
|
59 | 74 | int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr) |
|
60 | 75 | { |
|
61 | 76 | char d[2]; |
|
62 | 77 | unsigned int tmr; |
|
63 | 78 | volatile unsigned int i=0; |
|
64 | 79 | for (tmr = 50000; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */ |
|
65 | 80 | sdcard->rcvr_mmc(sdcard->phy,d, 1); |
|
66 | 81 | if (d[0] != 0xFF) break; |
|
67 | 82 | for(i=0;i<64;i++); |
|
68 | 83 | } |
|
69 | 84 | if (d[0] != 0xFE) return 0; /* If not valid data token, return with error */ |
|
70 | 85 | |
|
71 | 86 | sdcard->rcvr_mmc(sdcard->phy,buff, btr); /* Receive the data block into buffer */ |
|
72 | 87 | sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Discard CRC */ |
|
73 | 88 | |
|
74 | 89 | return 1; /* Return with success */ |
|
75 | 90 | } |
|
76 | 91 | |
|
77 | 92 | |
|
78 | 93 | int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token) |
|
79 | 94 | { |
|
80 | 95 | char d[2]; |
|
81 | 96 | |
|
82 | 97 | |
|
83 | 98 | if (!sdcardwait_ready(sdcard)) return 0; |
|
84 | 99 | |
|
85 | 100 | d[0] = token; |
|
86 | 101 | sdcard->xmit_mmc(sdcard->phy,d, 1); /* Xmit a token */ |
|
87 | 102 | if (token != 0xFD) { /* Is it data token? */ |
|
88 | 103 | sdcard->xmit_mmc(sdcard->phy,buff, 512); /* Xmit the 512 byte data block to MMC */ |
|
89 | 104 | sdcard->rcvr_mmc(sdcard->phy,d, 2); /* Xmit dummy CRC (0xFF,0xFF) */ |
|
90 | 105 | sdcard->rcvr_mmc(sdcard->phy,d, 1); /* Receive data response */ |
|
91 | 106 | if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */ |
|
92 | 107 | return 0; |
|
93 | 108 | } |
|
94 | 109 | |
|
95 | 110 | return 1; |
|
96 | 111 | } |
|
97 | 112 | |
|
98 | 113 | |
|
99 | 114 | char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg) |
|
100 | 115 | { |
|
101 | 116 | char n, d, buf[6]; |
|
102 | 117 | |
|
103 | 118 | |
|
104 | 119 | if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */ |
|
105 | 120 | cmd &= 0x7F; |
|
106 | 121 | n = sdcardsend_cmd(_this,CMD55, 0); |
|
107 | 122 | if (n > 1) return n; |
|
108 | 123 | } |
|
109 | 124 | /* Select the card and wait for ready */ |
|
110 | 125 | sdcarddeselect(_this); |
|
111 | 126 | if (!sdcardselect(_this)) return 0xFF; |
|
112 | 127 | /* Send a command packet */ |
|
113 | 128 | buf[0] = 0x40 | cmd; /* Start + Command index */ |
|
114 | 129 | buf[1] = (char)(arg >> 24); /* Argument[31..24] */ |
|
115 | 130 | buf[2] = (char)(arg >> 16); /* Argument[23..16] */ |
|
116 | 131 | buf[3] = (char)(arg >> 8); /* Argument[15..8] */ |
|
117 | 132 | buf[4] = (char)arg; /* Argument[7..0] */ |
|
118 | 133 | n = 0x01; /* Dummy CRC + Stop */ |
|
119 | 134 | if (cmd == CMD0) n = 0x95; /* (valid CRC for CMD0(0)) */ |
|
120 | 135 | if (cmd == CMD8) n = 0x87; /* (valid CRC for CMD8(0x1AA)) */ |
|
121 | 136 | if (cmd == CMD55) n = 0x63; |
|
122 | 137 | buf[5] = n; |
|
123 | 138 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,buf, 6); |
|
124 | 139 | /* Receive command response */ |
|
125 | 140 | if (cmd == CMD12) {((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1);} /* Skip a stuff byte when stop reading */ |
|
126 | 141 | n = 10; /* Wait for a valid response in timeout of 10 attempts */ |
|
127 | 142 | do |
|
128 | 143 | { |
|
129 | 144 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,&d, 1); |
|
130 | 145 | //libucprintf("resp=%d\n\r",d); |
|
131 | 146 | } |
|
132 | 147 | while ((d & 0x80) && --n); |
|
133 | 148 | |
|
134 | 149 | return d; /* Return with the response value */ |
|
135 | 150 | } |
|
136 | 151 | |
|
137 | 152 | |
|
138 | 153 | |
|
139 | 154 | |
|
140 | 155 | DSTATUS sdcarddisk_status (blkdeviceptr _this) |
|
141 | 156 | { |
|
142 | 157 | DSTATUS s = ((sdcardDev*)_this->phy)->Stat; |
|
143 | 158 | char ocr[4]; |
|
144 | 159 | |
|
145 | 160 | |
|
146 | 161 | if ((_this==0) || !_this->detect()) { |
|
147 | 162 | s = STA_NODISK | STA_NOINIT; |
|
148 | 163 | } else { |
|
149 | 164 | s &= ~STA_NODISK; |
|
150 | 165 | if (_this->writeprotected()) /* Check card write protection */ |
|
151 | 166 | s |= STA_PROTECT; |
|
152 | 167 | else |
|
153 | 168 | s &= ~STA_PROTECT; |
|
154 | 169 | if (!(s & STA_NOINIT)) { |
|
155 | 170 | if (sdcardsend_cmd(_this,CMD58, 0)) /* Check if the card is kept initialized */ |
|
156 | 171 | s |= STA_NOINIT; |
|
157 | 172 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
158 | 173 | _this->select(0); |
|
159 | 174 | } |
|
160 | 175 | } |
|
161 | 176 | ((sdcardDev*)_this->phy)->Stat = s; |
|
162 | 177 | |
|
163 | 178 | return s; |
|
164 | 179 | } |
|
165 | 180 | |
|
166 | 181 | |
|
167 | 182 | |
|
168 | 183 | |
|
169 | 184 | DSTATUS sdcarddisk_initialize (blkdeviceptr _this) |
|
170 | 185 | { |
|
171 | 186 | char cmd, ty, ocr[4],ocr2[4]; |
|
172 | 187 | int n; |
|
173 | 188 | const char dummy=0xff; |
|
174 | 189 | uint32_t speed = 1000000; |
|
175 | 190 | |
|
176 | 191 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,350000); |
|
177 | 192 | if (_this==0) return STA_NOINIT; /* Supports only drive 0 */ |
|
178 | 193 | if (((sdcardDev*)_this->phy)->Stat & STA_NODISK) return ((sdcardDev*)_this->phy)->Stat; /* Is card existing in the soket? */ |
|
179 | 194 | |
|
180 | 195 | for (n = 10; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Send 80 dummy clocks */ |
|
181 | 196 | |
|
182 | 197 | ty = 0; |
|
183 | 198 | if (sdcardsend_cmd(_this,CMD0, 0) == 1) { /* Put the card SPI/Idle state */ |
|
184 | 199 | Timer1 = 1000; /* Initialization timeout = 1 sec */ |
|
185 | 200 | if (sdcardsend_cmd(_this,CMD8, 0x1AA) == 1) { /* SDv2? */ |
|
186 | 201 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
187 | 202 | //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get 32 bit return value of R7 resp */ |
|
188 | 203 | if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* Is the card supports vcc of 2.7-3.6V? */ |
|
189 | 204 | while (Timer1 && sdcardsend_cmd(_this,ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */ |
|
190 | 205 | if (Timer1 && sdcardsend_cmd(_this,CMD58, 0) == 0) { /* Check CCS bit in the OCR */ |
|
191 | 206 | //for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); |
|
192 | 207 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
193 | 208 | ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Card id SDv2 */ |
|
194 | 209 | } |
|
195 | 210 | } |
|
196 | 211 | } else { /* Not SDv2 card */ |
|
197 | 212 | if (sdcardsend_cmd(_this,ACMD41, 0) <= 1) { /* SDv1 or MMC? */ |
|
198 | 213 | ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */ |
|
199 | 214 | } else { |
|
200 | 215 | ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */ |
|
201 | 216 | } |
|
202 | 217 | while (Timer1 && sdcardsend_cmd(_this,cmd, 0)) ; /* Wait for end of initialization */ |
|
203 | 218 | if (!Timer1 || sdcardsend_cmd(_this,CMD16, 512) != 0) /* Set block length: 512 */ |
|
204 | 219 | ty = 0; |
|
205 | 220 | } |
|
206 | 221 | } |
|
207 | 222 | ((sdcardDev*)_this->phy)->CardType = ty; /* Card type */ |
|
208 | 223 | |
|
209 | 224 | if (ty) { /* OK */ |
|
210 | 225 | ((sdcardDev*)_this->phy)->Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */ |
|
211 | 226 | sdcardsend_cmd(_this,CMD58, 0); |
|
212 | 227 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr, 4); |
|
213 | 228 | do |
|
214 | 229 | { |
|
215 | 230 | speed+=1000000; |
|
216 | 231 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed); |
|
217 | 232 | sdcardsend_cmd(_this,CMD58, 0); |
|
218 | 233 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ocr2, 4); |
|
219 | 234 | for(n=0;n<4;n++) |
|
220 | 235 | { |
|
221 | 236 | if(ocr[n]!=ocr2[n]) |
|
222 | 237 | { |
|
223 | 238 | n=0; |
|
224 | 239 | break; |
|
225 | 240 | } |
|
226 | 241 | } |
|
227 | 242 | if(speed>(50*1000*1000)) |
|
228 | 243 | { |
|
229 | 244 | break; |
|
230 | 245 | } |
|
231 | 246 | }while(n); |
|
232 | 247 | if(!n) |
|
233 | 248 | { |
|
234 | 249 | speed-=1000000; |
|
235 | 250 | ((sdcardDev*)_this->phy)->setspeed(((sdcardDev*)_this->phy)->phy,speed); |
|
236 | 251 | } |
|
237 | 252 | |
|
238 | 253 | } else { /* Failed */ |
|
239 | 254 | //power_off(); |
|
240 | 255 | ((sdcardDev*)_this->phy)->Stat = STA_NOINIT; |
|
241 | 256 | } |
|
242 | 257 | sdcarddeselect (_this); |
|
243 | 258 | return ((sdcardDev*)_this->phy)->Stat; |
|
244 | 259 | } |
|
245 | 260 | |
|
246 | 261 | |
|
247 | 262 | |
|
248 | 263 | |
|
249 | 264 | |
|
250 | 265 | |
|
251 | 266 | |
|
252 | 267 | |
|
253 | 268 | |
|
254 | 269 | |
|
255 | 270 | DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count) |
|
256 | 271 | { |
|
257 | 272 | DSTATUS s; |
|
258 | 273 | |
|
259 | 274 | |
|
260 | 275 | s = sdcarddisk_status(_this); |
|
261 | 276 | if (s & STA_NOINIT) return RES_NOTRDY; |
|
262 | 277 | if (!count) return RES_PARERR; |
|
263 | 278 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */ |
|
264 | 279 | |
|
265 | 280 | if (count == 1) { /* Single block read */ |
|
266 | 281 | if ((sdcardsend_cmd(_this,CMD17, sector) == 0) && sdcardrcvr_datablock((sdcardDev*)(_this->phy),buff, 512)) |
|
267 | 282 | count = 0; |
|
268 | 283 | } |
|
269 | 284 | else { /* Multiple block read */ |
|
270 | 285 | if (sdcardsend_cmd(_this,CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */ |
|
271 | 286 | do { |
|
272 | 287 | if (!sdcardrcvr_datablock (((sdcardDev*)_this->phy),buff, 512)) break; |
|
273 | 288 | buff += 512; |
|
274 | 289 | } while (--count); |
|
275 | 290 | sdcardsend_cmd(_this,CMD12, 0); /* STOP_TRANSMISSION */ |
|
276 | 291 | } |
|
277 | 292 | } |
|
278 | 293 | sdcarddeselect (_this); |
|
279 | 294 | |
|
280 | 295 | return count ? RES_ERROR : RES_OK; |
|
281 | 296 | } |
|
282 | 297 | |
|
283 | 298 | |
|
284 | 299 | |
|
285 | 300 | |
|
286 | 301 | |
|
287 | 302 | DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count) |
|
288 | 303 | { |
|
289 | 304 | DSTATUS s; |
|
290 | 305 | |
|
291 | 306 | |
|
292 | 307 | s = sdcarddisk_status(_this); |
|
293 | 308 | if (s & STA_NOINIT) return RES_NOTRDY; |
|
294 | 309 | if (s & STA_PROTECT) return RES_WRPRT; |
|
295 | 310 | if (!count) return RES_PARERR; |
|
296 | 311 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */ |
|
297 | 312 | |
|
298 | 313 | if (count == 1) { /* Single block write */ |
|
299 | 314 | if ((sdcardsend_cmd(_this,CMD24, sector) == 0) /* WRITE_BLOCK */ |
|
300 | 315 | && sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFE)) |
|
301 | 316 | count = 0; |
|
302 | 317 | } |
|
303 | 318 | else { /* Multiple block write */ |
|
304 | 319 | if (((sdcardDev*)_this->phy)->CardType & CT_SDC) sdcardsend_cmd(_this,ACMD23, count); |
|
305 | 320 | if (sdcardsend_cmd(_this,CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */ |
|
306 | 321 | do { |
|
307 | 322 | if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),buff, 0xFC)) break; |
|
308 | 323 | buff += 512; |
|
309 | 324 | } while (--count); |
|
310 | 325 | if (!sdcardxmit_datablock (((sdcardDev*)_this->phy),0, 0xFD)) /* STOP_TRAN token */ |
|
311 | 326 | count = 1; |
|
312 | 327 | } |
|
313 | 328 | } |
|
314 | 329 | sdcarddeselect (_this); |
|
315 | 330 | |
|
316 | 331 | return count ? RES_ERROR : RES_OK; |
|
317 | 332 | } |
|
318 | 333 | |
|
319 | 334 | |
|
320 | 335 | DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff) |
|
321 | 336 | { |
|
322 | 337 | DRESULT res; |
|
323 | 338 | const char dummy=0xff; |
|
324 | 339 | char n, csd[16], *ptr = buff; |
|
325 | 340 | uint16_t csize; |
|
326 | 341 | uint32_t *dp, st, ed; |
|
327 | 342 | |
|
328 | 343 | |
|
329 | 344 | if (_this==0) return RES_PARERR; /* Check parameter */ |
|
330 | 345 | if (((sdcardDev*)_this->phy)->Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */ |
|
331 | 346 | |
|
332 | 347 | res = RES_ERROR; |
|
333 | 348 | |
|
334 | 349 | switch (ctrl) { |
|
335 | 350 | case CTRL_SYNC : /* Wait for end of internal write process of the drive */ |
|
336 | 351 | if (sdcardselect (_this)) { |
|
337 | 352 | sdcarddeselect (_this); |
|
338 | 353 | res = RES_OK; |
|
339 | 354 | } |
|
340 | 355 | break; |
|
341 | 356 | |
|
342 | 357 | case GET_SECTOR_COUNT : /* Get drive capacity in unit of sector (DWORD) */ |
|
343 | 358 | if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { |
|
344 | 359 | if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */ |
|
345 | 360 | csize = csd[9] + ((uint16_t)csd[8] << 8) + 1; |
|
346 | 361 | *(uint32_t*)buff = (uint32_t)csize << 10; |
|
347 | 362 | } else { /* SDC ver 1.XX or MMC ver 3 */ |
|
348 | 363 | n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; |
|
349 | 364 | csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 3) << 10) + 1; |
|
350 | 365 | *(uint32_t*)buff = (uint32_t)csize << (n - 9); |
|
351 | 366 | } |
|
352 | 367 | res = RES_OK; |
|
353 | 368 | } |
|
354 | 369 | break; |
|
355 | 370 | |
|
356 | 371 | case GET_SECTOR_SIZE : /* Get sector size in unit of byte (WORD) */ |
|
357 | 372 | *(uint16_t*)buff = 512; |
|
358 | 373 | res = RES_OK; |
|
359 | 374 | break; |
|
360 | 375 | |
|
361 | 376 | case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */ |
|
362 | 377 | if (((sdcardDev*)_this->phy)->CardType & CT_SD2) { /* SDC ver 2.00 */ |
|
363 | 378 | if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* Read SD status */ |
|
364 | 379 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
365 | 380 | if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read partial block */ |
|
366 | 381 | for (n = 64 - 16; n; n--) ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); /* Purge trailing data */ |
|
367 | 382 | *(uint32_t*)buff = 16UL << (csd[10] >> 4); |
|
368 | 383 | res = RES_OK; |
|
369 | 384 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
370 | 385 | } |
|
371 | 386 | } |
|
372 | 387 | } else { /* SDC ver 1.XX or MMC */ |
|
373 | 388 | if ((sdcardsend_cmd(_this,CMD9, 0) == 0) && sdcardrcvr_datablock (((sdcardDev*)_this->phy),csd, 16)) { /* Read CSD */ |
|
374 | 389 | if (((sdcardDev*)_this->phy)->CardType & CT_SD1) { /* SDC ver 1.XX */ |
|
375 | 390 | *(uint32_t*)buff = (((csd[10] & 63) << 1) + ((uint16_t)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1); |
|
376 | 391 | } else { /* MMC */ |
|
377 | 392 | *(uint32_t*)buff = ((uint16_t)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1); |
|
378 | 393 | } |
|
379 | 394 | res = RES_OK; |
|
380 | 395 | } |
|
381 | 396 | } |
|
382 | 397 | break; |
|
383 | 398 | |
|
384 | 399 | case CTRL_ERASE_SECTOR : /* Erase a block of sectors (used when _USE_ERASE == 1) */ |
|
385 | 400 | if (!(((sdcardDev*)_this->phy)->CardType & CT_SDC)) break; /* Check if the card is SDC */ |
|
386 | 401 | if (sdcarddisk_ioctl(_this, MMC_GET_CSD, csd)) break; /* Get CSD */ |
|
387 | 402 | if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */ |
|
388 | 403 | dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */ |
|
389 | 404 | if (!(((sdcardDev*)_this->phy)->CardType & CT_BLOCK)) { |
|
390 | 405 | st *= 512; ed *= 512; |
|
391 | 406 | } |
|
392 | 407 | 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 */ |
|
393 | 408 | res = RES_OK; /* FatFs does not check result of this command */ |
|
394 | 409 | break; |
|
395 | 410 | |
|
396 | 411 | /* Following command are not used by FatFs module */ |
|
397 | 412 | |
|
398 | 413 | case MMC_GET_TYPE : /* Get MMC/SDC type (BYTE) */ |
|
399 | 414 | *ptr = ((sdcardDev*)_this->phy)->CardType; |
|
400 | 415 | res = RES_OK; |
|
401 | 416 | break; |
|
402 | 417 | |
|
403 | 418 | case MMC_GET_CSD : /* Read CSD (16 bytes) */ |
|
404 | 419 | if (sdcardsend_cmd(_this,CMD9, 0) == 0 /* READ_CSD */ |
|
405 | 420 | && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16)) |
|
406 | 421 | res = RES_OK; |
|
407 | 422 | break; |
|
408 | 423 | |
|
409 | 424 | case MMC_GET_CID : /* Read CID (16 bytes) */ |
|
410 | 425 | if (sdcardsend_cmd(_this,CMD10, 0) == 0 /* READ_CID */ |
|
411 | 426 | && sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 16)) |
|
412 | 427 | res = RES_OK; |
|
413 | 428 | break; |
|
414 | 429 | |
|
415 | 430 | case MMC_GET_OCR : /* Read OCR (4 bytes) */ |
|
416 | 431 | if (sdcardsend_cmd(_this,CMD58, 0) == 0) { /* READ_OCR */ |
|
417 | 432 | //for (n = 4; n; n--) *ptr++ = |
|
418 | 433 | ((sdcardDev*)_this->phy)->rcvr_mmc(((sdcardDev*)_this->phy)->phy,ptr, 4); |
|
419 | 434 | res = RES_OK; |
|
420 | 435 | } |
|
421 | 436 | break; |
|
422 | 437 | |
|
423 | 438 | case MMC_GET_SDSTAT : /* Read SD status (64 bytes) */ |
|
424 | 439 | if (sdcardsend_cmd(_this,ACMD13, 0) == 0) { /* SD_STATUS */ |
|
425 | 440 | ((sdcardDev*)_this->phy)->xmit_mmc(((sdcardDev*)_this->phy)->phy,&dummy, 1); |
|
426 | 441 | if (sdcardrcvr_datablock (((sdcardDev*)_this->phy),ptr, 64)) |
|
427 | 442 | res = RES_OK; |
|
428 | 443 | } |
|
429 | 444 | break; |
|
430 | 445 | |
|
431 | 446 | default: |
|
432 | 447 | res = RES_PARERR; |
|
433 | 448 | } |
|
434 | 449 | |
|
435 | 450 | sdcarddeselect (_this); |
|
436 | 451 | |
|
437 | 452 | return res; |
|
438 | 453 | } |
|
439 | 454 | |
|
440 | 455 | |
|
441 | 456 | |
|
442 | 457 | void sdcardmake(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)) |
|
443 | 458 | { |
|
444 | 459 | sdcard->phy = phy; |
|
445 | 460 | sdcard->rcvr_mmc = rcvr_mmc; |
|
446 | 461 | sdcard->xmit_mmc = xmit_mmc; |
|
447 | 462 | sdcard->setspeed = setspeed; |
|
448 | 463 | sdcard->getspeed = getspeed; |
|
449 | 464 | } |
|
450 | 465 | |
|
451 | 466 | void sdcardmakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected) |
|
452 | 467 | { |
|
453 | 468 | dev->phy=sdcard; |
|
454 | 469 | dev->select=select; |
|
455 | 470 | dev->power = power; |
|
456 | 471 | dev->detect = detect; |
|
457 | 472 | dev->writeprotected = writeprotected; |
|
458 | 473 | dev->write = sdcarddisk_write; |
|
459 | 474 | dev->read = sdcarddisk_read; |
|
460 | 475 | dev->ioctl = sdcarddisk_ioctl; |
|
461 | 476 | dev->initialize = sdcarddisk_initialize; |
|
462 | 477 | dev->status = sdcarddisk_status; |
|
463 | 478 | } |
|
464 | 479 | |
|
465 | 480 | |
|
466 | 481 | |
|
467 | 482 | |
|
468 | 483 | |
|
469 | 484 | |
|
470 | 485 | |
|
471 | 486 | |
|
472 | 487 | |
|
473 | 488 | |
|
474 | 489 | |
|
475 | 490 | |
|
476 | 491 | |
|
477 | 492 | |
|
478 | 493 | |
|
479 | 494 |
@@ -1,118 +1,106 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2012, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #ifndef SDCARD_H |
|
23 | 23 | #define SDCARD_H |
|
24 | 24 | #include "blkdevice.h" |
|
25 | 25 | #include <libucstrings.h> |
|
26 | 26 | #include <uhandle.h> |
|
27 | 27 | |
|
28 | 28 | /* MMC/SD command */ |
|
29 | 29 | #define CMD0 (0) /* GO_IDLE_STATE */ |
|
30 | 30 | #define CMD1 (1) /* SEND_OP_COND (MMC) */ |
|
31 | 31 | #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */ |
|
32 | 32 | #define CMD8 (8) /* SEND_IF_COND */ |
|
33 | 33 | #define CMD9 (9) /* SEND_CSD */ |
|
34 | 34 | #define CMD10 (10) /* SEND_CID */ |
|
35 | 35 | #define CMD12 (12) /* STOP_TRANSMISSION */ |
|
36 | 36 | #define ACMD13 (0x80+13) /* SD_STATUS (SDC) */ |
|
37 | 37 | #define CMD16 (16) /* SET_BLOCKLEN */ |
|
38 | 38 | #define CMD17 (17) /* READ_SINGLE_BLOCK */ |
|
39 | 39 | #define CMD18 (18) /* READ_MULTIPLE_BLOCK */ |
|
40 | 40 | #define CMD23 (23) /* SET_BLOCK_COUNT (MMC) */ |
|
41 | 41 | #define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */ |
|
42 | 42 | #define CMD24 (24) /* WRITE_BLOCK */ |
|
43 | 43 | #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */ |
|
44 | 44 | #define CMD32 (32) /* ERASE_ER_BLK_START */ |
|
45 | 45 | #define CMD33 (33) /* ERASE_ER_BLK_END */ |
|
46 | 46 | #define CMD38 (38) /* ERASE */ |
|
47 | 47 | #define CMD55 (55) /* APP_CMD */ |
|
48 | 48 | #define CMD58 (58) /* READ_OCR */ |
|
49 | 49 | |
|
50 | 50 | /* Card type flags (CardType) */ |
|
51 | 51 | #define CT_MMC 0x01 /* MMC ver 3 */ |
|
52 | 52 | #define CT_SD1 0x02 /* SD ver 1 */ |
|
53 | 53 | #define CT_SD2 0x04 /* SD ver 2 */ |
|
54 | 54 | #define CT_SDC (CT_SD1|CT_SD2) /* SD */ |
|
55 | 55 | #define CT_BLOCK 0x08 /* Block addressing */ |
|
56 | 56 | |
|
57 | 57 | struct sdcard_str |
|
58 | 58 | { |
|
59 | 59 | UHANDLE phy; |
|
60 | 60 | void (*rcvr_mmc) (UHANDLE phy,char *buff,uint32_t bc); |
|
61 | 61 | void (*xmit_mmc) (UHANDLE phy,const char *buff,uint32_t bc); |
|
62 | 62 | void (*setspeed) (UHANDLE phy,uint32_t speed); |
|
63 | 63 | uint32_t (*getspeed) (UHANDLE phy); |
|
64 | 64 | DSTATUS Stat; |
|
65 | 65 | char CardType; |
|
66 | 66 | }; |
|
67 | 67 | |
|
68 | 68 | typedef volatile struct sdcard_str sdcardDev; |
|
69 | 69 | |
|
70 | extern void sdcardmake(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)); | |
|
71 | extern void sdcardmakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected); | |
|
70 | //extern void sdcardmake(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)); | |
|
71 | //extern void sdcardmakeblkdev(blkdevice* dev,sdcardDev* sdcard, blkdevselect_t select,blkdevpower_t power,blkdevdetect_t detect,blkdevwriteprotected_t writeprotected); | |
|
72 | 72 | |
|
73 | extern int sdcardselect (blkdeviceptr _this); | |
|
74 | extern void sdcarddeselect (blkdeviceptr _this); | |
|
75 | extern int sdcardwait_ready (sdcardDev* sdcard); | |
|
76 | extern int sdcardxmit_datablock (sdcardDev* sdcard,const char *buff,char token); | |
|
77 | extern int sdcardrcvr_datablock (sdcardDev* sdcard,char *buff,uint32_t btr); | |
|
78 | extern char sdcardsend_cmd (blkdeviceptr _this,char cmd,uint32_t arg); | |
|
79 | ||
|
80 | extern DSTATUS sdcarddisk_status (blkdeviceptr _this); | |
|
81 | extern DSTATUS sdcarddisk_initialize (blkdeviceptr _this); | |
|
82 | extern DRESULT sdcarddisk_read (blkdeviceptr _this,char *buff,uint32_t sector,char count); | |
|
83 | extern DRESULT sdcarddisk_write (blkdeviceptr _this,const char *buff,uint32_t sector,char count); | |
|
84 | extern DRESULT sdcarddisk_ioctl (blkdeviceptr _this,char ctrl,void *buff); | |
|
85 | 73 | |
|
86 | 74 | #endif |
|
87 | 75 | |
|
88 | 76 | |
|
89 | 77 | |
|
90 | 78 | |
|
91 | 79 | |
|
92 | 80 | |
|
93 | 81 | |
|
94 | 82 | |
|
95 | 83 | |
|
96 | 84 | |
|
97 | 85 | |
|
98 | 86 | |
|
99 | 87 | |
|
100 | 88 | |
|
101 | 89 | |
|
102 | 90 | |
|
103 | 91 | |
|
104 | 92 | |
|
105 | 93 | |
|
106 | 94 | |
|
107 | 95 | |
|
108 | 96 | |
|
109 | 97 | |
|
110 | 98 | |
|
111 | 99 | |
|
112 | 100 | |
|
113 | 101 | |
|
114 | 102 | |
|
115 | 103 | |
|
116 | 104 | |
|
117 | 105 | |
|
118 | 106 |
General Comments 0
You need to be logged in to leave comments.
Login now