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